Skip to contentSkip to conversation

libselinux/utils/selabel_digest: drop unsupported option -d

PATCH In review Christian Göttsche 2 participants 7 messages Mar 13, 2024 – Mar 20, 2024 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 libselinux/utils/selabel_digest: drop unsupported option -d

The command line option -d is not supported, drop from usage message.

---
 libselinux/utils/selabel_digest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libselinux/utils/selabel_digest.c +1 -1
index bf22b472..50f55311 100644
@@ -11,7 +11,7 @@ static size_t digest_len;
 static __attribute__ ((__noreturn__)) void usage(const char *progname)
 {
 	fprintf(stderr,
-		"usage: %s -b backend [-d] [-v] [-B] [-i] [-f file]\n\n"
+		"usage: %s -b backend [-v] [-B] [-i] [-f file]\n\n"
 		"Where:\n\t"
 		"-b  The backend - \"file\", \"media\", \"x\", \"db\" or "
 			"\"prop\"\n\t"
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/5 libselinux/utils/selabel_digest: cleanup

Avoid global variable.
Constify read-only parameters.

---
 libselinux/utils/selabel_digest.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
libselinux/utils/selabel_digest.c +4 -6
index 50f55311..db0d443a 100644
@@ -6,8 +6,6 @@
 #include <selinux/selinux.h>
 #include <selinux/label.h>
 
-static size_t digest_len;
-
 static __attribute__ ((__noreturn__)) void usage(const char *progname)
 {
 	fprintf(stderr,
@@ -25,11 +23,11 @@ static __attribute__ ((__noreturn__)) void usage(const char *progname)
 	exit(1);
 }
 
-static int run_check_digest(char *cmd, char *selabel_digest)
+static int run_check_digest(const char *cmd, const char *selabel_digest, size_t digest_len)
 {
 	FILE *fp;
 	char files_digest[128];
-	char *files_ptr;
+	const char *files_ptr;
 	int rc = 0;
 
 	fp = popen(cmd, "r");
@@ -64,7 +62,7 @@ int main(int argc, char **argv)
 	char *baseonly = NULL, *file = NULL, *digest = (char *)1;
 	char **specfiles = NULL;
 	unsigned char *sha1_digest = NULL;
-	size_t i, num_specfiles;
+	size_t digest_len, i, num_specfiles;
 
 	char cmd_buf[4096];
 	char *cmd_ptr;
@@ -181,7 +179,7 @@ int main(int argc, char **argv)
 		sprintf(cmd_ptr, "| /usr/bin/openssl dgst -sha1 -hex");
 
 		if (validate)
-			rc = run_check_digest(cmd_buf, sha1_buf);
+			rc = run_check_digest(cmd_buf, sha1_buf, digest_len);
 	}
 
 	free(sha1_buf);
Signature · 1 line
2.43.0
#2 lore raw

PATCH 3/5 libselinux/utils/selabel_digest: avoid buffer overflow

In reply to Christian Göttsche

In case the specfiles have very long paths or there are too many abort instead of writing past the stack buffer.

---
 libselinux/utils/selabel_digest.c | 45 ++++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 9 deletions(-)
libselinux/utils/selabel_digest.c +36 -9
index db0d443a..64051070 100644
@@ -66,7 +66,7 @@ int main(int argc, char **argv)
 
 	char cmd_buf[4096];
 	char *cmd_ptr;
-	char *sha1_buf;
+	char *sha1_buf = NULL;
 
 	struct selabel_handle *hnd;
 	struct selinux_opt selabel_option[] = {
@@ -167,23 +167,50 @@ int main(int argc, char **argv)
 	printf("calculated using the following specfile(s):\n");
 
 	if (specfiles) {
-		cmd_ptr = &cmd_buf[0];
-		sprintf(cmd_ptr, "/usr/bin/cat ");
-		cmd_ptr = &cmd_buf[0] + strlen(cmd_buf);
+		size_t cmd_rem = sizeof(cmd_buf);
+		int ret;
+
+		if (validate) {
+			cmd_ptr = &cmd_buf[0];
+			ret = snprintf(cmd_ptr, cmd_rem, "/usr/bin/cat ");
+			if (ret < 0 || (size_t)ret >= cmd_rem) {
+				fprintf(stderr, "Could not format validate command\n");
+				rc = -1;
+				goto err;
+			}
+			cmd_ptr += ret;
+			cmd_rem -= ret;
+		}
 
 		for (i = 0; i < num_specfiles; i++) {
-			sprintf(cmd_ptr, "%s ", specfiles[i]);
-			cmd_ptr += strlen(specfiles[i]) + 1;
+			if (validate) {
+				ret = snprintf(cmd_ptr, cmd_rem, "%s ", specfiles[i]);
+				if (ret < 0 || (size_t)ret >= cmd_rem) {
+					fprintf(stderr, "Could not format validate command\n");
+					rc = -1;
+					goto err;
+				}
+				cmd_ptr += ret;
+				cmd_rem -= ret;
+			}
+
 			printf("%s\n", specfiles[i]);
 		}
-		sprintf(cmd_ptr, "| /usr/bin/openssl dgst -sha1 -hex");
 
-		if (validate)
+		if (validate) {
+			ret = snprintf(cmd_ptr, cmd_rem, "| /usr/bin/openssl dgst -sha1 -hex");
+			if (ret < 0 || (size_t)ret >= cmd_rem) {
+				fprintf(stderr, "Could not format validate command\n");
+				rc = -1;
+				goto err;
+			}
+
 			rc = run_check_digest(cmd_buf, sha1_buf, digest_len);
+		}
 	}
 
-	free(sha1_buf);
 err:
+	free(sha1_buf);
 	selabel_close(hnd);
 	return rc;
 }
Signature · 1 line
2.43.0
#3 lore raw

PATCH 4/5 libselinux: free data on selabel open failure

In reply to Christian Göttsche

In case the init function for a selabel backend fails, free the possible already allocated data:

    Direct leak of 16 byte(s) in 1 object(s) allocated from:
        #0 0x5e7e2bf001e3 in malloc (/tmp/destdir/usr/sbin/selabel_digest+0xc71e3)
        #1 0x7233764baa65 in selabel_media_init /home/christian/Coding/workspaces/selinux/libselinux/src/label_media.c:226:30
        #2 0x7233764ac1fe in selabel_open /home/christian/Coding/workspaces/selinux/libselinux/src/label.c:227:6
        #3 0x5e7e2bf3ebfc in main /home/christian/Coding/workspaces/selinux/libselinux/utils/selabel_digest.c:125:8
        #4 0x7233761856c9 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16

    SUMMARY: AddressSanitizer: 16 byte(s) leaked in 1 allocation(s).
---
 libselinux/src/label.c                  | 5 +----
 libselinux/src/label_backends_android.c | 3 +++
 libselinux/src/label_db.c               | 3 +++
 libselinux/src/label_file.c             | 3 +++
 libselinux/src/label_media.c            | 3 +++
 libselinux/src/label_x.c                | 3 +++
 6 files changed, 16 insertions(+), 4 deletions(-)
libselinux/src/label.c +1 -4
index d2e703ef..06d743ec 100644
@@ -225,10 +225,7 @@ struct selabel_handle *selabel_open(unsigned int backend,
 	rec->digest = selabel_is_digest_set(opts, nopts);
 
 	if ((*initfuncs[backend])(rec, opts, nopts)) {
-		if (rec->digest)
-			selabel_digest_fini(rec->digest);
-		free(rec->spec_file);
-		free(rec);
+		selabel_close(rec);
 		rec = NULL;
 	}
 
libselinux/src/label_backends_android.c +3 -0
index 33a17236..49a87686 100644
@@ -246,6 +246,9 @@ static void closef(struct selabel_handle *rec)
 	struct spec *spec;
 	unsigned int i;
 
+	if (!data)
+		return;
+
 	for (i = 0; i < data->nspec; i++) {
 		spec = &data->spec_arr[i];
 		free(spec->property_key);
libselinux/src/label_db.c +3 -0
index 2ff10b2f..40d5fc4a 100644
@@ -178,6 +178,9 @@ db_close(struct selabel_handle *rec)
 	spec_t	       *spec;
 	unsigned int	i;
 
+	if (!catalog)
+		return;
+
 	for (i = 0; i < catalog->nspec; i++) {
 		spec = &catalog->specs[i];
 		free(spec->key);
libselinux/src/label_file.c +3 -0
index 3b2bda97..2732972e 100644
@@ -904,6 +904,9 @@ static void closef(struct selabel_handle *rec)
 	struct stem *stem;
 	unsigned int i;
 
+	if (!data)
+		return;
+
 	selabel_subs_fini(data->subs);
 	selabel_subs_fini(data->dist_subs);
 
libselinux/src/label_media.c +3 -0
index fad5ea6d..94a58062 100644
@@ -167,6 +167,9 @@ static void close(struct selabel_handle *rec)
 	struct spec *spec, *spec_arr = data->spec_arr;
 	unsigned int i;
 
+	if (!data)
+		return;
+
 	for (i = 0; i < data->nspec; i++) {
 		spec = &spec_arr[i];
 		free(spec->key);
libselinux/src/label_x.c +3 -0
index bf569ca5..f994eefa 100644
@@ -194,6 +194,9 @@ static void close(struct selabel_handle *rec)
 	struct spec *spec, *spec_arr = data->spec_arr;
 	unsigned int i;
 
+	if (!data)
+		return;
+
 	for (i = 0; i < data->nspec; i++) {
 		spec = &spec_arr[i];
 		free(spec->key);
Signature · 1 line
2.43.0
#4 lore raw

PATCH 5/5 libselinux/utils/selabel_digest: pass BASEONLY only for file backend

In reply to Christian Göttsche

Since commit 65c8fd45 ("libselinux: fail selabel_open(3) on invalid option") selabel_open(3) rejects options not supported for the respective backend. Pass SELABEL_OPT_BASEONLY only if the file backend is selected.

Fixes 65c8fd45 ("libselinux: fail selabel_open(3) on invalid option")
---
 libselinux/utils/selabel_digest.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
libselinux/utils/selabel_digest.c +5 -5
index 64051070..47aad21f 100644
@@ -71,8 +71,8 @@ int main(int argc, char **argv)
 	struct selabel_handle *hnd;
 	struct selinux_opt selabel_option[] = {
 		{ SELABEL_OPT_PATH, file },
-		{ SELABEL_OPT_BASEONLY, baseonly },
-		{ SELABEL_OPT_DIGEST, digest }
+		{ SELABEL_OPT_DIGEST, digest },
+		{ SELABEL_OPT_BASEONLY, baseonly }
 	};
 
 	if (argc < 3)
@@ -119,10 +119,10 @@ int main(int argc, char **argv)
 	memset(cmd_buf, 0, sizeof(cmd_buf));
 
 	selabel_option[0].value = file;
-	selabel_option[1].value = baseonly;
-	selabel_option[2].value = digest;
+	selabel_option[1].value = digest;
+	selabel_option[2].value = baseonly;
 
-	hnd = selabel_open(backend, selabel_option, 3);
+	hnd = selabel_open(backend, selabel_option, backend == SELABEL_CTX_FILE ? 3 : 2);
 	if (!hnd) {
 		switch (errno) {
 		case EOVERFLOW:
Signature · 1 line
2.43.0
Acked-by on v1 1/5
#5 lore raw
In reply to Christian Göttsche
Quoted 4 lines · Christian Göttsche

The command line option -d is not supported, drop from usage message.

For these 5 patches:

Quoted 21 lines
---
 libselinux/utils/selabel_digest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libselinux/utils/selabel_digest.c +1 -2
index bf22b472..50f55311 100644
@@ -11,7 +11,7 @@ static size_t digest_len;
 static __attribute__ ((__noreturn__)) void usage(const char *progname)
 {
        fprintf(stderr,
-               "usage: %s -b backend [-d] [-v] [-B] [-i] [-f file]\n\n"
+               "usage: %s -b backend [-v] [-B] [-i] [-f file]\n\n"
                "Where:\n\t"
                "-b  The backend - \"file\", \"media\", \"x\", \"db\" or "
                        "\"prop\"\n\t"
--

2.43.0

#6 lore raw
Quoted 11 lines · James Carter
Quoted 4 lines · Christian Göttsche

The command line option -d is not supported, drop from usage message.

For these 5 patches:

These 5 patches have been merged.
Thanks,
Jim

Quoted 21 lines
Quoted 21 lines
---
 libselinux/utils/selabel_digest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libselinux/utils/selabel_digest.c +1 -2
index bf22b472..50f55311 100644
@@ -11,7 +11,7 @@ static size_t digest_len;
 static __attribute__ ((__noreturn__)) void usage(const char *progname)
 {
        fprintf(stderr,
-               "usage: %s -b backend [-d] [-v] [-B] [-i] [-f file]\n\n"
+               "usage: %s -b backend [-v] [-B] [-i] [-f file]\n\n"
                "Where:\n\t"
                "-b  The backend - \"file\", \"media\", \"x\", \"db\" or "
                        "\"prop\"\n\t"
--

2.43.0