From patchwork Sat Oct 22 07:23:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 7395 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp1097055wrr; Sat, 22 Oct 2022 01:06:41 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4P1vajVyIEqv7bB/urlysIdYBtRrIlRQJpWH7BkSiZkc9HXVtAHfzYTr83dU3E/jwi73KO X-Received: by 2002:a17:90b:3e82:b0:20d:2fa1:d071 with SMTP id rj2-20020a17090b3e8200b0020d2fa1d071mr62481192pjb.58.1666426001286; Sat, 22 Oct 2022 01:06:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666426001; cv=none; d=google.com; s=arc-20160816; b=OtZoSvbn9gl0L0h4T4YggRNWn+qmGrCvLR+wWA2Cdm9G7Uozw+/IDqb5OV2hfk4Rjn 64NQPMblTMLlAokps609/Dsav7Oj8QjxeFZjdmNZs1zsdgkoA1pj2Xaw+I2dpUVo2g21 J+zeuSuAjfWZjWItRYzzsoTSKy6UYQPaFXBpDJze7Gc/h75W0VSQbomOCvSySBfP0BFN 3k+abnhUGxdWhjp6oSXfjSiAuKiH9mvWgkfGQKMzAx/LrZQr0NzYClBkfLbvE33UZf4C b/Of/2LnT4eIP2meIMmKDwd6eX2puYD5VnYWiOxWGzhwqJagtHRdpDvc5LumUZv4zrME KSLw== 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=EE4IxnV8LsII/VS2kG4DSx6NzDPbom4LFJ2hAI1GLFU=; b=CFYiT7y6yOkataF8zmQq3DzcVwTrGV9pxqagBZc1GhuG6/Kl4uThKmScIfQbSPPiHg BuAPvLpAiE+SfjexCzNXNy1mL4h8Jizm+v5zDR1V9J3LT+qSY8hDdkQKmdrcHoaXwNJk bd61h9uRbBV7EK4bFxBPCtcWURKO7b8VzFCmN1bTDfUAafTLdGj7fziaNxN3JgpN+23Z Wc20dk9zN0j8Cr7SGiW+PgVcgagmjGkHrS9LeVS/GKQI1p0EL3AncAlD4sS3jw2vQm6/ wydymSBa2wwgQJcj9guUXJA9WgQ7XnFJn8yXPcZhkDpfwaUOBh5BQ6/i4IAAOk4GaqjZ yILg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BzAtGnuz; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n11-20020a634d4b000000b004386d70cf15si25776930pgl.206.2022.10.22.01.06.27; Sat, 22 Oct 2022 01:06:41 -0700 (PDT) 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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BzAtGnuz; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232776AbiJVIGB (ORCPT + 99 others); Sat, 22 Oct 2022 04:06:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232708AbiJVH7m (ORCPT ); Sat, 22 Oct 2022 03:59:42 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF5DAFC1C9; Sat, 22 Oct 2022 00:49:59 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 3E08860B28; Sat, 22 Oct 2022 07:49:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54AD5C433D7; Sat, 22 Oct 2022 07:49:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666424990; bh=j2FU4rD3Ftcr09PDebDiLe2go+wH3L/m5kM1bnd19uA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BzAtGnuzlIQyGHfBz45n/WzXYi86oF4ahJjjqZT7PFy9EQ8CQWCpzNuYmBVHSGSUY KG7HHCpOITFqbsb6t+bU5lBvQWowsN0RDdQS9wX8WrnK8zWN07H0QwaaW/3T0XCn9Y ExXB9JEUlWpLQGhewGPsCeTxY6TNoG9ppRFPTgEA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jeff Layton , Amir Goldstein , Al Viro , Sasha Levin Subject: [PATCH 5.19 351/717] locks: fix TOCTOU race when granting write lease Date: Sat, 22 Oct 2022 09:23:50 +0200 Message-Id: <20221022072511.297825603@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221022072415.034382448@linuxfoundation.org> References: <20221022072415.034382448@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747374310760023152?= X-GMAIL-MSGID: =?utf-8?q?1747374310760023152?= From: Amir Goldstein [ Upstream commit d6da19c9cace63290ccfccb1fc35151ffefc0bec ] Thread A trying to acquire a write lease checks the value of i_readcount and i_writecount in check_conflicting_open() to verify that its own fd is the only fd referencing the file. Thread B trying to open the file for read will call break_lease() in do_dentry_open() before incrementing i_readcount, which leaves a small window where thread A can acquire the write lease and then thread B completes the open of the file for read without breaking the write lease that was acquired by thread A. Fix this race by incrementing i_readcount before checking for existing leases, same as the case with i_writecount. Use a helper put_file_access() to decrement i_readcount or i_writecount in do_dentry_open() and __fput(). Fixes: 387e3746d01c ("locks: eliminate false positive conflicts for write lease") Reviewed-by: Jeff Layton Signed-off-by: Amir Goldstein Signed-off-by: Al Viro Signed-off-by: Sasha Levin --- fs/file_table.c | 7 +------ fs/internal.h | 10 ++++++++++ fs/open.c | 11 ++++------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/fs/file_table.c b/fs/file_table.c index 5424e3a8df5f..543a501b0247 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -321,12 +321,7 @@ static void __fput(struct file *file) } fops_put(file->f_op); put_pid(file->f_owner.pid); - if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) - i_readcount_dec(inode); - if (mode & FMODE_WRITER) { - put_write_access(inode); - __mnt_drop_write(mnt); - } + put_file_access(file); dput(dentry); if (unlikely(mode & FMODE_NEED_UNMOUNT)) dissolve_on_fput(mnt); diff --git a/fs/internal.h b/fs/internal.h index 3e206d3e317c..4372d67a3753 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -102,6 +102,16 @@ extern void chroot_fs_refs(const struct path *, const struct path *); extern struct file *alloc_empty_file(int, const struct cred *); extern struct file *alloc_empty_file_noaccount(int, const struct cred *); +static inline void put_file_access(struct file *file) +{ + if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) { + i_readcount_dec(file->f_inode); + } else if (file->f_mode & FMODE_WRITER) { + put_write_access(file->f_inode); + __mnt_drop_write(file->f_path.mnt); + } +} + /* * super.c */ diff --git a/fs/open.c b/fs/open.c index 1d57fbde2feb..5874258b54bd 100644 --- a/fs/open.c +++ b/fs/open.c @@ -810,7 +810,9 @@ static int do_dentry_open(struct file *f, return 0; } - if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) { + if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) { + i_readcount_inc(inode); + } else if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) { error = get_write_access(inode); if (unlikely(error)) goto cleanup_file; @@ -850,8 +852,6 @@ static int do_dentry_open(struct file *f, goto cleanup_all; } f->f_mode |= FMODE_OPENED; - if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) - i_readcount_inc(inode); if ((f->f_mode & FMODE_READ) && likely(f->f_op->read || f->f_op->read_iter)) f->f_mode |= FMODE_CAN_READ; @@ -902,10 +902,7 @@ static int do_dentry_open(struct file *f, if (WARN_ON_ONCE(error > 0)) error = -EINVAL; fops_put(f->f_op); - if (f->f_mode & FMODE_WRITER) { - put_write_access(inode); - __mnt_drop_write(f->f_path.mnt); - } + put_file_access(f); cleanup_file: path_put(&f->f_path); f->f_path.mnt = NULL;