diff mbox series

[v2] drm/amdgpu: Fix possible NULL pointer dereference

Message ID 20241029101839.2605713-1-lihuafei1@huawei.com (mailing list archive)
State New, archived
Headers show
Series [v2] drm/amdgpu: Fix possible NULL pointer dereference | expand

Commit Message

Li Huafei Oct. 29, 2024, 10:18 a.m. UTC
Fix the potential NULL pointer dereference on mem_ranges in
amdgpu_discovery_get_nps_info(). Additionally, assign the output
parameters nps_type and range_cnt after the kvzalloc() call to prevent
modifying the output parameters in case of an error return.

Fixes: b194d21b9bcc ("drm/amdgpu: Use NPS ranges from discovery table")
Signed-off-by: Li Huafei <lihuafei1@huawei.com>
---
Changes in v2:
 - kvzalloc() call uses 'nps_info->v1.count' instead of '*range_cnt'
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

Comments

Lazar, Lijo Oct. 29, 2024, 4:04 a.m. UTC | #1
On 10/29/2024 3:48 PM, Li Huafei wrote:
> Fix the potential NULL pointer dereference on mem_ranges in
> amdgpu_discovery_get_nps_info(). Additionally, assign the output
> parameters nps_type and range_cnt after the kvzalloc() call to prevent
> modifying the output parameters in case of an error return.
> 
> Fixes: b194d21b9bcc ("drm/amdgpu: Use NPS ranges from discovery table")
> Signed-off-by: Li Huafei <lihuafei1@huawei.com>

Reviewed-by: Lijo Lazar <lijo.lazar@amd.com>

Thanks,
Lijo

> ---
> Changes in v2:
>  - kvzalloc() call uses 'nps_info->v1.count' instead of '*range_cnt'
> ---
>  drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
> index 4bd61c169ca8..9a0b1b208a9d 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
> @@ -1757,11 +1757,13 @@ int amdgpu_discovery_get_nps_info(struct amdgpu_device *adev,
>  
>  	switch (le16_to_cpu(nps_info->v1.header.version_major)) {
>  	case 1:
> -		*nps_type = nps_info->v1.nps_type;
> -		*range_cnt = nps_info->v1.count;
>  		mem_ranges = kvzalloc(
> -			*range_cnt * sizeof(struct amdgpu_gmc_memrange),
> +			nps_info->v1.count * sizeof(struct amdgpu_gmc_memrange),
>  			GFP_KERNEL);
> +		if (!mem_ranges)
> +			return -ENOMEM;
> +		*nps_type = nps_info->v1.nps_type;
> +		*range_cnt = nps_info->v1.count;
>  		for (i = 0; i < *range_cnt; i++) {
>  			mem_ranges[i].base_address =
>  				nps_info->v1.instance_info[i].base_address;
Christophe JAILLET Oct. 29, 2024, 8:18 a.m. UTC | #2
Le 29/10/2024 à 11:18, Li Huafei a écrit :
> Fix the potential NULL pointer dereference on mem_ranges in
> amdgpu_discovery_get_nps_info(). Additionally, assign the output
> parameters nps_type and range_cnt after the kvzalloc() call to prevent
> modifying the output parameters in case of an error return.
> 
> Fixes: b194d21b9bcc ("drm/amdgpu: Use NPS ranges from discovery table")
> Signed-off-by: Li Huafei <lihuafei1@huawei.com>
> ---
> Changes in v2:
>   - kvzalloc() call uses 'nps_info->v1.count' instead of '*range_cnt'
> ---
>   drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 8 +++++---
>   1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
> index 4bd61c169ca8..9a0b1b208a9d 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
> @@ -1757,11 +1757,13 @@ int amdgpu_discovery_get_nps_info(struct amdgpu_device *adev,
>   
>   	switch (le16_to_cpu(nps_info->v1.header.version_major)) {
>   	case 1:
> -		*nps_type = nps_info->v1.nps_type;
> -		*range_cnt = nps_info->v1.count;
>   		mem_ranges = kvzalloc(

Not strictly related to your fix, but this could also be kvcalloc() to 
avoid potential overflow in the multiplication.

CJ

> -			*range_cnt * sizeof(struct amdgpu_gmc_memrange),
> +			nps_info->v1.count * sizeof(struct amdgpu_gmc_memrange),
>   			GFP_KERNEL);
> +		if (!mem_ranges)
> +			return -ENOMEM;
> +		*nps_type = nps_info->v1.nps_type;
> +		*range_cnt = nps_info->v1.count;
>   		for (i = 0; i < *range_cnt; i++) {
>   			mem_ranges[i].base_address =
>   				nps_info->v1.instance_info[i].base_address;
Li Huafei Oct. 29, 2024, 12:36 p.m. UTC | #3
On 2024/10/29 16:18, Christophe JAILLET wrote:
> Le 29/10/2024 à 11:18, Li Huafei a écrit :
>> Fix the potential NULL pointer dereference on mem_ranges in
>> amdgpu_discovery_get_nps_info(). Additionally, assign the output
>> parameters nps_type and range_cnt after the kvzalloc() call to prevent
>> modifying the output parameters in case of an error return.
>>
>> Fixes: b194d21b9bcc ("drm/amdgpu: Use NPS ranges from discovery table")
>> Signed-off-by: Li Huafei <lihuafei1@huawei.com>
>> ---
>> Changes in v2:
>>   - kvzalloc() call uses 'nps_info->v1.count' instead of '*range_cnt'
>> ---
>>   drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 8 +++++---
>>   1 file changed, 5 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
>> b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
>> index 4bd61c169ca8..9a0b1b208a9d 100644
>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
>> @@ -1757,11 +1757,13 @@ int amdgpu_discovery_get_nps_info(struct
>> amdgpu_device *adev,
>>         switch (le16_to_cpu(nps_info->v1.header.version_major)) {
>>       case 1:
>> -        *nps_type = nps_info->v1.nps_type;
>> -        *range_cnt = nps_info->v1.count;
>>           mem_ranges = kvzalloc(
> 
> Not strictly related to your fix, but this could also be kvcalloc() to
> avoid potential overflow in the multiplication.
> 

Good suggestion! I added this modification in v3:

v3: https://lore.kernel.org/lkml/20241029202758.2764535-1-lihuafei1@huawei.com/T/#u

Lijo, I've added your Reviewed-by tag in v3. If you have any questions about the kvcalloc() modification, please let me know.

Thanks,
Huafei
> CJ
> 
>> -            *range_cnt * sizeof(struct amdgpu_gmc_memrange),
>> +            nps_info->v1.count * sizeof(struct amdgpu_gmc_memrange),
>>               GFP_KERNEL);
>> +        if (!mem_ranges)
>> +            return -ENOMEM;
>> +        *nps_type = nps_info->v1.nps_type;
>> +        *range_cnt = nps_info->v1.count;
>>           for (i = 0; i < *range_cnt; i++) {
>>               mem_ranges[i].base_address =
>>                   nps_info->v1.instance_info[i].base_address;
> 
> 
> .
diff mbox series

Patch

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
index 4bd61c169ca8..9a0b1b208a9d 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
@@ -1757,11 +1757,13 @@  int amdgpu_discovery_get_nps_info(struct amdgpu_device *adev,
 
 	switch (le16_to_cpu(nps_info->v1.header.version_major)) {
 	case 1:
-		*nps_type = nps_info->v1.nps_type;
-		*range_cnt = nps_info->v1.count;
 		mem_ranges = kvzalloc(
-			*range_cnt * sizeof(struct amdgpu_gmc_memrange),
+			nps_info->v1.count * sizeof(struct amdgpu_gmc_memrange),
 			GFP_KERNEL);
+		if (!mem_ranges)
+			return -ENOMEM;
+		*nps_type = nps_info->v1.nps_type;
+		*range_cnt = nps_info->v1.count;
 		for (i = 0; i < *range_cnt; i++) {
 			mem_ranges[i].base_address =
 				nps_info->v1.instance_info[i].base_address;