From nobody Sun May 24 19:33:15 2026 Received: from mail-qv1-f48.google.com (mail-qv1-f48.google.com [209.85.219.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79C923314AE for ; Sat, 23 May 2026 02:34:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779503684; cv=none; b=gZ5e7Ial2Jzh1UiXMk7AYtxGsB1rAGSZlRYsd4v2vNPlgU25GJzFpUDJdRrQw/ZyWwKdMC7fBt93cekXuUiNj7AQ3kbYNlL9TgRlK9ozOQf/01e+wcFfyXA7zritHJV78tUBhQ6IvWcl1Q3MHI+mxtQ4zDmnb4e1MYAw0NuL358= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779503684; c=relaxed/simple; bh=w62EixdXxW4lxN7YjkeQjbgrtjS8Wu59Jj7E2YpYpSg=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=YVLdokk9tkbPGuzTZbt3lKXeyeHX5Wgx2Z7ulSFPz77QF1YOjNJq7zuLEvdUx/wsrM3n2gvYxSczQcmzsgkD7Hvb3SUAhhqC8bjSGoqfwB6xHZA9uUg+JjWcrNFcBTFdAebTxp/6g5WlkJRClGNT8O6AV8E3UB/Yn1f54QwXSqI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=cIB65uF3; arc=none smtp.client-ip=209.85.219.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cIB65uF3" Received: by mail-qv1-f48.google.com with SMTP id 6a1803df08f44-8bc3ef10cc4so106524646d6.1 for ; Fri, 22 May 2026 19:34:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779503682; x=1780108482; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=sK2eCVykGOBgCXXqQhMEZuidmUMZ94iDMgqX9P1DHWY=; b=cIB65uF35qSBBH97wlM+Y7NmRZ6BMJsvKhNP/9ZRu3HoaW/oXIY6Ov5/HyGuNUVAnh ZGvAJyZQFRzPRir4ZYMOn9Or8Tjlfu9m75WadLVsEZ3pR8/C3Bx8tq9W/e2+6v84cZoR mPrPQ7FPvshDTgQMtL3IKmCb+/mTYrEoZIjaJT0KO0VClncJasoNitwZ7+63DwwrzUtH j1EeC8/GmP3Anx8ZOI/NuZeokFhq5u8qiC1rPH09ofmMzjLH+fi6wm2ehXIgicmCkDFN FOh/sRX9GnDQQvzPtq6ax8Nk30bIZ+XLQM5QG3VGDsB3JF06kOtVgib6ww4oPFeRjoGX UnFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779503682; x=1780108482; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=sK2eCVykGOBgCXXqQhMEZuidmUMZ94iDMgqX9P1DHWY=; b=BtqldENM/S8UggvtgJszllb1F/vlRoedPRShpkWrqL12tz6rp+OqU0T8SeAAOJunA/ R9F6Ft2MWT10GtJ50QfFKpscA/kbunyY1oJ4C6wiA5bzkF8aTEMDADWgphISwXo64nfZ 3GI3uVMsi1D+dSZ8dMHb/UJ8+cdTXXWzrnzM18HzmEsjjWAzyQV6gvejIRDDBi1Vt3SG 8L1bqhdWDCMWwAs1JCL07Jc3rEt2zI159ZwKyKOQSa8b8B4JsFcXmLfLkh2NX9P6haq3 IvBiQKZ7qwv87OkS1Bu9ZgD9fhjRsiOKDxKEwLz8CkjvRo6+rwtCaZnpxd/gGDU0SLdQ 3uOg== X-Forwarded-Encrypted: i=1; AFNElJ8BMap51VJBiTo4c6ZSvX+3Tjr6OUpW+H627sP4cHNP9ZEDDwhjgVt3i1ImgVPKPs8ZJMLYs02PQn4AgN0=@vger.kernel.org X-Gm-Message-State: AOJu0YwStXi3AQRYn39lKCNLKta2otY7BAE+pnXfGvBP9vKHukhPvvto CuMkiX/ObQjeYQWLRahZ9f2quHHrFkMq2FCEfzGDzGis2MjuQslTIYu4 X-Gm-Gg: Acq92OH033orBB+wggyI6mCUVZ9r9D0RBMgC9CRKBSWZU643U8GSIKtfRoolQUnGVhn bbD5eeO3X3OaOrJkCEbcYv1+P8yPWQ9pMK9DU29NnbzACwKOjrGCzUpgkAifeHYDQMEtQBZ8C3a 4Y42wwhzjU8UD7CStI1aQrwQmnPcy0Ysn7wMgIpDTUqqrZwjjIO8gG6olp9vYV3Io5L9j1fcsVd 2/shgbREhUIepd9zyHprtTeudIIFZJwx83UqmbVMNxz9X0xBsewOplN7vp1zSCF84irF7DtYTN3 LthcAPGN9jpcEiVnNRrHlYwL8q2J0qK8KZfUFlWuYGGselvbfYajk3cCAWUqcRNAysvavdPvzZC KeBocSWT0Dpxpw6fotL3gknn6mo8iC9nkutBCbPqKU0EX47iYZx8U/j6It4XZ9xBExU/ybFOhHh LhtQbNBxDEJTneGXQDUp45YPbSV0GjJLOU/BtYVLmzeHiEa5GjlfusuYMh7aIpOknUYy1W3hZLV rm+y1s76lAQ+xjAoGRK X-Received: by 2002:a05:6214:5b85:b0:8ac:b677:c408 with SMTP id 6a1803df08f44-8cc7b5ebbfamr110690436d6.44.1779503682214; Fri, 22 May 2026 19:34:42 -0700 (PDT) Received: from server0 (c-68-48-65-54.hsd1.mi.comcast.net. [68.48.65.54]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8cc812e269csm35095256d6.25.2026.05.22.19.34.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 May 2026 19:34:41 -0700 (PDT) From: Michael Bommarito To: "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: James Chapman , Tom Parkin , Guillaume Nault , Simon Horman , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Kees Cook , netdev@vger.kernel.org, linux-rt-devel@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH net] l2tp: use refcount_inc_not_zero in l2tp_session_get_by_ifname Date: Fri, 22 May 2026 22:34:23 -0400 Message-ID: <20260523023423.2568972-1-michael.bommarito@gmail.com> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A reader in l2tp_session_get_by_ifname() can return a pointer to a session whose refcount has reached zero. The getter takes its reference with plain refcount_inc(), but every other session getter in the same file (l2tp_v2_session_get, l2tp_v3_session_get, and the corresponding _get_next variants) uses refcount_inc_not_zero() because the IDR/RCU lookup can race with refcount_dec_and_test() -> l2tp_session_free() -> kfree_rcu(). The ifname getter is the only outlier; the inconsistency was raised on-list after 979c017803c4 ("l2tp: use list_del_rcu in l2tp_session_unhash"). A reader inside rcu_read_lock_bh() that matches session->ifname can be preempted between the strcmp() and the refcount_inc(). If the last reference drops on another CPU in that window, the reader's refcount_inc() runs on a counter that has reached zero. refcount_t catches the addition-on-zero, prints "refcount_t: addition on 0; use-after-free", saturates the counter, and returns the saturated pointer to the caller. Session memory is held live by the in-flight RCU read section, but the kfree_rcu() callback queued from l2tp_session_free() will free it once the grace period closes; a caller that dereferences the returned session past that point hits a slab-use-after-free. On PREEMPT_RT local_bh_disable() is a per-CPU sleeping lock and the preemption window is real; on stock PREEMPT kernels local_bh_disable() is a preempt_count increment that closes the cross-CPU race in practice (see below). Use refcount_inc_not_zero() and continue the list walk on failure, matching the other session getters in the file. The ifname getter is the only session getter in net/l2tp/ that still uses the bare refcount_inc() pattern; this change restores file-internal consistency. The success path is unchanged. Fixes: abe7a1a7d0b6 ("l2tp: improve tunnel/session refcount helpers") Cc: stable@vger.kernel.org Assisted-by: Claude:claude-opus-4-7 Signed-off-by: Michael Bommarito --- Distro reachability: l2tp_core and l2tp_netlink autoload via the genl family alias net-pf-16-proto-16-family-l2tp on first AF_NETLINK CTRL_CMD_GETFAMILY lookup; pppol2tp autoloads on demand via net-l2tp-type-7 from L2TP_CMD_SESSION_CREATE with pw_type=3DL2TP_PWTYPE_PPP. Neither autoload requires CAP_SYS_MODULE. SESSION_GET / SESSION_CREATE / SESSION_DELETE are GENL_UNS_ADMIN_PERM, so CAP_NET_ADMIN in the netns user namespace suffices and is reachable from `unshare -Urn` on Debian 11/12, Ubuntu 22.04 LTS / 23.10+ / 24.04+, Arch, Alpine; RHEL/Fedora blacklist l2tp_netlink by default but on hosts running NetworkManager-l2tp, xl2tpd, or any L2TPv3 endpoint the gate is open there too. PREEMPT_RT ships on Ubuntu Pro Realtime, RHEL 9 RT, SUSE RT, and Yocto-RT. Reproduced on x86_64 QEMU/KVM. Under tracefs-kprobe widening at the refcount-inc instruction, a PREEMPT_RT build hits "refcount_t: addition on 0; use-after-free" through l2tp_session_get_by_ifname at 533 s; patched runs 1800 s clean. A stock PREEMPT (non-RT) build with the same widening ran 1800 s without firing: local_bh_disable() on non-RT prevents preemption inside the RCU section so the cross-CPU window does not open at observable rates. net/l2tp/l2tp_core.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c index 1455f67e01ddb..0000000000000 100644 --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c @@ -441,12 +441,13 @@ struct l2tp_session *l2tp_session_get_by_ifname(const= struct net *net, idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) { if (tunnel) { list_for_each_entry_rcu(session, &tunnel->session_list, list) { - if (!strcmp(session->ifname, ifname)) { - refcount_inc(&session->ref_count); - rcu_read_unlock_bh(); + if (strcmp(session->ifname, ifname)) + continue; + if (!refcount_inc_not_zero(&session->ref_count)) + continue; + rcu_read_unlock_bh(); - return session; - } + return session; } } } -- 2.46.0