Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1952128rwb; Thu, 17 Nov 2022 04:39:49 -0800 (PST) X-Google-Smtp-Source: AA0mqf62U8JouNS9WsMk9+dTilWDpUptEBbnngjiJ6gMC3W1XsjCWS4S67miwzhlxR0o5PaIu4eS X-Received: by 2002:a17:902:7c81:b0:186:9c32:79d9 with SMTP id y1-20020a1709027c8100b001869c3279d9mr2705793pll.47.1668688788949; Thu, 17 Nov 2022 04:39:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668688788; cv=none; d=google.com; s=arc-20160816; b=ur6+YuXQqH4PG72ba4Cq5Vt4qZ/4fhG2nakyTdNOS9pQe7zrhAg2O4HahgcWLj4XW/ KA4FjKia1L9BBpTaSx+qggIO2LRKyeXY99egvbGI1ThFzwNRIcyq3uJ/mW0Q8/8VE7wL 9M6BaaAe1MKlOs7HNh9Z28f4sIvo/RF6J4DIKu1Qu6R8XbJEQeCzY7WKNRIAFp4pjDTU XDSsiqzFOHlfJfuyrAvIJYw0rtghVnCr5tRjsjjmDKkcUBNXBUD6NozlVJzXIA0pcBTZ d06QBvccMGh2JyWbp73xbU3jwLqplhQN1Np1qAbFYiE9U5cevPqfnxx/rIs3w4aKRBji 4mCQ== 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 :message-id:date:subject:cc:to:from; bh=bMTQmK+oQqH+M7cMTyQkclxiXHToy0+OqIG+fErhHNg=; b=0ZCeFjb0nTp4VGCfhfyjWUuRrFEEpUjQS6+I1uBJs/7KB1mh1MQE5VzOoNUCUBnv0q 0kQsK1d8eK+XKVDDVaIGyvwZAYocMIvxNQArP+gcO70dm9l/E2IxIMba+YNk/aD6B709 d6ws/RS2JUs+9GHtdhl8cmGhYCD2vJv3sp5RNlXz/RmhDHKfPXafwtXVnXBqUJ4tZjut 4XgvmRBo/rZnfO2OTHEjjfs3UbyUYmhOaNvd2F3GiYDidu/H6q+38tKPQkmwhboJZnT9 yJdscrd4VEGDMWG+XYk6q/yCOYTzQfPc0Qr/fOO/whkzuQj3H7bfWmIeA5e4vPbeXj5u TdFQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e14-20020a17090301ce00b001783bc48eb2si1040728plh.4.2022.11.17.04.39.37; Thu, 17 Nov 2022 04:39:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239282AbiKQLx7 (ORCPT + 92 others); Thu, 17 Nov 2022 06:53:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239197AbiKQLxq (ORCPT ); Thu, 17 Nov 2022 06:53:46 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41011263D; Thu, 17 Nov 2022 03:53:44 -0800 (PST) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.54]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NCdWP15TzzqSTT; Thu, 17 Nov 2022 19:49:53 +0800 (CST) Received: from dggpemm500006.china.huawei.com (7.185.36.236) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 17 Nov 2022 19:53:42 +0800 Received: from thunder-town.china.huawei.com (10.174.178.55) by dggpemm500006.china.huawei.com (7.185.36.236) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 17 Nov 2022 19:53:41 +0800 From: Zhen Lei To: Alexander Viro , Matthew Wilcox , , CC: Zhen Lei , David Howells Subject: [PATCH] pipe: fix potential use-after-free in pipe_read() Date: Thu, 17 Nov 2022 19:53:23 +0800 Message-ID: <20221117115323.1718-1-thunder.leizhen@huawei.com> X-Mailer: git-send-email 2.37.3.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.174.178.55] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500006.china.huawei.com (7.185.36.236) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Accessing buf->flags after pipe_buf_release(pipe, buf) is unsafe, because the 'buf' memory maybe freed. In fact, pipe->note_loss does not need the protection of spinlock pipe->rd_wait.lock, it only needs the protection of __pipe_lock(pipe). So make the assignment of pipe->note_loss complete before releasing 'buf' to eliminate the risk. Fixes: e7d553d69cf6 ("pipe: Add notification lossage handling") Signed-off-by: Zhen Lei --- fs/pipe.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/pipe.c b/fs/pipe.c index 42c7ff41c2dba29..0f873949337ed28 100644 --- a/fs/pipe.c +++ b/fs/pipe.c @@ -321,12 +321,12 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to) } if (!buf->len) { - pipe_buf_release(pipe, buf); - spin_lock_irq(&pipe->rd_wait.lock); #ifdef CONFIG_WATCH_QUEUE if (buf->flags & PIPE_BUF_FLAG_LOSS) pipe->note_loss = true; #endif + pipe_buf_release(pipe, buf); + spin_lock_irq(&pipe->rd_wait.lock); tail++; pipe->tail = tail; spin_unlock_irq(&pipe->rd_wait.lock); -- 2.25.1