Skip to contentSkip to conversation

policycoreutils: setfiles: do not restrict checks against a binary policy

PATCH Applied Antoine Tenart 3 participants 3 messages Jul 7, 2020 – Jul 10, 2020 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 policycoreutils: setfiles: do not restrict checks against a binary policy

The -c option allows to check the validity of contexts against a specified binary policy. Its use is restricted: no pathname can be used when a binary policy is given to setfiles. It's not clear if this is intentional as the built-in help and the man page are not stating the same thing about this (the man page document -c as a normal option, while the built-in help shows it is restricted).

When generating full system images later used with SELinux in enforcing mode, the extended attributed of files have to be set by the build machine. The issue is setfiles always checks the contexts against a policy (ctx_validate = 1) and using an external binary policy is not currently possible when using a pathname. This ends up in setfiles failing early as the contexts of the target image are not always compatible with the ones of the build machine.

This patch reworks a check on optind only made when -c is used, that enforced the use of a single argument to allow 1+ arguments, allowing to use setfiles with an external binary policy and pathnames. The following command is then allowed, as already documented in the man page:

  $ setfiles -m -r target/ -c policy.32 file_contexts target/

---

Since v1:
- Updated the number of 'name' arguments to reflect the format string change.

 policycoreutils/setfiles/setfiles.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
policycoreutils/setfiles/setfiles.c +5 -6
index ed67b5f12245..422c3767b845 100644
@@ -48,11 +48,10 @@ static __attribute__((__noreturn__)) void usage(const char *const name)
 			name, name);
 	} else {
 		fprintf(stderr,
-			"usage:  %s [-diIDlmnpqvEFW] [-e excludedir] [-r alt_root_path] spec_file pathname...\n"
-			"usage:  %s [-diIDlmnpqvEFW] [-e excludedir] [-r alt_root_path] spec_file -f filename\n"
-			"usage:  %s -s [-diIDlmnpqvFW] spec_file\n"
-			"usage:  %s -c policyfile spec_file\n",
-			name, name, name, name);
+			"usage:  %s [-diIDlmnpqvEFW] [-e excludedir] [-r alt_root_path] [-c policyfile] spec_file pathname...\n"
+			"usage:  %s [-diIDlmnpqvEFW] [-e excludedir] [-r alt_root_path] [-c policyfile] spec_file -f filename\n"
+			"usage:  %s -s [-diIDlmnpqvFW] spec_file\n",
+			name, name, name);
 	}
 	exit(-1);
 }
@@ -409,7 +408,7 @@ int main(int argc, char **argv)
 
 	if (!iamrestorecon) {
 		if (policyfile) {
-			if (optind != (argc - 1))
+			if (optind > (argc - 1))
 				usage(argv[0]);
 		} else if (use_input_file) {
 			if (optind != (argc - 1)) {
Signature · 1 line
2.26.2
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 16 lines · Antoine Tenart

The -c option allows to check the validity of contexts against a specified binary policy. Its use is restricted: no pathname can be used when a binary policy is given to setfiles. It's not clear if this is intentional as the built-in help and the man page are not stating the same thing about this (the man page document -c as a normal option, while the built-in help shows it is restricted).

When generating full system images later used with SELinux in enforcing mode, the extended attributed of files have to be set by the build machine. The issue is setfiles always checks the contexts against a policy (ctx_validate = 1) and using an external binary policy is not currently possible when using a pathname. This ends up in setfiles failing early as the contexts of the target image are not always compatible with the ones of the build machine.

Quoted 8 lines

This patch reworks a check on optind only made when -c is used, that enforced the use of a single argument to allow 1+ arguments, allowing to use setfiles with an external binary policy and pathnames. The following command is then allowed, as already documented in the man page:

  $ setfiles -m -r target/ -c policy.32 file_contexts target/
#2 lore raw
Quoted 23 lines · Stephen Smalley
Quoted 21 lines · Antoine Tenart

The -c option allows to check the validity of contexts against a specified binary policy. Its use is restricted: no pathname can be used when a binary policy is given to setfiles. It's not clear if this is intentional as the built-in help and the man page are not stating the same thing about this (the man page document -c as a normal option, while the built-in help shows it is restricted).

When generating full system images later used with SELinux in enforcing mode, the extended attributed of files have to be set by the build machine. The issue is setfiles always checks the contexts against a policy (ctx_validate = 1) and using an external binary policy is not currently possible when using a pathname. This ends up in setfiles failing early as the contexts of the target image are not always compatible with the ones of the build machine.

This patch reworks a check on optind only made when -c is used, that enforced the use of a single argument to allow 1+ arguments, allowing to use setfiles with an external binary policy and pathnames. The following command is then allowed, as already documented in the man page:

Quoted 6 lines
Quoted 3 lines
  $ setfiles -m -r target/ -c policy.32 file_contexts target/

Applied. Thanks!