Skip to contentSkip to conversation

Add tests for selabel_file backend

PATCH Superseded Thiébaud Weksteen 2 participants 2 messages Mar 2 – Mar 9 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add tests for selabel_file backend

Add basic set of tests to validate the behaviour of the selabel_file backend. The parsing and usage of file_contexts is confirmed, in the following scenarios:

- selabel_open with no options,
- selabel_open with a NULL SELABEL_OPT_PATH,
- selabel_open with a valid SELABEL_OPT_PATH,
- selabel_open with a valid SELABEL_OPT_PATH and SELABEL_OPT_BASEONLY,
- selabel_open with SELABEL_OPT_VALIDATE and known types,
- selabel_open with SELABEL_OPT_VALIDATE and unknown types.

Validate that substitutions and local customizations are loaded according to selabel_file(5).
---
Some more context on this change: we are currently trying to upstream some of our internal changes to libselinux. A major difference is our support for multiple SELABEL_OPT_PATH options which was discussed in [1]. The intent is to follow up with an updated patch and tests which will build on this commit.

It is not clear to me if this is the best location for these tests. Some could be implemented directly in libselinux, but the ability to load a test policy was required to confirm the behaviour of SELABEL_OPT_VALIDATE.

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

 policy/Makefile                      |  3 +-
 policy/test_file_contexts.te         |  6 ++
 tests/Makefile                       |  2 +-
 tests/file_contexts/Makefile         | 10 +++
 tests/file_contexts/f1.fc            |  1 +
 tests/file_contexts/f2.fc            |  4 ++
 tests/file_contexts/f3.fc            |  2 +
 tests/file_contexts/f3.fc.homedirs   |  1 +
 tests/file_contexts/f3.fc.local      |  1 +
 tests/file_contexts/f3.fc.subs       |  1 +
 tests/file_contexts/f3.fc.subs_dist  |  1 +
 tests/file_contexts/internal.c       | 41 +++++++++++++
 tests/file_contexts/internal.h       | 16 +++++
 tests/file_contexts/test             | 25 ++++++++
 tests/file_contexts/test_lookup.c    | 46 ++++++++++++++
 tests/file_contexts/test_open.c      | 82 +++++++++++++++++++++++++
 tests/file_contexts/test_open_base.c | 92 ++++++++++++++++++++++++++++
 tests/file_contexts/test_validate.c  | 56 +++++++++++++++++
 18 files changed, 388 insertions(+), 2 deletions(-)
 create mode 100644 policy/test_file_contexts.te
 create mode 100644 tests/file_contexts/Makefile
 create mode 100644 tests/file_contexts/f1.fc
 create mode 100644 tests/file_contexts/f2.fc
 create mode 100644 tests/file_contexts/f3.fc
 create mode 100644 tests/file_contexts/f3.fc.homedirs
 create mode 100644 tests/file_contexts/f3.fc.local
 create mode 100644 tests/file_contexts/f3.fc.subs
 create mode 100644 tests/file_contexts/f3.fc.subs_dist
 create mode 100644 tests/file_contexts/internal.c
 create mode 100644 tests/file_contexts/internal.h
 create mode 100755 tests/file_contexts/test
 create mode 100644 tests/file_contexts/test_lookup.c
 create mode 100644 tests/file_contexts/test_open.c
 create mode 100644 tests/file_contexts/test_open_base.c
 create mode 100644 tests/file_contexts/test_validate.c
policy/Makefile +2 -1
index 40bf393..bf1ea76 100644
@@ -29,7 +29,8 @@ TARGETS = \
 	test_task_getsid.te test_task_setpgid.te test_task_setsched.te \
 	test_transition.te test_unix_socket.te \
 	test_mmap.te test_overlayfs.te test_mqueue.te \
-	test_ibpkey.te test_atsecure.te test_cgroupfs.te
+	test_ibpkey.te test_atsecure.te test_cgroupfs.te \
+	test_file_contexts.te
 
 ifeq (x$(DISTRO),$(filter x$(DISTRO),xRHEL4 xRHEL5 xRHEL6))
 SUPPORTS_CIL = n
policy/test_file_contexts.te new file +6 -0
new file mode 100644
index 0000000..c61f3b1
@@ -0,0 +1,6 @@
+####### Policy for testing file_contexts #########
+
+type test_base_t;
+type test_base_wildcard_t;
+type test_base_sub_t;
+type test_file_list_t;
tests/Makefile +1 -1
index 6df220c..07403be 100644
@@ -28,7 +28,7 @@ SUBDIRS:= domain_trans entrypoint execshare exectrace execute_no_trans \
 	task_getpgid task_setpgid file ioctl capable_file capable_net \
 	capable_sys dyntrans dyntrace bounds nnp_nosuid mmap unix_socket \
 	inet_socket/tcp inet_socket/udp overlay checkreqprot mqueue \
-	mac_admin atsecure infiniband_endport infiniband_pkey
+	mac_admin atsecure infiniband_endport infiniband_pkey file_contexts
 
 ifeq ($(shell grep -q cap_userns $(POLDEV)/include/support/all_perms.spt && echo true),true)
 ifneq ($(shell ./kvercmp $$(uname -r) 4.7),-1)
tests/file_contexts/Makefile new file +10 -0
new file mode 100644
index 0000000..592a65f
@@ -0,0 +1,10 @@
+TARGETS=test_open test_lookup test_open_base test_validate
+CFLAGS += -O2 -Werror -Wall
+LDLIBS += -lselinux
+
+all: $(TARGETS)
+
+$(TARGETS): internal.o
+
+clean:
+	rm -f $(TARGETS) *.o
tests/file_contexts/f1.fc new file +1 -0
new file mode 100644
index 0000000..6672a49
@@ -0,0 +1 @@
+/       system_u:object_r:rootfs:s0
tests/file_contexts/f2.fc new file +4 -0
new file mode 100644
index 0000000..0d8c087
@@ -0,0 +1,4 @@
+/base             system_u:object_r:test_base_t:s0
+/base(/.*)?       system_u:object_r:test_base_wildcard_t:s0
+/base/sub         system_u:object_r:test_base_sub_t:s0
+/base/file\.list  system_u:object_r:test_file_list_t:s0
tests/file_contexts/f3.fc new file +2 -0
new file mode 100644
index 0000000..a7361cd
@@ -0,0 +1,2 @@
+/        system_u:object_r:rootfs:s0
+/subbed  system_u:object_r:test_subbed:s0
tests/file_contexts/f3.fc.homedirs new file +1 -0
new file mode 100644
index 0000000..d1878ce
@@ -0,0 +1 @@
+/homedirs  system_u:object_r:test_homedirs:s0
tests/file_contexts/f3.fc.local new file +1 -0
new file mode 100644
index 0000000..96e432b
@@ -0,0 +1 @@
+/local    system_u:object_r:test_local:s0
tests/file_contexts/f3.fc.subs new file +1 -0
new file mode 100644
index 0000000..922ed96
@@ -0,0 +1 @@
+/sub /subbed
tests/file_contexts/f3.fc.subs_dist new file +1 -0
new file mode 100644
index 0000000..abe31fe
@@ -0,0 +1 @@
+/sub_dist /subbed
tests/file_contexts/internal.c new file +41 -0

41 lines not on this page · raw message

tests/file_contexts/internal.h new file +16 -0
new file mode 100644
index 0000000..a07548f
@@ -0,0 +1,16 @@
+#include <selinux/label.h>
+#include <selinux/selinux.h>
+
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
+/* Structure to capture a test. The |path| will be resolved and expected to
+ * match the |context|. If |context| is NULL, the lookup is expected to fail. */
+struct test_t {
+	const char *path;
+	const char *context;
+};
+
+/* Assert that all paths described in |tests| resolve appropriately. |hnd| must
+ * be opened. |n| is the number of tests in |tests|. */
+void assertContextsMatch(struct selabel_handle *hnd, struct test_t *tests,
+			 size_t n);
tests/file_contexts/test new file +25 -0
new file mode 100755
index 0000000..0f698e9
@@ -0,0 +1,25 @@
+#!/usr/bin/perl
+#
+# This test validates the parsing of file_contexts.
+#
+
+use Test;
+
+BEGIN { plan tests => 4; }
+
+$basedir = $0;
+$basedir =~ s|(.*)/[^/]*|$1|;
+
+$result = system "$basedir/test_open $basedir";
+ok( $result, 0 );
+
+$result = system "$basedir/test_lookup $basedir";
+ok( $result, 0 );
+
+$result = system "$basedir/test_open_base $basedir";
+ok( $result, 0 );
+
+$result = system "$basedir/test_validate $basedir";
+ok( $result, 0 );
+
+exit;
tests/file_contexts/test_lookup.c new file +46 -0

46 lines not on this page · raw message

tests/file_contexts/test_open.c new file +82 -0

82 lines not on this page · raw message

tests/file_contexts/test_open_base.c new file +92 -0

92 lines not on this page · raw message

tests/file_contexts/test_validate.c new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.53.0.473.g4a7958ca14-goog
maintainer
#1 lore raw
Quoted 21 lines · Thiébaud Weksteen

Add basic set of tests to validate the behaviour of the selabel_file backend. The parsing and usage of file_contexts is confirmed, in the following scenarios:

- selabel_open with no options,
- selabel_open with a NULL SELABEL_OPT_PATH,
- selabel_open with a valid SELABEL_OPT_PATH,
- selabel_open with a valid SELABEL_OPT_PATH and SELABEL_OPT_BASEONLY,
- selabel_open with SELABEL_OPT_VALIDATE and known types,
- selabel_open with SELABEL_OPT_VALIDATE and unknown types.

Validate that substitutions and local customizations are loaded according to selabel_file(5).
---
Some more context on this change: we are currently trying to upstream some of our internal changes to libselinux. A major difference is our support for multiple SELABEL_OPT_PATH options which was discussed in [1]. The intent is to follow up with an updated patch and tests which will build on this commit.

Quoted 4 lines

It is not clear to me if this is the best location for these tests. Some could be implemented directly in libselinux, but the ability to load a test policy was required to confirm the behaviour of SELABEL_OPT_VALIDATE.

Currently this produces output during a make test:
file_contexts/test .......... 1/4 file_contexts/f1.fc: line 1 has invalid context system_u:object_r:rootfs:s0 file_contexts/f1.fc: line 1 has invalid context system_u:object_r:rootfs:s0 file_contexts/test .......... ok

We generally suppress such expected output during make test runs and only show extra output on test failures.