public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Re: [PATCH] drm/amdgpu: Fix possible NULL pointer dereference
  2024-10-28 21:59 [PATCH] drm/amdgpu: Fix possible NULL pointer dereference Li Huafei
@ 2024-10-28 14:33 ` Lazar, Lijo
  2024-10-29  1:17   ` Li Huafei
  0 siblings, 1 reply; 3+ messages in thread
From: Lazar, Lijo @ 2024-10-28 14:33 UTC (permalink / raw)
  To: Li Huafei, alexander.deucher
  Cc: christian.koenig, Xinhui.Pan, airlied, simona, Hawking.Zhang,
	yifan1.zhang, Likun.Gao, Tim.Huang, pratap.nirujogi,
	victorchengchi.lu, Jun.Ma2, le.ma, amd-gfx, dri-devel,
	linux-kernel



On 10/29/2024 3:29 AM, 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>
> ---
>  drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
> index 4bd61c169ca8..1b1aedebb3c2 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;

This one is used in the allocation call below. If you prefer to not
assign any of the out params before memory allocation is successful,
then use nps_info->v1.count in the alloc call.

Thanks for adding the alloc fail check.

Thanks,
Lijo
>  		mem_ranges = kvzalloc(
>  			*range_cnt * 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;

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] drm/amdgpu: Fix possible NULL pointer dereference
@ 2024-10-28 21:59 Li Huafei
  2024-10-28 14:33 ` Lazar, Lijo
  0 siblings, 1 reply; 3+ messages in thread
From: Li Huafei @ 2024-10-28 21:59 UTC (permalink / raw)
  To: alexander.deucher, lijo.lazar
  Cc: christian.koenig, Xinhui.Pan, airlied, simona, Hawking.Zhang,
	yifan1.zhang, Likun.Gao, Tim.Huang, pratap.nirujogi,
	victorchengchi.lu, Jun.Ma2, le.ma, amd-gfx, dri-devel,
	linux-kernel, lihuafei1

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>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
index 4bd61c169ca8..1b1aedebb3c2 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),
 			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;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/amdgpu: Fix possible NULL pointer dereference
  2024-10-28 14:33 ` Lazar, Lijo
@ 2024-10-29  1:17   ` Li Huafei
  0 siblings, 0 replies; 3+ messages in thread
From: Li Huafei @ 2024-10-29  1:17 UTC (permalink / raw)
  To: Lazar, Lijo, alexander.deucher
  Cc: christian.koenig, Xinhui.Pan, airlied, simona, Hawking.Zhang,
	yifan1.zhang, Likun.Gao, Tim.Huang, pratap.nirujogi,
	victorchengchi.lu, Jun.Ma2, le.ma, amd-gfx, dri-devel,
	linux-kernel



On 2024/10/28 22:33, Lazar, Lijo wrote:
> 
> 
> On 10/29/2024 3:29 AM, 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>
>> ---
>>  drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 6 ++++--
>>  1 file changed, 4 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
>> index 4bd61c169ca8..1b1aedebb3c2 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;
> 
> This one is used in the allocation call below. If you prefer to not
> assign any of the out params before memory allocation is successful,
> then use nps_info->v1.count in the alloc call.
> 

too bad! I missed it. I will send v2 to fix it.

Thanks,
Huafei

> Thanks for adding the alloc fail check.
> 
> Thanks,
> Lijo
>>  		mem_ranges = kvzalloc(
>>  			*range_cnt * 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;
> .
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-10-29  1:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-28 21:59 [PATCH] drm/amdgpu: Fix possible NULL pointer dereference Li Huafei
2024-10-28 14:33 ` Lazar, Lijo
2024-10-29  1:17   ` Li Huafei

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox