diff mbox series

[v5,bpf-next,5/5] bpf/selftests: Add a selftest for bpf_getxattr

Message ID 20220628161948.475097-6-kpsingh@kernel.org (mailing list archive)
State New
Headers show
Series Add bpf_getxattr | expand

Commit Message

KP Singh June 28, 2022, 4:19 p.m. UTC
A simple test that adds an xattr on a copied /bin/ls and reads it back
when the copied ls is executed.

Signed-off-by: KP Singh <kpsingh@kernel.org>
---
 .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
 tools/testing/selftests/bpf/progs/xattr.c     | 37 +++++++++++++
 2 files changed, 91 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/xattr.c
 create mode 100644 tools/testing/selftests/bpf/progs/xattr.c

Comments

Christian Brauner June 28, 2022, 5:33 p.m. UTC | #1
On Tue, Jun 28, 2022 at 04:19:48PM +0000, KP Singh wrote:
> A simple test that adds an xattr on a copied /bin/ls and reads it back
> when the copied ls is executed.
> 
> Signed-off-by: KP Singh <kpsingh@kernel.org>
> ---
>  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
>  tools/testing/selftests/bpf/progs/xattr.c     | 37 +++++++++++++
>  2 files changed, 91 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/xattr.c
>  create mode 100644 tools/testing/selftests/bpf/progs/xattr.c
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/xattr.c b/tools/testing/selftests/bpf/prog_tests/xattr.c
> new file mode 100644
> index 000000000000..ef07fa8a1763
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/xattr.c
> @@ -0,0 +1,54 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +/*
> + * Copyright 2022 Google LLC.
> + */
> +
> +#include <test_progs.h>
> +#include <sys/xattr.h>
> +#include "xattr.skel.h"
> +
> +#define XATTR_NAME "security.bpf"
> +#define XATTR_VALUE "test_progs"
> +
> +void test_xattr(void)
> +{
> +	struct xattr *skel = NULL;
> +	char tmp_dir_path[] = "/tmp/xattrXXXXXX";
> +	char tmp_exec_path[64];
> +	char cmd[256];
> +	int err;
> +
> +	if (CHECK_FAIL(!mkdtemp(tmp_dir_path)))
> +		goto close_prog;
> +
> +	snprintf(tmp_exec_path, sizeof(tmp_exec_path), "%s/copy_of_ls",
> +		 tmp_dir_path);
> +	snprintf(cmd, sizeof(cmd), "cp /bin/ls %s", tmp_exec_path);
> +	if (CHECK_FAIL(system(cmd)))
> +		goto close_prog_rmdir;
> +
> +	if (CHECK_FAIL(setxattr(tmp_exec_path, XATTR_NAME, XATTR_VALUE,
> +			   sizeof(XATTR_VALUE), 0)))
> +		goto close_prog_rmdir;
> +
> +	skel = xattr__open_and_load();
> +	if (!ASSERT_OK_PTR(skel, "skel_load"))
> +		goto close_prog_rmdir;
> +
> +	err = xattr__attach(skel);
> +	if (!ASSERT_OK(err, "xattr__attach failed"))
> +		goto close_prog_rmdir;
> +
> +	snprintf(cmd, sizeof(cmd), "%s -l", tmp_exec_path);
> +	if (CHECK_FAIL(system(cmd)))
> +		goto close_prog_rmdir;
> +
> +	ASSERT_EQ(skel->bss->result, 1, "xattr result");
> +
> +close_prog_rmdir:
> +	snprintf(cmd, sizeof(cmd), "rm -rf %s", tmp_dir_path);
> +	system(cmd);
> +close_prog:
> +	xattr__destroy(skel);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/xattr.c b/tools/testing/selftests/bpf/progs/xattr.c
> new file mode 100644
> index 000000000000..ccc078fb8ebd
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/xattr.c
> @@ -0,0 +1,37 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +/*
> + * Copyright 2022 Google LLC.
> + */
> +
> +#include "vmlinux.h"
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +char _license[] SEC("license") = "GPL";
> +
> +#define XATTR_NAME "security.bpf"
> +#define XATTR_VALUE "test_progs"
> +
> +__u64 result = 0;
> +
> +extern ssize_t bpf_getxattr(struct dentry *dentry, struct inode *inode,
> +			    const char *name, void *value, int size) __ksym;
> +
> +SEC("lsm.s/bprm_committed_creds")
> +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> +{
> +	struct task_struct *current = bpf_get_current_task_btf();
> +	char dir_xattr_value[64] = {0};
> +	int xattr_sz = 0;
> +
> +	xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> +				bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> +				dir_xattr_value, 64);

Yeah, this isn't right. You're not accounting for the caller's userns
nor for the idmapped mount. If this is supposed to work you will need a
variant of vfs_getxattr() that takes the mount's idmapping into account
afaict. See what needs to happen after do_getxattr().
KP Singh June 28, 2022, 5:52 p.m. UTC | #2
On Tue, Jun 28, 2022 at 7:33 PM Christian Brauner <brauner@kernel.org> wrote:
>
> On Tue, Jun 28, 2022 at 04:19:48PM +0000, KP Singh wrote:
> > A simple test that adds an xattr on a copied /bin/ls and reads it back
> > when the copied ls is executed.
> >
> > Signed-off-by: KP Singh <kpsingh@kernel.org>
> > ---
> >  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++

[...]

> > +SEC("lsm.s/bprm_committed_creds")
> > +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > +{
> > +     struct task_struct *current = bpf_get_current_task_btf();
> > +     char dir_xattr_value[64] = {0};
> > +     int xattr_sz = 0;
> > +
> > +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> > +                             dir_xattr_value, 64);
>
> Yeah, this isn't right. You're not accounting for the caller's userns
> nor for the idmapped mount. If this is supposed to work you will need a
> variant of vfs_getxattr() that takes the mount's idmapping into account
> afaict. See what needs to happen after do_getxattr().

Thanks for taking a look.

So, If I understand correctly, we don't need xattr_permission (and
other checks in
vfs_getxattr) here as the BPF programs run as CAP_SYS_ADMIN.

but...

So, Is this bit what's missing then?

error = vfs_getxattr(mnt_userns, d, kname, ctx->kvalue, ctx->size);
if (error > 0) {
    if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
(strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
        posix_acl_fix_xattr_to_user(mnt_userns, d_inode(d),
            ctx->kvalue, error);
    if (ctx->size && copy_to_user(ctx->value, ctx->kvalue, error))
        error = -EFAULT;
}
else if (error == -ERANGE && ctx->size >= XATTR_SIZE_MAX) {
    /* The file system tried to returned a value bigger
than XATTR_SIZE_MAX bytes. Not possible. */
    error = -E2BIG;
}
Alexei Starovoitov June 28, 2022, 10:28 p.m. UTC | #3
On Tue, Jun 28, 2022 at 10:52 AM KP Singh <kpsingh@kernel.org> wrote:
>
> On Tue, Jun 28, 2022 at 7:33 PM Christian Brauner <brauner@kernel.org> wrote:
> >
> > On Tue, Jun 28, 2022 at 04:19:48PM +0000, KP Singh wrote:
> > > A simple test that adds an xattr on a copied /bin/ls and reads it back
> > > when the copied ls is executed.
> > >
> > > Signed-off-by: KP Singh <kpsingh@kernel.org>
> > > ---
> > >  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
>
> [...]
>
> > > +SEC("lsm.s/bprm_committed_creds")
> > > +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > +{
> > > +     struct task_struct *current = bpf_get_current_task_btf();
> > > +     char dir_xattr_value[64] = {0};
> > > +     int xattr_sz = 0;
> > > +
> > > +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> > > +                             dir_xattr_value, 64);
> >
> > Yeah, this isn't right. You're not accounting for the caller's userns
> > nor for the idmapped mount. If this is supposed to work you will need a
> > variant of vfs_getxattr() that takes the mount's idmapping into account
> > afaict. See what needs to happen after do_getxattr().
>
> Thanks for taking a look.
>
> So, If I understand correctly, we don't need xattr_permission (and
> other checks in
> vfs_getxattr) here as the BPF programs run as CAP_SYS_ADMIN.
>
> but...
>
> So, Is this bit what's missing then?
>
> error = vfs_getxattr(mnt_userns, d, kname, ctx->kvalue, ctx->size);
> if (error > 0) {
>     if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
> (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
>         posix_acl_fix_xattr_to_user(mnt_userns, d_inode(d),
>             ctx->kvalue, error);

That will not be correct.
posix_acl_fix_xattr_to_user checking current_user_ns()
is checking random tasks that happen to be running
when lsm hook got invoked.

KP,
we probably have to document clearly that neither 'current*'
should not be used here.
xattr_permission also makes little sense in this context.
If anything it can be a different kfunc if there is a use case,
but I don't see it yet.
bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
call it directly. It's the kernel that is doing its security thing.
Christian Brauner June 29, 2022, 8:11 a.m. UTC | #4
On Tue, Jun 28, 2022 at 03:28:42PM -0700, Alexei Starovoitov wrote:
> On Tue, Jun 28, 2022 at 10:52 AM KP Singh <kpsingh@kernel.org> wrote:
> >
> > On Tue, Jun 28, 2022 at 7:33 PM Christian Brauner <brauner@kernel.org> wrote:
> > >
> > > On Tue, Jun 28, 2022 at 04:19:48PM +0000, KP Singh wrote:
> > > > A simple test that adds an xattr on a copied /bin/ls and reads it back
> > > > when the copied ls is executed.
> > > >
> > > > Signed-off-by: KP Singh <kpsingh@kernel.org>
> > > > ---
> > > >  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
> >
> > [...]
> >
> > > > +SEC("lsm.s/bprm_committed_creds")
> > > > +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > > +{
> > > > +     struct task_struct *current = bpf_get_current_task_btf();
> > > > +     char dir_xattr_value[64] = {0};
> > > > +     int xattr_sz = 0;
> > > > +
> > > > +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > > +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> > > > +                             dir_xattr_value, 64);
> > >
> > > Yeah, this isn't right. You're not accounting for the caller's userns
> > > nor for the idmapped mount. If this is supposed to work you will need a
> > > variant of vfs_getxattr() that takes the mount's idmapping into account
> > > afaict. See what needs to happen after do_getxattr().
> >
> > Thanks for taking a look.
> >
> > So, If I understand correctly, we don't need xattr_permission (and
> > other checks in
> > vfs_getxattr) here as the BPF programs run as CAP_SYS_ADMIN.
> >
> > but...
> >
> > So, Is this bit what's missing then?
> >
> > error = vfs_getxattr(mnt_userns, d, kname, ctx->kvalue, ctx->size);
> > if (error > 0) {
> >     if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
> > (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
> >         posix_acl_fix_xattr_to_user(mnt_userns, d_inode(d),
> >             ctx->kvalue, error);
> 
> That will not be correct.
> posix_acl_fix_xattr_to_user checking current_user_ns()
> is checking random tasks that happen to be running
> when lsm hook got invoked.
> 
> KP,
> we probably have to document clearly that neither 'current*'
> should not be used here.
> xattr_permission also makes little sense in this context.
> If anything it can be a different kfunc if there is a use case,
> but I don't see it yet.
> bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
> call it directly. It's the kernel that is doing its security thing.

Right, but LSMs usually only retrieve their own xattr namespace (ima,
selinux, smack) or they calculate hashes for xattrs based on the raw
filesystem xattr values (evm).

But this new bpf_getxattr() is different. It allows to retrieve _any_
xattr in any security hook it can be attached to. So someone can write a
bpf program that retrieves filesystem capabilites or posix acls. And
these are xattrs that require higher-level vfs involvement to be
sensible in most contexts.

So looking at:

SEC("lsm.s/bprm_committed_creds")
void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
{
	struct task_struct *current = bpf_get_current_task_btf();
	char dir_xattr_value[64] = {0};
	int xattr_sz = 0;

	xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
				bprm->file->f_path.dentry->d_inode, XATTR_NAME,
				dir_xattr_value, 64);

	if (xattr_sz <= 0)
		return;

	if (!bpf_strncmp(dir_xattr_value, sizeof(XATTR_VALUE), XATTR_VALUE))
		result = 1;
}

This hooks a bpf-lsm program to the security_bprm_committed_creds()
hook. It then retrieves the extended attributes of the file to be
executed. The hook currently always retrieves the raw filesystem values.

But for example any XATTR_NAME_CAPS filesystem capabilities that
might've been stored will be taken into account during exec. And both
the idmapping of the mount and the caller matter when determing whether
they are used or not.

But the current implementation of bpf_getxattr() just ignores both. It
will always retrieve the raw filesystem values. So if one invokes this
hook they're not actually retrieving the values as they are seen by
fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
some use-cases it might very well become a security issue in others if
access decisions are always based on the raw values.

I'm not well-versed in this so bear with me, please.
Christian Brauner June 29, 2022, 9:55 a.m. UTC | #5
On Wed, Jun 29, 2022 at 10:11:19AM +0200, Christian Brauner wrote:
> On Tue, Jun 28, 2022 at 03:28:42PM -0700, Alexei Starovoitov wrote:
> > On Tue, Jun 28, 2022 at 10:52 AM KP Singh <kpsingh@kernel.org> wrote:
> > >
> > > On Tue, Jun 28, 2022 at 7:33 PM Christian Brauner <brauner@kernel.org> wrote:
> > > >
> > > > On Tue, Jun 28, 2022 at 04:19:48PM +0000, KP Singh wrote:
> > > > > A simple test that adds an xattr on a copied /bin/ls and reads it back
> > > > > when the copied ls is executed.
> > > > >
> > > > > Signed-off-by: KP Singh <kpsingh@kernel.org>
> > > > > ---
> > > > >  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
> > >
> > > [...]
> > >
> > > > > +SEC("lsm.s/bprm_committed_creds")
> > > > > +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > > > +{
> > > > > +     struct task_struct *current = bpf_get_current_task_btf();
> > > > > +     char dir_xattr_value[64] = {0};
> > > > > +     int xattr_sz = 0;
> > > > > +
> > > > > +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > > > +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> > > > > +                             dir_xattr_value, 64);
> > > >
> > > > Yeah, this isn't right. You're not accounting for the caller's userns
> > > > nor for the idmapped mount. If this is supposed to work you will need a
> > > > variant of vfs_getxattr() that takes the mount's idmapping into account
> > > > afaict. See what needs to happen after do_getxattr().
> > >
> > > Thanks for taking a look.
> > >
> > > So, If I understand correctly, we don't need xattr_permission (and
> > > other checks in
> > > vfs_getxattr) here as the BPF programs run as CAP_SYS_ADMIN.
> > >
> > > but...
> > >
> > > So, Is this bit what's missing then?
> > >
> > > error = vfs_getxattr(mnt_userns, d, kname, ctx->kvalue, ctx->size);
> > > if (error > 0) {
> > >     if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
> > > (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
> > >         posix_acl_fix_xattr_to_user(mnt_userns, d_inode(d),
> > >             ctx->kvalue, error);
> > 
> > That will not be correct.
> > posix_acl_fix_xattr_to_user checking current_user_ns()
> > is checking random tasks that happen to be running
> > when lsm hook got invoked.
> > 
> > KP,
> > we probably have to document clearly that neither 'current*'
> > should not be used here.
> > xattr_permission also makes little sense in this context.
> > If anything it can be a different kfunc if there is a use case,
> > but I don't see it yet.
> > bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
> > call it directly. It's the kernel that is doing its security thing.
> 
> Right, but LSMs usually only retrieve their own xattr namespace (ima,
> selinux, smack) or they calculate hashes for xattrs based on the raw
> filesystem xattr values (evm).
> 
> But this new bpf_getxattr() is different. It allows to retrieve _any_
> xattr in any security hook it can be attached to. So someone can write a
> bpf program that retrieves filesystem capabilites or posix acls. And
> these are xattrs that require higher-level vfs involvement to be
> sensible in most contexts.
> 
> So looking at:
> 
> SEC("lsm.s/bprm_committed_creds")
> void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> {
> 	struct task_struct *current = bpf_get_current_task_btf();
> 	char dir_xattr_value[64] = {0};
> 	int xattr_sz = 0;
> 
> 	xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> 				bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> 				dir_xattr_value, 64);
> 
> 	if (xattr_sz <= 0)
> 		return;
> 
> 	if (!bpf_strncmp(dir_xattr_value, sizeof(XATTR_VALUE), XATTR_VALUE))
> 		result = 1;
> }
> 
> This hooks a bpf-lsm program to the security_bprm_committed_creds()
> hook. It then retrieves the extended attributes of the file to be
> executed. The hook currently always retrieves the raw filesystem values.
> 
> But for example any XATTR_NAME_CAPS filesystem capabilities that
> might've been stored will be taken into account during exec. And both
> the idmapping of the mount and the caller matter when determing whether
> they are used or not.
> 
> But the current implementation of bpf_getxattr() just ignores both. It
> will always retrieve the raw filesystem values. So if one invokes this
> hook they're not actually retrieving the values as they are seen by
> fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
> some use-cases it might very well become a security issue in others if
> access decisions are always based on the raw values.
> 
> I'm not well-versed in this so bear with me, please.

If this is really just about retrieving the "security.bpf" xattr and no
other xattr then the bpf_getxattr() variant should somehow hard-code
that to ensure that no other xattrs can be retrieved, imho.
Alexei Starovoitov June 30, 2022, 3:02 a.m. UTC | #6
On Wed, Jun 29, 2022 at 2:56 AM Christian Brauner <brauner@kernel.org> wrote:
>
> On Wed, Jun 29, 2022 at 10:11:19AM +0200, Christian Brauner wrote:
> > On Tue, Jun 28, 2022 at 03:28:42PM -0700, Alexei Starovoitov wrote:
> > > On Tue, Jun 28, 2022 at 10:52 AM KP Singh <kpsingh@kernel.org> wrote:
> > > >
> > > > On Tue, Jun 28, 2022 at 7:33 PM Christian Brauner <brauner@kernel.org> wrote:
> > > > >
> > > > > On Tue, Jun 28, 2022 at 04:19:48PM +0000, KP Singh wrote:
> > > > > > A simple test that adds an xattr on a copied /bin/ls and reads it back
> > > > > > when the copied ls is executed.
> > > > > >
> > > > > > Signed-off-by: KP Singh <kpsingh@kernel.org>
> > > > > > ---
> > > > > >  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
> > > >
> > > > [...]
> > > >
> > > > > > +SEC("lsm.s/bprm_committed_creds")
> > > > > > +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > > > > +{
> > > > > > +     struct task_struct *current = bpf_get_current_task_btf();
> > > > > > +     char dir_xattr_value[64] = {0};
> > > > > > +     int xattr_sz = 0;
> > > > > > +
> > > > > > +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > > > > +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> > > > > > +                             dir_xattr_value, 64);
> > > > >
> > > > > Yeah, this isn't right. You're not accounting for the caller's userns
> > > > > nor for the idmapped mount. If this is supposed to work you will need a
> > > > > variant of vfs_getxattr() that takes the mount's idmapping into account
> > > > > afaict. See what needs to happen after do_getxattr().
> > > >
> > > > Thanks for taking a look.
> > > >
> > > > So, If I understand correctly, we don't need xattr_permission (and
> > > > other checks in
> > > > vfs_getxattr) here as the BPF programs run as CAP_SYS_ADMIN.
> > > >
> > > > but...
> > > >
> > > > So, Is this bit what's missing then?
> > > >
> > > > error = vfs_getxattr(mnt_userns, d, kname, ctx->kvalue, ctx->size);
> > > > if (error > 0) {
> > > >     if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
> > > > (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
> > > >         posix_acl_fix_xattr_to_user(mnt_userns, d_inode(d),
> > > >             ctx->kvalue, error);
> > >
> > > That will not be correct.
> > > posix_acl_fix_xattr_to_user checking current_user_ns()
> > > is checking random tasks that happen to be running
> > > when lsm hook got invoked.
> > >
> > > KP,
> > > we probably have to document clearly that neither 'current*'
> > > should not be used here.
> > > xattr_permission also makes little sense in this context.
> > > If anything it can be a different kfunc if there is a use case,
> > > but I don't see it yet.
> > > bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
> > > call it directly. It's the kernel that is doing its security thing.
> >
> > Right, but LSMs usually only retrieve their own xattr namespace (ima,
> > selinux, smack) or they calculate hashes for xattrs based on the raw
> > filesystem xattr values (evm).
> >
> > But this new bpf_getxattr() is different. It allows to retrieve _any_
> > xattr in any security hook it can be attached to. So someone can write a
> > bpf program that retrieves filesystem capabilites or posix acls. And
> > these are xattrs that require higher-level vfs involvement to be
> > sensible in most contexts.
> >
> > So looking at:
> >
> > SEC("lsm.s/bprm_committed_creds")
> > void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > {
> >       struct task_struct *current = bpf_get_current_task_btf();
> >       char dir_xattr_value[64] = {0};
> >       int xattr_sz = 0;
> >
> >       xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> >                               bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> >                               dir_xattr_value, 64);
> >
> >       if (xattr_sz <= 0)
> >               return;
> >
> >       if (!bpf_strncmp(dir_xattr_value, sizeof(XATTR_VALUE), XATTR_VALUE))
> >               result = 1;
> > }
> >
> > This hooks a bpf-lsm program to the security_bprm_committed_creds()
> > hook. It then retrieves the extended attributes of the file to be
> > executed. The hook currently always retrieves the raw filesystem values.
> >
> > But for example any XATTR_NAME_CAPS filesystem capabilities that
> > might've been stored will be taken into account during exec. And both
> > the idmapping of the mount and the caller matter when determing whether
> > they are used or not.
> >
> > But the current implementation of bpf_getxattr() just ignores both. It
> > will always retrieve the raw filesystem values. So if one invokes this
> > hook they're not actually retrieving the values as they are seen by
> > fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
> > some use-cases it might very well become a security issue in others if
> > access decisions are always based on the raw values.
> >
> > I'm not well-versed in this so bear with me, please.
>
> If this is really just about retrieving the "security.bpf" xattr and no
> other xattr then the bpf_getxattr() variant should somehow hard-code
> that to ensure that no other xattrs can be retrieved, imho.

All of these restrictions look very artificial to me.
Especially the part "might very well become a security issue"
just doesn't click.
We're talking about bpf-lsm progs here that implement security.
Can somebody implement a poor bpf-lsm that doesn't enforce
any actual security? Sure. It's a code.
No one complains about the usage of EXPORT_SYMBOL(__vfs_getxattr)
in the existing LSMs like selinux.
No one complains about its usage in out of tree LSMs.
Is that a security issue? Of course not.
__vfs_getxattr is a kernel mechanism that LSMs use to implement
the security features they need.
__vfs_getxattr as kfunc here is pretty much the same as EXPORT_SYMBOL
with a big difference that it's EXPORT_SYMBOL_GPL.
BPF land doesn't have an equivalent of non-gpl export and is not going
to get one.
Christian Brauner June 30, 2022, 11:45 a.m. UTC | #7
On Wed, Jun 29, 2022 at 08:02:50PM -0700, Alexei Starovoitov wrote:
> On Wed, Jun 29, 2022 at 2:56 AM Christian Brauner <brauner@kernel.org> wrote:
> >
> > On Wed, Jun 29, 2022 at 10:11:19AM +0200, Christian Brauner wrote:
> > > On Tue, Jun 28, 2022 at 03:28:42PM -0700, Alexei Starovoitov wrote:
> > > > On Tue, Jun 28, 2022 at 10:52 AM KP Singh <kpsingh@kernel.org> wrote:
> > > > >
> > > > > On Tue, Jun 28, 2022 at 7:33 PM Christian Brauner <brauner@kernel.org> wrote:
> > > > > >
> > > > > > On Tue, Jun 28, 2022 at 04:19:48PM +0000, KP Singh wrote:
> > > > > > > A simple test that adds an xattr on a copied /bin/ls and reads it back
> > > > > > > when the copied ls is executed.
> > > > > > >
> > > > > > > Signed-off-by: KP Singh <kpsingh@kernel.org>
> > > > > > > ---
> > > > > > >  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
> > > > >
> > > > > [...]
> > > > >
> > > > > > > +SEC("lsm.s/bprm_committed_creds")
> > > > > > > +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > > > > > +{
> > > > > > > +     struct task_struct *current = bpf_get_current_task_btf();
> > > > > > > +     char dir_xattr_value[64] = {0};
> > > > > > > +     int xattr_sz = 0;
> > > > > > > +
> > > > > > > +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > > > > > +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> > > > > > > +                             dir_xattr_value, 64);
> > > > > >
> > > > > > Yeah, this isn't right. You're not accounting for the caller's userns
> > > > > > nor for the idmapped mount. If this is supposed to work you will need a
> > > > > > variant of vfs_getxattr() that takes the mount's idmapping into account
> > > > > > afaict. See what needs to happen after do_getxattr().
> > > > >
> > > > > Thanks for taking a look.
> > > > >
> > > > > So, If I understand correctly, we don't need xattr_permission (and
> > > > > other checks in
> > > > > vfs_getxattr) here as the BPF programs run as CAP_SYS_ADMIN.
> > > > >
> > > > > but...
> > > > >
> > > > > So, Is this bit what's missing then?
> > > > >
> > > > > error = vfs_getxattr(mnt_userns, d, kname, ctx->kvalue, ctx->size);
> > > > > if (error > 0) {
> > > > >     if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
> > > > > (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
> > > > >         posix_acl_fix_xattr_to_user(mnt_userns, d_inode(d),
> > > > >             ctx->kvalue, error);
> > > >
> > > > That will not be correct.
> > > > posix_acl_fix_xattr_to_user checking current_user_ns()
> > > > is checking random tasks that happen to be running
> > > > when lsm hook got invoked.
> > > >
> > > > KP,
> > > > we probably have to document clearly that neither 'current*'
> > > > should not be used here.
> > > > xattr_permission also makes little sense in this context.
> > > > If anything it can be a different kfunc if there is a use case,
> > > > but I don't see it yet.
> > > > bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
> > > > call it directly. It's the kernel that is doing its security thing.
> > >
> > > Right, but LSMs usually only retrieve their own xattr namespace (ima,
> > > selinux, smack) or they calculate hashes for xattrs based on the raw
> > > filesystem xattr values (evm).
> > >
> > > But this new bpf_getxattr() is different. It allows to retrieve _any_
> > > xattr in any security hook it can be attached to. So someone can write a
> > > bpf program that retrieves filesystem capabilites or posix acls. And
> > > these are xattrs that require higher-level vfs involvement to be
> > > sensible in most contexts.
> > >
> > > So looking at:
> > >
> > > SEC("lsm.s/bprm_committed_creds")
> > > void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > {
> > >       struct task_struct *current = bpf_get_current_task_btf();
> > >       char dir_xattr_value[64] = {0};
> > >       int xattr_sz = 0;
> > >
> > >       xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > >                               bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> > >                               dir_xattr_value, 64);
> > >
> > >       if (xattr_sz <= 0)
> > >               return;
> > >
> > >       if (!bpf_strncmp(dir_xattr_value, sizeof(XATTR_VALUE), XATTR_VALUE))
> > >               result = 1;
> > > }
> > >
> > > This hooks a bpf-lsm program to the security_bprm_committed_creds()
> > > hook. It then retrieves the extended attributes of the file to be
> > > executed. The hook currently always retrieves the raw filesystem values.
> > >
> > > But for example any XATTR_NAME_CAPS filesystem capabilities that
> > > might've been stored will be taken into account during exec. And both
> > > the idmapping of the mount and the caller matter when determing whether
> > > they are used or not.
> > >
> > > But the current implementation of bpf_getxattr() just ignores both. It
> > > will always retrieve the raw filesystem values. So if one invokes this
> > > hook they're not actually retrieving the values as they are seen by
> > > fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
> > > some use-cases it might very well become a security issue in others if
> > > access decisions are always based on the raw values.
> > >
> > > I'm not well-versed in this so bear with me, please.
> >
> > If this is really just about retrieving the "security.bpf" xattr and no
> > other xattr then the bpf_getxattr() variant should somehow hard-code
> > that to ensure that no other xattrs can be retrieved, imho.
> 
> All of these restrictions look very artificial to me.
> Especially the part "might very well become a security issue"
> just doesn't click.
> We're talking about bpf-lsm progs here that implement security.
> Can somebody implement a poor bpf-lsm that doesn't enforce
> any actual security? Sure. It's a code.

The point is that with the current implementation of bpf_getxattr() you
are able to retrieve any xattrs and we have way less control over a
bpf-lsm program than we do over selinux which a simple git grep
__vfs_getxattr() is all we need.

The thing is that with bpf_getxattr() as it stands it is currently
impossible to retrieve xattr values - specifically filesystem
capabilities and posix acls - and see them exactly like the code you're
trying to supervise is. And that seems very strange from a security
perspective. So if someone were to write

SEC("lsm.s/bprm_creds_from_file")
void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
{
	struct task_struct *current = bpf_get_current_task_btf();

	xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
				bprm->file->f_path.dentry->d_inode,
				XATTR_NAME_POSIX_ACL_ACCESS, ..);
	// or
	xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
				bprm->file->f_path.dentry->d_inode,
				XATTR_NAME_CAPS, ..);

}

they'd get the raw nscaps and the raw xattrs back. But now, as just a
tiny example, the nscaps->rootuid and the ->e_id fields in the posix
ACLs make zero sense in this context.

And what's more there's no way for the bpf-lsm program to turn them into
something that makes sense in the context of the hook they are retrieved
in. It lacks all the necessary helpers to do so afaict.

> No one complains about the usage of EXPORT_SYMBOL(__vfs_getxattr)
> in the existing LSMs like selinux.

Selinux only cares about its own xattr namespace. It doesn't retrieve
fscaps or posix acls and it's not possible to write selinux programs
that do so. With the bpf-lsm that's very much possible.

And if we'd notice selinux would start retrieving random xattrs we'd ask
the same questions we do here.

> No one complains about its usage in out of tree LSMs.
> Is that a security issue? Of course not.
> __vfs_getxattr is a kernel mechanism that LSMs use to implement
> the security features they need.
> __vfs_getxattr as kfunc here is pretty much the same as EXPORT_SYMBOL
> with a big difference that it's EXPORT_SYMBOL_GPL.
> BPF land doesn't have an equivalent of non-gpl export and is not going
> to get one.

This discussion would probably be a lot shorter if this series were sent
with a proper explanation of how this supposed to work and what it's
used for.

A series without a cover letter and no detailed explanation in the
commit messages makes it quite hard to understand whether what is asked
can be acked or not.

I'm just adding Serge and Casey to double-check here as the LSM stuff is
more up their alley. I can just look at this from the perspective of a
vfs person.

If you have your eBPF meeting thing I'm also happy to jump on there next
week to get more context.
KP Singh June 30, 2022, 12:21 p.m. UTC | #8
On Thu, Jun 30, 2022 at 1:45 PM Christian Brauner <brauner@kernel.org> wrote:
>
> On Wed, Jun 29, 2022 at 08:02:50PM -0700, Alexei Starovoitov wrote:
> > On Wed, Jun 29, 2022 at 2:56 AM Christian Brauner <brauner@kernel.org> wrote:

[...]

> > > > > > > >
> > > > > > > > Signed-off-by: KP Singh <kpsingh@kernel.org>
> > > > > > > > ---
> > > > > > > >  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
> > > > > >
> > > > > > [...]
> > > > > >
> > > > > > > > +SEC("lsm.s/bprm_committed_creds")
> > > > > > > > +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > > > > > > +{
> > > > > > > > +     struct task_struct *current = bpf_get_current_task_btf();
> > > > > > > > +     char dir_xattr_value[64] = {0};
> > > > > > > > +     int xattr_sz = 0;
> > > > > > > > +
> > > > > > > > +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > > > > > > +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> > > > > > > > +                             dir_xattr_value, 64);
> > > > > > >
> > > > > > > Yeah, this isn't right. You're not accounting for the caller's userns
> > > > > > > nor for the idmapped mount. If this is supposed to work you will need a
> > > > > > > variant of vfs_getxattr() that takes the mount's idmapping into account
> > > > > > > afaict. See what needs to happen after do_getxattr().
> > > > > >
> > > > > > Thanks for taking a look.
> > > > > >

[...]

> > > > >
> > > > > That will not be correct.
> > > > > posix_acl_fix_xattr_to_user checking current_user_ns()
> > > > > is checking random tasks that happen to be running
> > > > > when lsm hook got invoked.
> > > > >
> > > > > KP,
> > > > > we probably have to document clearly that neither 'current*'
> > > > > should not be used here.
> > > > > xattr_permission also makes little sense in this context.
> > > > > If anything it can be a different kfunc if there is a use case,
> > > > > but I don't see it yet.
> > > > > bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
> > > > > call it directly. It's the kernel that is doing its security thing.
> > > >
> > > > Right, but LSMs usually only retrieve their own xattr namespace (ima,
> > > > selinux, smack) or they calculate hashes for xattrs based on the raw
> > > > filesystem xattr values (evm).
> > > >
> > > > But this new bpf_getxattr() is different. It allows to retrieve _any_
> > > > xattr in any security hook it can be attached to. So someone can write a
> > > > bpf program that retrieves filesystem capabilites or posix acls. And
> > > > these are xattrs that require higher-level vfs involvement to be
> > > > sensible in most contexts.
> > > >

[...]

> > > >
> > > > This hooks a bpf-lsm program to the security_bprm_committed_creds()
> > > > hook. It then retrieves the extended attributes of the file to be
> > > > executed. The hook currently always retrieves the raw filesystem values.
> > > >
> > > > But for example any XATTR_NAME_CAPS filesystem capabilities that
> > > > might've been stored will be taken into account during exec. And both
> > > > the idmapping of the mount and the caller matter when determing whether
> > > > they are used or not.
> > > >
> > > > But the current implementation of bpf_getxattr() just ignores both. It
> > > > will always retrieve the raw filesystem values. So if one invokes this
> > > > hook they're not actually retrieving the values as they are seen by
> > > > fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
> > > > some use-cases it might very well become a security issue in others if
> > > > access decisions are always based on the raw values.
> > > >
> > > > I'm not well-versed in this so bear with me, please.
> > >
> > > If this is really just about retrieving the "security.bpf" xattr and no
> > > other xattr then the bpf_getxattr() variant should somehow hard-code
> > > that to ensure that no other xattrs can be retrieved, imho.
> >
> > All of these restrictions look very artificial to me.
> > Especially the part "might very well become a security issue"
> > just doesn't click.
> > We're talking about bpf-lsm progs here that implement security.
> > Can somebody implement a poor bpf-lsm that doesn't enforce
> > any actual security? Sure. It's a code.
>
> The point is that with the current implementation of bpf_getxattr() you
> are able to retrieve any xattrs and we have way less control over a
> bpf-lsm program than we do over selinux which a simple git grep
> __vfs_getxattr() is all we need.
>
> The thing is that with bpf_getxattr() as it stands it is currently
> impossible to retrieve xattr values - specifically filesystem
> capabilities and posix acls - and see them exactly like the code you're
> trying to supervise is. And that seems very strange from a security
> perspective. So if someone were to write
>
> SEC("lsm.s/bprm_creds_from_file")
> void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> {
>         struct task_struct *current = bpf_get_current_task_btf();
>
>         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
>                                 bprm->file->f_path.dentry->d_inode,
>                                 XATTR_NAME_POSIX_ACL_ACCESS, ..);
>         // or
>         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
>                                 bprm->file->f_path.dentry->d_inode,
>                                 XATTR_NAME_CAPS, ..);
>
> }
>
> they'd get the raw nscaps and the raw xattrs back. But now, as just a
> tiny example, the nscaps->rootuid and the ->e_id fields in the posix
> ACLs make zero sense in this context.
>
> And what's more there's no way for the bpf-lsm program to turn them into
> something that makes sense in the context of the hook they are retrieved
> in. It lacks all the necessary helpers to do so afaict.
>
> > No one complains about the usage of EXPORT_SYMBOL(__vfs_getxattr)
> > in the existing LSMs like selinux.
>
> Selinux only cares about its own xattr namespace. It doesn't retrieve
> fscaps or posix acls and it's not possible to write selinux programs
> that do so. With the bpf-lsm that's very much possible.
>
> And if we'd notice selinux would start retrieving random xattrs we'd ask
> the same questions we do here.
>
> > No one complains about its usage in out of tree LSMs.
> > Is that a security issue? Of course not.
> > __vfs_getxattr is a kernel mechanism that LSMs use to implement
> > the security features they need.
> > __vfs_getxattr as kfunc here is pretty much the same as EXPORT_SYMBOL
> > with a big difference that it's EXPORT_SYMBOL_GPL.
> > BPF land doesn't have an equivalent of non-gpl export and is not going
> > to get one.

I want to reiterate what Alexei is saying here:

*Please* consider this as a simple wrapper around __vfs_getxattr
with a limited attach surface and extra verification checks and
and nothing else.

What you are saying is __vfs_getxattr does not make sense in some
contexts. But kernel modules can still use it right?

The user is implementing an LSM, if they chose to do things that don't make
sense, then they can surely cause a lot more harm:

SEC("lsm/bprm_check_security")
int BPF_PROG(bprm_check, struct linux_binprm *bprm)
{
     return -EPERM;
}

>
> This discussion would probably be a lot shorter if this series were sent
> with a proper explanation of how this supposed to work and what it's
> used for.

It's currently scoped to BPF LSM (albeit limited to LSM for now)
but it won't just be used in LSM programs but some (allow-listed)
tracing programs too.

We want to leave the flexibility to the implementer of the LSM hooks. If the
implementer choses to retrieve posix_acl_* we can also expose
posix_acl_fix_xattr_to_user or a different kfunc that adds this logic too
but that would be a separate kfunc (and a separate use-case).

>
> A series without a cover letter and no detailed explanation in the
> commit messages makes it quite hard to understand whether what is asked
> can be acked or not.

As I mentioned in

https://lore.kernel.org/bpf/CACYkzJ70uqVJr5EnM0i03Lu+zkuSsXOXcOLQoUS6HZPqH=skpQ@mail.gmail.com/T/#m74f32bae800a97d5c2caf08cee4199d3ba48d76c

I will resend with a cover letter that has more details.

>
> I'm just adding Serge and Casey to double-check here as the LSM stuff is
> more up their alley. I can just look at this from the perspective of a
> vfs person.
>
> If you have your eBPF meeting thing I'm also happy to jump on there next

Sure, we can discuss this during BPF office hours next week.


> week to get more context.
KP Singh June 30, 2022, 12:23 p.m. UTC | #9
On Thu, Jun 30, 2022 at 2:21 PM KP Singh <kpsingh@kernel.org> wrote:
>
> On Thu, Jun 30, 2022 at 1:45 PM Christian Brauner <brauner@kernel.org> wrote:
> >
> > On Wed, Jun 29, 2022 at 08:02:50PM -0700, Alexei Starovoitov wrote:
> > > On Wed, Jun 29, 2022 at 2:56 AM Christian Brauner <brauner@kernel.org> wrote:
>
> [...]
>
> > > > > > > > >
> > > > > > > > > Signed-off-by: KP Singh <kpsingh@kernel.org>
> > > > > > > > > ---
> > > > > > > > >  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
> > > > > > >
> > > > > > > [...]
> > > > > > >
> > > > > > > > > +SEC("lsm.s/bprm_committed_creds")
> > > > > > > > > +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > > > > > > > +{
> > > > > > > > > +     struct task_struct *current = bpf_get_current_task_btf();
> > > > > > > > > +     char dir_xattr_value[64] = {0};
> > > > > > > > > +     int xattr_sz = 0;
> > > > > > > > > +
> > > > > > > > > +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > > > > > > > +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> > > > > > > > > +                             dir_xattr_value, 64);
> > > > > > > >
> > > > > > > > Yeah, this isn't right. You're not accounting for the caller's userns
> > > > > > > > nor for the idmapped mount. If this is supposed to work you will need a
> > > > > > > > variant of vfs_getxattr() that takes the mount's idmapping into account
> > > > > > > > afaict. See what needs to happen after do_getxattr().
> > > > > > >
> > > > > > > Thanks for taking a look.
> > > > > > >
>
> [...]
>
> > > > > >
> > > > > > That will not be correct.
> > > > > > posix_acl_fix_xattr_to_user checking current_user_ns()
> > > > > > is checking random tasks that happen to be running
> > > > > > when lsm hook got invoked.
> > > > > >
> > > > > > KP,
> > > > > > we probably have to document clearly that neither 'current*'
> > > > > > should not be used here.
> > > > > > xattr_permission also makes little sense in this context.
> > > > > > If anything it can be a different kfunc if there is a use case,
> > > > > > but I don't see it yet.
> > > > > > bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
> > > > > > call it directly. It's the kernel that is doing its security thing.
> > > > >
> > > > > Right, but LSMs usually only retrieve their own xattr namespace (ima,
> > > > > selinux, smack) or they calculate hashes for xattrs based on the raw
> > > > > filesystem xattr values (evm).
> > > > >
> > > > > But this new bpf_getxattr() is different. It allows to retrieve _any_
> > > > > xattr in any security hook it can be attached to. So someone can write a
> > > > > bpf program that retrieves filesystem capabilites or posix acls. And
> > > > > these are xattrs that require higher-level vfs involvement to be
> > > > > sensible in most contexts.
> > > > >
>
> [...]
>
> > > > >
> > > > > This hooks a bpf-lsm program to the security_bprm_committed_creds()
> > > > > hook. It then retrieves the extended attributes of the file to be
> > > > > executed. The hook currently always retrieves the raw filesystem values.
> > > > >
> > > > > But for example any XATTR_NAME_CAPS filesystem capabilities that
> > > > > might've been stored will be taken into account during exec. And both
> > > > > the idmapping of the mount and the caller matter when determing whether
> > > > > they are used or not.
> > > > >
> > > > > But the current implementation of bpf_getxattr() just ignores both. It
> > > > > will always retrieve the raw filesystem values. So if one invokes this
> > > > > hook they're not actually retrieving the values as they are seen by
> > > > > fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
> > > > > some use-cases it might very well become a security issue in others if
> > > > > access decisions are always based on the raw values.
> > > > >
> > > > > I'm not well-versed in this so bear with me, please.
> > > >
> > > > If this is really just about retrieving the "security.bpf" xattr and no
> > > > other xattr then the bpf_getxattr() variant should somehow hard-code
> > > > that to ensure that no other xattrs can be retrieved, imho.
> > >
> > > All of these restrictions look very artificial to me.
> > > Especially the part "might very well become a security issue"
> > > just doesn't click.
> > > We're talking about bpf-lsm progs here that implement security.
> > > Can somebody implement a poor bpf-lsm that doesn't enforce
> > > any actual security? Sure. It's a code.
> >
> > The point is that with the current implementation of bpf_getxattr() you
> > are able to retrieve any xattrs and we have way less control over a
> > bpf-lsm program than we do over selinux which a simple git grep
> > __vfs_getxattr() is all we need.
> >
> > The thing is that with bpf_getxattr() as it stands it is currently
> > impossible to retrieve xattr values - specifically filesystem
> > capabilities and posix acls - and see them exactly like the code you're
> > trying to supervise is. And that seems very strange from a security
> > perspective. So if someone were to write
> >
> > SEC("lsm.s/bprm_creds_from_file")
> > void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > {
> >         struct task_struct *current = bpf_get_current_task_btf();
> >
> >         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> >                                 bprm->file->f_path.dentry->d_inode,
> >                                 XATTR_NAME_POSIX_ACL_ACCESS, ..);
> >         // or
> >         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> >                                 bprm->file->f_path.dentry->d_inode,
> >                                 XATTR_NAME_CAPS, ..);
> >
> > }
> >
> > they'd get the raw nscaps and the raw xattrs back. But now, as just a
> > tiny example, the nscaps->rootuid and the ->e_id fields in the posix
> > ACLs make zero sense in this context.
> >
> > And what's more there's no way for the bpf-lsm program to turn them into
> > something that makes sense in the context of the hook they are retrieved
> > in. It lacks all the necessary helpers to do so afaict.
> >
> > > No one complains about the usage of EXPORT_SYMBOL(__vfs_getxattr)
> > > in the existing LSMs like selinux.
> >
> > Selinux only cares about its own xattr namespace. It doesn't retrieve
> > fscaps or posix acls and it's not possible to write selinux programs
> > that do so. With the bpf-lsm that's very much possible.
> >
> > And if we'd notice selinux would start retrieving random xattrs we'd ask
> > the same questions we do here.
> >
> > > No one complains about its usage in out of tree LSMs.
> > > Is that a security issue? Of course not.
> > > __vfs_getxattr is a kernel mechanism that LSMs use to implement
> > > the security features they need.
> > > __vfs_getxattr as kfunc here is pretty much the same as EXPORT_SYMBOL

Alexei, should we consider renaming it to bpf__vfs_getxattr to emphasize the
fact that this is just a simple wrapper around __vfs_getxattr?

> > > with a big difference that it's EXPORT_SYMBOL_GPL.
> > > BPF land doesn't have an equivalent of non-gpl export and is not going
> > > to get one.
>
> I want to reiterate what Alexei is saying here:
>
> *Please* consider this as a simple wrapper around __vfs_getxattr
> with a limited attach surface and extra verification checks and
> and nothing else.
>
> What you are saying is __vfs_getxattr does not make sense in some
> contexts. But kernel modules can still use it right?
>
> The user is implementing an LSM, if they chose to do things that don't make
> sense, then they can surely cause a lot more harm:
>
> SEC("lsm/bprm_check_security")
> int BPF_PROG(bprm_check, struct linux_binprm *bprm)
> {
>      return -EPERM;
> }
>
> >
> > This discussion would probably be a lot shorter if this series were sent
> > with a proper explanation of how this supposed to work and what it's
> > used for.
>
> It's currently scoped to BPF LSM (albeit limited to LSM for now)
> but it won't just be used in LSM programs but some (allow-listed)
> tracing programs too.
>
> We want to leave the flexibility to the implementer of the LSM hooks. If the
> implementer choses to retrieve posix_acl_* we can also expose
> posix_acl_fix_xattr_to_user or a different kfunc that adds this logic too
> but that would be a separate kfunc (and a separate use-case).
>
> >
> > A series without a cover letter and no detailed explanation in the
> > commit messages makes it quite hard to understand whether what is asked
> > can be acked or not.
>
> As I mentioned in
>
> https://lore.kernel.org/bpf/CACYkzJ70uqVJr5EnM0i03Lu+zkuSsXOXcOLQoUS6HZPqH=skpQ@mail.gmail.com/T/#m74f32bae800a97d5c2caf08cee4199d3ba48d76c
>
> I will resend with a cover letter that has more details.
>
> >
> > I'm just adding Serge and Casey to double-check here as the LSM stuff is
> > more up their alley. I can just look at this from the perspective of a
> > vfs person.
> >
> > If you have your eBPF meeting thing I'm also happy to jump on there next
>
> Sure, we can discuss this during BPF office hours next week.
>
>
> > week to get more context.
Christian Brauner June 30, 2022, 1:26 p.m. UTC | #10
On Thu, Jun 30, 2022 at 02:21:56PM +0200, KP Singh wrote:
> On Thu, Jun 30, 2022 at 1:45 PM Christian Brauner <brauner@kernel.org> wrote:
> >
> > On Wed, Jun 29, 2022 at 08:02:50PM -0700, Alexei Starovoitov wrote:
> > > On Wed, Jun 29, 2022 at 2:56 AM Christian Brauner <brauner@kernel.org> wrote:
> 
> [...]
> 
> > > > > > > > >
> > > > > > > > > Signed-off-by: KP Singh <kpsingh@kernel.org>
> > > > > > > > > ---
> > > > > > > > >  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
> > > > > > >
> > > > > > > [...]
> > > > > > >
> > > > > > > > > +SEC("lsm.s/bprm_committed_creds")
> > > > > > > > > +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > > > > > > > +{
> > > > > > > > > +     struct task_struct *current = bpf_get_current_task_btf();
> > > > > > > > > +     char dir_xattr_value[64] = {0};
> > > > > > > > > +     int xattr_sz = 0;
> > > > > > > > > +
> > > > > > > > > +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > > > > > > > +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> > > > > > > > > +                             dir_xattr_value, 64);
> > > > > > > >
> > > > > > > > Yeah, this isn't right. You're not accounting for the caller's userns
> > > > > > > > nor for the idmapped mount. If this is supposed to work you will need a
> > > > > > > > variant of vfs_getxattr() that takes the mount's idmapping into account
> > > > > > > > afaict. See what needs to happen after do_getxattr().
> > > > > > >
> > > > > > > Thanks for taking a look.
> > > > > > >
> 
> [...]
> 
> > > > > >
> > > > > > That will not be correct.
> > > > > > posix_acl_fix_xattr_to_user checking current_user_ns()
> > > > > > is checking random tasks that happen to be running
> > > > > > when lsm hook got invoked.
> > > > > >
> > > > > > KP,
> > > > > > we probably have to document clearly that neither 'current*'
> > > > > > should not be used here.
> > > > > > xattr_permission also makes little sense in this context.
> > > > > > If anything it can be a different kfunc if there is a use case,
> > > > > > but I don't see it yet.
> > > > > > bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
> > > > > > call it directly. It's the kernel that is doing its security thing.
> > > > >
> > > > > Right, but LSMs usually only retrieve their own xattr namespace (ima,
> > > > > selinux, smack) or they calculate hashes for xattrs based on the raw
> > > > > filesystem xattr values (evm).
> > > > >
> > > > > But this new bpf_getxattr() is different. It allows to retrieve _any_
> > > > > xattr in any security hook it can be attached to. So someone can write a
> > > > > bpf program that retrieves filesystem capabilites or posix acls. And
> > > > > these are xattrs that require higher-level vfs involvement to be
> > > > > sensible in most contexts.
> > > > >
> 
> [...]
> 
> > > > >
> > > > > This hooks a bpf-lsm program to the security_bprm_committed_creds()
> > > > > hook. It then retrieves the extended attributes of the file to be
> > > > > executed. The hook currently always retrieves the raw filesystem values.
> > > > >
> > > > > But for example any XATTR_NAME_CAPS filesystem capabilities that
> > > > > might've been stored will be taken into account during exec. And both
> > > > > the idmapping of the mount and the caller matter when determing whether
> > > > > they are used or not.
> > > > >
> > > > > But the current implementation of bpf_getxattr() just ignores both. It
> > > > > will always retrieve the raw filesystem values. So if one invokes this
> > > > > hook they're not actually retrieving the values as they are seen by
> > > > > fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
> > > > > some use-cases it might very well become a security issue in others if
> > > > > access decisions are always based on the raw values.
> > > > >
> > > > > I'm not well-versed in this so bear with me, please.
> > > >
> > > > If this is really just about retrieving the "security.bpf" xattr and no
> > > > other xattr then the bpf_getxattr() variant should somehow hard-code
> > > > that to ensure that no other xattrs can be retrieved, imho.
> > >
> > > All of these restrictions look very artificial to me.
> > > Especially the part "might very well become a security issue"
> > > just doesn't click.
> > > We're talking about bpf-lsm progs here that implement security.
> > > Can somebody implement a poor bpf-lsm that doesn't enforce
> > > any actual security? Sure. It's a code.
> >
> > The point is that with the current implementation of bpf_getxattr() you
> > are able to retrieve any xattrs and we have way less control over a
> > bpf-lsm program than we do over selinux which a simple git grep
> > __vfs_getxattr() is all we need.
> >
> > The thing is that with bpf_getxattr() as it stands it is currently
> > impossible to retrieve xattr values - specifically filesystem
> > capabilities and posix acls - and see them exactly like the code you're
> > trying to supervise is. And that seems very strange from a security
> > perspective. So if someone were to write
> >
> > SEC("lsm.s/bprm_creds_from_file")
> > void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > {
> >         struct task_struct *current = bpf_get_current_task_btf();
> >
> >         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> >                                 bprm->file->f_path.dentry->d_inode,
> >                                 XATTR_NAME_POSIX_ACL_ACCESS, ..);
> >         // or
> >         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> >                                 bprm->file->f_path.dentry->d_inode,
> >                                 XATTR_NAME_CAPS, ..);
> >
> > }
> >
> > they'd get the raw nscaps and the raw xattrs back. But now, as just a
> > tiny example, the nscaps->rootuid and the ->e_id fields in the posix
> > ACLs make zero sense in this context.
> >
> > And what's more there's no way for the bpf-lsm program to turn them into
> > something that makes sense in the context of the hook they are retrieved
> > in. It lacks all the necessary helpers to do so afaict.
> >
> > > No one complains about the usage of EXPORT_SYMBOL(__vfs_getxattr)
> > > in the existing LSMs like selinux.
> >
> > Selinux only cares about its own xattr namespace. It doesn't retrieve
> > fscaps or posix acls and it's not possible to write selinux programs
> > that do so. With the bpf-lsm that's very much possible.
> >
> > And if we'd notice selinux would start retrieving random xattrs we'd ask
> > the same questions we do here.
> >
> > > No one complains about its usage in out of tree LSMs.
> > > Is that a security issue? Of course not.
> > > __vfs_getxattr is a kernel mechanism that LSMs use to implement
> > > the security features they need.
> > > __vfs_getxattr as kfunc here is pretty much the same as EXPORT_SYMBOL
> > > with a big difference that it's EXPORT_SYMBOL_GPL.
> > > BPF land doesn't have an equivalent of non-gpl export and is not going
> > > to get one.
> 
> I want to reiterate what Alexei is saying here:
> 
> *Please* consider this as a simple wrapper around __vfs_getxattr
> with a limited attach surface and extra verification checks and
> and nothing else.
> 
> What you are saying is __vfs_getxattr does not make sense in some
> contexts. But kernel modules can still use it right?
> 
> The user is implementing an LSM, if they chose to do things that don't make
> sense, then they can surely cause a lot more harm:
> 
> SEC("lsm/bprm_check_security")
> int BPF_PROG(bprm_check, struct linux_binprm *bprm)
> {
>      return -EPERM;
> }
> 
> >
> > This discussion would probably be a lot shorter if this series were sent
> > with a proper explanation of how this supposed to work and what it's
> > used for.
> 
> It's currently scoped to BPF LSM (albeit limited to LSM for now)
> but it won't just be used in LSM programs but some (allow-listed)
> tracing programs too.
> 
> We want to leave the flexibility to the implementer of the LSM hooks. If the
> implementer choses to retrieve posix_acl_* we can also expose
> posix_acl_fix_xattr_to_user or a different kfunc that adds this logic too
> but that would be a separate kfunc (and a separate use-case).

No, sorry. That's what I feared and that's why I think this low-level
exposure of __vfs_getxattr() is wrong:
The posix_acl_fix_xattr_*() helpers, as well as the helpers like
get_file_caps() will not be exported. We're not going to export that
deeply internal vfs machinery. So I would NACK that. If you want that -
and that's what I'm saying here - you need to encapsulate this into your
vfs_*xattr() helper that you can call from your kfuncs.

> 
> >
> > A series without a cover letter and no detailed explanation in the
> > commit messages makes it quite hard to understand whether what is asked
> > can be acked or not.
> 
> As I mentioned in
> 
> https://lore.kernel.org/bpf/CACYkzJ70uqVJr5EnM0i03Lu+zkuSsXOXcOLQoUS6HZPqH=skpQ@mail.gmail.com/T/#m74f32bae800a97d5c2caf08cee4199d3ba48d76c
> 
> I will resend with a cover letter that has more details.

Thank you!

> 
> >
> > I'm just adding Serge and Casey to double-check here as the LSM stuff is
> > more up their alley. I can just look at this from the perspective of a
> > vfs person.
> >
> > If you have your eBPF meeting thing I'm also happy to jump on there next
> 
> Sure, we can discuss this during BPF office hours next week.

Sounds good.
KP Singh June 30, 2022, 1:29 p.m. UTC | #11
On Thu, Jun 30, 2022 at 3:26 PM Christian Brauner <brauner@kernel.org> wrote:
>
> On Thu, Jun 30, 2022 at 02:21:56PM +0200, KP Singh wrote:
> > On Thu, Jun 30, 2022 at 1:45 PM Christian Brauner <brauner@kernel.org> wrote:
> > >
> > > On Wed, Jun 29, 2022 at 08:02:50PM -0700, Alexei Starovoitov wrote:
> > > > On Wed, Jun 29, 2022 at 2:56 AM Christian Brauner <brauner@kernel.org> wrote:
> >
> > [...]
> >
> > > > > > > > > >
> > > > > > > > > > Signed-off-by: KP Singh <kpsingh@kernel.org>
> > > > > > > > > > ---
> > > > > > > > > >  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
> > > > > > > >
> > > > > > > > [...]
> > > > > > > >
> > > > > > > > > > +SEC("lsm.s/bprm_committed_creds")
> > > > > > > > > > +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > > > > > > > > +{
> > > > > > > > > > +     struct task_struct *current = bpf_get_current_task_btf();
> > > > > > > > > > +     char dir_xattr_value[64] = {0};
> > > > > > > > > > +     int xattr_sz = 0;
> > > > > > > > > > +
> > > > > > > > > > +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > > > > > > > > +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> > > > > > > > > > +                             dir_xattr_value, 64);
> > > > > > > > >
> > > > > > > > > Yeah, this isn't right. You're not accounting for the caller's userns
> > > > > > > > > nor for the idmapped mount. If this is supposed to work you will need a
> > > > > > > > > variant of vfs_getxattr() that takes the mount's idmapping into account
> > > > > > > > > afaict. See what needs to happen after do_getxattr().
> > > > > > > >
> > > > > > > > Thanks for taking a look.
> > > > > > > >
> >
> > [...]
> >
> > > > > > >
> > > > > > > That will not be correct.
> > > > > > > posix_acl_fix_xattr_to_user checking current_user_ns()
> > > > > > > is checking random tasks that happen to be running
> > > > > > > when lsm hook got invoked.
> > > > > > >
> > > > > > > KP,
> > > > > > > we probably have to document clearly that neither 'current*'
> > > > > > > should not be used here.
> > > > > > > xattr_permission also makes little sense in this context.
> > > > > > > If anything it can be a different kfunc if there is a use case,
> > > > > > > but I don't see it yet.
> > > > > > > bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
> > > > > > > call it directly. It's the kernel that is doing its security thing.
> > > > > >
> > > > > > Right, but LSMs usually only retrieve their own xattr namespace (ima,
> > > > > > selinux, smack) or they calculate hashes for xattrs based on the raw
> > > > > > filesystem xattr values (evm).
> > > > > >
> > > > > > But this new bpf_getxattr() is different. It allows to retrieve _any_
> > > > > > xattr in any security hook it can be attached to. So someone can write a
> > > > > > bpf program that retrieves filesystem capabilites or posix acls. And
> > > > > > these are xattrs that require higher-level vfs involvement to be
> > > > > > sensible in most contexts.
> > > > > >
> >
> > [...]
> >
> > > > > >
> > > > > > This hooks a bpf-lsm program to the security_bprm_committed_creds()
> > > > > > hook. It then retrieves the extended attributes of the file to be
> > > > > > executed. The hook currently always retrieves the raw filesystem values.
> > > > > >
> > > > > > But for example any XATTR_NAME_CAPS filesystem capabilities that
> > > > > > might've been stored will be taken into account during exec. And both
> > > > > > the idmapping of the mount and the caller matter when determing whether
> > > > > > they are used or not.
> > > > > >
> > > > > > But the current implementation of bpf_getxattr() just ignores both. It
> > > > > > will always retrieve the raw filesystem values. So if one invokes this
> > > > > > hook they're not actually retrieving the values as they are seen by
> > > > > > fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
> > > > > > some use-cases it might very well become a security issue in others if
> > > > > > access decisions are always based on the raw values.
> > > > > >
> > > > > > I'm not well-versed in this so bear with me, please.
> > > > >
> > > > > If this is really just about retrieving the "security.bpf" xattr and no
> > > > > other xattr then the bpf_getxattr() variant should somehow hard-code
> > > > > that to ensure that no other xattrs can be retrieved, imho.
> > > >
> > > > All of these restrictions look very artificial to me.
> > > > Especially the part "might very well become a security issue"
> > > > just doesn't click.
> > > > We're talking about bpf-lsm progs here that implement security.
> > > > Can somebody implement a poor bpf-lsm that doesn't enforce
> > > > any actual security? Sure. It's a code.
> > >
> > > The point is that with the current implementation of bpf_getxattr() you
> > > are able to retrieve any xattrs and we have way less control over a
> > > bpf-lsm program than we do over selinux which a simple git grep
> > > __vfs_getxattr() is all we need.
> > >
> > > The thing is that with bpf_getxattr() as it stands it is currently
> > > impossible to retrieve xattr values - specifically filesystem
> > > capabilities and posix acls - and see them exactly like the code you're
> > > trying to supervise is. And that seems very strange from a security
> > > perspective. So if someone were to write
> > >
> > > SEC("lsm.s/bprm_creds_from_file")
> > > void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > {
> > >         struct task_struct *current = bpf_get_current_task_btf();
> > >
> > >         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > >                                 bprm->file->f_path.dentry->d_inode,
> > >                                 XATTR_NAME_POSIX_ACL_ACCESS, ..);
> > >         // or
> > >         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > >                                 bprm->file->f_path.dentry->d_inode,
> > >                                 XATTR_NAME_CAPS, ..);
> > >
> > > }
> > >
> > > they'd get the raw nscaps and the raw xattrs back. But now, as just a
> > > tiny example, the nscaps->rootuid and the ->e_id fields in the posix
> > > ACLs make zero sense in this context.
> > >
> > > And what's more there's no way for the bpf-lsm program to turn them into
> > > something that makes sense in the context of the hook they are retrieved
> > > in. It lacks all the necessary helpers to do so afaict.
> > >
> > > > No one complains about the usage of EXPORT_SYMBOL(__vfs_getxattr)
> > > > in the existing LSMs like selinux.
> > >
> > > Selinux only cares about its own xattr namespace. It doesn't retrieve
> > > fscaps or posix acls and it's not possible to write selinux programs
> > > that do so. With the bpf-lsm that's very much possible.
> > >
> > > And if we'd notice selinux would start retrieving random xattrs we'd ask
> > > the same questions we do here.
> > >
> > > > No one complains about its usage in out of tree LSMs.
> > > > Is that a security issue? Of course not.
> > > > __vfs_getxattr is a kernel mechanism that LSMs use to implement
> > > > the security features they need.
> > > > __vfs_getxattr as kfunc here is pretty much the same as EXPORT_SYMBOL
> > > > with a big difference that it's EXPORT_SYMBOL_GPL.
> > > > BPF land doesn't have an equivalent of non-gpl export and is not going
> > > > to get one.
> >
> > I want to reiterate what Alexei is saying here:
> >
> > *Please* consider this as a simple wrapper around __vfs_getxattr
> > with a limited attach surface and extra verification checks and
> > and nothing else.
> >
> > What you are saying is __vfs_getxattr does not make sense in some
> > contexts. But kernel modules can still use it right?
> >
> > The user is implementing an LSM, if they chose to do things that don't make
> > sense, then they can surely cause a lot more harm:
> >
> > SEC("lsm/bprm_check_security")
> > int BPF_PROG(bprm_check, struct linux_binprm *bprm)
> > {
> >      return -EPERM;
> > }
> >
> > >
> > > This discussion would probably be a lot shorter if this series were sent
> > > with a proper explanation of how this supposed to work and what it's
> > > used for.
> >
> > It's currently scoped to BPF LSM (albeit limited to LSM for now)
> > but it won't just be used in LSM programs but some (allow-listed)
> > tracing programs too.
> >
> > We want to leave the flexibility to the implementer of the LSM hooks. If the
> > implementer choses to retrieve posix_acl_* we can also expose
> > posix_acl_fix_xattr_to_user or a different kfunc that adds this logic too
> > but that would be a separate kfunc (and a separate use-case).
>
> No, sorry. That's what I feared and that's why I think this low-level
> exposure of __vfs_getxattr() is wrong:
> The posix_acl_fix_xattr_*() helpers, as well as the helpers like
> get_file_caps() will not be exported. We're not going to export that

I don't want to expose them and I don't want any others to be
exposed either.

> deeply internal vfs machinery. So I would NACK that. If you want that -
> and that's what I'm saying here - you need to encapsulate this into your
> vfs_*xattr() helper that you can call from your kfuncs.

It seems like __vfs_getxattr is already exposed and does the wrong thing in
some contexts, why can't we just "fix" __vfs_getxattr then?


- KP

>
> >
> > >
> > > A series without a cover letter and no detailed explanation in the
> > > commit messages makes it quite hard to understand whether what is asked
> > > can be acked or not.
> >
> > As I mentioned in
> >
> > https://lore.kernel.org/bpf/CACYkzJ70uqVJr5EnM0i03Lu+zkuSsXOXcOLQoUS6HZPqH=skpQ@mail.gmail.com/T/#m74f32bae800a97d5c2caf08cee4199d3ba48d76c
> >
> > I will resend with a cover letter that has more details.
>
> Thank you!
>
> >
> > >
> > > I'm just adding Serge and Casey to double-check here as the LSM stuff is
> > > more up their alley. I can just look at this from the perspective of a
> > > vfs person.
> > >
> > > If you have your eBPF meeting thing I'm also happy to jump on there next
> >
> > Sure, we can discuss this during BPF office hours next week.
>
> Sounds good.
Christian Brauner June 30, 2022, 1:47 p.m. UTC | #12
On Thu, Jun 30, 2022 at 03:29:53PM +0200, KP Singh wrote:
> On Thu, Jun 30, 2022 at 3:26 PM Christian Brauner <brauner@kernel.org> wrote:
> >
> > On Thu, Jun 30, 2022 at 02:21:56PM +0200, KP Singh wrote:
> > > On Thu, Jun 30, 2022 at 1:45 PM Christian Brauner <brauner@kernel.org> wrote:
> > > >
> > > > On Wed, Jun 29, 2022 at 08:02:50PM -0700, Alexei Starovoitov wrote:
> > > > > On Wed, Jun 29, 2022 at 2:56 AM Christian Brauner <brauner@kernel.org> wrote:
> > >
> > > [...]
> > >
> > > > > > > > > > >
> > > > > > > > > > > Signed-off-by: KP Singh <kpsingh@kernel.org>
> > > > > > > > > > > ---
> > > > > > > > > > >  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
> > > > > > > > >
> > > > > > > > > [...]
> > > > > > > > >
> > > > > > > > > > > +SEC("lsm.s/bprm_committed_creds")
> > > > > > > > > > > +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > > > > > > > > > +{
> > > > > > > > > > > +     struct task_struct *current = bpf_get_current_task_btf();
> > > > > > > > > > > +     char dir_xattr_value[64] = {0};
> > > > > > > > > > > +     int xattr_sz = 0;
> > > > > > > > > > > +
> > > > > > > > > > > +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > > > > > > > > > +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> > > > > > > > > > > +                             dir_xattr_value, 64);
> > > > > > > > > >
> > > > > > > > > > Yeah, this isn't right. You're not accounting for the caller's userns
> > > > > > > > > > nor for the idmapped mount. If this is supposed to work you will need a
> > > > > > > > > > variant of vfs_getxattr() that takes the mount's idmapping into account
> > > > > > > > > > afaict. See what needs to happen after do_getxattr().
> > > > > > > > >
> > > > > > > > > Thanks for taking a look.
> > > > > > > > >
> > >
> > > [...]
> > >
> > > > > > > >
> > > > > > > > That will not be correct.
> > > > > > > > posix_acl_fix_xattr_to_user checking current_user_ns()
> > > > > > > > is checking random tasks that happen to be running
> > > > > > > > when lsm hook got invoked.
> > > > > > > >
> > > > > > > > KP,
> > > > > > > > we probably have to document clearly that neither 'current*'
> > > > > > > > should not be used here.
> > > > > > > > xattr_permission also makes little sense in this context.
> > > > > > > > If anything it can be a different kfunc if there is a use case,
> > > > > > > > but I don't see it yet.
> > > > > > > > bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
> > > > > > > > call it directly. It's the kernel that is doing its security thing.
> > > > > > >
> > > > > > > Right, but LSMs usually only retrieve their own xattr namespace (ima,
> > > > > > > selinux, smack) or they calculate hashes for xattrs based on the raw
> > > > > > > filesystem xattr values (evm).
> > > > > > >
> > > > > > > But this new bpf_getxattr() is different. It allows to retrieve _any_
> > > > > > > xattr in any security hook it can be attached to. So someone can write a
> > > > > > > bpf program that retrieves filesystem capabilites or posix acls. And
> > > > > > > these are xattrs that require higher-level vfs involvement to be
> > > > > > > sensible in most contexts.
> > > > > > >
> > >
> > > [...]
> > >
> > > > > > >
> > > > > > > This hooks a bpf-lsm program to the security_bprm_committed_creds()
> > > > > > > hook. It then retrieves the extended attributes of the file to be
> > > > > > > executed. The hook currently always retrieves the raw filesystem values.
> > > > > > >
> > > > > > > But for example any XATTR_NAME_CAPS filesystem capabilities that
> > > > > > > might've been stored will be taken into account during exec. And both
> > > > > > > the idmapping of the mount and the caller matter when determing whether
> > > > > > > they are used or not.
> > > > > > >
> > > > > > > But the current implementation of bpf_getxattr() just ignores both. It
> > > > > > > will always retrieve the raw filesystem values. So if one invokes this
> > > > > > > hook they're not actually retrieving the values as they are seen by
> > > > > > > fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
> > > > > > > some use-cases it might very well become a security issue in others if
> > > > > > > access decisions are always based on the raw values.
> > > > > > >
> > > > > > > I'm not well-versed in this so bear with me, please.
> > > > > >
> > > > > > If this is really just about retrieving the "security.bpf" xattr and no
> > > > > > other xattr then the bpf_getxattr() variant should somehow hard-code
> > > > > > that to ensure that no other xattrs can be retrieved, imho.
> > > > >
> > > > > All of these restrictions look very artificial to me.
> > > > > Especially the part "might very well become a security issue"
> > > > > just doesn't click.
> > > > > We're talking about bpf-lsm progs here that implement security.
> > > > > Can somebody implement a poor bpf-lsm that doesn't enforce
> > > > > any actual security? Sure. It's a code.
> > > >
> > > > The point is that with the current implementation of bpf_getxattr() you
> > > > are able to retrieve any xattrs and we have way less control over a
> > > > bpf-lsm program than we do over selinux which a simple git grep
> > > > __vfs_getxattr() is all we need.
> > > >
> > > > The thing is that with bpf_getxattr() as it stands it is currently
> > > > impossible to retrieve xattr values - specifically filesystem
> > > > capabilities and posix acls - and see them exactly like the code you're
> > > > trying to supervise is. And that seems very strange from a security
> > > > perspective. So if someone were to write
> > > >
> > > > SEC("lsm.s/bprm_creds_from_file")
> > > > void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > > {
> > > >         struct task_struct *current = bpf_get_current_task_btf();
> > > >
> > > >         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > >                                 bprm->file->f_path.dentry->d_inode,
> > > >                                 XATTR_NAME_POSIX_ACL_ACCESS, ..);
> > > >         // or
> > > >         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > >                                 bprm->file->f_path.dentry->d_inode,
> > > >                                 XATTR_NAME_CAPS, ..);
> > > >
> > > > }
> > > >
> > > > they'd get the raw nscaps and the raw xattrs back. But now, as just a
> > > > tiny example, the nscaps->rootuid and the ->e_id fields in the posix
> > > > ACLs make zero sense in this context.
> > > >
> > > > And what's more there's no way for the bpf-lsm program to turn them into
> > > > something that makes sense in the context of the hook they are retrieved
> > > > in. It lacks all the necessary helpers to do so afaict.
> > > >
> > > > > No one complains about the usage of EXPORT_SYMBOL(__vfs_getxattr)
> > > > > in the existing LSMs like selinux.
> > > >
> > > > Selinux only cares about its own xattr namespace. It doesn't retrieve
> > > > fscaps or posix acls and it's not possible to write selinux programs
> > > > that do so. With the bpf-lsm that's very much possible.
> > > >
> > > > And if we'd notice selinux would start retrieving random xattrs we'd ask
> > > > the same questions we do here.
> > > >
> > > > > No one complains about its usage in out of tree LSMs.
> > > > > Is that a security issue? Of course not.
> > > > > __vfs_getxattr is a kernel mechanism that LSMs use to implement
> > > > > the security features they need.
> > > > > __vfs_getxattr as kfunc here is pretty much the same as EXPORT_SYMBOL
> > > > > with a big difference that it's EXPORT_SYMBOL_GPL.
> > > > > BPF land doesn't have an equivalent of non-gpl export and is not going
> > > > > to get one.
> > >
> > > I want to reiterate what Alexei is saying here:
> > >
> > > *Please* consider this as a simple wrapper around __vfs_getxattr
> > > with a limited attach surface and extra verification checks and
> > > and nothing else.
> > >
> > > What you are saying is __vfs_getxattr does not make sense in some
> > > contexts. But kernel modules can still use it right?
> > >
> > > The user is implementing an LSM, if they chose to do things that don't make
> > > sense, then they can surely cause a lot more harm:
> > >
> > > SEC("lsm/bprm_check_security")
> > > int BPF_PROG(bprm_check, struct linux_binprm *bprm)
> > > {
> > >      return -EPERM;
> > > }
> > >
> > > >
> > > > This discussion would probably be a lot shorter if this series were sent
> > > > with a proper explanation of how this supposed to work and what it's
> > > > used for.
> > >
> > > It's currently scoped to BPF LSM (albeit limited to LSM for now)
> > > but it won't just be used in LSM programs but some (allow-listed)
> > > tracing programs too.
> > >
> > > We want to leave the flexibility to the implementer of the LSM hooks. If the
> > > implementer choses to retrieve posix_acl_* we can also expose
> > > posix_acl_fix_xattr_to_user or a different kfunc that adds this logic too
> > > but that would be a separate kfunc (and a separate use-case).
> >
> > No, sorry. That's what I feared and that's why I think this low-level
> > exposure of __vfs_getxattr() is wrong:
> > The posix_acl_fix_xattr_*() helpers, as well as the helpers like
> > get_file_caps() will not be exported. We're not going to export that
> 
> I don't want to expose them and I don't want any others to be
> exposed either.
> 
> > deeply internal vfs machinery. So I would NACK that. If you want that -
> > and that's what I'm saying here - you need to encapsulate this into your
> > vfs_*xattr() helper that you can call from your kfuncs.
> 
> It seems like __vfs_getxattr is already exposed and does the wrong thing in
> some contexts, why can't we just "fix" __vfs_getxattr then?

To me having either a version of bpf_getxattr() that restricts access to
certain xattrs or a version that takes care to perform the neccesary
translations is what seems to make the most sense. I suggested that in
one of my first mails.

The one thing where the way the xattrs are retrieved really matters is
for vfscaps (see get_vfs_caps_from_disk()) you really need something
like that function in order for vfs caps to make any sense and be
interpretable by the user of the hook.

But again, I might just misunderstand the context here and for the
bpf-lsm all of this isn't really a concern. If your new series comes out
I'll try to get more into the wider context.
If the security folks are happy with this then I won't argue.
Christian Brauner June 30, 2022, 2:37 p.m. UTC | #13
On Thu, Jun 30, 2022 at 03:47:02PM +0200, Christian Brauner wrote:
> On Thu, Jun 30, 2022 at 03:29:53PM +0200, KP Singh wrote:
> > On Thu, Jun 30, 2022 at 3:26 PM Christian Brauner <brauner@kernel.org> wrote:
> > >
> > > On Thu, Jun 30, 2022 at 02:21:56PM +0200, KP Singh wrote:
> > > > On Thu, Jun 30, 2022 at 1:45 PM Christian Brauner <brauner@kernel.org> wrote:
> > > > >
> > > > > On Wed, Jun 29, 2022 at 08:02:50PM -0700, Alexei Starovoitov wrote:
> > > > > > On Wed, Jun 29, 2022 at 2:56 AM Christian Brauner <brauner@kernel.org> wrote:
> > > >
> > > > [...]
> > > >
> > > > > > > > > > > >
> > > > > > > > > > > > Signed-off-by: KP Singh <kpsingh@kernel.org>
> > > > > > > > > > > > ---
> > > > > > > > > > > >  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
> > > > > > > > > >
> > > > > > > > > > [...]
> > > > > > > > > >
> > > > > > > > > > > > +SEC("lsm.s/bprm_committed_creds")
> > > > > > > > > > > > +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > > > > > > > > > > +{
> > > > > > > > > > > > +     struct task_struct *current = bpf_get_current_task_btf();
> > > > > > > > > > > > +     char dir_xattr_value[64] = {0};
> > > > > > > > > > > > +     int xattr_sz = 0;
> > > > > > > > > > > > +
> > > > > > > > > > > > +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > > > > > > > > > > +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> > > > > > > > > > > > +                             dir_xattr_value, 64);
> > > > > > > > > > >
> > > > > > > > > > > Yeah, this isn't right. You're not accounting for the caller's userns
> > > > > > > > > > > nor for the idmapped mount. If this is supposed to work you will need a
> > > > > > > > > > > variant of vfs_getxattr() that takes the mount's idmapping into account
> > > > > > > > > > > afaict. See what needs to happen after do_getxattr().
> > > > > > > > > >
> > > > > > > > > > Thanks for taking a look.
> > > > > > > > > >
> > > >
> > > > [...]
> > > >
> > > > > > > > >
> > > > > > > > > That will not be correct.
> > > > > > > > > posix_acl_fix_xattr_to_user checking current_user_ns()
> > > > > > > > > is checking random tasks that happen to be running
> > > > > > > > > when lsm hook got invoked.
> > > > > > > > >
> > > > > > > > > KP,
> > > > > > > > > we probably have to document clearly that neither 'current*'
> > > > > > > > > should not be used here.
> > > > > > > > > xattr_permission also makes little sense in this context.
> > > > > > > > > If anything it can be a different kfunc if there is a use case,
> > > > > > > > > but I don't see it yet.
> > > > > > > > > bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
> > > > > > > > > call it directly. It's the kernel that is doing its security thing.
> > > > > > > >
> > > > > > > > Right, but LSMs usually only retrieve their own xattr namespace (ima,
> > > > > > > > selinux, smack) or they calculate hashes for xattrs based on the raw
> > > > > > > > filesystem xattr values (evm).
> > > > > > > >
> > > > > > > > But this new bpf_getxattr() is different. It allows to retrieve _any_
> > > > > > > > xattr in any security hook it can be attached to. So someone can write a
> > > > > > > > bpf program that retrieves filesystem capabilites or posix acls. And
> > > > > > > > these are xattrs that require higher-level vfs involvement to be
> > > > > > > > sensible in most contexts.
> > > > > > > >
> > > >
> > > > [...]
> > > >
> > > > > > > >
> > > > > > > > This hooks a bpf-lsm program to the security_bprm_committed_creds()
> > > > > > > > hook. It then retrieves the extended attributes of the file to be
> > > > > > > > executed. The hook currently always retrieves the raw filesystem values.
> > > > > > > >
> > > > > > > > But for example any XATTR_NAME_CAPS filesystem capabilities that
> > > > > > > > might've been stored will be taken into account during exec. And both
> > > > > > > > the idmapping of the mount and the caller matter when determing whether
> > > > > > > > they are used or not.
> > > > > > > >
> > > > > > > > But the current implementation of bpf_getxattr() just ignores both. It
> > > > > > > > will always retrieve the raw filesystem values. So if one invokes this
> > > > > > > > hook they're not actually retrieving the values as they are seen by
> > > > > > > > fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
> > > > > > > > some use-cases it might very well become a security issue in others if
> > > > > > > > access decisions are always based on the raw values.
> > > > > > > >
> > > > > > > > I'm not well-versed in this so bear with me, please.
> > > > > > >
> > > > > > > If this is really just about retrieving the "security.bpf" xattr and no
> > > > > > > other xattr then the bpf_getxattr() variant should somehow hard-code
> > > > > > > that to ensure that no other xattrs can be retrieved, imho.
> > > > > >
> > > > > > All of these restrictions look very artificial to me.
> > > > > > Especially the part "might very well become a security issue"
> > > > > > just doesn't click.
> > > > > > We're talking about bpf-lsm progs here that implement security.
> > > > > > Can somebody implement a poor bpf-lsm that doesn't enforce
> > > > > > any actual security? Sure. It's a code.
> > > > >
> > > > > The point is that with the current implementation of bpf_getxattr() you
> > > > > are able to retrieve any xattrs and we have way less control over a
> > > > > bpf-lsm program than we do over selinux which a simple git grep
> > > > > __vfs_getxattr() is all we need.
> > > > >
> > > > > The thing is that with bpf_getxattr() as it stands it is currently
> > > > > impossible to retrieve xattr values - specifically filesystem
> > > > > capabilities and posix acls - and see them exactly like the code you're
> > > > > trying to supervise is. And that seems very strange from a security
> > > > > perspective. So if someone were to write
> > > > >
> > > > > SEC("lsm.s/bprm_creds_from_file")
> > > > > void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > > > > {
> > > > >         struct task_struct *current = bpf_get_current_task_btf();
> > > > >
> > > > >         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > > >                                 bprm->file->f_path.dentry->d_inode,
> > > > >                                 XATTR_NAME_POSIX_ACL_ACCESS, ..);
> > > > >         // or
> > > > >         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > > > >                                 bprm->file->f_path.dentry->d_inode,
> > > > >                                 XATTR_NAME_CAPS, ..);
> > > > >
> > > > > }
> > > > >
> > > > > they'd get the raw nscaps and the raw xattrs back. But now, as just a
> > > > > tiny example, the nscaps->rootuid and the ->e_id fields in the posix
> > > > > ACLs make zero sense in this context.
> > > > >
> > > > > And what's more there's no way for the bpf-lsm program to turn them into
> > > > > something that makes sense in the context of the hook they are retrieved
> > > > > in. It lacks all the necessary helpers to do so afaict.
> > > > >
> > > > > > No one complains about the usage of EXPORT_SYMBOL(__vfs_getxattr)
> > > > > > in the existing LSMs like selinux.
> > > > >
> > > > > Selinux only cares about its own xattr namespace. It doesn't retrieve
> > > > > fscaps or posix acls and it's not possible to write selinux programs
> > > > > that do so. With the bpf-lsm that's very much possible.
> > > > >
> > > > > And if we'd notice selinux would start retrieving random xattrs we'd ask
> > > > > the same questions we do here.
> > > > >
> > > > > > No one complains about its usage in out of tree LSMs.
> > > > > > Is that a security issue? Of course not.
> > > > > > __vfs_getxattr is a kernel mechanism that LSMs use to implement
> > > > > > the security features they need.
> > > > > > __vfs_getxattr as kfunc here is pretty much the same as EXPORT_SYMBOL
> > > > > > with a big difference that it's EXPORT_SYMBOL_GPL.
> > > > > > BPF land doesn't have an equivalent of non-gpl export and is not going
> > > > > > to get one.
> > > >
> > > > I want to reiterate what Alexei is saying here:
> > > >
> > > > *Please* consider this as a simple wrapper around __vfs_getxattr
> > > > with a limited attach surface and extra verification checks and
> > > > and nothing else.
> > > >
> > > > What you are saying is __vfs_getxattr does not make sense in some
> > > > contexts. But kernel modules can still use it right?
> > > >
> > > > The user is implementing an LSM, if they chose to do things that don't make
> > > > sense, then they can surely cause a lot more harm:
> > > >
> > > > SEC("lsm/bprm_check_security")
> > > > int BPF_PROG(bprm_check, struct linux_binprm *bprm)
> > > > {
> > > >      return -EPERM;
> > > > }
> > > >
> > > > >
> > > > > This discussion would probably be a lot shorter if this series were sent
> > > > > with a proper explanation of how this supposed to work and what it's
> > > > > used for.
> > > >
> > > > It's currently scoped to BPF LSM (albeit limited to LSM for now)
> > > > but it won't just be used in LSM programs but some (allow-listed)
> > > > tracing programs too.
> > > >
> > > > We want to leave the flexibility to the implementer of the LSM hooks. If the
> > > > implementer choses to retrieve posix_acl_* we can also expose
> > > > posix_acl_fix_xattr_to_user or a different kfunc that adds this logic too
> > > > but that would be a separate kfunc (and a separate use-case).
> > >
> > > No, sorry. That's what I feared and that's why I think this low-level
> > > exposure of __vfs_getxattr() is wrong:
> > > The posix_acl_fix_xattr_*() helpers, as well as the helpers like
> > > get_file_caps() will not be exported. We're not going to export that
> > 
> > I don't want to expose them and I don't want any others to be
> > exposed either.
> > 
> > > deeply internal vfs machinery. So I would NACK that. If you want that -
> > > and that's what I'm saying here - you need to encapsulate this into your
> > > vfs_*xattr() helper that you can call from your kfuncs.
> > 
> > It seems like __vfs_getxattr is already exposed and does the wrong thing in
> > some contexts, why can't we just "fix" __vfs_getxattr then?
> 
> To me having either a version of bpf_getxattr() that restricts access to
> certain xattrs or a version that takes care to perform the neccesary
> translations is what seems to make the most sense. I suggested that in
> one of my first mails.
> 
> The one thing where the way the xattrs are retrieved really matters is
> for vfscaps (see get_vfs_caps_from_disk()) you really need something
> like that function in order for vfs caps to make any sense and be
> interpretable by the user of the hook.
> 
> But again, I might just misunderstand the context here and for the
> bpf-lsm all of this isn't really a concern. If your new series comes out
> I'll try to get more into the wider context.
> If the security folks are happy with this then I won't argue.

I think for posix acls you're actually fine since you never report
anything to userspace. So one of your bpf-lsms might reasonably
interpret this.

But for vfscaps you need them fixed up. That's what's done in
vfs_getxattr() via xattr_getsecurity() which calls into
security_inode_getsecurity() and then into cap_inode_getsecurity() which
does the conversion from the on-disk into the proper in-memory
representation. And that's nasty because fscaps are versioned depending
on whether they are namespaced or not.
Casey Schaufler June 30, 2022, 4:10 p.m. UTC | #14
On 6/30/2022 6:47 AM, Christian Brauner wrote:
> On Thu, Jun 30, 2022 at 03:29:53PM +0200, KP Singh wrote:
>> On Thu, Jun 30, 2022 at 3:26 PM Christian Brauner <brauner@kernel.org> wrote:
>>> On Thu, Jun 30, 2022 at 02:21:56PM +0200, KP Singh wrote:
>>>> On Thu, Jun 30, 2022 at 1:45 PM Christian Brauner <brauner@kernel.org> wrote:
>>>>> On Wed, Jun 29, 2022 at 08:02:50PM -0700, Alexei Starovoitov wrote:
>>>>>> On Wed, Jun 29, 2022 at 2:56 AM Christian Brauner <brauner@kernel.org> wrote:
>>>> [...]
>>>>
>>>>>>>>>>>> Signed-off-by: KP Singh <kpsingh@kernel.org>
>>>>>>>>>>>> ---
>>>>>>>>>>>>  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
>>>>>>>>>> [...]
>>>>>>>>>>
>>>>>>>>>>>> +SEC("lsm.s/bprm_committed_creds")
>>>>>>>>>>>> +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
>>>>>>>>>>>> +{
>>>>>>>>>>>> +     struct task_struct *current = bpf_get_current_task_btf();
>>>>>>>>>>>> +     char dir_xattr_value[64] = {0};
>>>>>>>>>>>> +     int xattr_sz = 0;
>>>>>>>>>>>> +
>>>>>>>>>>>> +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
>>>>>>>>>>>> +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
>>>>>>>>>>>> +                             dir_xattr_value, 64);
>>>>>>>>>>> Yeah, this isn't right. You're not accounting for the caller's userns
>>>>>>>>>>> nor for the idmapped mount. If this is supposed to work you will need a
>>>>>>>>>>> variant of vfs_getxattr() that takes the mount's idmapping into account
>>>>>>>>>>> afaict. See what needs to happen after do_getxattr().
>>>>>>>>>> Thanks for taking a look.
>>>>>>>>>>
>>>> [...]
>>>>
>>>>>>>>> That will not be correct.
>>>>>>>>> posix_acl_fix_xattr_to_user checking current_user_ns()
>>>>>>>>> is checking random tasks that happen to be running
>>>>>>>>> when lsm hook got invoked.
>>>>>>>>>
>>>>>>>>> KP,
>>>>>>>>> we probably have to document clearly that neither 'current*'
>>>>>>>>> should not be used here.
>>>>>>>>> xattr_permission also makes little sense in this context.
>>>>>>>>> If anything it can be a different kfunc if there is a use case,
>>>>>>>>> but I don't see it yet.
>>>>>>>>> bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
>>>>>>>>> call it directly. It's the kernel that is doing its security thing.
>>>>>>>> Right, but LSMs usually only retrieve their own xattr namespace (ima,
>>>>>>>> selinux, smack) or they calculate hashes for xattrs based on the raw
>>>>>>>> filesystem xattr values (evm).
>>>>>>>>
>>>>>>>> But this new bpf_getxattr() is different. It allows to retrieve _any_
>>>>>>>> xattr in any security hook it can be attached to. So someone can write a
>>>>>>>> bpf program that retrieves filesystem capabilites or posix acls. And
>>>>>>>> these are xattrs that require higher-level vfs involvement to be
>>>>>>>> sensible in most contexts.
>>>>>>>>
>>>> [...]
>>>>
>>>>>>>> This hooks a bpf-lsm program to the security_bprm_committed_creds()
>>>>>>>> hook. It then retrieves the extended attributes of the file to be
>>>>>>>> executed. The hook currently always retrieves the raw filesystem values.
>>>>>>>>
>>>>>>>> But for example any XATTR_NAME_CAPS filesystem capabilities that
>>>>>>>> might've been stored will be taken into account during exec. And both
>>>>>>>> the idmapping of the mount and the caller matter when determing whether
>>>>>>>> they are used or not.
>>>>>>>>
>>>>>>>> But the current implementation of bpf_getxattr() just ignores both. It
>>>>>>>> will always retrieve the raw filesystem values. So if one invokes this
>>>>>>>> hook they're not actually retrieving the values as they are seen by
>>>>>>>> fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
>>>>>>>> some use-cases it might very well become a security issue in others if
>>>>>>>> access decisions are always based on the raw values.
>>>>>>>>
>>>>>>>> I'm not well-versed in this so bear with me, please.
>>>>>>> If this is really just about retrieving the "security.bpf" xattr and no
>>>>>>> other xattr then the bpf_getxattr() variant should somehow hard-code
>>>>>>> that to ensure that no other xattrs can be retrieved, imho.
>>>>>> All of these restrictions look very artificial to me.
>>>>>> Especially the part "might very well become a security issue"
>>>>>> just doesn't click.
>>>>>> We're talking about bpf-lsm progs here that implement security.
>>>>>> Can somebody implement a poor bpf-lsm that doesn't enforce
>>>>>> any actual security? Sure. It's a code.
>>>>> The point is that with the current implementation of bpf_getxattr() you
>>>>> are able to retrieve any xattrs and we have way less control over a
>>>>> bpf-lsm program than we do over selinux which a simple git grep
>>>>> __vfs_getxattr() is all we need.
>>>>>
>>>>> The thing is that with bpf_getxattr() as it stands it is currently
>>>>> impossible to retrieve xattr values - specifically filesystem
>>>>> capabilities and posix acls - and see them exactly like the code you're
>>>>> trying to supervise is. And that seems very strange from a security
>>>>> perspective. So if someone were to write
>>>>>
>>>>> SEC("lsm.s/bprm_creds_from_file")
>>>>> void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
>>>>> {
>>>>>         struct task_struct *current = bpf_get_current_task_btf();
>>>>>
>>>>>         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
>>>>>                                 bprm->file->f_path.dentry->d_inode,
>>>>>                                 XATTR_NAME_POSIX_ACL_ACCESS, ..);
>>>>>         // or
>>>>>         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
>>>>>                                 bprm->file->f_path.dentry->d_inode,
>>>>>                                 XATTR_NAME_CAPS, ..);
>>>>>
>>>>> }
>>>>>
>>>>> they'd get the raw nscaps and the raw xattrs back. But now, as just a
>>>>> tiny example, the nscaps->rootuid and the ->e_id fields in the posix
>>>>> ACLs make zero sense in this context.
>>>>>
>>>>> And what's more there's no way for the bpf-lsm program to turn them into
>>>>> something that makes sense in the context of the hook they are retrieved
>>>>> in. It lacks all the necessary helpers to do so afaict.
>>>>>
>>>>>> No one complains about the usage of EXPORT_SYMBOL(__vfs_getxattr)
>>>>>> in the existing LSMs like selinux.
>>>>> Selinux only cares about its own xattr namespace. It doesn't retrieve
>>>>> fscaps or posix acls and it's not possible to write selinux programs
>>>>> that do so. With the bpf-lsm that's very much possible.
>>>>>
>>>>> And if we'd notice selinux would start retrieving random xattrs we'd ask
>>>>> the same questions we do here.
>>>>>
>>>>>> No one complains about its usage in out of tree LSMs.
>>>>>> Is that a security issue? Of course not.
>>>>>> __vfs_getxattr is a kernel mechanism that LSMs use to implement
>>>>>> the security features they need.
>>>>>> __vfs_getxattr as kfunc here is pretty much the same as EXPORT_SYMBOL
>>>>>> with a big difference that it's EXPORT_SYMBOL_GPL.
>>>>>> BPF land doesn't have an equivalent of non-gpl export and is not going
>>>>>> to get one.
>>>> I want to reiterate what Alexei is saying here:
>>>>
>>>> *Please* consider this as a simple wrapper around __vfs_getxattr
>>>> with a limited attach surface and extra verification checks and
>>>> and nothing else.
>>>>
>>>> What you are saying is __vfs_getxattr does not make sense in some
>>>> contexts. But kernel modules can still use it right?
>>>>
>>>> The user is implementing an LSM, if they chose to do things that don't make
>>>> sense, then they can surely cause a lot more harm:
>>>>
>>>> SEC("lsm/bprm_check_security")
>>>> int BPF_PROG(bprm_check, struct linux_binprm *bprm)
>>>> {
>>>>      return -EPERM;
>>>> }
>>>>
>>>>> This discussion would probably be a lot shorter if this series were sent
>>>>> with a proper explanation of how this supposed to work and what it's
>>>>> used for.
>>>> It's currently scoped to BPF LSM (albeit limited to LSM for now)
>>>> but it won't just be used in LSM programs but some (allow-listed)
>>>> tracing programs too.
>>>>
>>>> We want to leave the flexibility to the implementer of the LSM hooks. If the
>>>> implementer choses to retrieve posix_acl_* we can also expose
>>>> posix_acl_fix_xattr_to_user or a different kfunc that adds this logic too
>>>> but that would be a separate kfunc (and a separate use-case).
>>> No, sorry. That's what I feared and that's why I think this low-level
>>> exposure of __vfs_getxattr() is wrong:
>>> The posix_acl_fix_xattr_*() helpers, as well as the helpers like
>>> get_file_caps() will not be exported. We're not going to export that
>> I don't want to expose them and I don't want any others to be
>> exposed either.
>>
>>> deeply internal vfs machinery. So I would NACK that. If you want that -
>>> and that's what I'm saying here - you need to encapsulate this into your
>>> vfs_*xattr() helper that you can call from your kfuncs.
>> It seems like __vfs_getxattr is already exposed and does the wrong thing in
>> some contexts, why can't we just "fix" __vfs_getxattr then?
> To me having either a version of bpf_getxattr() that restricts access to
> certain xattrs or a version that takes care to perform the neccesary
> translations is what seems to make the most sense. I suggested that in
> one of my first mails.
>
> The one thing where the way the xattrs are retrieved really matters is
> for vfscaps (see get_vfs_caps_from_disk()) you really need something
> like that function in order for vfs caps to make any sense and be
> interpretable by the user of the hook.
>
> But again, I might just misunderstand the context here and for the
> bpf-lsm all of this isn't really a concern. If your new series comes out
> I'll try to get more into the wider context.
> If the security folks are happy with this then I won't argue.

A security module (BPF) using another security module's (Smack)
xattrs without that module's (Smack) explicit approval would be
considered extremely rude.  Smack and SELinux use published interfaces
of the capability security module, but never access the capability
attributes directly. The details of a security module's implementation
are not a factor. The fact that BPF uses loadable programs as opposed
to loadable policy is not relevant. The only security.xattr values
that the BPF security module should allow the programs it runs to
access are the ones it is managing. If you decided to create an eBPF
implementation of SELinux you would still have to use attributes
specific to the BPF security module. If, on the other hand, you wanted
to extend Smack using eBPF programs, and the Smack maintainer liked
the idea, it would be OK for the BPF security module to access some
of the security.SMACK64 attributes.

I want it to be clear that BPF is a Linux Security Module (LSM) and
a collection of eBPF programs is *not* an LSM. BPF is responsible
for being a good kernel citizen, and must ensure that it does not
allow a set of configuration data that violates proper behavior.
You can't write an SELinux policy that monster-mashes an ACL.
You can't allow BPF to permit that either. You can't count on the
good intentions, wisdom or skill of the author of an unreviewed,
out of tree, eBPF program. I believe that this was understood during
the review process of the BPF LSM.
Amir Goldstein June 30, 2022, 4:28 p.m. UTC | #15
> >
> > This discussion would probably be a lot shorter if this series were sent
> > with a proper explanation of how this supposed to work and what it's
> > used for.
>
> It's currently scoped to BPF LSM (albeit limited to LSM for now)
> but it won't just be used in LSM programs but some (allow-listed)
> tracing programs too.
>

KP,

Without taking sides in the discussion about the security aspect of
bpf_getxattr(),
I wanted to say that we have plans to add BPF hooks for fanotify event
filters and
AFAIK Alessio's team is working on adding BPF hooks for FUSE bypass decisions.

In both those cases, being able to tag files with some xattr and use
that as part of
criteria in the hook would be very useful IMO, but I don't think that
it should be a
problem to limit the scope of the allowed namespace to security.bpf.* for these
use cases.

Thanks,
Amir.
KP Singh June 30, 2022, 10:23 p.m. UTC | #16
On Thu, Jun 30, 2022 at 6:10 PM Casey Schaufler <casey@schaufler-ca.com> wrote:
>
> On 6/30/2022 6:47 AM, Christian Brauner wrote:
> > On Thu, Jun 30, 2022 at 03:29:53PM +0200, KP Singh wrote:
> >> On Thu, Jun 30, 2022 at 3:26 PM Christian Brauner <brauner@kernel.org> wrote:
> >>> On Thu, Jun 30, 2022 at 02:21:56PM +0200, KP Singh wrote:
> >>>> On Thu, Jun 30, 2022 at 1:45 PM Christian Brauner <brauner@kernel.org> wrote:
> >>>>> On Wed, Jun 29, 2022 at 08:02:50PM -0700, Alexei Starovoitov wrote:
> >>>>>> On Wed, Jun 29, 2022 at 2:56 AM Christian Brauner <brauner@kernel.org> wrote:
> >>>> [...]
> >>>>
> >>>>>>>>>>>> Signed-off-by: KP Singh <kpsingh@kernel.org>
> >>>>>>>>>>>> ---
> >>>>>>>>>>>>  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
> >>>>>>>>>> [...]
> >>>>>>>>>>
> >>>>>>>>>>>> +SEC("lsm.s/bprm_committed_creds")
> >>>>>>>>>>>> +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> >>>>>>>>>>>> +{
> >>>>>>>>>>>> +     struct task_struct *current = bpf_get_current_task_btf();
> >>>>>>>>>>>> +     char dir_xattr_value[64] = {0};
> >>>>>>>>>>>> +     int xattr_sz = 0;
> >>>>>>>>>>>> +
> >>>>>>>>>>>> +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> >>>>>>>>>>>> +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> >>>>>>>>>>>> +                             dir_xattr_value, 64);
> >>>>>>>>>>> Yeah, this isn't right. You're not accounting for the caller's userns
> >>>>>>>>>>> nor for the idmapped mount. If this is supposed to work you will need a
> >>>>>>>>>>> variant of vfs_getxattr() that takes the mount's idmapping into account
> >>>>>>>>>>> afaict. See what needs to happen after do_getxattr().
> >>>>>>>>>> Thanks for taking a look.
> >>>>>>>>>>
> >>>> [...]
> >>>>
> >>>>>>>>> That will not be correct.
> >>>>>>>>> posix_acl_fix_xattr_to_user checking current_user_ns()
> >>>>>>>>> is checking random tasks that happen to be running
> >>>>>>>>> when lsm hook got invoked.
> >>>>>>>>>
> >>>>>>>>> KP,
> >>>>>>>>> we probably have to document clearly that neither 'current*'
> >>>>>>>>> should not be used here.
> >>>>>>>>> xattr_permission also makes little sense in this context.
> >>>>>>>>> If anything it can be a different kfunc if there is a use case,
> >>>>>>>>> but I don't see it yet.
> >>>>>>>>> bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
> >>>>>>>>> call it directly. It's the kernel that is doing its security thing.
> >>>>>>>> Right, but LSMs usually only retrieve their own xattr namespace (ima,
> >>>>>>>> selinux, smack) or they calculate hashes for xattrs based on the raw
> >>>>>>>> filesystem xattr values (evm).
> >>>>>>>>
> >>>>>>>> But this new bpf_getxattr() is different. It allows to retrieve _any_
> >>>>>>>> xattr in any security hook it can be attached to. So someone can write a
> >>>>>>>> bpf program that retrieves filesystem capabilites or posix acls. And
> >>>>>>>> these are xattrs that require higher-level vfs involvement to be
> >>>>>>>> sensible in most contexts.
> >>>>>>>>
> >>>> [...]
> >>>>
> >>>>>>>> This hooks a bpf-lsm program to the security_bprm_committed_creds()
> >>>>>>>> hook. It then retrieves the extended attributes of the file to be
> >>>>>>>> executed. The hook currently always retrieves the raw filesystem values.
> >>>>>>>>
> >>>>>>>> But for example any XATTR_NAME_CAPS filesystem capabilities that
> >>>>>>>> might've been stored will be taken into account during exec. And both
> >>>>>>>> the idmapping of the mount and the caller matter when determing whether
> >>>>>>>> they are used or not.
> >>>>>>>>
> >>>>>>>> But the current implementation of bpf_getxattr() just ignores both. It
> >>>>>>>> will always retrieve the raw filesystem values. So if one invokes this
> >>>>>>>> hook they're not actually retrieving the values as they are seen by
> >>>>>>>> fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
> >>>>>>>> some use-cases it might very well become a security issue in others if
> >>>>>>>> access decisions are always based on the raw values.
> >>>>>>>>
> >>>>>>>> I'm not well-versed in this so bear with me, please.
> >>>>>>> If this is really just about retrieving the "security.bpf" xattr and no
> >>>>>>> other xattr then the bpf_getxattr() variant should somehow hard-code
> >>>>>>> that to ensure that no other xattrs can be retrieved, imho.
> >>>>>> All of these restrictions look very artificial to me.
> >>>>>> Especially the part "might very well become a security issue"
> >>>>>> just doesn't click.
> >>>>>> We're talking about bpf-lsm progs here that implement security.
> >>>>>> Can somebody implement a poor bpf-lsm that doesn't enforce
> >>>>>> any actual security? Sure. It's a code.
> >>>>> The point is that with the current implementation of bpf_getxattr() you
> >>>>> are able to retrieve any xattrs and we have way less control over a
> >>>>> bpf-lsm program than we do over selinux which a simple git grep
> >>>>> __vfs_getxattr() is all we need.
> >>>>>
> >>>>> The thing is that with bpf_getxattr() as it stands it is currently
> >>>>> impossible to retrieve xattr values - specifically filesystem
> >>>>> capabilities and posix acls - and see them exactly like the code you're
> >>>>> trying to supervise is. And that seems very strange from a security
> >>>>> perspective. So if someone were to write
> >>>>>
> >>>>> SEC("lsm.s/bprm_creds_from_file")
> >>>>> void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> >>>>> {
> >>>>>         struct task_struct *current = bpf_get_current_task_btf();
> >>>>>
> >>>>>         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> >>>>>                                 bprm->file->f_path.dentry->d_inode,
> >>>>>                                 XATTR_NAME_POSIX_ACL_ACCESS, ..);
> >>>>>         // or
> >>>>>         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> >>>>>                                 bprm->file->f_path.dentry->d_inode,
> >>>>>                                 XATTR_NAME_CAPS, ..);
> >>>>>
> >>>>> }
> >>>>>
> >>>>> they'd get the raw nscaps and the raw xattrs back. But now, as just a
> >>>>> tiny example, the nscaps->rootuid and the ->e_id fields in the posix
> >>>>> ACLs make zero sense in this context.
> >>>>>
> >>>>> And what's more there's no way for the bpf-lsm program to turn them into
> >>>>> something that makes sense in the context of the hook they are retrieved
> >>>>> in. It lacks all the necessary helpers to do so afaict.
> >>>>>
> >>>>>> No one complains about the usage of EXPORT_SYMBOL(__vfs_getxattr)
> >>>>>> in the existing LSMs like selinux.
> >>>>> Selinux only cares about its own xattr namespace. It doesn't retrieve
> >>>>> fscaps or posix acls and it's not possible to write selinux programs
> >>>>> that do so. With the bpf-lsm that's very much possible.
> >>>>>
> >>>>> And if we'd notice selinux would start retrieving random xattrs we'd ask
> >>>>> the same questions we do here.
> >>>>>
> >>>>>> No one complains about its usage in out of tree LSMs.
> >>>>>> Is that a security issue? Of course not.
> >>>>>> __vfs_getxattr is a kernel mechanism that LSMs use to implement
> >>>>>> the security features they need.
> >>>>>> __vfs_getxattr as kfunc here is pretty much the same as EXPORT_SYMBOL
> >>>>>> with a big difference that it's EXPORT_SYMBOL_GPL.
> >>>>>> BPF land doesn't have an equivalent of non-gpl export and is not going
> >>>>>> to get one.
> >>>> I want to reiterate what Alexei is saying here:
> >>>>
> >>>> *Please* consider this as a simple wrapper around __vfs_getxattr
> >>>> with a limited attach surface and extra verification checks and
> >>>> and nothing else.
> >>>>
> >>>> What you are saying is __vfs_getxattr does not make sense in some
> >>>> contexts. But kernel modules can still use it right?
> >>>>
> >>>> The user is implementing an LSM, if they chose to do things that don't make
> >>>> sense, then they can surely cause a lot more harm:
> >>>>
> >>>> SEC("lsm/bprm_check_security")
> >>>> int BPF_PROG(bprm_check, struct linux_binprm *bprm)
> >>>> {
> >>>>      return -EPERM;
> >>>> }
> >>>>
> >>>>> This discussion would probably be a lot shorter if this series were sent
> >>>>> with a proper explanation of how this supposed to work and what it's
> >>>>> used for.
> >>>> It's currently scoped to BPF LSM (albeit limited to LSM for now)
> >>>> but it won't just be used in LSM programs but some (allow-listed)
> >>>> tracing programs too.
> >>>>
> >>>> We want to leave the flexibility to the implementer of the LSM hooks. If the
> >>>> implementer choses to retrieve posix_acl_* we can also expose
> >>>> posix_acl_fix_xattr_to_user or a different kfunc that adds this logic too
> >>>> but that would be a separate kfunc (and a separate use-case).
> >>> No, sorry. That's what I feared and that's why I think this low-level
> >>> exposure of __vfs_getxattr() is wrong:
> >>> The posix_acl_fix_xattr_*() helpers, as well as the helpers like
> >>> get_file_caps() will not be exported. We're not going to export that
> >> I don't want to expose them and I don't want any others to be
> >> exposed either.
> >>
> >>> deeply internal vfs machinery. So I would NACK that. If you want that -
> >>> and that's what I'm saying here - you need to encapsulate this into your
> >>> vfs_*xattr() helper that you can call from your kfuncs.
> >> It seems like __vfs_getxattr is already exposed and does the wrong thing in
> >> some contexts, why can't we just "fix" __vfs_getxattr then?
> > To me having either a version of bpf_getxattr() that restricts access to
> > certain xattrs or a version that takes care to perform the neccesary
> > translations is what seems to make the most sense. I suggested that in
> > one of my first mails.
> >
> > The one thing where the way the xattrs are retrieved really matters is
> > for vfscaps (see get_vfs_caps_from_disk()) you really need something
> > like that function in order for vfs caps to make any sense and be
> > interpretable by the user of the hook.
> >
> > But again, I might just misunderstand the context here and for the
> > bpf-lsm all of this isn't really a concern. If your new series comes out
> > I'll try to get more into the wider context.
> > If the security folks are happy with this then I won't argue.
>
> A security module (BPF) using another security module's (Smack)
> xattrs without that module's (Smack) explicit approval would be
> considered extremely rude.  Smack and SELinux use published interfaces
> of the capability security module, but never access the capability
> attributes directly. The details of a security module's implementation
> are not a factor. The fact that BPF uses loadable programs as opposed
> to loadable policy is not relevant. The only security.xattr values
> that the BPF security module should allow the programs it runs to
> access are the ones it is managing. If you decided to create an eBPF

What about kernel modules who can use __vfs_getxattr already as
it's an exported symbol? This can still end up influencing
security policy or using them in any way they like.

Anyways, I think, for now, for the use case we have, it can work with
a restriction to security.bpf xattrs.



> implementation of SELinux you would still have to use attributes
> specific to the BPF security module. If, on the other hand, you wanted
> to extend Smack using eBPF programs, and the Smack maintainer liked
> the idea, it would be OK for the BPF security module to access some
> of the security.SMACK64 attributes.
>
> I want it to be clear that BPF is a Linux Security Module (LSM) and
> a collection of eBPF programs is *not* an LSM. BPF is responsible
> for being a good kernel citizen, and must ensure that it does not
> allow a set of configuration data that violates proper behavior.
> You can't write an SELinux policy that monster-mashes an ACL.
> You can't allow BPF to permit that either. You can't count on the
> good intentions, wisdom or skill of the author of an unreviewed,
> out of tree, eBPF program. I believe that this was understood during
> the review process of the BPF LSM.
>
>
KP Singh June 30, 2022, 10:25 p.m. UTC | #17
On Thu, Jun 30, 2022 at 6:29 PM Amir Goldstein <amir73il@gmail.com> wrote:
>
> > >
> > > This discussion would probably be a lot shorter if this series were sent
> > > with a proper explanation of how this supposed to work and what it's
> > > used for.
> >
> > It's currently scoped to BPF LSM (albeit limited to LSM for now)
> > but it won't just be used in LSM programs but some (allow-listed)
> > tracing programs too.
> >
>
> KP,
>
> Without taking sides in the discussion about the security aspect of
> bpf_getxattr(),
> I wanted to say that we have plans to add BPF hooks for fanotify event
> filters and
> AFAIK Alessio's team is working on adding BPF hooks for FUSE bypass decisions.
>
> In both those cases, being able to tag files with some xattr and use
> that as part of
> criteria in the hook would be very useful IMO, but I don't think that
> it should be a
> problem to limit the scope of the allowed namespace to security.bpf.* for these
> use cases.

Thanks Amir, I agree, this does seem like a practical way to move forward.

Cheers,
- KP

>
> Thanks,
> Amir.
Casey Schaufler June 30, 2022, 11:23 p.m. UTC | #18
On 6/30/2022 3:23 PM, KP Singh wrote:
> On Thu, Jun 30, 2022 at 6:10 PM Casey Schaufler <casey@schaufler-ca.com> wrote:
>> On 6/30/2022 6:47 AM, Christian Brauner wrote:
>>> On Thu, Jun 30, 2022 at 03:29:53PM +0200, KP Singh wrote:
>>>> On Thu, Jun 30, 2022 at 3:26 PM Christian Brauner <brauner@kernel.org> wrote:
>>>>> On Thu, Jun 30, 2022 at 02:21:56PM +0200, KP Singh wrote:
>>>>>> On Thu, Jun 30, 2022 at 1:45 PM Christian Brauner <brauner@kernel.org> wrote:
>>>>>>> On Wed, Jun 29, 2022 at 08:02:50PM -0700, Alexei Starovoitov wrote:
>>>>>>>> On Wed, Jun 29, 2022 at 2:56 AM Christian Brauner <brauner@kernel.org> wrote:
>>>>>> [...]
>>>>>>
>>>>>>>>>>>>>> Signed-off-by: KP Singh <kpsingh@kernel.org>
>>>>>>>>>>>>>> ---
>>>>>>>>>>>>>>  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
>>>>>>>>>>>> [...]
>>>>>>>>>>>>
>>>>>>>>>>>>>> +SEC("lsm.s/bprm_committed_creds")
>>>>>>>>>>>>>> +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
>>>>>>>>>>>>>> +{
>>>>>>>>>>>>>> +     struct task_struct *current = bpf_get_current_task_btf();
>>>>>>>>>>>>>> +     char dir_xattr_value[64] = {0};
>>>>>>>>>>>>>> +     int xattr_sz = 0;
>>>>>>>>>>>>>> +
>>>>>>>>>>>>>> +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
>>>>>>>>>>>>>> +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
>>>>>>>>>>>>>> +                             dir_xattr_value, 64);
>>>>>>>>>>>>> Yeah, this isn't right. You're not accounting for the caller's userns
>>>>>>>>>>>>> nor for the idmapped mount. If this is supposed to work you will need a
>>>>>>>>>>>>> variant of vfs_getxattr() that takes the mount's idmapping into account
>>>>>>>>>>>>> afaict. See what needs to happen after do_getxattr().
>>>>>>>>>>>> Thanks for taking a look.
>>>>>>>>>>>>
>>>>>> [...]
>>>>>>
>>>>>>>>>>> That will not be correct.
>>>>>>>>>>> posix_acl_fix_xattr_to_user checking current_user_ns()
>>>>>>>>>>> is checking random tasks that happen to be running
>>>>>>>>>>> when lsm hook got invoked.
>>>>>>>>>>>
>>>>>>>>>>> KP,
>>>>>>>>>>> we probably have to document clearly that neither 'current*'
>>>>>>>>>>> should not be used here.
>>>>>>>>>>> xattr_permission also makes little sense in this context.
>>>>>>>>>>> If anything it can be a different kfunc if there is a use case,
>>>>>>>>>>> but I don't see it yet.
>>>>>>>>>>> bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
>>>>>>>>>>> call it directly. It's the kernel that is doing its security thing.
>>>>>>>>>> Right, but LSMs usually only retrieve their own xattr namespace (ima,
>>>>>>>>>> selinux, smack) or they calculate hashes for xattrs based on the raw
>>>>>>>>>> filesystem xattr values (evm).
>>>>>>>>>>
>>>>>>>>>> But this new bpf_getxattr() is different. It allows to retrieve _any_
>>>>>>>>>> xattr in any security hook it can be attached to. So someone can write a
>>>>>>>>>> bpf program that retrieves filesystem capabilites or posix acls. And
>>>>>>>>>> these are xattrs that require higher-level vfs involvement to be
>>>>>>>>>> sensible in most contexts.
>>>>>>>>>>
>>>>>> [...]
>>>>>>
>>>>>>>>>> This hooks a bpf-lsm program to the security_bprm_committed_creds()
>>>>>>>>>> hook. It then retrieves the extended attributes of the file to be
>>>>>>>>>> executed. The hook currently always retrieves the raw filesystem values.
>>>>>>>>>>
>>>>>>>>>> But for example any XATTR_NAME_CAPS filesystem capabilities that
>>>>>>>>>> might've been stored will be taken into account during exec. And both
>>>>>>>>>> the idmapping of the mount and the caller matter when determing whether
>>>>>>>>>> they are used or not.
>>>>>>>>>>
>>>>>>>>>> But the current implementation of bpf_getxattr() just ignores both. It
>>>>>>>>>> will always retrieve the raw filesystem values. So if one invokes this
>>>>>>>>>> hook they're not actually retrieving the values as they are seen by
>>>>>>>>>> fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
>>>>>>>>>> some use-cases it might very well become a security issue in others if
>>>>>>>>>> access decisions are always based on the raw values.
>>>>>>>>>>
>>>>>>>>>> I'm not well-versed in this so bear with me, please.
>>>>>>>>> If this is really just about retrieving the "security.bpf" xattr and no
>>>>>>>>> other xattr then the bpf_getxattr() variant should somehow hard-code
>>>>>>>>> that to ensure that no other xattrs can be retrieved, imho.
>>>>>>>> All of these restrictions look very artificial to me.
>>>>>>>> Especially the part "might very well become a security issue"
>>>>>>>> just doesn't click.
>>>>>>>> We're talking about bpf-lsm progs here that implement security.
>>>>>>>> Can somebody implement a poor bpf-lsm that doesn't enforce
>>>>>>>> any actual security? Sure. It's a code.
>>>>>>> The point is that with the current implementation of bpf_getxattr() you
>>>>>>> are able to retrieve any xattrs and we have way less control over a
>>>>>>> bpf-lsm program than we do over selinux which a simple git grep
>>>>>>> __vfs_getxattr() is all we need.
>>>>>>>
>>>>>>> The thing is that with bpf_getxattr() as it stands it is currently
>>>>>>> impossible to retrieve xattr values - specifically filesystem
>>>>>>> capabilities and posix acls - and see them exactly like the code you're
>>>>>>> trying to supervise is. And that seems very strange from a security
>>>>>>> perspective. So if someone were to write
>>>>>>>
>>>>>>> SEC("lsm.s/bprm_creds_from_file")
>>>>>>> void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
>>>>>>> {
>>>>>>>         struct task_struct *current = bpf_get_current_task_btf();
>>>>>>>
>>>>>>>         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
>>>>>>>                                 bprm->file->f_path.dentry->d_inode,
>>>>>>>                                 XATTR_NAME_POSIX_ACL_ACCESS, ..);
>>>>>>>         // or
>>>>>>>         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
>>>>>>>                                 bprm->file->f_path.dentry->d_inode,
>>>>>>>                                 XATTR_NAME_CAPS, ..);
>>>>>>>
>>>>>>> }
>>>>>>>
>>>>>>> they'd get the raw nscaps and the raw xattrs back. But now, as just a
>>>>>>> tiny example, the nscaps->rootuid and the ->e_id fields in the posix
>>>>>>> ACLs make zero sense in this context.
>>>>>>>
>>>>>>> And what's more there's no way for the bpf-lsm program to turn them into
>>>>>>> something that makes sense in the context of the hook they are retrieved
>>>>>>> in. It lacks all the necessary helpers to do so afaict.
>>>>>>>
>>>>>>>> No one complains about the usage of EXPORT_SYMBOL(__vfs_getxattr)
>>>>>>>> in the existing LSMs like selinux.
>>>>>>> Selinux only cares about its own xattr namespace. It doesn't retrieve
>>>>>>> fscaps or posix acls and it's not possible to write selinux programs
>>>>>>> that do so. With the bpf-lsm that's very much possible.
>>>>>>>
>>>>>>> And if we'd notice selinux would start retrieving random xattrs we'd ask
>>>>>>> the same questions we do here.
>>>>>>>
>>>>>>>> No one complains about its usage in out of tree LSMs.
>>>>>>>> Is that a security issue? Of course not.
>>>>>>>> __vfs_getxattr is a kernel mechanism that LSMs use to implement
>>>>>>>> the security features they need.
>>>>>>>> __vfs_getxattr as kfunc here is pretty much the same as EXPORT_SYMBOL
>>>>>>>> with a big difference that it's EXPORT_SYMBOL_GPL.
>>>>>>>> BPF land doesn't have an equivalent of non-gpl export and is not going
>>>>>>>> to get one.
>>>>>> I want to reiterate what Alexei is saying here:
>>>>>>
>>>>>> *Please* consider this as a simple wrapper around __vfs_getxattr
>>>>>> with a limited attach surface and extra verification checks and
>>>>>> and nothing else.
>>>>>>
>>>>>> What you are saying is __vfs_getxattr does not make sense in some
>>>>>> contexts. But kernel modules can still use it right?
>>>>>>
>>>>>> The user is implementing an LSM, if they chose to do things that don't make
>>>>>> sense, then they can surely cause a lot more harm:
>>>>>>
>>>>>> SEC("lsm/bprm_check_security")
>>>>>> int BPF_PROG(bprm_check, struct linux_binprm *bprm)
>>>>>> {
>>>>>>      return -EPERM;
>>>>>> }
>>>>>>
>>>>>>> This discussion would probably be a lot shorter if this series were sent
>>>>>>> with a proper explanation of how this supposed to work and what it's
>>>>>>> used for.
>>>>>> It's currently scoped to BPF LSM (albeit limited to LSM for now)
>>>>>> but it won't just be used in LSM programs but some (allow-listed)
>>>>>> tracing programs too.
>>>>>>
>>>>>> We want to leave the flexibility to the implementer of the LSM hooks. If the
>>>>>> implementer choses to retrieve posix_acl_* we can also expose
>>>>>> posix_acl_fix_xattr_to_user or a different kfunc that adds this logic too
>>>>>> but that would be a separate kfunc (and a separate use-case).
>>>>> No, sorry. That's what I feared and that's why I think this low-level
>>>>> exposure of __vfs_getxattr() is wrong:
>>>>> The posix_acl_fix_xattr_*() helpers, as well as the helpers like
>>>>> get_file_caps() will not be exported. We're not going to export that
>>>> I don't want to expose them and I don't want any others to be
>>>> exposed either.
>>>>
>>>>> deeply internal vfs machinery. So I would NACK that. If you want that -
>>>>> and that's what I'm saying here - you need to encapsulate this into your
>>>>> vfs_*xattr() helper that you can call from your kfuncs.
>>>> It seems like __vfs_getxattr is already exposed and does the wrong thing in
>>>> some contexts, why can't we just "fix" __vfs_getxattr then?
>>> To me having either a version of bpf_getxattr() that restricts access to
>>> certain xattrs or a version that takes care to perform the neccesary
>>> translations is what seems to make the most sense. I suggested that in
>>> one of my first mails.
>>>
>>> The one thing where the way the xattrs are retrieved really matters is
>>> for vfscaps (see get_vfs_caps_from_disk()) you really need something
>>> like that function in order for vfs caps to make any sense and be
>>> interpretable by the user of the hook.
>>>
>>> But again, I might just misunderstand the context here and for the
>>> bpf-lsm all of this isn't really a concern. If your new series comes out
>>> I'll try to get more into the wider context.
>>> If the security folks are happy with this then I won't argue.
>> A security module (BPF) using another security module's (Smack)
>> xattrs without that module's (Smack) explicit approval would be
>> considered extremely rude.  Smack and SELinux use published interfaces
>> of the capability security module, but never access the capability
>> attributes directly. The details of a security module's implementation
>> are not a factor. The fact that BPF uses loadable programs as opposed
>> to loadable policy is not relevant. The only security.xattr values
>> that the BPF security module should allow the programs it runs to
>> access are the ones it is managing. If you decided to create an eBPF
> What about kernel modules who can use __vfs_getxattr already as
> it's an exported symbol? This can still end up influencing
> security policy or using them in any way they like.

If I put code in Smack to read SELinux attributes I would expect
to get a possibly polite but definitely strongly worded email
from Paul Moore regarding that behavior. The integrity subsystem
looks at Smack and SELinux attributes, but that's upstream and
we can see what nefarious things are being done with them. Because
I can see the upstream kernel code I can convince myself that
regardless of the SELinux policy loaded SELinux isn't going to
muck with the Smack attributes. I can't say the same for eBPF
programs that aren't going to be in Linus' tree.

> Anyways, I think, for now, for the use case we have, it can work with
> a restriction to security.bpf xattrs.

I can't say that this whole discussion is making me feel better
about the BPF LSM concept. The approval was based on the notion
that eBPF programs were restricted to "safe" behavior. It's
hard to see how allowing access to security.selinux could be
guaranteed to be in support of safe behavior.
Amir Goldstein July 1, 2022, 8:32 a.m. UTC | #19
On Fri, Jul 1, 2022 at 2:39 AM Casey Schaufler <casey@schaufler-ca.com> wrote:
>
> On 6/30/2022 3:23 PM, KP Singh wrote:
> > On Thu, Jun 30, 2022 at 6:10 PM Casey Schaufler <casey@schaufler-ca.com> wrote:
> >> On 6/30/2022 6:47 AM, Christian Brauner wrote:
> >>> On Thu, Jun 30, 2022 at 03:29:53PM +0200, KP Singh wrote:
> >>>> On Thu, Jun 30, 2022 at 3:26 PM Christian Brauner <brauner@kernel.org> wrote:
> >>>>> On Thu, Jun 30, 2022 at 02:21:56PM +0200, KP Singh wrote:
> >>>>>> On Thu, Jun 30, 2022 at 1:45 PM Christian Brauner <brauner@kernel.org> wrote:
> >>>>>>> On Wed, Jun 29, 2022 at 08:02:50PM -0700, Alexei Starovoitov wrote:
> >>>>>>>> On Wed, Jun 29, 2022 at 2:56 AM Christian Brauner <brauner@kernel.org> wrote:
> >>>>>> [...]
> >>>>>>
> >>>>>>>>>>>>>> Signed-off-by: KP Singh <kpsingh@kernel.org>
> >>>>>>>>>>>>>> ---
> >>>>>>>>>>>>>>  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
> >>>>>>>>>>>> [...]
> >>>>>>>>>>>>
> >>>>>>>>>>>>>> +SEC("lsm.s/bprm_committed_creds")
> >>>>>>>>>>>>>> +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> >>>>>>>>>>>>>> +{
> >>>>>>>>>>>>>> +     struct task_struct *current = bpf_get_current_task_btf();
> >>>>>>>>>>>>>> +     char dir_xattr_value[64] = {0};
> >>>>>>>>>>>>>> +     int xattr_sz = 0;
> >>>>>>>>>>>>>> +
> >>>>>>>>>>>>>> +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> >>>>>>>>>>>>>> +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> >>>>>>>>>>>>>> +                             dir_xattr_value, 64);
> >>>>>>>>>>>>> Yeah, this isn't right. You're not accounting for the caller's userns
> >>>>>>>>>>>>> nor for the idmapped mount. If this is supposed to work you will need a
> >>>>>>>>>>>>> variant of vfs_getxattr() that takes the mount's idmapping into account
> >>>>>>>>>>>>> afaict. See what needs to happen after do_getxattr().
> >>>>>>>>>>>> Thanks for taking a look.
> >>>>>>>>>>>>
> >>>>>> [...]
> >>>>>>
> >>>>>>>>>>> That will not be correct.
> >>>>>>>>>>> posix_acl_fix_xattr_to_user checking current_user_ns()
> >>>>>>>>>>> is checking random tasks that happen to be running
> >>>>>>>>>>> when lsm hook got invoked.
> >>>>>>>>>>>
> >>>>>>>>>>> KP,
> >>>>>>>>>>> we probably have to document clearly that neither 'current*'
> >>>>>>>>>>> should not be used here.
> >>>>>>>>>>> xattr_permission also makes little sense in this context.
> >>>>>>>>>>> If anything it can be a different kfunc if there is a use case,
> >>>>>>>>>>> but I don't see it yet.
> >>>>>>>>>>> bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
> >>>>>>>>>>> call it directly. It's the kernel that is doing its security thing.
> >>>>>>>>>> Right, but LSMs usually only retrieve their own xattr namespace (ima,
> >>>>>>>>>> selinux, smack) or they calculate hashes for xattrs based on the raw
> >>>>>>>>>> filesystem xattr values (evm).
> >>>>>>>>>>
> >>>>>>>>>> But this new bpf_getxattr() is different. It allows to retrieve _any_
> >>>>>>>>>> xattr in any security hook it can be attached to. So someone can write a
> >>>>>>>>>> bpf program that retrieves filesystem capabilites or posix acls. And
> >>>>>>>>>> these are xattrs that require higher-level vfs involvement to be
> >>>>>>>>>> sensible in most contexts.
> >>>>>>>>>>
> >>>>>> [...]
> >>>>>>
> >>>>>>>>>> This hooks a bpf-lsm program to the security_bprm_committed_creds()
> >>>>>>>>>> hook. It then retrieves the extended attributes of the file to be
> >>>>>>>>>> executed. The hook currently always retrieves the raw filesystem values.
> >>>>>>>>>>
> >>>>>>>>>> But for example any XATTR_NAME_CAPS filesystem capabilities that
> >>>>>>>>>> might've been stored will be taken into account during exec. And both
> >>>>>>>>>> the idmapping of the mount and the caller matter when determing whether
> >>>>>>>>>> they are used or not.
> >>>>>>>>>>
> >>>>>>>>>> But the current implementation of bpf_getxattr() just ignores both. It
> >>>>>>>>>> will always retrieve the raw filesystem values. So if one invokes this
> >>>>>>>>>> hook they're not actually retrieving the values as they are seen by
> >>>>>>>>>> fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
> >>>>>>>>>> some use-cases it might very well become a security issue in others if
> >>>>>>>>>> access decisions are always based on the raw values.
> >>>>>>>>>>
> >>>>>>>>>> I'm not well-versed in this so bear with me, please.
> >>>>>>>>> If this is really just about retrieving the "security.bpf" xattr and no
> >>>>>>>>> other xattr then the bpf_getxattr() variant should somehow hard-code
> >>>>>>>>> that to ensure that no other xattrs can be retrieved, imho.
> >>>>>>>> All of these restrictions look very artificial to me.
> >>>>>>>> Especially the part "might very well become a security issue"
> >>>>>>>> just doesn't click.
> >>>>>>>> We're talking about bpf-lsm progs here that implement security.
> >>>>>>>> Can somebody implement a poor bpf-lsm that doesn't enforce
> >>>>>>>> any actual security? Sure. It's a code.
> >>>>>>> The point is that with the current implementation of bpf_getxattr() you
> >>>>>>> are able to retrieve any xattrs and we have way less control over a
> >>>>>>> bpf-lsm program than we do over selinux which a simple git grep
> >>>>>>> __vfs_getxattr() is all we need.
> >>>>>>>
> >>>>>>> The thing is that with bpf_getxattr() as it stands it is currently
> >>>>>>> impossible to retrieve xattr values - specifically filesystem
> >>>>>>> capabilities and posix acls - and see them exactly like the code you're
> >>>>>>> trying to supervise is. And that seems very strange from a security
> >>>>>>> perspective. So if someone were to write
> >>>>>>>
> >>>>>>> SEC("lsm.s/bprm_creds_from_file")
> >>>>>>> void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> >>>>>>> {
> >>>>>>>         struct task_struct *current = bpf_get_current_task_btf();
> >>>>>>>
> >>>>>>>         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> >>>>>>>                                 bprm->file->f_path.dentry->d_inode,
> >>>>>>>                                 XATTR_NAME_POSIX_ACL_ACCESS, ..);
> >>>>>>>         // or
> >>>>>>>         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> >>>>>>>                                 bprm->file->f_path.dentry->d_inode,
> >>>>>>>                                 XATTR_NAME_CAPS, ..);
> >>>>>>>
> >>>>>>> }
> >>>>>>>
> >>>>>>> they'd get the raw nscaps and the raw xattrs back. But now, as just a
> >>>>>>> tiny example, the nscaps->rootuid and the ->e_id fields in the posix
> >>>>>>> ACLs make zero sense in this context.
> >>>>>>>
> >>>>>>> And what's more there's no way for the bpf-lsm program to turn them into
> >>>>>>> something that makes sense in the context of the hook they are retrieved
> >>>>>>> in. It lacks all the necessary helpers to do so afaict.
> >>>>>>>
> >>>>>>>> No one complains about the usage of EXPORT_SYMBOL(__vfs_getxattr)
> >>>>>>>> in the existing LSMs like selinux.
> >>>>>>> Selinux only cares about its own xattr namespace. It doesn't retrieve
> >>>>>>> fscaps or posix acls and it's not possible to write selinux programs
> >>>>>>> that do so. With the bpf-lsm that's very much possible.
> >>>>>>>
> >>>>>>> And if we'd notice selinux would start retrieving random xattrs we'd ask
> >>>>>>> the same questions we do here.
> >>>>>>>
> >>>>>>>> No one complains about its usage in out of tree LSMs.
> >>>>>>>> Is that a security issue? Of course not.
> >>>>>>>> __vfs_getxattr is a kernel mechanism that LSMs use to implement
> >>>>>>>> the security features they need.
> >>>>>>>> __vfs_getxattr as kfunc here is pretty much the same as EXPORT_SYMBOL
> >>>>>>>> with a big difference that it's EXPORT_SYMBOL_GPL.
> >>>>>>>> BPF land doesn't have an equivalent of non-gpl export and is not going
> >>>>>>>> to get one.
> >>>>>> I want to reiterate what Alexei is saying here:
> >>>>>>
> >>>>>> *Please* consider this as a simple wrapper around __vfs_getxattr
> >>>>>> with a limited attach surface and extra verification checks and
> >>>>>> and nothing else.
> >>>>>>
> >>>>>> What you are saying is __vfs_getxattr does not make sense in some
> >>>>>> contexts. But kernel modules can still use it right?
> >>>>>>
> >>>>>> The user is implementing an LSM, if they chose to do things that don't make
> >>>>>> sense, then they can surely cause a lot more harm:
> >>>>>>
> >>>>>> SEC("lsm/bprm_check_security")
> >>>>>> int BPF_PROG(bprm_check, struct linux_binprm *bprm)
> >>>>>> {
> >>>>>>      return -EPERM;
> >>>>>> }
> >>>>>>
> >>>>>>> This discussion would probably be a lot shorter if this series were sent
> >>>>>>> with a proper explanation of how this supposed to work and what it's
> >>>>>>> used for.
> >>>>>> It's currently scoped to BPF LSM (albeit limited to LSM for now)
> >>>>>> but it won't just be used in LSM programs but some (allow-listed)
> >>>>>> tracing programs too.
> >>>>>>
> >>>>>> We want to leave the flexibility to the implementer of the LSM hooks. If the
> >>>>>> implementer choses to retrieve posix_acl_* we can also expose
> >>>>>> posix_acl_fix_xattr_to_user or a different kfunc that adds this logic too
> >>>>>> but that would be a separate kfunc (and a separate use-case).
> >>>>> No, sorry. That's what I feared and that's why I think this low-level
> >>>>> exposure of __vfs_getxattr() is wrong:
> >>>>> The posix_acl_fix_xattr_*() helpers, as well as the helpers like
> >>>>> get_file_caps() will not be exported. We're not going to export that
> >>>> I don't want to expose them and I don't want any others to be
> >>>> exposed either.
> >>>>
> >>>>> deeply internal vfs machinery. So I would NACK that. If you want that -
> >>>>> and that's what I'm saying here - you need to encapsulate this into your
> >>>>> vfs_*xattr() helper that you can call from your kfuncs.
> >>>> It seems like __vfs_getxattr is already exposed and does the wrong thing in
> >>>> some contexts, why can't we just "fix" __vfs_getxattr then?
> >>> To me having either a version of bpf_getxattr() that restricts access to
> >>> certain xattrs or a version that takes care to perform the neccesary
> >>> translations is what seems to make the most sense. I suggested that in
> >>> one of my first mails.
> >>>
> >>> The one thing where the way the xattrs are retrieved really matters is
> >>> for vfscaps (see get_vfs_caps_from_disk()) you really need something
> >>> like that function in order for vfs caps to make any sense and be
> >>> interpretable by the user of the hook.
> >>>
> >>> But again, I might just misunderstand the context here and for the
> >>> bpf-lsm all of this isn't really a concern. If your new series comes out
> >>> I'll try to get more into the wider context.
> >>> If the security folks are happy with this then I won't argue.
> >> A security module (BPF) using another security module's (Smack)
> >> xattrs without that module's (Smack) explicit approval would be
> >> considered extremely rude.  Smack and SELinux use published interfaces
> >> of the capability security module, but never access the capability
> >> attributes directly. The details of a security module's implementation
> >> are not a factor. The fact that BPF uses loadable programs as opposed
> >> to loadable policy is not relevant. The only security.xattr values
> >> that the BPF security module should allow the programs it runs to
> >> access are the ones it is managing. If you decided to create an eBPF
> > What about kernel modules who can use __vfs_getxattr already as
> > it's an exported symbol? This can still end up influencing
> > security policy or using them in any way they like.
>
> If I put code in Smack to read SELinux attributes I would expect
> to get a possibly polite but definitely strongly worded email
> from Paul Moore regarding that behavior. The integrity subsystem
> looks at Smack and SELinux attributes, but that's upstream and
> we can see what nefarious things are being done with them. Because
> I can see the upstream kernel code I can convince myself that
> regardless of the SELinux policy loaded SELinux isn't going to
> muck with the Smack attributes. I can't say the same for eBPF
> programs that aren't going to be in Linus' tree.
>
> > Anyways, I think, for now, for the use case we have, it can work with
> > a restriction to security.bpf xattrs.
>
> I can't say that this whole discussion is making me feel better
> about the BPF LSM concept. The approval was based on the notion
> that eBPF programs were restricted to "safe" behavior. It's
> hard to see how allowing access to security.selinux could be
> guaranteed to be in support of safe behavior.
>

Apropos __vfs_getxattr(), looks like ecryptfs_getxattr_lower()
is abusing it.
Christian, not sure if you intend to spend time of idmapped
mount support of ecryptfs lower layer, but anyway that's that.

Thanks,
Amir.
Christian Brauner July 1, 2022, 8:58 a.m. UTC | #20
On Fri, Jul 01, 2022 at 11:32:55AM +0300, Amir Goldstein wrote:
> On Fri, Jul 1, 2022 at 2:39 AM Casey Schaufler <casey@schaufler-ca.com> wrote:
> >
> > On 6/30/2022 3:23 PM, KP Singh wrote:
> > > On Thu, Jun 30, 2022 at 6:10 PM Casey Schaufler <casey@schaufler-ca.com> wrote:
> > >> On 6/30/2022 6:47 AM, Christian Brauner wrote:
> > >>> On Thu, Jun 30, 2022 at 03:29:53PM +0200, KP Singh wrote:
> > >>>> On Thu, Jun 30, 2022 at 3:26 PM Christian Brauner <brauner@kernel.org> wrote:
> > >>>>> On Thu, Jun 30, 2022 at 02:21:56PM +0200, KP Singh wrote:
> > >>>>>> On Thu, Jun 30, 2022 at 1:45 PM Christian Brauner <brauner@kernel.org> wrote:
> > >>>>>>> On Wed, Jun 29, 2022 at 08:02:50PM -0700, Alexei Starovoitov wrote:
> > >>>>>>>> On Wed, Jun 29, 2022 at 2:56 AM Christian Brauner <brauner@kernel.org> wrote:
> > >>>>>> [...]
> > >>>>>>
> > >>>>>>>>>>>>>> Signed-off-by: KP Singh <kpsingh@kernel.org>
> > >>>>>>>>>>>>>> ---
> > >>>>>>>>>>>>>>  .../testing/selftests/bpf/prog_tests/xattr.c  | 54 +++++++++++++++++++
> > >>>>>>>>>>>> [...]
> > >>>>>>>>>>>>
> > >>>>>>>>>>>>>> +SEC("lsm.s/bprm_committed_creds")
> > >>>>>>>>>>>>>> +void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > >>>>>>>>>>>>>> +{
> > >>>>>>>>>>>>>> +     struct task_struct *current = bpf_get_current_task_btf();
> > >>>>>>>>>>>>>> +     char dir_xattr_value[64] = {0};
> > >>>>>>>>>>>>>> +     int xattr_sz = 0;
> > >>>>>>>>>>>>>> +
> > >>>>>>>>>>>>>> +     xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > >>>>>>>>>>>>>> +                             bprm->file->f_path.dentry->d_inode, XATTR_NAME,
> > >>>>>>>>>>>>>> +                             dir_xattr_value, 64);
> > >>>>>>>>>>>>> Yeah, this isn't right. You're not accounting for the caller's userns
> > >>>>>>>>>>>>> nor for the idmapped mount. If this is supposed to work you will need a
> > >>>>>>>>>>>>> variant of vfs_getxattr() that takes the mount's idmapping into account
> > >>>>>>>>>>>>> afaict. See what needs to happen after do_getxattr().
> > >>>>>>>>>>>> Thanks for taking a look.
> > >>>>>>>>>>>>
> > >>>>>> [...]
> > >>>>>>
> > >>>>>>>>>>> That will not be correct.
> > >>>>>>>>>>> posix_acl_fix_xattr_to_user checking current_user_ns()
> > >>>>>>>>>>> is checking random tasks that happen to be running
> > >>>>>>>>>>> when lsm hook got invoked.
> > >>>>>>>>>>>
> > >>>>>>>>>>> KP,
> > >>>>>>>>>>> we probably have to document clearly that neither 'current*'
> > >>>>>>>>>>> should not be used here.
> > >>>>>>>>>>> xattr_permission also makes little sense in this context.
> > >>>>>>>>>>> If anything it can be a different kfunc if there is a use case,
> > >>>>>>>>>>> but I don't see it yet.
> > >>>>>>>>>>> bpf-lsm prog calling __vfs_getxattr is just like other lsm-s that
> > >>>>>>>>>>> call it directly. It's the kernel that is doing its security thing.
> > >>>>>>>>>> Right, but LSMs usually only retrieve their own xattr namespace (ima,
> > >>>>>>>>>> selinux, smack) or they calculate hashes for xattrs based on the raw
> > >>>>>>>>>> filesystem xattr values (evm).
> > >>>>>>>>>>
> > >>>>>>>>>> But this new bpf_getxattr() is different. It allows to retrieve _any_
> > >>>>>>>>>> xattr in any security hook it can be attached to. So someone can write a
> > >>>>>>>>>> bpf program that retrieves filesystem capabilites or posix acls. And
> > >>>>>>>>>> these are xattrs that require higher-level vfs involvement to be
> > >>>>>>>>>> sensible in most contexts.
> > >>>>>>>>>>
> > >>>>>> [...]
> > >>>>>>
> > >>>>>>>>>> This hooks a bpf-lsm program to the security_bprm_committed_creds()
> > >>>>>>>>>> hook. It then retrieves the extended attributes of the file to be
> > >>>>>>>>>> executed. The hook currently always retrieves the raw filesystem values.
> > >>>>>>>>>>
> > >>>>>>>>>> But for example any XATTR_NAME_CAPS filesystem capabilities that
> > >>>>>>>>>> might've been stored will be taken into account during exec. And both
> > >>>>>>>>>> the idmapping of the mount and the caller matter when determing whether
> > >>>>>>>>>> they are used or not.
> > >>>>>>>>>>
> > >>>>>>>>>> But the current implementation of bpf_getxattr() just ignores both. It
> > >>>>>>>>>> will always retrieve the raw filesystem values. So if one invokes this
> > >>>>>>>>>> hook they're not actually retrieving the values as they are seen by
> > >>>>>>>>>> fs/exec.c. And I'm wondering why that is ok? And even if this is ok for
> > >>>>>>>>>> some use-cases it might very well become a security issue in others if
> > >>>>>>>>>> access decisions are always based on the raw values.
> > >>>>>>>>>>
> > >>>>>>>>>> I'm not well-versed in this so bear with me, please.
> > >>>>>>>>> If this is really just about retrieving the "security.bpf" xattr and no
> > >>>>>>>>> other xattr then the bpf_getxattr() variant should somehow hard-code
> > >>>>>>>>> that to ensure that no other xattrs can be retrieved, imho.
> > >>>>>>>> All of these restrictions look very artificial to me.
> > >>>>>>>> Especially the part "might very well become a security issue"
> > >>>>>>>> just doesn't click.
> > >>>>>>>> We're talking about bpf-lsm progs here that implement security.
> > >>>>>>>> Can somebody implement a poor bpf-lsm that doesn't enforce
> > >>>>>>>> any actual security? Sure. It's a code.
> > >>>>>>> The point is that with the current implementation of bpf_getxattr() you
> > >>>>>>> are able to retrieve any xattrs and we have way less control over a
> > >>>>>>> bpf-lsm program than we do over selinux which a simple git grep
> > >>>>>>> __vfs_getxattr() is all we need.
> > >>>>>>>
> > >>>>>>> The thing is that with bpf_getxattr() as it stands it is currently
> > >>>>>>> impossible to retrieve xattr values - specifically filesystem
> > >>>>>>> capabilities and posix acls - and see them exactly like the code you're
> > >>>>>>> trying to supervise is. And that seems very strange from a security
> > >>>>>>> perspective. So if someone were to write
> > >>>>>>>
> > >>>>>>> SEC("lsm.s/bprm_creds_from_file")
> > >>>>>>> void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
> > >>>>>>> {
> > >>>>>>>         struct task_struct *current = bpf_get_current_task_btf();
> > >>>>>>>
> > >>>>>>>         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > >>>>>>>                                 bprm->file->f_path.dentry->d_inode,
> > >>>>>>>                                 XATTR_NAME_POSIX_ACL_ACCESS, ..);
> > >>>>>>>         // or
> > >>>>>>>         xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
> > >>>>>>>                                 bprm->file->f_path.dentry->d_inode,
> > >>>>>>>                                 XATTR_NAME_CAPS, ..);
> > >>>>>>>
> > >>>>>>> }
> > >>>>>>>
> > >>>>>>> they'd get the raw nscaps and the raw xattrs back. But now, as just a
> > >>>>>>> tiny example, the nscaps->rootuid and the ->e_id fields in the posix
> > >>>>>>> ACLs make zero sense in this context.
> > >>>>>>>
> > >>>>>>> And what's more there's no way for the bpf-lsm program to turn them into
> > >>>>>>> something that makes sense in the context of the hook they are retrieved
> > >>>>>>> in. It lacks all the necessary helpers to do so afaict.
> > >>>>>>>
> > >>>>>>>> No one complains about the usage of EXPORT_SYMBOL(__vfs_getxattr)
> > >>>>>>>> in the existing LSMs like selinux.
> > >>>>>>> Selinux only cares about its own xattr namespace. It doesn't retrieve
> > >>>>>>> fscaps or posix acls and it's not possible to write selinux programs
> > >>>>>>> that do so. With the bpf-lsm that's very much possible.
> > >>>>>>>
> > >>>>>>> And if we'd notice selinux would start retrieving random xattrs we'd ask
> > >>>>>>> the same questions we do here.
> > >>>>>>>
> > >>>>>>>> No one complains about its usage in out of tree LSMs.
> > >>>>>>>> Is that a security issue? Of course not.
> > >>>>>>>> __vfs_getxattr is a kernel mechanism that LSMs use to implement
> > >>>>>>>> the security features they need.
> > >>>>>>>> __vfs_getxattr as kfunc here is pretty much the same as EXPORT_SYMBOL
> > >>>>>>>> with a big difference that it's EXPORT_SYMBOL_GPL.
> > >>>>>>>> BPF land doesn't have an equivalent of non-gpl export and is not going
> > >>>>>>>> to get one.
> > >>>>>> I want to reiterate what Alexei is saying here:
> > >>>>>>
> > >>>>>> *Please* consider this as a simple wrapper around __vfs_getxattr
> > >>>>>> with a limited attach surface and extra verification checks and
> > >>>>>> and nothing else.
> > >>>>>>
> > >>>>>> What you are saying is __vfs_getxattr does not make sense in some
> > >>>>>> contexts. But kernel modules can still use it right?
> > >>>>>>
> > >>>>>> The user is implementing an LSM, if they chose to do things that don't make
> > >>>>>> sense, then they can surely cause a lot more harm:
> > >>>>>>
> > >>>>>> SEC("lsm/bprm_check_security")
> > >>>>>> int BPF_PROG(bprm_check, struct linux_binprm *bprm)
> > >>>>>> {
> > >>>>>>      return -EPERM;
> > >>>>>> }
> > >>>>>>
> > >>>>>>> This discussion would probably be a lot shorter if this series were sent
> > >>>>>>> with a proper explanation of how this supposed to work and what it's
> > >>>>>>> used for.
> > >>>>>> It's currently scoped to BPF LSM (albeit limited to LSM for now)
> > >>>>>> but it won't just be used in LSM programs but some (allow-listed)
> > >>>>>> tracing programs too.
> > >>>>>>
> > >>>>>> We want to leave the flexibility to the implementer of the LSM hooks. If the
> > >>>>>> implementer choses to retrieve posix_acl_* we can also expose
> > >>>>>> posix_acl_fix_xattr_to_user or a different kfunc that adds this logic too
> > >>>>>> but that would be a separate kfunc (and a separate use-case).
> > >>>>> No, sorry. That's what I feared and that's why I think this low-level
> > >>>>> exposure of __vfs_getxattr() is wrong:
> > >>>>> The posix_acl_fix_xattr_*() helpers, as well as the helpers like
> > >>>>> get_file_caps() will not be exported. We're not going to export that
> > >>>> I don't want to expose them and I don't want any others to be
> > >>>> exposed either.
> > >>>>
> > >>>>> deeply internal vfs machinery. So I would NACK that. If you want that -
> > >>>>> and that's what I'm saying here - you need to encapsulate this into your
> > >>>>> vfs_*xattr() helper that you can call from your kfuncs.
> > >>>> It seems like __vfs_getxattr is already exposed and does the wrong thing in
> > >>>> some contexts, why can't we just "fix" __vfs_getxattr then?
> > >>> To me having either a version of bpf_getxattr() that restricts access to
> > >>> certain xattrs or a version that takes care to perform the neccesary
> > >>> translations is what seems to make the most sense. I suggested that in
> > >>> one of my first mails.
> > >>>
> > >>> The one thing where the way the xattrs are retrieved really matters is
> > >>> for vfscaps (see get_vfs_caps_from_disk()) you really need something
> > >>> like that function in order for vfs caps to make any sense and be
> > >>> interpretable by the user of the hook.
> > >>>
> > >>> But again, I might just misunderstand the context here and for the
> > >>> bpf-lsm all of this isn't really a concern. If your new series comes out
> > >>> I'll try to get more into the wider context.
> > >>> If the security folks are happy with this then I won't argue.
> > >> A security module (BPF) using another security module's (Smack)
> > >> xattrs without that module's (Smack) explicit approval would be
> > >> considered extremely rude.  Smack and SELinux use published interfaces
> > >> of the capability security module, but never access the capability
> > >> attributes directly. The details of a security module's implementation
> > >> are not a factor. The fact that BPF uses loadable programs as opposed
> > >> to loadable policy is not relevant. The only security.xattr values
> > >> that the BPF security module should allow the programs it runs to
> > >> access are the ones it is managing. If you decided to create an eBPF
> > > What about kernel modules who can use __vfs_getxattr already as
> > > it's an exported symbol? This can still end up influencing
> > > security policy or using them in any way they like.
> >
> > If I put code in Smack to read SELinux attributes I would expect
> > to get a possibly polite but definitely strongly worded email
> > from Paul Moore regarding that behavior. The integrity subsystem
> > looks at Smack and SELinux attributes, but that's upstream and
> > we can see what nefarious things are being done with them. Because
> > I can see the upstream kernel code I can convince myself that
> > regardless of the SELinux policy loaded SELinux isn't going to
> > muck with the Smack attributes. I can't say the same for eBPF
> > programs that aren't going to be in Linus' tree.
> >
> > > Anyways, I think, for now, for the use case we have, it can work with
> > > a restriction to security.bpf xattrs.
> >
> > I can't say that this whole discussion is making me feel better
> > about the BPF LSM concept. The approval was based on the notion
> > that eBPF programs were restricted to "safe" behavior. It's
> > hard to see how allowing access to security.selinux could be
> > guaranteed to be in support of safe behavior.
> >
> 
> Apropos __vfs_getxattr(), looks like ecryptfs_getxattr_lower()
> is abusing it.

Heh, quoting what I wrote to KP yesterday off-list about
__vfs_getxattr():

"it's [__vfs_getxattr()] exported but [afaict] it's not used in kernel
modules. afaict it's only exposed because of ecryptfs"

So right at the beginning I had already pondered whether we should just
rip out __vfs_getxattr() from ecryptfs and unexport the helper
completely because there's barely a reason to use it. Module/driver code
should not use something as low-level as __vfs_getxattr() imho.

Overlayfs does it correctly and uses vfs_getxattr() but maybe ecryptfs
needs to use it for for some reason?. I haven't looked yet.

> Christian, not sure if you intend to spend time of idmapped
> mount support of ecryptfs lower layer, but anyway that's that.

Not really. Remember the conversation we had with Tyler at LSFMM where
he considered marking it deprecated. I don't think it's worth putting in
the work.
Amir Goldstein July 1, 2022, 9:24 a.m. UTC | #21
> >
> > Apropos __vfs_getxattr(), looks like ecryptfs_getxattr_lower()
> > is abusing it.
>
> Heh, quoting what I wrote to KP yesterday off-list about
> __vfs_getxattr():
>
> "it's [__vfs_getxattr()] exported but [afaict] it's not used in kernel
> modules. afaict it's only exposed because of ecryptfs"
>
> So right at the beginning I had already pondered whether we should just
> rip out __vfs_getxattr() from ecryptfs and unexport the helper
> completely because there's barely a reason to use it. Module/driver code
> should not use something as low-level as __vfs_getxattr() imho.
>
> Overlayfs does it correctly and uses vfs_getxattr() but maybe ecryptfs
> needs to use it for for some reason?. I haven't looked yet.
>

No reason AFAIK (CC Tyler+Miklos)

Most lower ecryptfs operations use vfs_XXX()
48b512e68571 ("ecryptfs: call vfs_setxattr() in ecryptfs_setxattr()")
fixed vfs_setxattr() which was later changed to __vfs_setxattr_locked(),
but left __vfs_getxattr(), __vfs_removexattr() and i_op->listxattr().

> > Christian, not sure if you intend to spend time of idmapped
> > mount support of ecryptfs lower layer, but anyway that's that.
>
> Not really. Remember the conversation we had with Tyler at LSFMM where
> he considered marking it deprecated. I don't think it's worth putting in
> the work.

OK, so just need a volunteer to close the security hole and
possibly unexport __vfs_getxattr().

Does anybody know of any out of tree modules that use it
for a good reason?

Thanks,
Amir.
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/xattr.c b/tools/testing/selftests/bpf/prog_tests/xattr.c
new file mode 100644
index 000000000000..ef07fa8a1763
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/xattr.c
@@ -0,0 +1,54 @@ 
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2022 Google LLC.
+ */
+
+#include <test_progs.h>
+#include <sys/xattr.h>
+#include "xattr.skel.h"
+
+#define XATTR_NAME "security.bpf"
+#define XATTR_VALUE "test_progs"
+
+void test_xattr(void)
+{
+	struct xattr *skel = NULL;
+	char tmp_dir_path[] = "/tmp/xattrXXXXXX";
+	char tmp_exec_path[64];
+	char cmd[256];
+	int err;
+
+	if (CHECK_FAIL(!mkdtemp(tmp_dir_path)))
+		goto close_prog;
+
+	snprintf(tmp_exec_path, sizeof(tmp_exec_path), "%s/copy_of_ls",
+		 tmp_dir_path);
+	snprintf(cmd, sizeof(cmd), "cp /bin/ls %s", tmp_exec_path);
+	if (CHECK_FAIL(system(cmd)))
+		goto close_prog_rmdir;
+
+	if (CHECK_FAIL(setxattr(tmp_exec_path, XATTR_NAME, XATTR_VALUE,
+			   sizeof(XATTR_VALUE), 0)))
+		goto close_prog_rmdir;
+
+	skel = xattr__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_load"))
+		goto close_prog_rmdir;
+
+	err = xattr__attach(skel);
+	if (!ASSERT_OK(err, "xattr__attach failed"))
+		goto close_prog_rmdir;
+
+	snprintf(cmd, sizeof(cmd), "%s -l", tmp_exec_path);
+	if (CHECK_FAIL(system(cmd)))
+		goto close_prog_rmdir;
+
+	ASSERT_EQ(skel->bss->result, 1, "xattr result");
+
+close_prog_rmdir:
+	snprintf(cmd, sizeof(cmd), "rm -rf %s", tmp_dir_path);
+	system(cmd);
+close_prog:
+	xattr__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/xattr.c b/tools/testing/selftests/bpf/progs/xattr.c
new file mode 100644
index 000000000000..ccc078fb8ebd
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/xattr.c
@@ -0,0 +1,37 @@ 
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2022 Google LLC.
+ */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+#define XATTR_NAME "security.bpf"
+#define XATTR_VALUE "test_progs"
+
+__u64 result = 0;
+
+extern ssize_t bpf_getxattr(struct dentry *dentry, struct inode *inode,
+			    const char *name, void *value, int size) __ksym;
+
+SEC("lsm.s/bprm_committed_creds")
+void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
+{
+	struct task_struct *current = bpf_get_current_task_btf();
+	char dir_xattr_value[64] = {0};
+	int xattr_sz = 0;
+
+	xattr_sz = bpf_getxattr(bprm->file->f_path.dentry,
+				bprm->file->f_path.dentry->d_inode, XATTR_NAME,
+				dir_xattr_value, 64);
+
+	if (xattr_sz <= 0)
+		return;
+
+	if (!bpf_strncmp(dir_xattr_value, sizeof(XATTR_VALUE), XATTR_VALUE))
+		result = 1;
+}