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 24CDA215798; Thu, 12 Dec 2024 17:50:51 +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=1734025851; cv=none; b=kLKPnLmdSQnXwCNOXy1JNV+T1Vmy8jhds5kchQp6/IZSmGfhqpjJG4E1ZOgcEsShK8UBrjEQsCQ8kJL6gVir5y6Wg1Vvw5XQE3i1VUsIzkDIyTL63Nn32P5xbhMQNFADB/cBkZKbBslu2q7scNm7Gk8Suk76MXDSfrJsV9mPaSk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734025851; c=relaxed/simple; bh=x4zpX+V5Unog2GHl4uvbg0z9w1tyffAOFdxNC84QH18=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WM0ZEb1682Qbf0zGTIvOc7Bjp1TT1F7kkJRE26L1pIUS84QIetCfK568tJ+2w71J24glT8aLqNYRBwVN3d5Cr/1AB6qUmghJvESvbh4ZyNyd7Eo/0Sm7htXTeFo3lVucHIxWXuunVSgsyVpgpg7gJolPe+0BIoLx+FnxqNdRIKA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=PmikCFHt; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="PmikCFHt" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A03CFC4CECE; Thu, 12 Dec 2024 17:50:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1734025851; bh=x4zpX+V5Unog2GHl4uvbg0z9w1tyffAOFdxNC84QH18=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PmikCFHtxOVs3YLhr4VYKPCxll/Y+B2oQH2NuF7N8nxzpvBDA1qdbANO3RjnXDeFN 1yQ9QhEgITJyj43lB8a3+rTHh7WIkyDg5LdqvLT54QmoBi65tCIvpwssY5vj7cFjKg K2PCXijlo2oiLdjcXYgwyk96tI0LMzmi2K9GIilQ= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Ignat Korchagin , Kuniyuki Iwashima , Eric Dumazet , Jakub Kicinski , Sasha Levin Subject: [PATCH 5.4 278/321] net: inet: do not leave a dangling sk pointer in inet_create() Date: Thu, 12 Dec 2024 16:03:16 +0100 Message-ID: <20241212144240.961741117@linuxfoundation.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241212144229.291682835@linuxfoundation.org> References: <20241212144229.291682835@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.4-stable review patch. If anyone has any objections, please let me know. ------------------ 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 be2b786cee2bd..486ab202303ff 100644 --- a/net/ipv4/af_inet.c +++ b/net/ipv4/af_inet.c @@ -369,32 +369,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