On Thu, Sep 24, 2020 at 4:16 PM Stephen Smalley
[off-list ref] wrote:
On Thu, Sep 24, 2020 at 4:36 AM Ondrej Mosnacek [off-list ref] wrote:
quoted
On Wed, Sep 23, 2020 at 3:55 AM Ian Kent [off-list ref] wrote:
quoted
On Tue, 2020-09-22 at 09:33 +0200, Ondrej Mosnacek wrote:
quoted
On Mon, Sep 21, 2020 at 6:30 PM Al Viro [off-list ref]
wrote:
quoted
On Mon, Sep 21, 2020 at 06:09:22PM +0200, Christoph Hellwig wrote:
quoted
[adding Linus and Al]
On Mon, Sep 21, 2020 at 04:51:35PM +0200, Ondrej Mosnacek wrote:
quoted
Hi folks,
It seems that after commit 13c164b1a186 ("autofs: switch to
kernel_write") there is now an extra LSM permission required
(for the
current task to write to the automount pipe) for processes
accessing
some yet-to-to-be mounted directory on which an autofs mount is
set
up. The call chain is:
[...]
autofs_wait() ->
autofs_notify_daemon() ->
autofs_write() ->
kernel_write() ->
rw_verify_area() ->
security_file_permission()
The bug report that led me to this commit is at [1].
Technically, this is a regression for LSM users, since this is
a
kernel-internal operation and an LSM permission for the current
task
shouldn't be required. Can this patch be reverted? Perhaps
__kernel_{read|write}() could instead be renamed to
kernel_*_nocheck()
so that the name is more descriptive?
So we obviously should not break existing user space and need to
102 more quoted lines
IIUC he was against adding an FMODE flag, while I was rather
suggesting a new function parameter (I realize it probably wasn't
clear from what I wrote). The latter is less prone to accidents and
any new user would have to set that parameter to *something* and so
would be forced to think about whether he needs an LSM check or not.
Whereas with a separate _nosec()/nolsmcheck()/_whatever() function
they would likely just use the kernel_write() function they already
know and may not even realize that there is some decision to be made.
But that's just my opinion, I would still be happier with a separate
function than reverting to __kernel_write().
Historically we have taken one of two approaches for these situations:
1) Provide a separate interface like kernel_write() for use when we
don't want permission checking and don't have it call the security
hook at all. If you prefer kernel_write_nosec() that's fine but I
think that's somewhat implicit in the fact that it is a
kernel-initiated write, not a userspace write (which would hopefully
go through vfs_write() or similar and end up calling the hook).
Unfortunately not all of the kernel_write() calls seem to be of the
kind that we want to skip the permission checking. One in particular
that caught my attention is this btrfs one:
https://elixir.bootlin.com/linux/v5.9-rc6/source/fs/btrfs/send.c#L561
Basically a userspace process passes some fd via ioctl and kernel then
writes something in it. If we don't do any check here (and BTW we in
fact want the current's creds to apply here), then the process could
pass any open fd and bypass any LSM write permissions to the file
behind that fd. Possibly there are some checks that restrict what can
be passed there (I didn't check), but it would still be fragile to
rely on them.
So I guess we can't just skip the check whenever the kernel_*()
function is called.
2) Temporarily override creds to the init_cred or the result of
prepare_kernel_creds() before calling any credential-checking
functions and then revert creds afterward.
Most users of kernel_write() seem to also create/open the file
themselves, usually using the current's creds, so this would break
consistency when applied universally. Autofs (and also
bpfilter_send_req()) are special in that they first get the fd from a
userspace process and then write into it from other tasks' syscalls.
Then there are one or two users that create the file using
shmem_kernel_file_setup(), which does use prepare_kernel_creds(), but
the resulting file is marked S_PRIVATE, so all LSM checks are already
skipped for it anyway.
The problem with #2 is that it still requires that the policy allow
kernel_t (or its equivalent) to be able to write to these pipes, which
wasn't previously necessary and thus might not be allowed in all
policies (e.g. Android?). #1 avoids any need for policy for these
operations.
Yeah, I'm starting to think checking "kernel creds -> userspace
object" access vectors on purpose doesn't make much sense... you would
pretty much want to always allow that (no point in kernel blocking its
own operations) so just skipping the check in those cases where using
the current's creds doesn't make sense seems to be a better choice.
BTW, I don't have the candidate patch written yet, as I haven't had
time and also need to process new information after having reviewed
the existing callers.
--
Ondrej Mosnacek
Software Engineer, Platform Security - SELinux kernel
Red Hat, Inc.