drm/amdgpu: Handle potential NULL pointer dereference
Subsystems (2)
PATCH 1/1 drm/amdgpu: Handle potential NULL pointer dereference
If m is NULL, we will end up referencing a NULL pointer in the subsequent m elements like extcpu, bank and status. Pull the NULL check out and do it first before referencing m's elements.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +4 -1
@@ -2838,12 +2838,15 @@ static int amdgpu_bad_page_notifier(struct notifier_block *nb,struct eeprom_table_record err_rec;uint64_t retired_page; +if (!m) +return NOTIFY_DONE ; +/* * If the error was generated in UMC_V2, which belongs to GPU UMCs, * and error occurred in DramECC (Extended error code = 0) then only * process the error, else bail out. */ -if (!m || !((smca_get_bank_type (m->extcpu, m->bank) ==SMCA_UMC_V2 ) && +if (!((smca_get_bank_type (m->extcpu, m->bank) ==SMCA_UMC_V2 ) && (XEC (m->status,0x3f ) ==0x0 )))return NOTIFY_DONE ;
Signature · 1 line
2.25.1
[AMD Official Use Only - General]
Friendly ping
Kent
-----Original Message-----
From: Russell, Kent <[email protected]>
Sent: Monday, August 15, 2022 11:31 AM
To: [email protected]
Cc: Ghannam, Yazen <[email protected]>; Russell, Kent <[email protected]>
Subject: [PATCH] drm/amdgpu: Handle potential NULL pointer dereference
If m is NULL, we will end up referencing a NULL pointer in the subsequent m elements like extcpu, bank and status. Pull the NULL check out and do it first before referencing m's elements.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +4 -1
@@ -2838,12 +2838,15 @@ static int amdgpu_bad_page_notifier(struct notifier_block *nb,struct eeprom_table_record err_rec;uint64_t retired_page; +if (!m) +return NOTIFY_DONE ; +/* * If the error was generated in UMC_V2, which belongs to GPU UMCs, * and error occurred in DramECC (Extended error code = 0) then only * process the error, else bail out. */ -if (!m || !((smca_get_bank_type (m->extcpu, m->bank) ==SMCA_UMC_V2 ) && +if (!((smca_get_bank_type (m->extcpu, m->bank) ==SMCA_UMC_V2 ) && (XEC (m->status,0x3f ) ==0x0 )))return NOTIFY_DONE ;
--
2.25.1
Quoted 4 lines · Kent
[AMD Official Use Only - General]
Friendly ping
Wonder how it goes any further when m is NULL. It should do shortcut evaluation and return NOTIFY_DONE, right? Or is this for better readability?
Thanks,
Lijo
Quoted 40 lines
Kent
-----Original Message-----
From: Russell, Kent <[email protected]>
Sent: Monday, August 15, 2022 11:31 AM
To: [email protected]
Cc: Ghannam, Yazen <[email protected]>; Russell, Kent <[email protected]>
Subject: [PATCH] drm/amdgpu: Handle potential NULL pointer dereferenceIf m is NULL, we will end up referencing a NULL pointer in the subsequent m elements like extcpu, bank and status. Pull the NULL check out and do it first before referencing m's elements.
Signed-off-by Kent Russell <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +4
-2@@ -2838,12 +2838,15 @@ static int amdgpu_bad_page_notifier(struct notifier_block *nb,struct eeprom_table_record err_rec;uint64_t retired_page; +if (!m) +return NOTIFY_DONE ; +/* * If the error was generated in UMC_V2, which belongs to GPU UMCs, * and error occurred in DramECC (Extended error code = 0) then only * process the error, else bail out. */ -if (!m || !((smca_get_bank_type (m->extcpu, m->bank) ==SMCA_UMC_V2 ) && +if (!((smca_get_bank_type (m->extcpu, m->bank) ==SMCA_UMC_V2 ) && (XEC (m->status,0x3f ) ==0x0 )))return NOTIFY_DONE ; --2.25.1
[AMD Official Use Only - General]
Good point, as if (1 || 0) should short-circuit at the if (1) part. Thus we should go down to NOTIFY_DONE if m is NULL. Yazen can confirm here, as he was the one who with me when we found the original issue. It's possible that one of the 3 message fields was NULL instead of the actual message in our repro situation, but I'll double-check the short-circuit side of C to confirm. I know it short circuits for &&, I don't know if it does for ||.
Kent
-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Thursday, August 25, 2022 8:34 AM
To: Russell, Kent <[email protected]>; [email protected]
Cc: Ghannam, Yazen <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: Handle potential NULL pointer dereference
Quoted 4 lines · Kent
[AMD Official Use Only - General]
Friendly ping
Wonder how it goes any further when m is NULL. It should do shortcut evaluation and return NOTIFY_DONE, right? Or is this for better readability?
Thanks,
Lijo
Quoted 42 lines
Kent
-----Original Message-----
From: Russell, Kent <[email protected]>
Sent: Monday, August 15, 2022 11:31 AM
To: [email protected]
Cc: Ghannam, Yazen <[email protected]>; Russell, Kent <[email protected]>
Subject: [PATCH] drm/amdgpu: Handle potential NULL pointer dereferenceIf m is NULL, we will end up referencing a NULL pointer in the subsequent m elements like extcpu, bank and status. Pull the NULL check out and do it first before referencing m's elements.
Signed-off-by Kent Russell <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +4
-2@@ -2838,12 +2838,15 @@ static int amdgpu_bad_page_notifier(struct notifier_block *nb,struct eeprom_table_record err_rec;uint64_t retired_page; +if (!m) +return NOTIFY_DONE ; +/* * If the error was generated in UMC_V2, which belongs to GPU UMCs, * and error occurred in DramECC (Extended error code = 0) then only * process the error, else bail out. */ -if (!m || !((smca_get_bank_type (m->extcpu, m->bank) ==SMCA_UMC_V2 ) && +if (!((smca_get_bank_type (m->extcpu, m->bank) ==SMCA_UMC_V2 ) && (XEC (m->status,0x3f ) ==0x0 )))return NOTIFY_DONE ; --2.25.1
[AMD Official Use Only - General]
It does indeed short-circuit on || (If the left side of an || statement is not 0, it doesn't evaluate the right side and returns true). So we can ignore this patch, since checking for each individual field on the 2nd term is probably overkill. We were still investigating what got passed in and why it wasn't valid, so I'll drop this for now. Thanks Lijo!
Kent
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Russell, Kent
Sent: Thursday, August 25, 2022 8:52 AM
To: Lazar, Lijo <[email protected]>; [email protected]
Cc: Ghannam, Yazen <[email protected]>
Subject: RE: [PATCH] drm/amdgpu: Handle potential NULL pointer dereference
[AMD Official Use Only - General]
Good point, as if (1 || 0) should short-circuit at the if (1) part. Thus we should go down to NOTIFY_DONE if m is NULL. Yazen can confirm here, as he was the one who with me when we found the original issue. It's possible that one of the 3 message fields was NULL instead of the actual message in our repro situation, but I'll double-check the short-circuit side of C to confirm. I know it short circuits for &&, I don't know if it does for ||.
Kent
-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Thursday, August 25, 2022 8:34 AM
To: Russell, Kent <[email protected]>; [email protected]
Cc: Ghannam, Yazen <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: Handle potential NULL pointer dereference
Quoted 4 lines · Kent
[AMD Official Use Only - General]
Friendly ping
Wonder how it goes any further when m is NULL. It should do shortcut evaluation and return NOTIFY_DONE, right? Or is this for better readability?
Thanks,
Lijo
Quoted 42 lines
Kent
-----Original Message-----
From: Russell, Kent <[email protected]>
Sent: Monday, August 15, 2022 11:31 AM
To: [email protected]
Cc: Ghannam, Yazen <[email protected]>; Russell, Kent <[email protected]>
Subject: [PATCH] drm/amdgpu: Handle potential NULL pointer dereferenceIf m is NULL, we will end up referencing a NULL pointer in the subsequent m elements like extcpu, bank and status. Pull the NULL check out and do it first before referencing m's elements.
Signed-off-by Kent Russell <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +4
-2@@ -2838,12 +2838,15 @@ static int amdgpu_bad_page_notifier(struct notifier_block *nb,struct eeprom_table_record err_rec;uint64_t retired_page; +if (!m) +return NOTIFY_DONE ; +/* * If the error was generated in UMC_V2, which belongs to GPU UMCs, * and error occurred in DramECC (Extended error code = 0) then only * process the error, else bail out. */ -if (!m || !((smca_get_bank_type (m->extcpu, m->bank) ==SMCA_UMC_V2 ) && +if (!((smca_get_bank_type (m->extcpu, m->bank) ==SMCA_UMC_V2 ) && (XEC (m->status,0x3f ) ==0x0 )))return NOTIFY_DONE ; --2.25.1
Quoted 3 lines · Kent
[AMD Official Use Only - General]
It does indeed short-circuit on || (If the left side of an || statement is not 0, it doesn't evaluate the right side and returns true). So we can ignore this patch, since checking for each individual field on the 2nd term is probably overkill. We were still investigating what got passed in and why it wasn't valid, so I'll drop this for now. Thanks Lijo!
Right, the "||" will short-circuit, but not the "&&".
...
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c static int amdgpu_bad_page_notifier(struct notifier_block *nb, · line 2850
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +4
-1+if (!m) +return NOTIFY_DONE ; +/* * If the error was generated in UMC_V2, which belongs to GPU UMCs, * and error occurred in DramECC (Extended error code = 0) then only * process the error, else bail out. */ -if (!m || !((smca_get_bank_type (m->extcpu, m->bank) ==SMCA_UMC_V2 ) && +if (!((smca_get_bank_type (m->extcpu, m->bank) ==SMCA_UMC_V2 ) && (XEC (m->status,0x3f ) ==0x0 )))
If m is NULL, then we get (!m || "Don't care") = true. We avoid the second operand.
But then we have (true && "Something with 'm->' in it"). The second operand in the && needs to be checked, and this will cause a NULL pointer dereference if m is NULL.
What do you guys think?
Thanks,
Yazen
[AMD Official Use Only - General]
Quoted 36 lines
-----Original Message-----
From: Ghannam, Yazen <[email protected]>
Sent: Friday, August 26, 2022 11:20 AM
To: Russell, Kent <[email protected]>
Cc: Lazar, Lijo <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: Handle potential NULL pointer dereferenceQuoted 3 lines · Kent
[AMD Official Use Only - General]
It does indeed short-circuit on || (If the left side of an || statement is not 0, it
doesn't evaluate the right side and returns true). So we can ignore this patch, since checking for each individual field on the 2nd term is probably overkill. We were still investigating what got passed in and why it wasn't valid, so I'll drop this for now. Thanks Lijo!
Right, the "||" will short-circuit, but not the "&&".
...
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c static int amdgpu_bad_page_notifier(struct notifier_block *nb,
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +3
-1+if (!m) +return NOTIFY_DONE ; +/* * If the error was generated in UMC_V2, which belongs to GPU UMCs, * and error occurred in DramECC (Extended error code = 0) then only * process the error, else bail out. */ - if (!m || !((smca_get_bank_type(m->extcpu, m->bank) ==
SMCA_UMC_V2) &&Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c static int amdgpu_bad_page_notifier(struct notifier_block *nb, · line 2850
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1
-0+if (!((smca_get_bank_type (m->extcpu, m->bank) ==SMCA_UMC_V2 ) && (XEC (m->status,0x3f ) ==0x0 )))If m is NULL, then we get (!m || "Don't care") = true. We avoid the second operand.
Quoted 6 lines
But then we have (true && "Something with 'm->' in it"). The second operand in the && needs to be checked, and this will cause a NULL pointer dereference if m is NULL.
What do you guys think?
Since the 2nd field is in parentheses, it would be whatever = (smca_get_bank_type && XEC)
if (!m || !(whatever))
In that case we should still jump out at !m, since the 2nd term is completely contained in that !((...)) clump. At least that's how I understand it after Lijo's original comments.
Kent
Quoted 2 lines
Thanks,
Yazen