From patchwork Mon Oct 24 11:29:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 9355 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp467367wru; Mon, 24 Oct 2022 06:57:16 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5jCvzODJIWLmY75XKLYzQRej4+/zbEbx+hjgNr7rybKF8eHbHxpvW5+IRo2ocTPI5tUJq1 X-Received: by 2002:a17:90b:1d11:b0:20d:4c69:6886 with SMTP id on17-20020a17090b1d1100b0020d4c696886mr72972340pjb.14.1666619835822; Mon, 24 Oct 2022 06:57:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666619835; cv=none; d=google.com; s=arc-20160816; b=V2BPZ05HrOLP3g3xWKPmz0eM4893jqcZHaqN30eyQXaNqjAZg6sxEp+//4DyRliXZ4 Rw0sk8z4/sFa6ekTzjIsBFxwa/B66awquggMpS6GgCEhDG5qKrnMObpGfHdHNwwJVDRZ X/lhXuPd9tQW8c4txaOUR82LSfoZzqBPAHIWGgT9LbTk46NUQ+S445t3Bz3XRqzV9+sI QqK2/wPjWy9IVHY2hXxlMNvo+sYvmor2brBTqHE9aoMM/5eZ0ZRsSFTMpxfxHcnByy4n LZp2hjJmJM9dcIvBVo6VUG1PbYAAWoWNVv2WYpLBy2a+bVhxFteIYlhArrp4u98DXsDG yepA== 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=w8Dmo5YlqXmOVETNil9AdZ/WHnmEZrz4uXdnFaT6F24=; b=ahjf6MJqnXeKuO1ELrj9NdAb1MxibOrj1GMMZyv3xPU7GYVrw4bZBFIoacaVhW0/NO t6oosdumBz21Uz4HC+XZcNz5SkDTs3phOeIEU+PrcuN6pJTYfdb7d2dceHuaQCJcDaqK tUvQCfpTfYMt+EEo9oBNwJc6Fvub0E4VZ/2yWqZ0hYXRP5wgGRO+Rxi7xmU/YZpJStvP aqupbx/wqr80VUs9cIE70x+J2cjbjpMGA8yHjjbcI9rMCMDUL3Pkst96x7biyGnk8a17 05GLHdJrI0dewd8N16CahuJt+tJarRghiLQVrkMNeFP/CQ5uHugOEvkAehSWkMrqNCX0 eLNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fuJybRC8; 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 w7-20020a056a0014c700b0056befaa9593si2404979pfu.317.2022.10.24.06.57.02; Mon, 24 Oct 2022 06:57:15 -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=fuJybRC8; 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 S236829AbiJXN43 (ORCPT + 99 others); Mon, 24 Oct 2022 09:56:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236839AbiJXNyT (ORCPT ); Mon, 24 Oct 2022 09:54:19 -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 12765BD067; Mon, 24 Oct 2022 05:43:56 -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 E1185612B9; Mon, 24 Oct 2022 12:43:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 02977C433D6; Mon, 24 Oct 2022 12:43:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666615434; bh=pDb1qSlX5k+7QmuGyJdhWjYpgsjs5GTh4TeBNyLE3+E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fuJybRC8HsvBSM0VtV59Fu5ffAL+n7teogp5cXfwUT3stiaH1o360rWg4FY8uPXlI rRjrXA52J1biv0Uk1Xg22RMKbAiTZOCSM59HcBNqhqskfMVSO5QHkuWJShEYsajdF4 S3OzfwyyyyqWZz//z9wtyLOWO8BeQsmBGp77nIcw= 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.15 249/530] locks: fix TOCTOU race when granting write lease Date: Mon, 24 Oct 2022 13:29:53 +0200 Message-Id: <20221024113056.381976118@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113044.976326639@linuxfoundation.org> References: <20221024113044.976326639@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.6 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?1747577560671312476?= X-GMAIL-MSGID: =?utf-8?q?1747577560671312476?= 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 e8c9016703ad..6f297f9782fc 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -284,12 +284,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 4f1fe6d08866..9075490f21a6 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -100,6 +100,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 1ba1d2ab2ef0..5e322f188e83 100644 --- a/fs/open.c +++ b/fs/open.c @@ -786,7 +786,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; @@ -826,8 +828,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; @@ -880,10 +880,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;