Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp3879511rwi; Sat, 22 Oct 2022 01:26:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4FlNLPBcH/OY76uKoZeIvOeG3r5gZq0Ni6LHagNYaTF0sBcw1l3uQyj2OjFqaDyav1JYh1 X-Received: by 2002:a17:906:9b8b:b0:78d:8276:ff47 with SMTP id dd11-20020a1709069b8b00b0078d8276ff47mr19365814ejc.430.1666427210811; Sat, 22 Oct 2022 01:26:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666427210; cv=none; d=google.com; s=arc-20160816; b=HjAHaWWiY6lB/irBg1GixZb/TSBJ2xA5oo4WtaLFXXennDQY9mmLwjgwzTvkBB9Qru wRO5V76fj5Z0HFrajdFrISc7x1AWxVZYIvjuPtVQ1vOqQnei3DKvTYJUFcKqc/Ve62I3 nYHS7PEcrncQipyc9BhQjd5/1+FODIg+nLejUq7Dao3wtBBYEW8fhvjMHkvpaFeiRKHO hgc4VewAOJTNst2SytoC6A5zvEoGnuK0EvuLLtLySUPZoWv64SJnRbIeT6Tyyt4KDpsc rQg6mVeTq5vv5yGtNuvuZakQn8RKgXmH67OcGvcxHucMVFBJcNkDMNu1M6aCcjF1Mi+b RR5w== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p2-20020aa7d302000000b004599fc7f310si17944209edq.140.2022.10.22.01.26.25; Sat, 22 Oct 2022 01:26:50 -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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 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; -- 2.35.1