sctp: diag: fix uninitialized stack leak via INET_DIAG_LOCALS/PEERS
Subsystems (4)
PATCH 1/1 sctp: diag: fix uninitialized stack leak via INET_DIAG_LOCALS/PEERS
inet_diag_msg_sctpladdrs_fill() copies sizeof(union sctp_addr) (28 bytes, the size of sockaddr_in6) from each sctp_sockaddr_entry.a into the netlink INET_DIAG_LOCALS attribute and then only zeroes the bytes from offset 28 to sizeof(sockaddr_storage). The same pattern is used by inet_diag_msg_sctpaddrs_fill() for INET_DIAG_PEERS.
The IPv4 address-filling helpers sctp_v4_from_addr_param() and sctp_v4_from_skb() only initialize the sockaddr_in portion (16 bytes) of the union sctp_addr; the trailing 12 bytes (offset 16..27, the sockaddr_in6-only region) are left uninitialized. Those bytes are propagated verbatim through sctp_add_bind_addr() (which copies sizeof(union sctp_addr)=28 bytes) and then copied straight to userspace by the diag fill functions, leaking 12 bytes of kernel stack residue per local/peer address to any process that can issue a SOCK_DIAG_BY_FAMILY dump for IPPROTO_SCTP.
Fix it by computing the actually-initialized length of the address from its sa_family (struct sockaddr_in for AF_INET, the whole union otherwise) and copying only that many bytes into an already-zeroed sockaddr_storage slot, so the uninitialized tail is never read and never reaches userspace.
--- net/sctp/diag.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
net/sctp/diag.c +12 -5
@@ -85,8 +85,12 @@ static int inet_diag_msg_sctpladdrs_fill(struct sk_buff *skb, info =nla_data (attr);rcu_read_lock ();list_for_each_entry_rcu (laddr, address_list, list) { -memcpy (info, &laddr->a,sizeof (laddr->a)); -memset (info +sizeof (laddr->a),0 , addrlen -sizeof (laddr->a)); +size_t addr_len = laddr->a.sa.sa_family ==AF_INET ? +sizeof (struct sockaddr_in ) : +sizeof (struct sockaddr_in6 ); + +memset (info,0 , addrlen); +memcpy (info, &laddr->a, addr_len); info += addrlen;if (!--addrcnt)
@@ -113,9 +117,12 @@ static int inet_diag_msg_sctpaddrs_fill(struct sk_buff *skb, info =nla_data (attr);list_for_each_entry (from, &asoc->peer.transport_addr_list, transports) { -memcpy (info, &from->ipaddr,sizeof (from->ipaddr)); -memset (info +sizeof (from->ipaddr),0 , - addrlen -sizeof (from->ipaddr)); +size_t addr_len = from->ipaddr.sa.sa_family ==AF_INET ? +sizeof (struct sockaddr_in ) : +sizeof (struct sockaddr_in6 ); + +memset (info,0 , addrlen); +memcpy (info, &from->ipaddr, addr_len); info += addrlen; }
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 64 lines · MingXuan
inet_diag_msg_sctpladdrs_fill()copiessizeof(union sctp_addr)(28 bytes, the size ofsockaddr_in6) from each sctp_sockaddr_entry.a into the netlinkINET_DIAG_LOCALSattribute and then only zeroes the bytes from offset 28 tosizeof(sockaddr_storage). The same pattern is used byinet_diag_msg_sctpaddrs_fill()forINET_DIAG_PEERS.The IPv4 address-filling helpers
sctp_v4_from_addr_param()andsctp_v4_from_skb()only initialize thesockaddr_inportion (16 bytes) of the unionsctp_addr; the trailing 12 bytes (offset 16..27, the sockaddr_in6-only region) are left uninitialized. Those bytes are propagated verbatim throughsctp_add_bind_addr()(which copiessizeof(union sctp_addr)=28 bytes) and then copied straight to userspace by the diag fill functions, leaking 12 bytes of kernel stack residue per local/peer address to any process that can issue aSOCK_DIAG_BY_FAMILYdump forIPPROTO_SCTP.Fix it by computing the actually-initialized length of the address from its
sa_family(structsockaddr_inforAF_INET, the whole union otherwise) and copying only that many bytes into an already-zeroedsockaddr_storageslot, so the uninitialized tail is never read and never reaches userspace.Fixes 8f840e47f190cbe61a96945c13e9551048d42cef ("sctp: add the sctp_diag.c file")Signed-off-by MingXuan <[email protected]>--- net/sctp/diag.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)net/sctp/diag.c +12
-6@@ -85,8 +85,12 @@ static int inet_diag_msg_sctpladdrs_fill(struct sk_buff *skb, info =nla_data (attr);rcu_read_lock ();list_for_each_entry_rcu (laddr, address_list, list) { -memcpy (info, &laddr->a,sizeof (laddr->a)); -memset (info +sizeof (laddr->a),0 , addrlen -sizeof (laddr->a)); +size_t addr_len = laddr->a.sa.sa_family ==AF_INET ? +sizeof (struct sockaddr_in ) : +sizeof (struct sockaddr_in6 ); + +memset (info,0 , addrlen); +memcpy (info, &laddr->a, addr_len); info += addrlen;if (!--addrcnt)@@ -113,9 +117,12 @@ static int inet_diag_msg_sctpaddrs_fill(struct sk_buff *skb, info =nla_data (attr);list_for_each_entry (from, &asoc->peer.transport_addr_list, transports) { -memcpy (info, &from->ipaddr,sizeof (from->ipaddr)); -memset (info +sizeof (from->ipaddr),0 , - addrlen -sizeof (from->ipaddr)); +size_t addr_len = from->ipaddr.sa.sa_family ==AF_INET ? +sizeof (struct sockaddr_in ) : +sizeof (struct sockaddr_in6 ); + +memset (info,0 , addrlen); +memcpy (info, &from->ipaddr, addr_len); info += addrlen; } --
Quoted 2 lines
2.50.1 (Apple Git-155)
Jakub Kicinski
maintainerQuoted 1 line · On Wed, 15 Jul 2026 11:35:36 +0800 MingXuan wrote:
Fixes 8f840e47f190cbe61a96945c13e9551048d42cef ("sctp: add the sctp_diag.c file")
Please trim the commit hash per recommended format of the Fixes tag
Quoted 12 lines
Signed-off-by MingXuan <[email protected]>--- net/sctp/diag.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)net/sctp/diag.c +0
-0@@ -85,8 +85,12 @@ static int inet_diag_msg_sctpladdrs_fill(struct sk_buff *skb, info =nla_data (attr);
net/sctp/diag.c static int inet_diag_msg_sctpladdrs_fill(struct sk_buff *skb, · line 93
Quoted 10 lines
net/sctp/diag.c +6
-2rcu_read_lock ();list_for_each_entry_rcu (laddr, address_list, list) { -memcpy (info, &laddr->a,sizeof (laddr->a)); -memset (info +sizeof (laddr->a),0 , addrlen -sizeof (laddr->a)); +size_t addr_len = laddr->a.sa.sa_family ==AF_INET ? +sizeof (struct sockaddr_in ) : +sizeof (struct sockaddr_in6 ); + +memset (info,0 , addrlen); +memcpy (info, &laddr->a, addr_len);
Having a variables called addrlen and addr_len in the same context is quite confusing. Can you come up with a better name?
Signature · 1 line
pw-bot: cr