policy,tests: add tests for memfd_file class
PATCH 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.
[1] https://lore.kernel.org/selinux/[email protected]/
--- 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 | 39 +++++++++++++++++++++++++++ 8 files changed, 162 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
@@ -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: policy >= 30, memfd_file class, memfd_class capability +ifeq ($(shell [ $(MOD_POL_VERS) -ge 18 -a $(MAX_KERNEL_POLICY) -ge 30 ] && [ -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
@@ -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
@@ -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: policy >= 30, memfd_file class, memfd_class capability +ifeq ($(shell [ $(MOD_POL_VERS) -ge 18 -a $(MAX_KERNEL_POLICY) -ge 30 ] && [ -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
@@ -0,0 +1,5 @@ +TARGETS=memfd nothing memfd_fexecve + +all: $(TARGETS) +clean: + rm -f $(TARGETS)
tests/memfd/memfd.c new file +24 -0
@@ -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
@@ -0,0 +1,6 @@ +#include <stdlib.h> + +int main (int argc,char *argv[]) +{ +return 0 ; +}
tests/memfd/test new file +39 -0
@@ -0,0 +1,39 @@ +#!/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;
Signature · 1 line
2.51.0.338.gd7d06c2dae-goog
Stephen Smalley
maintainerQuoted 8 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 thatfexecve()on a memfd requires the "execute_no_trans" permission.
Thanks for this patch. Do you have example CIL modules for defining the memfd_file class and memfd_class policy capability that we can use to exercise these tests without needing to modify the base policy? And corresponding sed command for updating /usr/share/selinux/devel/include/support/obj_perm_sets.spt to define it for use by the test_memfd.te file?
Is there an actual policy version dependency or is that just cut-and-pasted from prior examples?
And then lastly, please run ./toosl/check-syntax -f to fix up formatting issues with the C and perl code before submission. Thank you!
Quoted 244 lines
[1] https://lore.kernel.org/selinux/[email protected]/
Signed-off-by Thiébaud Weksteen <[email protected]>--- 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 | 39 +++++++++++++++++++++++++++ 8 files changed, 162 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/testpolicy/Makefile +5
-0@@ -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: policy >= 30, memfd_file class, memfd_class capability +ifeq ($(shell [ $(MOD_POL_VERS) -ge 18 -a $(MAX_KERNEL_POLICY) -ge 30 ] && [ -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)) endifpolicy/test_memfd.te new file +25
-0@@ -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@@ -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: policy >= 30, memfd_file class, memfd_class capability +ifeq ($(shell [ $(MOD_POL_VERS) -ge 18 -a $(MAX_KERNEL_POLICY) -ge 30 ] && [ -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)) endiftests/memfd/Makefile new file +5
-0@@ -0,0 +1,5 @@ +TARGETS=memfd nothing memfd_fexecve + +all: $(TARGETS) +clean: + rm -f $(TARGETS)tests/memfd/memfd.c new file +24
-0@@ -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@@ -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@@ -0,0 +1,6 @@ +#include <stdlib.h> + +int main (int argc,char *argv[]) +{ +return 0 ; +}tests/memfd/test new file +39
-1@@ -0,0 +1,39 @@ +#!/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.338.gd7d06c2dae-goog
Quoted 11 lines · Stephen Smalley
Quoted 8 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 thatfexecve()on a memfd requires the "execute_no_trans" permission.
Quoted 6 lines
Thanks for this patch. Do you have example CIL modules for defining the
memfd_fileclass andmemfd_classpolicy capability that we can use to exercise these tests without needing to modify the base policy? And corresponding sed command for updating/usr/share/selinux/devel/include/support/obj_perm_sets.sptto define it for use by the test_memfd.te file?
Thanks for the review. Here are the changes I added to the tmt configuration for testing:
# Fix the current policy.
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_fileall_memfd_file_perms;/' \
/usr/share/selinux/devel/include/support/all_perms.spt
I've pushed the complete tmt configuration I used to:
https://github.com/tweksteen/selinux-testsuite/commit/6920c02f6d78a9ddd190c62479df3b7569c694cf
I am not sure which part is suitable for upstreaming, probably the policy update similar to nlmsg?
Quoted 2 lines
Is there an actual policy version dependency or is that just cut-and-pasted from prior examples?
cut-and-paste, I'll remove it. I assume the check on capability is sufficient here?
Quoted 2 lines
And then lastly, please run ./toosl/check-syntax -f to fix up formatting issues with the C and perl code before submission.
Ack. Thanks, I will run it for the next version of this patch.
Stephen Smalley
maintainerQuoted 42 lines · Thiébaud Weksteen
Quoted 11 lines · Stephen Smalley
Quoted 8 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 thatfexecve()on a memfd requires the "execute_no_trans" permission.Quoted 6 lines
Thanks for this patch. Do you have example CIL modules for defining the
memfd_fileclass andmemfd_classpolicy capability that we can use to exercise these tests without needing to modify the base policy? And corresponding sed command for updating/usr/share/selinux/devel/include/support/obj_perm_sets.sptto define it for use by the test_memfd.te file?Thanks for the review. Here are the changes I added to the tmt configuration for testing:
# Fix the current policy.
semodule -c -E base
sed -i \
-e 's/\((classuser_namespace(create ))\)/\1\n(classmemfd_file
(execute_no_transentrypoint ))\n(classcommonmemfd_filefile)/' \
-e 's/\(anon_inode socket\)/memfd_file \1/' \
base.cil
echo "(policycapmemfd_class)" >>base.cil
semodule -X 456 -i base.cil
rm -f base.cilsed -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_fileall_memfd_file_perms;/' \
/usr/share/selinux/devel/include/support/all_perms.spt
Quoted 6 lines
I've pushed the complete tmt configuration I used to:
https://github.com/tweksteen/selinux-testsuite/commit/6920c02f6d78a9ddd190c62479df3b7569c694cf
I am not sure which part is suitable for upstreaming, probably the policy update similar to nlmsg?
Thanks, with those commands (which i would encourage you to add to the commit/patch description if you re-spin the patch), I was able to run your tests successfully in enforcing mode and also confirm that in permissive mode, 2 of the 4 test fail as expected. I don't actually know about what is expected for the tmt configuration - will defer to Ondrej on that since he created and maintains it.
Quoted 5 lines
Quoted 2 lines
Is there an actual policy version dependency or is that just cut-and-pasted from prior examples?
cut-and-paste, I'll remove it. I assume the check on capability is sufficient here?
Checking for both the class being defined and the policy capability being enabled is probably a good idea since nothing ties those together currently (that's a longstanding open issue on the selinux userspace, https://github.com/SELinuxProject/selinux/issues/55).
Quoted 4 lines
Quoted 2 lines
And then lastly, please run ./toosl/check-syntax -f to fix up formatting issues with the C and perl code before submission.
Ack. Thanks, I will run it for the next version of this patch.
Thanks, happy to add an Acked-by once that is done.