[PATCH v3 03/10] uaccess: Add masked_user_{read/write}_access_begin

Christophe Leroy posted 10 patches 3 months, 3 weeks ago
There is a newer version of this series
[PATCH v3 03/10] uaccess: Add masked_user_{read/write}_access_begin
Posted by Christophe Leroy 3 months, 3 weeks ago
Allthough masked_user_access_begin() is to only be used when reading
data from user at the moment, introduce masked_user_read_access_begin()
and masked_user_write_access_begin() in order to match
user_read_access_begin() and user_write_access_begin().

That means masked_user_read_access_begin() is used when user memory is
exclusively read during the window, masked_user_write_access_begin()
is used when user memory is exclusively writen during the window,
masked_user_access_begin() remains and is used when both reads and
writes are performed during the open window. Each of them is expected
to be terminated by the matching user_read_access_end(),
user_write_access_end() and user_access_end().

Have them default to masked_user_access_begin() when they are
not defined.

Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
---
v3: Rebased on top of v6.18-rc1 ==> change in net/core/scm.c

v2: Added more explanations in the commit message following comments received.
---
 fs/select.c             | 2 +-
 include/linux/uaccess.h | 7 +++++++
 kernel/futex/futex.h    | 4 ++--
 lib/strncpy_from_user.c | 2 +-
 lib/strnlen_user.c      | 2 +-
 net/core/scm.c          | 2 +-
 6 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/fs/select.c b/fs/select.c
index 082cf60c7e235..36db0359388c8 100644
--- a/fs/select.c
+++ b/fs/select.c
@@ -777,7 +777,7 @@ static inline int get_sigset_argpack(struct sigset_argpack *to,
 	// the path is hot enough for overhead of copy_from_user() to matter
 	if (from) {
 		if (can_do_masked_user_access())
-			from = masked_user_access_begin(from);
+			from = masked_user_read_access_begin(from);
 		else if (!user_read_access_begin(from, sizeof(*from)))
 			return -EFAULT;
 		unsafe_get_user(to->p, &from->p, Efault);
diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index 1beb5b395d81d..aa48d5415d32f 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -41,6 +41,13 @@
  #define mask_user_address(src) (src)
 #endif
 
+#ifndef masked_user_write_access_begin
+#define masked_user_write_access_begin masked_user_access_begin
+#endif
+#ifndef masked_user_read_access_begin
+#define masked_user_read_access_begin masked_user_access_begin
+#endif
+
 /*
  * Architectures should provide two primitives (raw_copy_{to,from}_user())
  * and get rid of their private instances of copy_{to,from}_user() and
diff --git a/kernel/futex/futex.h b/kernel/futex/futex.h
index 2cd57096c38e1..a1120a318c186 100644
--- a/kernel/futex/futex.h
+++ b/kernel/futex/futex.h
@@ -303,7 +303,7 @@ static __always_inline int futex_get_value(u32 *dest, u32 __user *from)
 	u32 val;
 
 	if (can_do_masked_user_access())
-		from = masked_user_access_begin(from);
+		from = masked_user_read_access_begin(from);
 	else if (!user_read_access_begin(from, sizeof(*from)))
 		return -EFAULT;
 	unsafe_get_user(val, from, Efault);
@@ -318,7 +318,7 @@ static __always_inline int futex_get_value(u32 *dest, u32 __user *from)
 static __always_inline int futex_put_value(u32 val, u32 __user *to)
 {
 	if (can_do_masked_user_access())
-		to = masked_user_access_begin(to);
+		to = masked_user_write_access_begin(to);
 	else if (!user_write_access_begin(to, sizeof(*to)))
 		return -EFAULT;
 	unsafe_put_user(val, to, Efault);
diff --git a/lib/strncpy_from_user.c b/lib/strncpy_from_user.c
index 6dc234913dd58..5bb752ff7c61b 100644
--- a/lib/strncpy_from_user.c
+++ b/lib/strncpy_from_user.c
@@ -126,7 +126,7 @@ long strncpy_from_user(char *dst, const char __user *src, long count)
 	if (can_do_masked_user_access()) {
 		long retval;
 
-		src = masked_user_access_begin(src);
+		src = masked_user_read_access_begin(src);
 		retval = do_strncpy_from_user(dst, src, count, count);
 		user_read_access_end();
 		return retval;
diff --git a/lib/strnlen_user.c b/lib/strnlen_user.c
index 6e489f9e90f15..4a6574b67f824 100644
--- a/lib/strnlen_user.c
+++ b/lib/strnlen_user.c
@@ -99,7 +99,7 @@ long strnlen_user(const char __user *str, long count)
 	if (can_do_masked_user_access()) {
 		long retval;
 
-		str = masked_user_access_begin(str);
+		str = masked_user_read_access_begin(str);
 		retval = do_strnlen_user(str, count, count);
 		user_read_access_end();
 		return retval;
diff --git a/net/core/scm.c b/net/core/scm.c
index 66eaee783e8be..4a65f9baa87e7 100644
--- a/net/core/scm.c
+++ b/net/core/scm.c
@@ -274,7 +274,7 @@ int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
 		check_object_size(data, cmlen - sizeof(*cm), true);
 
 		if (can_do_masked_user_access())
-			cm = masked_user_access_begin(cm);
+			cm = masked_user_write_access_begin(cm);
 		else if (!user_write_access_begin(cm, cmlen))
 			goto efault;
 
-- 
2.49.0
Re: [PATCH v3 03/10] uaccess: Add masked_user_{read/write}_access_begin
Posted by Thomas Gleixner 3 months, 2 weeks ago
On Fri, Oct 17 2025 at 12:20, Christophe Leroy wrote:
> Allthough masked_user_access_begin() is to only be used when reading
> data from user at the moment, introduce masked_user_read_access_begin()
> and masked_user_write_access_begin() in order to match
> user_read_access_begin() and user_write_access_begin().
>
> That means masked_user_read_access_begin() is used when user memory is
> exclusively read during the window, masked_user_write_access_begin()
> is used when user memory is exclusively writen during the window,
> masked_user_access_begin() remains and is used when both reads and
> writes are performed during the open window. Each of them is expected
> to be terminated by the matching user_read_access_end(),
> user_write_access_end() and user_access_end().
>
> Have them default to masked_user_access_begin() when they are
> not defined.
>
> Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>

Can we please coordinate on that vs. the scoped_access() work as this
nicely collides all over the place?

Thanks,

        tglx
Re: [PATCH v3 03/10] uaccess: Add masked_user_{read/write}_access_begin
Posted by Christophe Leroy 3 months, 1 week ago

Le 22/10/2025 à 19:05, Thomas Gleixner a écrit :
> On Fri, Oct 17 2025 at 12:20, Christophe Leroy wrote:
>> Allthough masked_user_access_begin() is to only be used when reading
>> data from user at the moment, introduce masked_user_read_access_begin()
>> and masked_user_write_access_begin() in order to match
>> user_read_access_begin() and user_write_access_begin().
>>
>> That means masked_user_read_access_begin() is used when user memory is
>> exclusively read during the window, masked_user_write_access_begin()
>> is used when user memory is exclusively writen during the window,
>> masked_user_access_begin() remains and is used when both reads and
>> writes are performed during the open window. Each of them is expected
>> to be terminated by the matching user_read_access_end(),
>> user_write_access_end() and user_access_end().
>>
>> Have them default to masked_user_access_begin() when they are
>> not defined.
>>
>> Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
> 
> Can we please coordinate on that vs. the scoped_access() work as this
> nicely collides all over the place?

Sure, I will rebase on top of your series.

Once it is rebased, could you take the non powerpc patches in your tree ?

Thanks
Christophe
Re: [PATCH v3 03/10] uaccess: Add masked_user_{read/write}_access_begin
Posted by Thomas Gleixner 3 months, 1 week ago
On Tue, Nov 04 2025 at 07:39, Christophe Leroy wrote:
> Le 22/10/2025 à 19:05, Thomas Gleixner a écrit :
>> On Fri, Oct 17 2025 at 12:20, Christophe Leroy wrote:
>>> Allthough masked_user_access_begin() is to only be used when reading
>>> data from user at the moment, introduce masked_user_read_access_begin()
>>> and masked_user_write_access_begin() in order to match
>>> user_read_access_begin() and user_write_access_begin().
>>>
>>> That means masked_user_read_access_begin() is used when user memory is
>>> exclusively read during the window, masked_user_write_access_begin()
>>> is used when user memory is exclusively writen during the window,
>>> masked_user_access_begin() remains and is used when both reads and
>>> writes are performed during the open window. Each of them is expected
>>> to be terminated by the matching user_read_access_end(),
>>> user_write_access_end() and user_access_end().
>>>
>>> Have them default to masked_user_access_begin() when they are
>>> not defined.
>>>
>>> Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
>> 
>> Can we please coordinate on that vs. the scoped_access() work as this
>> nicely collides all over the place?
>
> Sure, I will rebase on top of your series.
>
> Once it is rebased, could you take the non powerpc patches in your tree ?

Sure. The current lot is at:

  git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git scoped-uaccess

Thanks,

        tglx