Skip to contentSkip to conversation

policy,tests: add tests for memfd_file class

PATCH Applied Thiébaud Weksteen 2 participants 4 messages Sep 8, 2025 – Sep 10, 2025 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 policy,tests: add tests for memfd_file class

A new class "memfd_file" is introduced to the kernel in [1]. This class is only used when the policy capability "memfd_class" is set. Add tests to validate this new class:

1. Validate that memfd_create() requires the "create" permission.
2. Validate that fexecve() on a memfd requires the "execute_no_trans" permission.

This can be tested by modifying the policy with:

  semodule -c -E base
  sed -i \
    -e 's/\((class user_namespace (create ))\)/\1\n(class memfd_file (execute_no_trans entrypoint ))\n(classcommon memfd_file file)/' \
    -e 's/\(anon_inode socket\)/memfd_file \1/' \
    base.cil
  echo "(policycap memfd_class)" >>base.cil
  semodule -X 456 -i base.cil
  rm -f base.cil

  sed -i.orig \
    -e 's/\(define(`all_file_perms'\'',\)\(.*\)$/\1\2\ndefine(`all_memfd_file_perms'\'',\2/' \
    -e 's/\(class file all_file_perms;\)/\1\nclass memfd_file all_memfd_file_perms;/' \
    /usr/share/selinux/devel/include/support/all_perms.spt

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

---
Changelog since v1:
- Add commands to test the policy to the commit message.
- Only check for class and capability to include the new tests.
- Ran check-syntax.

 policy/Makefile             |  5 ++++
 policy/test_memfd.te        | 25 +++++++++++++++++
 tests/Makefile              |  5 ++++
 tests/memfd/Makefile        |  5 ++++
 tests/memfd/memfd.c         | 24 +++++++++++++++++
 tests/memfd/memfd_fexecve.c | 53 +++++++++++++++++++++++++++++++++++++
 tests/memfd/nothing.c       |  6 +++++
 tests/memfd/test            | 41 ++++++++++++++++++++++++++++
 8 files changed, 164 insertions(+)
 create mode 100644 policy/test_memfd.te
 create mode 100644 tests/memfd/Makefile
 create mode 100644 tests/memfd/memfd.c
 create mode 100644 tests/memfd/memfd_fexecve.c
 create mode 100644 tests/memfd/nothing.c
 create mode 100755 tests/memfd/test
policy/Makefile +5 -0
index ffd774d..520f891 100644
@@ -163,6 +163,11 @@ ifeq ($(shell [ $(MOD_POL_VERS) -ge 18 -a $(MAX_KERNEL_POLICY) -ge 30 ] && [ -f
 TARGETS += test_nlmsg.te
 endif
 
+# memfd_file test dependencies: memfd_file class, memfd_class capability
+ifeq ($(shell [ -d /sys/fs/selinux/class/memfd_file ] && grep -q 1 $(SELINUXFS)/policy_capabilities/memfd_class && echo true),true)
+TARGETS += test_memfd.te
+endif
+
 ifeq (x$(DISTRO),$(filter x$(DISTRO),xRHEL4 xRHEL5 xRHEL6))
 TARGETS:=$(filter-out test_overlayfs.te test_mqueue.te test_ibpkey.te, $(TARGETS))
 endif
policy/test_memfd.te new file +25 -0
new file mode 100644
index 0000000..5ca1348
@@ -0,0 +1,25 @@
+########################################
+#
+# Policy for testing memfd_file.
+
+attribute memfdtestdomain;
+
+type test_memfd_t;
+typeattribute test_memfd_t memfdtestdomain;
+testsuite_domain_type(test_memfd_t);
+allow test_memfd_t self:memfd_file { create mmap_rw_file_perms };
+
+type test_memfd_nocreate_t;
+typeattribute test_memfd_nocreate_t memfdtestdomain;
+testsuite_domain_type(test_memfd_nocreate_t);
+allow test_memfd_nocreate_t self:memfd_file mmap_rw_file_perms;
+
+type test_memfd_with_exec_t;
+typeattribute test_memfd_with_exec_t memfdtestdomain;
+testsuite_domain_type(test_memfd_with_exec_t);
+allow test_memfd_with_exec_t self:memfd_file { create mmap_rw_file_perms execute execute_no_trans };
+
+type test_memfd_with_noexec_t;
+typeattribute test_memfd_with_noexec_t memfdtestdomain;
+testsuite_domain_type(test_memfd_with_noexec_t);
+allow test_memfd_with_noexec_t self:memfd_file { create mmap_rw_file_perms execute };
tests/Makefile +5 -0
index 7a6aace..903ab11 100644
@@ -158,6 +158,11 @@ ifeq ($(shell [ $(MOD_POL_VERS) -ge 18 -a $(MAX_KERNEL_POLICY) -ge 30 ] && [ -f
 SUBDIRS += nlmsg
 endif
 
+# memfd_file test dependencies: memfd_file class, memfd_class capability
+ifeq ($(shell [ -d /sys/fs/selinux/class/memfd_file ] && grep -q 1 $(SELINUXFS)/policy_capabilities/memfd_class && echo true),true)
+SUBDIRS += memfd
+endif
+
 ifeq ($(DISTRO),RHEL4)
     SUBDIRS:=$(filter-out bounds dyntrace dyntrans inet_socket mmap nnp_nosuid overlay unix_socket, $(SUBDIRS))
 endif
tests/memfd/Makefile new file +5 -0
new file mode 100644
index 0000000..471900f
@@ -0,0 +1,5 @@
+TARGETS=memfd nothing memfd_fexecve
+
+all: $(TARGETS)
+clean:
+	rm -f $(TARGETS)
tests/memfd/memfd.c new file +24 -0
new file mode 100644
index 0000000..0d6f838
@@ -0,0 +1,24 @@
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+int main(int argc, char *argv[])
+{
+	int fd;
+	char *name = "mymemfd";
+
+	fd = memfd_create(name, 0);
+	if (fd < 0) {
+		perror("memfd_create");
+		exit(-1);
+	}
+
+	close(fd);
+	exit(0);
+}
tests/memfd/memfd_fexecve.c new file +53 -0

53 lines not on this page · raw message

tests/memfd/nothing.c new file +6 -0
new file mode 100644
index 0000000..fc53d37
@@ -0,0 +1,6 @@
+#include <stdlib.h>
+
+int main(int argc, char *argv[])
+{
+	return 0;
+}
tests/memfd/test new file +41 -0

41 lines not on this page · raw message

Signature · 1 line
2.51.0.384.g4c02a37b29-goog
maintainer Reviewed-by Tested-by on v2 1/1
#1 lore raw
Quoted 11 lines · Thiébaud Weksteen

A new class "memfd_file" is introduced to the kernel in [1]. This class is only used when the policy capability "memfd_class" is set. Add tests to validate this new class:

1. Validate that memfd_create() requires the "create" permission.
2. Validate that fexecve() on a memfd requires the "execute_no_trans" permission.

This can be tested by modifying the policy with:

Quoted 7 lines
  semodule -c -E base
  sed -i \
    -e 's/\((class user_namespace (create ))\)/\1\n(class memfd_file (execute_no_trans entrypoint ))\n(classcommon memfd_file file)/' \
    -e 's/\(anon_inode socket\)/memfd_file \1/' \
    base.cil
  echo "(policycap memfd_class)" >>base.cil
  semodule -X 456 -i base.cil

No need to re-spin just for this, but for future reference, I'd just do the echo to a separate file (e.g. memfdclass.cil) and append -i memfdclass.cil to the semodule command line,
and I wouldn't bother specifying a priority (just let it use the default 400). The reason is that I usually do these via sudo and an echo with file redirect won't work as is when
appending to the root-owned base.cil file created by the earlier semodule -c -E base.

Quoted 6 lines

rm -f base.cil

  sed -i.orig \
    -e 's/\(define(`all_file_perms'\'',\)\(.*\)$/\1\2\ndefine(`all_memfd_file_perms'\'',\2/' \
    -e 's/\(class file all_file_perms;\)/\1\nclass memfd_file all_memfd_file_perms;/' \
    /usr/share/selinux/devel/include/support/all_perms.spt

Likewise, no need to re-spin just for this, but I'd then say to run "semodule -r base memfdclass" and
restore the all_perms.spt.org file to all_perms.spt.

Quoted 4 lines

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

Quoted 247 lines

---
Changelog since v1:
- Add commands to test the policy to the commit message.
- Only check for class and capability to include the new tests.
- Ran check-syntax.

 policy/Makefile             |  5 ++++
 policy/test_memfd.te        | 25 +++++++++++++++++
 tests/Makefile              |  5 ++++
 tests/memfd/Makefile        |  5 ++++
 tests/memfd/memfd.c         | 24 +++++++++++++++++
 tests/memfd/memfd_fexecve.c | 53 +++++++++++++++++++++++++++++++++++++
 tests/memfd/nothing.c       |  6 +++++
 tests/memfd/test            | 41 ++++++++++++++++++++++++++++
 8 files changed, 164 insertions(+)
 create mode 100644 policy/test_memfd.te
 create mode 100644 tests/memfd/Makefile
 create mode 100644 tests/memfd/memfd.c
 create mode 100644 tests/memfd/memfd_fexecve.c
 create mode 100644 tests/memfd/nothing.c
 create mode 100755 tests/memfd/test
policy/Makefile +5 -0
index ffd774d..520f891 100644
@@ -163,6 +163,11 @@ ifeq ($(shell [ $(MOD_POL_VERS) -ge 18 -a $(MAX_KERNEL_POLICY) -ge 30 ] && [ -f
 TARGETS += test_nlmsg.te
 endif
 
+# memfd_file test dependencies: memfd_file class, memfd_class capability
+ifeq ($(shell [ -d /sys/fs/selinux/class/memfd_file ] && grep -q 1 $(SELINUXFS)/policy_capabilities/memfd_class && echo true),true)
+TARGETS += test_memfd.te
+endif
+
 ifeq (x$(DISTRO),$(filter x$(DISTRO),xRHEL4 xRHEL5 xRHEL6))
 TARGETS:=$(filter-out test_overlayfs.te test_mqueue.te test_ibpkey.te, $(TARGETS))
 endif
policy/test_memfd.te new file +25 -0
new file mode 100644
index 0000000..5ca1348
@@ -0,0 +1,25 @@
+########################################
+#
+# Policy for testing memfd_file.
+
+attribute memfdtestdomain;
+
+type test_memfd_t;
+typeattribute test_memfd_t memfdtestdomain;
+testsuite_domain_type(test_memfd_t);
+allow test_memfd_t self:memfd_file { create mmap_rw_file_perms };
+
+type test_memfd_nocreate_t;
+typeattribute test_memfd_nocreate_t memfdtestdomain;
+testsuite_domain_type(test_memfd_nocreate_t);
+allow test_memfd_nocreate_t self:memfd_file mmap_rw_file_perms;
+
+type test_memfd_with_exec_t;
+typeattribute test_memfd_with_exec_t memfdtestdomain;
+testsuite_domain_type(test_memfd_with_exec_t);
+allow test_memfd_with_exec_t self:memfd_file { create mmap_rw_file_perms execute execute_no_trans };
+
+type test_memfd_with_noexec_t;
+typeattribute test_memfd_with_noexec_t memfdtestdomain;
+testsuite_domain_type(test_memfd_with_noexec_t);
+allow test_memfd_with_noexec_t self:memfd_file { create mmap_rw_file_perms execute };
tests/Makefile +5 -0
index 7a6aace..903ab11 100644
@@ -158,6 +158,11 @@ ifeq ($(shell [ $(MOD_POL_VERS) -ge 18 -a $(MAX_KERNEL_POLICY) -ge 30 ] && [ -f
 SUBDIRS += nlmsg
 endif
 
+# memfd_file test dependencies: memfd_file class, memfd_class capability
+ifeq ($(shell [ -d /sys/fs/selinux/class/memfd_file ] && grep -q 1 $(SELINUXFS)/policy_capabilities/memfd_class && echo true),true)
+SUBDIRS += memfd
+endif
+
 ifeq ($(DISTRO),RHEL4)
     SUBDIRS:=$(filter-out bounds dyntrace dyntrans inet_socket mmap nnp_nosuid overlay unix_socket, $(SUBDIRS))
 endif
tests/memfd/Makefile new file +5 -0
new file mode 100644
index 0000000..471900f
@@ -0,0 +1,5 @@
+TARGETS=memfd nothing memfd_fexecve
+
+all: $(TARGETS)
+clean:
+       rm -f $(TARGETS)
tests/memfd/memfd.c new file +24 -0
new file mode 100644
index 0000000..0d6f838
@@ -0,0 +1,24 @@
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+int main(int argc, char *argv[])
+{
+       int fd;
+       char *name = "mymemfd";
+
+       fd = memfd_create(name, 0);
+       if (fd < 0) {
+               perror("memfd_create");
+               exit(-1);
+       }
+
+       close(fd);
+       exit(0);
+}
tests/memfd/memfd_fexecve.c new file +53 -0
new file mode 100644
index 0000000..977081e
@@ -0,0 +1,53 @@
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+int main(int argc, char *argv[])
+{
+       int memfd_fd, exec_fd;
+       int len, written, rc;
+       char *name = "mymemfd";
+
+       if (argc != 2) {
+               printf("Usage: %s <fexec_binary>\n", argv[0]);
+               exit(-1);
+       }
+
+       memfd_fd = memfd_create(name, 0);
+       if (memfd_fd < 0) {
+               perror("memfd_create");
+               exit(-1);
+       }
+
+       exec_fd = open(argv[1], O_RDONLY);
+       if (exec_fd < 0) {
+               perror("open");
+               exit(-1);
+       }
+
+       char buf[8192];
+       while ((len = read(exec_fd, buf, sizeof(buf))) > 0) {
+               written = write(memfd_fd, buf, len);
+               if (len != written) {
+                       perror("read/write");
+                       exit(-1);
+               }
+       }
+       close(exec_fd);
+
+       char *empty_env[] = {NULL};
+       char *nothing_argv[] = {argv[1], NULL};
+
+       rc = fexecve(memfd_fd, nothing_argv, empty_env);
+
+       perror("fexecve");
+       exit(rc);
+}
tests/memfd/nothing.c new file +6 -0
new file mode 100644
index 0000000..fc53d37
@@ -0,0 +1,6 @@
+#include <stdlib.h>
+
+int main(int argc, char *argv[])
+{
+       return 0;
+}
tests/memfd/test new file +41 -1
new file mode 100755
index 0000000..1b1e75f
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+#
+# This test exercises the memfd_class support
+#
+
+use Test;
+
+BEGIN {
+    $test_count = 4;
+    plan tests => $test_count;
+}
+
+$basedir = $0;
+$basedir =~ s|(.*)/[^/]*|$1|;
+
+#
+# Attempt to call memfd_create() from the allowed domain.
+#
+$result = system "runcon -t test_memfd_t -- $basedir/memfd 2>&1";
+ok( $result, 0 );
+
+#
+# Attempt to call memfd_create() from the not-allowed domain.
+#
+$result = system "runcon -t test_memfd_nocreate_t -- $basedir/memfd 2>&1";
+ok($result);
+
+#
+# Attempt to fexecve() on a memfd_create() fd from the allowed domain.
+#
+$result = system
+"runcon -t test_memfd_with_exec_t -- $basedir/memfd_fexecve $basedir/nothing 2>&1";
+ok( $result, 0 );
+
+#
+# Attempt to fexecve() on a memfd_create() fd from the not-allowed domain.
+#
+$result = system
+"runcon -t test_memfd_with_noexec_t -- $basedir/memfd_fexecve $basedir/nothing 2>&1";
+ok($result);
+exit;
--

2.51.0.384.g4c02a37b29-goog

maintainer Applied
#2 lore raw
Quoted 42 lines · Stephen Smalley
Quoted 11 lines · Thiébaud Weksteen

A new class "memfd_file" is introduced to the kernel in [1]. This class is only used when the policy capability "memfd_class" is set. Add tests to validate this new class:

1. Validate that memfd_create() requires the "create" permission.
2. Validate that fexecve() on a memfd requires the "execute_no_trans" permission.

This can be tested by modifying the policy with:

Quoted 7 lines
  semodule -c -E base
  sed -i \
    -e 's/\((class user_namespace (create ))\)/\1\n(class memfd_file (execute_no_trans entrypoint ))\n(classcommon memfd_file file)/' \
    -e 's/\(anon_inode socket\)/memfd_file \1/' \
    base.cil
  echo "(policycap memfd_class)" >>base.cil
  semodule -X 456 -i base.cil

No need to re-spin just for this, but for future reference, I'd just do the echo to a separate file (e.g. memfdclass.cil) and append -i memfdclass.cil to the semodule command line,
and I wouldn't bother specifying a priority (just let it use the default 400). The reason is that I usually do these via sudo and an echo with file redirect won't work as is when
appending to the root-owned base.cil file created by the earlier semodule -c -E base.

Quoted 6 lines

rm -f base.cil

  sed -i.orig \
    -e 's/\(define(`all_file_perms'\'',\)\(.*\)$/\1\2\ndefine(`all_memfd_file_perms'\'',\2/' \
    -e 's/\(class file all_file_perms;\)/\1\nclass memfd_file all_memfd_file_perms;/' \
    /usr/share/selinux/devel/include/support/all_perms.spt

Likewise, no need to re-spin just for this, but I'd then say to run "semodule -r base memfdclass" and
restore the all_perms.spt.org file to all_perms.spt.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

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

Thanks, applied.

Quoted 248 lines
Quoted 248 lines

---
Changelog since v1:
- Add commands to test the policy to the commit message.
- Only check for class and capability to include the new tests.
- Ran check-syntax.

 policy/Makefile             |  5 ++++
 policy/test_memfd.te        | 25 +++++++++++++++++
 tests/Makefile              |  5 ++++
 tests/memfd/Makefile        |  5 ++++
 tests/memfd/memfd.c         | 24 +++++++++++++++++
 tests/memfd/memfd_fexecve.c | 53 +++++++++++++++++++++++++++++++++++++
 tests/memfd/nothing.c       |  6 +++++
 tests/memfd/test            | 41 ++++++++++++++++++++++++++++
 8 files changed, 164 insertions(+)
 create mode 100644 policy/test_memfd.te
 create mode 100644 tests/memfd/Makefile
 create mode 100644 tests/memfd/memfd.c
 create mode 100644 tests/memfd/memfd_fexecve.c
 create mode 100644 tests/memfd/nothing.c
 create mode 100755 tests/memfd/test
policy/Makefile +5 -0
index ffd774d..520f891 100644
@@ -163,6 +163,11 @@ ifeq ($(shell [ $(MOD_POL_VERS) -ge 18 -a $(MAX_KERNEL_POLICY) -ge 30 ] && [ -f
 TARGETS += test_nlmsg.te
 endif
 
+# memfd_file test dependencies: memfd_file class, memfd_class capability
+ifeq ($(shell [ -d /sys/fs/selinux/class/memfd_file ] && grep -q 1 $(SELINUXFS)/policy_capabilities/memfd_class && echo true),true)
+TARGETS += test_memfd.te
+endif
+
 ifeq (x$(DISTRO),$(filter x$(DISTRO),xRHEL4 xRHEL5 xRHEL6))
 TARGETS:=$(filter-out test_overlayfs.te test_mqueue.te test_ibpkey.te, $(TARGETS))
 endif
policy/test_memfd.te new file +25 -0
new file mode 100644
index 0000000..5ca1348
@@ -0,0 +1,25 @@
+########################################
+#
+# Policy for testing memfd_file.
+
+attribute memfdtestdomain;
+
+type test_memfd_t;
+typeattribute test_memfd_t memfdtestdomain;
+testsuite_domain_type(test_memfd_t);
+allow test_memfd_t self:memfd_file { create mmap_rw_file_perms };
+
+type test_memfd_nocreate_t;
+typeattribute test_memfd_nocreate_t memfdtestdomain;
+testsuite_domain_type(test_memfd_nocreate_t);
+allow test_memfd_nocreate_t self:memfd_file mmap_rw_file_perms;
+
+type test_memfd_with_exec_t;
+typeattribute test_memfd_with_exec_t memfdtestdomain;
+testsuite_domain_type(test_memfd_with_exec_t);
+allow test_memfd_with_exec_t self:memfd_file { create mmap_rw_file_perms execute execute_no_trans };
+
+type test_memfd_with_noexec_t;
+typeattribute test_memfd_with_noexec_t memfdtestdomain;
+testsuite_domain_type(test_memfd_with_noexec_t);
+allow test_memfd_with_noexec_t self:memfd_file { create mmap_rw_file_perms execute };
tests/Makefile +5 -0
index 7a6aace..903ab11 100644
@@ -158,6 +158,11 @@ ifeq ($(shell [ $(MOD_POL_VERS) -ge 18 -a $(MAX_KERNEL_POLICY) -ge 30 ] && [ -f
 SUBDIRS += nlmsg
 endif
 
+# memfd_file test dependencies: memfd_file class, memfd_class capability
+ifeq ($(shell [ -d /sys/fs/selinux/class/memfd_file ] && grep -q 1 $(SELINUXFS)/policy_capabilities/memfd_class && echo true),true)
+SUBDIRS += memfd
+endif
+
 ifeq ($(DISTRO),RHEL4)
     SUBDIRS:=$(filter-out bounds dyntrace dyntrans inet_socket mmap nnp_nosuid overlay unix_socket, $(SUBDIRS))
 endif
tests/memfd/Makefile new file +5 -0
new file mode 100644
index 0000000..471900f
@@ -0,0 +1,5 @@
+TARGETS=memfd nothing memfd_fexecve
+
+all: $(TARGETS)
+clean:
+       rm -f $(TARGETS)
tests/memfd/memfd.c new file +24 -0
new file mode 100644
index 0000000..0d6f838
@@ -0,0 +1,24 @@
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+int main(int argc, char *argv[])
+{
+       int fd;
+       char *name = "mymemfd";
+
+       fd = memfd_create(name, 0);
+       if (fd < 0) {
+               perror("memfd_create");
+               exit(-1);
+       }
+
+       close(fd);
+       exit(0);
+}
tests/memfd/memfd_fexecve.c new file +53 -0
new file mode 100644
index 0000000..977081e
@@ -0,0 +1,53 @@
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+int main(int argc, char *argv[])
+{
+       int memfd_fd, exec_fd;
+       int len, written, rc;
+       char *name = "mymemfd";
+
+       if (argc != 2) {
+               printf("Usage: %s <fexec_binary>\n", argv[0]);
+               exit(-1);
+       }
+
+       memfd_fd = memfd_create(name, 0);
+       if (memfd_fd < 0) {
+               perror("memfd_create");
+               exit(-1);
+       }
+
+       exec_fd = open(argv[1], O_RDONLY);
+       if (exec_fd < 0) {
+               perror("open");
+               exit(-1);
+       }
+
+       char buf[8192];
+       while ((len = read(exec_fd, buf, sizeof(buf))) > 0) {
+               written = write(memfd_fd, buf, len);
+               if (len != written) {
+                       perror("read/write");
+                       exit(-1);
+               }
+       }
+       close(exec_fd);
+
+       char *empty_env[] = {NULL};
+       char *nothing_argv[] = {argv[1], NULL};
+
+       rc = fexecve(memfd_fd, nothing_argv, empty_env);
+
+       perror("fexecve");
+       exit(rc);
+}
tests/memfd/nothing.c new file +6 -0
new file mode 100644
index 0000000..fc53d37
@@ -0,0 +1,6 @@
+#include <stdlib.h>
+
+int main(int argc, char *argv[])
+{
+       return 0;
+}
tests/memfd/test new file +41 -1
new file mode 100755
index 0000000..1b1e75f
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+#
+# This test exercises the memfd_class support
+#
+
+use Test;
+
+BEGIN {
+    $test_count = 4;
+    plan tests => $test_count;
+}
+
+$basedir = $0;
+$basedir =~ s|(.*)/[^/]*|$1|;
+
+#
+# Attempt to call memfd_create() from the allowed domain.
+#
+$result = system "runcon -t test_memfd_t -- $basedir/memfd 2>&1";
+ok( $result, 0 );
+
+#
+# Attempt to call memfd_create() from the not-allowed domain.
+#
+$result = system "runcon -t test_memfd_nocreate_t -- $basedir/memfd 2>&1";
+ok($result);
+
+#
+# Attempt to fexecve() on a memfd_create() fd from the allowed domain.
+#
+$result = system
+"runcon -t test_memfd_with_exec_t -- $basedir/memfd_fexecve $basedir/nothing 2>&1";
+ok( $result, 0 );
+
+#
+# Attempt to fexecve() on a memfd_create() fd from the not-allowed domain.
+#
+$result = system
+"runcon -t test_memfd_with_noexec_t -- $basedir/memfd_fexecve $basedir/nothing 2>&1";
+ok($result);
+exit;
--

2.51.0.384.g4c02a37b29-goog

maintainer
#3 lore raw
Quoted 45 lines · Stephen Smalley
Quoted 42 lines · Stephen Smalley
Quoted 11 lines · Thiébaud Weksteen

A new class "memfd_file" is introduced to the kernel in [1]. This class is only used when the policy capability "memfd_class" is set. Add tests to validate this new class:

1. Validate that memfd_create() requires the "create" permission.
2. Validate that fexecve() on a memfd requires the "execute_no_trans" permission.

This can be tested by modifying the policy with:

Quoted 7 lines
  semodule -c -E base
  sed -i \
    -e 's/\((class user_namespace (create ))\)/\1\n(class memfd_file (execute_no_trans entrypoint ))\n(classcommon memfd_file file)/' \
    -e 's/\(anon_inode socket\)/memfd_file \1/' \
    base.cil
  echo "(policycap memfd_class)" >>base.cil
  semodule -X 456 -i base.cil

No need to re-spin just for this, but for future reference, I'd just do the echo to a separate file (e.g. memfdclass.cil) and append -i memfdclass.cil to the semodule command line,
and I wouldn't bother specifying a priority (just let it use the default 400). The reason is that I usually do these via sudo and an echo with file redirect won't work as is when
appending to the root-owned base.cil file created by the earlier semodule -c -E base.

Quoted 6 lines

rm -f base.cil

  sed -i.orig \
    -e 's/\(define(`all_file_perms'\'',\)\(.*\)$/\1\2\ndefine(`all_memfd_file_perms'\'',\2/' \
    -e 's/\(class file all_file_perms;\)/\1\nclass memfd_file all_memfd_file_perms;/' \
    /usr/share/selinux/devel/include/support/all_perms.spt

Likewise, no need to re-spin just for this, but I'd then say to run "semodule -r base memfdclass" and
restore the all_perms.spt.org file to all_perms.spt.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

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

Thanks, applied.

Going to leave this one applied at least for now unless there are objections because it doesn't break anything and won't be affected by any change to the policy capability number. Can always revert if we later decide the kernel patch won't ever be accepted for some reason.