mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/5] uaccess: Updates to scoped_user_access()
@ 2025-12-20 21:56 david.laight.linux
  2025-12-20 21:56 ` [PATCH 1/5] uaccess: Fix scoped_user_read_access() for 'pointer to const' david.laight.linux
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: david.laight.linux @ 2025-12-20 21:56 UTC (permalink / raw)
  To: Thomas Gleixner, Linus Torvalds, Peter Zijlstra,
	Mathieu Desnoyers, Kees Cook, linux-kernel, akpm, Al Viro
  Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

Converting kernel/signal.c to use scoped_user_access() had compilation
warnings because some of the pointers are 'pointer to const'.
This is fixed in patch 1.

Patches 2 and 3 factor out the 'autoterminating nested for loops'.
I'm sure there'll be a 'bikeshed' discussion about the names.

I think the two 'typeof(uptr)' could be 'auto', compiles for me
but IIRC there are issues with old compilers.

Patch 4 stops warnings from -Wshadow (enabled by W=2).
I did think about making the _diag_xxx conditional on a W=2 build,
but since the pre-processor just emits #pragma lines and they are
smaller that the for() loop it doesn't seem worth while.

Patch 5 is the change to signal.c that prompted patch 1.
The generated code looks fine, but I've not tested it.
Most of the changes are to 'compat' code - so are probably not
usually performance critical.
IIRC the non-compat code uses copy_to/from_user() for the structures
so is probably slower than the compat code's member by member copy.

David Laight (5):
  uaccess: Fix scoped_user_read_access() for 'pointer to const'
  compiler.h: Add generic support for 'autoterminating nested for()
    loops'
  uaccess.h: Use with() and and_with() in __scoped_user_access()
  uaccess: Disable -Wshadow in __scoped_user_access()
  signal: Use scoped_user_access() instead of __put/get_user()

 include/linux/compiler.h | 26 +++++++++++++++
 include/linux/uaccess.h  | 32 +++++++++---------
 kernel/signal.c          | 72 +++++++++++++++++++++++-----------------
 3 files changed, 84 insertions(+), 46 deletions(-)

-- 
2.39.5


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/5] uaccess: Fix scoped_user_read_access() for 'pointer to const'
  2025-12-20 21:56 [PATCH 0/5] uaccess: Updates to scoped_user_access() david.laight.linux
@ 2025-12-20 21:56 ` david.laight.linux
  2025-12-20 21:56 ` [PATCH 2/5] compiler.h: Add generic support for 'autoterminating nested for() loops' david.laight.linux
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: david.laight.linux @ 2025-12-20 21:56 UTC (permalink / raw)
  To: Thomas Gleixner, Linus Torvalds, Peter Zijlstra,
	Mathieu Desnoyers, Kees Cook, linux-kernel, akpm, Al Viro
  Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

If a 'const struct foo __user *ptr' is used for the address passed
to scoped_user_read_access() then you get a warning/error
uaccess.h:691:1: error: initialization discards 'const' qualifier
    from pointer target type [-Werror=discarded-qualifiers]
for the
    void __user *_tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl)
assignment.

Fix by using typeof(uptr) in that assignment and changing the 'read' functions
to use 'const void __user *ptr' rather than 'void __user *ptr'.

Fixes: e497310b4ffb "(uaccess: Provide scoped user access regions)"
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
 include/linux/uaccess.h | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index 1f3804245c06..c5d5f2d395bc 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -650,32 +650,32 @@ static inline void user_access_restore(unsigned long flags) { }
 #define user_rw_access_end()		user_access_end()
 
 /* Scoped user access */
-#define USER_ACCESS_GUARD(_mode)				\
-static __always_inline void __user *				\
-class_user_##_mode##_begin(void __user *ptr)			\
+#define USER_ACCESS_GUARD(_mode, type)				\
+static __always_inline type __user *				\
+class_user_##_mode##_begin(type __user *ptr)			\
 {								\
 	return ptr;						\
 }								\
 								\
 static __always_inline void					\
-class_user_##_mode##_end(void __user *ptr)			\
+class_user_##_mode##_end(type __user *ptr)			\
 {								\
 	user_##_mode##_access_end();				\
 }								\
 								\
-DEFINE_CLASS(user_ ##_mode## _access, void __user *,		\
+DEFINE_CLASS(user_ ##_mode## _access, type __user *,		\
 	     class_user_##_mode##_end(_T),			\
-	     class_user_##_mode##_begin(ptr), void __user *ptr)	\
+	     class_user_##_mode##_begin(ptr), type __user *ptr)	\
 								\
 static __always_inline class_user_##_mode##_access_t		\
-class_user_##_mode##_access_ptr(void __user *scope)		\
+class_user_##_mode##_access_ptr(type __user *scope)		\
 {								\
 	return scope;						\
 }
 
-USER_ACCESS_GUARD(read)
-USER_ACCESS_GUARD(write)
-USER_ACCESS_GUARD(rw)
+USER_ACCESS_GUARD(read, const void)
+USER_ACCESS_GUARD(write, void)
+USER_ACCESS_GUARD(rw, void)
 #undef USER_ACCESS_GUARD
 
 /**
@@ -752,7 +752,7 @@ USER_ACCESS_GUARD(rw)
  */
 #define __scoped_user_access(mode, uptr, size, elbl)					\
 for (bool done = false; !done; done = true)						\
-	for (void __user *_tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl); \
+	for (typeof(uptr) _tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl); \
 	     !done; done = true)							\
 		for (CLASS(user_##mode##_access, scope)(_tmpptr); !done; done = true)	\
 			/* Force modified pointer usage within the scope */		\
-- 
2.39.5


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/5] compiler.h: Add generic support for 'autoterminating nested for() loops'
  2025-12-20 21:56 [PATCH 0/5] uaccess: Updates to scoped_user_access() david.laight.linux
  2025-12-20 21:56 ` [PATCH 1/5] uaccess: Fix scoped_user_read_access() for 'pointer to const' david.laight.linux
@ 2025-12-20 21:56 ` david.laight.linux
  2025-12-20 21:56 ` [PATCH 3/5] uaccess.h: Use with() and and_with() in __scoped_user_access() david.laight.linux
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: david.laight.linux @ 2025-12-20 21:56 UTC (permalink / raw)
  To: Thomas Gleixner, Linus Torvalds, Peter Zijlstra,
	Mathieu Desnoyers, Kees Cook, linux-kernel, akpm, Al Viro
  Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

Autoterminating nested for() loops can be used inside #defines to
declare variables that are scoped to the statement that follows.
These are used by __scoped_user_access() but may have other uses
and the gory details are best separated from the use.

Using 'with (declaration)' and 'and_with (declaration)' seems to
read reasonably well and doesn't seem to collide with any existing
code.

As an example the scoped user access definition becomes:
	with (typeof(uptr) _tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl)) \
		and_with (CLASS(user_##mode##_access, scope)(_tmpptr))			\
		/* Force modified pointer usage within the scope */			\
		and_with (const typeof(uptr) uptr = _tmpptr)

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
 include/linux/compiler.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 04487c9bd751..da89a5efda5a 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -377,6 +377,32 @@ static inline void *offset_to_ptr(const int *off)
  */
 #define prevent_tail_call_optimization()	mb()
 
+/*
+ * Sometimes a #define needs to declare a variable that is scoped
+ * to the statement that follows without having mismatched {}.
+ *	with (int x = expression) {
+ *		statements
+ *	}
+ * is the same as:
+ *	{
+ *		int x = expression;
+ *		statements
+ *	}
+ * but lets it all be hidden from the call site, eg:
+ *	frobnicate(args) {
+ *		statements
+ *	} 
+ * Only a single variable can be defined, and_with() allows extra ones
+ * without adding an additional outer loop.
+ *
+ * The controlled scope can be terminated using break, continue or goto.
+ */
+#define with(declaration) \
+	for (bool _with_done = false; !_with_done; _with_done = true)	\
+		and_with (declaration)
+#define and_with(declaration) \
+	for (declaration; !_with_done; _with_done = true)
+
 #include <asm/rwonce.h>
 
 #endif /* __LINUX_COMPILER_H */
-- 
2.39.5


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 3/5] uaccess.h: Use with() and and_with() in __scoped_user_access()
  2025-12-20 21:56 [PATCH 0/5] uaccess: Updates to scoped_user_access() david.laight.linux
  2025-12-20 21:56 ` [PATCH 1/5] uaccess: Fix scoped_user_read_access() for 'pointer to const' david.laight.linux
  2025-12-20 21:56 ` [PATCH 2/5] compiler.h: Add generic support for 'autoterminating nested for() loops' david.laight.linux
@ 2025-12-20 21:56 ` david.laight.linux
  2025-12-20 21:56 ` [PATCH 4/5] uaccess: Disable -Wshadow " david.laight.linux
  2025-12-20 21:56 ` [PATCH 5/5] signal: Use scoped_user_access() instead of __put/get_user() david.laight.linux
  4 siblings, 0 replies; 6+ messages in thread
From: david.laight.linux @ 2025-12-20 21:56 UTC (permalink / raw)
  To: Thomas Gleixner, Linus Torvalds, Peter Zijlstra,
	Mathieu Desnoyers, Kees Cook, linux-kernel, akpm, Al Viro
  Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

Wrappers for autoterminating nested for() loops have been added to
compiler.h, use them to hide the gory details.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
 include/linux/uaccess.h | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index c5d5f2d395bc..589cb2ef2e7b 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -751,12 +751,10 @@ USER_ACCESS_GUARD(rw, void)
  * Don't use directly. Use scoped_masked_user_$MODE_access() instead.
  */
 #define __scoped_user_access(mode, uptr, size, elbl)					\
-for (bool done = false; !done; done = true)						\
-	for (typeof(uptr) _tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl); \
-	     !done; done = true)							\
-		for (CLASS(user_##mode##_access, scope)(_tmpptr); !done; done = true)	\
-			/* Force modified pointer usage within the scope */		\
-			for (const typeof(uptr) uptr = _tmpptr; !done; done = true)
+	with (typeof(uptr) _tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl)) \
+		and_with (CLASS(user_##mode##_access, scope)(_tmpptr))			\
+		/* Force modified pointer usage within the scope */			\
+		and_with (const typeof(uptr) uptr = _tmpptr)
 
 /**
  * scoped_user_read_access_size - Start a scoped user read access with given size
-- 
2.39.5


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 4/5] uaccess: Disable -Wshadow in __scoped_user_access()
  2025-12-20 21:56 [PATCH 0/5] uaccess: Updates to scoped_user_access() david.laight.linux
                   ` (2 preceding siblings ...)
  2025-12-20 21:56 ` [PATCH 3/5] uaccess.h: Use with() and and_with() in __scoped_user_access() david.laight.linux
@ 2025-12-20 21:56 ` david.laight.linux
  2025-12-20 21:56 ` [PATCH 5/5] signal: Use scoped_user_access() instead of __put/get_user() david.laight.linux
  4 siblings, 0 replies; 6+ messages in thread
From: david.laight.linux @ 2025-12-20 21:56 UTC (permalink / raw)
  To: Thomas Gleixner, Linus Torvalds, Peter Zijlstra,
	Mathieu Desnoyers, Kees Cook, linux-kernel, akpm, Al Viro
  Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

-Wshadow is enabled by W=2 builds and __scoped_user_access() quite
deliberately creates a 'const' shadow of the 'user' address that
references a 'guard page' when the application passes a kernel pointer.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
 include/linux/uaccess.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index 589cb2ef2e7b..e2ff4146c5d0 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -754,7 +754,9 @@ USER_ACCESS_GUARD(rw, void)
 	with (typeof(uptr) _tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl)) \
 		and_with (CLASS(user_##mode##_access, scope)(_tmpptr))			\
 		/* Force modified pointer usage within the scope */			\
-		and_with (const typeof(uptr) uptr = _tmpptr)
+		__diag_push() __diag_ignore_all("-Wshadow", "uptr is readonly copy")	\
+		and_with (const typeof(uptr) uptr = _tmpptr)				\
+		__diag_pop()
 
 /**
  * scoped_user_read_access_size - Start a scoped user read access with given size
-- 
2.39.5


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 5/5] signal: Use scoped_user_access() instead of __put/get_user()
  2025-12-20 21:56 [PATCH 0/5] uaccess: Updates to scoped_user_access() david.laight.linux
                   ` (3 preceding siblings ...)
  2025-12-20 21:56 ` [PATCH 4/5] uaccess: Disable -Wshadow " david.laight.linux
@ 2025-12-20 21:56 ` david.laight.linux
  4 siblings, 0 replies; 6+ messages in thread
From: david.laight.linux @ 2025-12-20 21:56 UTC (permalink / raw)
  To: Thomas Gleixner, Linus Torvalds, Peter Zijlstra,
	Mathieu Desnoyers, Kees Cook, linux-kernel, akpm, Al Viro
  Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

Mechanically change the access_ok() and __get/put_user() to use
scoped_user_read/write_access() and unsafe_get/put_user().

This generates better code with fewer STAC/CLAC pairs.

It also ensures that access_ok() is called near the user accesses.
I failed to find the one for __save_altstack().

Looking at the change, perhaps there should be aliases:
#define scoped_put_user unsafe_put_user
#define scoped_get_user unsafe_get_user

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
 kernel/signal.c | 72 ++++++++++++++++++++++++++++---------------------
 1 file changed, 42 insertions(+), 30 deletions(-)

diff --git a/kernel/signal.c b/kernel/signal.c
index e42b8bd6922f..806b19041f72 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -4469,10 +4469,16 @@ int restore_altstack(const stack_t __user *uss)
 int __save_altstack(stack_t __user *uss, unsigned long sp)
 {
 	struct task_struct *t = current;
-	int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
-		__put_user(t->sas_ss_flags, &uss->ss_flags) |
-		__put_user(t->sas_ss_size, &uss->ss_size);
-	return err;
+
+	scoped_user_write_access(uss, Efault) {
+		unsafe_put_user((void __user *)t->sas_ss_sp, &uss->ss_sp, Efault);
+		unsafe_put_user(t->sas_ss_flags, &uss->ss_flags, Efault);
+		unsafe_put_user(t->sas_ss_size, &uss->ss_size, Efault);
+	}
+	return 0;
+
+Efault:
+	return -EFAULT;
 }
 
 #ifdef CONFIG_COMPAT
@@ -4705,12 +4711,12 @@ SYSCALL_DEFINE3(sigaction, int, sig,
 
 	if (act) {
 		old_sigset_t mask;
-		if (!access_ok(act, sizeof(*act)) ||
-		    __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
-		    __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
-		    __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
-		    __get_user(mask, &act->sa_mask))
-			return -EFAULT;
+		scoped_user_read_access(act, Efault) {
+		    unsafe_get_user(new_ka.sa.sa_handler, &act->sa_handler, Efault);
+		    unsafe_get_user(new_ka.sa.sa_restorer, &act->sa_restorer, Efault);
+		    unsafe_get_user(new_ka.sa.sa_flags, &act->sa_flags, Efault);
+		    unsafe_get_user(mask, &act->sa_mask, Efault);
+		}
 #ifdef __ARCH_HAS_KA_RESTORER
 		new_ka.ka_restorer = NULL;
 #endif
@@ -4720,15 +4726,18 @@ SYSCALL_DEFINE3(sigaction, int, sig,
 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
 
 	if (!ret && oact) {
-		if (!access_ok(oact, sizeof(*oact)) ||
-		    __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
-		    __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
-		    __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
-		    __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
-			return -EFAULT;
+		scoped_user_write_access(oact, Efault) {
+		    unsafe_put_user(old_ka.sa.sa_handler, &oact->sa_handler, Efault);
+		    unsafe_put_user(old_ka.sa.sa_restorer, &oact->sa_restorer, Efault);
+		    unsafe_put_user(old_ka.sa.sa_flags, &oact->sa_flags, Efault);
+		    unsafe_put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask, Efault);
+		}
 	}
 
 	return ret;
+
+Efault:
+	return -EFAULT;
 }
 #endif
 #ifdef CONFIG_COMPAT_OLD_SIGACTION
@@ -4742,12 +4751,12 @@ COMPAT_SYSCALL_DEFINE3(sigaction, int, sig,
 	compat_uptr_t handler, restorer;
 
 	if (act) {
-		if (!access_ok(act, sizeof(*act)) ||
-		    __get_user(handler, &act->sa_handler) ||
-		    __get_user(restorer, &act->sa_restorer) ||
-		    __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
-		    __get_user(mask, &act->sa_mask))
-			return -EFAULT;
+		scoped_user_read_access(act, Efault) {
+		    unsafe_get_user(handler, &act->sa_handler, Efault);
+		    unsafe_get_user(restorer, &act->sa_restorer, Efault);
+		    unsafe_get_user(new_ka.sa.sa_flags, &act->sa_flags, Efault);
+		    unsafe_get_user(mask, &act->sa_mask, Efault);
+		}
 
 #ifdef __ARCH_HAS_KA_RESTORER
 		new_ka.ka_restorer = NULL;
@@ -4760,16 +4769,19 @@ COMPAT_SYSCALL_DEFINE3(sigaction, int, sig,
 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
 
 	if (!ret && oact) {
-		if (!access_ok(oact, sizeof(*oact)) ||
-		    __put_user(ptr_to_compat(old_ka.sa.sa_handler),
-			       &oact->sa_handler) ||
-		    __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
-			       &oact->sa_restorer) ||
-		    __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
-		    __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
-			return -EFAULT;
+		scoped_user_write_access(oact, Efault) {
+		    unsafe_put_user(ptr_to_compat(old_ka.sa.sa_handler),
+			       &oact->sa_handler, Efault);
+		    unsafe_put_user(ptr_to_compat(old_ka.sa.sa_restorer),
+			       &oact->sa_restorer, Efault);
+		    unsafe_put_user(old_ka.sa.sa_flags, &oact->sa_flags, Efault);
+		    unsafe_put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask, Efault);
+		}
 	}
 	return ret;
+
+Efault:
+	return -EFAULT;
 }
 #endif
 
-- 
2.39.5


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-12-20 21:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-20 21:56 [PATCH 0/5] uaccess: Updates to scoped_user_access() david.laight.linux
2025-12-20 21:56 ` [PATCH 1/5] uaccess: Fix scoped_user_read_access() for 'pointer to const' david.laight.linux
2025-12-20 21:56 ` [PATCH 2/5] compiler.h: Add generic support for 'autoterminating nested for() loops' david.laight.linux
2025-12-20 21:56 ` [PATCH 3/5] uaccess.h: Use with() and and_with() in __scoped_user_access() david.laight.linux
2025-12-20 21:56 ` [PATCH 4/5] uaccess: Disable -Wshadow " david.laight.linux
2025-12-20 21:56 ` [PATCH 5/5] signal: Use scoped_user_access() instead of __put/get_user() david.laight.linux

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®