Skip to contentSkip to conversation

mm/uaccess: Use 'unsigned long' to placate UBSAN warnings, again

PATCH In review Josh Poimboeuf 8 participants 15 messages Dec 23, 2020 – Feb 10, 2021

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

PATCH 1/1 mm/uaccess: Use 'unsigned long' to placate UBSAN warnings, again

GCC 7 has a known bug where UBSAN ignores '-fwrapv' and generates false signed-overflow-UB warnings. The type mismatch between 'i' and 'nr_segs' in copy_compat_iovec_from_user() is causing such a warning, which also happens to violate uaccess rules:

lib/iov_iter.o: warning: objtool: iovec_from_user()+0x22d: call to __ubsan_handle_add_overflow() with UACCESS enabled

Fix it by making the variable types match.

This is similar to a previous commit:

29da93fea3ea ("mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions")

---
 lib/iov_iter.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/iov_iter.c +2 -1
index 1635111c5bd2..2e6a42f5d1df 100644
@@ -1656,7 +1656,8 @@ static int copy_compat_iovec_from_user(struct iovec *iov,
 {
 	const struct compat_iovec __user *uiov =
 		(const struct compat_iovec __user *)uvec;
-	int ret = -EFAULT, i;
+	int ret = -EFAULT;
+	unsigned long i;
 
 	if (!user_access_begin(uvec, nr_segs * sizeof(*uvec)))
 		return -EFAULT;
Signature · 1 line
2.29.2
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Josh Poimboeuf

GCC 7 has a known bug where UBSAN ignores '-fwrapv' and generates false signed-overflow-UB warnings. The type mismatch between 'i' and 'nr_segs' in copy_compat_iovec_from_user() is causing such a warning, which also happens to violate uaccess rules:

lib/iov_iter.o: warning: objtool: iovec_from_user()+0x22d: call to __ubsan_handle_add_overflow() with UACCESS enabled

Quoted 8 lines

Fix it by making the variable types match.

This is similar to a previous commit:

29da93fea3ea ("mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions")

All good. Thanks.

Quoted 19 lines
---
 lib/iov_iter.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/iov_iter.c +2 -1
index 1635111c5bd2..2e6a42f5d1df 100644
@@ -1656,7 +1656,8 @@ static int copy_compat_iovec_from_user(struct iovec *iov,
 {
 	const struct compat_iovec __user *uiov =
 		(const struct compat_iovec __user *)uvec;
-	int ret = -EFAULT, i;
+	int ret = -EFAULT;
+	unsigned long i;
 
 	if (!user_access_begin(uvec, nr_segs * sizeof(*uvec)))
 		return -EFAULT;
Signature · 1 line
~Randy
Acked-by
#2 lore raw

From: Randy Dunlap

Quoted 15 lines

Sent: 23 December 2020 07:19

Quoted 6 lines · Josh Poimboeuf

GCC 7 has a known bug where UBSAN ignores '-fwrapv' and generates false signed-overflow-UB warnings. The type mismatch between 'i' and 'nr_segs' in copy_compat_iovec_from_user() is causing such a warning, which also happens to violate uaccess rules:

lib/iov_iter.o: warning: objtool: iovec_from_user()+0x22d: call to __ubsan_handle_add_overflow()

with UACCESS enabled

Quoted 5 lines

Fix it by making the variable types match.

This is similar to a previous commit:

Quoted 8 lines
Quoted 4 lines

29da93fea3ea ("mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions")

All good. Thanks.

FWIW I've tested the equivalent change locally.
It generates rather better code on amd64.
(You don't want to index arrays with 'int' unless you really have to.) So probably:

David

Quoted 18 lines
Quoted 18 lines

---
 lib/iov_iter.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/iov_iter.c +2 -1
index 1635111c5bd2..2e6a42f5d1df 100644
@@ -1656,7 +1656,8 @@ static int copy_compat_iovec_from_user(struct iovec *iov,
 {
 	const struct compat_iovec __user *uiov =
 		(const struct compat_iovec __user *)uvec;
-	int ret = -EFAULT, i;
+	int ret = -EFAULT;
+	unsigned long i;
 
Quoted 7 lines

lib/iov_iter.c static int copy_compat_iovec_from_user(struct iovec *iov, · line 1663

Quoted 3 lines
lib/iov_iter.c +0 -0
 	if (!user_access_begin(uvec, nr_segs * sizeof(*uvec)))
 		return -EFAULT;

--
~Randy

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#3 lore raw
In reply to Josh Poimboeuf
Quoted 5 lines · Josh Poimboeuf

GCC 7 has a known bug where UBSAN ignores '-fwrapv' and generates false signed-overflow-UB warnings. The type mismatch between 'i' and 'nr_segs' in copy_compat_iovec_from_user() is causing such a warning, which also happens to violate uaccess rules:

Quoted 7 lines

lib/iov_iter.o: warning: objtool: iovec_from_user()+0x22d: call to __ubsan_handle_add_overflow() with UACCESS enabled

Fix it by making the variable types match.

This is similar to a previous commit:

29da93fea3ea ("mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions")

Maybe it's time we make UBSAN builds depend on GCC-8+ ?

#4 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Josh Poimboeuf

GCC 7 has a known bug where UBSAN ignores '-fwrapv' and generates false signed-overflow-UB warnings. The type mismatch between 'i' and 'nr_segs' in copy_compat_iovec_from_user() is causing such a warning, which also happens to violate uaccess rules:

lib/iov_iter.o: warning: objtool: iovec_from_user()+0x22d: call to __ubsan_handle_add_overflow() with UACCESS enabled

Quoted 7 lines
Quoted 5 lines

Fix it by making the variable types match.

This is similar to a previous commit:

29da93fea3ea ("mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions")

Maybe it's time we make UBSAN builds depend on GCC-8+ ?

I would be totally fine with that. The only thing I can think of that might care is syzbot. Dmitry, does syzbot use anything older than gcc 8?

Signature · 1 line
Kees Cook
#5 lore raw
Quoted 11 lines · Kees Cook
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Josh Poimboeuf

GCC 7 has a known bug where UBSAN ignores '-fwrapv' and generates false signed-overflow-UB warnings. The type mismatch between 'i' and 'nr_segs' in copy_compat_iovec_from_user() is causing such a warning, which also happens to violate uaccess rules:

lib/iov_iter.o: warning: objtool: iovec_from_user()+0x22d: call to __ubsan_handle_add_overflow() with UACCESS enabled

Fix it by making the variable types match.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

This is similar to a previous commit:

29da93fea3ea ("mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions")

Maybe it's time we make UBSAN builds depend on GCC-8+ ?

I would be totally fine with that. The only thing I can think of that might care is syzbot. Dmitry, does syzbot use anything older than gcc 8?

I use UBSAN successfully with GCC 7.5.0.
However, I can revert whatever future patch someone adds for this...

Signature · 1 line
~Randy
#6 lore raw
Quoted 16 lines · Randy Dunlap
Quoted 15 lines · Kees Cook
Quoted 14 lines · Peter Zijlstra
Quoted 12 lines · Josh Poimboeuf

GCC 7 has a known bug where UBSAN ignores '-fwrapv' and generates false signed-overflow-UB warnings. The type mismatch between 'i' and 'nr_segs' in copy_compat_iovec_from_user() is causing such a warning, which also happens to violate uaccess rules:

lib/iov_iter.o: warning: objtool: iovec_from_user()+0x22d: call to __ubsan_handle_add_overflow() with UACCESS enabled

Fix it by making the variable types match.

This is similar to a previous commit:

29da93fea3ea ("mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions")

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Maybe it's time we make UBSAN builds depend on GCC-8+ ?

I would be totally fine with that. The only thing I can think of that might care is syzbot. Dmitry, does syzbot use anything older than gcc 8?

I use UBSAN successfully with GCC 7.5.0.
However, I can revert whatever future patch someone adds for this...

Peter, which GCC version specifically are you seeing this on? (i.e. can I just make in 7.5+ instead of 8+ to make Randy's life easier?)

Signature · 1 line
Kees Cook
#7 lore raw
Quoted 19 lines · Kees Cook
Quoted 18 lines · Randy Dunlap
Quoted 17 lines · Kees Cook
Quoted 15 lines · Peter Zijlstra
Quoted 5 lines · Josh Poimboeuf

GCC 7 has a known bug where UBSAN ignores '-fwrapv' and generates false signed-overflow-UB warnings. The type mismatch between 'i' and 'nr_segs' in copy_compat_iovec_from_user() is causing such a warning, which also happens to violate uaccess rules:

Quoted 7 lines

lib/iov_iter.o: warning: objtool: iovec_from_user()+0x22d: call to __ubsan_handle_add_overflow() with UACCESS enabled

Fix it by making the variable types match.

This is similar to a previous commit:

29da93fea3ea ("mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions")

Maybe it's time we make UBSAN builds depend on GCC-8+ ?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I would be totally fine with that. The only thing I can think of that might care is syzbot. Dmitry, does syzbot use anything older than gcc 8?

I use UBSAN successfully with GCC 7.5.0.
However, I can revert whatever future patch someone adds for this...

Peter, which GCC version specifically are you seeing this on? (i.e. can I just make in 7.5+ instead of 8+ to make Randy's life easier?)

I don't think that would help, we saw this bug on GCC 7.5.

Signature · 1 line
Josh
#8 lore raw
In reply to Kees Cook
Quoted 12 lines · Kees Cook
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Josh Poimboeuf

GCC 7 has a known bug where UBSAN ignores '-fwrapv' and generates false signed-overflow-UB warnings. The type mismatch between 'i' and 'nr_segs' in copy_compat_iovec_from_user() is causing such a warning, which also happens to violate uaccess rules:

lib/iov_iter.o: warning: objtool: iovec_from_user()+0x22d: call to __ubsan_handle_add_overflow() with UACCESS enabled

Fix it by making the variable types match.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

This is similar to a previous commit:

29da93fea3ea ("mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions")

Maybe it's time we make UBSAN builds depend on GCC-8+ ?

I would be totally fine with that. The only thing I can think of that might care is syzbot. Dmitry, does syzbot use anything older than gcc 8?

syzbot should use gcc 10 everywhere by now (and if not, we will update). And in general I think it's a reasonable requirement today.

#9 lore raw

PATCH 1/1 ubsan: Require GCC-8+ or Clang to use UBSAN

In reply to Peter Zijlstra
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Josh Poimboeuf

GCC 7 has a known bug where UBSAN ignores '-fwrapv' and generates false signed-overflow-UB warnings. The type mismatch between 'i' and 'nr_segs' in copy_compat_iovec_from_user() is causing such a warning, which also happens to violate uaccess rules:

lib/iov_iter.o: warning: objtool: iovec_from_user()+0x22d: call to __ubsan_handle_add_overflow() with UACCESS enabled

Quoted 7 lines
Quoted 5 lines

Fix it by making the variable types match.

This is similar to a previous commit:

29da93fea3ea ("mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions")

Maybe it's time we make UBSAN builds depend on GCC-8+ ?

---
Subject: ubsan: Require GCC-8+ or Clang to use UBSAN

Just like how we require GCC-8.2 for KASAN due to compiler bugs, require a sane version of GCC for UBSAN.

Specifically, before GCC-8 UBSAN doesn't respect -fwrapv and thinks signed arithmetic is buggered.

---
 lib/Kconfig.ubsan | 5 +++++
 1 file changed, 5 insertions(+)
lib/Kconfig.ubsan +5 -0
index 8b635fd75fe4..acc3df62460e 100644
@@ -2,8 +2,13 @@
 config ARCH_HAS_UBSAN_SANITIZE_ALL
 	bool
 
+# UBSAN prior to GCC-8 gets -fwrapv wrong, we rely on that
+config UBSAN_SANE
+	def_bool !CC_IS_GCC || GCC_VERSION >= 80000
+
 menuconfig UBSAN
 	bool "Undefined behaviour sanity checker"
+	depends on UBSAN_SANE
 	help
 	  This option enables the Undefined Behaviour sanity checker.
 	  Compile-time instrumentation is used to detect various undefined
#10 lore raw

Re: [PATCH] ubsan: Require GCC-8+ or Clang to use UBSAN

Quoted 17 lines · Peter Zijlstra
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Josh Poimboeuf

GCC 7 has a known bug where UBSAN ignores '-fwrapv' and generates false signed-overflow-UB warnings. The type mismatch between 'i' and 'nr_segs' in copy_compat_iovec_from_user() is causing such a warning, which also happens to violate uaccess rules:

lib/iov_iter.o: warning: objtool: iovec_from_user()+0x22d: call to __ubsan_handle_add_overflow() with UACCESS enabled

Quoted 7 lines
Quoted 5 lines

Fix it by making the variable types match.

This is similar to a previous commit:

29da93fea3ea ("mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions")

Maybe it's time we make UBSAN builds depend on GCC-8+ ?

Quoted 9 lines

---
Subject: ubsan: Require GCC-8+ or Clang to use UBSAN

Just like how we require GCC-8.2 for KASAN due to compiler bugs, require a sane version of GCC for UBSAN.

Specifically, before GCC-8 UBSAN doesn't respect -fwrapv and thinks signed arithmetic is buggered.

Actually removing CONFIG_UBSAN_SIGNED_OVERFLOW would give us the same effect without restricting GCC versions.

#11 lore raw

Re: [PATCH] ubsan: Require GCC-8+ or Clang to use UBSAN

Quoted 26 lines · Andrey Ryabinin
Quoted 23 lines · Peter Zijlstra
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Josh Poimboeuf

GCC 7 has a known bug where UBSAN ignores '-fwrapv' and generates false signed-overflow-UB warnings. The type mismatch between 'i' and 'nr_segs' in copy_compat_iovec_from_user() is causing such a warning, which also happens to violate uaccess rules:

lib/iov_iter.o: warning: objtool: iovec_from_user()+0x22d: call to __ubsan_handle_add_overflow() with UACCESS enabled

Quoted 7 lines
Quoted 5 lines

Fix it by making the variable types match.

This is similar to a previous commit:

29da93fea3ea ("mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions")

Maybe it's time we make UBSAN builds depend on GCC-8+ ?

---
Subject: ubsan: Require GCC-8+ or Clang to use UBSAN

Just like how we require GCC-8.2 for KASAN due to compiler bugs, require a sane version of GCC for UBSAN.

Quoted 6 lines
Quoted 3 lines

Specifically, before GCC-8 UBSAN doesn't respect -fwrapv and thinks signed arithmetic is buggered.

Actually removing CONFIG_UBSAN_SIGNED_OVERFLOW would give us the same effect without restricting GCC versions.

Is that preferable? Always happy to remove code, just need some justification behind it.

Signature · 1 line
Josh
#12 lore raw

Re: [PATCH] ubsan: Require GCC-8+ or Clang to use UBSAN

Quoted 31 lines · Josh Poimboeuf
Quoted 30 lines · Andrey Ryabinin
Quoted 26 lines · Peter Zijlstra
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Josh Poimboeuf

GCC 7 has a known bug where UBSAN ignores '-fwrapv' and generates false signed-overflow-UB warnings. The type mismatch between 'i' and 'nr_segs' in copy_compat_iovec_from_user() is causing such a warning, which also happens to violate uaccess rules:

lib/iov_iter.o: warning: objtool: iovec_from_user()+0x22d: call to __ubsan_handle_add_overflow() with UACCESS enabled

Quoted 7 lines
Quoted 5 lines

Fix it by making the variable types match.

This is similar to a previous commit:

29da93fea3ea ("mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions")

Maybe it's time we make UBSAN builds depend on GCC-8+ ?

---
Subject: ubsan: Require GCC-8+ or Clang to use UBSAN

Just like how we require GCC-8.2 for KASAN due to compiler bugs, require a sane version of GCC for UBSAN.

Specifically, before GCC-8 UBSAN doesn't respect -fwrapv and thinks signed arithmetic is buggered.

Quoted 5 lines
Quoted 2 lines

Actually removing CONFIG_UBSAN_SIGNED_OVERFLOW would give us the same effect without restricting GCC versions.

Is that preferable? Always happy to remove code, just need some justification behind it.

Andrey,

Is Peter's patch acceptable or do you want to do something else?

Signature · 1 line
Josh
#13 lore raw

Re: [PATCH] ubsan: Require GCC-8+ or Clang to use UBSAN

Quoted 35 lines · Josh Poimboeuf
Quoted 34 lines · Josh Poimboeuf
Quoted 32 lines · Andrey Ryabinin
Quoted 17 lines · Peter Zijlstra
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Josh Poimboeuf

GCC 7 has a known bug where UBSAN ignores '-fwrapv' and generates false signed-overflow-UB warnings. The type mismatch between 'i' and 'nr_segs' in copy_compat_iovec_from_user() is causing such a warning, which also happens to violate uaccess rules:

lib/iov_iter.o: warning: objtool: iovec_from_user()+0x22d: call to __ubsan_handle_add_overflow() with UACCESS enabled

Quoted 7 lines
Quoted 5 lines

Fix it by making the variable types match.

This is similar to a previous commit:

29da93fea3ea ("mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions")

Maybe it's time we make UBSAN builds depend on GCC-8+ ?

Quoted 9 lines

---
Subject: ubsan: Require GCC-8+ or Clang to use UBSAN

Just like how we require GCC-8.2 for KASAN due to compiler bugs, require a sane version of GCC for UBSAN.

Specifically, before GCC-8 UBSAN doesn't respect -fwrapv and thinks signed arithmetic is buggered.

Actually removing CONFIG_UBSAN_SIGNED_OVERFLOW would give us the same effect without restricting GCC versions.

Quoted 7 lines
Quoted 2 lines

Is that preferable? Always happy to remove code, just need some justification behind it.

Andrey,

Is Peter's patch acceptable or do you want to do something else?

I do prefer to just remove the code, I'll send the patch shortly.

#14 lore raw

Re: [PATCH] ubsan: Require GCC-8+ or Clang to use UBSAN

Quoted 11 lines · Andrey Rybainin
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines

Subject: ubsan: Require GCC-8+ or Clang to use UBSAN

Just like how we require GCC-8.2 for KASAN due to compiler bugs, require a sane version of GCC for UBSAN.

Specifically, before GCC-8 UBSAN doesn't respect -fwrapv and thinks signed arithmetic is buggered.

Actually removing CONFIG_UBSAN_SIGNED_OVERFLOW would give us the same effect without restricting GCC versions.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Is that preferable? Always happy to remove code, just need some justification behind it.

Is Peter's patch acceptable or do you want to do something else?

I do prefer to just remove the code, I'll send the patch shortly.

I have a specific goal of getting both signed and unsigned overflow detection working sanely, so removing this entirely from the kernel really makes working on that difficult. :)

I view the primary problem as compiler-specific. I'd much rather we correctly mask against versions (or better yet, behaviors).

Signature · 1 line
Kees Cook