From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6B7451FA15C; Sun, 24 Nov 2024 13:52:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732456366; cv=none; b=ef4YuYhEaEnDDlD24120dghM4oiUpaYBz7YALaRNw+z5jT8hBq6yDo5NpQ84wn1KNvNX/uf0iyD5WBNKY24kp+a37pJLN9+51mOSCRuV9Sg4CP/dswP3/s3dm4UceRCaO9nsR2I+/G4vBZfLvH+JOEveMFe1Cmw6wHi50dhTlsI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732456366; c=relaxed/simple; bh=uriQ62Bg+ivq8F9PLoUKANOvfHZDWI229t2t5azpyF0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TuG/nXEZujyv+tQPY+gfXstJO4DkRh0sYImSmXfwlavTL4BIj3DvyyJ1IilRYxCf/qhVs27+nZ0z1mUZyuHLjsCVB5OfaqEQDCQnNed59tifDExKhX336SCMUA5/TxLIhPcgoszqmBB46N0xMkqszmrQnKkSqpJutAJ9MFL2evU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rjL1G/ph; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rjL1G/ph" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 231D4C4CECC; Sun, 24 Nov 2024 13:52:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732456366; bh=uriQ62Bg+ivq8F9PLoUKANOvfHZDWI229t2t5azpyF0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rjL1G/phsw/4FcsuVcarEjCtkn04NblQX1LS9235ncb0nZN/h8EmCvTnJryuU+lEB 89PtUciGs47MDsAnyv5BUTbb8rJbSKVteuOMRDYKliV3BSIjrBdjx75MrIcI3+6cB0 v1OxMnlor4eOjTlqa26SYO/Au4sr/LHJtoR6+KTyse3bm4FoDArMOXZhOc+BQzqaKP UiUZwzmwonEw09Qolpx+fQ8TelVz0rMhuFwigis7bPeskw1PC/qsLDHmYTTIv/9dzt J+8AeB7c2ZwDJMv2UvtK1jQJftbd3OqyER0aWeI5YtpSBUvd8C87XXvat+eBGYbhFE emGJ4tpU0+/cw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Ignat Korchagin , Kuniyuki Iwashima , Eric Dumazet , Jakub Kicinski , Sasha Levin , davem@davemloft.net, dsahern@kernel.org, pabeni@redhat.com, netdev@vger.kernel.org Subject: [PATCH AUTOSEL 5.15 14/36] net: inet: do not leave a dangling sk pointer in inet_create() Date: Sun, 24 Nov 2024 08:51:28 -0500 Message-ID: <20241124135219.3349183-14-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241124135219.3349183-1-sashal@kernel.org> References: <20241124135219.3349183-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 5.15.173 Content-Transfer-Encoding: 8bit From: Ignat Korchagin [ Upstream commit 9365fa510c6f82e3aa550a09d0c5c6b44dbc78ff ] sock_init_data() attaches the allocated sk object to the provided sock object. If inet_create() fails later, the sk object is freed, but the sock object retains the dangling pointer, which may create use-after-free later. Clear the sk pointer in the sock object on error. Signed-off-by: Ignat Korchagin Reviewed-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet Link: https://patch.msgid.link/20241014153808.51894-7-ignat@cloudflare.com Signed-off-by: Jakub Kicinski Signed-off-by: Sasha Levin --- net/ipv4/af_inet.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c index b225e049daea2..0701de762c4c0 100644 --- a/net/ipv4/af_inet.c +++ b/net/ipv4/af_inet.c @@ -373,32 +373,30 @@ static int inet_create(struct net *net, struct socket *sock, int protocol, inet->inet_sport = htons(inet->inet_num); /* Add to protocol hash chains. */ err = sk->sk_prot->hash(sk); - if (err) { - sk_common_release(sk); - goto out; - } + if (err) + goto out_sk_release; } if (sk->sk_prot->init) { err = sk->sk_prot->init(sk); - if (err) { - sk_common_release(sk); - goto out; - } + if (err) + goto out_sk_release; } if (!kern) { err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk); - if (err) { - sk_common_release(sk); - goto out; - } + if (err) + goto out_sk_release; } out: return err; out_rcu_unlock: rcu_read_unlock(); goto out; +out_sk_release: + sk_common_release(sk); + sock->sk = NULL; + goto out; } -- 2.43.0