Received: from smtp-fw-80008.amazon.com (smtp-fw-80008.amazon.com [99.78.197.219]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 95B445C917 for ; Sat, 3 Feb 2024 09:15:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.219 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706951714; cv=none; b=ft/C7n+yRUxS+5eJRVXl9vPKEqWOyzsdtqM+vpQSrT7THMElL3h2wmqNFQgM7s2efvjuTTR0+55FCQPQpAQcidpObJJImGSYtIr/UDiDa5TRt35Qf4hJARxMqm7OG05gcuLjNeMHML93EW75Z4gPTiaXyY36wKlp0oEOVIelWfg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706951714; c=relaxed/simple; bh=+XcHrARjKRURqoSM8s8sFRenAT30ihq4j0f33Ml4xBk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=sfeOljPF2AwiDV5FnMcFFhVChA6UnrYP1MKcTsg4nrOwhH5ZiM7RyTkpFSJPp+m1OZc5lQtXU7Dyz8vEXG9UCYXQWHRHTu5dfuDU9vCWAQ9sl8pCFb5fiKo/9NVqo3XZdkhgTOskRFDldAQy/u7cHuIK/e+f05cfc1ZoenMdAQE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=dC7INJUw; arc=none smtp.client-ip=99.78.197.219 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="dC7INJUw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1706951712; x=1738487712; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wP8mCgr2YAhc03be+/frac0wELY/R0FS3AyPgirlxyo=; b=dC7INJUwWQpNuN7EdkxjUmwPz5rjGyp9RBaf0fhOL7obRrJ0EWlb8NMZ 2KMSpB14Lsas40jjhrjYUZvDYkjMf1FgJYPTUzc2Ga/RRHI6xcVusPzHq cClyAmVAHbRs4pVlylVf1mAq2gvfqNuYJpBRzC7CCL1esEzLF33y8Kp8b I=; X-IronPort-AV: E=Sophos;i="6.05,240,1701129600"; d="scan'208";a="63441505" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-80008.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2024 09:15:10 +0000 Received: from EX19MTAUWC001.ant.amazon.com [10.0.21.151:36033] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.4.177:2525] with esmtp (Farcaster) id 14199849-eae7-4bb4-8ae1-720a0a36f356; Sat, 3 Feb 2024 09:15:10 +0000 (UTC) X-Farcaster-Flow-ID: 14199849-eae7-4bb4-8ae1-720a0a36f356 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWC001.ant.amazon.com (10.250.64.174) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Sat, 3 Feb 2024 09:15:10 +0000 Received: from 88665a182662.ant.amazon.com (10.106.101.14) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Sat, 3 Feb 2024 09:15:07 +0000 From: Kuniyuki Iwashima To: CC: , , , , , , Subject: Re: [PATCH v1 net] af_unix: Call kfree_skb() for dead unix_(sk)->oob_skb in GC. Date: Sat, 3 Feb 2024 01:14:59 -0800 Message-ID: <20240203091459.9066-1-kuniyu@amazon.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-ClientProxiedBy: EX19D044UWA004.ant.amazon.com (10.13.139.7) To EX19D004ANA001.ant.amazon.com (10.37.240.138) From: Eric Dumazet Date: Sat, 3 Feb 2024 10:01:04 +0100 > On Sat, Feb 3, 2024 at 9:33 AM Kuniyuki Iwashima wrote: > > > > syzbot reported a warning [0] in __unix_gc() with a repro, which > > creates a socketpair and sends one socket's fd to itself using the > > peer. > > > > socketpair(AF_UNIX, SOCK_STREAM, 0, [3, 4]) = 0 > > sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="\360", iov_len=1}], > > msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_SOCKET, > > cmsg_type=SCM_RIGHTS, cmsg_data=[3]}], > > msg_controllen=24, msg_flags=0}, MSG_OOB|MSG_PROBE|MSG_DONTWAIT|MSG_ZEROCOPY) = 1 > > > > This forms a self-cyclic reference that GC should finally untangle > > but does not due to lack of MSG_OOB handling, resulting in memory > > leak. > > > > Recently, commit 11498715f266 ("af_unix: Remove io_uring code for > > GC.") removed io_uring's dead code in GC and revealed the problem. > > > > The code was executed at the final stage of GC and unconditionally > > moved all GC candidates from gc_candidates to gc_inflight_list. > > That papered over the reported problem by always making the following > > WARN_ON_ONCE(!list_empty(&gc_candidates)) false. > > > > The problem has been there since commit 2aab4b969002 ("af_unix: fix > > struct pid leaks in OOB support") added full scm support for MSG_OOB > > while fixing another bug. > > > > To fix this problem, we must call kfree_skb() for unix_sk(sk)->oob_skb > > if the socket still exists in gc_candidates after purging collected skb. > > > > Note that the leaked socket remained being linked to a global list, so > > kmemleak also could not detect it. We need to check /proc/net/protocol > > to notice the unfreed socket. > > > > [ > > Reported-by: syzbot+fa3ef895554bdbfd1183@syzkaller.appspotmail.com > > Closes: https://syzkaller.appspot.com/bug?extid=fa3ef895554bdbfd1183 > > Fixes: 2aab4b969002 ("af_unix: fix struct pid leaks in OOB support") > > Signed-off-by: Kuniyuki Iwashima > > --- > > Given the commit disabling SCM_RIGHTS w/ io_uring was backporeted to > > stable trees, we can backport this patch without commit 11498715f266, > > so targeting net tree. > > --- > > net/unix/garbage.c | 9 +++++++++ > > 1 file changed, 9 insertions(+) > > > > diff --git a/net/unix/garbage.c b/net/unix/garbage.c > > index 2405f0f9af31..61f313d4a5dd 100644 > > --- a/net/unix/garbage.c > > +++ b/net/unix/garbage.c > > @@ -314,6 +314,15 @@ void unix_gc(void) > > /* Here we are. Hitlist is filled. Die. */ > > __skb_queue_purge(&hitlist); > > > > + list_for_each_entry_safe(u, next, &gc_candidates, link) { > > + struct sk_buff *skb = u->oob_skb; > > + > > + if (skb) { > > + u->oob_skb = NULL; > > + kfree_skb(skb); > > + } > > + } > > + > > Reviewed-by: Eric Dumazet > > Note there is already a 'struct sk_buff *skb;" variable in scope. > > This could be rewritten > > list_for_each_entry_safe(u, next, &gc_candidates, link) { > kfree_skb(u->oob_skb); > u->oob_skb = NULL; > } I wrote that in the inital fix but noticed that this kfree_skb() triggers fput(), and later in unix_release_sock() we will call the duplicate kfree_skb(). if (u->oob_skb) { kfree_skb(u->oob_skb); u->oob_skb = NULL; } So, we need to set NULL before kfree_skb() in __unix_gc(). > > Also we probably can send this later: Yes, I changed as such in a new GC implementation, this needs respin for MSG_OOB support though... https://lore.kernel.org/netdev/20240203030058.60750-14-kuniyu@amazon.com/ Thanks! > > diff --git a/net/unix/garbage.c b/net/unix/garbage.c > index 2405f0f9af31c0ccefe2aa404002cfab8583c090..02466224445c9ec9b1259468d30c89fc5e905a6b > 100644 > --- a/net/unix/garbage.c > +++ b/net/unix/garbage.c > @@ -283,7 +283,7 @@ void unix_gc(void) > * inflight counters for these as well, and remove the skbuffs > * which are creating the cycle(s). > */ > - skb_queue_head_init(&hitlist); > + __skb_queue_head_init(&hitlist); > list_for_each_entry(u, &gc_candidates, link) > scan_children(&u->sk, inc_inflight, &hitlist);