Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp3642991pxm; Tue, 1 Mar 2022 02:29:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJwX5vyM2zgu2/Q/U32J/ML+NFl1YhRlpucPQzGrh86vj5jm64mI5AnqA8EH+aAeNkyRvvyC X-Received: by 2002:aa7:cac8:0:b0:410:cc6c:6512 with SMTP id l8-20020aa7cac8000000b00410cc6c6512mr23477285edt.408.1646130569239; Tue, 01 Mar 2022 02:29:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646130569; cv=none; d=google.com; s=arc-20160816; b=opH006aaDX+7Zt8+NUR6i0kWgHfapYMSmNaJv7rfMmEgOYfpBy2ewco+GONT4MrTGU fTQ+kRsHHJIn0Hx7fkV+g4WCUPKAog7dvCoKZN+gZI51QdK+2keW/wLZd436r5SkF3af jyYuTQnZP62zdAw8mVHTknX7+5qjAlpSnFH7GCGv92IsuAvWd0O8scB0/yjkyxuq8reL Td0nZ2muZ2ddLG+w9eiNHzugNQSNII46rUjv+ec2DVqi6Bt3DCmqjHWWCJmk9VM6giXy NkcdClFV14QkXtSjywhRZAXIgnRuxoalrsgsqt9sSOdxVsbhk9+9CzheIm8g86Gy0s2E jTLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :from:references:cc:to:content-language:user-agent:mime-version:date :message-id; bh=pdkcvBWj2T1vUqUfuqEgRw1XPENczDhOFgXuv76EqNk=; b=gK31QC4t+Pxb2PxU68kTXT1Cf5yZje6lvlTUMgFtBHHOSMyyufp69EkY1HFNC/cghR 8hWePq6f/VaofiPWmavnMJx9+ym+Ih1FdFfDypzPjqE0weKdpSDr1vw8YZJHhaL84K1p Wn/85uRCMgf9lOnILWocxY85Av55hqjsOsMOyRcUZLRlrHpG3kqYOz9TgmXjKMiRj7P6 ghCnakycI4uc2aUSWQwjXZup1XJiI5H+EeSZxIwKhqd5i8BdJElCVJw2p/cr5X3dobj2 9ewadWgGXM9hoJHvWXB9AoTvEKkhtMZFdZBq3ZaYFncezN6J8+nESZ6/GKscrObzdXwO NT0g== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n26-20020a05640206da00b004109fbe1d44si7673688edy.251.2022.03.01.02.29.05; Tue, 01 Mar 2022 02:29:29 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234099AbiCAKP7 (ORCPT + 99 others); Tue, 1 Mar 2022 05:15:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229944AbiCAKP5 (ORCPT ); Tue, 1 Mar 2022 05:15:57 -0500 Received: from smtp-bc0a.mail.infomaniak.ch (smtp-bc0a.mail.infomaniak.ch [45.157.188.10]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 438398CDB2 for ; Tue, 1 Mar 2022 02:15:16 -0800 (PST) Received: from smtp-3-0000.mail.infomaniak.ch (unknown [10.4.36.107]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4K7Cmd6DjQzMq5kH; Tue, 1 Mar 2022 11:15:13 +0100 (CET) Received: from ns3096276.ip-94-23-54.eu (unknown [23.97.221.149]) by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4K7CmZ5H8YzljTgL; Tue, 1 Mar 2022 11:15:10 +0100 (CET) Message-ID: Date: Tue, 1 Mar 2022 11:15:09 +0100 MIME-Version: 1.0 User-Agent: Content-Language: en-US To: Christian Brauner Cc: Al Viro , Casey Schaufler , "Darrick J . Wong" , Eric Paris , James Morris , John Johansen , Kentaro Takeda , Miklos Szeredi , Paul Moore , "Serge E . Hallyn" , Stephen Smalley , Steve French , Tetsuo Handa , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, =?UTF-8?Q?Micka=c3=abl_Sala=c3=bcn?= References: <20220228215935.748017-1-mic@digikod.net> <20220301092232.wh7m3fxbe7hyxmcu@wittgenstein> From: =?UTF-8?Q?Micka=c3=abl_Sala=c3=bcn?= Subject: Re: [PATCH v1] fs: Fix inconsistent f_mode In-Reply-To: <20220301092232.wh7m3fxbe7hyxmcu@wittgenstein> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE 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 On 01/03/2022 10:22, Christian Brauner wrote: > On Mon, Feb 28, 2022 at 10:59:35PM +0100, Mickaël Salaün wrote: >> From: Mickaël Salaün >> >> While transitionning to ACC_MODE() with commit 5300990c0370 ("Sanitize >> f_flags helpers") and then fixing it with commit 6d125529c6cb ("Fix >> ACC_MODE() for real"), we lost an open flags consistency check. Opening >> a file with O_WRONLY | O_RDWR leads to an f_flags containing MAY_READ | >> MAY_WRITE (thanks to the ACC_MODE() helper) and an empty f_mode. >> Indeed, the OPEN_FMODE() helper transforms 3 (an incorrect value) to 0. >> >> Fortunately, vfs_read() and vfs_write() both check for FMODE_READ, or >> respectively FMODE_WRITE, and return an EBADF error if it is absent. >> Before commit 5300990c0370 ("Sanitize f_flags helpers"), opening a file >> with O_WRONLY | O_RDWR returned an EINVAL error. Let's restore this safe >> behavior. > > That specific part seems a bit risky at first glance. Given that the > patch referenced is from 2009 this means we've been allowing O_WRONLY | > O_RDWR to succeed for almost 13 years now. Yeah, it's an old bug, but we should keep in mind that a file descriptor created with such flags cannot be used to read nor write. However, unfortunately, it can be used for things like ioctl, fstat, chdir… I don't know if there is any user of this trick. Either way, there is an inconsistency between those using ACC_MODE() and those using OPEN_FMODE(). If we decide to take a side for the behavior of one or the other, without denying to create such FD, it could also break security policies. We have to choose what to potentially break… > >> >> To make it consistent with ACC_MODE(), this patch also changes >> OPEN_FMODE() to return FMODE_READ | FMODE_WRITE for O_WRONLY | O_RDWR. >> This may help protect from potential spurious issues. >> >> This issue could result in inconsistencies with AppArmor, Landlock and >> SELinux, but the VFS checks would still forbid read and write accesses. >> Tomoyo uses the ACC_MODE() transformation which is correct, and Smack >> doesn't check the file mode. Filesystems using OPEN_FMODE() should also >> be protected by the VFS checks. >> >> Fixes: 5300990c0370 ("Sanitize f_flags helpers") >> Cc: Al Viro >> Cc: Casey Schaufler >> Cc: Darrick J. Wong >> Cc: Eric Paris >> Cc: John Johansen >> Cc: Kentaro Takeda >> Cc: Miklos Szeredi >> Cc: Paul Moore >> Cc: Stephen Smalley >> Cc: Steve French >> Cc: Tetsuo Handa >> Signed-off-by: Mickaël Salaün >> Link: https://lore.kernel.org/r/20220228215935.748017-1-mic@digikod.net >> --- >> fs/file_table.c | 3 +++ >> include/linux/fs.h | 5 +++-- >> 2 files changed, 6 insertions(+), 2 deletions(-) >> >> diff --git a/fs/file_table.c b/fs/file_table.c >> index 7d2e692b66a9..b936f69525d0 100644 >> --- a/fs/file_table.c >> +++ b/fs/file_table.c >> @@ -135,6 +135,9 @@ static struct file *__alloc_file(int flags, const struct cred *cred) >> struct file *f; >> int error; >> >> + if ((flags & O_ACCMODE) == O_ACCMODE) >> + return ERR_PTR(-EINVAL); >> + >> f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL); >> if (unlikely(!f)) >> return ERR_PTR(-ENOMEM); >> diff --git a/include/linux/fs.h b/include/linux/fs.h >> index e2d892b201b0..83bc5aaf1c41 100644 >> --- a/include/linux/fs.h >> +++ b/include/linux/fs.h >> @@ -3527,8 +3527,9 @@ int __init list_bdev_fs_names(char *buf, size_t size); >> #define __FMODE_NONOTIFY ((__force int) FMODE_NONOTIFY) >> >> #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE]) >> -#define OPEN_FMODE(flag) ((__force fmode_t)(((flag + 1) & O_ACCMODE) | \ >> - (flag & __FMODE_NONOTIFY))) >> +#define OPEN_FMODE(flag) ((__force fmode_t)( \ >> + (((flag + 1) & O_ACCMODE) ?: O_ACCMODE) | \ >> + (flag & __FMODE_NONOTIFY))) >> >> static inline bool is_sxid(umode_t mode) >> { >> >> base-commit: 7e57714cd0ad2d5bb90e50b5096a0e671dec1ef3 >> -- >> 2.35.1 >>