From patchwork Fri Oct 4 19:36:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 13822891 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2B4D4CF8857 for ; Fri, 4 Oct 2024 19:37:16 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1swo70-0004At-Vb; Fri, 04 Oct 2024 19:37:15 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1swo6z-0004Ak-Pf for linux-f2fs-devel@lists.sourceforge.net; Fri, 04 Oct 2024 19:37:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:Message-ID: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=t5yMOtY153RwY6h4t7sWRvT7dt3L5Ez1ktDDmxdrtnU=; b=VugOER40DTcUmpoegH8YMU4oAz fo8IjG3wEd2jHhDfWqpBC3N/odWwq+QV4T9gsaYCHzhzMc1kxUrHJrUriR2ZpvINQCnMpl3a1vcz2 4oKvdsWHPRCdsddAM2hTldyPDOoxmolK+u5+O1xXwjdyOK2YgAVyt3C0dyvKErv0UUFQ=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject:Cc:To:From :Sender:Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post: List-Owner:List-Archive; bh=t5yMOtY153RwY6h4t7sWRvT7dt3L5Ez1ktDDmxdrtnU=; b=h BWuhtCMJrnAcnSKjKHLHnbCBcbn75sWob0IsaUZgLF0hUD1paiFQ8hHArsxF6nSjfjptngOAIujS9 UVNnFsXRAef5hoGJosY7EcuaGmdG/sDTCmMpbu+5tLnnfBaWEGd/Tff7zkGbXfBD1TdLkzVmDutVX V+lL/tTlS2N6gZPQ=; Received: from nyc.source.kernel.org ([147.75.193.91]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1swo6z-0001ql-M7 for linux-f2fs-devel@lists.sourceforge.net; Fri, 04 Oct 2024 19:37:14 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 7F308A44D31; Fri, 4 Oct 2024 19:36:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6C4FBC4CEC6; Fri, 4 Oct 2024 19:37:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728070627; bh=dkL3L2bqK29d84C7yW4lPON2QKH/kQS0VZta8hKzgfM=; h=From:To:Cc:Subject:Date:From; b=tlXraFbuBzhZEyjFeYK5D2J95Tasue1LDgR6yUKUWcg6iMWV4I3TgZ42EqZtA/Wh2 rsDbL2wBAdZXFkpc4QapIfLeXyeL308yJsZTye5yO/lA6wRZv9LdhYukMGprngkStU lLac+Or6foFhb5BBzchT1eaXs/XZDK2OAPt9OmjejAoj6osoI0Sm/ANaIE6rMHpqcJ qNdrF93mW6/pFQEJYK441BNHH/bz/nv9aId/jApDplepoC9HHR/wXv3oK7YGxR8gHi Mpe3Z5ztfXaejg0Fgxe5io7IiGYLsJe5/qVJIT5H+txDwy/1Ea13pR+YNRovR94IFl H0dIL4LVtzSzA== To: stable@vger.kernel.org Date: Fri, 4 Oct 2024 19:36:43 +0000 Message-ID: <20241004193643.190072-1-ebiggers@kernel.org> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog MIME-Version: 1.0 X-Headers-End: 1swo6z-0001ql-M7 Subject: [f2fs-dev] [PATCH 5.10] f2fs: Require FMODE_WRITE for atomic write ioctls X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Eric Biggers via Linux-f2fs-devel From: Eric Biggers Reply-To: Eric Biggers Cc: Jaegeuk Kim , Jann Horn , linux-f2fs-devel@lists.sourceforge.net Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net From: Jann Horn commit 4f5a100f87f32cb65d4bb1ad282a08c92f6f591e upstream. The F2FS ioctls for starting and committing atomic writes check for inode_owner_or_capable(), but this does not give LSMs like SELinux or Landlock an opportunity to deny the write access - if the caller's FSUID matches the inode's UID, inode_owner_or_capable() immediately returns true. There are scenarios where LSMs want to deny a process the ability to write particular files, even files that the FSUID of the process owns; but this can currently partially be bypassed using atomic write ioctls in two ways: - F2FS_IOC_START_ATOMIC_REPLACE + F2FS_IOC_COMMIT_ATOMIC_WRITE can truncate an inode to size 0 - F2FS_IOC_START_ATOMIC_WRITE + F2FS_IOC_ABORT_ATOMIC_WRITE can revert changes another process concurrently made to a file Fix it by requiring FMODE_WRITE for these operations, just like for F2FS_IOC_MOVE_RANGE. Since any legitimate caller should only be using these ioctls when intending to write into the file, that seems unlikely to break anything. Fixes: 88b88a667971 ("f2fs: support atomic writes") Cc: stable@vger.kernel.org Signed-off-by: Jann Horn Reviewed-by: Chao Yu Reviewed-by: Eric Biggers Signed-off-by: Jaegeuk Kim Signed-off-by: Eric Biggers --- fs/f2fs/file.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) base-commit: ceb091e2c4ccf93b1ee0e0e8a202476a433784ff diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index 50514962771a1..e25788e643bbd 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -2047,10 +2047,13 @@ static int f2fs_ioc_start_atomic_write(struct file *filp) struct inode *inode = file_inode(filp); struct f2fs_inode_info *fi = F2FS_I(inode); struct f2fs_sb_info *sbi = F2FS_I_SB(inode); int ret; + if (!(filp->f_mode & FMODE_WRITE)) + return -EBADF; + if (!inode_owner_or_capable(inode)) return -EACCES; if (!S_ISREG(inode->i_mode)) return -EINVAL; @@ -2117,10 +2120,13 @@ static int f2fs_ioc_start_atomic_write(struct file *filp) static int f2fs_ioc_commit_atomic_write(struct file *filp) { struct inode *inode = file_inode(filp); int ret; + if (!(filp->f_mode & FMODE_WRITE)) + return -EBADF; + if (!inode_owner_or_capable(inode)) return -EACCES; ret = mnt_want_write_file(filp); if (ret) @@ -2159,10 +2165,13 @@ static int f2fs_ioc_commit_atomic_write(struct file *filp) static int f2fs_ioc_start_volatile_write(struct file *filp) { struct inode *inode = file_inode(filp); int ret; + if (!(filp->f_mode & FMODE_WRITE)) + return -EBADF; + if (!inode_owner_or_capable(inode)) return -EACCES; if (!S_ISREG(inode->i_mode)) return -EINVAL; @@ -2194,10 +2203,13 @@ static int f2fs_ioc_start_volatile_write(struct file *filp) static int f2fs_ioc_release_volatile_write(struct file *filp) { struct inode *inode = file_inode(filp); int ret; + if (!(filp->f_mode & FMODE_WRITE)) + return -EBADF; + if (!inode_owner_or_capable(inode)) return -EACCES; ret = mnt_want_write_file(filp); if (ret) @@ -2223,10 +2235,13 @@ static int f2fs_ioc_release_volatile_write(struct file *filp) static int f2fs_ioc_abort_volatile_write(struct file *filp) { struct inode *inode = file_inode(filp); int ret; + if (!(filp->f_mode & FMODE_WRITE)) + return -EBADF; + if (!inode_owner_or_capable(inode)) return -EACCES; ret = mnt_want_write_file(filp); if (ret)