From: "Mickaël Salaün" <mic@digikod.net>
To: Christian Brauner <brauner@kernel.org>
Cc: "Al Viro" <viro@zeniv.linux.org.uk>,
"Casey Schaufler" <casey@schaufler-ca.com>,
"Darrick J . Wong" <djwong@kernel.org>,
"Eric Paris" <eparis@parisplace.org>,
"James Morris" <jmorris@namei.org>,
"John Johansen" <john.johansen@canonical.com>,
"Kentaro Takeda" <takedakn@nttdata.co.jp>,
"Miklos Szeredi" <miklos@szeredi.hu>,
"Paul Moore" <paul@paul-moore.com>,
"Serge E . Hallyn" <serge@hallyn.com>,
"Stephen Smalley" <stephen.smalley.work@gmail.com>,
"Steve French" <sfrench@samba.org>,
"Tetsuo Handa" <penguin-kernel@I-love.SAKURA.ne.jp>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-security-module@vger.kernel.org,
"Mickaël Salaün" <mic@linux.microsoft.com>
Subject: Re: [PATCH v1] fs: Fix inconsistent f_mode
Date: Tue, 1 Mar 2022 11:15:09 +0100 [thread overview]
Message-ID: <f6b63133-d555-a77c-0847-de15a9302283@digikod.net> (raw)
In-Reply-To: <20220301092232.wh7m3fxbe7hyxmcu@wittgenstein>
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 <mic@linux.microsoft.com>
>>
>> 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 <viro@zeniv.linux.org.uk>
>> Cc: Casey Schaufler <casey@schaufler-ca.com>
>> Cc: Darrick J. Wong <djwong@kernel.org>
>> Cc: Eric Paris <eparis@parisplace.org>
>> Cc: John Johansen <john.johansen@canonical.com>
>> Cc: Kentaro Takeda <takedakn@nttdata.co.jp>
>> Cc: Miklos Szeredi <miklos@szeredi.hu>
>> Cc: Paul Moore <paul@paul-moore.com>
>> Cc: Stephen Smalley <stephen.smalley.work@gmail.com>
>> Cc: Steve French <sfrench@samba.org>
>> Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
>> Signed-off-by: Mickaël Salaün <mic@linux.microsoft.com>
>> 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
>>
next prev parent reply other threads:[~2022-03-01 10:15 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-28 21:59 [PATCH v1] fs: Fix inconsistent f_mode Mickaël Salaün
2022-03-01 9:22 ` Christian Brauner
2022-03-01 10:15 ` Mickaël Salaün [this message]
2022-03-09 21:31 ` Paul Moore
2022-03-10 0:36 ` John Johansen
2022-03-11 22:15 ` Paul Moore
2022-03-12 1:34 ` Tetsuo Handa
2022-03-12 15:17 ` Paul Moore
2022-03-14 8:22 ` Mickaël Salaün
2022-03-13 0:44 ` Al Viro
2022-03-13 0:42 ` Al Viro
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f6b63133-d555-a77c-0847-de15a9302283@digikod.net \
--to=mic@digikod.net \
--cc=brauner@kernel.org \
--cc=casey@schaufler-ca.com \
--cc=djwong@kernel.org \
--cc=eparis@parisplace.org \
--cc=jmorris@namei.org \
--cc=john.johansen@canonical.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=mic@linux.microsoft.com \
--cc=miklos@szeredi.hu \
--cc=paul@paul-moore.com \
--cc=penguin-kernel@I-love.SAKURA.ne.jp \
--cc=serge@hallyn.com \
--cc=sfrench@samba.org \
--cc=stephen.smalley.work@gmail.com \
--cc=takedakn@nttdata.co.jp \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox