All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Christian Göttsche" <cgoettsche@seltendoof.de>
To: selinux@vger.kernel.org
Cc: "Christian Göttsche" <cgzones@googlemail.com>
Subject: [PATCH 3/6] libsepol: validate access vector permissions
Date: Tue,  2 Apr 2024 17:29:22 +0200	[thread overview]
Message-ID: <20240402152925.99781-3-cgoettsche@seltendoof.de> (raw)
In-Reply-To: <20240402152925.99781-1-cgoettsche@seltendoof.de>

From: Christian Göttsche <cgzones@googlemail.com>

Since commit c205b924e280 ("libsepol: Fix buffer overflow when using
sepol_av_to_string()") writing an access vector with no valid permission
results in an error instead of an empty string being written.

Validate that at least one permission of an access vector is valid.
There might be invalid bits set, e.g. by previous versions of
checkpolicy setting all bits for the wildcard (*) permission.

Reported-by: oss-fuzz (issue 67730)
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libsepol/src/policydb_validate.c | 62 ++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)

diff --git a/libsepol/src/policydb_validate.c b/libsepol/src/policydb_validate.c
index e987d8da..c4f8c300 100644
--- a/libsepol/src/policydb_validate.c
+++ b/libsepol/src/policydb_validate.c
@@ -876,6 +876,49 @@ static int validate_xperms(const avtab_extended_perms_t *xperms)
 bad:
 	return -1;
 }
+
+static int perm_match(__attribute__ ((unused)) hashtab_key_t key, hashtab_datum_t datum, void *data)
+{
+	const uint32_t *v = data;
+	const perm_datum_t *perdatum = datum;
+
+	return *v == perdatum->s.value;
+}
+
+static int validate_access_vector(sepol_handle_t *handle, const policydb_t *p, sepol_security_class_t tclass,
+				  sepol_access_vector_t av)
+{
+	const class_datum_t *cladatum = p->class_val_to_struct[tclass - 1];
+	uint32_t i;
+
+	/*
+	 * Check that at least one permission bit is valid.
+	 * Older compilers might set invalid bits for the wildcard permission.
+	 */
+	for (i = 0; i < cladatum->permissions.nprim; i++) {
+		if (av & (UINT32_C(1) << i)) {
+			uint32_t v = i + 1;
+			int rc;
+
+			rc = hashtab_map(cladatum->permissions.table, perm_match, &v);
+			if (rc == 1)
+				goto good;
+
+			if (cladatum->comdatum) {
+				rc = hashtab_map(cladatum->comdatum->permissions.table, perm_match, &v);
+				if (rc == 1)
+					goto good;
+			}
+		}
+	}
+
+	ERR(handle, "Invalid access vector");
+	return -1;
+
+good:
+	return 0;
+}
+
 static int validate_avtab_key_and_datum(avtab_key_t *k, avtab_datum_t *d, void *args)
 {
 	map_arg_t *margs = args;
@@ -883,6 +926,16 @@ static int validate_avtab_key_and_datum(avtab_key_t *k, avtab_datum_t *d, void *
 	if (validate_avtab_key(k, 0, margs->policy, margs->flavors))
 		return -1;
 
+	if (k->specified & AVTAB_AV) {
+		uint32_t data = d->data;
+
+		if ((0xFFF & k->specified) == AVTAB_AUDITDENY)
+			data = ~data;
+
+		if (validate_access_vector(margs->handle, margs->policy, k->target_class, data))
+			return -1;
+	}
+
 	if ((k->specified & AVTAB_TYPE) && validate_simpletype(d->data, margs->policy, margs->flavors))
 		return -1;
 
@@ -915,6 +968,15 @@ static int validate_cond_av_list(sepol_handle_t *handle, const cond_av_list_t *c
 
 			if (validate_avtab_key(key, 1, p, flavors))
 				goto bad;
+			if (key->specified & AVTAB_AV) {
+				uint32_t data = datum->data;
+
+				if ((0xFFF & key->specified) == AVTAB_AUDITDENY)
+					data = ~data;
+
+				if (validate_access_vector(handle, p, key->target_class, data))
+					goto bad;
+			}
 			if ((key->specified & AVTAB_TYPE) && validate_simpletype(datum->data, p, flavors))
 				goto bad;
 		}
-- 
2.43.0


  parent reply	other threads:[~2024-04-02 15:29 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-02 15:29 [PATCH 1/6] checkpolicy: include <ctype.h> for isprint(3) Christian Göttsche
2024-04-02 15:29 ` [PATCH 2/6] checkpolicy/fuzz: override YY_FATAL_ERROR Christian Göttsche
2024-04-02 15:29 ` Christian Göttsche [this message]
2024-04-02 15:29 ` [PATCH 4/6] checkpolicy: drop never read member Christian Göttsche
2024-04-02 15:29 ` [PATCH 5/6] checkpolicy: drop union stack_item_u Christian Göttsche
2024-04-02 15:29 ` [PATCH 6/6] checkpolicy: free complete role_allow_rule on error Christian Göttsche
2024-04-03 19:35 ` [PATCH 1/6] checkpolicy: include <ctype.h> for isprint(3) James Carter
2024-04-04 16:22   ` James Carter

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=20240402152925.99781-3-cgoettsche@seltendoof.de \
    --to=cgoettsche@seltendoof.de \
    --cc=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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.