Skip to contentSkip to conversation

Introduce security_create_user_ns()

PATCH Superseded Frederick Lawler 7 participants 26 messages Jun 21, 2022 – Jul 1, 2022

Subsystems (5)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover Introduce security_create_user_ns()

While creating a LSM BPF MAC policy to block user namespace creation, we used the LSM cred_prepare hook because that is the closest hook to prevent a call to create_user_ns().

The calls look something like this:

cred = prepare_creds()
security_prepare_creds()
call_int_hook(cred_prepare, ...
if (cred)
create_user_ns(cred)

We noticed that error codes were not propagated from this hook and introduced a patch [1] to propagate those errors.

The discussion notes that security_prepare_creds() is not appropriate for MAC policies, and instead the hook is meant for LSM authors to prepare credentials for mutation. [2]

Ultimately, we concluded that a better course of action is to introduce a new security hook for LSM authors. [3]

This patch set first introduces a new security_create_user_ns() function and create_user_ns LSM hook, then marks the hook as sleepable in BPF.

Links:
1. https://lore.kernel.org/all/[email protected]/
2. https://lore.kernel.org/all/[email protected]/
3. https://lore.kernel.org/all/[email protected]/

Frederick Lawler (2):
security, lsm: Introduce security_create_user_ns()
bpf-lsm: Make bpf_lsm_create_user_ns() sleepable

 include/linux/lsm_hook_defs.h | 2 ++
 include/linux/lsm_hooks.h     | 5 +++++
 include/linux/security.h      | 8 ++++++++
 kernel/bpf/bpf_lsm.c          | 1 +
 kernel/user_namespace.c       | 5 +++++
 security/security.c           | 6 ++++++
 6 files changed, 27 insertions(+)

--
2.30.2

#2 lore raw

PATCH 2/2 bpf-lsm: Make bpf_lsm_create_user_ns() sleepable

Users may want to audit calls to security_create_user_ns() and access user space memory. Also create_user_ns() runs without pagefault_disabled(). Therefore, make bpf_lsm_create_user_ns() sleepable for mandatory access control policies.

---
 kernel/bpf/bpf_lsm.c | 1 +
 1 file changed, 1 insertion(+)
kernel/bpf/bpf_lsm.c +1 -0
index c1351df9f7ee..75853965e7b0 100644
@@ -250,6 +250,7 @@ BTF_ID(func, bpf_lsm_task_getsecid_obj)
 BTF_ID(func, bpf_lsm_task_prctl)
 BTF_ID(func, bpf_lsm_task_setscheduler)
 BTF_ID(func, bpf_lsm_task_to_inode)
+BTF_ID(func, bpf_lsm_create_user_ns)
 BTF_SET_END(sleepable_lsm_hooks)
 
 bool bpf_lsm_is_sleepable_hook(u32 btf_id)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/2 security, lsm: Introduce security_create_user_ns()

In reply to Frederick Lawler

Preventing user namespace (privileged or otherwise) creation comes in a few of forms in order of granularity:

1. /proc/sys/user/max_user_namespaces sysctl
2. OS specific patch(es)
3. CONFIG_USER_NS

To block a task based on its attributes, the LSM hook cred_prepare is a good candidate for use because it provides more granular control, and it is called before create_user_ns():

cred = prepare_creds()
security_prepare_creds()
call_int_hook(cred_prepare, ...
if (cred)
create_user_ns(cred)

Since security_prepare_creds() is meant for LSMs to copy and prepare credentials, access control is an unintended use of the hook. Therefore introduce a new function security_create_user_ns() with an accompanying create_user_ns LSM hook.

This hook takes the prepared creds and the newly created user namespace for LSM authors to write policy against. On success, the new namespace is applied to credentials, otherwise an error is returned.

---
 include/linux/lsm_hook_defs.h | 2 ++
 include/linux/lsm_hooks.h     | 5 +++++
 include/linux/security.h      | 8 ++++++++
 kernel/user_namespace.c       | 5 +++++
 security/security.c           | 6 ++++++
 5 files changed, 26 insertions(+)
include/linux/lsm_hook_defs.h +2 -0
index eafa1d2489fd..bd9b38db4d03 100644
@@ -223,6 +223,8 @@ LSM_HOOK(int, -ENOSYS, task_prctl, int option, unsigned long arg2,
 	 unsigned long arg3, unsigned long arg4, unsigned long arg5)
 LSM_HOOK(void, LSM_RET_VOID, task_to_inode, struct task_struct *p,
 	 struct inode *inode)
+LSM_HOOK(int, 0, create_user_ns, const struct cred *new,
+	 const struct user_namespace *new_userns)
 LSM_HOOK(int, 0, ipc_permission, struct kern_ipc_perm *ipcp, short flag)
 LSM_HOOK(void, LSM_RET_VOID, ipc_getsecid, struct kern_ipc_perm *ipcp,
 	 u32 *secid)
include/linux/lsm_hooks.h +5 -0
index 91c8146649f5..1356a792a6bd 100644
@@ -799,6 +799,11 @@
  *	security attributes, e.g. for /proc/pid inodes.
  *	@p contains the task_struct for the task.
  *	@inode contains the inode structure for the inode.
+ * @create_user_ns:
+ *	Check permission prior to assigning the new namespace to @cred->user_ns.
+ *	@cred points to prepared creds.
+ *	@new_userns points to the newly created user namespace.
+ *	Return 0 if successful, otherwise < 0 error code.
  *
  * Security hooks for Netlink messaging.
  *
include/linux/security.h +8 -0
index 7fc4e9f49f54..a656dbe7b65a 100644
@@ -435,6 +435,8 @@ int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
 int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
 			unsigned long arg4, unsigned long arg5);
 void security_task_to_inode(struct task_struct *p, struct inode *inode);
+int security_create_user_ns(const struct cred *cred,
+			    const struct user_namespace *new_userns);
 int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag);
 void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid);
 int security_msg_msg_alloc(struct msg_msg *msg);
@@ -1185,6 +1187,12 @@ static inline int security_task_prctl(int option, unsigned long arg2,
 static inline void security_task_to_inode(struct task_struct *p, struct inode *inode)
 { }
 
+static inline int security_create_user_ns(const struct cred *cred,
+					  const struct user_namespace *new_userns)
+{
+	return 0;
+}
+
 static inline int security_ipc_permission(struct kern_ipc_perm *ipcp,
 					  short flag)
 {
kernel/user_namespace.c +5 -0
index 5481ba44a8d6..8c5e5592a503 100644
@@ -9,6 +9,7 @@
 #include <linux/highuid.h>
 #include <linux/cred.h>
 #include <linux/securebits.h>
+#include <linux/security.h>
 #include <linux/keyctl.h>
 #include <linux/key-type.h>
 #include <keys/user-type.h>
@@ -153,6 +154,10 @@ int create_user_ns(struct cred *new)
 	if (!setup_userns_sysctls(ns))
 		goto fail_keyring;
 
+	ret = security_create_user_ns(new, ns);
+	if (ret < 0)
+		goto fail_keyring;
+
 	set_cred_user_ns(new, ns);
 	return 0;
 fail_keyring:
security/security.c +6 -0
index 188b8f782220..d6b1751805ca 100644
@@ -1903,6 +1903,12 @@ void security_task_to_inode(struct task_struct *p, struct inode *inode)
 	call_void_hook(task_to_inode, p, inode);
 }
 
+int security_create_user_ns(const struct cred *cred,
+			    const struct user_namespace *new_userns)
+{
+	return call_int_hook(create_user_ns, 0, cred, new_userns);
+}
+
 int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
 {
 	return call_int_hook(ipc_permission, 0, ipcp, flag);
Signature · 1 line
2.30.2
#3 lore raw
In reply to Frederick Lawler
Quoted 19 lines · Frederick Lawler

While creating a LSM BPF MAC policy to block user namespace creation, we used the LSM cred_prepare hook because that is the closest hook to prevent a call to create_user_ns().

The calls look something like this:

cred = prepare_creds()
security_prepare_creds()
call_int_hook(cred_prepare, ...
if (cred)
create_user_ns(cred)

We noticed that error codes were not propagated from this hook and introduced a patch [1] to propagate those errors.

The discussion notes that security_prepare_creds() is not appropriate for MAC policies, and instead the hook is meant for LSM authors to prepare credentials for mutation. [2]

Quoted 5 lines

Ultimately, we concluded that a better course of action is to introduce a new security hook for LSM authors. [3]

This patch set first introduces a new security_create_user_ns() function and create_user_ns LSM hook, then marks the hook as sleepable in BPF.

Why restrict this hook to user namespaces? It seems that an LSM that chooses to preform controls on user namespaces may want to do so for network namespaces as well. Also, the hook seems backwards. You should decide if the creation of the namespace is allowed before you create it. Passing the new namespace to a function that checks to see creating a namespace is allowed doesn't make a lot off sense.

Quoted 21 lines

Links:
1. https://lore.kernel.org/all/[email protected]/
2. https://lore.kernel.org/all/[email protected]/
3. https://lore.kernel.org/all/[email protected]/

Frederick Lawler (2):
security, lsm: Introduce security_create_user_ns()
bpf-lsm: Make bpf_lsm_create_user_ns() sleepable

  include/linux/lsm_hook_defs.h | 2 ++
  include/linux/lsm_hooks.h     | 5 +++++
  include/linux/security.h      | 8 ++++++++
  kernel/bpf/bpf_lsm.c          | 1 +
  kernel/user_namespace.c       | 5 +++++
  security/security.c           | 6 ++++++
  6 files changed, 27 insertions(+)

--
2.30.2

#4 lore raw

Hi Casey,

Quoted 23 lines · Casey Schaufler
Quoted 22 lines · Frederick Lawler

While creating a LSM BPF MAC policy to block user namespace creation, we used the LSM cred_prepare hook because that is the closest hook to prevent a call to create_user_ns().

The calls look something like this:

     cred = prepare_creds()
         security_prepare_creds()
             call_int_hook(cred_prepare, ...
     if (cred)
         create_user_ns(cred)

We noticed that error codes were not propagated from this hook and introduced a patch [1] to propagate those errors.

The discussion notes that security_prepare_creds() is not appropriate for MAC policies, and instead the hook is meant for LSM authors to prepare credentials for mutation. [2]

Ultimately, we concluded that a better course of action is to introduce a new security hook for LSM authors. [3]

Quoted 6 lines
Quoted 2 lines

This patch set first introduces a new security_create_user_ns() function and create_user_ns LSM hook, then marks the hook as sleepable in BPF.

Why restrict this hook to user namespaces? It seems that an LSM that chooses to preform controls on user namespaces may want to do so for network namespaces as well.

IIRC, CLONE_NEWUSER is the only namespace flag that does not require CAP_SYS_ADMIN. There is a security use case to prevent this namespace from being created within an unprivileged environment. I'm not opposed to a more generic hook, but I don't currently have a use case to block any others. We can also say the same is true for the other namespaces: add this generic security function to these too.

I'm curious what others think about this too.

Quoted 5 lines

Also, the hook seems backwards. You should
decide if the creation of the namespace is allowed before you create it. Passing the new namespace to a function that checks to see creating a namespace is allowed doesn't make a lot off sense.

I think having more context to a security hook is a good thing. I believe you brought up in the previous discussions that you'd like to use this hook for xattr purposes. Doesn't that require a namespace?

Quoted 23 lines
Quoted 23 lines

Links:
1. https://lore.kernel.org/all/[email protected]/
2. https://lore.kernel.org/all/[email protected]/

3. https://lore.kernel.org/all/[email protected]/

Frederick Lawler (2):
   security, lsm: Introduce security_create_user_ns()
   bpf-lsm: Make bpf_lsm_create_user_ns() sleepable

  include/linux/lsm_hook_defs.h | 2 ++
  include/linux/lsm_hooks.h     | 5 +++++
  include/linux/security.h      | 8 ++++++++
  kernel/bpf/bpf_lsm.c          | 1 +
  kernel/user_namespace.c       | 5 +++++
  security/security.c           | 6 ++++++

  6 files changed, 27 insertions(+)
Signature · 1 line
2.30.2
#5 lore raw
Quoted 26 lines · Frederick Lawler

Hi Casey,

Quoted 23 lines · Casey Schaufler
Quoted 22 lines · Frederick Lawler

While creating a LSM BPF MAC policy to block user namespace creation, we used the LSM cred_prepare hook because that is the closest hook to prevent a call to create_user_ns().

The calls look something like this:

     cred = prepare_creds()
         security_prepare_creds()
             call_int_hook(cred_prepare, ...
     if (cred)
         create_user_ns(cred)

We noticed that error codes were not propagated from this hook and introduced a patch [1] to propagate those errors.

The discussion notes that security_prepare_creds() is not appropriate for MAC policies, and instead the hook is meant for LSM authors to prepare credentials for mutation. [2]

Ultimately, we concluded that a better course of action is to introduce a new security hook for LSM authors. [3]

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

This patch set first introduces a new security_create_user_ns() function and create_user_ns LSM hook, then marks the hook as sleepable in BPF.

Why restrict this hook to user namespaces? It seems that an LSM that chooses to preform controls on user namespaces may want to do so for network namespaces as well.

IIRC, CLONE_NEWUSER is the only namespace flag that does not require CAP_SYS_ADMIN.

LSM hooks are (or should be) orthogonal to capabilities, except for where they are required to implement capabilities.

Quoted 1 line

There is a security use case to prevent this namespace from being created within an unprivileged environment.

Yes, which is why some people argued against allowing unprivileged creation of user namespaces.

Quoted 1 line

I'm not opposed to a more generic hook, but I don't currently have a use case to block any others. We can also say the same is true for the other namespaces: add this generic security function to these too.

If the only reason to have the hook is to disallow unprivileged user namespaces it's probably time to revise the decision to always allow them. Make it a build or runtime option. That would address the issue more directly than creating a security module.

Quoted 11 lines

I'm curious what others think about this too.

Quoted 5 lines

Also, the hook seems backwards. You should
decide if the creation of the namespace is allowed before you create it. Passing the new namespace to a function that checks to see creating a namespace is allowed doesn't make a lot off sense.

I think having more context to a security hook is a good thing. I believe you brought up in the previous discussions that you'd like to use this hook for xattr purposes. Doesn't that require a namespace?

I'm not saying the information isn't required. But if you create a new namespace and then decide the user isn't allowed to create a namespace you have to tear it down. That's ugly. Better to pass the creation parameters to the hook before creating the namespace.

The relationship between xattrs and namespaces is it's own can of worms.

Quoted 23 lines
Quoted 22 lines
Quoted 22 lines

Links:
1. https://lore.kernel.org/all/[email protected]/
2. https://lore.kernel.org/all/[email protected]/
3. https://lore.kernel.org/all/[email protected]/

Frederick Lawler (2):
   security, lsm: Introduce security_create_user_ns()
   bpf-lsm: Make bpf_lsm_create_user_ns() sleepable

  include/linux/lsm_hook_defs.h | 2 ++
  include/linux/lsm_hooks.h     | 5 +++++
  include/linux/security.h      | 8 ++++++++
  kernel/bpf/bpf_lsm.c          | 1 +
  kernel/user_namespace.c       | 5 +++++
  security/security.c           | 6 ++++++

  6 files changed, 27 insertions(+)
Signature · 1 line
2.30.2
#6 lore raw
In reply to Frederick Lawler
Quoted 26 lines · Frederick Lawler

Hi Casey,

Quoted 23 lines · Casey Schaufler
Quoted 22 lines · Frederick Lawler

While creating a LSM BPF MAC policy to block user namespace creation, we used the LSM cred_prepare hook because that is the closest hook to prevent a call to create_user_ns().

The calls look something like this:

     cred = prepare_creds()
         security_prepare_creds()
             call_int_hook(cred_prepare, ...
     if (cred)
         create_user_ns(cred)

We noticed that error codes were not propagated from this hook and introduced a patch [1] to propagate those errors.

The discussion notes that security_prepare_creds() is not appropriate for MAC policies, and instead the hook is meant for LSM authors to prepare credentials for mutation. [2]

Ultimately, we concluded that a better course of action is to introduce a new security hook for LSM authors. [3]

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

This patch set first introduces a new security_create_user_ns() function and create_user_ns LSM hook, then marks the hook as sleepable in BPF.

Why restrict this hook to user namespaces? It seems that an LSM that chooses to preform controls on user namespaces may want to do so for network namespaces as well.

IIRC, CLONE_NEWUSER is the only namespace flag that does not require CAP_SYS_ADMIN.

LSM hooks are (or should be) orthogonal to capabilities, except for where they are required to implement capabilities.

Quoted 1 line

There is a security use case to prevent this namespace from being created within an unprivileged environment.

Yes, which is why some people argued against allowing unprivileged creation of user namespaces.

Quoted 1 line

I'm not opposed to a more generic hook, but I don't currently have a use case to block any others. We can also say the same is true for the other namespaces: add this generic security function to these too.

If the only reason to have the hook is to disallow unprivileged user namespaces it's probably time to revise the decision to always allow them. Make it a build or runtime option. That would address the issue more directly than creating a security module.

Quoted 11 lines

I'm curious what others think about this too.

Quoted 5 lines

Also, the hook seems backwards. You should
decide if the creation of the namespace is allowed before you create it. Passing the new namespace to a function that checks to see creating a namespace is allowed doesn't make a lot off sense.

I think having more context to a security hook is a good thing. I believe you brought up in the previous discussions that you'd like to use this hook for xattr purposes. Doesn't that require a namespace?

I'm not saying the information isn't required. But if you create a new namespace and then decide the user isn't allowed to create a namespace you have to tear it down. That's ugly. Better to pass the creation parameters to the hook before creating the namespace.

The relationship between xattrs and namespaces is it's own can of worms.

Quoted 23 lines
Quoted 22 lines
Quoted 22 lines

Links:
1. https://lore.kernel.org/all/[email protected]/
2. https://lore.kernel.org/all/[email protected]/
3. https://lore.kernel.org/all/[email protected]/

Frederick Lawler (2):
   security, lsm: Introduce security_create_user_ns()
   bpf-lsm: Make bpf_lsm_create_user_ns() sleepable

  include/linux/lsm_hook_defs.h | 2 ++
  include/linux/lsm_hooks.h     | 5 +++++
  include/linux/security.h      | 8 ++++++++
  kernel/bpf/bpf_lsm.c          | 1 +
  kernel/user_namespace.c       | 5 +++++
  security/security.c           | 6 ++++++

  6 files changed, 27 insertions(+)
Signature · 1 line
2.30.2
maintainer
#7 lore raw
In reply to Frederick Lawler
Quoted 32 lines · Frederick Lawler
Quoted 24 lines · Casey Schaufler
Quoted 23 lines · Frederick Lawler

While creating a LSM BPF MAC policy to block user namespace creation, we used the LSM cred_prepare hook because that is the closest hook to prevent
a call to create_user_ns().

The calls look something like this:

cred = prepare_creds()
security_prepare_creds()
call_int_hook(cred_prepare, ...
if (cred)
create_user_ns(cred)

We noticed that error codes were not propagated from this hook and introduced a patch [1] to propagate those errors.

The discussion notes that security_prepare_creds() is not appropriate for MAC policies, and instead the hook is meant for LSM authors to prepare credentials for mutation. [2]

Ultimately, we concluded that a better course of action is to introduce a new security hook for LSM authors. [3]

Quoted 6 lines
Quoted 2 lines

This patch set first introduces a new security_create_user_ns() function and create_user_ns LSM hook, then marks the hook as sleepable in BPF.

Why restrict this hook to user namespaces? It seems that an LSM that chooses to preform controls on user namespaces may want to do so for network namespaces as well.

Quoted 8 lines

IIRC, CLONE_NEWUSER is the only namespace flag that does not require CAP_SYS_ADMIN. There is a security use case to prevent this namespace from being created within an unprivileged environment. I'm not opposed to a more generic hook, but I don't currently have a use case to block any others. We can also say the same is true for the other namespaces:
add this generic security function to these too.

I'm curious what others think about this too.

While user namespaces are obviously one of the more significant namespaces from a security perspective, I do think it seems reasonable that the LSMs could benefit from additional namespace creation hooks. However, I don't think we need to do all of them at once, starting with a userns hook seems okay to me.

I also think that using the same LSM hook as an access control point for all of the different namespaces would be a mistake. At the very least we would need to pass a flag or some form of context to the hook to indicate which new namespace(s) are being requested and I fear that is a problem waiting to happen. That isn't to say someone couldn't mistakenly call the security_create_user_ns(...) from the mount namespace code, but I suspect that is much easier to identify as wrong than the equivalent security_create_ns(USER, ...).

We also should acknowledge that while in most cases the current task's credentials are probably sufficient to make any LSM access control decisions around namespace creation, it's possible that for some namespaces we would need to pass additional, namespace specific info to the LSM. With a shared LSM hook this could become rather awkward.

Quoted 6 lines
Quoted 4 lines

Also, the hook seems backwards. You should
decide if the creation of the namespace is allowed before you create it. Passing the new namespace to a function that checks to see creating a namespace is allowed doesn't make a lot off sense.

I think having more context to a security hook is a good thing.

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

[aside: If you would like to explore the SELinux implementation let me know, I'm happy to work with you on this. I suspect Casey and the other LSM maintainers would also be willing to do the same for their LSMs.]

In this particular case I think the calling task's credentials are generally all that is needed. You mention that the newly created namespace would be helpful, so I'll ask: what info in the new ns do you believe would be helpful in making an access decision about its creation?

Once we've sorted that we can make a better decision about the hook placement, but right now my gut feeling is that we only need to pass the task's creds, and I think placing the hook right after the UID/GID mapping check (before the new ns allocation) would be the best spot.

Signature · 1 line
paul-moore.com
#8 lore raw
Quoted 42 lines · Paul Moore
Quoted 32 lines · Frederick Lawler
Quoted 24 lines · Casey Schaufler
Quoted 23 lines · Frederick Lawler

While creating a LSM BPF MAC policy to block user namespace creation, we used the LSM cred_prepare hook because that is the closest hook to prevent
a call to create_user_ns().

The calls look something like this:

cred = prepare_creds()
security_prepare_creds()
call_int_hook(cred_prepare, ...
if (cred)
create_user_ns(cred)

We noticed that error codes were not propagated from this hook and introduced a patch [1] to propagate those errors.

The discussion notes that security_prepare_creds() is not appropriate for MAC policies, and instead the hook is meant for LSM authors to prepare credentials for mutation. [2]

Ultimately, we concluded that a better course of action is to introduce a new security hook for LSM authors. [3]

Quoted 6 lines
Quoted 2 lines

This patch set first introduces a new security_create_user_ns() function and create_user_ns LSM hook, then marks the hook as sleepable in BPF.

Why restrict this hook to user namespaces? It seems that an LSM that chooses to preform controls on user namespaces may want to do so for network namespaces as well.

Quoted 8 lines

IIRC, CLONE_NEWUSER is the only namespace flag that does not require CAP_SYS_ADMIN. There is a security use case to prevent this namespace from being created within an unprivileged environment. I'm not opposed to a more generic hook, but I don't currently have a use case to block any others. We can also say the same is true for the other namespaces:
add this generic security function to these too.

I'm curious what others think about this too.

Quoted 8 lines

While user namespaces are obviously one of the more significant namespaces from a security perspective, I do think it seems reasonable that the LSMs could benefit from additional namespace creation hooks. However, I don't think we need to do all of them at once, starting with a userns hook seems okay to me.

I also think that using the same LSM hook as an access control point for all of the different namespaces would be a mistake. At the very

Agreed.

Quoted 6 lines

least we would need to pass a flag or some form of context to the hook to indicate which new namespace(s) are being requested and I fear that is a problem waiting to happen. That isn't to say someone couldn't mistakenly call the security_create_user_ns(...) from the mount namespace code, but I suspect that is much easier to identify as wrong than the equivalent security_create_ns(USER, ...).

Yeah, I think that's a pretty unlikely scenario.

Quoted 6 lines

We also should acknowledge that while in most cases the current task's credentials are probably sufficient to make any LSM access control decisions around namespace creation, it's possible that for some namespaces we would need to pass additional, namespace specific info to the LSM. With a shared LSM hook this could become rather awkward.

Agreed.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Also, the hook seems backwards. You should
decide if the creation of the namespace is allowed before you create it. Passing the new namespace to a function that checks to see creating a namespace is allowed doesn't make a lot off sense.

Quoted 7 lines
Quoted 1 line

I think having more context to a security hook is a good thing.

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Quoted 8 lines

[aside: If you would like to explore the SELinux implementation let me know, I'm happy to work with you on this. I suspect Casey and the other LSM maintainers would also be willing to do the same for their LSMs.]

In this particular case I think the calling task's credentials are generally all that is needed. You mention that the newly created

Agreed.

Quoted 8 lines

namespace would be helpful, so I'll ask: what info in the new ns do you believe would be helpful in making an access decision about its creation?

Once we've sorted that we can make a better decision about the hook placement, but right now my gut feeling is that we only need to pass the task's creds, and I think placing the hook right after the UID/GID mapping check (before the new ns allocation) would be the best spot.

When I toyed with this I placed it directly into create_user_ns() and only relied on the calling task's cred. I just created an eBPF program that verifies the caller is capable(CAP_SYS_ADMIN). Since both the chrooted and mapping check return EPERM it doesn't really matter that much where exactly. Conceptually it makes more sense to me to place it after the mapping check because then all the preliminaries are done.

Christian

#9 lore raw
Quoted 85 lines · Christian Brauner
Quoted 42 lines · Paul Moore
Quoted 32 lines · Frederick Lawler
Quoted 24 lines · Casey Schaufler
Quoted 23 lines · Frederick Lawler

While creating a LSM BPF MAC policy to block user namespace creation, we used the LSM cred_prepare hook because that is the closest hook to prevent
a call to create_user_ns().

The calls look something like this:

cred = prepare_creds()
security_prepare_creds()
call_int_hook(cred_prepare, ...
if (cred)
create_user_ns(cred)

We noticed that error codes were not propagated from this hook and introduced a patch [1] to propagate those errors.

The discussion notes that security_prepare_creds() is not appropriate for MAC policies, and instead the hook is meant for LSM authors to prepare credentials for mutation. [2]

Ultimately, we concluded that a better course of action is to introduce a new security hook for LSM authors. [3]

Quoted 6 lines
Quoted 2 lines

This patch set first introduces a new security_create_user_ns() function and create_user_ns LSM hook, then marks the hook as sleepable in BPF.

Why restrict this hook to user namespaces? It seems that an LSM that chooses to preform controls on user namespaces may want to do so for network namespaces as well.

Quoted 8 lines

IIRC, CLONE_NEWUSER is the only namespace flag that does not require CAP_SYS_ADMIN. There is a security use case to prevent this namespace from being created within an unprivileged environment. I'm not opposed to a more generic hook, but I don't currently have a use case to block any others. We can also say the same is true for the other namespaces:
add this generic security function to these too.

I'm curious what others think about this too.

Quoted 8 lines

While user namespaces are obviously one of the more significant namespaces from a security perspective, I do think it seems reasonable that the LSMs could benefit from additional namespace creation hooks. However, I don't think we need to do all of them at once, starting with a userns hook seems okay to me.

I also think that using the same LSM hook as an access control point for all of the different namespaces would be a mistake. At the very

Agreed. >

Quoted 6 lines

least we would need to pass a flag or some form of context to the hook to indicate which new namespace(s) are being requested and I fear that is a problem waiting to happen. That isn't to say someone couldn't mistakenly call the security_create_user_ns(...) from the mount namespace code, but I suspect that is much easier to identify as wrong than the equivalent security_create_ns(USER, ...).

Yeah, I think that's a pretty unlikely scenario.

Quoted 6 lines

We also should acknowledge that while in most cases the current task's credentials are probably sufficient to make any LSM access control decisions around namespace creation, it's possible that for some namespaces we would need to pass additional, namespace specific info to the LSM. With a shared LSM hook this could become rather awkward.

Agreed.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Also, the hook seems backwards. You should
decide if the creation of the namespace is allowed before you create it. Passing the new namespace to a function that checks to see creating a namespace is allowed doesn't make a lot off sense.

Quoted 7 lines
Quoted 1 line

I think having more context to a security hook is a good thing.

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

Quoted 12 lines

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Quoted 6 lines

[aside: If you would like to explore the SELinux implementation let me know, I'm happy to work with you on this. I suspect Casey and the other LSM maintainers would also be willing to do the same for their LSMs.]

I can take a shot at making a SELinux implementation, but the question becomes: is that for v2 or a later patch? I don't think the implementation for SELinux would be too complicated (i.e. make a call to avc_has_perm()?) but, testing and revisions might take a bit longer.

Quoted 9 lines
Quoted 2 lines

In this particular case I think the calling task's credentials are generally all that is needed. You mention that the newly created

Agreed.

Quoted 4 lines

namespace would be helpful, so I'll ask: what info in the new ns do you believe would be helpful in making an access decision about its creation?

In the other thread [1], there was mention of xattr mapping support. As I understand Caseys response to this thread [2], that feature is no longer requested for this hook.

Users can still access the older parent ns from the passed in cred, but I was thinking of handling the transition point here. There's probably more suitable hooks for that case.

Quoted 5 lines
Quoted 4 lines

Once we've sorted that we can make a better decision about the hook placement, but right now my gut feeling is that we only need to pass the task's creds, and I think placing the hook right after the UID/GID mapping check (before the new ns allocation) would be the best spot.

I don't specifically have a use case to pass the new user namespace for this hook at this time. I'll move the hook in v2.

Quoted 7 lines

When I toyed with this I placed it directly into create_user_ns() and only relied on the calling task's cred. I just created an eBPF program that verifies the caller is capable(CAP_SYS_ADMIN). Since both the chrooted and mapping check return EPERM it doesn't really matter that much where exactly. Conceptually it makes more sense to me to place it after the mapping check because then all the preliminaries are done.

Agreed.

Quoted 1 line

Christian

Links:
1. https://lore.kernel.org/all/[email protected]/
2. https://lore.kernel.org/all/[email protected]/

#10 lore raw
Quoted 109 lines · Frederick Lawler
Quoted 85 lines · Christian Brauner
Quoted 42 lines · Paul Moore
Quoted 32 lines · Frederick Lawler
Quoted 24 lines · Casey Schaufler
Quoted 23 lines · Frederick Lawler

While creating a LSM BPF MAC policy to block user namespace creation, we used the LSM cred_prepare hook because that is the closest hook to prevent
a call to create_user_ns().

The calls look something like this:

cred = prepare_creds()
security_prepare_creds()
call_int_hook(cred_prepare, ...
if (cred)
create_user_ns(cred)

We noticed that error codes were not propagated from this hook and introduced a patch [1] to propagate those errors.

The discussion notes that security_prepare_creds() is not appropriate for MAC policies, and instead the hook is meant for LSM authors to prepare credentials for mutation. [2]

Ultimately, we concluded that a better course of action is to introduce a new security hook for LSM authors. [3]

Quoted 6 lines
Quoted 2 lines

This patch set first introduces a new security_create_user_ns() function and create_user_ns LSM hook, then marks the hook as sleepable in BPF.

Why restrict this hook to user namespaces? It seems that an LSM that chooses to preform controls on user namespaces may want to do so for network namespaces as well.

Quoted 8 lines

IIRC, CLONE_NEWUSER is the only namespace flag that does not require CAP_SYS_ADMIN. There is a security use case to prevent this namespace from being created within an unprivileged environment. I'm not opposed to a more generic hook, but I don't currently have a use case to block any others. We can also say the same is true for the other namespaces:
add this generic security function to these too.

I'm curious what others think about this too.

Quoted 8 lines

While user namespaces are obviously one of the more significant namespaces from a security perspective, I do think it seems reasonable that the LSMs could benefit from additional namespace creation hooks. However, I don't think we need to do all of them at once, starting with a userns hook seems okay to me.

I also think that using the same LSM hook as an access control point for all of the different namespaces would be a mistake. At the very

Agreed. >

Quoted 6 lines

least we would need to pass a flag or some form of context to the hook to indicate which new namespace(s) are being requested and I fear that is a problem waiting to happen. That isn't to say someone couldn't mistakenly call the security_create_user_ns(...) from the mount namespace code, but I suspect that is much easier to identify as wrong than the equivalent security_create_ns(USER, ...).

Yeah, I think that's a pretty unlikely scenario.

Quoted 6 lines

We also should acknowledge that while in most cases the current task's credentials are probably sufficient to make any LSM access control decisions around namespace creation, it's possible that for some namespaces we would need to pass additional, namespace specific info to the LSM. With a shared LSM hook this could become rather awkward.

Agreed.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Also, the hook seems backwards. You should
decide if the creation of the namespace is allowed before you create it. Passing the new namespace to a function that checks to see creating a namespace is allowed doesn't make a lot off sense.

Quoted 7 lines
Quoted 1 line

I think having more context to a security hook is a good thing.

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

Quoted 12 lines

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Quoted 6 lines

[aside: If you would like to explore the SELinux implementation let me know, I'm happy to work with you on this. I suspect Casey and the other LSM maintainers would also be willing to do the same for their LSMs.]

I can take a shot at making a SELinux implementation, but the question
becomes: is that for v2 or a later patch? I don't think the implementation for SELinux would be too complicated (i.e. make a call to avc_has_perm()?) but, testing and revisions might take a bit longer.

Quoted 5 lines
Quoted 2 lines

In this particular case I think the calling task's credentials are generally all that is needed. You mention that the newly created

Agreed.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

namespace would be helpful, so I'll ask: what info in the new ns do you believe would be helpful in making an access decision about its creation?

In the other thread [1], there was mention of xattr mapping support. As I understand Caseys response to this thread [2], that feature is no longer requested for this hook.

I think that is an orthogonal problem at least wrt to this hook.

Quoted 4 lines

Users can still access the older parent ns from the passed in cred, but I was thinking of handling the transition point here. There's probably more suitable hooks for that case.

Yes.

#11 lore raw
Quoted 76 lines · Christian Brauner
Quoted 75 lines · Frederick Lawler
Quoted 74 lines · Christian Brauner
Quoted 39 lines · Paul Moore
Quoted 30 lines · Frederick Lawler
Quoted 21 lines · Casey Schaufler
Quoted 20 lines · Frederick Lawler

While creating a LSM BPF MAC policy to block user namespace creation, we used the LSM cred_prepare hook because that is the closest hook to prevent
a call to create_user_ns().

The calls look something like this:

cred = prepare_creds()
security_prepare_creds()
call_int_hook(cred_prepare, ...
if (cred)
create_user_ns(cred)

We noticed that error codes were not propagated from this hook and introduced a patch [1] to propagate those errors.

The discussion notes that security_prepare_creds() is not appropriate for MAC policies, and instead the hook is meant for LSM authors to prepare credentials for mutation. [2]

Quoted 8 lines
Quoted 5 lines

Ultimately, we concluded that a better course of action is to introduce a new security hook for LSM authors. [3]

This patch set first introduces a new security_create_user_ns() function and create_user_ns LSM hook, then marks the hook as sleepable in BPF.

Why restrict this hook to user namespaces? It seems that an LSM that chooses to preform controls on user namespaces may want to do so for network namespaces as well.

Quoted 8 lines

IIRC, CLONE_NEWUSER is the only namespace flag that does not require CAP_SYS_ADMIN. There is a security use case to prevent this namespace from being created within an unprivileged environment. I'm not opposed to a more generic hook, but I don't currently have a use case to block any others. We can also say the same is true for the other namespaces:
add this generic security function to these too.

I'm curious what others think about this too.

Quoted 8 lines

While user namespaces are obviously one of the more significant namespaces from a security perspective, I do think it seems reasonable that the LSMs could benefit from additional namespace creation hooks. However, I don't think we need to do all of them at once, starting with a userns hook seems okay to me.

I also think that using the same LSM hook as an access control point for all of the different namespaces would be a mistake. At the very

Agreed. >

Quoted 6 lines

least we would need to pass a flag or some form of context to the hook to indicate which new namespace(s) are being requested and I fear that is a problem waiting to happen. That isn't to say someone couldn't mistakenly call the security_create_user_ns(...) from the mount namespace code, but I suspect that is much easier to identify as wrong than the equivalent security_create_ns(USER, ...).

Yeah, I think that's a pretty unlikely scenario.

Quoted 5 lines

We also should acknowledge that while in most cases the current task's credentials are probably sufficient to make any LSM access control decisions around namespace creation, it's possible that for some namespaces we would need to pass additional, namespace specific info to the LSM. With a shared LSM hook this could become rather awkward.

Agreed.

Quoted 10 lines
Quoted 5 lines
Quoted 4 lines

Also, the hook seems backwards. You should
decide if the creation of the namespace is allowed before you create it. Passing the new namespace to a function that checks to see creating a namespace is allowed doesn't make a lot off sense.

I think having more context to a security hook is a good thing.

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Yes, although it would be really helpful if there were a recognized upstream for eBPF programs so that we could see not only that the hook is used but how it is being used. It is possible (even likely) that someone will want to change either the interface or the caller some day. Without having the eBPF that depends on it, it's hard to determine if a change would be a regression.

Quoted 15 lines
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines

[aside: If you would like to explore the SELinux implementation let me know, I'm happy to work with you on this. I suspect Casey and the other LSM maintainers would also be willing to do the same for their LSMs.]

I can take a shot at making a SELinux implementation, but the question
becomes: is that for v2 or a later patch? I don't think the implementation for SELinux would be too complicated (i.e. make a call to avc_has_perm()?) but, testing and revisions might take a bit longer.

Quoted 4 lines
Quoted 2 lines

In this particular case I think the calling task's credentials are generally all that is needed. You mention that the newly created

Agreed.

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines

namespace would be helpful, so I'll ask: what info in the new ns do you believe would be helpful in making an access decision about its creation?

In the other thread [1], there was mention of xattr mapping support. As I understand Caseys response to this thread [2], that feature is no longer requested for this hook.

I think that is an orthogonal problem at least wrt to this hook.

Agreed. It was always a look deeply into the future sort of thing. At this point I don't see anything blocking the proposed hook moving forward.

Quoted 5 lines
Quoted 4 lines

Users can still access the older parent ns from the passed in cred, but I was thinking of handling the transition point here. There's probably more suitable hooks for that case.

Yes.

maintainer
#13 lore raw
In reply to Christian Brauner
Quoted 1 line · Christian Brauner

On Thu, Jun 23, 2022 at 11:21:37PM -0400, Paul Moore wrote:

...

Quoted 11 lines
Quoted 5 lines

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Yes, for the most part I don't really worry about the "is a BPF LSM a LSM?" question, it's generally not important for most discussions. However, there is an issue unique to the BPF LSMs which I think is relevant here: there is no hook implementation code living under security/. While I talked about a hook implementation being helpful to verify the hook prototype, it is also helpful in providing an in-tree example for other LSMs; unfortunately we don't get that same example value when the initial hook implementation is a BPF LSM.

Signature · 1 line
paul-moore.com
maintainer
#12 lore raw
In reply to Frederick Lawler
Quoted 5 lines · Frederick Lawler
Quoted 4 lines · Christian Brauner
Quoted 3 lines · Paul Moore
Quoted 2 lines · Frederick Lawler
Quoted 1 line · Casey Schaufler

On 6/21/2022 4:39 PM, Frederick Lawler wrote:

...

Quoted 94 lines
Quoted 81 lines
Quoted 39 lines
Quoted 30 lines
Quoted 23 lines
Quoted 23 lines

While creating a LSM BPF MAC policy to block user namespace creation, we used the LSM cred_prepare hook because that is the closest hook to prevent
a call to create_user_ns().

The calls look something like this:

cred = prepare_creds()
security_prepare_creds()
call_int_hook(cred_prepare, ...
if (cred)
create_user_ns(cred)

We noticed that error codes were not propagated from this hook and introduced a patch [1] to propagate those errors.

The discussion notes that security_prepare_creds() is not appropriate for MAC policies, and instead the hook is meant for LSM authors to prepare credentials for mutation. [2]

Ultimately, we concluded that a better course of action is to introduce a new security hook for LSM authors. [3]

Quoted 6 lines
Quoted 2 lines

This patch set first introduces a new security_create_user_ns() function and create_user_ns LSM hook, then marks the hook as sleepable in BPF.

Why restrict this hook to user namespaces? It seems that an LSM that chooses to preform controls on user namespaces may want to do so for network namespaces as well.

Quoted 8 lines

IIRC, CLONE_NEWUSER is the only namespace flag that does not require CAP_SYS_ADMIN. There is a security use case to prevent this namespace from being created within an unprivileged environment. I'm not opposed to a more generic hook, but I don't currently have a use case to block any others. We can also say the same is true for the other namespaces:
add this generic security function to these too.

I'm curious what others think about this too.

Quoted 8 lines

While user namespaces are obviously one of the more significant namespaces from a security perspective, I do think it seems reasonable that the LSMs could benefit from additional namespace creation hooks. However, I don't think we need to do all of them at once, starting with a userns hook seems okay to me.

I also think that using the same LSM hook as an access control point for all of the different namespaces would be a mistake. At the very

Agreed. >

Quoted 6 lines

least we would need to pass a flag or some form of context to the hook to indicate which new namespace(s) are being requested and I fear that is a problem waiting to happen. That isn't to say someone couldn't mistakenly call the security_create_user_ns(...) from the mount namespace code, but I suspect that is much easier to identify as wrong than the equivalent security_create_ns(USER, ...).

Yeah, I think that's a pretty unlikely scenario.

Quoted 6 lines

We also should acknowledge that while in most cases the current task's credentials are probably sufficient to make any LSM access control decisions around namespace creation, it's possible that for some namespaces we would need to pass additional, namespace specific info to the LSM. With a shared LSM hook this could become rather awkward.

Agreed.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Also, the hook seems backwards. You should
decide if the creation of the namespace is allowed before you create it. Passing the new namespace to a function that checks to see creating a namespace is allowed doesn't make a lot off sense.

Quoted 7 lines
Quoted 1 line

I think having more context to a security hook is a good thing.

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

Quoted 12 lines

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Quoted 6 lines

[aside: If you would like to explore the SELinux implementation let me know, I'm happy to work with you on this. I suspect Casey and the other LSM maintainers would also be willing to do the same for their LSMs.]

Quoted 4 lines

I can take a shot at making a SELinux implementation, but the question
becomes: is that for v2 or a later patch? I don't think the implementation for SELinux would be too complicated (i.e. make a call to avc_has_perm()?) but, testing and revisions might take a bit longer.

Isn't that the truth, writing code is easy(ish); testing it well is the hard part ;) Joking aside, I would suggest starting with v2.

As an example, the code below might be a good place to start - we would need to discuss this on the SELinux list as there are some design decisions I'm glossing over[1].

  int selinux_userns_create(struct cred *new)
  {
    u32 sid = current_sid();

return avc_has_perm(&selinux_state, sid, sid,
SECCLASS_NAMESPACE, NAMESPACE__USERNS_CREATE);
}

You would also need to add the "namespace" class and the "userns_create" permission in security/selinux/include/classmap.h.

  const struct security_class_mapping secclass_map[] = {
    ...
    { "namespace",
      { "userns_create", NULL } },
  }

As I mentioned earlier, if you find yourself getting stuck, or needing some help, please feel free to send mail.

[1] This code snippet uses a new object class and permission for this (namespace:userns_create). I made that choice as object classes are limited to 32 unique permissions and I expect the number of namespaces to continue to grow.

Signature · 1 line
paul-moore.com
maintainer
#14 lore raw
In reply to Paul Moore
Quoted 17 lines · Paul Moore
Quoted 1 line · Christian Brauner

On Thu, Jun 23, 2022 at 11:21:37PM -0400, Paul Moore wrote:

...

Quoted 11 lines
Quoted 5 lines

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Quoted 8 lines

Yes, for the most part I don't really worry about the "is a BPF LSM a LSM?" question, it's generally not important for most discussions. However, there is an issue unique to the BPF LSMs which I think is relevant here: there is no hook implementation code living under security/. While I talked about a hook implementation being helpful to verify the hook prototype, it is also helpful in providing an in-tree example for other LSMs; unfortunately we don't get that same example value when the initial hook implementation is a BPF LSM.

I would argue that such a patch series must come together with a BPF selftest which then i) contains an in-tree usage example, ii) adds BPF CI test coverage. Shipping with a BPF selftest at least would be the usual expectation.

Thanks,
Daniel

maintainer
#15 lore raw
Quoted 28 lines · Daniel Borkmann
Quoted 17 lines · Paul Moore
Quoted 1 line · Christian Brauner

On Thu, Jun 23, 2022 at 11:21:37PM -0400, Paul Moore wrote:

...

Quoted 11 lines
Quoted 5 lines

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Quoted 8 lines

Yes, for the most part I don't really worry about the "is a BPF LSM a LSM?" question, it's generally not important for most discussions. However, there is an issue unique to the BPF LSMs which I think is relevant here: there is no hook implementation code living under security/. While I talked about a hook implementation being helpful to verify the hook prototype, it is also helpful in providing an in-tree example for other LSMs; unfortunately we don't get that same example value when the initial hook implementation is a BPF LSM.

Quoted 4 lines

I would argue that such a patch series must come together with a BPF selftest which then i) contains an in-tree usage example, ii) adds BPF CI test coverage. Shipping with a BPF selftest at least would be the usual expectation.

+1 I would also recommend that this comes with a BPF selftest as suggested by Daniel.

Quoted 3 lines

Thanks,
Daniel

maintainer
#16 lore raw
In reply to Daniel Borkmann
Quoted 27 lines · Daniel Borkmann
Quoted 17 lines · Paul Moore
Quoted 1 line · Christian Brauner

On Thu, Jun 23, 2022 at 11:21:37PM -0400, Paul Moore wrote:

...

Quoted 11 lines
Quoted 5 lines

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Quoted 8 lines

Yes, for the most part I don't really worry about the "is a BPF LSM a LSM?" question, it's generally not important for most discussions. However, there is an issue unique to the BPF LSMs which I think is relevant here: there is no hook implementation code living under security/. While I talked about a hook implementation being helpful to verify the hook prototype, it is also helpful in providing an in-tree example for other LSMs; unfortunately we don't get that same example value when the initial hook implementation is a BPF LSM.

Quoted 4 lines

I would argue that such a patch series must come together with a BPF selftest which then i) contains an in-tree usage example, ii) adds BPF CI test coverage. Shipping with a BPF selftest at least would be the usual expectation.

I'm not going to disagree with that, I generally require matching tests for new SELinux kernel code, but I was careful to mention code under 'security/' and not necessarily just a test implementation :) I don't want to get into a big discussion about it, but I think having a working implementation somewhere under 'security/' is more discoverable for most LSM folks.

Signature · 1 line
paul-moore.com
#17 lore raw
Quoted 28 lines · Paul Moore
Quoted 23 lines · Daniel Borkmann
Quoted 14 lines · Paul Moore
Quoted 1 line · Christian Brauner

On Thu, Jun 23, 2022 at 11:21:37PM -0400, Paul Moore wrote:

...

Quoted 10 lines
Quoted 5 lines

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Quoted 8 lines

Yes, for the most part I don't really worry about the "is a BPF LSM a LSM?" question, it's generally not important for most discussions. However, there is an issue unique to the BPF LSMs which I think is relevant here: there is no hook implementation code living under security/. While I talked about a hook implementation being helpful to verify the hook prototype, it is also helpful in providing an in-tree example for other LSMs; unfortunately we don't get that same example value when the initial hook implementation is a BPF LSM.

Quoted 4 lines

I would argue that such a patch series must come together with a BPF selftest which then i) contains an in-tree usage example, ii) adds BPF CI test coverage. Shipping with a BPF selftest at least would be the usual expectation.

Quoted 6 lines

I'm not going to disagree with that, I generally require matching tests for new SELinux kernel code, but I was careful to mention code under 'security/' and not necessarily just a test implementation :) I don't want to get into a big discussion about it, but I think having a working implementation somewhere under 'security/' is more discoverable for most LSM folks.

I agree. It would be unfortunate if we added a hook explicitly for eBPF only to discover that the proposed user needs something different. The LSM community should have a chance to review the code before committing to all the maintenance required in supporting it.

Is there a reference on how to write an eBPF security module? There should be something out there warning the eBPF programmer of the implications of providing a secid_to_secctx hook for starters.

#22 lore raw
In reply to Daniel Borkmann
Quoted 27 lines · Daniel Borkmann
Quoted 17 lines · Paul Moore
Quoted 1 line · Christian Brauner

On Thu, Jun 23, 2022 at 11:21:37PM -0400, Paul Moore wrote:

...

Quoted 11 lines
Quoted 5 lines

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook.  The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Quoted 8 lines

Yes, for the most part I don't really worry about the "is a BPF LSM a LSM?" question, it's generally not important for most discussions. However, there is an issue unique to the BPF LSMs which I think is relevant here: there is no hook implementation code living under security/.  While I talked about a hook implementation being helpful to verify the hook prototype, it is also helpful in providing an in-tree example for other LSMs; unfortunately we don't get that same example value when the initial hook implementation is a BPF LSM.

Quoted 4 lines

I would argue that such a patch series must come together with a BPF selftest which then i) contains an in-tree usage example, ii) adds BPF CI test coverage. Shipping with a BPF selftest at least would be the usual expectation.

Sounds good. I'll add both a eBPF selftest and SELinux implementation for v2.

Quoted 3 lines

Thanks,
Daniel

maintainer
#23 lore raw
Quoted 29 lines · Frederick Lawler
Quoted 28 lines · Daniel Borkmann
Quoted 26 lines · Paul Moore
Quoted 1 line · Christian Brauner

On Thu, Jun 23, 2022 at 11:21:37PM -0400, Paul Moore wrote:

...

Quoted 11 lines
Quoted 5 lines

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Yes, for the most part I don't really worry about the "is a BPF LSM a LSM?" question, it's generally not important for most discussions. However, there is an issue unique to the BPF LSMs which I think is relevant here: there is no hook implementation code living under security/. While I talked about a hook implementation being helpful to verify the hook prototype, it is also helpful in providing an in-tree example for other LSMs; unfortunately we don't get that same example value when the initial hook implementation is a BPF LSM.

Quoted 7 lines
Quoted 4 lines

I would argue that such a patch series must come together with a BPF selftest which then i) contains an in-tree usage example, ii) adds BPF CI test coverage. Shipping with a BPF selftest at least would be the usual expectation.

Sounds good. I'll add both a eBPF selftest and SELinux implementation for v2.

Thanks Daniel!

Signature · 1 line
paul-moore.com
#18 lore raw
In reply to Casey Schaufler
Quoted 36 lines · Casey Schaufler
Quoted 28 lines · Paul Moore
Quoted 23 lines · Daniel Borkmann
Quoted 14 lines · Paul Moore
Quoted 1 line · Christian Brauner

On Thu, Jun 23, 2022 at 11:21:37PM -0400, Paul Moore wrote:

...

Quoted 10 lines
Quoted 5 lines

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook.  The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Quoted 8 lines

Yes, for the most part I don't really worry about the "is a BPF LSM a LSM?" question, it's generally not important for most discussions. However, there is an issue unique to the BPF LSMs which I think is relevant here: there is no hook implementation code living under security/.  While I talked about a hook implementation being helpful to verify the hook prototype, it is also helpful in providing an in-tree example for other LSMs; unfortunately we don't get that same example value when the initial hook implementation is a BPF LSM.

Quoted 4 lines

I would argue that such a patch series must come together with a BPF selftest which then i) contains an in-tree usage example, ii) adds BPF CI test coverage. Shipping with a BPF selftest at least would be the usual expectation.

Quoted 6 lines

I'm not going to disagree with that, I generally require matching tests for new SELinux kernel code, but I was careful to mention code under 'security/' and not necessarily just a test implementation :)  I don't want to get into a big discussion about it, but I think having a working implementation somewhere under 'security/' is more discoverable for most LSM folks.

Quoted 6 lines

I agree. It would be unfortunate if we added a hook explicitly for eBPF only to discover that the proposed user needs something different. The LSM community should have a chance to review the code before committing to all the maintenance required in supporting it.

Is there a reference on how to write an eBPF security module?

There's a documentation page that briefly touches on a BPF LSM implementation [1].

Quoted 3 lines

There should be something out there warning the eBPF programmer of the implications of providing a secid_to_secctx hook for starters.

Links:
1. https://docs.kernel.org/bpf/prog_lsm.html?highlight=bpf+lsm#

#19 lore raw
Quoted 37 lines · Frederick Lawler
Quoted 36 lines · Casey Schaufler
Quoted 34 lines · Paul Moore
Quoted 23 lines · Daniel Borkmann
Quoted 14 lines · Paul Moore
Quoted 1 line · Christian Brauner

On Thu, Jun 23, 2022 at 11:21:37PM -0400, Paul Moore wrote:

...

Quoted 10 lines
Quoted 5 lines

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook.  The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Quoted 8 lines

Yes, for the most part I don't really worry about the "is a BPF LSM a LSM?" question, it's generally not important for most discussions. However, there is an issue unique to the BPF LSMs which I think is relevant here: there is no hook implementation code living under security/.  While I talked about a hook implementation being helpful to verify the hook prototype, it is also helpful in providing an in-tree example for other LSMs; unfortunately we don't get that same example value when the initial hook implementation is a BPF LSM.

Quoted 4 lines

I would argue that such a patch series must come together with a BPF selftest which then i) contains an in-tree usage example, ii) adds BPF CI test coverage. Shipping with a BPF selftest at least would be the usual expectation.

I'm not going to disagree with that, I generally require matching tests for new SELinux kernel code, but I was careful to mention code under 'security/' and not necessarily just a test implementation :)  I don't want to get into a big discussion about it, but I think having a working implementation somewhere under 'security/' is more discoverable for most LSM folks.

Quoted 8 lines
Quoted 6 lines

I agree. It would be unfortunate if we added a hook explicitly for eBPF only to discover that the proposed user needs something different. The LSM community should have a chance to review the code before committing to all the maintenance required in supporting it.

Is there a reference on how to write an eBPF security module?

There's a documentation page that briefly touches on a BPF LSM implementation [1].

That's a brief touch, alright. I'll grant that the LSM interface isn't especially well documented for C developers, but we have done tutorials and have multiple examples. I worry that without an in-tree example for eBPF we might well be setting developers up for spectacular failure.

Quoted 8 lines
Quoted 4 lines

There should be something out there warning the eBPF programmer of the implications of providing a secid_to_secctx hook for starters.

Links:
1. https://docs.kernel.org/bpf/prog_lsm.html?highlight=bpf+lsm#

maintainer
#20 lore raw
Quoted 44 lines · Casey Schaufler
Quoted 42 lines · Frederick Lawler
Quoted 41 lines · Casey Schaufler
Quoted 28 lines · Paul Moore
Quoted 23 lines · Daniel Borkmann
Quoted 14 lines · Paul Moore
Quoted 1 line · Christian Brauner

On Thu, Jun 23, 2022 at 11:21:37PM -0400, Paul Moore wrote:

...

Quoted 10 lines
Quoted 5 lines

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Quoted 8 lines

Yes, for the most part I don't really worry about the "is a BPF LSM a LSM?" question, it's generally not important for most discussions. However, there is an issue unique to the BPF LSMs which I think is relevant here: there is no hook implementation code living under security/. While I talked about a hook implementation being helpful to verify the hook prototype, it is also helpful in providing an in-tree example for other LSMs; unfortunately we don't get that same example value when the initial hook implementation is a BPF LSM.

Quoted 4 lines

I would argue that such a patch series must come together with a BPF selftest which then i) contains an in-tree usage example, ii) adds BPF CI test coverage. Shipping with a BPF selftest at least would be the usual expectation.

Quoted 6 lines

I'm not going to disagree with that, I generally require matching tests for new SELinux kernel code, but I was careful to mention code under 'security/' and not necessarily just a test implementation :) I don't want to get into a big discussion about it, but I think having a working implementation somewhere under 'security/' is more discoverable for most LSM folks.

I agree. It would be unfortunate if we added a hook explicitly for eBPF only to discover that the proposed user needs something different. The LSM community should have a chance to review the code before committing to all the maintenance required in supporting it.

Quoted 9 lines
Quoted 3 lines
Quoted 1 line

Is there a reference on how to write an eBPF security module?

There's a documentation page that briefly touches on a BPF LSM implementation [1].

That's a brief touch, alright. I'll grant that the LSM interface isn't especially well documented for C developers, but we have done tutorials and have multiple examples. I worry that without an in-tree example for eBPF we might well be setting developers up for spectacular failure.

Casey, Daniel and I are recommending an in-tree example, it will be in BPF selftests and we will CC you on the reviews.

Frederick, is that okay with you?

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

There should be something out there warning the eBPF programmer of the implications of providing a secid_to_secctx hook for starters.

Links:
1. https://docs.kernel.org/bpf/prog_lsm.html?highlight=bpf+lsm#

#21 lore raw
Quoted 55 lines · KP Singh
Quoted 44 lines · Casey Schaufler
Quoted 42 lines · Frederick Lawler
Quoted 41 lines · Casey Schaufler
Quoted 28 lines · Paul Moore
Quoted 23 lines · Daniel Borkmann
Quoted 14 lines · Paul Moore
Quoted 1 line · Christian Brauner

On Thu, Jun 23, 2022 at 11:21:37PM -0400, Paul Moore wrote:

...

Quoted 10 lines
Quoted 5 lines

This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :)

I spoke to Frederick at length during LSS and as I've been given to understand there's a eBPF program that would immediately use this new hook. Now I don't want to get into the whole "Is the eBPF LSM hook infrastructure an LSM" but I think we can let this count as a legitimate first user of this hook/code.

Quoted 8 lines

Yes, for the most part I don't really worry about the "is a BPF LSM a LSM?" question, it's generally not important for most discussions. However, there is an issue unique to the BPF LSMs which I think is relevant here: there is no hook implementation code living under security/. While I talked about a hook implementation being helpful to verify the hook prototype, it is also helpful in providing an in-tree example for other LSMs; unfortunately we don't get that same example value when the initial hook implementation is a BPF LSM.

Quoted 4 lines

I would argue that such a patch series must come together with a BPF selftest which then i) contains an in-tree usage example, ii) adds BPF CI test coverage. Shipping with a BPF selftest at least would be the usual expectation.

Quoted 6 lines

I'm not going to disagree with that, I generally require matching tests for new SELinux kernel code, but I was careful to mention code under 'security/' and not necessarily just a test implementation :) I don't want to get into a big discussion about it, but I think having a working implementation somewhere under 'security/' is more discoverable for most LSM folks.

I agree. It would be unfortunate if we added a hook explicitly for eBPF only to discover that the proposed user needs something different. The LSM community should have a chance to review the code before committing to all the maintenance required in supporting it.

Quoted 9 lines
Quoted 3 lines
Quoted 1 line

Is there a reference on how to write an eBPF security module?

There's a documentation page that briefly touches on a BPF LSM implementation [1].

That's a brief touch, alright. I'll grant that the LSM interface isn't especially well documented for C developers, but we have done tutorials and have multiple examples. I worry that without an in-tree example for eBPF we might well be setting developers up for spectacular failure.

Quoted 4 lines

Casey, Daniel and I are recommending an in-tree example, it will be in BPF selftests and we will CC you on the reviews.

Frederick, is that okay with you?

Yep.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines

There should be something out there warning the eBPF programmer of the implications of providing a secid_to_secctx hook for starters.

Links:
1. https://docs.kernel.org/bpf/prog_lsm.html?highlight=bpf+lsm#

#24 lore raw
In reply to Frederick Lawler
Quoted 31 lines · Frederick Lawler

Hi Casey,

Quoted 20 lines · Casey Schaufler
Quoted 19 lines · Frederick Lawler

While creating a LSM BPF MAC policy to block user namespace creation, we used the LSM cred_prepare hook because that is the closest hook to prevent a call to create_user_ns().

The calls look something like this:

     cred = prepare_creds()
         security_prepare_creds()
             call_int_hook(cred_prepare, ...
     if (cred)
         create_user_ns(cred)

We noticed that error codes were not propagated from this hook and introduced a patch [1] to propagate those errors.

The discussion notes that security_prepare_creds() is not appropriate for MAC policies, and instead the hook is meant for LSM authors to prepare credentials for mutation. [2]

Quoted 8 lines
Quoted 5 lines

Ultimately, we concluded that a better course of action is to introduce a new security hook for LSM authors. [3]

This patch set first introduces a new security_create_user_ns() function and create_user_ns LSM hook, then marks the hook as sleepable in BPF.

Why restrict this hook to user namespaces? It seems that an LSM that chooses to preform controls on user namespaces may want to do so for network namespaces as well.

Quoted 6 lines

IIRC, CLONE_NEWUSER is the only namespace flag that does not require CAP_SYS_ADMIN. There is a security use case to prevent this namespace from being created within an unprivileged environment. I'm not opposed to a more generic hook, but I don't currently have a use case to block any others. We can also say the same is true for the other namespaces: add this generic security function to these too.

There is also a very strong security use case for not putting security checks in the creation of the user namespace.

In particular there are all kinds of kernel features that are useful for building sandboxes namespaces, chroot, etc, that previous to user namespaces were not possible to make available to unprivileged users because they could confuse suid-root executables. With user namespaces the concern about confusing suid-root executable goes away.

The only justification I have ever heard for restricting the user namespace is because it indirectly allows for greater kernel attack surface.

Do you have a case for restricting the user namespace other than the kernel is buggy and the user namespace makes the kernel bugs easier to access?

How does increasing the attack surface of the kernel make the situation that the kernel is buggy and the attack surface is too big better?

Perhaps it is explained somewhere down-thread and I just have not caught up yet, but so far I have not see a description of why it makes sense for a security module to restrict the kernel here.

Eric

p.s. I am little disappointed that I was not copied on this thread given that it is my code you are messing with, and I was in an earlier version of this thread.

#25 lore raw
Quoted 49 lines · Eric W. Biederman
Quoted 31 lines · Frederick Lawler

Hi Casey,

Quoted 20 lines · Casey Schaufler
Quoted 19 lines · Frederick Lawler

While creating a LSM BPF MAC policy to block user namespace creation, we used the LSM cred_prepare hook because that is the closest hook to prevent a call to create_user_ns().

The calls look something like this:

     cred = prepare_creds()
         security_prepare_creds()
             call_int_hook(cred_prepare, ...
     if (cred)
         create_user_ns(cred)

We noticed that error codes were not propagated from this hook and introduced a patch [1] to propagate those errors.

The discussion notes that security_prepare_creds() is not appropriate for MAC policies, and instead the hook is meant for LSM authors to prepare credentials for mutation. [2]

Quoted 8 lines
Quoted 5 lines

Ultimately, we concluded that a better course of action is to introduce a new security hook for LSM authors. [3]

This patch set first introduces a new security_create_user_ns() function and create_user_ns LSM hook, then marks the hook as sleepable in BPF.

Why restrict this hook to user namespaces? It seems that an LSM that chooses to preform controls on user namespaces may want to do so for network namespaces as well.

Quoted 6 lines

IIRC, CLONE_NEWUSER is the only namespace flag that does not require CAP_SYS_ADMIN. There is a security use case to prevent this namespace from being created within an unprivileged environment. I'm not opposed to a more generic hook, but I don't currently have a use case to block any others. We can also say the same is true for the other namespaces: add this generic security function to these too.

There is also a very strong security use case for not putting security checks in the creation of the user namespace.

In particular there are all kinds of kernel features that are useful for building sandboxes namespaces, chroot, etc, that previous to user namespaces were not possible to make available to unprivileged users because they could confuse suid-root executables. With user namespaces the concern about confusing suid-root executable goes away.

Quoted 7 lines

The only justification I have ever heard for restricting the user namespace is because it indirectly allows for greater kernel attack surface.

Do you have a case for restricting the user namespace other than the kernel is buggy and the user namespace makes the kernel bugs easier to access?

No.

Quoted 4 lines

How does increasing the attack surface of the kernel make the situation that the kernel is buggy and the attack surface is too big better?

We agree that completely blocking this feature may disable effective sandboxing techniques, but uncontrolled user namespace creation increases the attack surface as well. There are known examples where creating a new namespace is the first step in a priv escalation attack.

Quoted 4 lines

Perhaps it is explained somewhere down-thread and I just have not caught up yet, but so far I have not see a description of why it makes sense for a security module to restrict the kernel here.

Having a new security hook allows us to decide when unpriv user namespace creation is acceptable and unacceptable. Patching vulnerabilities takes time and effort amongst the community. We want to protect our systems so that we are not as impacted by the next 0-day disclosure. This in turn decreases our overall attack surface.

Security hooks are a good fit because they offer us flexibility to protect our systems how we see fit.

Quoted 5 lines

Eric

p.s. I am little disappointed that I was not copied on this thread given that it is my code you are messing with, and I was in an earlier version of this thread.