Skip to contentSkip to conversation

memfd,selinux: call security_inode_init_security_anon

PATCH Superseded Thiébaud Weksteen 3 participants 6 messages Sep 8, 2025 – Sep 9, 2025

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 memfd,selinux: call security_inode_init_security_anon

Prior to this change, no security hooks were called at the creation of a memfd file. It means that, for SELinux as an example, it will receive the default type of the filesystem that backs the in-memory inode. In most cases, that would be tmpfs, but if MFD_HUGETLB is passed, it will be hugetlbfs. Both can be considered implementation details of memfd.

It also means that it is not possible to differentiate between a file coming from memfd_create and a file coming from a standard tmpfs mount point.

Additionally, no permission is validated at creation, which differs from the similar memfd_secret syscall.

Call security_inode_init_security_anon during creation. This ensures that the file is setup similarly to other anonymous inodes. On SELinux, it means that the file will receive the security context of its task.

The ability to limit fexecve on memfd has been of interest to avoid potential pitfalls where /proc/self/exe or similar would be executed [1][2]. Reuse the "execute_no_trans" and "entrypoint" access vectors, similarly to the file class. These access vectors may not make sense for the existing "anon_inode" class. Therefore, define and assign a new class "memfd_file" to support such access vectors.

Guard these changes behind a new policy capability named "memfd_class".

[1] https://crbug.com/1305267
[2] https://lore.kernel.org/lkml/[email protected]/

---
Changes since v1:
- Move test of class earlier in selinux_bprm_creds_for_exec
- Remove duplicate call to security_transition_sid

Changes since RFC:
- Remove enum argument, simply compare the anon inode name
- Introduce a policy capability for compatility
- Add validation of class in selinux_bprm_creds_for_exec

 include/linux/memfd.h                      |  2 ++
 mm/memfd.c                                 | 14 ++++++++++--
 security/selinux/hooks.c                   | 26 +++++++++++++++++-----
 security/selinux/include/classmap.h        |  2 ++
 security/selinux/include/policycap.h       |  1 +
 security/selinux/include/policycap_names.h |  1 +
 security/selinux/include/security.h        |  5 +++++
 7 files changed, 44 insertions(+), 7 deletions(-)
include/linux/memfd.h +2 -0
index 6f606d9573c3..cc74de3dbcfe 100644
@@ -4,6 +4,8 @@
 
 #include <linux/file.h>
 
+#define MEMFD_ANON_NAME "[memfd]"
+
 #ifdef CONFIG_MEMFD_CREATE
 extern long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg);
 struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx);
mm/memfd.c +12 -2
index bbe679895ef6..63b439eb402a 100644
@@ -433,6 +433,8 @@ static struct file *alloc_file(const char *name, unsigned int flags)
 {
 	unsigned int *file_seals;
 	struct file *file;
+	struct inode *inode;
+	int err = 0;
 
 	if (flags & MFD_HUGETLB) {
 		file = hugetlb_file_setup(name, 0, VM_NORESERVE,
@@ -444,12 +446,20 @@ static struct file *alloc_file(const char *name, unsigned int flags)
 	}
 	if (IS_ERR(file))
 		return file;
+
+	inode = file_inode(file);
+	err = security_inode_init_security_anon(inode,
+			&QSTR(MEMFD_ANON_NAME), NULL);
+	if (err) {
+		fput(file);
+		file = ERR_PTR(err);
+		return file;
+	}
+
 	file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
 	file->f_flags |= O_LARGEFILE;
 
 	if (flags & MFD_NOEXEC_SEAL) {
-		struct inode *inode = file_inode(file);
-
 		inode->i_mode &= ~0111;
 		file_seals = memfd_file_seals_ptr(file);
 		if (file_seals) {
security/selinux/hooks.c +21 -5
index c95a5874bf7d..6adf2f393ed9 100644
@@ -93,6 +93,7 @@
 #include <linux/fanotify.h>
 #include <linux/io_uring/cmd.h>
 #include <uapi/linux/lsm.h>
+#include <linux/memfd.h>
 
 #include "avc.h"
 #include "objsec.h"
@@ -2315,6 +2316,9 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
 	new_tsec = selinux_cred(bprm->cred);
 	isec = inode_security(inode);
 
+	if (isec->sclass != SECCLASS_FILE && isec->sclass != SECCLASS_MEMFD_FILE)
+		return -EPERM;
+
 	/* Default to the current task SID. */
 	new_tsec->sid = old_tsec->sid;
 	new_tsec->osid = old_tsec->sid;
@@ -2366,9 +2370,10 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
 	ad.type = LSM_AUDIT_DATA_FILE;
 	ad.u.file = bprm->file;
 
+
 	if (new_tsec->sid == old_tsec->sid) {
-		rc = avc_has_perm(old_tsec->sid, isec->sid,
-				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
+		rc = avc_has_perm(old_tsec->sid, isec->sid, isec->sclass,
+				  FILE__EXECUTE_NO_TRANS, &ad);
 		if (rc)
 			return rc;
 	} else {
@@ -2378,8 +2383,8 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
 		if (rc)
 			return rc;
 
-		rc = avc_has_perm(new_tsec->sid, isec->sid,
-				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
+		rc = avc_has_perm(new_tsec->sid, isec->sid, isec->sclass,
+				  FILE__ENTRYPOINT, &ad);
 		if (rc)
 			return rc;
 
@@ -2974,10 +2979,18 @@ static int selinux_inode_init_security_anon(struct inode *inode,
 	struct common_audit_data ad;
 	struct inode_security_struct *isec;
 	int rc;
+	bool is_memfd = false;
 
 	if (unlikely(!selinux_initialized()))
 		return 0;
 
+	if (name != NULL && name->name != NULL &&
+	    !strcmp(name->name, MEMFD_ANON_NAME)) {
+		if (!selinux_policycap_memfd_class())
+			return 0;
+		is_memfd = true;
+	}
+
 	isec = selinux_inode(inode);
 
 	/*
@@ -2997,7 +3010,10 @@ static int selinux_inode_init_security_anon(struct inode *inode,
 		isec->sclass = context_isec->sclass;
 		isec->sid = context_isec->sid;
 	} else {
-		isec->sclass = SECCLASS_ANON_INODE;
+		if (is_memfd)
+			isec->sclass = SECCLASS_MEMFD_FILE;
+		else
+			isec->sclass = SECCLASS_ANON_INODE;
 		rc = security_transition_sid(
 			sid, sid,
 			isec->sclass, name, &isec->sid);
security/selinux/include/classmap.h +2 -0
index 5665aa5e7853..3ec85142771f 100644
@@ -179,6 +179,8 @@ const struct security_class_mapping secclass_map[] = {
 	{ "anon_inode", { COMMON_FILE_PERMS, NULL } },
 	{ "io_uring", { "override_creds", "sqpoll", "cmd", "allowed", NULL } },
 	{ "user_namespace", { "create", NULL } },
+	{ "memfd_file",
+	  { COMMON_FILE_PERMS, "execute_no_trans", "entrypoint", NULL } },
 	/* last one */ { NULL, {} }
 };
 
security/selinux/include/policycap.h +1 -0
index 7405154e6c42..dabcc9f14dde 100644
@@ -17,6 +17,7 @@ enum {
 	POLICYDB_CAP_NETLINK_XPERM,
 	POLICYDB_CAP_NETIF_WILDCARD,
 	POLICYDB_CAP_GENFS_SECLABEL_WILDCARD,
+	POLICYDB_CAP_MEMFD_CLASS,
 	__POLICYDB_CAP_MAX
 };
 #define POLICYDB_CAP_MAX (__POLICYDB_CAP_MAX - 1)
security/selinux/include/policycap_names.h +1 -0
index d8962fcf2ff9..8e96f2a816b6 100644
@@ -20,6 +20,7 @@ const char *const selinux_policycap_names[__POLICYDB_CAP_MAX] = {
 	"netlink_xperm",
 	"netif_wildcard",
 	"genfs_seclabel_wildcard",
+	"memfd_class",
 };
 /* clang-format on */
 
security/selinux/include/security.h +5 -0
index 8201e6a3ac0f..72c963f54148 100644
@@ -209,6 +209,11 @@ static inline bool selinux_policycap_netif_wildcard(void)
 		selinux_state.policycap[POLICYDB_CAP_NETIF_WILDCARD]);
 }
 
+static inline bool selinux_policycap_memfd_class(void)
+{
+	return READ_ONCE(selinux_state.policycap[POLICYDB_CAP_MEMFD_CLASS]);
+}
+
 struct selinux_policy_convert_data;
 
 struct selinux_load_state {
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
maintainer
#1 lore raw
Quoted 25 lines · Thiébaud Weksteen

Prior to this change, no security hooks were called at the creation of a memfd file. It means that, for SELinux as an example, it will receive the default type of the filesystem that backs the in-memory inode. In most cases, that would be tmpfs, but if MFD_HUGETLB is passed, it will be hugetlbfs. Both can be considered implementation details of memfd.

It also means that it is not possible to differentiate between a file coming from memfd_create and a file coming from a standard tmpfs mount point.

Additionally, no permission is validated at creation, which differs from the similar memfd_secret syscall.

Call security_inode_init_security_anon during creation. This ensures that the file is setup similarly to other anonymous inodes. On SELinux, it means that the file will receive the security context of its task.

The ability to limit fexecve on memfd has been of interest to avoid potential pitfalls where /proc/self/exe or similar would be executed [1][2]. Reuse the "execute_no_trans" and "entrypoint" access vectors, similarly to the file class. These access vectors may not make sense for the existing "anon_inode" class. Therefore, define and assign a new class "memfd_file" to support such access vectors.

Quoted 8 lines

Guard these changes behind a new policy capability named "memfd_class".

[1] https://crbug.com/1305267
[2] https://lore.kernel.org/lkml/[email protected]/

When you revise a patch, you aren't supposed to retain other's tags since they haven't technically reviewed, agreed to, or tested the revised change.
That said, I have now done so and thus these tags can remain!

Quoted 201 lines

---
Changes since v1:
- Move test of class earlier in selinux_bprm_creds_for_exec
- Remove duplicate call to security_transition_sid

Changes since RFC:
- Remove enum argument, simply compare the anon inode name
- Introduce a policy capability for compatility
- Add validation of class in selinux_bprm_creds_for_exec

 include/linux/memfd.h                      |  2 ++
 mm/memfd.c                                 | 14 ++++++++++--
 security/selinux/hooks.c                   | 26 +++++++++++++++++-----
 security/selinux/include/classmap.h        |  2 ++
 security/selinux/include/policycap.h       |  1 +
 security/selinux/include/policycap_names.h |  1 +
 security/selinux/include/security.h        |  5 +++++
 7 files changed, 44 insertions(+), 7 deletions(-)
include/linux/memfd.h +2 -0
index 6f606d9573c3..cc74de3dbcfe 100644
@@ -4,6 +4,8 @@
 
 #include <linux/file.h>
 
+#define MEMFD_ANON_NAME "[memfd]"
+
 #ifdef CONFIG_MEMFD_CREATE
 extern long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg);
 struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx);
mm/memfd.c +12 -2
index bbe679895ef6..63b439eb402a 100644
@@ -433,6 +433,8 @@ static struct file *alloc_file(const char *name, unsigned int flags)
 {
        unsigned int *file_seals;
        struct file *file;
+       struct inode *inode;
+       int err = 0;
 
        if (flags & MFD_HUGETLB) {
                file = hugetlb_file_setup(name, 0, VM_NORESERVE,
@@ -444,12 +446,20 @@ static struct file *alloc_file(const char *name, unsigned int flags)
        }
        if (IS_ERR(file))
                return file;
+
+       inode = file_inode(file);
+       err = security_inode_init_security_anon(inode,
+                       &QSTR(MEMFD_ANON_NAME), NULL);
+       if (err) {
+               fput(file);
+               file = ERR_PTR(err);
+               return file;
+       }
+
        file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
        file->f_flags |= O_LARGEFILE;
 
        if (flags & MFD_NOEXEC_SEAL) {
-               struct inode *inode = file_inode(file);
-
                inode->i_mode &= ~0111;
                file_seals = memfd_file_seals_ptr(file);
                if (file_seals) {
security/selinux/hooks.c +21 -5
index c95a5874bf7d..6adf2f393ed9 100644
@@ -93,6 +93,7 @@
 #include <linux/fanotify.h>
 #include <linux/io_uring/cmd.h>
 #include <uapi/linux/lsm.h>
+#include <linux/memfd.h>
 
 #include "avc.h"
 #include "objsec.h"
@@ -2315,6 +2316,9 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
        new_tsec = selinux_cred(bprm->cred);
        isec = inode_security(inode);
 
+       if (isec->sclass != SECCLASS_FILE && isec->sclass != SECCLASS_MEMFD_FILE)
+               return -EPERM;
+
        /* Default to the current task SID. */
        new_tsec->sid = old_tsec->sid;
        new_tsec->osid = old_tsec->sid;
@@ -2366,9 +2370,10 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
        ad.type = LSM_AUDIT_DATA_FILE;
        ad.u.file = bprm->file;
 
+
        if (new_tsec->sid == old_tsec->sid) {
-               rc = avc_has_perm(old_tsec->sid, isec->sid,
-                                 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
+               rc = avc_has_perm(old_tsec->sid, isec->sid, isec->sclass,
+                                 FILE__EXECUTE_NO_TRANS, &ad);
                if (rc)
                        return rc;
        } else {
@@ -2378,8 +2383,8 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
                if (rc)
                        return rc;
 
-               rc = avc_has_perm(new_tsec->sid, isec->sid,
-                                 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
+               rc = avc_has_perm(new_tsec->sid, isec->sid, isec->sclass,
+                                 FILE__ENTRYPOINT, &ad);
                if (rc)
                        return rc;
 
@@ -2974,10 +2979,18 @@ static int selinux_inode_init_security_anon(struct inode *inode,
        struct common_audit_data ad;
        struct inode_security_struct *isec;
        int rc;
+       bool is_memfd = false;
 
        if (unlikely(!selinux_initialized()))
                return 0;
 
+       if (name != NULL && name->name != NULL &&
+           !strcmp(name->name, MEMFD_ANON_NAME)) {
+               if (!selinux_policycap_memfd_class())
+                       return 0;
+               is_memfd = true;
+       }
+
        isec = selinux_inode(inode);
 
        /*
@@ -2997,7 +3010,10 @@ static int selinux_inode_init_security_anon(struct inode *inode,
                isec->sclass = context_isec->sclass;
                isec->sid = context_isec->sid;
        } else {
-               isec->sclass = SECCLASS_ANON_INODE;
+               if (is_memfd)
+                       isec->sclass = SECCLASS_MEMFD_FILE;
+               else
+                       isec->sclass = SECCLASS_ANON_INODE;
                rc = security_transition_sid(
                        sid, sid,
                        isec->sclass, name, &isec->sid);
security/selinux/include/classmap.h +2 -0
index 5665aa5e7853..3ec85142771f 100644
@@ -179,6 +179,8 @@ const struct security_class_mapping secclass_map[] = {
        { "anon_inode", { COMMON_FILE_PERMS, NULL } },
        { "io_uring", { "override_creds", "sqpoll", "cmd", "allowed", NULL } },
        { "user_namespace", { "create", NULL } },
+       { "memfd_file",
+         { COMMON_FILE_PERMS, "execute_no_trans", "entrypoint", NULL } },
        /* last one */ { NULL, {} }
 };
 
security/selinux/include/policycap.h +1 -0
index 7405154e6c42..dabcc9f14dde 100644
@@ -17,6 +17,7 @@ enum {
        POLICYDB_CAP_NETLINK_XPERM,
        POLICYDB_CAP_NETIF_WILDCARD,
        POLICYDB_CAP_GENFS_SECLABEL_WILDCARD,
+       POLICYDB_CAP_MEMFD_CLASS,
        __POLICYDB_CAP_MAX
 };
 #define POLICYDB_CAP_MAX (__POLICYDB_CAP_MAX - 1)
security/selinux/include/policycap_names.h +1 -0
index d8962fcf2ff9..8e96f2a816b6 100644
@@ -20,6 +20,7 @@ const char *const selinux_policycap_names[__POLICYDB_CAP_MAX] = {
        "netlink_xperm",
        "netif_wildcard",
        "genfs_seclabel_wildcard",
+       "memfd_class",
 };
 /* clang-format on */
 
security/selinux/include/security.h +5 -1
index 8201e6a3ac0f..72c963f54148 100644
@@ -209,6 +209,11 @@ static inline bool selinux_policycap_netif_wildcard(void)
                selinux_state.policycap[POLICYDB_CAP_NETIF_WILDCARD]);
 }
 
+static inline bool selinux_policycap_memfd_class(void)
+{
+       return READ_ONCE(selinux_state.policycap[POLICYDB_CAP_MEMFD_CLASS]);
+}
+
 struct selinux_policy_convert_data;
 
 struct selinux_load_state {
--

2.51.0.384.g4c02a37b29-goog

#2 lore raw
Quoted 32 lines · Stephen Smalley
Quoted 30 lines · Thiébaud Weksteen

Prior to this change, no security hooks were called at the creation of a memfd file. It means that, for SELinux as an example, it will receive the default type of the filesystem that backs the in-memory inode. In most cases, that would be tmpfs, but if MFD_HUGETLB is passed, it will be hugetlbfs. Both can be considered implementation details of memfd.

It also means that it is not possible to differentiate between a file coming from memfd_create and a file coming from a standard tmpfs mount point.

Additionally, no permission is validated at creation, which differs from the similar memfd_secret syscall.

Call security_inode_init_security_anon during creation. This ensures that the file is setup similarly to other anonymous inodes. On SELinux, it means that the file will receive the security context of its task.

The ability to limit fexecve on memfd has been of interest to avoid potential pitfalls where /proc/self/exe or similar would be executed [1][2]. Reuse the "execute_no_trans" and "entrypoint" access vectors, similarly to the file class. These access vectors may not make sense for the existing "anon_inode" class. Therefore, define and assign a new class "memfd_file" to support such access vectors.

Guard these changes behind a new policy capability named "memfd_class".

[1] https://crbug.com/1305267
[2] https://lore.kernel.org/lkml/[email protected]/

Quoted 9 lines
Quoted 3 lines

When you revise a patch, you aren't supposed to retain other's tags since they haven't technically reviewed, agreed to, or tested the revised change.
That said, I have now done so and thus these tags can remain!

I'm sorry for that. Thanks for the clarification, I wasn't sure what the process was. And thanks for the review!

maintainer
#3 lore raw
In reply to Thiébaud Weksteen
Quoted 58 lines · Thiébaud Weksteen

Prior to this change, no security hooks were called at the creation of a memfd file. It means that, for SELinux as an example, it will receive the default type of the filesystem that backs the in-memory inode. In most cases, that would be tmpfs, but if MFD_HUGETLB is passed, it will be hugetlbfs. Both can be considered implementation details of memfd.

It also means that it is not possible to differentiate between a file coming from memfd_create and a file coming from a standard tmpfs mount point.

Additionally, no permission is validated at creation, which differs from the similar memfd_secret syscall.

Call security_inode_init_security_anon during creation. This ensures that the file is setup similarly to other anonymous inodes. On SELinux, it means that the file will receive the security context of its task.

The ability to limit fexecve on memfd has been of interest to avoid potential pitfalls where /proc/self/exe or similar would be executed [1][2]. Reuse the "execute_no_trans" and "entrypoint" access vectors, similarly to the file class. These access vectors may not make sense for the existing "anon_inode" class. Therefore, define and assign a new class "memfd_file" to support such access vectors.

Guard these changes behind a new policy capability named "memfd_class".

[1] https://crbug.com/1305267
[2] https://lore.kernel.org/lkml/[email protected]/

---
Changes since v1:
- Move test of class earlier in selinux_bprm_creds_for_exec
- Remove duplicate call to security_transition_sid

Changes since RFC:
- Remove enum argument, simply compare the anon inode name
- Introduce a policy capability for compatility
- Add validation of class in selinux_bprm_creds_for_exec

 include/linux/memfd.h                      |  2 ++
 mm/memfd.c                                 | 14 ++++++++++--
 security/selinux/hooks.c                   | 26 +++++++++++++++++-----
 security/selinux/include/classmap.h        |  2 ++
 security/selinux/include/policycap.h       |  1 +
 security/selinux/include/policycap_names.h |  1 +
 security/selinux/include/security.h        |  5 +++++
 7 files changed, 44 insertions(+), 7 deletions(-)
security/selinux/hooks.c +0 -0
index c95a5874bf7d..6adf2f393ed9 100644

security/selinux/hooks.c static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm) · line 2321

Quoted 6 lines
security/selinux/hooks.c +3 -0
@@ -2315,6 +2316,9 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
        new_tsec = selinux_cred(bprm->cred);
        isec = inode_security(inode);
 
+       if (isec->sclass != SECCLASS_FILE && isec->sclass != SECCLASS_MEMFD_FILE)
+               return -EPERM;
+

Sorry, I should have mentioned this earlier, but usually we try to avoid triggering silent denials from SELinux since it provides no hint to the user as to what went wrong or how to resolve. Arguably reaching this code would be suggestive of a kernel bug but I know that BUG_ON() is frowned upon these days.
Maybe we should WARN_ON_ONCE() here or similar? We also rarely return -EPERM from SELinux outside of capability checks since usually EPERM means a failed capability check
(vs -EACCES). Defer to Paul on how/if he wants to handle this and whether it requires re-spinning this patch or just a follow-on one.

Quoted 3 lines
security/selinux/hooks.c +0 -0
        /* Default to the current task SID. */
        new_tsec->sid = old_tsec->sid;
        new_tsec->osid = old_tsec->sid;
maintainer
#4 lore raw
Quoted 62 lines · Stephen Smalley
Quoted 61 lines · Thiébaud Weksteen

Prior to this change, no security hooks were called at the creation of a memfd file. It means that, for SELinux as an example, it will receive the default type of the filesystem that backs the in-memory inode. In most cases, that would be tmpfs, but if MFD_HUGETLB is passed, it will be hugetlbfs. Both can be considered implementation details of memfd.

It also means that it is not possible to differentiate between a file coming from memfd_create and a file coming from a standard tmpfs mount point.

Additionally, no permission is validated at creation, which differs from the similar memfd_secret syscall.

Call security_inode_init_security_anon during creation. This ensures that the file is setup similarly to other anonymous inodes. On SELinux, it means that the file will receive the security context of its task.

The ability to limit fexecve on memfd has been of interest to avoid potential pitfalls where /proc/self/exe or similar would be executed [1][2]. Reuse the "execute_no_trans" and "entrypoint" access vectors, similarly to the file class. These access vectors may not make sense for the existing "anon_inode" class. Therefore, define and assign a new class "memfd_file" to support such access vectors.

Guard these changes behind a new policy capability named "memfd_class".

[1] https://crbug.com/1305267
[2] https://lore.kernel.org/lkml/[email protected]/

---
Changes since v1:
- Move test of class earlier in selinux_bprm_creds_for_exec
- Remove duplicate call to security_transition_sid

Changes since RFC:
- Remove enum argument, simply compare the anon inode name
- Introduce a policy capability for compatility
- Add validation of class in selinux_bprm_creds_for_exec

 include/linux/memfd.h                      |  2 ++
 mm/memfd.c                                 | 14 ++++++++++--
 security/selinux/hooks.c                   | 26 +++++++++++++++++-----
 security/selinux/include/classmap.h        |  2 ++
 security/selinux/include/policycap.h       |  1 +
 security/selinux/include/policycap_names.h |  1 +
 security/selinux/include/security.h        |  5 +++++
 7 files changed, 44 insertions(+), 7 deletions(-)
security/selinux/hooks.c +0 -0
index c95a5874bf7d..6adf2f393ed9 100644
@@ -2315,6 +2316,9 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
        new_tsec = selinux_cred(bprm->cred);
        isec = inode_security(inode);
 
Quoted 7 lines

security/selinux/hooks.c static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm) · line 2321

Quoted 3 lines
security/selinux/hooks.c +3 -0
+       if (isec->sclass != SECCLASS_FILE && isec->sclass != SECCLASS_MEMFD_FILE)
+               return -EPERM;
+

Sorry, I should have mentioned this earlier, but usually we try to avoid triggering silent denials from SELinux since it provides no hint to the user as to what went wrong or how to resolve.

Ooof, yeah, I should have noticed that too.

Quoted 3 lines

Arguably reaching this code would be suggestive of a kernel bug but I know that BUG_ON() is frowned upon these days.
Maybe we should WARN_ON_ONCE() here or similar?

BUG_ON() is definitely a no-no, but WARN_ON_ONCE()/WARN_ON() is still considered okay last I checked (no forced panic). Of the two I think WARN_ON() would be a better choice here.

Quoted 5 lines

We also rarely return
-EPERM from SELinux outside of capability checks since usually EPERM means a failed capability check
(vs -EACCES). Defer to Paul on how/if he wants to handle this and whether it requires re-spinning this patch or just a follow-on one.

Another fair point.

Considering that we are at -rc5 right now, we only have a few more days left in the current dev cycle, I'm going to merge this now (with a subject line tweak and some unnecessary vertical whitespace removed), and I'll put together a quick little patch to do the WARN_ON()/EACCES conversion which you'll see on list shortly ...

Signature · 1 line
paul-moore.com
maintainer
#5 lore raw
Quoted 6 lines · Paul Moore

Considering that we are at -rc5 right now, we only have a few more days left in the current dev cycle, I'm going to merge this now (with a subject line tweak and some unnecessary vertical whitespace removed), and I'll put together a quick little patch to do the WARN_ON()/EACCES conversion which you'll see on list shortly ...

The patch can be found at the link below.

https://lore.kernel.org/selinux/[email protected]/

Signature · 1 line
paul-moore.com