selinux: initialize sport/dport in __ad_net_init()
PATCH 1/1 selinux: initialize sport/dport in __ad_net_init()
Since commit dd51fcd42fd6 ("selinux: introduce and use lsm_ad_net_init*() helpers") the struct lsm_network_audit used by the SELinux network hooks is no longer zeroed; __ad_net_init() sets only netif, sk and family. The addresses are always filled in by selinux_parse_skb(), but sport and dport are set by selinux_parse_skb_ipv4()/selinux_parse_skb_ipv6() only for TCP, UDP and SCTP. For any other protocol (e.g. a raw socket with IPPROTO_IGMP, UDP-Lite) and for non-first fragments they are left uninitialized.
When such a packet hits an AVC denial (e.g. with NetLabel or secmark peer labeling enabled), dump_common_audit_data() passes the garbage ports to print_ipv4_addr()/print_ipv6_addr(), which leaks up to four bytes of kernel stack into the audit log as "src=" / "dest=" and is reported by KMSAN:
BUG: KMSAN: uninit-value in common_lsm_audit+0x2196/0x2ff0 print_ipv6_addr security/lsm_audit.c:178 [inline] dump_common_audit_data security/lsm_audit.c:373 [inline] common_lsm_audit+0x2196/0x2ff0 security/lsm_audit.c:458 slow_avc_audit+0x17f/0x1d0 security/selinux/avc.c:773 avc_has_perm+0x2d6/0x360 security/selinux/avc.c:1191 selinux_inet_sys_rcv_skb+0x116/0x280 security/selinux/hooks.c:5326 selinux_socket_sock_rcv_skb+0xaae/0x1270 security/selinux/hooks.c:5408 security_sock_rcv_skb+0x79/0x120 security/security.c:4853 sk_filter_trim_cap+0x18b/0xa60 net/core/filter.c:151 sock_queue_rcv_skb_reason+0x46/0x1a0 net/core/sock.c:522 rawv6_rcv+0x1b17/0x1c20 net/ipv6/raw.c:371 ... Local variable net created at: selinux_socket_sock_rcv_skb+0x118/0x1270 security/selinux/hooks.c
Initialize sport and dport in __ad_net_init(). Zeroing only these two fields keeps the cost low, which was the point of dd51fcd42fd6.
Found by Ideco on behalf of Linux Verification Center (linuxtesting.org) with Syzkaller.
---
Tested on a 6.12.114-based kernel with KMSAN: the reproducer (NetLabel static default label + raw IPv4/IPv6 socket with protocol 2, and UDP-Lite over IPv6) triggers the report on the unpatched kernel and no longer does with the fix. Mainline was not booted; the affected code there is identical.
security/selinux/hooks.c | 2 ++ 1 file changed, 2 insertions(+)
security/selinux/hooks.c +2 -0
@@ -242,6 +242,8 @@ static void __ad_net_init(struct common_audit_data *ad, net->netif = ifindex; net->sk = sk; net->family = family; + net->sport =0 ; + net->dport =0 ; } static void ad_net_init_from_sk(struct common_audit_data *ad,
Signature · 1 line
2.56.1
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 15 lines
Since commit
dd51fcd42fd6("selinux: introduce and use lsm_ad_net_init*() helpers") the structlsm_network_auditused by the SELinux network hooks is no longer zeroed;__ad_net_init()sets only netif, sk and family. The addresses are always filled in byselinux_parse_skb(), but sport and dport are set by selinux_parse_skb_ipv4()/selinux_parse_skb_ipv6() only for TCP, UDP and SCTP. For any other protocol (e.g. a raw socket withIPPROTO_IGMP, UDP-Lite) and for non-first fragments they are left uninitialized.When such a packet hits an AVC denial (e.g. with NetLabel or secmark peer labeling enabled),
dump_common_audit_data()passes the garbage ports to print_ipv4_addr()/print_ipv6_addr(), which leaks up to four bytes of kernel stack into the audit log as "src=" / "dest=" and is reported by KMSAN:
Quoted 6 lines
[ ... ]
(linuxtesting.org) with Syzkaller.Fixes dd51fcd42fd6 ("selinux: introduce and use lsm_ad_net_init*() helpers")Signed-off-by Eduard Orehov <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 35 lines · Eduard Orehov
Since commit
dd51fcd42fd6("selinux: introduce and use lsm_ad_net_init*() helpers") the structlsm_network_auditused by the SELinux network hooks is no longer zeroed;__ad_net_init()sets only netif, sk and family. The addresses are always filled in byselinux_parse_skb(), but sport and dport are set by selinux_parse_skb_ipv4()/selinux_parse_skb_ipv6() only for TCP, UDP and SCTP. For any other protocol (e.g. a raw socket withIPPROTO_IGMP, UDP-Lite) and for non-first fragments they are left uninitialized.When such a packet hits an AVC denial (e.g. with NetLabel or secmark peer labeling enabled),
dump_common_audit_data()passes the garbage ports to print_ipv4_addr()/print_ipv6_addr(), which leaks up to four bytes of kernel stack into the audit log as "src=" / "dest=" and is reported by KMSAN:BUG: KMSAN: uninit-value in common_lsm_audit+0x2196/0x2ff0 print_ipv6_addr security/lsm_audit.c:178 [inline] dump_common_audit_data security/lsm_audit.c:373 [inline] common_lsm_audit+0x2196/0x2ff0 security/lsm_audit.c:458 slow_avc_audit+0x17f/0x1d0 security/selinux/avc.c:773 avc_has_perm+0x2d6/0x360 security/selinux/avc.c:1191 selinux_inet_sys_rcv_skb+0x116/0x280 security/selinux/hooks.c:5326 selinux_socket_sock_rcv_skb+0xaae/0x1270 security/selinux/hooks.c:5408 security_sock_rcv_skb+0x79/0x120 security/security.c:4853 sk_filter_trim_cap+0x18b/0xa60 net/core/filter.c:151 sock_queue_rcv_skb_reason+0x46/0x1a0 net/core/sock.c:522 rawv6_rcv+0x1b17/0x1c20 net/ipv6/raw.c:371 ... Local variable net created at: selinux_socket_sock_rcv_skb+0x118/0x1270 security/selinux/hooks.cInitialize sport and dport in
__ad_net_init(). Zeroing only these two fields keeps the cost low, which was the point ofdd51fcd42fd6.
Quoted 6 lines
Found by Ideco on behalf of Linux Verification Center (linuxtesting.org) with Syzkaller.
Fixes dd51fcd42fd6 ("selinux: introduce and use lsm_ad_net_init*() helpers")Signed-off-by Eduard Orehov <[email protected]>
Quoted 49 lines · On Nov 23, 2026 Stephen Smalley <[email protected]> wrote:
Since commit
dd51fcd42fd6("selinux: introduce and use lsm_ad_net_init*() helpers") the structlsm_network_auditused by the SELinux network hooks is no longer zeroed;__ad_net_init()sets only netif, sk and family. The addresses are always filled in byselinux_parse_skb(), but sport and dport are set by selinux_parse_skb_ipv4()/selinux_parse_skb_ipv6() only for TCP, UDP and SCTP. For any other protocol (e.g. a raw socket withIPPROTO_IGMP, UDP-Lite) and for non-first fragments they are left uninitialized.When such a packet hits an AVC denial (e.g. with NetLabel or secmark peer labeling enabled),
dump_common_audit_data()passes the garbage ports to print_ipv4_addr()/print_ipv6_addr(), which leaks up to four bytes of kernel stack into the audit log as "src=" / "dest=" and is reported by KMSAN:BUG: KMSAN: uninit-value in common_lsm_audit+0x2196/0x2ff0 print_ipv6_addr security/lsm_audit.c:178 [inline] dump_common_audit_data security/lsm_audit.c:373 [inline] common_lsm_audit+0x2196/0x2ff0 security/lsm_audit.c:458 slow_avc_audit+0x17f/0x1d0 security/selinux/avc.c:773 avc_has_perm+0x2d6/0x360 security/selinux/avc.c:1191 selinux_inet_sys_rcv_skb+0x116/0x280 security/selinux/hooks.c:5326 selinux_socket_sock_rcv_skb+0xaae/0x1270 security/selinux/hooks.c:5408 security_sock_rcv_skb+0x79/0x120 security/security.c:4853 sk_filter_trim_cap+0x18b/0xa60 net/core/filter.c:151 sock_queue_rcv_skb_reason+0x46/0x1a0 net/core/sock.c:522 rawv6_rcv+0x1b17/0x1c20 net/ipv6/raw.c:371 ... Local variable net created at: selinux_socket_sock_rcv_skb+0x118/0x1270 security/selinux/hooks.cInitialize sport and dport in
__ad_net_init(). Zeroing only these two fields keeps the cost low, which was the point ofdd51fcd42fd6.Found by Ideco on behalf of Linux Verification Center (linuxtesting.org) with Syzkaller.
Fixes dd51fcd42fd6 ("selinux: introduce and use lsm_ad_net_init*() helpers")Signed-off-by Eduard Orehov <[email protected]>Acked-by Stephen Smalley <[email protected]>---
Tested on a 6.12.114-based kernel with KMSAN: the reproducer (NetLabel static default label + raw IPv4/IPv6 socket with protocol 2, and UDP-Lite over IPv6) triggers the report on the unpatched kernel and no longer does with the fix. Mainline was not booted; the affected code there is identical.
Quoted 2 lines
security/selinux/hooks.c | 2 ++ 1 file changed, 2 insertions(+)
Merged into selinux/stable-7.4, thanks!
--
paul-moore.com