From patchwork Sat Feb 10 12:50:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?R=C3=A9mi_Denis-Courmont?= X-Patchwork-Id: 199259 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp1450205dyd; Sat, 10 Feb 2024 04:57:12 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX8x6TtGbhRLpUEW5nUrXLIpFhXZSf7aBfy4+5H1YF9872CEnT45XUwAkr912/ORt083OMzEgmHhcNES7EASffyRKj16A== X-Google-Smtp-Source: AGHT+IFs8OiJf/bLb8CUrcrOkVuKu8gUX73pd6zfhdy+558zawRrod0rWxpOFL2N51Qo/VXydP4K X-Received: by 2002:a05:6512:b95:b0:511:47ed:d744 with SMTP id b21-20020a0565120b9500b0051147edd744mr1495432lfv.12.1707569832468; Sat, 10 Feb 2024 04:57:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707569832; cv=pass; d=google.com; s=arc-20160816; b=IGISVhXhtEPENOB1rOB9BBXEMcGnBTqps7V62fxDz7A3bQo7icj3kV7Y50/SPaGy3Y 01e9QI8LpbyQYPn8aNxXmhrVjFoKg83Cw9tKJSdVz6Q2DrQcBNaO5Or8C/0nn4I5tboh IRhCNBhL8cdMEHwy8iECek418y5ZiQ2M1tg/qqbW6il/SRUIDUd/sqzNh0ZxLG4gebiZ ZfJILKAgPRicZez6thRxgGXlX51iBsZMU8j0j+SVdZ9nQiXMo8z0Xx9u00K9pIdMS8Up w+Rq6V+x28UaoXVFQEp5a7Biuzz8aF0Z614/whCg8ULQj5nfn1VcgJ75wKrLNf3V75XP 7iUQ== 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:references:in-reply-to:message-id :date:subject:cc:to:from; bh=Ov7Y3oY8cyxQ1E1EHVJeUDE59OPvmeEO3SeeBKUwYuc=; fh=aZQLjNHuzFg0PUR+nwxoHD5zoEhNCU7w8jTBknkd1fY=; b=bSzShLjoC4/lV3fbqVL0tjvlelVNv2+ALG/loVCRJnvIyPE6bXPHtuhnjfBN4UhiGe OpTW4kVopNOKKBqJNhKmuEWFjyB73D5TEu8khIlsNpcoArpgpwg6zdUgPnk9yqOPSLaS +h5qXrTHnzABWhXJiuLMw71pnHovl9vOxRf0t9GuG2ZSdrj4KY29NqRJu58/8cLSaAY6 TfnRWbo1vkepEt4aub+rHMo7xOjdHp0KfLWVgdLE4OPb0+81oSark14sbbeWm1pgjojT kfynmm9s440/Gwjy5P1lJgj5QHdS0ZzYjA0C+s7sn7b0j7H+LmaME4EDkKqJjIuuOifV Is/w==; 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-60404-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60404-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=2; AJvYcCX7RyCB8UAGNhc6OaObN8YYx3kTdXKOWj5OYk1R92VLqN6azykrxh/BTjszoOu0XifCWCgA1siyxt1LF6xGqsLx12GV/w== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id ho20-20020a1709070e9400b00a389f3138e5si1955730ejc.975.2024.02.10.04.57.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Feb 2024 04:57:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60404-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=remlab.net); spf=pass (google.com: domain of linux-kernel+bounces-60404-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60404-ouuuleilei=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 174E71F2260E for ; Sat, 10 Feb 2024 12:57:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 363E84C637; Sat, 10 Feb 2024 12:56:45 +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 7332E47F42; Sat, 10 Feb 2024 12:56:41 +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=1707569803; cv=none; b=t6SM8mmSo97pfDmtZYo25By1pRjV1ANsDgCq9QOJpbbO/fWYpcPcwVLczjVxxzFlM3ROxUoxCPdFIzvQzgdJpOHo8bUmRQUY+acOCp88ZAkpZZw5xoejKrUgMBAE8bNW1o2Pfw+mp1daNMPrAdQFR/E1BLX1oT1DbiIfRTrWUxs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707569803; c=relaxed/simple; bh=jTamXYQSY1bQylvKzgcgdRQfmjJ8/TU+GEYdw1q4oMw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Q4u2e4akCFqqBLax7i7QxT6EfOPqPydXM7MazLa6FKXlbSRB8frPKk3m2ogDYw9nho7dMs8eNo6vPzVQ2BoF6Ei1rxSx6wwKWlVNPwv35hTEU1R2XCTJTQ8Y3kS9qnGtMNf8FHf8/HPFNsV3MKzxInritAJCUStVSTCnaTPan0c= 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 C1E2AC022E; Sat, 10 Feb 2024 14:50:54 +0200 (EET) From: =?utf-8?q?R=C3=A9mi_Denis-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: [PATCH 2/2] phonet/pep: fix racy skb_queue_empty() use Date: Sat, 10 Feb 2024 14:50:54 +0200 Message-ID: <20240210125054.71391-2-remi@remlab.net> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240210125054.71391-1-remi@remlab.net> References: <20240210125054.71391-1-remi@remlab.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790516744535952052 X-GMAIL-MSGID: 1790516744535952052 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. 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;