sock: Use unsafe_memcpy() for sock_copy()
Subsystems (4)
PATCH v3 1/1 sock: Use unsafe_memcpy() for sock_copy()
While testing for places where zero-sized destinations were still showing up in the kernel, sock_copy() and inet_reqsk_clone() were found, which are using very specific memcpy() offsets for both avoiding a portion of struct sock, and copying beyond the end of it (since struct sock is really just a common header before the protocol-specific allocation). Instead of trying to unravel this historical lack of container_of(), just switch to unsafe_memcpy(), since that's effectively what was happening already (memcpy() wasn't checking 0-sized destinations while the code base was being converted away from fake flexible arrays).
Avoid the following false positive warning with future changes to CONFIG_FORTIFY_SOURCE:
memcpy: detected field-spanning write (size 3068) of destination "&nsk->__sk_common.skc_dontcopy_end" at net/core/sock.c:2057 (size 0)
---
Cc: Jakub Kicinski <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: [email protected]
v3: fix inet_reqsk_clone() comment
v2: https://lore.kernel.org/lkml/[email protected]
v1: https://lore.kernel.org/lkml/[email protected]
--- net/core/sock.c | 5 +++-- net/ipv4/inet_connection_sock.c | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-)
net/core/sock.c +3 -2
@@ -2053,8 +2053,9 @@ static void sock_copy(struct sock *nsk, const struct sock *osk)memcpy (nsk, osk, offsetof(struct sock , sk_dontcopy_begin)); -memcpy (&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end, - prot->obj_size - offsetof(struct sock , sk_dontcopy_end)); +unsafe_memcpy (&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end, + prot->obj_size - offsetof(struct sock , sk_dontcopy_end), + /* alloc is larger than struct, see sk_prot_alloc() */);#ifdef CONFIG_SECURITY_NETWORK nsk->sk_security = sptr;
net/ipv4/inet_connection_sock.c +3 -2
@@ -906,8 +906,9 @@ static struct request_sock *inet_reqsk_clone(struct request_sock *req,memcpy (nreq_sk, req_sk, offsetof(struct sock , sk_dontcopy_begin)); -memcpy (&nreq_sk->sk_dontcopy_end, &req_sk->sk_dontcopy_end, - req->rsk_ops->obj_size - offsetof(struct sock , sk_dontcopy_end)); +unsafe_memcpy (&nreq_sk->sk_dontcopy_end, &req_sk->sk_dontcopy_end, + req->rsk_ops->obj_size - offsetof(struct sock , sk_dontcopy_end), + /* alloc is larger than struct, see above */);sk_node_init (&nreq_sk->sk_node); nreq_sk->sk_tx_queue_mapping = req_sk->sk_tx_queue_mapping;
Signature · 1 line
2.34.1
Quoted 16 lines · Kees Cook
While testing for places where zero-sized destinations were still showing up in the kernel,
sock_copy()andinet_reqsk_clone()were found, which are using very specificmemcpy()offsets for both avoiding a portion of struct sock, and copying beyond the end of it (since struct sock is really just a common header before the protocol-specific allocation). Instead of trying to unravel this historical lack ofcontainer_of(), just switch tounsafe_memcpy(), since that's effectively what was happening already (memcpy()wasn't checking 0-sized destinations while the code base was being converted away from fake flexible arrays).Avoid the following false positive warning with future changes to
CONFIG_FORTIFY_SOURCE:memcpy: detected field-spanning write (size 3068) of destination "
&nsk->__sk_common.skc_dontcopy_end" atnet/core/sock.c:2057(size 0)Signed-off-by Kees Cook <[email protected]>
Quoted 9 lines
---
Cc: Jakub Kicinski <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: [email protected]
v3: fixinet_reqsk_clone()comment
v2: https://lore.kernel.org/lkml/[email protected]
v1: https://lore.kernel.org/lkml/[email protected]
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 11 lines · On Mon, 4 Mar 2024 13:29:31 -0800 you wrote:
While testing for places where zero-sized destinations were still showing up in the kernel,
sock_copy()andinet_reqsk_clone()were found, which are using very specificmemcpy()offsets for both avoiding a portion of struct sock, and copying beyond the end of it (since struct sock is really just a common header before the protocol-specific allocation). Instead of trying to unravel this historical lack ofcontainer_of(), just switch tounsafe_memcpy(), since that's effectively what was happening already (memcpy()wasn't checking 0-sized destinations while the code base was being converted away from fake flexible arrays).[...]
Here is the summary with links:
- [v3] sock: Use unsafe_memcpy() for sock_copy()
https://git.kernel.org/netdev/net-next/c/ff73f8344e58
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html