Skip to contentSkip to conversation

memory: tegra: Fix possible null pointer dereference

PATCH In review Ethan Tidmore 3 participants 3 messages May 8 – May 13

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 memory: tegra: Fix possible null pointer dereference

The function tegra114_emc_find_timing() has the possibility of returning null and it's return value 'timing' is dereferenced before it is checked for null.

Place dereference after null pointer check.

Detected by Smatch:
drivers/memory/tegra/tegra114-emc.c:520 tegra114_emc_prepare_timing_change()
warn: variable dereferenced before check 'timing' (see line 515)

Fixes dce208b5405f4 ("memory: tegra: Add Tegra114 EMC driver")
---
 drivers/memory/tegra/tegra114-emc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/memory/tegra/tegra114-emc.c +3 -1
index be6c699cb656..fc3bf1a28630 100644
@@ -512,14 +512,16 @@ static int tegra114_emc_prepare_timing_change(struct tegra_emc *emc,
 	enum emc_dll_change dll_change;
 	unsigned int pre_wait = 0;
 	u32 val, mask;
-	bool next_dll_enabled = !(timing->emc_mode_1 & 0x1);
 	bool last_dll_enabled = !(last->emc_mode_1 & 0x1);
 	bool update = false;
+	bool next_dll_enabled;
 	unsigned int i;
 
 	if (!timing)
 		return -ENOENT;
 
+	next_dll_enabled = !(timing->emc_mode_1 & 0x1);
+
 	if (next_dll_enabled == last_dll_enabled)
 		dll_change = DLL_CHANGE_NONE;
 	else if (next_dll_enabled)
Signature · 1 line
2.54.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Ethan Tidmore

The function tegra114_emc_find_timing() has the possibility of returning null and it's return value 'timing' is dereferenced before it is checked for null.

Place dereference after null pointer check.

Detected by Smatch:
drivers/memory/tegra/tegra114-emc.c:520 tegra114_emc_prepare_timing_change()
warn: variable dereferenced before check 'timing' (see line 515)

Quoted 5 lines
Fixes dce208b5405f4 ("memory: tegra: Add Tegra114 EMC driver")
---
 drivers/memory/tegra/tegra114-emc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

It's a *very* theoretically possible NULL pointer dereference. Both the clock and EMC drivers read the timing information from exactly the same device tree nodes, so their timings should always match. If they don't something *really* bad is going on and it might be safer to just crash.

On the other hand, if this silences a smatch warning people are just going to keep sending patches and we might as well just "fix" it now, so:

#2 lore raw
Quoted 17 lines · Thierry Reding
Quoted 10 lines · Ethan Tidmore

The function tegra114_emc_find_timing() has the possibility of returning null and it's return value 'timing' is dereferenced before it is checked for null.

Place dereference after null pointer check.

Detected by Smatch:
drivers/memory/tegra/tegra114-emc.c:520 tegra114_emc_prepare_timing_change()
warn: variable dereferenced before check 'timing' (see line 515)

Quoted 5 lines
Fixes dce208b5405f4 ("memory: tegra: Add Tegra114 EMC driver")
---
 drivers/memory/tegra/tegra114-emc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 8 lines

It's a *very* theoretically possible NULL pointer dereference. Both the clock and EMC drivers read the timing information from exactly the same device tree nodes, so their timings should always match. If they don't something *really* bad is going on and it might be safer to just crash.

On the other hand, if this silences a smatch warning people are just going to keep sending patches and we might as well just "fix" it now, so:

One more argument is that the function, when read alone, is confusing. Explicit obviousness is better than figuring out why the unnecessary condition is there (unnecessary because of earlier NULL ptr exception).

I will apply it with Dan's reported-by, because he send similar report shortly after.

Best regards,
Krzysztof