Skip to contentSkip to conversation

net: initialize ICMP error queue opt_stats

PATCH In review Weiming Shi 3 participants 3 messages Nov 11 – Nov 15

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: initialize ICMP error queue opt_stats

ip_icmp_error() and ipv6_icmp_error() clone a received ICMP skb before building its error queue entry. skb_clone() preserves the complete control buffer, while the IP receive paths initialize only the portion aliased by sock_exterr_skb::header. The two functions initialize ee, addr_offset, and port, but leave the final byte containing opt_stats and unused untouched. A TCX classifier can leave bpf_skb_data_end::data_end across that byte, forging opt_stats and making __sock_recv_timestamp() pass skb->len bytes from skb->data to put_cmsg(). For a non-linear ICMP skb, this reads beyond its linear head.

Initialize both bitfields in the two clone-based producers. Do not clear the whole sock_exterr_skb because the error receive paths consume the saved IP control-buffer header.

usercopy: Kernel memory exposure attempt detected from SLUB object

            'skbuff_small_head' (offset 72, size 59964)!
  kernel BUG at mm/usercopy.c:102!
  Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
  RIP: usercopy_abort (mm/usercopy.c:102)
  Call Trace:
   __check_heap_object (mm/slub.c:8146)
   __check_object_size (mm/usercopy.c:258)
   put_cmsg (net/core/scm.c:276)
   __sock_recv_timestamp (net/socket.c:1061)
   ip_recv_error (net/ipv4/ip_sockglue.c:550)
   udp_recvmsg (net/ipv4/udp.c:2034)
   inet_recvmsg (net/ipv4/af_inet.c:896)
   sock_recvmsg (net/socket.c:1159)
   ____sys_recvmsg (net/socket.c:2918)
   ___sys_recvmsg (net/socket.c:2961)
   __sys_recvmsg (net/socket.c:2993)
   do_syscall_64 (arch/x86/entry/syscall_64.c:94)
   entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:130)
  Kernel panic - not syncing: Fatal exception

With HARDENED_USERCOPY enabled, the reproducer aborted the unpatched kernel on its first packet. With runtime usercopy checks disabled and KASAN left in report mode, the same recvmsg() returned 59,964 control-message data bytes sourced past a 704-byte skb head. Beyond offset 1,024, 58,932 bytes differed from the prefilled userspace marker, including 6,288 nonzero bytes. The patched build completed 2,000 packets under both configurations without a forged SCM_TIMESTAMPING_OPT_STATS message or sanitizer report. Networking selftests were not run.

Fixes 4ef1b2869447 ("tcp: mark skbs with SCM_TIMESTAMPING_OPT_STATS")
---
 net/ipv4/ip_sockglue.c | 2 ++
 net/ipv6/datagram.c    | 2 ++
 2 files changed, 4 insertions(+)
net/ipv4/ip_sockglue.c +2 -0
index a55ef327ec93..3ab84a4c31e5 100644
@@ -429,6 +429,8 @@ void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
 	serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
 				   skb_network_header(skb);
 	serr->port = port;
+	serr->opt_stats = 0;
+	serr->unused = 0;
 
 	if (skb_pull(skb, payload - skb->data)) {
 		if (inet_test_bit(RECVERR_RFC4884, sk))
net/ipv6/datagram.c +2 -0
index ca3605acb433..85f646f8d059 100644
@@ -330,6 +330,8 @@ void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
 	serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
 				  skb_network_header(skb);
 	serr->port = port;
+	serr->opt_stats = 0;
+	serr->unused = 0;
 
 	__skb_pull(skb, payload - skb->data);
 
Signature · 1 line
2.56.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 83 lines · Weiming Shi

ip_icmp_error() and ipv6_icmp_error() clone a received ICMP skb before building its error queue entry. skb_clone() preserves the complete control buffer, while the IP receive paths initialize only the portion aliased by sock_exterr_skb::header. The two functions initialize ee, addr_offset, and port, but leave the final byte containing opt_stats and unused untouched. A TCX classifier can leave bpf_skb_data_end::data_end across that byte, forging opt_stats and making __sock_recv_timestamp() pass skb->len bytes from skb->data to put_cmsg(). For a non-linear ICMP skb, this reads beyond its linear head.

Initialize both bitfields in the two clone-based producers. Do not clear the whole sock_exterr_skb because the error receive paths consume the saved IP control-buffer header.

usercopy: Kernel memory exposure attempt detected from SLUB object

            'skbuff_small_head' (offset 72, size 59964)!
  kernel BUG at mm/usercopy.c:102!
  Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
  RIP: usercopy_abort (mm/usercopy.c:102)
  Call Trace:
   __check_heap_object (mm/slub.c:8146)
   __check_object_size (mm/usercopy.c:258)
   put_cmsg (net/core/scm.c:276)
   __sock_recv_timestamp (net/socket.c:1061)
   ip_recv_error (net/ipv4/ip_sockglue.c:550)
   udp_recvmsg (net/ipv4/udp.c:2034)
   inet_recvmsg (net/ipv4/af_inet.c:896)
   sock_recvmsg (net/socket.c:1159)
   ____sys_recvmsg (net/socket.c:2918)
   ___sys_recvmsg (net/socket.c:2961)
   __sys_recvmsg (net/socket.c:2993)
   do_syscall_64 (arch/x86/entry/syscall_64.c:94)
   entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:130)
  Kernel panic - not syncing: Fatal exception

With HARDENED_USERCOPY enabled, the reproducer aborted the unpatched kernel on its first packet. With runtime usercopy checks disabled and KASAN left in report mode, the same recvmsg() returned 59,964 control-message data bytes sourced past a 704-byte skb head. Beyond offset 1,024, 58,932 bytes differed from the prefilled userspace marker, including 6,288 nonzero bytes. The patched build completed 2,000 packets under both configurations without a forged SCM_TIMESTAMPING_OPT_STATS message or sanitizer report. Networking selftests were not run.

Fixes 4ef1b2869447 ("tcp: mark skbs with SCM_TIMESTAMPING_OPT_STATS")
---
 net/ipv4/ip_sockglue.c | 2 ++
 net/ipv6/datagram.c    | 2 ++
 2 files changed, 4 insertions(+)
net/ipv4/ip_sockglue.c +2 -0
index a55ef327ec93..3ab84a4c31e5 100644
@@ -429,6 +429,8 @@ void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
 	serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
 				   skb_network_header(skb);
 	serr->port = port;
+	serr->opt_stats = 0;
+	serr->unused = 0;
 
 	if (skb_pull(skb, payload - skb->data)) {
 		if (inet_test_bit(RECVERR_RFC4884, sk))
net/ipv6/datagram.c +2 -0
index ca3605acb433..85f646f8d059 100644
@@ -330,6 +330,8 @@ void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
 	serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
 				  skb_network_header(skb);
 	serr->port = port;
+	serr->opt_stats = 0;
+	serr->unused = 0;
 
 	__skb_pull(skb, payload - skb->data);
 
Signature · 1 line
2.56.0
#2 lore raw
In reply to Weiming Shi
Quoted 72 lines · Weiming Shi

ip_icmp_error() and ipv6_icmp_error() clone a received ICMP skb before building its error queue entry. skb_clone() preserves the complete control buffer, while the IP receive paths initialize only the portion aliased by sock_exterr_skb::header. The two functions initialize ee, addr_offset, and port, but leave the final byte containing opt_stats and unused untouched. A TCX classifier can leave bpf_skb_data_end::data_end across that byte, forging opt_stats and making __sock_recv_timestamp() pass skb->len bytes from skb->data to put_cmsg(). For a non-linear ICMP skb, this reads beyond its linear head.

Initialize both bitfields in the two clone-based producers. Do not clear the whole sock_exterr_skb because the error receive paths consume the saved IP control-buffer header.

usercopy: Kernel memory exposure attempt detected from SLUB object

            'skbuff_small_head' (offset 72, size 59964)!
  kernel BUG at mm/usercopy.c:102!
  Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
  RIP: usercopy_abort (mm/usercopy.c:102)
  Call Trace:
   __check_heap_object (mm/slub.c:8146)
   __check_object_size (mm/usercopy.c:258)
   put_cmsg (net/core/scm.c:276)
   __sock_recv_timestamp (net/socket.c:1061)
   ip_recv_error (net/ipv4/ip_sockglue.c:550)
   udp_recvmsg (net/ipv4/udp.c:2034)
   inet_recvmsg (net/ipv4/af_inet.c:896)
   sock_recvmsg (net/socket.c:1159)
   ____sys_recvmsg (net/socket.c:2918)
   ___sys_recvmsg (net/socket.c:2961)
   __sys_recvmsg (net/socket.c:2993)
   do_syscall_64 (arch/x86/entry/syscall_64.c:94)
   entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:130)
  Kernel panic - not syncing: Fatal exception

With HARDENED_USERCOPY enabled, the reproducer aborted the unpatched kernel on its first packet. With runtime usercopy checks disabled and KASAN left in report mode, the same recvmsg() returned 59,964 control-message data bytes sourced past a 704-byte skb head. Beyond offset 1,024, 58,932 bytes differed from the prefilled userspace marker, including 6,288 nonzero bytes. The patched build completed 2,000 packets under both configurations without a forged SCM_TIMESTAMPING_OPT_STATS message or sanitizer report. Networking selftests were not run.

Fixes 4ef1b2869447 ("tcp: mark skbs with SCM_TIMESTAMPING_OPT_STATS")
---
 net/ipv4/ip_sockglue.c | 2 ++
 net/ipv6/datagram.c    | 2 ++
 2 files changed, 4 insertions(+)
net/ipv4/ip_sockglue.c +2 -0
index a55ef327ec93..3ab84a4c31e5 100644
@@ -429,6 +429,8 @@ void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
 	serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
 				   skb_network_header(skb);
 	serr->port = port;
+	serr->opt_stats = 0;
+	serr->unused = 0;
 
 	if (skb_pull(skb, payload - skb->data)) {
 		if (inet_test_bit(RECVERR_RFC4884, sk))
net/ipv6/datagram.c +0 -0
index ca3605acb433..85f646f8d059 100644

net/ipv6/datagram.c void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, · line 336

Quoted 7 lines
net/ipv6/datagram.c +2 -0
@@ -330,6 +330,8 @@ void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
 	serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
 				  skb_network_header(skb);
 	serr->port = port;
+	serr->opt_stats = 0;
+	serr->unused = 0;
 
 	__skb_pull(skb, payload - skb->data);

How about [1] instead?

Allows us to avoid potential issues in the future.

[1]

net/ipv4/ip_sockglue.c +1 -2
index e06c1f48ecad..096546727c3d 100644
@@ -419,13 +419,12 @@ void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
 		return;
 
 	serr = SKB_EXT_ERR(skb);
+	memset_after(serr, 0, header);
 	serr->ee.ee_errno = err;
 	serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
 	serr->ee.ee_type = icmp_hdr(skb)->type;
 	serr->ee.ee_code = icmp_hdr(skb)->code;
-	serr->ee.ee_pad = 0;
 	serr->ee.ee_info = info;
-	serr->ee.ee_data = 0;
 	serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
 				   skb_network_header(skb);
 	serr->port = port;
net/ipv6/datagram.c +1 -2
index ff7a7ffbf983..f932d5a0d618 100644
@@ -324,13 +324,12 @@ void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
 	skb->protocol = htons(ETH_P_IPV6);
 
 	serr = SKB_EXT_ERR(skb);
+	memset_after(serr, 0, header);
 	serr->ee.ee_errno = err;
 	serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
 	serr->ee.ee_type = icmph->icmp6_type;
 	serr->ee.ee_code = icmph->icmp6_code;
-	serr->ee.ee_pad = 0;
 	serr->ee.ee_info = info;
-	serr->ee.ee_data = 0;
 	serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
 				  skb_network_header(skb);
 	serr->port = port;