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 F100C213E6B; Thu, 12 Dec 2024 16:33:17 +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=1734021198; cv=none; b=YH6eXqckxQo6g4+BNdmfOHyhoGCGM8IkkDix5ozDTH9nCyt3TTSqN4jzTNx2xIRRL0f9RETpuGlOn4I4GF+I+7kRWtWz2ol405NGSpPYYVqqMHrsGj0Nk5szmwTVTX+2DYhEUjDKccYNmv2tyf7A0QBQPuXT50g+AZlTjSHieb0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734021198; c=relaxed/simple; bh=iUXpDFAF94jgoOIGYy8kpQOK815c5u6aZy5L3u/dUH0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fWp29IvEzZxqsrC/Kgi3LSx00RIm2iPD3V+ijUKkTWg3orQ5TphWl1aTgQIXJyneZiw6ZvkMCuYy5GcoLamzqLQc88b06p3MHgvBISdSZxZqhvL8hYxgfnR0MrqQwRy3dkIFrIbPJY7nC+8PxdZR4FYqq7YwhaYJQNWHQPhgghI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=LJpIQien; 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="LJpIQien" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6DD7FC4CECE; Thu, 12 Dec 2024 16:33:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1734021197; bh=iUXpDFAF94jgoOIGYy8kpQOK815c5u6aZy5L3u/dUH0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LJpIQien3Q1l/BvjnFPuxU9RkZ9U2TD4ub+/Z4n23hF9imqyVFzOy0MNUMvi2HeS/ lP3t2i5IPhKqEkAy6w+TYAuVffXJSCb0/JOY6/iBFwrCPyQa0vAVRIqVyb2KGOGNEz Vz7QGJZprwx3h+CQg45d1uGTbKpcykuybhnmfWBs= 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 6.1 672/772] net: inet: do not leave a dangling sk pointer in inet_create() Date: Thu, 12 Dec 2024 16:00:17 +0100 Message-ID: <20241212144417.684407214@linuxfoundation.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241212144349.797589255@linuxfoundation.org> References: <20241212144349.797589255@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 6.1-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 cc013be9b02c4..4f3c932e37ed5 100644 --- a/net/ipv4/af_inet.c +++ b/net/ipv4/af_inet.c @@ -371,32 +371,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