uaccess/sockptr: copy_struct_ fixes and more helpers
Subsystems (4)
cover uaccess/sockptr: copy_struct_ fixes and more helpers
Hi,
here are some patches related to copy_struct_{from,to}_{user,sockptr}()
I collected during my work on an IPPROTO_SMBDIRECT implementation wrapping the smbdirect code used by cifs.ko and ksmbd.ko.
The first patch fixes copy_struct_to_user() to behave like documented.
The 2nd patch fixes the case wherecopy_struct_from_user() is called bycopy_struct_from_sockptr().
The 3rd patch introduces copy_struct_{from,to}_bounce_buffer()
as a result of a discussion about the IPPROTO_QUIC driver in order to
be future prove when handling msg_control messages in sendmsg and recvmsg.
But I'll likely also use them in my IPPROTO_SMBDIRECT driver.
The 4th patch makes copy_struct_from_sockptr() a trivial wrapper switching between copy_struct_from_user() and copy_struct_from_bounce_buffer()
The 5th patch introduces copy_struct_to_sockptr() which I'll also use in my IPPROTO_SMBDIRECT driver.
As future cleanup I was thinking about trick to hide size_t ksize from the callers, similar to what was done with kmalloc_obj(). Whould others think that would be useful?
Stefan Metzmacher (5):
uaccess: fix ignored_trailing logic in copy_struct_to_user()
sockptr: fix usize check in copy_struct_from_sockptr() for user
pointers
uaccess: add copy_struct_{from,to}_bounce_buffer() helpers
sockptr: let copy_struct_from_sockptr() use
copy_struct_from_bounce_buffer()
sockptr: introduce copy_struct_to_sockptr()
include/linux/sockptr.h | 28 ++++++++---------- include/linux/uaccess.h | 65 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 17 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/5 uaccess: fix ignored_trailing logic in copy_struct_to_user()
Currently all callers pass ignored_trailing=NULL, but I have code that will make use of.
Now it actually behaves like documented:
* If @usize < @ksize, then the kernel is trying to pass userspace a newer struct than it supports. Thus we only copy the interoperable portions (@usize) and ignore the rest (but @ignored_trailing is set to %true if any of the trailing (@ksize - @usize) bytes are non-zero).
--- include/linux/uaccess.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/uaccess.h +1 -1
@@ -505,7 +505,7 @@ copy_struct_to_user(void __user *dst, size_t usize, const void *src,return -EFAULT ; }if (ignored_trailing) - *ignored_trailing = ksize < usize && + *ignored_trailing = usize < ksize &&memchr_inv (src + size,0 , rest) !=NULL ;/* Copy the interoperable parts of the struct. */ if (copy_to_user (dst, src, size))
Signature · 1 line
2.43.0
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1078270
---Test result---
Test Summary:
CheckPatch PENDING 0.53 seconds GitLint PENDING 0.35 seconds SubjectPrefix FAIL 0.68 seconds BuildKernel PASS 26.30 seconds CheckAllWarning PASS 28.58 seconds CheckSparse PASS 27.59 seconds BuildKernel32 PASS 25.24 seconds TestRunnerSetup PASS 563.43 seconds TestRunner_l2cap-tester PASS 27.70 seconds TestRunner_iso-tester PASS 41.94 seconds TestRunner_bnep-tester PASS 6.27 seconds TestRunner_mgmt-tester FAIL 114.63 seconds TestRunner_rfcomm-tester PASS 9.39 seconds TestRunner_sco-tester FAIL 14.42 seconds TestRunner_ioctl-tester PASS 10.14 seconds TestRunner_mesh-tester FAIL 11.56 seconds TestRunner_smp-tester PASS 8.64 seconds TestRunner_userchan-tester PASS 6.61 seconds IncrementalBuild PENDING 0.77 seconds
Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script
Output:
##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:
##############################
Test: SubjectPrefix - FAIL
Desc: Check subject contains "Bluetooth" prefix Output:
"Bluetooth: " prefix is not specified in the subject "Bluetooth: " prefix is not specified in the subject "Bluetooth: " prefix is not specified in the subject "Bluetooth: " prefix is not specified in the subject "Bluetooth: " prefix is not specified in the subject
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner Output:
Total: 494, Passed: 489 (99.0%), Failed: 1, Not Run: 4
Failed Test Cases
Read Exp Feature - Success Failed 0.105 seconds
##############################
Test: TestRunner_sco-tester - FAIL
Desc: Run sco-tester with test-runner
Output:
WARNING: possible circular locking dependency detected
BUG: sleeping function called from invalid context at net/core/sock.c:3782
Total: 30, Passed: 30 (100.0%), Failed: 0, Not Run: 0
##############################
Test: TestRunner_mesh-tester - FAIL
Desc: Run mesh-tester with test-runner Output:
Total: 10, Passed: 8 (80.0%), Failed: 2, Not Run: 0
Failed Test Cases
Mesh - Send cancel - 1 Timed out 1.832 seconds Mesh - Send cancel - 2 Timed out 2.000 seconds
##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series Output:
---
Regards,
Linux Bluetooth
Re: [PATCH 1/5] uaccess: fix ignored_trailing logic in copy_struct_to_user()
In reply to Stefan MetzmacherQuoted 9 lines · Stefan Metzmacher
Currently all callers pass ignored_trailing=NULL, but I have code that will make use of.
Now it actually behaves like documented:
* If @usize < @ksize, then the kernel is trying to pass userspace a newer struct than it supports. Thus we only copy the interoperable portions (@usize) and ignore the rest (but @ignored_trailing is set to %true if any of the trailing (@ksize - @usize) bytes are non-zero).
Good catch, though I want to mention that the current API design for copy_struct_to_user() is a bit of a compromise -- I was trying to think of a way of making it generic but what information you need really depends on your API.
For request-flag APIs (like statx) then you can just unset the bits in the response mask for fields past usize and so it is a non-fatal error, but it requires knowing which field offsets map to which flags.
My initial idea for ignored_trailing was for it to return the offset memchr_inv() gives you -- but unfortunately, this doesn't help in the more generic case where you have multiple non-zero bits that need to unset multiple flags.
Out of interest, how did you plan on using it? It might be a good idea to rethink this API before it starts getting used "in anger" in a way that leaks to uAPIs we can't change.
In any case, for this patch feel free to take my
Quoted 47 lines
Fixes 424a55a4a908 ("uaccess: add copy_struct_to_user helper")Cc Dmitry Safonov <[email protected]>Cc Dmitry Safonov <[email protected]>Cc Francesco Ruggeri <[email protected]>Cc Salam Noureddine <[email protected]>Cc David Ahern <[email protected]>Cc David S. Miller <[email protected]>Cc Michal Luczaj <[email protected]>Cc David Wei <[email protected]>Cc Luiz Augusto von Dentz <[email protected]>Cc Luiz Augusto von Dentz <[email protected]>Cc Marcel Holtmann <[email protected]>Cc Xin Long <[email protected]>Cc Eric Dumazet <[email protected]>Cc Kuniyuki Iwashima <[email protected]>Cc Paolo Abeni <[email protected]>Cc Willem de Bruijn <[email protected]>Cc Neal Cardwell <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Simon Horman <[email protected]>Cc Aleksa Sarai <[email protected]>Cc Christian Brauner <[email protected]>CC Kees Cook <[email protected]>Signed-off-by Stefan Metzmacher <[email protected]>--- include/linux/uaccess.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/uaccess.h +1
-1@@ -505,7 +505,7 @@ copy_struct_to_user(void __user *dst, size_t usize, const void *src,return -EFAULT ; }if (ignored_trailing) - *ignored_trailing = ksize < usize && + *ignored_trailing = usize < ksize &&memchr_inv (src + size,0 , rest) !=NULL ;/* Copy the interoperable parts of the struct. */ if (copy_to_user (dst, src, size))Signature · 1 line
2.43.0
Signature · 2 lines
Aleksa Sarai https://www.cyphar.com/
Re: [PATCH 1/5] uaccess: fix ignored_trailing logic in copy_struct_to_user()
Hi Aleksa,
Quoted 16 lines · Stefan Metzmacher
Quoted 9 lines
Currently all callers pass ignored_trailing=NULL, but I have code that will make use of.
Now it actually behaves like documented:
* If @usize < @ksize, then the kernel is trying to pass userspace a newer struct than it supports. Thus we only copy the interoperable portions (@usize) and ignore the rest (but @ignored_trailing is set to %true if any of the trailing (@ksize - @usize) bytes are non-zero).
Good catch, though I want to mention that the current API design for
copy_struct_to_user()is a bit of a compromise -- I was trying to think of a way of making it generic but what information you need really depends on your API.
Quoted 8 lines
For request-flag APIs (like statx) then you can just unset the bits in the response mask for fields past usize and so it is a non-fatal error, but it requires knowing which field offsets map to which flags.
My initial idea for
ignored_trailingwas for it to return the offsetmemchr_inv()gives you -- but unfortunately, this doesn't help in the more generic case where you have multiple non-zero bits that need to unset multiple flags.
I guess the caller could use if (ignored_trailing) { ... } to check more complex stuff and then decide ignore or return an error.
Quoted 3 lines
Out of interest, how did you plan on using it? It might be a good idea to rethink this API before it starts getting used "in anger" in a way that leaks to uAPIs we can't change.
Currently I only use it with WARN_ON_ONCE(ignored_trailing); in order to catch internal errors. See
https://git.samba.org/?p=metze/linux/wip.git;a=blob;f=fs/smb/common/smbdirect/smbdirect_proto.c;h=ce7c78eb6795041ba672da434ffb01db73269cb7;hb=37c61ef9758f3e113d4078220d8fc2aee366c955#l1625
But I guess I will at least change it to
if (WARN_ON_ONCE(ignored_trailing))
return...
And in general I thought it would be good practice to check that case in new code in order to avoid unexpected behavior.
Quoted 3 lines
In any case, for this patch feel free to take my
Reviewed-by Aleksa Sarai <[email protected]>
Thanks!
metze
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1078270
---Test result---
Test Summary:
CheckPatch PENDING 0.97 seconds GitLint PENDING 0.29 seconds SubjectPrefix FAIL 1.00 seconds BuildKernel PASS 26.30 seconds CheckAllWarning PASS 28.71 seconds CheckSparse PASS 28.09 seconds BuildKernel32 PASS 25.68 seconds TestRunnerSetup PASS 567.74 seconds TestRunner_l2cap-tester PASS 27.95 seconds TestRunner_iso-tester PASS 41.46 seconds TestRunner_bnep-tester PASS 6.30 seconds TestRunner_mgmt-tester FAIL 112.93 seconds TestRunner_rfcomm-tester PASS 9.38 seconds TestRunner_sco-tester FAIL 14.32 seconds TestRunner_ioctl-tester PASS 10.16 seconds TestRunner_mesh-tester FAIL 11.61 seconds TestRunner_smp-tester PASS 8.97 seconds TestRunner_userchan-tester PASS 6.71 seconds TestRunner_6lowpan-tester FAIL 8.69 seconds IncrementalBuild PENDING 0.26 seconds
Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script
Output:
##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:
##############################
Test: SubjectPrefix - FAIL
Desc: Check subject contains "Bluetooth" prefix Output:
"Bluetooth: " prefix is not specified in the subject "Bluetooth: " prefix is not specified in the subject "Bluetooth: " prefix is not specified in the subject "Bluetooth: " prefix is not specified in the subject "Bluetooth: " prefix is not specified in the subject
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner Output:
Total: 494, Passed: 489 (99.0%), Failed: 1, Not Run: 4
Failed Test Cases
Read Exp Feature - Success Failed 0.105 seconds
##############################
Test: TestRunner_sco-tester - FAIL
Desc: Run sco-tester with test-runner
Output:
WARNING: possible circular locking dependency detected
7.0.0-rc2-ga0d9588efccb #1 Not tainted
------------------------------------------------------
kworker/u5:2/117 is trying to acquire lock:ffff8880021b5240 (sk_lock-AF_BLUETOOTH-BTPROTO_SCO){+.+.}-{0:0}, at: sco_connect_cfm+0x358/0x8d0
but task is already holding lock:ffff8880025fc220 (&conn->lock){+.+.}-{3:3}, at: sco_connect_cfm+0x22d/0x8d0
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (&conn->lock){+.+.}-{3:3}:
lock_acquire+0xf7/0x2c0
_raw_spin_lock+0x2a/0x40
sco_sock_connect+0x4d7/0x1280
__sys_connect+0x1a3/0x260
__x64_sys_connect+0x6e/0xb0
do_syscall_64+0xa0/0x570
entry_SYSCALL_64_after_hwframe+0x74/0x7c-> #0 (sk_lock-AF_BLUETOOTH-BTPROTO_SCO){+.+.}-{0:0}:
check_prev_add+0xe9/0xc70
__lock_acquire+0x1457/0x1df0
lock_acquire+0xf7/0x2c0
lock_sock_nested+0x36/0xd0
sco_connect_cfm+0x358/0x8d0
hci_sync_conn_complete_evt+0x3d3/0x8e0
hci_event_packet+0x74f/0xb10
hci_rx_work+0x398/0xd00
process_scheduled_works+0xb16/0x1ac0
worker_thread+0x4ff/0xba0
kthread+0x368/0x490
ret_from_fork+0x498/0x7e0
ret_from_fork_asm+0x19/0x30other info that might help us debug this:
...
BUG: sleeping function called from invalid context at net/core/sock.c:3782
in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 117, name: kworker/u5:2preempt_count: 1, expected: 0
RCU nest depth: 0, expected: 0
INFO: lockdep is turned off.
CPU: 0 UID: 0 PID: 117 Comm: kworker/u5:2 Not tainted 7.0.0-rc2-ga0d9588efccb #1 PREEMPT(lazy) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.13.0-1ubuntu1.1 04/01/2014 Workqueue: hci0 hci_rx_work Call Trace: <TASK> dump_stack_lvl+0x49/0x60 __might_resched+0x2ea/0x500 lock_sock_nested+0x47/0xd0 ? sco_connect_cfm+0x358/0x8d0 sco_connect_cfm+0x358/0x8d0 ? hci_debugfs_create_conn+0x190/0x210 ? __pfx_sco_connect_cfm+0x10/0x10 hci_sync_conn_complete_evt+0x3d3/0x8e0 hci_event_packet+0x74f/0xb10 ? __pfx_hci_sync_conn_complete_evt+0x10/0x10 ? __pfx_hci_event_packet+0x10/0x10 ? mark_held_locks+0x49/0x80 ? lockdep_hardirqs_on_prepare+0xd4/0x180 ? _raw_spin_unlock_irqrestore+0x2c/0x50 hci_rx_work+0x398/0xd00 process_scheduled_works+0xb16/0x1ac0 ? __pfx_process_scheduled_works+0x10/0x10 ? lock_acquire+0xf7/0x2c0 ? lock_is_held_type+0x9b/0x110 ? __pfx_hci_rx_work+0x10/0x10 worker_thread+0x4ff/0xba0 ? _raw_spin_unlock_irqrestore+0x2c/0x50 ? __pfx_worker_thread+0x10/0x10 kthread+0x368/0x490 ? _raw_spin_unlock_irq+0x23/0x40 ? __pfx_kthread+0x10/0x10 ret_from_fork+0x498/0x7e0 ? __pfx_ret_from_fork+0x10/0x10 ? __switch_to+0x9e4/0xe50 ? __switch_to_asm+0x32/0x60
...
Total: 30, Passed: 30 (100.0%), Failed: 0, Not Run: 0
##############################
Test: TestRunner_mesh-tester - FAIL
Desc: Run mesh-tester with test-runner Output:
Total: 10, Passed: 8 (80.0%), Failed: 2, Not Run: 0
Failed Test Cases
Mesh - Send cancel - 1 Timed out 1.786 seconds Mesh - Send cancel - 2 Timed out 1.994 seconds
##############################
Test: TestRunner_6lowpan-tester - FAIL
Desc: Run 6lowpan-tester with test-runner
Output:
WARNING: possible circular locking dependency detected 7.0.0-rc2-ga0d9588efccb #1 Not tainted
------------------------------------------------------
kworker/0:1/11 is trying to acquire lock:ffff8880026e0940 ((wq_completion)hci0#2){+.+.}-{0:0}, at: touch_wq_lockdep_map+0x75/0x180
but task is already holding lock:ffffffff97e4d720 (rtnl_mutex){+.+.}-{4:4}, at: lowpan_unregister_netdev+0xd/0x30
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #4 (rtnl_mutex){+.+.}-{4:4}:
lock_acquire+0xf7/0x2c0
__mutex_lock+0x16b/0x1fc0
lowpan_register_netdev+0x11/0x30
chan_ready_cb+0x836/0xd00
l2cap_recv_frame+0x6a06/0x8920
l2cap_recv_acldata+0x790/0xdf0
hci_rx_work+0x500/0xd00
process_scheduled_works+0xb16/0x1ac0
worker_thread+0x4ff/0xba0
kthread+0x368/0x490
ret_from_fork+0x498/0x7e0
ret_from_fork_asm+0x19/0x30-> #3 (&chan->lock#3/1){+.+.}-{4:4}:
lock_acquire+0xf7/0x2c0
__mutex_lock+0x16b/0x1fc0
l2cap_chan_connect+0x74e/0x1980
lowpan_control_write+0x523/0x660
full_proxy_write+0x10b/0x190
vfs_write+0x1c0/0xf60
ksys_write+0xf1/0x1d0
do_syscall_64+0xa0/0x570
entry_SYSCALL_64_after_hwframe+0x74/0x7c-> #2 (&conn->lock){+.+.}-{4:4}:
...
Total: 8, Passed: 8 (100.0%), Failed: 0, Not Run: 0
##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series Output:
https://github.com/bluez/bluetooth-next/pull/51/checks
---
Regards,
Linux Bluetooth
PATCH 2/5 sockptr: fix usize check in copy_struct_from_sockptr() for user pointers
In reply to Stefan Metzmachercopy_struct_from_user will never hit the check_zeroed_user() call and will never return -E2BIG if new userspace passed new bits in a larger structure than the current kernel structure.
As far as I can there are no critical/related uapi changes in
- include/net/bluetooth/bluetooth.h and net/bluetooth/sco.c after the use of copy_struct_from_sockptr in v6.13-rc3
- include/uapi/linux/tcp.h and net/ipv4/tcp_ao.c after the use of copy_struct_from_sockptr in v6.6-rc1
So that new callers will get the correct behavior from the start.
--- include/linux/sockptr.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/sockptr.h +1 -1
@@ -91,7 +91,7 @@ static inline int copy_struct_from_sockptr(void *dst, size_t ksize,size_t rest =max (ksize, usize) - size;if (!sockptr_is_kernel (src)) -return copy_struct_from_user (dst, ksize, src.user, size); +return copy_struct_from_user (dst, ksize, src.user, usize);if (usize < ksize) {memset (dst + size,0 , rest);
Signature · 1 line
2.43.0
Re: [PATCH 2/5] sockptr: fix usize check in copy_struct_from_sockptr() for user pointers
Quoted 52 lines · Stefan Metzmacher
copy_struct_from_userwill never hit thecheck_zeroed_user()call and will never return-E2BIGif new userspace passed new bits in a larger structure than the current kernel structure.As far as I can there are no critical/related uapi changes in
-
include/net/bluetooth/bluetooth.handnet/bluetooth/sco.cafter the use ofcopy_struct_from_sockptrin v6.13-rc3
-include/uapi/linux/tcp.handnet/ipv4/tcp_ao.cafter the use ofcopy_struct_from_sockptrin v6.6-rc1So that new callers will get the correct behavior from the start.
Fixes 4954f17ddefc ("net/tcp: Introduce TCP_AO setsockopt()s")Fixes ef84703a911f ("net/tcp: Add TCP-AO getsockopt()s")Fixes faadfaba5e01 ("net/tcp: Add TCP_AO_REPAIR")Fixes 3e643e4efa1e ("Bluetooth: Improve setsockopt() handling of malformed user input")Cc Dmitry Safonov <[email protected]>Cc Dmitry Safonov <[email protected]>Cc Francesco Ruggeri <[email protected]>Cc Salam Noureddine <[email protected]>Cc David Ahern <[email protected]>Cc David S. Miller <[email protected]>Cc Michal Luczaj <[email protected]>Cc David Wei <[email protected]>Cc Luiz Augusto von Dentz <[email protected]>Cc Luiz Augusto von Dentz <[email protected]>Cc Marcel Holtmann <[email protected]>Cc Xin Long <[email protected]>Cc Eric Dumazet <[email protected]>Cc Kuniyuki Iwashima <[email protected]>Cc Paolo Abeni <[email protected]>Cc Willem de Bruijn <[email protected]>Cc Neal Cardwell <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Simon Horman <[email protected]>Cc Aleksa Sarai <[email protected]>Cc Christian Brauner <[email protected]>CC Kees Cook <[email protected]>Signed-off-by Stefan Metzmacher <[email protected]>--- include/linux/sockptr.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/sockptr.h +0
-0
include/linux/sockptr.h static inline int copy_struct_from_sockptr(void *dst, size_t ksize, · line 97
Quoted 8 lines
include/linux/sockptr.h +1
-1@@ -91,7 +91,7 @@ static inline int copy_struct_from_sockptr(void *dst, size_t ksize,size_t rest =max (ksize, usize) - size;if (!sockptr_is_kernel (src)) -return copy_struct_from_user (dst, ksize, src.user, size); +return copy_struct_from_user (dst, ksize, src.user, usize);if (usize < ksize) {memset (dst + size,0 , rest);
It is a little weird that this function open-codes memchr_inv() -- check_zeroed_sockptr() exists and does the right thing. Maybe it'd be nice to fix that too?
In any case,
Signature · 2 lines
Aleksa Sarai https://www.cyphar.com/
Re: [PATCH 2/5] sockptr: fix usize check in copy_struct_from_sockptr() for user pointers
Quoted 59 lines · Aleksa Sarai
Quoted 58 lines · Stefan Metzmacher
copy_struct_from_userwill never hit thecheck_zeroed_user()call and will never return-E2BIGif new userspace passed new bits in a larger structure than the current kernel structure.As far as I can there are no critical/related uapi changes in
-
include/net/bluetooth/bluetooth.handnet/bluetooth/sco.cafter the use ofcopy_struct_from_sockptrin v6.13-rc3
-include/uapi/linux/tcp.handnet/ipv4/tcp_ao.cafter the use ofcopy_struct_from_sockptrin v6.6-rc1So that new callers will get the correct behavior from the start.
Fixes 4954f17ddefc ("net/tcp: Introduce TCP_AO setsockopt()s")Fixes ef84703a911f ("net/tcp: Add TCP-AO getsockopt()s")Fixes faadfaba5e01 ("net/tcp: Add TCP_AO_REPAIR")Fixes 3e643e4efa1e ("Bluetooth: Improve setsockopt() handling of malformed user input")Cc Dmitry Safonov <[email protected]>Cc Dmitry Safonov <[email protected]>Cc Francesco Ruggeri <[email protected]>Cc Salam Noureddine <[email protected]>Cc David Ahern <[email protected]>Cc David S. Miller <[email protected]>Cc Michal Luczaj <[email protected]>Cc David Wei <[email protected]>Cc Luiz Augusto von Dentz <[email protected]>Cc Luiz Augusto von Dentz <[email protected]>Cc Marcel Holtmann <[email protected]>Cc Xin Long <[email protected]>Cc Eric Dumazet <[email protected]>Cc Kuniyuki Iwashima <[email protected]>Cc Paolo Abeni <[email protected]>Cc Willem de Bruijn <[email protected]>Cc Neal Cardwell <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Simon Horman <[email protected]>Cc Aleksa Sarai <[email protected]>Cc Christian Brauner <[email protected]>CC Kees Cook <[email protected]>Signed-off-by Stefan Metzmacher <[email protected]>--- include/linux/sockptr.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/sockptr.h +1
-1@@ -91,7 +91,7 @@ static inline int copy_struct_from_sockptr(void *dst, size_t ksize,size_t rest =max (ksize, usize) - size;if (!sockptr_is_kernel (src)) -return copy_struct_from_user (dst, ksize, src.user, size); +return copy_struct_from_user (dst, ksize, src.user, usize);
Quoted 6 lines
include/linux/sockptr.h static inline int copy_struct_from_sockptr(void *dst, size_t ksize, · line 97
Quoted 2 lines
include/linux/sockptr.h +0
-0if (usize < ksize) {memset (dst + size,0 , rest);It is a little weird that this function open-codes
memchr_inv()--check_zeroed_sockptr()exists and does the right thing. Maybe it'd be nice to fix that too?
Ah my bad, I didn't see that you already fixed this in patch #4.
Quoted 3 lines
In any case,
Reviewed-by Aleksa Sarai <[email protected]>
Signature · 2 lines
Aleksa Sarai https://www.cyphar.com/
PATCH 3/5 uaccess: add copy_struct_{from,to}_bounce_buffer() helpers
In reply to Stefan MetzmacherThese are similar to copy_struct_{from,to}_user() but operate on kernel buffers instead of user buffers.
They can be used when there is a temporary bounce buffer used, e.g. in msg_control or similar places.
It allows us to have the same logic to handle old vs. current and current vs. new structures in the same compatible way.
copy_struct_from_sockptr() will also be able to use copy_struct_from_bounce_buffer() for the kernel case as follow us patch.
I'll use this in my IPPROTO_SMBDIRECT work,
but maybe it will also be useful for others...IPPROTO_QUIC will likely also use it.
--- include/linux/uaccess.h | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)
include/linux/uaccess.h +63 -0
@@ -513,6 +513,69 @@ copy_struct_to_user(void __user *dst, size_t usize, const void *src,return 0 ; } +static __always_inline void +__copy_struct_generic_bounce_buffer (void *dst,size_t dstsize, +const void *src,size_t srcsize, +bool *ignored_trailing) +{ +size_t size =min (dstsize, srcsize); +size_t rest =max (dstsize, srcsize) - size; + +/* Deal with trailing bytes. */ +if (dstsize > srcsize) +memset (dst + size,0 , rest); +if (ignored_trailing) + *ignored_trailing = dstsize < srcsize && +memchr_inv (src + size,0 , rest) !=NULL ; +/* Copy the interoperable parts of the struct. */ +memcpy (dst, src, size); +} + +/** +* This is like copy_struct_from_user(), but the +* src buffer was already copied into a kernel +* bounce buffer, so it will never return -EFAULT. +*/ +static __always_inline __must_check int +copy_struct_from_bounce_buffer (void *dst,size_t dstsize, +const void *src,size_t srcsize) +{ +bool ignored_trailing; + +/* Double check if ksize is larger than a known object size. */ +if (WARN_ON_ONCE (dstsize >__builtin_object_size (dst,1 ))) +return -E2BIG ; + +__copy_struct_generic_bounce_buffer (dst, dstsize, + src, srcsize, + &ignored_trailing); +if (unlikely (ignored_trailing)) +return -E2BIG ; + +return 0 ; +} + +/** +* This is like copy_struct_to_user(), but the +* dst buffer is a kernel bounce buffer instead +* of a direct userspace buffer, so it will never return -EFAULT. +*/ +static __always_inline __must_check int +copy_struct_to_bounce_buffer (void *dst,size_t dstsize, +const void *src, +size_t srcsize, +bool *ignored_trailing) +{ +/* Double check if srcsize is larger than a known object size. */ +if (WARN_ON_ONCE (srcsize >__builtin_object_size (src,1 ))) +return -E2BIG ; + +__copy_struct_generic_bounce_buffer (dst, dstsize, + src, srcsize, + ignored_trailing); +return 0 ; +} +bool copy_from_kernel_nofault_allowed (const void *unsafe_src,size_t size);long copy_from_kernel_nofault (void *dst,const void *src,size_t size);
Signature · 1 line
2.43.0
Re: [PATCH 3/5] uaccess: add copy_struct_{from,to}_bounce_buffer() helpers
Quoted 62 lines · On Tue, 7 Apr 2026 18:03:15 +0200 Stefan Metzmacher <[email protected]> wrote:
These are similar to copy_struct_{from,to}_user() but operate on kernel buffers instead of user buffers.
They can be used when there is a temporary bounce buffer used, e.g. in
msg_controlor similar places.It allows us to have the same logic to handle old vs. current and current vs. new structures in the same compatible way.
copy_struct_from_sockptr()will also be able to usecopy_struct_from_bounce_buffer()for the kernel case as follow us patch.I'll use this in my
IPPROTO_SMBDIRECTwork,
but maybe it will also be useful for others...IPPROTO_QUICwill likely also use it.Cc Dmitry Safonov <[email protected]>Cc Dmitry Safonov <[email protected]>Cc Francesco Ruggeri <[email protected]>Cc Salam Noureddine <[email protected]>Cc David Ahern <[email protected]>Cc David S. Miller <[email protected]>Cc Michal Luczaj <[email protected]>Cc David Wei <[email protected]>Cc Luiz Augusto von Dentz <[email protected]>Cc Luiz Augusto von Dentz <[email protected]>Cc Marcel Holtmann <[email protected]>Cc Xin Long <[email protected]>Cc Eric Dumazet <[email protected]>Cc Kuniyuki Iwashima <[email protected]>Cc Paolo Abeni <[email protected]>Cc Willem de Bruijn <[email protected]>Cc Neal Cardwell <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Simon Horman <[email protected]>Cc Aleksa Sarai <[email protected]>Cc Christian Brauner <[email protected]>CC Kees Cook <[email protected]>Signed-off-by Stefan Metzmacher <[email protected]>--- include/linux/uaccess.h | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)include/linux/uaccess.h +7
-0@@ -513,6 +513,69 @@ copy_struct_to_user(void __user *dst, size_t usize, const void *src,return 0 ; } +static __always_inline void +__copy_struct_generic_bounce_buffer (void *dst,size_t dstsize, +const void *src,size_t srcsize, +bool *ignored_trailing) +{ +size_t size =min (dstsize, srcsize); +size_t rest =max (dstsize, srcsize) - size;
include/linux/uaccess.h copy_struct_to_user(void __user *dst, size_t usize, const void *src, · line 532
Quoted 10 lines
include/linux/uaccess.h +10
-0+ +/* Deal with trailing bytes. */ +if (dstsize > srcsize) +memset (dst + size,0 , rest); +if (ignored_trailing) + *ignored_trailing = dstsize < srcsize && +memchr_inv (src + size,0 , rest) !=NULL ; +/* Copy the interoperable parts of the struct. */ +memcpy (dst, src, size); +}
Return 'ignored_trailing' rather than pass by reference. And this is probably too big to inline.
David
Quoted 49 lines
include/linux/uaccess.h +46
-0+ +/** +* This is like copy_struct_from_user(), but the +* src buffer was already copied into a kernel +* bounce buffer, so it will never return -EFAULT. +*/ +static __always_inline __must_check int +copy_struct_from_bounce_buffer (void *dst,size_t dstsize, +const void *src,size_t srcsize) +{ +bool ignored_trailing; + +/* Double check if ksize is larger than a known object size. */ +if (WARN_ON_ONCE (dstsize >__builtin_object_size (dst,1 ))) +return -E2BIG ; + +__copy_struct_generic_bounce_buffer (dst, dstsize, + src, srcsize, + &ignored_trailing); +if (unlikely (ignored_trailing)) +return -E2BIG ; + +return 0 ; +} + +/** +* This is like copy_struct_to_user(), but the +* dst buffer is a kernel bounce buffer instead +* of a direct userspace buffer, so it will never return -EFAULT. +*/ +static __always_inline __must_check int +copy_struct_to_bounce_buffer (void *dst,size_t dstsize, +const void *src, +size_t srcsize, +bool *ignored_trailing) +{ +/* Double check if srcsize is larger than a known object size. */ +if (WARN_ON_ONCE (srcsize >__builtin_object_size (src,1 ))) +return -E2BIG ; + +__copy_struct_generic_bounce_buffer (dst, dstsize, + src, srcsize, + ignored_trailing); +return 0 ; +} +bool copy_from_kernel_nofault_allowed (const void *unsafe_src,size_t size);long copy_from_kernel_nofault (void *dst,const void *src,size_t size);
Re: [PATCH 3/5] uaccess: add copy_struct_{from,to}_bounce_buffer() helpers
Hi David,
Quoted 69 lines
Quoted 66 lines · On Tue, 7 Apr 2026 18:03:15 +0200 Stefan Metzmacher <[email protected]> wrote:
These are similar to copy_struct_{from,to}_user() but operate on kernel buffers instead of user buffers.
They can be used when there is a temporary bounce buffer used, e.g. in
msg_controlor similar places.It allows us to have the same logic to handle old vs. current and current vs. new structures in the same compatible way.
copy_struct_from_sockptr()will also be able to usecopy_struct_from_bounce_buffer()for the kernel case as follow us patch.I'll use this in my
IPPROTO_SMBDIRECTwork,
but maybe it will also be useful for others...IPPROTO_QUICwill likely also use it.Cc Dmitry Safonov <[email protected]>Cc Dmitry Safonov <[email protected]>Cc Francesco Ruggeri <[email protected]>Cc Salam Noureddine <[email protected]>Cc David Ahern <[email protected]>Cc David S. Miller <[email protected]>Cc Michal Luczaj <[email protected]>Cc David Wei <[email protected]>Cc Luiz Augusto von Dentz <[email protected]>Cc Luiz Augusto von Dentz <[email protected]>Cc Marcel Holtmann <[email protected]>Cc Xin Long <[email protected]>Cc Eric Dumazet <[email protected]>Cc Kuniyuki Iwashima <[email protected]>Cc Paolo Abeni <[email protected]>Cc Willem de Bruijn <[email protected]>Cc Neal Cardwell <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Simon Horman <[email protected]>Cc Aleksa Sarai <[email protected]>Cc Christian Brauner <[email protected]>CC Kees Cook <[email protected]>Signed-off-by Stefan Metzmacher <[email protected]>--- include/linux/uaccess.h | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)include/linux/uaccess.h +11
-0@@ -513,6 +513,69 @@ copy_struct_to_user(void __user *dst, size_t usize, const void *src,return 0 ; } +static __always_inline void +__copy_struct_generic_bounce_buffer (void *dst,size_t dstsize, +const void *src,size_t srcsize, +bool *ignored_trailing) +{ +size_t size =min (dstsize, srcsize); +size_t rest =max (dstsize, srcsize) - size; + +/* Deal with trailing bytes. */ +if (dstsize > srcsize) +memset (dst + size,0 , rest);
Quoted 8 lines
include/linux/uaccess.h copy_struct_to_user(void __user *dst, size_t usize, const void *src, · line 532
Quoted 6 lines
include/linux/uaccess.h +6
-0+if (ignored_trailing) + *ignored_trailing = dstsize < srcsize && +memchr_inv (src + size,0 , rest) !=NULL ; +/* Copy the interoperable parts of the struct. */ +memcpy (dst, src, size); +}Return '
ignored_trailing' rather than pass by reference.
I also thought about that but it makes
the copy_struct_to_ case more complex.
I'm not sure but my guess would be that
the compiler would have the chance to skip the ignore_trailing logic if (as all current callers do) NULL is passed.
Quoted 1 line
And this is probably too big to inline.
In the next patch this replace open coded logic in copy_struct_from_sockptr. So as all of copy_struct_* consists of inline functions I thought it would be good to keep it that way.
So unless there a real good reason to change it I'd like to keep it as is.
Thanks!
metze
PATCH 4/5 sockptr: let copy_struct_from_sockptr() use copy_struct_from_bounce_buffer()
In reply to Stefan MetzmacherThe world would be better without sockptr_t, but this at least simplifies copy_struct_from_sockptr() to be just a dispatcher for copy_struct_from_user() or copy_struct_from_bounce_buffer() without any special logic on its own.
--- include/linux/sockptr.h | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-)
include/linux/sockptr.h +1 -15
@@ -87,24 +87,10 @@ static inline int copy_safe_from_sockptr(void *dst, size_t ksize,static inline int copy_struct_from_sockptr (void *dst,size_t ksize,sockptr_t src,size_t usize) { -size_t size =min (ksize, usize); -size_t rest =max (ksize, usize) - size; -if (!sockptr_is_kernel (src))return copy_struct_from_user (dst, ksize, src.user, usize); -if (usize < ksize) { -memset (dst + size,0 , rest); - }else if (usize > ksize) { -char *p = src.kernel; - -while (rest--) { -if (*p++) -return -E2BIG ; - } - } -memcpy (dst, src.kernel, size); -return 0 ; +return copy_struct_from_bounce_buffer (dst, ksize, src.kernel, usize); } static inline int copy_to_sockptr_offset(sockptr_t dst, size_t offset,
Signature · 1 line
2.43.0
PATCH 5/5 sockptr: introduce copy_struct_to_sockptr()
In reply to Stefan MetzmacherWe already have copy_struct_from_sockptr() as wrapper to copy_struct_from_user() or copy_struct_from_bounce_buffer(), so it's good to have copy_struct_to_sockptr()
as well matching the behavior of copy_struct_to_user() or copy_struct_to_bounce_buffer().
The world would be better without sockptr_t, but having copy_struct_to_sockptr() is better than open code it in various places.
I'll use this in my IPPROTO_SMBDIRECT work,
but maybe it will also be useful for others...IPPROTO_QUIC will likely also use it.
--- include/linux/sockptr.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
include/linux/sockptr.h +10 -0
@@ -107,6 +107,16 @@ static inline int copy_to_sockptr(sockptr_t dst, const void *src, size_t size)return copy_to_sockptr_offset (dst,0 , src, size); } +static inline int +copy_struct_to_sockptr (sockptr_t dst,size_t usize,const void *src, +size_t ksize,bool *ignored_trailing) +{ +if (!sockptr_is_kernel (dst)) +return copy_struct_to_user (dst.user, usize, src, ksize, ignored_trailing); + +return copy_struct_to_bounce_buffer (dst.kernel, usize, src, ksize, ignored_trailing); +} +static inline void *memdup_sockptr_noprof (sockptr_t src,size_t len) {void *p =kmalloc_track_caller_noprof (len,GFP_USER | __GFP_NOWARN);
Signature · 1 line
2.43.0
Christian Brauner
AppliedQuoted 10 lines · Stefan Metzmacher
here are some patches related to copy_struct_{from,to}_{user,sockptr}()
I collected during my work on anIPPROTO_SMBDIRECTimplementation wrapping the smbdirect code used by cifs.ko and ksmbd.ko.The first patch fixes
copy_struct_to_user()to behave like documented.[...]
Applied to the vfs-7.2.misc branch of the vfs/vfs.git tree. Patches in the vfs-7.2.misc branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs-7.2.misc
[1/5] uaccess: fix ignored_trailing logic in copy_struct_to_user()
https://git.kernel.org/vfs/vfs/c/0a8d67b47a5c [2/5] sockptr: fix usize check in copy_struct_from_sockptr() for user pointers https://git.kernel.org/vfs/vfs/c/e555ebefea0a [3/5] uaccess: add copy_struct_{from,to}_bounce_buffer() helpers https://git.kernel.org/vfs/vfs/c/14e710721cd3 [4/5] sockptr: let copy_struct_from_sockptr() use copy_struct_from_bounce_buffer() https://git.kernel.org/vfs/vfs/c/62343ea38031 [5/5] sockptr: introduce copy_struct_to_sockptr() https://git.kernel.org/vfs/vfs/c/9899f22cc41d