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 05AAB223335; Thu, 12 Dec 2024 15:43:37 +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=1734018218; cv=none; b=EA74XriMpCEjrXvutCyPtIhIJ2yDpb5bibuQjI613AAwDr/5je4EY5tpqLFPK9elDR/sECBQCymrDLZCnjSTt255lzGyQd7nz47TQHYGLDvdrme4x5ymJh9QEtV0OqqFdR6XYm2N2v4xPmIFdh86cAy12G//ZrSapjroBhQKTcA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734018218; c=relaxed/simple; bh=QeF++zrA2uFT/o5t5UGd2k78P+2IBx09HKuKm5IL2pA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eFgs/F9a7sUCvc9uT0VRSUjHRZ7d6Yjbd6dkfMZgiab35gzLBXPWxOy4Zc0x5d1VM7RK+BBY7XjZbdXR7Uu/lX6xEilEJ1xlioVRvOg9cclQGkFpv+6ZQ52xZK0L7ZkGgiG2mlkPNrQFIr/t844ULJLAumsNJUaJhMqJafdYg2k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=C2v86oUM; 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="C2v86oUM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 22DBDC4CED0; Thu, 12 Dec 2024 15:43:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1734018217; bh=QeF++zrA2uFT/o5t5UGd2k78P+2IBx09HKuKm5IL2pA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C2v86oUMIZ3k/X4pU6Sq9WbUV6LxoHGzRq3Yp6GZHmXo1oMfGMkHSG9QUCuEU+jqA RSjlg2ll3oteQ2p/A7I75Cu8ppVwHZ7dlVSyMHDZFM1zqzzDICI5+eon4I3TR52qxT 2lqXFGJE3Dnck0MsuMMolDX3JLsFGlmQPzCFKz1o= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Eric Dumazet , Ignat Korchagin , Kuniyuki Iwashima , Willem de Bruijn , Jakub Kicinski , Sasha Levin Subject: [PATCH 6.6 224/356] af_packet: avoid erroring out after sock_init_data() in packet_create() Date: Thu, 12 Dec 2024 15:59:03 +0100 Message-ID: <20241212144253.464171950@linuxfoundation.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241212144244.601729511@linuxfoundation.org> References: <20241212144244.601729511@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.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Ignat Korchagin [ Upstream commit 46f2a11cb82b657fd15bab1c47821b635e03838b ] After sock_init_data() the allocated sk object is attached to the provided sock object. On error, packet_create() frees the sk object leaving the dangling pointer in the sock object on return. Some other code may try to use this pointer and cause use-after-free. Suggested-by: Eric Dumazet Signed-off-by: Ignat Korchagin Reviewed-by: Kuniyuki Iwashima Reviewed-by: Willem de Bruijn Reviewed-by: Eric Dumazet Link: https://patch.msgid.link/20241014153808.51894-2-ignat@cloudflare.com Signed-off-by: Jakub Kicinski Signed-off-by: Sasha Levin --- net/packet/af_packet.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index 3e5703537e4eb..56e3ae3b6be93 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -3428,17 +3428,17 @@ static int packet_create(struct net *net, struct socket *sock, int protocol, if (sock->type == SOCK_PACKET) sock->ops = &packet_ops_spkt; + po = pkt_sk(sk); + err = packet_alloc_pending(po); + if (err) + goto out_sk_free; + sock_init_data(sock, sk); - po = pkt_sk(sk); init_completion(&po->skb_completion); sk->sk_family = PF_PACKET; po->num = proto; - err = packet_alloc_pending(po); - if (err) - goto out2; - packet_cached_dev_reset(po); sk->sk_destruct = packet_sock_destruct; @@ -3470,7 +3470,7 @@ static int packet_create(struct net *net, struct socket *sock, int protocol, sock_prot_inuse_add(net, &packet_proto, 1); return 0; -out2: +out_sk_free: sk_free(sk); out: return err; -- 2.43.0