Skip to content

Commit ff73f83

Browse files
keeskuba-moo
authored andcommitted
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) Signed-off-by: Kees Cook <[email protected]> Reviewed-by: Simon Horman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 4166204 commit ff73f83

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

net/core/sock.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,8 +2053,9 @@ static void sock_copy(struct sock *nsk, const struct sock *osk)
20532053

20542054
memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
20552055

2056-
memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
2057-
prot->obj_size - offsetof(struct sock, sk_dontcopy_end));
2056+
unsafe_memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
2057+
prot->obj_size - offsetof(struct sock, sk_dontcopy_end),
2058+
/* alloc is larger than struct, see sk_prot_alloc() */);
20582059

20592060
#ifdef CONFIG_SECURITY_NETWORK
20602061
nsk->sk_security = sptr;

net/ipv4/inet_connection_sock.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,9 @@ static struct request_sock *inet_reqsk_clone(struct request_sock *req,
906906

907907
memcpy(nreq_sk, req_sk,
908908
offsetof(struct sock, sk_dontcopy_begin));
909-
memcpy(&nreq_sk->sk_dontcopy_end, &req_sk->sk_dontcopy_end,
910-
req->rsk_ops->obj_size - offsetof(struct sock, sk_dontcopy_end));
909+
unsafe_memcpy(&nreq_sk->sk_dontcopy_end, &req_sk->sk_dontcopy_end,
910+
req->rsk_ops->obj_size - offsetof(struct sock, sk_dontcopy_end),
911+
/* alloc is larger than struct, see above */);
911912

912913
sk_node_init(&nreq_sk->sk_node);
913914
nreq_sk->sk_tx_queue_mapping = req_sk->sk_tx_queue_mapping;

0 commit comments

Comments
 (0)