Skip to contentSkip to conversation

libselinux: rework selabel_file(5) database

RFC PATCH In review Christian Göttsche 2 participants 15 messages Jan 31, 2024 – Mar 11, 2024 SELinux security module

lore
Share & tools
lore
#0 lore raw

v2 cover libselinux: rework selabel_file(5) database

Currently the database for file backend of selabel stores the file context specifications in a single long array. This array is sorted by special precedence rules, e.g. regular expressions without meta character first, ordered by length, and the remaining regular expressions ordered by stem (the prefix part of the regular expressions without meta characters) length.

This results in suboptimal lookup performance for two reasons; File context specifications without any meta characters (e.g. '/etc/passwd') are still matched via an expensive regular expression match operation.
All such trivial regular expressions are matched against before any non- trivial regular expression, resulting in thousands of regex match operations for lookups for paths not matching any of the trivial ones.

Rework the internal representation of the database in two ways:
Convert regular expressions without any meta characters and containing only supported escaped characters (e.g. '/etc/rc\.d/init\.d') into literal strings, which get compared via strcmp(3) later on. Store the specifications in a tree structure to reduce the to number of specifications that need to be checked.

Since the internal representation is completely rewritten introduce a new compiled file context file format mirroring the tree structure. The new format also stores all multi-byte data in network byte-order, so that such compiled files can be cross-compiled, e.g. for embedded devices with read-only filesystems (except for the regular expressions, which are still architecture-dependent, but ignored on architecture mis- match).

The improved lookup performance will also benefit SELinux aware daemons, which create files with their default context, e.g. systemd.

Fedora 39 (pre-compiled regular expressions are omitted on Fedora):

    file_contexts.bin:           599034  ->   430211  (bytes)
    file_contexts.homedirs.bin:   21275  ->    13491  (bytes)

Debian Sid (pre-compiled regular expressions are included):

    file_contexts.bin:          7790690  ->  3646256  (bytes)
    file_contexts.homedirs.bin:  835950  ->   708793  (bytes)

(selabel_lookup -b file -k /bin/bash)

Fedora 39 in VM:

    text:      time:       3.1 ms  ->   3.6 ms
               peak heap:   2.33M  ->    1.81M
               peak rss:    6.64M  ->    6.37M
    compiled:  time:       1.8 ms  ->   1.7 ms
               peak heap:   2.14M  ->    1.23M
               peak rss:    6.76M  ->    5.91M

Debian Sid on Raspberry Pi 3:

    text:      time:      33.4 ms  ->  21.2 ms
               peak heap:  10.59M  ->  607.32K
               peak rss:    6.55M  ->    4.46M
    compiled:  time:      38.3 ms  ->  23.5 ms
               peak heap:  13.28M  ->    2.00M
               peak rss:   12.21M  ->    7.60M

(restorecon -vRn /)

Fedora 39 in VM:

      28.3 s  ->   3.6 s
Debian Sid on Raspberry Pi 3:
      94.6 s  ->  12.1 s

(restorecon -vRn -T0 /)

Fedora 39 in VM (8 cores):

      31.1 s  ->   2.5 s
Debian Sid on Raspberry Pi 3 (4 cores):
      58.9 s  ->  12.6 s

(note: I am unsure why the parallel runs on Fedora are slower)

There might be subtle differences in lookup results which evaded my testing, because some precedence rules are oblique. For example /usr/(.*/)?lib(/.*)? has to have a higher precedence than /usr/(.*/)?bin(/.*)? to match the current Fedora behavior. Please report any behavior changes.

The maximum node depth in the database is set to 3, which seems to give the best performance to memory usage ratio. Might be tweaked for systems with different filesystem hierarchies (Android?).

I am not that familiar with the selabel_partial_match(3), selabel_get_digests_all_partial_matches(3) and selabel_hash_all_partial_matches(3) related interfaces, so I only did some rudimentary tests for them.

# Patches

Patches 1 and 2 introduce two helpers useful for developers and users.

Patches 3.5 tweak the sidtab code to be used in a later patch.

Patch 6 is the main rework. Due to its complete rewrite it is too large for the mailing list, so I added some developers in CC for this one and the patch is available on GitHub (see below). I'd like to refrain splitting it since there are no trivial splitable parts and future reverts or bisections will be more complicated.

Patch 7 is removing unused code after the rework in patch 6.

Patch 8 introduces new fuzzers for selabel_file(5).

Patch 9 improves thread-safety for concurrent selabel lookup.

This patchset is also available at https://github.com/SELinuxProject/selinux/pull/406

v2:
- add two fuzzers performing label lookup, one for textual and one for compiled fcontext definitions
- various fixes, among others encountered via fuzzing
- rename helper to unsefiles
- add sidtab tweaks to store a context array in the binary fcontext format to deduplicate context strings
- add thread-safety patch

Christian Göttsche (9):
policycoreutils: introduce unsetfiles libselinux/utils: introduce selabel_compare
libselinux: use more appropriate types in sidtab
libselinux: add unique id to sidtab entries
libselinux: sidtab updates
libselinux: rework selabel_file(5) database
libselinux: remove unused hashtab code
libselinux: add selabel_file(5) fuzzer
libselinux: support parallel selabel_lookup(3)

 libselinux/fuzz/input                         |    0
 .../fuzz/selabel_file_compiled-fuzzer.c       |  281 +++
 libselinux/fuzz/selabel_file_text-fuzzer.c    |  225 ++
 libselinux/include/selinux/avc.h              |    2 +-
 libselinux/src/avc_sidtab.c                   |   68 +-
 libselinux/src/avc_sidtab.h                   |    4 +-
 libselinux/src/hashtab.c                      |  234 --
 libselinux/src/hashtab.h                      |  117 -
 libselinux/src/label.c                        |   56 +-
 libselinux/src/label_backends_android.c       |    2 +-
 libselinux/src/label_db.c                     |    2 +
 libselinux/src/label_file.c                   | 2216 ++++++++++++-----
 libselinux/src/label_file.h                   |  972 +++++---
 libselinux/src/label_internal.h               |    7 +-
 libselinux/src/label_media.c                  |    1 +
 libselinux/src/label_support.c                |   26 +-
 libselinux/src/label_x.c                      |    1 +
 libselinux/src/regex.c                        |   55 +-
 libselinux/utils/.gitignore                   |    1 +
 libselinux/utils/sefcontext_compile.c         |  658 +++--
 libselinux/utils/selabel_compare.c            |  122 +
 policycoreutils/.gitignore                    |    1 +
 policycoreutils/Makefile                      |    2 +-
 policycoreutils/unsetfiles/Makefile           |   26 +
 policycoreutils/unsetfiles/unsetfiles.1       |   46 +
 policycoreutils/unsetfiles/unsetfiles.c       |  183 ++
 scripts/oss-fuzz.sh                           |   25 +
 27 files changed, 3771 insertions(+), 1562 deletions(-)
 create mode 100644 libselinux/fuzz/input
 create mode 100644 libselinux/fuzz/selabel_file_compiled-fuzzer.c
 create mode 100644 libselinux/fuzz/selabel_file_text-fuzzer.c
 delete mode 100644 libselinux/src/hashtab.c
 delete mode 100644 libselinux/src/hashtab.h
 create mode 100644 libselinux/utils/selabel_compare.c
 create mode 100644 policycoreutils/unsetfiles/Makefile
 create mode 100644 policycoreutils/unsetfiles/unsetfiles.1
 create mode 100644 policycoreutils/unsetfiles/unsetfiles.c
Signature · 1 line
2.43.0
#1 lore raw

RFC PATCH v2 1/9 policycoreutils: introduce unsetfiles

Introduce a helper to remove SELinux file security contexts.

Mainly for testing label operations, and only for SELinux disabled systems, since removing file contexts is not supported by SELinux.

---
v2:
move from libselinux/utils to policycoreutils and rename

---
 policycoreutils/.gitignore              |   1 +
 policycoreutils/Makefile                |   2 +-
 policycoreutils/unsetfiles/Makefile     |  26 ++++
 policycoreutils/unsetfiles/unsetfiles.1 |  46 ++++++
 policycoreutils/unsetfiles/unsetfiles.c | 183 ++++++++++++++++++++++++
 5 files changed, 257 insertions(+), 1 deletion(-)
 create mode 100644 policycoreutils/unsetfiles/Makefile
 create mode 100644 policycoreutils/unsetfiles/unsetfiles.1
 create mode 100644 policycoreutils/unsetfiles/unsetfiles.c
policycoreutils/.gitignore +1 -0
index 47c9cc52..33e7414c 100644
@@ -9,4 +9,5 @@ setfiles/restorecon
 setfiles/restorecon_xattr
 setfiles/setfiles
 setsebool/setsebool
+unsetfiles/unsetfiles
 hll/pp/pp
policycoreutils/Makefile +1 -1
index b930b297..32ad0201 100644
@@ -1,4 +1,4 @@
-SUBDIRS = setfiles load_policy newrole run_init secon sestatus semodule setsebool scripts po man hll
+SUBDIRS = setfiles load_policy newrole run_init secon sestatus semodule setsebool scripts po man hll unsetfiles
 
 all install relabel clean indent:
 	@for subdir in $(SUBDIRS); do \
policycoreutils/unsetfiles/Makefile new file +26 -0
new file mode 100644
index 00000000..9e5edc04
@@ -0,0 +1,26 @@
+PREFIX ?= /usr
+SBINDIR ?= $(PREFIX)/sbin
+MANDIR ?= $(PREFIX)/share/man
+
+override CFLAGS += -D_GNU_SOURCE
+override LDLIBS += -lselinux
+
+
+all: unsetfiles
+
+unsetfiles: unsetfiles.o
+
+install: all
+	test -d $(DESTDIR)$(SBINDIR)     || install -m 755 -d $(DESTDIR)$(SBINDIR)
+	test -d $(DESTDIR)$(MANDIR)/man1 || install -m 755 -d $(DESTDIR)$(MANDIR)/man1
+	install -m 755 unsetfiles $(DESTDIR)$(SBINDIR)
+	install -m 644 unsetfiles.1 $(DESTDIR)$(MANDIR)/man1/
+
+clean:
+	-rm -f unsetfiles *.o
+
+indent:
+	../../scripts/Lindent $(wildcard *.[ch])
+
+relabel: install
+	/sbin/restorecon $(DESTDIR)$(SBINDIR)/unsetfiles
policycoreutils/unsetfiles/unsetfiles.1 new file +46 -0

46 lines not on this page · raw message

policycoreutils/unsetfiles/unsetfiles.c new file +183 -0

183 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

RFC PATCH v2 2/9 libselinux/utils: introduce selabel_compare

In reply to Christian Göttsche

Add a utility around selabel_cmp(3).

Can be used by users to compare a pre-compiled fcontext file to an original text-based file context definition file.

Can be used for development to verify compilation and parsing of the pre-compiled fcontext format works correctly.

---
v2:
split nested block into own function

---
 libselinux/utils/.gitignore        |   1 +
 libselinux/utils/selabel_compare.c | 122 +++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+)
 create mode 100644 libselinux/utils/selabel_compare.c
libselinux/utils/.gitignore +1 -0
index b3311360..2e10b14f 100644
@@ -16,6 +16,7 @@ getseuser
 matchpathcon
 policyvers
 sefcontext_compile
+selabel_compare
 selabel_digest
 selabel_get_digests_all_partial_matches
 selabel_lookup
libselinux/utils/selabel_compare.c new file +122 -0

122 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

Re: [RFC PATCH v2 2/9] libselinux/utils: introduce selabel_compare

Quoted 10 lines · Christian Göttsche

Add a utility around selabel_cmp(3).

Can be used by users to compare a pre-compiled fcontext file to an original text-based file context definition file.

Can be used for development to verify compilation and parsing of the pre-compiled fcontext format works correctly.

I don't have any comments on the code.

It took me a while to get this to work. It was very confusing. Not because of your code, but because of the behavior of selabel_cmp(). It is not intuitive and I think that it needs to be better documented. It will absolutely not work as expected on a copy of the contexts directory.

With all that, not everything worked as I thought it would.

current = installed contexts directory
the other file is by itself (and not in a copy of the contexts directory)

1) current file_contexts.bin vs file_contexts [Expected] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin file_contexts
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is equal to spec file_contexts

2) current file_contexts vs file_contexts [Not expected, diff says they are equal]
selabel_compare /etc/selinux/targeted/contexts/files/file_contexts file_contexts
selabel_cmp: mismatch regex_str left remnant in stem tmp
selabel_cmp: mismatch child node tmp stem (root)
spec /etc/selinux/targeted/contexts/files/file_contexts is uncomparable to spec file_contexts

3) current file_contexts.bin vs file_contexts_1 [Expected] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin file_contexts_1
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is equal to spec file_contexts_1

4) file_contexts vs file_contexts_1 [Expected] selabel_compare file_contexts file_contexts_1 spec file_contexts is equal to spec file_contexts_1

I realize now that selabel_cmp() can only find a superset or a subset if the only difference occurs at the very end. The longer file must be exactly like the shorter file except for the additional lines at the end.

I tried testing with the zebra module disabled, so it should have been a subset of the current policy, but, of course, selabel_cmp() was not sophisticated enough to say that. It just says that everything is uncomparable. It would be nice if something said that.

The following worked only in the most trivial case where I could just use diff instead.
contexts1 = copy of original contexts directory
contexts2 = copy of new contexts directory with new selabel db

1) current file_contexts vs contexts1 file_contexts [Expected, but Confusing] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts contexts1/files/file_contexts
contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.homedirs.bin: Old compiled fcontext format, skipping
spec /etc/selinux/targeted/contexts/files/file_contexts is equal to spec contexts1/files/file_contexts

2) current file_contexts.bin vs contexts1 file_contexts [Not expected. Expected to be able to compare the current file_contexts.bin with old text file_contexts]
selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin contexts1/files/file_contexts contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.homedirs.bin: Old compiled fcontext format, skipping
selabel_cmp: mismatch regex_str right remnant in stem tmp
selabel_cmp: mismatch child node tmp stem (root)
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is uncomparable to spec contexts1/files/file_contexts

3) current file_contexts.bin vs contexts1 file_contexts.bin [Expected, but confusing]
selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin contexts1/files/file_contexts.bin contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping
ERROR: selabel_open - Could not obtain handle for contexts1/files/file_contexts.bin: No such file or directory

4) current file_contexts vs contexts2 file_contexts [Expected] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts contexts2/files/file_contexts
spec /etc/selinux/targeted/contexts/files/file_contexts is equal to spec contexts2/files/file_contexts

5) current file_contexts.bin vs contexts2 file_contexts [Not expected. Expected to be able to compare the current file_contexts.bin with contexts2 text file_contexts]
selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin contexts2/files/file_contexts
selabel_cmp: mismatch regex_str right remnant in stem tmp
selabel_cmp: mismatch child node tmp stem (root)
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is uncomparable to spec contexts2/files/file_contexts

6) current file_contexts.bin vs contexts2 file_contexts.bin [Expected] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin contexts2/files/file_contexts.bin
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is equal to spec contexts2/files/file_contexts.bin

7) contexts1 file_contexts vs contexts2 file_contexts [Expected, but confusing] selabel_compare contexts1/files/file_contexts contexts2/files/file_contexts contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.homedirs.bin: Old compiled fcontext format, skipping
spec contexts1/files/file_contexts is equal to spec contexts2/files/file_contexts

8) contexts1 file_contexts vs contexts2 file_contexts.cpy (which is a copy of file_contexts) [Not expected. Expected them to be equal] selabel_compare contexts1/files/file_contexts contexts2/files/file_contexts.cpy contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.homedirs.bin: Old compiled fcontext format, skipping
selabel_cmp: mismatch regex_str left remnant in stem tmp
selabel_cmp: mismatch child node tmp stem (root)
spec contexts1/files/file_contexts is uncomparable to spec contexts2/files/file_contexts.cpy

With all of the issues, I wonder how useful this actually would be. It might cause more confusion then anything.

Thanks,
Jim

Quoted 153 lines

---
v2:
split nested block into own function

---
 libselinux/utils/.gitignore        |   1 +
 libselinux/utils/selabel_compare.c | 122 +++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+)
 create mode 100644 libselinux/utils/selabel_compare.c
libselinux/utils/.gitignore +1 -0
index b3311360..2e10b14f 100644
@@ -16,6 +16,7 @@ getseuser
 matchpathcon
 policyvers
 sefcontext_compile
+selabel_compare
 selabel_digest
 selabel_get_digests_all_partial_matches
 selabel_lookup
libselinux/utils/selabel_compare.c new file +122 -1
new file mode 100644
index 00000000..9ca6eff1
@@ -0,0 +1,122 @@
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <selinux/label.h>
+
+
+static void usage(const char *progname)
+{
+       fprintf(stderr,
+               "usage: %s [-b backend] [-v] file1 file2\n\n"
+               "Where:\n\t"
+               "-b           The backend - \"file\", \"media\", \"x\", \"db\" or \"prop\" (defaults to \"file\")\n\t"
+               "-v           Validate entries against loaded policy.\n\t"
+               "file1/file2  Files containing the specs.\n",
+               progname);
+}
+
+static int compare(const char *file1, const char *file2, const char *validate, unsigned int backend)
+{
+       struct selabel_handle *hnd1, *hnd2;
+       const struct selinux_opt selabel_option1[] = {
+               { SELABEL_OPT_PATH, file1 },
+               { SELABEL_OPT_VALIDATE, validate }
+       };
+       const struct selinux_opt selabel_option2[] = {
+               { SELABEL_OPT_PATH, file2 },
+               { SELABEL_OPT_VALIDATE, validate }
+       };
+       enum selabel_cmp_result result;
+
+       hnd1 = selabel_open(backend, selabel_option1, 2);
+       if (!hnd1) {
+               fprintf(stderr, "ERROR: selabel_open - Could not obtain handle for %s:  %m\n", file1);
+               return EXIT_FAILURE;
+       }
+
+       hnd2 = selabel_open(backend, selabel_option2, 2);
+       if (!hnd2) {
+               fprintf(stderr, "ERROR: selabel_open - Could not obtain handle for %s:  %m\n", file2);
+               selabel_close(hnd1);
+               return EXIT_FAILURE;
+       }
+
+       result = selabel_cmp(hnd1, hnd2);
+
+       selabel_close(hnd2);
+       selabel_close(hnd1);
+
+       switch (result) {
+       case SELABEL_SUBSET:
+               printf("spec %s is a subset of spec %s\n", file1, file2);
+               break;
+       case SELABEL_EQUAL:
+               printf("spec %s is equal to spec %s\n", file1, file2);
+               break;
+       case SELABEL_SUPERSET:
+               printf("spec %s is a superset of spec %s\n", file1, file2);
+               break;
+       case SELABEL_INCOMPARABLE:
+               printf("spec %s is uncomparable to spec %s\n", file1, file2);
+               break;
+       default:
+               fprintf(stderr, "ERROR: selabel_cmp - Unexpected result %d\n", result);
+               return EXIT_FAILURE;
+       }
+
+       return EXIT_SUCCESS;
+}
+
+int main(int argc, char *argv[])
+{
+       unsigned int backend = SELABEL_CTX_FILE;
+       int opt;
+       const char *validate = NULL, *file1 = NULL, *file2 = NULL;
+
+       if (argc < 3) {
+               usage(argv[0]);
+               return EXIT_FAILURE;
+       }
+
+       while ((opt = getopt(argc, argv, "b:v")) > 0) {
+               switch (opt) {
+               case 'b':
+                       if (!strcasecmp(optarg, "file")) {
+                               backend = SELABEL_CTX_FILE;
+                       } else if (!strcmp(optarg, "media")) {
+                               backend = SELABEL_CTX_MEDIA;
+                       } else if (!strcmp(optarg, "x")) {
+                               backend = SELABEL_CTX_X;
+                       } else if (!strcmp(optarg, "db")) {
+                               backend = SELABEL_CTX_DB;
+                       } else if (!strcmp(optarg, "prop")) {
+                               backend = SELABEL_CTX_ANDROID_PROP;
+                       } else if (!strcmp(optarg, "service")) {
+                               backend = SELABEL_CTX_ANDROID_SERVICE;
+                       } else {
+                               fprintf(stderr, "Unknown backend: %s\n", optarg);
+                               usage(argv[0]);
+                               return EXIT_FAILURE;
+                       }
+                       break;
+               case 'v':
+                       validate = (char *)1;
+                       break;
+               default:
+                       usage(argv[0]);
+                       return EXIT_FAILURE;
+               }
+       }
+
+       if (argc != optind + 2) {
+               usage(argv[0]);
+               return EXIT_FAILURE;
+       }
+
+       file1 = argv[optind++];
+       file2 = argv[optind];
+
+       return compare(file1, file2, validate, backend);
+}
--

2.43.0

#4 lore raw

Re: [RFC PATCH v2 2/9] libselinux/utils: introduce selabel_compare

Quoted 6 lines · James Carter
Quoted 3 lines · Christian Göttsche

Add a utility around selabel_cmp(3).

Quoted 8 lines
Quoted 7 lines

Can be used by users to compare a pre-compiled fcontext file to an original text-based file context definition file.

Can be used for development to verify compilation and parsing of the pre-compiled fcontext format works correctly.

First of all many thanks for testing!

Quoted 20 lines

I don't have any comments on the code.

It took me a while to get this to work. It was very confusing. Not because of your code, but because of the behavior of selabel_cmp(). It is not intuitive and I think that it needs to be better documented. It will absolutely not work as expected on a copy of the contexts directory.

With all that, not everything worked as I thought it would.

current = installed contexts directory
the other file is by itself (and not in a copy of the contexts directory)

1) current file_contexts.bin vs file_contexts [Expected] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin file_contexts
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is equal to spec file_contexts

Quoted 7 lines

2) current file_contexts vs file_contexts [Not expected, diff says they are equal]
selabel_compare /etc/selinux/targeted/contexts/files/file_contexts file_contexts
selabel_cmp: mismatch regex_str left remnant in stem tmp
selabel_cmp: mismatch child node tmp stem (root)
spec /etc/selinux/targeted/contexts/files/file_contexts is uncomparable to spec file_contexts

Since the database is opened via selabel_open(3) filecontexts.(homedirs|local) are implicitly loaded too.
It would be confusing if the selabel_compare helper would deviate from that default behavior.
Also currently there is no information in the in-memory database from which/how-many files the database was loaded.
Maybe that should be added so the selabel_compare helper could give a hint?

Quoted 11 lines

3) current file_contexts.bin vs file_contexts_1 [Expected] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin file_contexts_1
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is equal to spec file_contexts_1

4) file_contexts vs file_contexts_1 [Expected] selabel_compare file_contexts file_contexts_1 spec file_contexts is equal to spec file_contexts_1

Quoted 4 lines

I realize now that selabel_cmp() can only find a superset or a subset if the only difference occurs at the very end. The longer file must be exactly like the shorter file except for the additional lines at the end.

Thanks, this is indeed a shortcoming in v2 (regex prefix length were not considered). Fixed in wip-v3:
https://github.com/SELinuxProject/selinux/compare/f07e4c3b66416f6337d6a2ea1e0d69555f8704ad..090af7e6510edb90f4e7df55346e5023ac190b66

Quoted 11 lines

I tried testing with the zebra module disabled, so it should have been a subset of the current policy, but, of course, selabel_cmp() was not sophisticated enough to say that. It just says that everything is uncomparable. It would be nice if something said that.

The following worked only in the most trivial case where I could just use diff instead.
contexts1 = copy of original contexts directory
contexts2 = copy of new contexts directory with new selabel db

Quoted 8 lines

1) current file_contexts vs contexts1 file_contexts [Expected, but Confusing] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts contexts1/files/file_contexts
contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.homedirs.bin: Old compiled fcontext format, skipping
spec /etc/selinux/targeted/contexts/files/file_contexts is equal to spec contexts1/files/file_contexts

What is confusing here?

Quoted 4 lines

2) current file_contexts.bin vs contexts1 file_contexts [Not expected. Expected to be able to compare the current file_contexts.bin with old text file_contexts]
selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin

Quoted 8 lines

contexts1/files/file_contexts contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.homedirs.bin: Old compiled fcontext format, skipping
selabel_cmp: mismatch regex_str right remnant in stem tmp
selabel_cmp: mismatch child node tmp stem (root)
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is uncomparable to spec contexts1/files/file_contexts

Gives subset with v3 (since when using a .bin extension .homedirs and .local are not loaded).

Quoted 8 lines

3) current file_contexts.bin vs contexts1 file_contexts.bin [Expected, but confusing]
selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin contexts1/files/file_contexts.bin contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping
ERROR: selabel_open - Could not obtain handle for contexts1/files/file_contexts.bin: No such file or directory

Updated errno to EINVAL for this case.

Quoted 6 lines

4) current file_contexts vs contexts2 file_contexts [Expected] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts contexts2/files/file_contexts
spec /etc/selinux/targeted/contexts/files/file_contexts is equal to spec contexts2/files/file_contexts

Quoted 9 lines

5) current file_contexts.bin vs contexts2 file_contexts [Not expected. Expected to be able to compare the current file_contexts.bin with contexts2 text file_contexts]
selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin contexts2/files/file_contexts
selabel_cmp: mismatch regex_str right remnant in stem tmp
selabel_cmp: mismatch child node tmp stem (root)
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is uncomparable to spec contexts2/files/file_contexts

Should give subset (due to the missing .homedirs|.local) now.

Quoted 6 lines

6) current file_contexts.bin vs contexts2 file_contexts.bin [Expected] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin contexts2/files/file_contexts.bin
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is equal to spec contexts2/files/file_contexts.bin

Quoted 7 lines

7) contexts1 file_contexts vs contexts2 file_contexts [Expected, but confusing] selabel_compare contexts1/files/file_contexts contexts2/files/file_contexts contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.homedirs.bin: Old compiled fcontext format, skipping
spec contexts1/files/file_contexts is equal to spec contexts2/files/file_contexts

What is confusing here? selabel_open(3) tries to open binary fcontext files first for performance.

Quoted 170 lines

8) contexts1 file_contexts vs contexts2 file_contexts.cpy (which is a copy of file_contexts) [Not expected. Expected them to be equal] selabel_compare contexts1/files/file_contexts contexts2/files/file_contexts.cpy contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.homedirs.bin: Old compiled fcontext format, skipping
selabel_cmp: mismatch regex_str left remnant in stem tmp
selabel_cmp: mismatch child node tmp stem (root)
spec contexts1/files/file_contexts is uncomparable to spec contexts2/files/file_contexts.cpy

With all of the issues, I wonder how useful this actually would be. It might cause more confusion then anything.

Thanks,
Jim

Quoted 153 lines

---
v2:
split nested block into own function

---
 libselinux/utils/.gitignore        |   1 +
 libselinux/utils/selabel_compare.c | 122 +++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+)
 create mode 100644 libselinux/utils/selabel_compare.c
libselinux/utils/.gitignore +1 -0
index b3311360..2e10b14f 100644
@@ -16,6 +16,7 @@ getseuser
 matchpathcon
 policyvers
 sefcontext_compile
+selabel_compare
 selabel_digest
 selabel_get_digests_all_partial_matches
 selabel_lookup
libselinux/utils/selabel_compare.c new file +122 -1
new file mode 100644
index 00000000..9ca6eff1
@@ -0,0 +1,122 @@
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <selinux/label.h>
+
+
+static void usage(const char *progname)
+{
+       fprintf(stderr,
+               "usage: %s [-b backend] [-v] file1 file2\n\n"
+               "Where:\n\t"
+               "-b           The backend - \"file\", \"media\", \"x\", \"db\" or \"prop\" (defaults to \"file\")\n\t"
+               "-v           Validate entries against loaded policy.\n\t"
+               "file1/file2  Files containing the specs.\n",
+               progname);
+}
+
+static int compare(const char *file1, const char *file2, const char *validate, unsigned int backend)
+{
+       struct selabel_handle *hnd1, *hnd2;
+       const struct selinux_opt selabel_option1[] = {
+               { SELABEL_OPT_PATH, file1 },
+               { SELABEL_OPT_VALIDATE, validate }
+       };
+       const struct selinux_opt selabel_option2[] = {
+               { SELABEL_OPT_PATH, file2 },
+               { SELABEL_OPT_VALIDATE, validate }
+       };
+       enum selabel_cmp_result result;
+
+       hnd1 = selabel_open(backend, selabel_option1, 2);
+       if (!hnd1) {
+               fprintf(stderr, "ERROR: selabel_open - Could not obtain handle for %s:  %m\n", file1);
+               return EXIT_FAILURE;
+       }
+
+       hnd2 = selabel_open(backend, selabel_option2, 2);
+       if (!hnd2) {
+               fprintf(stderr, "ERROR: selabel_open - Could not obtain handle for %s:  %m\n", file2);
+               selabel_close(hnd1);
+               return EXIT_FAILURE;
+       }
+
+       result = selabel_cmp(hnd1, hnd2);
+
+       selabel_close(hnd2);
+       selabel_close(hnd1);
+
+       switch (result) {
+       case SELABEL_SUBSET:
+               printf("spec %s is a subset of spec %s\n", file1, file2);
+               break;
+       case SELABEL_EQUAL:
+               printf("spec %s is equal to spec %s\n", file1, file2);
+               break;
+       case SELABEL_SUPERSET:
+               printf("spec %s is a superset of spec %s\n", file1, file2);
+               break;
+       case SELABEL_INCOMPARABLE:
+               printf("spec %s is uncomparable to spec %s\n", file1, file2);
+               break;
+       default:
+               fprintf(stderr, "ERROR: selabel_cmp - Unexpected result %d\n", result);
+               return EXIT_FAILURE;
+       }
+
+       return EXIT_SUCCESS;
+}
+
+int main(int argc, char *argv[])
+{
+       unsigned int backend = SELABEL_CTX_FILE;
+       int opt;
+       const char *validate = NULL, *file1 = NULL, *file2 = NULL;
+
+       if (argc < 3) {
+               usage(argv[0]);
+               return EXIT_FAILURE;
+       }
+
+       while ((opt = getopt(argc, argv, "b:v")) > 0) {
+               switch (opt) {
+               case 'b':
+                       if (!strcasecmp(optarg, "file")) {
+                               backend = SELABEL_CTX_FILE;
+                       } else if (!strcmp(optarg, "media")) {
+                               backend = SELABEL_CTX_MEDIA;
+                       } else if (!strcmp(optarg, "x")) {
+                               backend = SELABEL_CTX_X;
+                       } else if (!strcmp(optarg, "db")) {
+                               backend = SELABEL_CTX_DB;
+                       } else if (!strcmp(optarg, "prop")) {
+                               backend = SELABEL_CTX_ANDROID_PROP;
+                       } else if (!strcmp(optarg, "service")) {
+                               backend = SELABEL_CTX_ANDROID_SERVICE;
+                       } else {
+                               fprintf(stderr, "Unknown backend: %s\n", optarg);
+                               usage(argv[0]);
+                               return EXIT_FAILURE;
+                       }
+                       break;
+               case 'v':
+                       validate = (char *)1;
+                       break;
+               default:
+                       usage(argv[0]);
+                       return EXIT_FAILURE;
+               }
+       }
+
+       if (argc != optind + 2) {
+               usage(argv[0]);
+               return EXIT_FAILURE;
+       }
+
+       file1 = argv[optind++];
+       file2 = argv[optind];
+
+       return compare(file1, file2, validate, backend);
+}
--

2.43.0

#5 lore raw

Re: [RFC PATCH v2 2/9] libselinux/utils: introduce selabel_compare

Quoted 47 lines · Christian Göttsche
Quoted 6 lines · James Carter
Quoted 3 lines · Christian Göttsche

Add a utility around selabel_cmp(3).

Quoted 8 lines
Quoted 7 lines

Can be used by users to compare a pre-compiled fcontext file to an original text-based file context definition file.

Can be used for development to verify compilation and parsing of the pre-compiled fcontext format works correctly.

First of all many thanks for testing!

Quoted 20 lines

I don't have any comments on the code.

It took me a while to get this to work. It was very confusing. Not because of your code, but because of the behavior of selabel_cmp(). It is not intuitive and I think that it needs to be better documented. It will absolutely not work as expected on a copy of the contexts directory.

With all that, not everything worked as I thought it would.

current = installed contexts directory
the other file is by itself (and not in a copy of the contexts directory)

1) current file_contexts.bin vs file_contexts [Expected] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin file_contexts
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is equal to spec file_contexts

Quoted 7 lines

2) current file_contexts vs file_contexts [Not expected, diff says they are equal]
selabel_compare /etc/selinux/targeted/contexts/files/file_contexts file_contexts
selabel_cmp: mismatch regex_str left remnant in stem tmp
selabel_cmp: mismatch child node tmp stem (root)
spec /etc/selinux/targeted/contexts/files/file_contexts is uncomparable to spec file_contexts

Quoted 8 lines

Since the database is opened via selabel_open(3) filecontexts.(homedirs|local) are implicitly loaded too.
It would be confusing if the selabel_compare helper would deviate from that default behavior.
Also currently there is no information in the in-memory database from which/how-many files the database was loaded.
Maybe that should be added so the selabel_compare helper could give a hint?

I am not very familiar with the selabel code, but I could see what it was doing. It is just very confusing behavior. The behavior is different depending on where the files being compared are located. If neither is in something that looks like /etc/selinux/targeted/contexts/files/, then it works as expected, otherwise it works differently. I think the expected behavior needs to be explained better.

Quoted 40 lines
Quoted 11 lines

3) current file_contexts.bin vs file_contexts_1 [Expected] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin file_contexts_1
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is equal to spec file_contexts_1

4) file_contexts vs file_contexts_1 [Expected] selabel_compare file_contexts file_contexts_1 spec file_contexts is equal to spec file_contexts_1

Quoted 4 lines

I realize now that selabel_cmp() can only find a superset or a subset if the only difference occurs at the very end. The longer file must be exactly like the shorter file except for the additional lines at the end.

Thanks, this is indeed a shortcoming in v2 (regex prefix length were not considered). Fixed in wip-v3:
https://github.com/SELinuxProject/selinux/compare/f07e4c3b66416f6337d6a2ea1e0d69555f8704ad..090af7e6510edb90f4e7df55346e5023ac190b66

Quoted 11 lines

I tried testing with the zebra module disabled, so it should have been a subset of the current policy, but, of course, selabel_cmp() was not sophisticated enough to say that. It just says that everything is uncomparable. It would be nice if something said that.

The following worked only in the most trivial case where I could just use diff instead.
contexts1 = copy of original contexts directory
contexts2 = copy of new contexts directory with new selabel db

Quoted 8 lines

1) current file_contexts vs contexts1 file_contexts [Expected, but Confusing] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts contexts1/files/file_contexts
contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.homedirs.bin: Old compiled fcontext format, skipping
spec /etc/selinux/targeted/contexts/files/file_contexts is equal to spec contexts1/files/file_contexts

Quoted 2 lines

What is confusing here?

What was confusing was the fact that it is referring to files that I did not ask it to compare. I understand that is the behavior of the library, but that is not what I would have expected from running the command.

Quoted 59 lines
Quoted 4 lines

2) current file_contexts.bin vs contexts1 file_contexts [Not expected. Expected to be able to compare the current file_contexts.bin with old text file_contexts]
selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin

Quoted 8 lines

contexts1/files/file_contexts contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.homedirs.bin: Old compiled fcontext format, skipping
selabel_cmp: mismatch regex_str right remnant in stem tmp
selabel_cmp: mismatch child node tmp stem (root)
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is uncomparable to spec contexts1/files/file_contexts

Gives subset with v3 (since when using a .bin extension .homedirs and .local are not loaded).

Quoted 8 lines

3) current file_contexts.bin vs contexts1 file_contexts.bin [Expected, but confusing]
selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin contexts1/files/file_contexts.bin contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping
ERROR: selabel_open - Could not obtain handle for contexts1/files/file_contexts.bin: No such file or directory

Updated errno to EINVAL for this case.

Quoted 6 lines

4) current file_contexts vs contexts2 file_contexts [Expected] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts contexts2/files/file_contexts
spec /etc/selinux/targeted/contexts/files/file_contexts is equal to spec contexts2/files/file_contexts

Quoted 9 lines

5) current file_contexts.bin vs contexts2 file_contexts [Not expected. Expected to be able to compare the current file_contexts.bin with contexts2 text file_contexts]
selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin contexts2/files/file_contexts
selabel_cmp: mismatch regex_str right remnant in stem tmp
selabel_cmp: mismatch child node tmp stem (root)
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is uncomparable to spec contexts2/files/file_contexts

Should give subset (due to the missing .homedirs|.local) now.

Quoted 6 lines

6) current file_contexts.bin vs contexts2 file_contexts.bin [Expected] selabel_compare /etc/selinux/targeted/contexts/files/file_contexts.bin contexts2/files/file_contexts.bin
spec /etc/selinux/targeted/contexts/files/file_contexts.bin is equal to spec contexts2/files/file_contexts.bin

Quoted 7 lines

7) contexts1 file_contexts vs contexts2 file_contexts [Expected, but confusing] selabel_compare contexts1/files/file_contexts contexts2/files/file_contexts contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.homedirs.bin: Old compiled fcontext format, skipping
spec contexts1/files/file_contexts is equal to spec contexts2/files/file_contexts

Quoted 3 lines

What is confusing here? selabel_open(3) tries to open binary fcontext files first for performance.

I understand what it is doing, but I think that this would be confusing to anyone using the tool.

Thanks,
Jim

Quoted 170 lines
Quoted 170 lines

8) contexts1 file_contexts vs contexts2 file_contexts.cpy (which is a copy of file_contexts) [Not expected. Expected them to be equal] selabel_compare contexts1/files/file_contexts contexts2/files/file_contexts.cpy contexts1/files/file_contexts.bin: Old compiled fcontext format, skipping contexts1/files/file_contexts.homedirs.bin: Old compiled fcontext format, skipping
selabel_cmp: mismatch regex_str left remnant in stem tmp
selabel_cmp: mismatch child node tmp stem (root)
spec contexts1/files/file_contexts is uncomparable to spec contexts2/files/file_contexts.cpy

With all of the issues, I wonder how useful this actually would be. It might cause more confusion then anything.

Thanks,
Jim

Quoted 153 lines

---
v2:
split nested block into own function

---
 libselinux/utils/.gitignore        |   1 +
 libselinux/utils/selabel_compare.c | 122 +++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+)
 create mode 100644 libselinux/utils/selabel_compare.c
libselinux/utils/.gitignore +1 -0
index b3311360..2e10b14f 100644
@@ -16,6 +16,7 @@ getseuser
 matchpathcon
 policyvers
 sefcontext_compile
+selabel_compare
 selabel_digest
 selabel_get_digests_all_partial_matches
 selabel_lookup
libselinux/utils/selabel_compare.c new file +122 -1
new file mode 100644
index 00000000..9ca6eff1
@@ -0,0 +1,122 @@
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <selinux/label.h>
+
+
+static void usage(const char *progname)
+{
+       fprintf(stderr,
+               "usage: %s [-b backend] [-v] file1 file2\n\n"
+               "Where:\n\t"
+               "-b           The backend - \"file\", \"media\", \"x\", \"db\" or \"prop\" (defaults to \"file\")\n\t"
+               "-v           Validate entries against loaded policy.\n\t"
+               "file1/file2  Files containing the specs.\n",
+               progname);
+}
+
+static int compare(const char *file1, const char *file2, const char *validate, unsigned int backend)
+{
+       struct selabel_handle *hnd1, *hnd2;
+       const struct selinux_opt selabel_option1[] = {
+               { SELABEL_OPT_PATH, file1 },
+               { SELABEL_OPT_VALIDATE, validate }
+       };
+       const struct selinux_opt selabel_option2[] = {
+               { SELABEL_OPT_PATH, file2 },
+               { SELABEL_OPT_VALIDATE, validate }
+       };
+       enum selabel_cmp_result result;
+
+       hnd1 = selabel_open(backend, selabel_option1, 2);
+       if (!hnd1) {
+               fprintf(stderr, "ERROR: selabel_open - Could not obtain handle for %s:  %m\n", file1);
+               return EXIT_FAILURE;
+       }
+
+       hnd2 = selabel_open(backend, selabel_option2, 2);
+       if (!hnd2) {
+               fprintf(stderr, "ERROR: selabel_open - Could not obtain handle for %s:  %m\n", file2);
+               selabel_close(hnd1);
+               return EXIT_FAILURE;
+       }
+
+       result = selabel_cmp(hnd1, hnd2);
+
+       selabel_close(hnd2);
+       selabel_close(hnd1);
+
+       switch (result) {
+       case SELABEL_SUBSET:
+               printf("spec %s is a subset of spec %s\n", file1, file2);
+               break;
+       case SELABEL_EQUAL:
+               printf("spec %s is equal to spec %s\n", file1, file2);
+               break;
+       case SELABEL_SUPERSET:
+               printf("spec %s is a superset of spec %s\n", file1, file2);
+               break;
+       case SELABEL_INCOMPARABLE:
+               printf("spec %s is uncomparable to spec %s\n", file1, file2);
+               break;
+       default:
+               fprintf(stderr, "ERROR: selabel_cmp - Unexpected result %d\n", result);
+               return EXIT_FAILURE;
+       }
+
+       return EXIT_SUCCESS;
+}
+
+int main(int argc, char *argv[])
+{
+       unsigned int backend = SELABEL_CTX_FILE;
+       int opt;
+       const char *validate = NULL, *file1 = NULL, *file2 = NULL;
+
+       if (argc < 3) {
+               usage(argv[0]);
+               return EXIT_FAILURE;
+       }
+
+       while ((opt = getopt(argc, argv, "b:v")) > 0) {
+               switch (opt) {
+               case 'b':
+                       if (!strcasecmp(optarg, "file")) {
+                               backend = SELABEL_CTX_FILE;
+                       } else if (!strcmp(optarg, "media")) {
+                               backend = SELABEL_CTX_MEDIA;
+                       } else if (!strcmp(optarg, "x")) {
+                               backend = SELABEL_CTX_X;
+                       } else if (!strcmp(optarg, "db")) {
+                               backend = SELABEL_CTX_DB;
+                       } else if (!strcmp(optarg, "prop")) {
+                               backend = SELABEL_CTX_ANDROID_PROP;
+                       } else if (!strcmp(optarg, "service")) {
+                               backend = SELABEL_CTX_ANDROID_SERVICE;
+                       } else {
+                               fprintf(stderr, "Unknown backend: %s\n", optarg);
+                               usage(argv[0]);
+                               return EXIT_FAILURE;
+                       }
+                       break;
+               case 'v':
+                       validate = (char *)1;
+                       break;
+               default:
+                       usage(argv[0]);
+                       return EXIT_FAILURE;
+               }
+       }
+
+       if (argc != optind + 2) {
+               usage(argv[0]);
+               return EXIT_FAILURE;
+       }
+
+       file1 = argv[optind++];
+       file2 = argv[optind];
+
+       return compare(file1, file2, validate, backend);
+}
--

2.43.0

#6 lore raw

RFC PATCH v2 3/9 libselinux: use more appropriate types in sidtab

In reply to Christian Göttsche

Use type unsigned for hash values, as returned by sidtab_hash(). Use size_t for buffer length and counting variables. Constify stats parameter.

---
v2:
add patch

---
 libselinux/src/avc_sidtab.c | 16 +++++++++-------
 libselinux/src/avc_sidtab.h |  2 +-
 2 files changed, 10 insertions(+), 8 deletions(-)
libselinux/src/avc_sidtab.c +9 -7
index 3303537b..fce5bddf 100644
@@ -45,7 +45,8 @@ int sidtab_init(struct sidtab *s)
 
 int sidtab_insert(struct sidtab *s, const char * ctx)
 {
-	int hvalue, rc = 0;
+	unsigned hvalue;
+	int rc = 0;
 	struct sidtab_node *newnode;
 	char * newctx;
 
@@ -75,7 +76,8 @@ int
 sidtab_context_to_sid(struct sidtab *s,
 		      const char * ctx, security_id_t * sid)
 {
-	int hvalue, rc = 0;
+	unsigned hvalue;
+	int rc = 0;
 	struct sidtab_node *cur;
 
 	*sid = NULL;
@@ -98,10 +100,10 @@ sidtab_context_to_sid(struct sidtab *s,
 	return rc;
 }
 
-void sidtab_sid_stats(struct sidtab *s, char *buf, int buflen)
+void sidtab_sid_stats(const struct sidtab *s, char *buf, size_t buflen)
 {
-	int i, chain_len, slots_used, max_chain_len;
-	struct sidtab_node *cur;
+	size_t i, chain_len, slots_used, max_chain_len;
+	const struct sidtab_node *cur;
 
 	slots_used = 0;
 	max_chain_len = 0;
@@ -121,8 +123,8 @@ void sidtab_sid_stats(struct sidtab *s, char *buf, int buflen)
 	}
 
 	snprintf(buf, buflen,
-		 "%s:  %u SID entries and %d/%d buckets used, longest "
-		 "chain length %d\n", avc_prefix, s->nel, slots_used,
+		 "%s:  %u SID entries and %zu/%d buckets used, longest "
+		 "chain length %zu\n", avc_prefix, s->nel, slots_used,
 		 SIDTAB_SIZE, max_chain_len);
 }
 
libselinux/src/avc_sidtab.h +1 -1
index cc5abe35..e823e3f3 100644
@@ -29,7 +29,7 @@ int sidtab_insert(struct sidtab *s, const char * ctx) ;
 int sidtab_context_to_sid(struct sidtab *s,
 			  const char * ctx, security_id_t * sid) ;
 
-void sidtab_sid_stats(struct sidtab *s, char *buf, int buflen) ;
+void sidtab_sid_stats(const struct sidtab *s, char *buf, size_t buflen) ;
 void sidtab_destroy(struct sidtab *s) ;
 
 #endif				/* _SELINUX_AVC_SIDTAB_H_ */
Signature · 1 line
2.43.0
#7 lore raw

RFC PATCH v2 4/9 libselinux: add unique id to sidtab entries

In reply to Christian Göttsche

Reinterpret the currently unused - and always initialized to 1 - member refcnt of the struct security_id to hold a unique number identifying the sidtab entry. This identifier can be used instead of the full context string within other data structures to minimize memory usage.

---
v2:
add patch

---
 libselinux/include/selinux/avc.h | 2 +-
 libselinux/src/avc_sidtab.c      | 9 +++++++--
 2 files changed, 8 insertions(+), 3 deletions(-)
libselinux/include/selinux/avc.h +1 -1
index 4bbd2382..361c5611 100644
@@ -20,7 +20,7 @@ extern "C" {
  */
 struct security_id {
 	char * ctx;
-	unsigned int refcnt;
+	unsigned int id;
 };
 typedef struct security_id *security_id_t;
 
libselinux/src/avc_sidtab.c +7 -2
index fce5bddf..9475dcb0 100644
@@ -4,6 +4,7 @@
  * Author : Eamon Walsh, <[email protected]>
  */
 #include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
@@ -50,6 +51,11 @@ int sidtab_insert(struct sidtab *s, const char * ctx)
 	struct sidtab_node *newnode;
 	char * newctx;
 
+	if (s->nel >= UINT_MAX - 1) {
+		rc = -1;
+		goto out;
+	}
+
 	newnode = (struct sidtab_node *)avc_malloc(sizeof(*newnode));
 	if (!newnode) {
 		rc = -1;
@@ -65,9 +71,8 @@ int sidtab_insert(struct sidtab *s, const char * ctx)
 	hvalue = sidtab_hash(newctx);
 	newnode->next = s->htable[hvalue];
 	newnode->sid_s.ctx = newctx;
-	newnode->sid_s.refcnt = 1;	/* unused */
+	newnode->sid_s.id = ++s->nel;
 	s->htable[hvalue] = newnode;
-	s->nel++;
       out:
 	return rc;
 }
Signature · 1 line
2.43.0
#8 lore raw

RFC PATCH v2 5/9 libselinux: sidtab updates

In reply to Christian Göttsche

Add sidtab_context_lookup() to just lookup a context, not inserting non-existent ones.

Tweak sidtab_destroy() to accept a zero'ed struct sidtab.

Remove redundant lookup in sidtab_context_to_sid() after insertion by returning the newly created node directly from sidtab_insert().

Drop declaration of only internal used sidtab_insert().

---
v2:
add patch

---
 libselinux/src/avc_sidtab.c | 55 +++++++++++++++++++++----------------
 libselinux/src/avc_sidtab.h |  2 +-
 2 files changed, 32 insertions(+), 25 deletions(-)
libselinux/src/avc_sidtab.c +31 -24
index 9475dcb0..3d347cea 100644
@@ -44,28 +44,23 @@ int sidtab_init(struct sidtab *s)
 	return rc;
 }
 
-int sidtab_insert(struct sidtab *s, const char * ctx)
+static struct sidtab_node *
+sidtab_insert(struct sidtab *s, const char * ctx)
 {
 	unsigned hvalue;
-	int rc = 0;
 	struct sidtab_node *newnode;
 	char * newctx;
 
-	if (s->nel >= UINT_MAX - 1) {
-		rc = -1;
-		goto out;
-	}
+	if (s->nel >= UINT_MAX - 1)
+		return NULL;
 
 	newnode = (struct sidtab_node *)avc_malloc(sizeof(*newnode));
-	if (!newnode) {
-		rc = -1;
-		goto out;
-	}
+	if (!newnode)
+		return NULL;
 	newctx = strdup(ctx);
 	if (!newctx) {
-		rc = -1;
 		avc_free(newnode);
-		goto out;
+		return NULL;
 	}
 
 	hvalue = sidtab_hash(newctx);
@@ -73,8 +68,25 @@ int sidtab_insert(struct sidtab *s, const char * ctx)
 	newnode->sid_s.ctx = newctx;
 	newnode->sid_s.id = ++s->nel;
 	s->htable[hvalue] = newnode;
-      out:
-	return rc;
+	return newnode;
+}
+
+const struct security_id *
+sidtab_context_lookup(const struct sidtab *s, const char *ctx)
+{
+	unsigned hvalue;
+	const struct sidtab_node *cur;
+
+	hvalue = sidtab_hash(ctx);
+
+	cur = s->htable[hvalue];
+	while (cur != NULL && strcmp(cur->sid_s.ctx, ctx))
+		cur = cur->next;
+
+	if (cur == NULL)
+		return NULL;
+
+	return &cur->sid_s;
 }
 
 int
@@ -82,27 +94,23 @@ sidtab_context_to_sid(struct sidtab *s,
 		      const char * ctx, security_id_t * sid)
 {
 	unsigned hvalue;
-	int rc = 0;
 	struct sidtab_node *cur;
 
 	*sid = NULL;
 	hvalue = sidtab_hash(ctx);
 
-      loop:
 	cur = s->htable[hvalue];
 	while (cur != NULL && strcmp(cur->sid_s.ctx, ctx))
 		cur = cur->next;
 
 	if (cur == NULL) {	/* need to make a new entry */
-		rc = sidtab_insert(s, ctx);
-		if (rc)
-			goto out;
-		goto loop;	/* find the newly inserted node */
+		cur = sidtab_insert(s, ctx);
+		if (cur == NULL)
+			return -1;
 	}
 
 	*sid = &cur->sid_s;
-      out:
-	return rc;
+	return 0;
 }
 
 void sidtab_sid_stats(const struct sidtab *s, char *buf, size_t buflen)
@@ -138,7 +146,7 @@ void sidtab_destroy(struct sidtab *s)
 	int i;
 	struct sidtab_node *cur, *temp;
 
-	if (!s)
+	if (!s || !s->htable)
 		return;
 
 	for (i = 0; i < SIDTAB_SIZE; i++) {
@@ -149,7 +157,6 @@ void sidtab_destroy(struct sidtab *s)
 			freecon(temp->sid_s.ctx);
 			avc_free(temp);
 		}
-		s->htable[i] = NULL;
 	}
 	avc_free(s->htable);
 	s->htable = NULL;
libselinux/src/avc_sidtab.h +1 -1
index e823e3f3..f62fd353 100644
@@ -24,8 +24,8 @@ struct sidtab {
 };
 
 int sidtab_init(struct sidtab *s) ;
-int sidtab_insert(struct sidtab *s, const char * ctx) ;
 
+const struct security_id * sidtab_context_lookup(const struct sidtab *s, const char *ctx);
 int sidtab_context_to_sid(struct sidtab *s,
 			  const char * ctx, security_id_t * sid) ;
 
Signature · 1 line
2.43.0
#9 lore raw

Re: [RFC PATCH v2 5/9] libselinux: sidtab updates

Quoted 109 lines · Christian Göttsche

Add sidtab_context_lookup() to just lookup a context, not inserting non-existent ones.

Tweak sidtab_destroy() to accept a zero'ed struct sidtab.

Remove redundant lookup in sidtab_context_to_sid() after insertion by returning the newly created node directly from sidtab_insert().

Drop declaration of only internal used sidtab_insert().

---
v2:
add patch

---
 libselinux/src/avc_sidtab.c | 55 +++++++++++++++++++++----------------
 libselinux/src/avc_sidtab.h |  2 +-
 2 files changed, 32 insertions(+), 25 deletions(-)
libselinux/src/avc_sidtab.c +27 -20
index 9475dcb0..3d347cea 100644
@@ -44,28 +44,23 @@ int sidtab_init(struct sidtab *s)
        return rc;
 }
 
-int sidtab_insert(struct sidtab *s, const char * ctx)
+static struct sidtab_node *
+sidtab_insert(struct sidtab *s, const char * ctx)
 {
        unsigned hvalue;
-       int rc = 0;
        struct sidtab_node *newnode;
        char * newctx;
 
-       if (s->nel >= UINT_MAX - 1) {
-               rc = -1;
-               goto out;
-       }
+       if (s->nel >= UINT_MAX - 1)
+               return NULL;
 
        newnode = (struct sidtab_node *)avc_malloc(sizeof(*newnode));
-       if (!newnode) {
-               rc = -1;
-               goto out;
-       }
+       if (!newnode)
+               return NULL;
        newctx = strdup(ctx);
        if (!newctx) {
-               rc = -1;
                avc_free(newnode);
-               goto out;
+               return NULL;
        }
 
        hvalue = sidtab_hash(newctx);
@@ -73,8 +68,25 @@ int sidtab_insert(struct sidtab *s, const char * ctx)
        newnode->sid_s.ctx = newctx;
        newnode->sid_s.id = ++s->nel;
        s->htable[hvalue] = newnode;
-      out:
-       return rc;
+       return newnode;
+}
+
+const struct security_id *
+sidtab_context_lookup(const struct sidtab *s, const char *ctx)
+{
+       unsigned hvalue;
+       const struct sidtab_node *cur;
+
+       hvalue = sidtab_hash(ctx);
+
+       cur = s->htable[hvalue];
+       while (cur != NULL && strcmp(cur->sid_s.ctx, ctx))
+               cur = cur->next;
+
+       if (cur == NULL)
+               return NULL;
+
+       return &cur->sid_s;
 }
 
 int
@@ -82,27 +94,23 @@ sidtab_context_to_sid(struct sidtab *s,
                      const char * ctx, security_id_t * sid)
 {
        unsigned hvalue;
-       int rc = 0;
        struct sidtab_node *cur;
 
        *sid = NULL;
        hvalue = sidtab_hash(ctx);
 
-      loop:
        cur = s->htable[hvalue];
        while (cur != NULL && strcmp(cur->sid_s.ctx, ctx))
                cur = cur->next;
 
        if (cur == NULL) {      /* need to make a new entry */
-               rc = sidtab_insert(s, ctx);
-               if (rc)
-                       goto out;
-               goto loop;      /* find the newly inserted node */
+               cur = sidtab_insert(s, ctx);

libselinux/src/avc_sidtab.c sidtab_context_to_sid(struct sidtab *s, · line 115

Quoted 10 lines
libselinux/src/avc_sidtab.c +3 -2
+               if (cur == NULL)
+                       return -1;
        }
 
        *sid = &cur->sid_s;
-      out:
-       return rc;
+       return 0;
 }
 

This duplicates the sidtab_context_lookup() code above, so why not just use that. If that returns NULL, then insert the context.

Thanks,
Jim

Quoted 36 lines
libselinux/src/avc_sidtab.c +1 -2
 void sidtab_sid_stats(const struct sidtab *s, char *buf, size_t buflen)
@@ -138,7 +146,7 @@ void sidtab_destroy(struct sidtab *s)
        int i;
        struct sidtab_node *cur, *temp;
 
-       if (!s)
+       if (!s || !s->htable)
                return;
 
        for (i = 0; i < SIDTAB_SIZE; i++) {
@@ -149,7 +157,6 @@ void sidtab_destroy(struct sidtab *s)
                        freecon(temp->sid_s.ctx);
                        avc_free(temp);
                }
-               s->htable[i] = NULL;
        }
        avc_free(s->htable);
        s->htable = NULL;
libselinux/src/avc_sidtab.h +1 -2
index e823e3f3..f62fd353 100644
@@ -24,8 +24,8 @@ struct sidtab {
 };
 
 int sidtab_init(struct sidtab *s) ;
-int sidtab_insert(struct sidtab *s, const char * ctx) ;
 
+const struct security_id * sidtab_context_lookup(const struct sidtab *s, const char *ctx);
 int sidtab_context_to_sid(struct sidtab *s,
                          const char * ctx, security_id_t * sid) ;
 
--

2.43.0

#10 lore raw

Re: [RFC PATCH v2 5/9] libselinux: sidtab updates

Quoted 126 lines · James Carter
Quoted 109 lines · Christian Göttsche

Add sidtab_context_lookup() to just lookup a context, not inserting non-existent ones.

Tweak sidtab_destroy() to accept a zero'ed struct sidtab.

Remove redundant lookup in sidtab_context_to_sid() after insertion by returning the newly created node directly from sidtab_insert().

Drop declaration of only internal used sidtab_insert().

---
v2:
add patch

---
 libselinux/src/avc_sidtab.c | 55 +++++++++++++++++++++----------------
 libselinux/src/avc_sidtab.h |  2 +-
 2 files changed, 32 insertions(+), 25 deletions(-)
libselinux/src/avc_sidtab.c +27 -20
index 9475dcb0..3d347cea 100644
@@ -44,28 +44,23 @@ int sidtab_init(struct sidtab *s)
        return rc;
 }
 
-int sidtab_insert(struct sidtab *s, const char * ctx)
+static struct sidtab_node *
+sidtab_insert(struct sidtab *s, const char * ctx)
 {
        unsigned hvalue;
-       int rc = 0;
        struct sidtab_node *newnode;
        char * newctx;
 
-       if (s->nel >= UINT_MAX - 1) {
-               rc = -1;
-               goto out;
-       }
+       if (s->nel >= UINT_MAX - 1)
+               return NULL;
 
        newnode = (struct sidtab_node *)avc_malloc(sizeof(*newnode));
-       if (!newnode) {
-               rc = -1;
-               goto out;
-       }
+       if (!newnode)
+               return NULL;
        newctx = strdup(ctx);
        if (!newctx) {
-               rc = -1;
                avc_free(newnode);
-               goto out;
+               return NULL;
        }
 
        hvalue = sidtab_hash(newctx);
@@ -73,8 +68,25 @@ int sidtab_insert(struct sidtab *s, const char * ctx)
        newnode->sid_s.ctx = newctx;
        newnode->sid_s.id = ++s->nel;
        s->htable[hvalue] = newnode;
-      out:
-       return rc;
+       return newnode;
+}
+
+const struct security_id *
+sidtab_context_lookup(const struct sidtab *s, const char *ctx)
+{
+       unsigned hvalue;
+       const struct sidtab_node *cur;
+
+       hvalue = sidtab_hash(ctx);
+
+       cur = s->htable[hvalue];
+       while (cur != NULL && strcmp(cur->sid_s.ctx, ctx))
+               cur = cur->next;
+
+       if (cur == NULL)
+               return NULL;
+
+       return &cur->sid_s;
 }
 
 int
@@ -82,27 +94,23 @@ sidtab_context_to_sid(struct sidtab *s,
                      const char * ctx, security_id_t * sid)
 {
        unsigned hvalue;
-       int rc = 0;
        struct sidtab_node *cur;
 
        *sid = NULL;
        hvalue = sidtab_hash(ctx);
 
-      loop:
        cur = s->htable[hvalue];
        while (cur != NULL && strcmp(cur->sid_s.ctx, ctx))
                cur = cur->next;
 
        if (cur == NULL) {      /* need to make a new entry */
-               rc = sidtab_insert(s, ctx);
-               if (rc)
-                       goto out;
-               goto loop;      /* find the newly inserted node */
+               cur = sidtab_insert(s, ctx);

libselinux/src/avc_sidtab.c sidtab_context_to_sid(struct sidtab *s, · line 115

Quoted 10 lines
libselinux/src/avc_sidtab.c +3 -2
+               if (cur == NULL)
+                       return -1;
        }
 
        *sid = &cur->sid_s;
-      out:
-       return rc;
+       return 0;
 }
 

This duplicates the sidtab_context_lookup() code above, so why not just use that. If that returns NULL, then insert the context.

Quoted 2 lines

Thanks,
Jim

True; applied in wip-v3:
https://github.com/SELinuxProject/selinux/pull/406/commits/445cc5fc903fb6da7cefa059e29e6a7ed91302e9

Quoted 36 lines
Quoted 36 lines
libselinux/src/avc_sidtab.c +1 -2
 void sidtab_sid_stats(const struct sidtab *s, char *buf, size_t buflen)
@@ -138,7 +146,7 @@ void sidtab_destroy(struct sidtab *s)
        int i;
        struct sidtab_node *cur, *temp;
 
-       if (!s)
+       if (!s || !s->htable)
                return;
 
        for (i = 0; i < SIDTAB_SIZE; i++) {
@@ -149,7 +157,6 @@ void sidtab_destroy(struct sidtab *s)
                        freecon(temp->sid_s.ctx);
                        avc_free(temp);
                }
-               s->htable[i] = NULL;
        }
        avc_free(s->htable);
        s->htable = NULL;
libselinux/src/avc_sidtab.h +1 -2
index e823e3f3..f62fd353 100644
@@ -24,8 +24,8 @@ struct sidtab {
 };
 
 int sidtab_init(struct sidtab *s) ;
-int sidtab_insert(struct sidtab *s, const char * ctx) ;
 
+const struct security_id * sidtab_context_lookup(const struct sidtab *s, const char *ctx);
 int sidtab_context_to_sid(struct sidtab *s,
                          const char * ctx, security_id_t * sid) ;
 
--

2.43.0

#11 lore raw

RFC PATCH v2 6/9 libselinux: rework selabel_file(5) database

In reply to Christian Göttsche

Currently the database for file backend of selabel stores the file context specifications in a single long array. This array is sorted by special precedence rules, e.g. regular expressions without meta character first, ordered by length, and the remaining regular expressions ordered by stem (the prefix part of the regular expressions without meta characters) length.

This results in suboptimal lookup performance for two reasons; File context specifications without any meta characters (e.g. '/etc/passwd') are still matched via an expensive regular expression match operation.
All such trivial regular expressions are matched against before any non- trivial regular expression, resulting in thousands of regex match operations for lookups for paths not matching any of the trivial ones.

Rework the internal representation of the database in two ways:
Convert regular expressions without any meta characters and containing only supported escaped characters (e.g. '/etc/rc\.d/init\.d') into literal strings, which get compared via strcmp(3) later on. Store the specifications in a tree structure to reduce the to number of specifications that need to be checked.

Since the internal representation is completely rewritten introduce a new compiled file context file format mirroring the tree structure. The new format also stores all multi-byte data in network byte-order, so that such compiled files can be cross-compiled, e.g. for embedded devices with read-only filesystems (except for the regular expressions, which are still architecture-dependent, but ignored on architecture mis- match).

The improved lookup performance will also benefit SELinux aware daemons, which create files with their default context, e.g. systemd.

Fedora 39 (pre-compiled regular expressions are omitted on Fedora):

    file_contexts.bin:           599034  ->   430211  (bytes)
    file_contexts.homedirs.bin:   21275  ->    13491  (bytes)

Debian Sid (pre-compiled regular expressions are included):

    file_contexts.bin:          7790690  ->  3646256  (bytes)
    file_contexts.homedirs.bin:  835950  ->   708793  (bytes)

(selabel_lookup -b file -k /bin/bash)

Fedora 39 in VM:

    text:      time:       3.1 ms  ->   3.6 ms
               peak heap:   2.33M  ->    1.81M
               peak rss:    6.64M  ->    6.37M
    compiled:  time:       1.8 ms  ->   1.7 ms
               peak heap:   2.14M  ->    1.23M
               peak rss:    6.76M  ->    5.91M

Debian Sid on Raspberry Pi 3:

    text:      time:      33.4 ms  ->  21.2 ms
               peak heap:  10.59M  ->  607.32K
               peak rss:    6.55M  ->    4.46M
    compiled:  time:      38.3 ms  ->  23.5 ms
               peak heap:  13.28M  ->    2.00M
               peak rss:   12.21M  ->    7.60M

(restorecon -vRn /)

Fedora 39 in VM:

      28.3 s  ->   3.6 s
Debian Sid on Raspberry Pi 3:
      94.6 s  ->  12.1 s

(restorecon -vRn -T0 /)

Fedora 39 in VM (8 cores):

      31.1 s  ->   2.5 s
Debian Sid on Raspberry Pi 3 (4 cores):
      58.9 s  ->  12.6 s

(note: I am unsure why the parallel runs on Fedora are slower)

There might be subtle differences in lookup results which evaded my testing, because some precedence rules are oblique. For example /usr/(.*/)?lib(/.*)? has to have a higher precedence than /usr/(.*/)?bin(/.*)? to match the current Fedora behavior. Please report any behavior changes.

The maximum node depth in the database is set to 3, which seems to give the best performance to memory usage ratio. Might be tweaked for systems with different filesystem hierarchies (Android?).

I am not that familiar with the selabel_partial_match(3), selabel_get_digests_all_partial_matches(3) and selabel_hash_all_partial_matches(3) related interfaces, so I only did some rudimentary tests for them.

CC Petr Lautrbach <[email protected]>
CC James Carter <[email protected]>
CC Stephen Smalley <[email protected]>

---
v2: misc issues revealed via fuzzing:
- free root node via data pointer in sefcontext_compile
- drop reachable assert in regex_simplify()
- fix crash on unexpected NUL byte in read_spec_entries()
- more strict checks when loading compiled format
- do not return <<none>> context for literal specs
- add missing partial support for literal specs
- set errno on load_mmap() failure
- support SELABEL_OPT_SUBSET
- de-duplicate context strings into a separate array for the binary format
- store an internal file kind instead of the whole mode_t

---
 libselinux/src/label_backends_android.c |    2 +-
 libselinux/src/label_file.c             | 2210 ++++++++++++++++-------
 libselinux/src/label_file.h             |  951 +++++++---
 libselinux/src/label_internal.h         |    6 +-
 libselinux/src/label_support.c          |   26 +-
 libselinux/src/regex.c                  |   55 +-
 libselinux/utils/sefcontext_compile.c   |  658 ++++---
 7 files changed, 2740 insertions(+), 1168 deletions(-)
libselinux/src/label_backends_android.c +1 -1
index 33a17236..97d58bb5 100644
@@ -91,7 +91,7 @@ static int process_line(struct selabel_handle *rec,
 	unsigned int nspec = data->nspec;
 	const char *errbuf = NULL;
 
-	items = read_spec_entries(line_buf, &errbuf, 2, &prop, &context);
+	items = read_spec_entries(line_buf, strlen(line_buf), &errbuf, 2, &prop, &context);
 	if (items < 0) {
 		if (errbuf) {
 			selinux_log(SELINUX_ERROR,
libselinux/src/label_file.c +1560 -650

2,585 lines not on this page · raw message

libselinux/src/label_file.h +671 -280

407 lines not on this page · raw message

libselinux/src/label_internal.h +3 -3
index ea60cd9a..854f92fa 100644

Lines of this file are not shown. View the raw message.

libselinux/src/label_support.c +16 -10
index f7ab9292..978ba828 100644

Lines of this file are not shown. View the raw message.

libselinux/src/regex.c +32 -23

55 lines not on this page · raw message

libselinux/utils/sefcontext_compile.c +457 -201

658 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.43.0

Part of the diff is not shown here. View the raw message.

#12 lore raw

RFC PATCH v2 7/9 libselinux: remove unused hashtab code

In reply to Christian Göttsche

Due to the selabel_file(5) rework this code is no longer used.

---
 libselinux/src/hashtab.c | 234 ---------------------------------------
 libselinux/src/hashtab.h | 117 --------------------
 2 files changed, 351 deletions(-)
 delete mode 100644 libselinux/src/hashtab.c
 delete mode 100644 libselinux/src/hashtab.h
libselinux/src/hashtab.c deleted +0 -234
deleted file mode 100644
index 7452613b..00000000
@@ -1,234 +0,0 @@
-
-/* Author : Stephen Smalley, <[email protected]> */
-
-/* FLASK */
-
-/*
- * Implementation of the hash table type.
- */
-
-#include <stdlib.h>
-#include <string.h>
-#include "hashtab.h"
-
-hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h,
-						     const_hashtab_key_t key),
-			 int (*keycmp) (hashtab_t h,
-					const_hashtab_key_t key1,
-					const_hashtab_key_t key2),
-			 unsigned int size)
-{
-
-	hashtab_t p;
-	unsigned int i;
-
-	p = (hashtab_t) malloc(sizeof(hashtab_val_t));
-	if (p == NULL)
-		return p;
-
-	memset(p, 0, sizeof(hashtab_val_t));
-	p->size = size;
-	p->nel = 0;
-	p->hash_value = hash_value;
-	p->keycmp = keycmp;
-	p->htable = (hashtab_ptr_t *) malloc(sizeof(hashtab_ptr_t) * size);
-	if (p->htable == NULL) {
-		free(p);
-		return NULL;
-	}
-	for (i = 0; i < size; i++)
-		p->htable[i] = (hashtab_ptr_t) NULL;
-
-	return p;
-}
-
-int hashtab_insert(hashtab_t h, hashtab_key_t key, hashtab_datum_t datum)
-{
-	unsigned int hvalue;
-	hashtab_ptr_t prev, cur, newnode;
-
-	if (!h)
-		return HASHTAB_OVERFLOW;
-
-	hvalue = h->hash_value(h, key);
-	prev = NULL;
-	cur = h->htable[hvalue];
-	while (cur && h->keycmp(h, key, cur->key) > 0) {
-		prev = cur;
-		cur = cur->next;
-	}
-
-	if (cur && (h->keycmp(h, key, cur->key) == 0))
-		return HASHTAB_PRESENT;
-
-	newnode = (hashtab_ptr_t) malloc(sizeof(hashtab_node_t));
-	if (newnode == NULL)
-		return HASHTAB_OVERFLOW;
-	memset(newnode, 0, sizeof(struct hashtab_node));
-	newnode->key = key;
-	newnode->datum = datum;
-	if (prev) {
-		newnode->next = prev->next;
-		prev->next = newnode;
-	} else {
-		newnode->next = h->htable[hvalue];
-		h->htable[hvalue] = newnode;
-	}
-
-	h->nel++;
-	return HASHTAB_SUCCESS;
-}
-
-int hashtab_remove(hashtab_t h, hashtab_key_t key,
-		   void (*destroy) (hashtab_key_t k,
-				    hashtab_datum_t d, void *args), void *args)
-{
-	unsigned int hvalue;
-	hashtab_ptr_t cur, last;
-
-	if (!h)
-		return HASHTAB_MISSING;
-
-	hvalue = h->hash_value(h, key);
-	last = NULL;
-	cur = h->htable[hvalue];
-	while (cur != NULL && h->keycmp(h, key, cur->key) > 0) {
-		last = cur;
-		cur = cur->next;
-	}
-
-	if (cur == NULL || (h->keycmp(h, key, cur->key) != 0))
-		return HASHTAB_MISSING;
-
-	if (last == NULL)
-		h->htable[hvalue] = cur->next;
-	else
-		last->next = cur->next;
-
-	if (destroy)
-		destroy(cur->key, cur->datum, args);
-	free(cur);
-	h->nel--;
-	return HASHTAB_SUCCESS;
-}
-
-hashtab_datum_t hashtab_search(hashtab_t h, const_hashtab_key_t key)
-{
-
-	unsigned int hvalue;
-	hashtab_ptr_t cur;
-
-	if (!h)
-		return NULL;
-
-	hvalue = h->hash_value(h, key);
-	cur = h->htable[hvalue];
-	while (cur != NULL && h->keycmp(h, key, cur->key) > 0)
-		cur = cur->next;
-
-	if (cur == NULL || (h->keycmp(h, key, cur->key) != 0))
-		return NULL;
-
-	return cur->datum;
-}
-
-void hashtab_destroy(hashtab_t h)
-{
-	unsigned int i;
-	hashtab_ptr_t cur, temp;
-
-	if (!h)
-		return;
-
-	for (i = 0; i < h->size; i++) {
-		cur = h->htable[i];
-		while (cur != NULL) {
-			temp = cur;
-			cur = cur->next;
-			free(temp);
-		}
-		h->htable[i] = NULL;
-	}
-
-	free(h->htable);
-	h->htable = NULL;
-
-	free(h);
-}
-
-void hashtab_destroy_key(hashtab_t h,
-		int (*destroy_key) (hashtab_key_t k))
-{
-	unsigned int i;
-	hashtab_ptr_t cur, temp;
-
-	if (!h)
-		return;
-
-	for (i = 0; i < h->size; i++) {
-		cur = h->htable[i];
-		while (cur != NULL) {
-			temp = cur;
-			cur = cur->next;
-			destroy_key(temp->key);
-			free(temp);
-		}
-		h->htable[i] = NULL;
-	}
-
-	free(h->htable);
-	h->htable = NULL;
-
-	free(h);
-}
-
-int hashtab_map(hashtab_t h,
-		int (*apply) (hashtab_key_t k,
-			      hashtab_datum_t d, void *args), void *args)
-{
-	unsigned int i;
-	hashtab_ptr_t cur;
-	int ret;
-
-	if (!h)
-		return HASHTAB_SUCCESS;
-
-	for (i = 0; i < h->size; i++) {
-		cur = h->htable[i];
-		while (cur != NULL) {
-			ret = apply(cur->key, cur->datum, args);
-			if (ret)
-				return ret;
-			cur = cur->next;
-		}
-	}
-	return HASHTAB_SUCCESS;
-}
-
-void hashtab_hash_eval(hashtab_t h, char *tag)
-{
-	unsigned int i;
-	int chain_len, slots_used, max_chain_len;
-	hashtab_ptr_t cur;
-
-	slots_used = 0;
-	max_chain_len = 0;
-	for (i = 0; i < h->size; i++) {
-		cur = h->htable[i];
-		if (cur) {
-			slots_used++;
-			chain_len = 0;
-			while (cur) {
-				chain_len++;
-				cur = cur->next;
-			}
-
-			if (chain_len > max_chain_len)
-				max_chain_len = chain_len;
-		}
-	}
-
-	printf
-	    ("%s:  %d entries and %d/%d buckets used, longest chain length %d\n",
-	     tag, h->nel, slots_used, h->size, max_chain_len);
-}
libselinux/src/hashtab.h deleted +0 -117

117 lines not on this page · raw message

Signature · 1 line
2.43.0
#13 lore raw

RFC PATCH v2 8/9 libselinux: add selabel_file(5) fuzzer

In reply to Christian Göttsche

Add two fuzzers reading and performing lookup on selabel_file(5) databases. One fuzzer takes input in form of a textual fcontext definition, the other one takes compiled fcontexts definitions. The lookup key and whether to lookup any or a specific file type is also part of the generated input.

CC Evgeny Vereshchagin <[email protected]>

---
v2: add patch

---
 libselinux/fuzz/input                         |   0
 .../fuzz/selabel_file_compiled-fuzzer.c       | 281 ++++++++++++++++++
 libselinux/fuzz/selabel_file_text-fuzzer.c    | 225 ++++++++++++++
 libselinux/src/label_file.c                   |  38 ++-
 libselinux/src/label_file.h                   |  17 ++
 scripts/oss-fuzz.sh                           |  25 ++
 6 files changed, 566 insertions(+), 20 deletions(-)
 create mode 100644 libselinux/fuzz/input
 create mode 100644 libselinux/fuzz/selabel_file_compiled-fuzzer.c
 create mode 100644 libselinux/fuzz/selabel_file_text-fuzzer.c
libselinux/fuzz/input new file +0 -0
new file mode 100644
index 00000000..e69de29b
libselinux/fuzz/selabel_file_compiled-fuzzer.c new file +281 -0

281 lines not on this page · raw message

libselinux/fuzz/selabel_file_text-fuzzer.c new file +225 -0

225 lines not on this page · raw message

libselinux/src/label_file.c +18 -20
index 096e5dab..ac349bc5 100644
@@ -27,6 +27,13 @@
 #include "label_file.h"
 
 
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+# define FUZZ_EXTERN
+#else
+# define FUZZ_EXTERN static
+#endif  /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
+
+
 /*
  * Warn about duplicate specifications.
  */
@@ -114,8 +121,8 @@ static int nodups_spec_node(const struct spec_node *node, const char *path)
 	return rc;
 }
 
-static int process_text_file(FILE *fp, const char *prefix,
-			     struct selabel_handle *rec, const char *path)
+FUZZ_EXTERN int process_text_file(FILE *fp, const char *prefix,
+				  struct selabel_handle *rec, const char *path)
 {
 	int rc;
 	size_t line_len;
@@ -721,8 +728,8 @@ static int load_mmap_spec_node(struct mmap_area *mmap_area, const char *path, bo
 	return 0;
 }
 
-static int load_mmap(FILE *fp, const size_t len, struct selabel_handle *rec,
-		     const char *path)
+FUZZ_EXTERN int load_mmap(FILE *fp, const size_t len, struct selabel_handle *rec,
+			  const char *path)
 {
 	struct saved_data *data = rec->data;
 	struct spec_node *root = NULL;
@@ -1443,16 +1450,7 @@ static uint32_t search_literal_spec(const struct literal_spec *array, uint32_t s
 	return (uint32_t)-1;
 }
 
-struct lookup_result {
-	const char *regex_str;
-	struct selabel_lookup_rec *lr;
-	uint16_t prefix_len;
-	uint8_t file_kind;
-	bool has_meta_chars;
-	struct lookup_result *next;
-};
-
-static void free_lookup_result(struct lookup_result *result)
+FUZZ_EXTERN void free_lookup_result(struct lookup_result *result)
 {
 	struct lookup_result *tmp;
 
@@ -1684,11 +1682,11 @@ static uint8_t mode_to_file_kind(int type) {
 // Finds all the matches of |key| in the given context. Returns the result in
 // the allocated array and updates the match count. If match_count is NULL,
 // stops early once the 1st match is found.
-static struct lookup_result *lookup_all(struct selabel_handle *rec,
-					const char *key,
-					int type,
-					bool partial,
-					bool find_all)
+FUZZ_EXTERN struct lookup_result *lookup_all(struct selabel_handle *rec,
+				 const char *key,
+				 int type,
+				 bool partial,
+				 bool find_all)
 {
 	struct saved_data *data = (struct saved_data *)rec->data;
 	struct lookup_result *result = NULL;
@@ -2255,7 +2253,7 @@ static enum selabel_cmp_result spec_node_cmp(const struct spec_node *node1, cons
 	return result;
 }
 
-static enum selabel_cmp_result cmp(const struct selabel_handle *h1, const struct selabel_handle *h2)
+FUZZ_EXTERN enum selabel_cmp_result cmp(const struct selabel_handle *h1, const struct selabel_handle *h2)
 {
 	const struct saved_data *data1, *data2;
 
libselinux/src/label_file.h +17 -0
index 2dc772eb..529a1bd2 100644
@@ -50,6 +50,23 @@
 #define LABEL_FILE_KIND_LNK		6
 #define LABEL_FILE_KIND_REG		7
 
+/* Only exported for fuzzing */
+struct lookup_result {
+	const char *regex_str;
+	struct selabel_lookup_rec *lr;
+	uint16_t prefix_len;
+	uint8_t file_kind;
+	bool has_meta_chars;
+	struct lookup_result *next;
+};
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+extern int load_mmap(FILE *fp, const size_t len, struct selabel_handle *rec, const char *path);
+extern int process_text_file(FILE *fp, const char *prefix, struct selabel_handle *rec, const char *path);
+extern void free_lookup_result(struct lookup_result *result);
+extern struct lookup_result *lookup_all(struct selabel_handle *rec, const char *key, int type, bool partial, bool find_all);
+extern enum selabel_cmp_result cmp(const struct selabel_handle *h1, const struct selabel_handle *h2);
+#endif  /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
+
 struct selabel_sub {
 	char *src;
 	unsigned int slen;
scripts/oss-fuzz.sh +25 -0
index 72d275e8..e51efe74 100755
@@ -44,10 +44,13 @@ export LIB_FUZZING_ENGINE=${LIB_FUZZING_ENGINE:--fsanitize=fuzzer}
 
 rm -rf "$DESTDIR"
 make -C libsepol clean
+make -C libselinux clean
 # LIBSO and LIBMAP shouldn't be expanded here because their values are unknown until Makefile
 # has been read by make
 # shellcheck disable=SC2016
 make -C libsepol V=1 LD_SONAME_FLAGS='-soname,$(LIBSO),--version-script=$(LIBMAP)' -j"$(nproc)" install
+# shellcheck disable=SC2016
+make -C libselinux V=1 LD_SONAME_FLAGS='-soname,$(LIBSO),--version-script=libselinux.map' -j"$(nproc)" install
 
 ## secilc fuzzer ##
 
@@ -70,3 +73,25 @@ $CC $CFLAGS -c -o binpolicy-fuzzer.o libsepol/fuzz/binpolicy-fuzzer.c
 $CXX $CXXFLAGS $LIB_FUZZING_ENGINE binpolicy-fuzzer.o "$DESTDIR/usr/lib/libsepol.a" -o "$OUT/binpolicy-fuzzer"
 
 zip -j "$OUT/binpolicy-fuzzer_seed_corpus.zip" libsepol/fuzz/policy.bin
+
+## selabel-file text fcontext based fuzzer ##
+
+# CFLAGS, CXXFLAGS and LIB_FUZZING_ENGINE have to be split to be accepted by
+# the compiler/linker so they shouldn't be quoted
+# shellcheck disable=SC2086
+$CC $CFLAGS -DUSE_PCRE2 -DPCRE2_CODE_UNIT_WIDTH=8 -c -o selabel_file_text-fuzzer.o libselinux/fuzz/selabel_file_text-fuzzer.c
+# shellcheck disable=SC2086
+$CXX $CXXFLAGS $LIB_FUZZING_ENGINE selabel_file_text-fuzzer.o "$DESTDIR/usr/lib/libselinux.a" -lpcre2-8 -o "$OUT/selabel_file_text-fuzzer"
+
+zip -j "$OUT/selabel_file_text-fuzzer_seed_corpus.zip" libselinux/fuzz/input
+
+## selabel-file compiled fcontext based fuzzer ##
+
+# CFLAGS, CXXFLAGS and LIB_FUZZING_ENGINE have to be split to be accepted by
+# the compiler/linker so they shouldn't be quoted
+# shellcheck disable=SC2086
+$CC $CFLAGS -DUSE_PCRE2 -DPCRE2_CODE_UNIT_WIDTH=8 -c -o selabel_file_compiled-fuzzer.o libselinux/fuzz/selabel_file_compiled-fuzzer.c
+# shellcheck disable=SC2086
+$CXX $CXXFLAGS $LIB_FUZZING_ENGINE selabel_file_compiled-fuzzer.o "$DESTDIR/usr/lib/libselinux.a" -lpcre2-8 -o "$OUT/selabel_file_compiled-fuzzer"
+
+zip -j "$OUT/selabel_file_compiled-fuzzer_seed_corpus.zip" libselinux/fuzz/input
Signature · 1 line
2.43.0
#14 lore raw

RFC PATCH v2 9/9 libselinux: support parallel selabel_lookup(3)

In reply to Christian Göttsche

Support the parallel usage of the translated label lookup via selabel_lookup(3) in multi threaded applications by locking the step of computing the translated context and the validation state.

A potential use case might can usage from a Rust application via FFI.

---
v2: add patch

---
 libselinux/src/label.c          | 56 +++++++++++++++++++++++++++------
 libselinux/src/label_db.c       |  2 ++
 libselinux/src/label_file.c     |  4 ++-
 libselinux/src/label_file.h     |  4 +++
 libselinux/src/label_internal.h |  1 +
 libselinux/src/label_media.c    |  1 +
 libselinux/src/label_x.c        |  1 +
 7 files changed, 59 insertions(+), 10 deletions(-)
libselinux/src/label.c +47 -9
index d2e703ef..10e78a9b 100644
@@ -124,18 +124,32 @@ static inline int selabel_is_validate_set(const struct selinux_opt *opts,
 
 int selabel_validate(struct selabel_lookup_rec *contexts)
 {
-	int rc = 0;
+	bool validated;
+	int rc;
 
-	if (contexts->validated)
-		goto out;
+	validated = __atomic_load_n(&contexts->validated, __ATOMIC_ACQUIRE);
+	if (validated)
+		return 0;
+
+	__pthread_mutex_lock(&contexts->lock);
+
+	/* Check if another thread validated the context while we waited on the mutex */
+	validated = __atomic_load_n(&contexts->validated, __ATOMIC_ACQUIRE);
+	if (validated) {
+		__pthread_mutex_unlock(&contexts->lock);
+		return 0;
+	}
 
 	rc = selinux_validate(&contexts->ctx_raw);
+	if (rc == 0)
+		__atomic_store_n(&contexts->validated, true, __ATOMIC_RELEASE);
+
+	__pthread_mutex_unlock(&contexts->lock);
+
 	if (rc < 0)
-		goto out;
+		return -1;
 
-	contexts->validated = true;
-out:
-	return rc;
+	return 0;
 }
 
 /* Public API helpers */
@@ -143,11 +157,35 @@ static int selabel_fini(const struct selabel_handle *rec,
 			    struct selabel_lookup_rec *lr,
 			    bool translating)
 {
+	char *ctx_trans;
+	int rc;
+
 	if (compat_validate(rec, lr, rec->spec_file, lr->lineno))
 		return -1;
 
-	if (translating && !lr->ctx_trans &&
-	    selinux_raw_to_trans_context(lr->ctx_raw, &lr->ctx_trans))
+	if (!translating)
+		return 0;
+
+	ctx_trans = __atomic_load_n(&lr->ctx_trans, __ATOMIC_ACQUIRE);
+	if (ctx_trans)
+		return 0;
+
+	__pthread_mutex_lock(&lr->lock);
+
+	/* Check if another thread translated the context while we waited on the mutex */
+	ctx_trans = __atomic_load_n(&lr->ctx_trans, __ATOMIC_ACQUIRE);
+	if (ctx_trans) {
+		__pthread_mutex_unlock(&lr->lock);
+		return 0;
+	}
+
+	rc = selinux_raw_to_trans_context(lr->ctx_raw, &ctx_trans);
+	if (rc == 0)
+		__atomic_store_n(&lr->ctx_trans, ctx_trans, __ATOMIC_RELEASE);
+
+	__pthread_mutex_unlock(&lr->lock);
+
+	if (rc)
 		return -1;
 
 	return 0;
libselinux/src/label_db.c +2 -0
index 2ff10b2f..ed178d34 100644
@@ -183,6 +183,7 @@ db_close(struct selabel_handle *rec)
 		free(spec->key);
 		free(spec->lr.ctx_raw);
 		free(spec->lr.ctx_trans);
+		__pthread_mutex_destroy(&spec->lr.lock);
 	}
 	free(catalog);
 }
@@ -355,6 +356,7 @@ out_error:
 		free(spec->key);
 		free(spec->lr.ctx_raw);
 		free(spec->lr.ctx_trans);
+		__pthread_mutex_destroy(&spec->lr.lock);
 	}
 	free(catalog);
 	fclose(filp);
libselinux/src/label_file.c +3 -1
index ac349bc5..3520fe2d 100644
@@ -175,6 +175,7 @@ static int merge_mmap_spec_nodes(struct spec_node *restrict dest, struct spec_no
 			for (uint32_t i = 0; i < source->literal_specs_num; i++) {
 				source->literal_specs[i].lr.ctx_raw = NULL;
 				source->literal_specs[i].lr.ctx_trans = NULL;
+				__pthread_mutex_destroy(&source->literal_specs[i].lr.lock);
 			}
 
 		} else {
@@ -214,9 +215,10 @@ static int merge_mmap_spec_nodes(struct spec_node *restrict dest, struct spec_no
 			for (uint32_t i = 0; i < source->regex_specs_num; i++) {
 				source->regex_specs[i].lr.ctx_raw = NULL;
 				source->regex_specs[i].lr.ctx_trans = NULL;
+				__pthread_mutex_destroy(&source->regex_specs[i].lr.lock);
 				source->regex_specs[i].regex = NULL;
 				source->regex_specs[i].regex_compiled = false;
-				__pthread_mutex_init(&source->regex_specs[i].regex_lock, NULL);
+				__pthread_mutex_destroy(&source->regex_specs[i].regex_lock);
 			}
 		} else {
 			assert(dest->regex_specs == NULL);
libselinux/src/label_file.h +4 -0
index 529a1bd2..de8190f9 100644
@@ -661,6 +661,7 @@ static int insert_spec(const struct selabel_handle *rec, struct saved_data *data
 			.lr.ctx_trans = NULL,
 			.lr.lineno = lineno,
 			.lr.validated = false,
+			.lr.lock = PTHREAD_MUTEX_INITIALIZER,
 		};
 
 		data->num_specs++;
@@ -794,6 +795,7 @@ static int insert_spec(const struct selabel_handle *rec, struct saved_data *data
 			.lr.ctx_trans = NULL,
 			.lr.lineno = lineno,
 			.lr.validated = false,
+			.lr.lock = PTHREAD_MUTEX_INITIALIZER,
 		};
 
 		data->num_specs++;
@@ -818,6 +820,7 @@ static inline void free_spec_node(struct spec_node *node)
 
 		free(lspec->lr.ctx_raw);
 		free(lspec->lr.ctx_trans);
+		__pthread_mutex_destroy(&lspec->lr.lock);
 
 		if (lspec->from_mmap)
 			continue;
@@ -832,6 +835,7 @@ static inline void free_spec_node(struct spec_node *node)
 
 		free(rspec->lr.ctx_raw);
 		free(rspec->lr.ctx_trans);
+		__pthread_mutex_destroy(&rspec->lr.lock);
 		regex_data_free(rspec->regex);
 		__pthread_mutex_destroy(&rspec->regex_lock);
 
libselinux/src/label_internal.h +1 -0
index 854f92fa..743dbf94 100644
@@ -71,6 +71,7 @@ extern void digest_gen_hash(struct selabel_digest *digest);
 struct selabel_lookup_rec {
 	char * ctx_raw;
 	char * ctx_trans;
+	pthread_mutex_t lock;	/* lock for validation and translation */
 	unsigned int lineno;
 	bool validated;
 };
libselinux/src/label_media.c +1 -0
index fad5ea6d..7bd85804 100644
@@ -172,6 +172,7 @@ static void close(struct selabel_handle *rec)
 		free(spec->key);
 		free(spec->lr.ctx_raw);
 		free(spec->lr.ctx_trans);
+		__pthread_mutex_destroy(&spec->lr.lock);
 	}
 
 	if (spec_arr)
libselinux/src/label_x.c +1 -0
index bf569ca5..49c33b3b 100644
@@ -199,6 +199,7 @@ static void close(struct selabel_handle *rec)
 		free(spec->key);
 		free(spec->lr.ctx_raw);
 		free(spec->lr.ctx_trans);
+		__pthread_mutex_destroy(&spec->lr.lock);
 	}
 
 	if (spec_arr)
Signature · 1 line
2.43.0