Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp551286rdb; Sun, 18 Feb 2024 00:19:08 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW+LzwUMsSzgjNrPCUn4dvF3+RxL6mj+OGxdbXVS7B1WXTgq6h1ODQ0y/cUXuGyMMWD8KVE0fk2ojr0xpIMzm7p2juxH1n42YjBaTrDag== X-Google-Smtp-Source: AGHT+IE2P4I9AIPEP/tnfvNbBtieXbW5rBHb8qdsVT6mDvZXwp/SecmiToV+8j0ifiuHbMNj+Ia5 X-Received: by 2002:a05:6a00:2fd1:b0:6db:d93e:5cdb with SMTP id fn17-20020a056a002fd100b006dbd93e5cdbmr8574017pfb.15.1708244348724; Sun, 18 Feb 2024 00:19:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708244348; cv=pass; d=google.com; s=arc-20160816; b=Gw/yuy0wAAQKj9fDAxB62O/00v39gGMQADNdWvRLP2cQ1QlxMGNA4pj0A57Y1rRE9K kGwZe1jFXbH/haIHsgI/m7OxXULdAvW7pXJB0bBxdgdlGnTMUqwk4Lr9ra9MsVDl9sLu pB4cLS4HtBGtSM62mgWCgiHWmNTtOVZ6yCuxU5Si3SZJ6dS7eM7cJHoNd8szZ+U5NxBZ /vYC3UTh+mIw+kqqIK2JEccwPH90fZa4xndKOgn6cVOgdNuHhWdXJ6nBLnF/bpt5XKfs lSbsBBsBbYIlIUbPvWCZ1Ui4FzgilfGlvyN734vEyV11ifKU/0LB4Nbn7Hwp0HSMvYOX WXDA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=ZcHFIGyKLdwUT9TX+J3Ch03qGYgeW7Ho2TR/Fq1WlhM=; fh=c/0DG8ZjWOGj3xa2WSPqExKKwAhUSsqk5MlZk+0ao+k=; b=YVZZICMDeXgkZC0t4H3gj2WL7GeKc98SAoTWBdHQylrU66W4joBJ3yKHeBQLaK/pGK 4+l6Y8Owl/f7+FJEzK1KwVa26dAYqHjPoJnpNd8MUVaHEo/1W0YvmFGQvJruOT21dXGG lKwbpG5UBj9XMZphPlL7HUekokJhSL7uK2Zo8KhcDZBhkjf8OzKfjg+nJK+fEHzpJMNM Ejw4kDBebyMuF3+67easUEZlkl/bd9HP62+Xc4boHcmJXrwlarCuooTmTHIaEP6Ot7SS pneTEIa3y/rCnM5cl/b7JPOFTeNilnDafPoBGELAfIcPxMyOnIM8wF2ORda+r2Xw/VRd JvCQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=remlab.net); spf=pass (google.com: domain of linux-kernel+bounces-70285-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70285-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id h22-20020aa786d6000000b006e2861bc3f5si2199271pfo.38.2024.02.18.00.19.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 00:19:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70285-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=remlab.net); spf=pass (google.com: domain of linux-kernel+bounces-70285-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70285-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 0C624B215DB for ; Sun, 18 Feb 2024 08:19:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8B60E16416; Sun, 18 Feb 2024 08:18:43 +0000 (UTC) Received: from ursule.remlab.net (vps-a2bccee9.vps.ovh.net [51.75.19.47]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A7B5EF4EB; Sun, 18 Feb 2024 08:18:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=51.75.19.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708244323; cv=none; b=STg3jJUU3uQA9FNfYMnx8rti2k8OquUZ+utQH9k4X2JnaMVmQTeo2QmP9Ug7WDWW/gw953pVVjM4P297GEk7RnO7YZvFA/pOL0tUVYJMwkqMMM5aNde1UJ203LYjQEOp/drChIjKFi95tngQMs2fz4w+z81SW662dS4aijytwVE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708244323; c=relaxed/simple; bh=N4ufLhqUZMGni7ljag9cL2yFnI4/1wwaxQxPHYTQSYg=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=eXDEDxV4XLamsp0HTUQax32SYrIgsRPi1bQBIe86igz43JMsZx9k50NrhLw2A//SLODTwxUXmnMOOQRy3zkZH+CHYBlpIAx5E4ajnpCi/F5tuXflu5pHJTfSSpW/QuAkOfWyqTyPfpiY8HwZz4fnhWD1l6qQ/yleWjw+rN0Yd2Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=remlab.net; spf=pass smtp.mailfrom=remlab.net; arc=none smtp.client-ip=51.75.19.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=remlab.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=remlab.net Received: from basile.remlab.net (localhost [IPv6:::1]) by ursule.remlab.net (Postfix) with ESMTP id 28DD2C009A; Sun, 18 Feb 2024 10:12:15 +0200 (EET) From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= To: courmisch@gmail.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCHv2 2/2] phonet/pep: fix racy skb_queue_empty() use Date: Sun, 18 Feb 2024 10:12:14 +0200 Message-ID: <20240218081214.4806-2-remi@remlab.net> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Rémi Denis-Courmont The receive queues are protected by their respective spin-lock, not the socket lock. This could lead to skb_peek() unexpectedly returning NULL or a pointer to an already dequeued socket buffer. Fixes: 9641458d3ec4 ("Phonet: Pipe End Point for Phonet Pipes protocol") Signed-off-by: Rémi Denis-Courmont --- net/phonet/pep.c | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/net/phonet/pep.c b/net/phonet/pep.c index faba31f2eff2..3dd5f52bc1b5 100644 --- a/net/phonet/pep.c +++ b/net/phonet/pep.c @@ -917,6 +917,37 @@ static int pep_sock_enable(struct sock *sk, struct sockaddr *addr, int len) return 0; } +static unsigned int pep_first_packet_length(struct sock *sk) +{ + struct pep_sock *pn = pep_sk(sk); + struct sk_buff_head *q; + struct sk_buff *skb; + unsigned int len = 0; + bool found = false; + + if (sock_flag(sk, SOCK_URGINLINE)) { + q = &pn->ctrlreq_queue; + spin_lock_bh(&q->lock); + skb = skb_peek(q); + if (skb) { + len = skb->len; + found = true; + } + spin_unlock_bh(&q->lock); + } + + if (likely(!found)) { + q = &sk->sk_receive_queue; + spin_lock_bh(&q->lock); + skb = skb_peek(q); + if (skb) + len = skb->len; + spin_unlock_bh(&q->lock); + } + + return len; +} + static int pep_ioctl(struct sock *sk, int cmd, int *karg) { struct pep_sock *pn = pep_sk(sk); @@ -929,15 +960,7 @@ static int pep_ioctl(struct sock *sk, int cmd, int *karg) break; } - lock_sock(sk); - if (sock_flag(sk, SOCK_URGINLINE) && - !skb_queue_empty(&pn->ctrlreq_queue)) - *karg = skb_peek(&pn->ctrlreq_queue)->len; - else if (!skb_queue_empty(&sk->sk_receive_queue)) - *karg = skb_peek(&sk->sk_receive_queue)->len; - else - *karg = 0; - release_sock(sk); + *karg = pep_first_packet_length(sk); ret = 0; break; -- 2.43.0