From nobody Thu Sep 24 12:52:59 2026 Received: from mail-pj2-f13.google.com (mail-pj2-f13.google.com [74.125.227.141]) (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 40FF641DDF0 for ; Thu, 24 Sep 2026 07:16:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.227.141 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790234202; cv=none; b=YpU57L79olqKOfJ/tu0L806t2u0TrXTQBH7JFH+B8tTt74KbJ+VvzMxbsTu/ULH1NMZ9Vi/T4sVrkfOZqwsdh5OXEPa0lqdHzc2tbsIGGu4kElPdAw5rGfxZPmuABEhj9n0jyE8CQ/dcDC0h4eDIh/WopWhhT2qCPVy5/VvdUyA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790234202; c=relaxed/simple; bh=hYnJ9I+SNx7F4uX7FBbOoRj/uPSXg/C3ApDaKmBP2ow=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OGBxTL93/u8HU76yQzgV0drlLlv3SlX305AOBAHFyKnXFn26gW6+wdHYSaSP9R/RsntKQ0pcMWcsZkfyTjGcMdV10mACSR2gLKzdDeM3gnpf6SRrTIE8IUWU6foUZeex4YAFlj2YcXx1ee63XGrVbbQP19HpNRVCUG+ieWtB2qo= 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=OjJElf+8; arc=none smtp.client-ip=74.125.227.141 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="OjJElf+8" Received: by mail-pj2-f13.google.com with SMTP id 98e67ed59e1d1-398b3b189e0so995307a91.2 for ; Thu, 24 Sep 2026 00:16:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790234192; x=1790838992; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=IsM2VZ0IjpuwkgawgmQ1mCqOQgkHltfRpIT8ot/Ka44=; b=OjJElf+8SyKAZx81/sX9xCITxHIaz11/iYWorlcKfjbrYCBXXuUfNkauFixvD5goZr V9CZqiZixhVYTV5WxxT7WpKrpC721pdP730GgFSTsxMbF+qnDztggpAS+XQ5Qj8F/IDn wDpt9DWbed/AN/14wrWdhnU2ZeqNtTOYgXID4PMrghUjVmMPT8eiLT4PpuH7FmEVre2X 5K/6gwv33djUr7KayYa0HCcB9sFmjMVmHaPC1yoOhCQjnyMsQaJlLoOC9NuLS9DMAvje MgUVCBWWJaLA11FG3qh4t+HEqPjpJrTN4Czg8Z57qGt44tI58HY+lgDEueN/65PbiWhP sOYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790234192; x=1790838992; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=IsM2VZ0IjpuwkgawgmQ1mCqOQgkHltfRpIT8ot/Ka44=; b=R8Z0v4mqlwSZWcAsfpSVion2PZSoI2zSeIbkzj0fganwGxJ9YYeZBxk7miQbUWHh45 f+h7jIR/j62N2C2GvYR0JopBfGtNNmnTufRHRexQkfOjx8BdciSV1wn090GvJWFn5Zz7 M8Ukv3Nrq+WPtkWppp/1pwcncnczvxrkiHK/ofoXEj2Xd24Z7RYHpGZ0dE7dGW9+8bpX kqePTF7hN6ERkyDrxjCNyyuYGNZdX61c3Kwul15sq+scdYNlSqw1N2yDI54f2ed+/0UU 0Li8HPKrwH6UynguTwshWQMEZZRGQhVeS8mOhaf4AcwMAZyyK9VIqlEJXcw28zicJGYQ xl2A== X-Forwarded-Encrypted: i=1; AKwUvBx6Ed4d5/1vUMCthWxEtYkul4DyM/5Knsr/0oyzwn/Bt+HC21x2KIDexAcqMveRo9VKsUadf1SjeV0oPEM=@vger.kernel.org X-Gm-Message-State: AFuF++lbeHEBrvGTz5Xu0R5Ik4pvD2kgXRT6RuQoTrELPFxNzrBEbYQJ mH3RWKWYIrupNuqb3XcqJZv6w/Kz9vjzUS+dQu8hBAUD4EcvGQEToX6A X-Gm-Gg: AYBFou3qKLmzjUBzjDjGQk7yG02qSzqNC62kd2x40N0zW4LrAc6kOZdk0Z1OfSs0T/b 883+GihBz8jFH8OOPWH2yRSI7mSbJiG5odBj1jqzK5dmPzJN1mFryZtqec13fjLSHLds5O8kYne O68NZuHUPka0Sfr7bZbQlChHrvzESq9vDrfsF2Ph/6G7O1G7x4HfSgLFlwKTJ81g0XVvhYJS8gJ TbvVndwCarUcV37FcXePMrZvmtQrpbzxnIIcWYhbbFbpqK5epOxYtKehIcruDLUojB7buYhvSF/ pMk5yUIGlsjCTHYfjfRWFaW2cs1Vkw+THREZPTtOBvYYjm7Rt3Dyj7qVGW5Anql2HCLWoQ7+Sbv N2lX/AJqqXlHzpoVQIzyAHc21D9LRmZSlLW2BF2TGfwNVKj0ufdQLhARPpWIeMllq2IYX9eysIK XNgawWt0MTNUgUoiLrZmXKEyJxoss4BcylbT9lLMZBGKJRewxI52WWEuFG+7yiBaSgwpF5Oezk+ KMpW3URqjzFSAa8K16GYkr8njFnNRFiQybinTE0IgW1IfWeclmof9Qr62n9T+/unteQWGE/gt3p ijcWuVwlbg== X-Received: by 2002:a17:90b:4c0b:b0:39e:6a81:5a96 with SMTP id 98e67ed59e1d1-3a098e37bbamr1320905a91.42.1790234191488; Thu, 24 Sep 2026 00:16:31 -0700 (PDT) Received: from phui-2.c.googlers.com.com (67.51.127.34.bc.googleusercontent.com. [34.127.51.67]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3a0972efa00sm4003402a91.5.2026.09.24.00.16.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Sep 2026 00:16:31 -0700 (PDT) From: Hui Peng To: kuniyu@amazon.com, edumazet@google.com, davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Hui Peng Subject: [PATCH net v2 1/2] af_unix: decrement u->inq_len when skipping unread OOB skb in manage_oob() Date: Thu, 24 Sep 2026 07:16:28 +0000 Message-ID: <20260924071629.2683160-2-benquike@gmail.com> X-Mailer: git-send-email 2.56.0.rc1.310.g51773c2048-goog In-Reply-To: <20260924071629.2683160-1-benquike@gmail.com> References: <20260924071629.2683160-1-benquike@gmail.com> 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" When a normal in-band read on an AF_UNIX stream socket (without MSG_PEEK and without SO_OOBINLINE) encounters an unread u->oob_skb in manage_oob(), manage_oob() clears u->oob_skb, unlinks the 1-byte skb from sk->sk_receive_queue, and drops it with SKB_DROP_REASON_UNIX_SKIP_OOB without decrementing u->inq_len. Because queue_oob() incremented u->inq_len by 1 when queuing the OOB skb, u->inq_len remains permanently inflated by 1 byte for each skipped unread OOB skb, causing SIOCINQ / FIONREAD to report a stale positive byte count on an empty socket. Decrement u->inq_len by 1 when unlinking the unread OOB skb in manage_oob(). Tested in QEMU against Linux 7.3.0-rc3 by sending three 1-byte MSG_OOB packets interleaved with normal stream data on an AF_UNIX SOCK_STREAM socketpair and draining all in-band data via recv(): on the unfixed kernel, ioctl(SIOCINQ) reports 3 on the empty socket; with this patch applied, ioctl(SIOCINQ) reports 0. Fixes: f4e1fb04c123 ("af_unix: Use cached value for SOCK_STREAM in unix_inq= _len().") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Hui Peng --- Changes in v2: - Split the manage_oob() and unix_stream_read_skb() fixes into a 2-patch series as requested by Kuniyuki Iwashima. - Update Fixes: tag to f4e1fb04c123 ("af_unix: Use cached value for SOCK_STREAM in unix_inq_len().") and clarify the commit message as noted by Sashiko. net/unix/af_unix.c | 1 + 1 file changed, 1 insertion(+) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 42cffeafc8c1..1770af3c2684 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2849,6 +2849,7 @@ static struct sk_buff *manage_oob(struct sk_buff *skb= , struct sk_buff **last, WRITE_ONCE(u->oob_skb, NULL); =20 if (!sock_flag(sk, SOCK_URGINLINE)) { + WRITE_ONCE(u->inq_len, u->inq_len - 1); __skb_unlink(skb, &sk->sk_receive_queue); unread_skb =3D skb; skb =3D skb_peek(&sk->sk_receive_queue); --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 12:52:59 2026 Received: from mail-pz2-f12.google.com (mail-pz2-f12.google.com [74.125.228.12]) (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 11AA5425880 for ; Thu, 24 Sep 2026 07:16:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.228.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790234199; cv=none; b=Y3ZehZPjDsKiyuyMU4vNe1gaA8/bbIxhehDheygZMHjytHBQrvOrX8AOVCP922DoXGjOw+v/07kVspRdXZCTe8xLxRZ9VSj3ydZ2n8cVAJIp5plw8KMl523JxZggu+oowCVF4ypT79GcDjd2vvkzVGBBFOji/5vn9PkGvWW5Z7A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790234199; c=relaxed/simple; bh=6frqMCwNqJz3N3QZ9P0MlfVB/WAQLVZicQlCmLK3YWU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N50X0qNrTaIfJv9PynMNqnkCfnaL+HKMZ1v/54d41IRa/ilcjpzScNrGK7+lJrPRpdDJMy3XTzaTrSdUIrqqbi76sGdu70AGon3BujT8vMMDcmFM3fWc6IS/tTs6X5yJP/NhOd8yi4nh/yhis29RSnEfRxksitxTbrOhxYSerVM= 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=WtqYSsk3; arc=none smtp.client-ip=74.125.228.12 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="WtqYSsk3" Received: by mail-pz2-f12.google.com with SMTP id 41be03b00d2f7-cc1ceb47d53so388261a12.0 for ; Thu, 24 Sep 2026 00:16:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790234192; x=1790838992; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+3DzxIn9zZnPR5YaGs2TT46i5wRUo2UQR+CEo7kDWoE=; b=WtqYSsk3DCCvoPBSqg899SYkAn2gcdsR2D1g4AqKFx2Pq3ar2Gcfj3NgsVW/GA5jEK z24Dljt5CxPmnAA6O6HK9NzSPHNzR+9WfLRVKFVaXjXHmzrE1qvtrQu5tf1W7vURXxzL CfuY9KSUeu6JJ+DsktKWjpVqWDLnCaoGtflbDtL0rkxiBHFuue4MDa1eJ7oK35l6hPG2 yj9yQhOps9zpW7rpczgVKEzGO57t73sxymF7L29Ea7R5P4YOwVh2SrDHxePfYnYeijss EtXNAi8kQ6Q0zFijvbvIZoBXqUQ75PsseRvoBWojvPqhJoFwf8Yq2tN9IjBKBFsOxBiM U8xA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790234192; x=1790838992; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=+3DzxIn9zZnPR5YaGs2TT46i5wRUo2UQR+CEo7kDWoE=; b=jjjWPD8cUJRbtkHaNEm3ws3BG1U3ATozzPwaGNLsyHzGNHNnJxomeXC60V5fjC57Is MY9zq9vQ8Xre35TRSshTdj4oGsebbblQZjG4M6MEoMdJyBKsqfJbBgzk+XG6hohxvCRr ZRXUFVomUtMU+EEE4OvTPTTxHVN7ExuE9VnQ4N8t997IMb7JAHxblp8We0veYPvXFSbT 9AMB8kHpoY7I1exkflQrtKy3c1/cevnY1T34JVM7HWuB8qGTb5GC6hfYVgPU40o3f6vn eJs7wkwjAMa2uOCmIRzwxmlFcn43oiDh62y3u9ZjyeaIOfgR1o928Q5Ar6FqUZUlTnxf jULQ== X-Forwarded-Encrypted: i=1; AKwUvBxYXuAFBwHxu2OHiwtmhZTu3Eu0dGY41fmix45BSv/ifMxurMCfLV7KMEySXGsKUdIIbb+vkq9oWHMzY/Q=@vger.kernel.org X-Gm-Message-State: AFuF++nZx3gwJDV5k+TmxP9Lynf2pGmmZ18S6LSMXdQefeM1XQXR0HCB v1BE7V/XVw8ByMFHsRh+romggWzTYwAwJ2IK/eesytPopmC8H+uO2xYt X-Gm-Gg: AYBFou2Ypv3Q9H96eep97fr2/8ly9mIZHzQyCTuvaq3lHx/18tUIxw/7Jt8BvIjlUl8 EHxeLvO77MX2b+UOws1SB8bZ0M/jAslyxtVssKUYVjqpvfUUcUjpYSVCIg5/WMCZpOGWL+SLupf ryg68TuIa6rdqHBxU7VFAlbrq29af54bT5+/0mbMM5UMeY5inCE/at75Ybci2Ymu+q6jFthkXap J33jf1/8Agib86rXtJ6078eJlAJX4BzyTtdfkjBY7Q6+ivPDsW1xikmL1GsSAUzM4sLNlR8Y0OE JiPmTv6fp6/SeDdIEhJuiIU2ueQavmcm24ssrBQ/Jb6gnnbEptHQl6FS67qynvcdmmhKBotmp4v nkaeTW6/Oyfou0izyMGaZLmEC+Qd3QZrlpinEuwBJ8VORMMr0gjvodQw4C/yT/ZqPA7+EY+aqNf XB/CKOta/lWR1Gx03c+3KU0L3C7Gzk1WBXuAlQM3mUGbwhxULeQEdVq8LRkMLQ7ZYAw14wNIyCX zmJo8gP85dHO8DzDHlZ1RRb5QmHmzThq/qRXC9iE6FL7OAQfmVSi8eWs3GGS7Pba5UccgjW4B/Y ZNyV/F5RlA== X-Received: by 2002:a17:90b:564d:b0:39e:6c66:cbdd with SMTP id 98e67ed59e1d1-3a098acb900mr929337a91.21.1790234191974; Thu, 24 Sep 2026 00:16:31 -0700 (PDT) Received: from phui-2.c.googlers.com.com (67.51.127.34.bc.googleusercontent.com. [34.127.51.67]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3a0972efa00sm4003402a91.5.2026.09.24.00.16.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Sep 2026 00:16:31 -0700 (PDT) From: Hui Peng To: kuniyu@amazon.com, edumazet@google.com, davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Hui Peng Subject: [PATCH net v2 2/2] af_unix: skip consumed OOB skb and pull consumed bytes in unix_stream_read_skb() Date: Thu, 24 Sep 2026 07:16:29 +0000 Message-ID: <20260924071629.2683160-3-benquike@gmail.com> X-Mailer: git-send-email 2.56.0.rc1.310.g51773c2048-goog In-Reply-To: <20260924071629.2683160-1-benquike@gmail.com> References: <20260924071629.2683160-1-benquike@gmail.com> 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" When an OOB byte is consumed via recv(MSG_OOB) in unix_stream_recv_urg(), u->oob_skb is cleared to NULL and UNIXCB(oob_skb).consumed is incremented to 1, but oob_skb remains on sk->sk_receive_queue (with unix_skb_len(skb) =3D=3D 0) to preserve the OOB mark until normal reads advance past it. Similarly, a partial recv() in unix_stream_read_generic() advances UNIXCB(skb).consumed without pulling the skb header and leaves the partially consumed skb at the head of sk->sk_receive_queue. If the socket is subsequently read via unix_stream_read_skb() (used by BPF sockmap), unix_stream_read_skb() only checks skb =3D=3D u->oob_skb (which is only true for an unconsumed OOB skb) and ignores UNIXCB(skb).consumed. As a result, a consumed OOB skb (unix_skb_len(skb) =3D=3D 0) is handed to recv_actor() and re-delivers the already consumed OOB byte, and a partially consumed skb re-delivers its already consumed prefix. In unix_stream_read_skb(), skip and free zero-length consumed skbs (after calling unix_orphan_scm() so SCM_RIGHTS fd accounting remains balanced) and pull UNIXCB(skb).consumed bytes via pskb_pull() (which safely handles both linear and non-linear paged skbs) before invoking recv_actor(). Tested in QEMU against Linux 7.3.0-rc3 with BPF_MAP_TYPE_SOCKMAP and a BPF_SK_SKB_STREAM_VERDICT program: 1. Consuming a 1-byte MSG_OOB packet ("Z") before inserting the socket into sockmap and sending "HELLO" re-delivers "ZHELLO" on the unfixed kernel, whereas with this patch applied recv() receives "HELLO!". 2. Consuming a 3-byte prefix ("123") of "12345678" before inserting the socket into sockmap re-delivers "12345678" on the unfixed kernel, whereas with this patch applied recv() receives "45678". Fixes: 77462de14a43 ("af_unix: Add read_sock for stream socket types") Fixes: 314001f0bf92 ("af_unix: Add OOB support") Fixes: 638f32604385 ("af_unix: Disable MSG_OOB handling for sockets in sock= map/sockhash") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Hui Peng --- Changes in v2: - Split out from the manage_oob() fix into patch 2/2 as requested by Kuniyuki Iwashima. - Call unix_orphan_scm(sk, skb) before consume_skb(skb) when dropping a zero-length consumed skb so u->scm_stat.nr_fds is decremented, and use pskb_pull() instead of skb_pull() to safely handle non-linear paged skbs without hitting BUG() in __skb_pull(), as noted by Sashiko. net/unix/af_unix.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 1770af3c2684..f5d64e7b5c1c 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2885,6 +2885,7 @@ static int unix_stream_read_skb(struct sock *sk, skb_= read_actor_t recv_actor) return err; =20 mutex_lock(&u->iolock); +again: spin_lock(&queue->lock); =20 skb =3D __skb_dequeue(queue); @@ -2894,6 +2895,13 @@ static int unix_stream_read_skb(struct sock *sk, skb= _read_actor_t recv_actor) return -EAGAIN; } =20 + if (!unix_skb_len(skb)) { + spin_unlock(&queue->lock); + unix_orphan_scm(sk, skb); + consume_skb(skb); + goto again; + } + WRITE_ONCE(u->inq_len, u->inq_len - unix_skb_len(skb)); =20 #if IS_ENABLED(CONFIG_AF_UNIX_OOB) @@ -2913,6 +2921,14 @@ static int unix_stream_read_skb(struct sock *sk, skb= _read_actor_t recv_actor) =20 mutex_unlock(&u->iolock); =20 + if (UNIXCB(skb).consumed) { + if (!pskb_pull(skb, UNIXCB(skb).consumed)) { + kfree_skb(skb); + return -ENOMEM; + } + UNIXCB(skb).consumed =3D 0; + } + return recv_actor(sk, skb); } =20 --=20 2.55.0.1082.g2b9226bbc0-goog