Skip to contentSkip to conversation

kobject_uevent: fix OOB read in kobject_action_type()

PATCH In review Hyeontaek Lim 2 participants 3 messages Sep 27 – Oct 5

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

PATCH 1/1 kobject_uevent: fix OOB read in kobject_action_type()

kobject_action_type() matches a user-supplied action string against the short "add"/"remove"/"change"/... literals in kobject_actions[] using strncmp() with a length (count_first) derived from the size of the user-supplied buffer (up to the first space, or the whole write).

strncmp() correctly stops comparing as soon as it hits a NUL byte in either string, so a caller can make it report a "match" against a short literal like "add" by embedding a NUL right after "add" in an otherwise much longer buffer (e.g. write "add\0" followed by 391 bytes of arbitrary padding, with no space, so count_first stays at the full write length).

The subsequent bounds check then indexes the matched literal at that same, much larger offset:

    if (kobject_actions[action][count_first] != '\0')

which reads far past the end of the 4-byte "add" literal, into whatever object happens to follow it in .rodata (observed to land inside uevent_helper_sysctl_table on the reporter's build).

Fix this by checking the literal's real length against count_first before doing the strncmp()/indexing at all, instead of trusting that a strncmp() "match" implies the lengths agree.

---
 lib/kobject_uevent.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/kobject_uevent.c +2 -2
index xxxxxxx..xxxxxxx 100644
@@ -78,10 +78,11 @@ static int kobject_action_type(const char *buf, size_t count,
 		count_first = count;
 
 	for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
+		if (strlen(kobject_actions[action]) != count_first)
+			continue;
 		if (strncmp(kobject_actions[action], buf, count_first) != 0)
 			continue;
-		if (kobject_actions[action][count_first] != '\0')
-			continue;
 		if (args)
 			*args = args_start;
 		*type = action;
#1 lore raw
Quoted 40 lines · Hyeontaek Lim

kobject_action_type() matches a user-supplied action string against the short "add"/"remove"/"change"/... literals in kobject_actions[] using strncmp() with a length (count_first) derived from the size of the user-supplied buffer (up to the first space, or the whole write).

strncmp() correctly stops comparing as soon as it hits a NUL byte in either string, so a caller can make it report a "match" against a short literal like "add" by embedding a NUL right after "add" in an otherwise much longer buffer (e.g. write "add\0" followed by 391 bytes of arbitrary padding, with no space, so count_first stays at the full write length).

The subsequent bounds check then indexes the matched literal at that same, much larger offset:

    if (kobject_actions[action][count_first] != '\0')

which reads far past the end of the 4-byte "add" literal, into whatever object happens to follow it in .rodata (observed to land inside uevent_helper_sysctl_table on the reporter's build).

Fix this by checking the literal's real length against count_first before doing the strncmp()/indexing at all, instead of trusting that a strncmp() "match" implies the lengths agree.

---
 lib/kobject_uevent.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/kobject_uevent.c +0 -0
index xxxxxxx..xxxxxxx 100644
@@ -78,10 +78,11 @@ static int kobject_action_type(const char *buf, size_t count,
 		count_first = count;
 

lib/kobject_uevent.c static int kobject_action_type(const char *buf, size_t count, · line 87

Quoted 10 lines
lib/kobject_uevent.c +2 -2
 	for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
+		if (strlen(kobject_actions[action]) != count_first)
+			continue;
 		if (strncmp(kobject_actions[action], buf, count_first) != 0)
 			continue;
-		if (kobject_actions[action][count_first] != '\0')
-			continue;
 		if (args)
 			*args = args_start;
 		*type = action;

Hasn't this been resolved already?

#2 lore raw

Hi Greg,

You're right -- I see now that this was already fixed by commit d516455c9405 ("kobject: fix out-of-bounds read in kobject_action_type()") from Jiacheng Xu, merged on Sep 24

Please disregard my patch -- it's a duplicate. I should have checked linux-next and the existing lore threads before sending;
I'm new to this and will be more careful next time. Sorry for the noise.

Thanks,
Hyeontaek Lim