From: "Christian Göttsche" <cgzones@googlemail.com>
To: selinux@vger.kernel.org
Subject: [RFC PATCH v2 2/9] libselinux/utils: introduce selabel_compare
Date: Wed, 31 Jan 2024 14:08:28 +0100 [thread overview]
Message-ID: <20240131130840.48155-3-cgzones@googlemail.com> (raw)
In-Reply-To: <20240131130840.48155-1-cgzones@googlemail.com>
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.
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
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
diff --git a/libselinux/utils/.gitignore b/libselinux/utils/.gitignore
index b3311360..2e10b14f 100644
--- a/libselinux/utils/.gitignore
+++ b/libselinux/utils/.gitignore
@@ -16,6 +16,7 @@ getseuser
matchpathcon
policyvers
sefcontext_compile
+selabel_compare
selabel_digest
selabel_get_digests_all_partial_matches
selabel_lookup
diff --git a/libselinux/utils/selabel_compare.c b/libselinux/utils/selabel_compare.c
new file mode 100644
index 00000000..9ca6eff1
--- /dev/null
+++ b/libselinux/utils/selabel_compare.c
@@ -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
next prev parent reply other threads:[~2024-01-31 13:08 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-31 13:08 [RFC PATCH v2 0/9] libselinux: rework selabel_file(5) database Christian Göttsche
2024-01-31 13:08 ` [RFC PATCH v2 1/9] policycoreutils: introduce unsetfiles Christian Göttsche
2024-01-31 13:08 ` Christian Göttsche [this message]
2024-03-07 19:50 ` [RFC PATCH v2 2/9] libselinux/utils: introduce selabel_compare James Carter
2024-03-11 17:20 ` Christian Göttsche
2024-03-11 20:49 ` James Carter
2024-01-31 13:08 ` [RFC PATCH v2 3/9] libselinux: use more appropriate types in sidtab Christian Göttsche
2024-01-31 13:08 ` [RFC PATCH v2 4/9] libselinux: add unique id to sidtab entries Christian Göttsche
2024-01-31 13:08 ` [RFC PATCH v2 5/9] libselinux: sidtab updates Christian Göttsche
2024-03-07 20:53 ` James Carter
2024-03-11 16:32 ` Christian Göttsche
2024-01-31 13:08 ` [RFC PATCH v2 6/9] libselinux: rework selabel_file(5) database Christian Göttsche
2024-01-31 13:08 ` [RFC PATCH v2 7/9] libselinux: remove unused hashtab code Christian Göttsche
2024-01-31 13:08 ` [RFC PATCH v2 8/9] libselinux: add selabel_file(5) fuzzer Christian Göttsche
2024-01-31 13:08 ` [RFC PATCH v2 9/9] libselinux: support parallel selabel_lookup(3) Christian Göttsche
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240131130840.48155-3-cgzones@googlemail.com \
--to=cgzones@googlemail.com \
--cc=selinux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox