uaccess: Prepare for masked user access on powerpc
Subsystems (7)
v5 cover uaccess: Prepare for masked user access on powerpc
This is v5 of the series "powerpc: Implement masked user access". This version only includes the preparatory patches to enable merging of powerpc architecture patches that depend on them on next cycle.
It applies on top of commit 6ec821f050e2 (tag: core-scoped-uaccess) from tip tree.
Thomas, Peter, could you please take those preparatory patches in tip tree for v6.19, then Maddy will take powerpc patches into powerpc-next for v6.20.
Masked user access avoids the address/size verification by access_ok(). Allthough its main purpose is to skip the speculation in the verification of user address and size hence avoid the need of spec mitigation, it also has the advantage to reduce the amount of instructions needed so it also benefits to platforms that don't need speculation mitigation, especially when the size of the copy is not know at build time.
Patches 1 and 2 are preparing to clean-up some redundant barrier_nospec() introduced by commit 74e19ef0ff80 ("uaccess: Add speculation barrier to copy_from_user()"). To do that, a speculation barrier is added to copy_from_user_iter() so that the barrier in powerpc raw_copy_from_user() which is redundant with the one in copy_from_user() can be removed. To avoid impacting x86, copy_from_user_iter() is first converted to using masked user access.
Patch 3 convert put_cmsg() to scoped user access.
Patch 4 replaces remaining wrong calls to masked_user_access_begin() with calls to masked_user_read_access_begin() and masked_user_write_access_begin() to match with user_read_access_end() and user_write_access_end().
Entire series is availiable at https://git.kernel.org/pub/scm/linux/kernel/git/chleroy/linux branch masked-uaccess
Changes in v5:
- Taken comments from tglx
- Only include core preparatory patches, powerpc patches are kept for following kernel development cycle
- Reworded patch 1 to make it more explicit it needs to come before patch 2
- Added patch 3 to convert put_cmsg() to scoped user access instead of just fixing the direction of the uacess
- Renamed patch 4 as it now only handles fonctions from lib/strn*.c
Changes in v4:
- Rebased on top of commit 6ec821f050e2 (tag: core-scoped-uaccess) from tip tree
- Patch 3: Simplified as masked_user_read_access_begin() and masked_user_write_access_begin() are already there.
- Patch 10: Simplified mask_user_address_simple() as suggested by Gabriel.
Changes in v3:
- Rebased on top of v6.18-rc1
- Patch 3: Impact on recently modified net/core/scm.c
- Patch 10: Rewrite mask_user_address_simple() for a smaller result on powerpc64, suggested by Gabriel
Changes in v2:
- Converted copy_from_user_iter() to using masked user access.
- Cleaned up powerpc uaccess function to minimise code duplication when adding masked user access
- Automated TASK_SIZE calculation to minimise use of BUILD_BUG_ON()
- Tried to make some commit messages more clean based on feedback from version 1 of the series.
Christophe Leroy (4):
iov_iter: Convert copy_from_user_iter() to masked user access
iov_iter: Add missing speculation barrier to copy_from_user_iter()
scm: Convert put_cmsg() to scoped user access lib/strn*,uaccess: Use masked_user_{read/write}_access_begin when
required
lib/iov_iter.c | 22 +++++++++++++++++----- lib/strncpy_from_user.c | 2 +- lib/strnlen_user.c | 2 +- net/core/scm.c | 20 +++++++------------- 4 files changed, 26 insertions(+), 20 deletions(-)
Signature · 1 line
2.49.0
PATCH v5 1/4 iov_iter: Convert copy_from_user_iter() to masked user access
Following patch will add missing barrier_nospec() to copy_from_user_iter(). On some architecture like x86 it might degrade performance, which would be unfortunate as copy_from_user_iter() is a critical function.
Convert copy_from_user_iter() to using masked user access on architecture that support it.
This is similar to what was done for copy_from_user() by commit 0fc810ae3ae1 ("x86/uaccess: Avoid barrier_nospec() in 64-bit copy_from_user()")
---
v5: Changed commit message
v2: New in v2
--- lib/iov_iter.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
lib/iov_iter.c +10 -6
@@ -49,12 +49,16 @@ size_t copy_from_user_iter(void __user *iter_from, size_t progress,if (should_fail_usercopy ())return len; -if (access_ok (iter_from, len)) { - to += progress; -instrument_copy_from_user_before (to, iter_from, len); - res =raw_copy_from_user (to, iter_from, len); -instrument_copy_from_user_after (to, iter_from, len, res); - } +if (can_do_masked_user_access ()) + iter_from =mask_user_address (iter_from); +else if (!access_ok (iter_from, len)) +return res; + + to += progress; +instrument_copy_from_user_before (to, iter_from, len); + res =raw_copy_from_user (to, iter_from, len); +instrument_copy_from_user_after (to, iter_from, len, res); +return res; }
Signature · 1 line
2.49.0
PATCH v5 2/4 iov_iter: Add missing speculation barrier to copy_from_user_iter()
In reply to Christophe LeroyThe results of "access_ok()" can be mis-speculated. The result is that you can end speculatively:
if (access_ok(from, size)) // Right here
For the same reason as done in copy_from_user() by
commit 74e19ef0ff80 ("uaccess: Add speculation barrier to copy_from_user()"), add a speculation barrier to copy_from_user_iter().
--- lib/iov_iter.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
lib/iov_iter.c +11 -3
@@ -49,11 +49,19 @@ size_t copy_from_user_iter(void __user *iter_from, size_t progress,if (should_fail_usercopy ())return len; -if (can_do_masked_user_access ()) +if (can_do_masked_user_access ()) { iter_from =mask_user_address (iter_from); -else if (!access_ok (iter_from, len)) -return res; + }else { +if (!access_ok (iter_from, len)) +return res; +/* +* Ensure that bad access_ok() speculation will not +* lead to nasty side effects *after* the copy is +* finished: +*/ +barrier_nospec (); + } to += progress;instrument_copy_from_user_before (to, iter_from, len); res =raw_copy_from_user (to, iter_from, len);
Signature · 1 line
2.49.0
PATCH v5 3/4 scm: Convert put_cmsg() to scoped user access
In reply to Christophe LeroyReplace the open coded implementation with the scoped user access guards.
No functional change intended.
---
v5: New
--- net/core/scm.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-)
net/core/scm.c +7 -13
@@ -273,17 +273,13 @@ 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); -else if (!user_write_access_begin (cm, cmlen)) - goto efault; - -unsafe_put_user (cmlen, &cm->cmsg_len, efault_end); -unsafe_put_user (level, &cm->cmsg_level, efault_end); -unsafe_put_user (type, &cm->cmsg_type, efault_end); -unsafe_copy_to_user (CMSG_USER_DATA (cm), data, - cmlen -sizeof (*cm), efault_end); -user_write_access_end (); + scoped_user_write_access_size(cm, cmlen, efault) { +unsafe_put_user (cmlen, &cm->cmsg_len, efault); +unsafe_put_user (level, &cm->cmsg_level, efault); +unsafe_put_user (type, &cm->cmsg_type, efault); +unsafe_copy_to_user (CMSG_USER_DATA (cm), data, + cmlen -sizeof (*cm), efault); + } }else {struct cmsghdr *cm = msg->msg_control;
@@ -301,8 +297,6 @@ int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data) msg->msg_controllen -= cmlen;return 0 ; -efault_end: -user_write_access_end (); efault:return -EFAULT ; }
Signature · 1 line
2.49.0
PATCH v5 4/4 lib/strn*,uaccess: Use masked_user_{read/write}_access_begin when required
In reply to Christophe LeroyProperly use masked_user_read_access_begin() and masked_user_write_access_begin() instead of masked_user_access_begin() in order to match user_read_access_end() and user_write_access_end(). This is important for architectures like powerpc that enable separately user reads and user writes.
That means masked_user_read_access_begin() is used when user memory is exclusively read during the window and 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().
---
v5:
- Removed net/core/scm.c which is converted to scope user access by previous patch
- Renamed the patch as it now only handles lib/strncpy_from_user.c and lib/strnlen_user.c
v4: Rebased on top of core-scoped-uaccess tag
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.
--- lib/strncpy_from_user.c | 2 +- lib/strnlen_user.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
lib/strncpy_from_user.c +1 -1
@@ -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;
lib/strnlen_user.c +1 -1
@@ -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;
Signature · 1 line
2.49.0
Quoted 10 lines · Christophe Leroy
This is v5 of the series "powerpc: Implement masked user access". This version only includes the preparatory patches to enable merging of powerpc architecture patches that depend on them on next cycle.
It applies on top of commit
6ec821f050e2(tag: core-scoped-uaccess) from tip tree.Thomas, Peter, could you please take those preparatory patches in tip tree for v6.19, then Maddy will take powerpc patches into powerpc-next for v6.20.
I've applied them to tip core/uaccess, which contains only the uaccess related bits. That branch is immutable and could be consumed by PPC if required.
Thanks,
tglx