From patchwork Wed Oct 19 08:28:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 5530 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp337680wrs; Wed, 19 Oct 2022 06:49:59 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7piAUutvaep/3hBzOh3vkVxSpHjqybRx6JeoKB1zjF5wqoMjdZbVy1gb+n3BS12MGoi1a0 X-Received: by 2002:a17:90b:1184:b0:20c:c0f:7c43 with SMTP id gk4-20020a17090b118400b0020c0c0f7c43mr46037319pjb.236.1666187399546; Wed, 19 Oct 2022 06:49:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666187399; cv=none; d=google.com; s=arc-20160816; b=MxOURxpWMWx7Wl0xLD7DU1u0fdzrk2kxwicTaCfjSnTZtSZRcjUwzF4JqeW1Wj9rYH c8EimTn6SYxZm9JDA9QLZGJZTEgkI9oBRve0BF/Ba08RzpMuUHfOtOTDdvW4u5PgLa+v uMmad4yr1k+qitRtENsqrv2aDJnBvvV5aXMgdYwWUF1poPq8mtFTLEf1goWj1rYptjKg C7xC7PGulR0LJbzeOGyOHBXc+NNHiVmBjL5SIeJqaaRJWnFHzIRE7dSEyrdRcdW5A+D+ 5Q9MHK2/2Qpzt0P2U694iqxVJccKb+RE9EpCl18OI4KI6mjzhB8ZO/dpot98JD4SMLNM rpuA== 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=6cXHkunxXmRYDzQya4ffmvx8btfUedOsKu1sSQVHKqA=; b=R+KjH1ClKXRUTs9x2BLmCVzWrJ/sejS0eu8WjFotp1g0n7ewRocWWUA3I0FFz8FCwA 0pIEOjJEM91Ypt8pnMsSX6u9XXi4Tbsn1/GaPlo1/17rXLU81RDdNqS7d17E+yyIJ5gk 8XJUgDwo62WaRFrn9xTrGMdyHmSmQnnGtyOi4dCATcAgK1LlbtSwlkO/WwOJ4xsAmi77 o4DgOOFs8J83YS3bzKATbbjpjvkut1+gdycQwbpvL5oN1Zo//+9F5u3l4CHzOsOpTbz/ 52UpIIBCNtO97PTYxeUIbPXDZzopR/GB09njd8khJlRJP1EXsiJocuftTZS8iwh4sB6T /hNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=be11nvRY; 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 w2-20020a170902904200b00183a85a9b54si17221682plz.279.2022.10.19.06.49.46; Wed, 19 Oct 2022 06:49:59 -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=be11nvRY; 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 S233564AbiJSNsw (ORCPT + 99 others); Wed, 19 Oct 2022 09:48:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233492AbiJSNrT (ORCPT ); Wed, 19 Oct 2022 09:47:19 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F85C172503; Wed, 19 Oct 2022 06:32:30 -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 ams.source.kernel.org (Postfix) with ESMTPS id 9DE8DB82435; Wed, 19 Oct 2022 08:58:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1897CC433C1; Wed, 19 Oct 2022 08:58:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666169901; bh=436goTtGP7cjA/vtXH88LMq5NRFPoKFfaJ/IZ4MBkUI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=be11nvRYUcNMJPsB9UbrpBGdmgfmxhCb441+vyBSU9vxoM2gIK8TYuZpa9uQoEYO2 wSxG0oeukW9iFp8X1TILnYRLDJoUpX/JgJySYZ24cHj+SOmLhxVnvd8pSsM22xlyvT ZQ2Nh0jStRNB4tmeimFdkI1XCGolbFFs4dLddFW8= 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 6.0 418/862] locks: fix TOCTOU race when granting write lease Date: Wed, 19 Oct 2022 10:28:25 +0200 Message-Id: <20221019083308.434615073@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.4 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?1747124118753199982?= X-GMAIL-MSGID: =?utf-8?q?1747124118753199982?= 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 99c6796c9f28..dd88701e54a9 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -324,12 +324,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 cf7e5c350a54..a81319b6177f 100644 --- a/fs/open.c +++ b/fs/open.c @@ -842,7 +842,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; @@ -882,8 +884,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; @@ -937,10 +937,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;