Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2827290pxb; Mon, 1 Nov 2021 02:38:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx0vkjuCswNSCV6r4pDZAv5gzoufQFye4K1Fl6ZhtZQWTKkSlAwepALlSa3evJPWsQCMbuC X-Received: by 2002:a02:cb58:: with SMTP id k24mr21192688jap.59.1635759536651; Mon, 01 Nov 2021 02:38:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635759536; cv=none; d=google.com; s=arc-20160816; b=v67ZVo7Gubv+ZYBD7+PyxFUjNmBWrdNakMHrlBLOMJjMYOXIqZERbdu0OgKOaioJh4 050/GcsqgLUxg2p+J+8KsczUzJE8pJbALBjuPAEwMDvYMaGupfG0Uvd2zy7huOh7x18I un4ngioTtN/Vvq1om3NzzqXnMjiisXcAYwdrhSZtRUAvRdjO1mZKwP+uErOTdG5foVUe LZDBxgokZwBl1+MiE55x12SlEAFWRtVZyx0G+15t0V9Zn4N90qO75/aB7kqL8R8VqJsy n2hSi/KdJh/8tmkCP+AJwnAzfIeS4BSbZ6hs5fnD3wb+HfF7vDEYlZms/cRsJyKA9eqF 9y0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=v4ykm22hJvTr4GST45KpmW25RJMwoD0YMLHpXyGSVxk=; b=qKhFWjqEjvUxSjBsGp5y0px5E5KMOl31BQGhs62NgBeB2lMpVWB5A5YQuuOwi7Rj/J b98IrH7InY8EmDX51D3v5ovfeGGBYaSuY84SeTJBsXilEQxHIyet43OaanAUHrRz12hy d3w3yONtcjcUl0Bc+MSN2ZNAzYPh1u7tfwxy4jGj3QawuM9Q4z8F1ERkaKas6FtlWkQs qweyutBdos+nRIQv7i+8n8ChEfljwbaN3X9TFe4uJV/3zzncLqrg7/06Zg3MOFg51qIc WHsiKSXXfRgtVFEepHamCVzIyiWDn4pjuwv+y68ZKFx8mjoFNj/LcQvBfplrzzbN0Ser QLWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=IAzUG9fx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i1si772805iow.0.2021.11.01.02.38.45; Mon, 01 Nov 2021 02:38:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=IAzUG9fx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232556AbhKAJkO (ORCPT + 99 others); Mon, 1 Nov 2021 05:40:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:43604 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232620AbhKAJhs (ORCPT ); Mon, 1 Nov 2021 05:37:48 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4F5D96128A; Mon, 1 Nov 2021 09:26:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1635758809; bh=RRqlQEZ47fdGBwq+opIEfcwMmp66tYLJ3Yj1NnoxAe0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IAzUG9fxCZOGpW12Nqoetg2tJ69Uk/0GIgtGtu3DkQH+uWgO5UtuYoPFTCsX05P1G PggK9cMp0VUcAgcyX7v5rZvej26qyJ8c9+dRY1RMwoYsQW0uVzY5xDZHkxNrPDVbro rUKBH+i7v9faAOh8/83UST0FfjdM1LIYZ6aHB3SQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Liu Jian , Alexei Starovoitov , John Fastabend Subject: [PATCH 5.10 36/77] tcp_bpf: Fix one concurrency problem in the tcp_bpf_send_verdict function Date: Mon, 1 Nov 2021 10:17:24 +0100 Message-Id: <20211101082519.419310564@linuxfoundation.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211101082511.254155853@linuxfoundation.org> References: <20211101082511.254155853@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Liu Jian commit cd9733f5d75c94a32544d6ce5be47e14194cf137 upstream. With two Msgs, msgA and msgB and a user doing nonblocking sendmsg calls (or multiple cores) on a single socket 'sk' we could get the following flow. msgA, sk msgB, sk ----------- --------------- tcp_bpf_sendmsg() lock(sk) psock = sk->psock tcp_bpf_sendmsg() lock(sk) ... blocking tcp_bpf_send_verdict if (psock->eval == NONE) psock->eval = sk_psock_msg_verdict .. < handle SK_REDIRECT case > release_sock(sk) < lock dropped so grab here > ret = tcp_bpf_sendmsg_redir psock = sk->psock tcp_bpf_send_verdict lock_sock(sk) ... blocking on B if (psock->eval == NONE) <- boom. psock->eval will have msgA state The problem here is we dropped the lock on msgA and grabbed it with msgB. Now we have old state in psock and importantly psock->eval has not been cleared. So msgB will run whatever action was done on A and the verdict program may never see it. Fixes: 604326b41a6fb ("bpf, sockmap: convert to generic sk_msg interface") Signed-off-by: Liu Jian Signed-off-by: Alexei Starovoitov Acked-by: John Fastabend Link: https://lore.kernel.org/bpf/20211012052019.184398-1-liujian56@huawei.com Signed-off-by: Greg Kroah-Hartman --- net/ipv4/tcp_bpf.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) --- a/net/ipv4/tcp_bpf.c +++ b/net/ipv4/tcp_bpf.c @@ -317,6 +317,7 @@ static int tcp_bpf_send_verdict(struct s bool cork = false, enospc = sk_msg_full(msg); struct sock *sk_redir; u32 tosend, delta = 0; + u32 eval = __SK_NONE; int ret; more_data: @@ -360,13 +361,24 @@ more_data: case __SK_REDIRECT: sk_redir = psock->sk_redir; sk_msg_apply_bytes(psock, tosend); + if (!psock->apply_bytes) { + /* Clean up before releasing the sock lock. */ + eval = psock->eval; + psock->eval = __SK_NONE; + psock->sk_redir = NULL; + } if (psock->cork) { cork = true; psock->cork = NULL; } sk_msg_return(sk, msg, tosend); release_sock(sk); + ret = tcp_bpf_sendmsg_redir(sk_redir, msg, tosend, flags); + + if (eval == __SK_REDIRECT) + sock_put(sk_redir); + lock_sock(sk); if (unlikely(ret < 0)) { int free = sk_msg_free_nocharge(sk, msg);