Skip to contentSkip to conversation

gpiolib: cdev: fix null pointer dereference in linereq_free()

PATCH In review Kent Gibson 3 participants 7 messages Jul 6, 2022 – Jul 9, 2022

Subsystems (2)
lore
Share & tools
lore

Kent Gibson

author reviewer
#0 lore raw

PATCH 1/1 gpiolib: cdev: fix null pointer dereference in linereq_free()

This patch fixes a kernel NULL pointer dereference that is reported by gpio kselftests.

linereq_free() can be called as part of the cleanup of a failed request, at which time the desc for a line may not have been determined, so it is unsafe to dereference without a check.

Add a check prior to dereferencing the line desc.

Fixes 2068339a6c35 ("gpiolib: cdev: Add hardware timestamp clock type")

---

I suspect the edge_detector_stop() and gpiod_free() could also be moved inside the same desc check but, as we are late in the rc cycle, I don't want to push my luck and have kept to the minimum change required to address the bug.

 drivers/gpio/gpiolib-cdev.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/gpio/gpiolib-cdev.c +4 -3
index f5aa5f93342a..0c9a63becfef 100644
@@ -1460,11 +1460,12 @@ static ssize_t linereq_read(struct file *file,
 static void linereq_free(struct linereq *lr)
 {
 	unsigned int i;
-	bool hte;
+	bool hte = false;
 
 	for (i = 0; i < lr->num_lines; i++) {
-		hte = !!test_bit(FLAG_EVENT_CLOCK_HTE,
-				 &lr->lines[i].desc->flags);
+		if (lr->lines[i].desc)
+			hte = !!test_bit(FLAG_EVENT_CLOCK_HTE,
+					 &lr->lines[i].desc->flags);
 		edge_detector_stop(&lr->lines[i], hte);
 		if (lr->lines[i].desc)
 			gpiod_free(lr->lines[i].desc);
Signature · 1 line
2.37.0
author reviewer
#1 lore raw
Quoted 3 lines · Kent Gibson

This patch fixes a kernel NULL pointer dereference that is reported by gpio kselftests.

Should be:

Fix a kernel NULL pointer dereference reported by gpio kselftests.

Sorry - I rushed this one a bit.

Cheers,
Kent.

author reviewer
#2 lore raw
Quoted 11 lines · Kent Gibson
Quoted 3 lines · Kent Gibson

This patch fixes a kernel NULL pointer dereference that is reported by gpio kselftests.

Should be:

Fix a kernel NULL pointer dereference reported by gpio kselftests.

Sorry - I rushed this one a bit.

And I might not've been totally clear, but this bug is present in v5.19-rc1 onwards (when HTE was added), up to and including rc5.

Would be good to get it fixed before v5.19 goes out the door.

Cheers,
Kent.

#3 lore raw
Quoted 14 lines · Kent Gibson
Quoted 11 lines · Kent Gibson
Quoted 3 lines · Kent Gibson

This patch fixes a kernel NULL pointer dereference that is reported by gpio kselftests.

Should be:

Fix a kernel NULL pointer dereference reported by gpio kselftests.

Sorry - I rushed this one a bit.

Quoted 7 lines

And I might not've been totally clear, but this bug is present in v5.19-rc1 onwards (when HTE was added), up to and including rc5.

Would be good to get it fixed before v5.19 goes out the door.

Cheers,
Kent.

Yep, figured that out. Applied and fixed the commit message, thanks!

Bart

author reviewer
#4 lore raw
Quoted 18 lines · Bartosz Golaszewski
Quoted 17 lines · Kent Gibson
Quoted 11 lines · Kent Gibson
Quoted 3 lines · Kent Gibson

This patch fixes a kernel NULL pointer dereference that is reported by gpio kselftests.

Should be:

Fix a kernel NULL pointer dereference reported by gpio kselftests.

Sorry - I rushed this one a bit.

And I might not've been totally clear, but this bug is present in v5.19-rc1 onwards (when HTE was added), up to and including rc5.

Quoted 7 lines
Quoted 4 lines

Would be good to get it fixed before v5.19 goes out the door.

Cheers,
Kent.

Yep, figured that out. Applied and fixed the commit message, thanks!

Good to hear. I never got around to reviewing that final HTE patch and, while it did end up pretty close to what I expected, there are a few things that I would've done slightly differently that I'd like to tidy up.
And also have the HTE specific code compiled out unless CONFIG_HTE is selected, as that is very likely to be the case for most builds. But that can wait for v5.20.

Cheers,
Kent.

#5 lore raw
Quoted 18 lines · Kent Gibson
Quoted 17 lines · Bartosz Golaszewski
Quoted 16 lines · Kent Gibson
Quoted 10 lines · Kent Gibson
Quoted 3 lines · Kent Gibson

This patch fixes a kernel NULL pointer dereference that is reported by gpio kselftests.

Should be:

Fix a kernel NULL pointer dereference reported by gpio kselftests.

Sorry - I rushed this one a bit.

And I might not've been totally clear, but this bug is present in v5.19-rc1 onwards (when HTE was added), up to and including rc5.

Would be good to get it fixed before v5.19 goes out the door.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Cheers,
Kent.

Yep, figured that out. Applied and fixed the commit message, thanks!

Good to hear. I never got around to reviewing that final HTE patch and, while it did end up pretty close to what I expected, there are a few things that I would've done slightly differently that I'd like to tidy up.

I can create another thread to address this. Let me know.

Quoted 3 lines

And also have the HTE specific code compiled out unless CONFIG_HTE is selected, as that is very likely to be the case for most builds. But that can wait for v5.20.

I am assuming #ifdef CONFIG_HTE blocks around HTE blocks. I think Linus W. also

indicated that.

Quoted 4 lines

Cheers,
Kent.

author reviewer
#6 lore raw
Quoted 21 lines · Dipen Patel
Quoted 20 lines · Kent Gibson
Quoted 19 lines · Bartosz Golaszewski
Quoted 18 lines · Kent Gibson
Quoted 10 lines · Kent Gibson
Quoted 3 lines · Kent Gibson

This patch fixes a kernel NULL pointer dereference that is reported by gpio kselftests.

Should be:

Fix a kernel NULL pointer dereference reported by gpio kselftests.

Sorry - I rushed this one a bit.

And I might not've been totally clear, but this bug is present in v5.19-rc1 onwards (when HTE was added), up to and including rc5.

Would be good to get it fixed before v5.19 goes out the door.

Cheers,
Kent.

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

Yep, figured that out. Applied and fixed the commit message, thanks!

Good to hear. I never got around to reviewing that final HTE patch and, while it did end up pretty close to what I expected, there are a few things that I would've done slightly differently that I'd like to tidy up.

I can create another thread to address this. Let me know.

I've already got the changes locally, so don't worry. There is nothing critical or earth shattering in there, mainly how the hte_en flag is passed around. You followed the pattern provided by polarity_change, which is only used on one occasion, when I would've followed the pattern from line->eflags, which is used more widely.

Though I might take the changes a little further than I have to also change the polarity_change flag to follow the eflags pattern to prevent anyone following from falling into the same trap - still stewing on that.

The basic idea is that, particularly during reconfig, the struct line contains the current applied state, while the desc->flags contain the requested state. The polarity_change flag was a bit of a lazy shortcut to save from adding an active_low (or equivalent) flag to struct line.

Quoted 8 lines
Quoted 3 lines

And also have the HTE specific code compiled out unless CONFIG_HTE is selected, as that is very likely to be the case for most builds. But that can wait for v5.20.

I am assuming #ifdef CONFIG_HTE blocks around HTE blocks. I think Linus W. also

indicated that.

Correct.

Cheers,
Kent.