Skip to contentSkip to conversation

fs/select: reject negative timeval components in do_compat_select()

PATCH In review Avi Weiss 2 participants 2 messages Oct 31 – Nov 5 VFS

lore
Share & tools
lore

Avi Weiss

author
#0 lore raw

PATCH 1/1 fs/select: reject negative timeval components in do_compat_select()

do_compat_select() normalizes old_timeval32 before validating the timeout. Negative seconds and microseconds can cancel each other, causing compat select() and old_select() to accept invalid timeouts.

Reject negative components before normalization (as kern_select() already does) - and preserve the handling of oversized positive tv_usec values.

A 32-bit syscall reproducer confirmed four incorrect results on the unpatched kernel. With the fix applied, all my test cases passed on both compat select interfaces.

Fixes 4d36a9e65d49 ("select: deal with math overflow from borderline valid userland data")
---
 fs/select.c | 2 ++
 1 file changed, 2 insertions(+)
fs/select.c +2 -0
index 95d76531015aa..8b6d1dd2977c6 100644
@@ -1265,6 +1265,8 @@ static int do_compat_select(int n, compat_ulong_t __user *inp,
 		if (copy_from_user(&tv, tvp, sizeof(tv)))
 			return -EFAULT;
 
+		if (tv.tv_sec < 0 || tv.tv_usec < 0)
+			return -EINVAL;
 		to = &end_time;
 		if (poll_select_set_timeout(to,
 				tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),

base-commit: 36aad21d50d1af3f6954bb0c988257a908202477

Signature · 1 line
2.43.0
reviewer
#1 lore raw
Quoted 3 lines · Avi Weiss

do_compat_select() normalizes old_timeval32 before validating the timeout. Negative seconds and microseconds can cancel each other, causing compat select() and old_select() to accept invalid timeouts.

Yes, but who cares? You can pass various strange values (like making sec + usec / USEC_PER_SEC overflow) resulting in odd behavior but you are only shooting yourself in the foot... So if it doesn't hurt integrity of the code flow, I'd just ignore this.

Honza

Quoted 33 lines

Reject negative components before normalization (as kern_select() already does) - and preserve the handling of oversized positive tv_usec values.

A 32-bit syscall reproducer confirmed four incorrect results on the unpatched kernel. With the fix applied, all my test cases passed on both compat select interfaces.

Fixes 4d36a9e65d49 ("select: deal with math overflow from borderline valid userland data")
---
 fs/select.c | 2 ++
 1 file changed, 2 insertions(+)
fs/select.c +2 -0
index 95d76531015aa..8b6d1dd2977c6 100644
@@ -1265,6 +1265,8 @@ static int do_compat_select(int n, compat_ulong_t __user *inp,
 		if (copy_from_user(&tv, tvp, sizeof(tv)))
 			return -EFAULT;
 
+		if (tv.tv_sec < 0 || tv.tv_usec < 0)
+			return -EINVAL;
 		to = &end_time;
 		if (poll_select_set_timeout(to,
 				tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),

base-commit: 36aad21d50d1af3f6954bb0c988257a908202477

Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR