Skip to contentSkip to conversation

libsepol: validate permission identifier length

RFC PATCH In review Christian Göttsche 3 participants 4 messages Dec 7, 2023 – Dec 8, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 libsepol: validate permission identifier length

Limit the maximum length of permission identifiers. Otherwise formatting an access vector might fail in the common used helper sepol_av_to_string().

The current longest permission within the Reference Policy is x_application_data { paste_after_confirm } with a length of 19.

Reported-by oss-fuzz (issue 64832)

---
The oss-fuzz issue got closed by the latest fuzzer updates (due to unrelated changes that invalid the current reproducer), but the issue is still valid.

---
 libsepol/src/policydb_validate.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
libsepol/src/policydb_validate.c +20 -0
index bd8e9f8f..72681120 100644
@@ -366,12 +366,30 @@ bad:
 	return -1;
 }
 
+static int validate_permission_wrapper(hashtab_key_t k, __attribute__((unused)) hashtab_datum_t d, void *args)
+{
+	sepol_handle_t *handle = args;
+	const char *name = k;
+	size_t len = strlen(name);
+
+	if (len == 0 || len >= 32)
+		goto bad;
+
+	return 0;
+
+bad:
+	ERR(handle, "Invalid permission");
+	return -1;
+}
+
 static int validate_common_datum(sepol_handle_t *handle, const common_datum_t *common, validate_t flavors[])
 {
 	if (validate_value(common->s.value, &flavors[SYM_COMMONS]))
 		goto bad;
 	if (common->permissions.table->nel == 0 || common->permissions.nprim > PERM_SYMTAB_SIZE)
 		goto bad;
+	if (hashtab_map(common->permissions.table, validate_permission_wrapper, handle))
+		goto bad;
 
 	return 0;
 
@@ -395,6 +413,8 @@ static int validate_class_datum(sepol_handle_t *handle, const class_datum_t *cla
 		goto bad;
 	if (class->permissions.nprim > PERM_SYMTAB_SIZE)
 		goto bad;
+	if (hashtab_map(class->permissions.table, validate_permission_wrapper, handle))
+		goto bad;
 	if (validate_constraint_nodes(handle, class->permissions.nprim, class->constraints, flavors))
 		goto bad;
 	if (validate_constraint_nodes(handle, 0, class->validatetrans, flavors))
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 7 lines · Christian Göttsche

Limit the maximum length of permission identifiers. Otherwise formatting an access vector might fail in the common used helper sepol_av_to_string().

The current longest permission within the Reference Policy is x_application_data { paste_after_confirm } with a length of 19.

Android has longer permission names. I'd rather just fix sepol_av_to_string() than impose some arbitrary limit here.

Quoted 59 lines
Reported-by oss-fuzz (issue 64832)

---
The oss-fuzz issue got closed by the latest fuzzer updates (due to unrelated changes that invalid the current reproducer), but the issue is still valid.

---
 libsepol/src/policydb_validate.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
libsepol/src/policydb_validate.c +20 -1
index bd8e9f8f..72681120 100644
@@ -366,12 +366,30 @@ bad:
        return -1;
 }
 
+static int validate_permission_wrapper(hashtab_key_t k, __attribute__((unused)) hashtab_datum_t d, void *args)
+{
+       sepol_handle_t *handle = args;
+       const char *name = k;
+       size_t len = strlen(name);
+
+       if (len == 0 || len >= 32)
+               goto bad;
+
+       return 0;
+
+bad:
+       ERR(handle, "Invalid permission");
+       return -1;
+}
+
 static int validate_common_datum(sepol_handle_t *handle, const common_datum_t *common, validate_t flavors[])
 {
        if (validate_value(common->s.value, &flavors[SYM_COMMONS]))
                goto bad;
        if (common->permissions.table->nel == 0 || common->permissions.nprim > PERM_SYMTAB_SIZE)
                goto bad;
+       if (hashtab_map(common->permissions.table, validate_permission_wrapper, handle))
+               goto bad;
 
        return 0;
 
@@ -395,6 +413,8 @@ static int validate_class_datum(sepol_handle_t *handle, const class_datum_t *cla
                goto bad;
        if (class->permissions.nprim > PERM_SYMTAB_SIZE)
                goto bad;
+       if (hashtab_map(class->permissions.table, validate_permission_wrapper, handle))
+               goto bad;
        if (validate_constraint_nodes(handle, class->permissions.nprim, class->constraints, flavors))
                goto bad;
        if (validate_constraint_nodes(handle, 0, class->validatetrans, flavors))
--

2.43.0

maintainer
#2 lore raw
Quoted 8 lines · Stephen Smalley
Quoted 5 lines · Christian Göttsche

Limit the maximum length of permission identifiers. Otherwise formatting an access vector might fail in the common used helper sepol_av_to_string().

Quoted 5 lines
Quoted 2 lines

The current longest permission within the Reference Policy is x_application_data { paste_after_confirm } with a length of 19.

Android has longer permission names. I'd rather just fix sepol_av_to_string() than impose some arbitrary limit here.

Looking at AOSP master, it appears that the longest permission is 32-bytes but since we haven't imposed such a limit in the past, we have no way of knowing what any particular version of refpolicy, AOSP policy, or downstream policies might have used.

#3 lore raw
In reply to Stephen Smalley
Quoted 8 lines · Stephen Smalley
Quoted 5 lines · Christian Göttsche

Limit the maximum length of permission identifiers. Otherwise formatting an access vector might fail in the common used helper sepol_av_to_string().

Quoted 6 lines
Quoted 2 lines

The current longest permission within the Reference Policy is x_application_data { paste_after_confirm } with a length of 19.

Android has longer permission names. I'd rather just fix sepol_av_to_string() than impose some arbitrary limit here.

I agree.

Christian, something like your recent patch to avoid a fixed size format buffer for xperms would be better.

Thanks for all your work.
Jim

Quoted 60 lines
Quoted 59 lines
Reported-by oss-fuzz (issue 64832)

---
The oss-fuzz issue got closed by the latest fuzzer updates (due to unrelated changes that invalid the current reproducer), but the issue is still valid.

---
 libsepol/src/policydb_validate.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
libsepol/src/policydb_validate.c +20 -1
index bd8e9f8f..72681120 100644
@@ -366,12 +366,30 @@ bad:
        return -1;
 }
 
+static int validate_permission_wrapper(hashtab_key_t k, __attribute__((unused)) hashtab_datum_t d, void *args)
+{
+       sepol_handle_t *handle = args;
+       const char *name = k;
+       size_t len = strlen(name);
+
+       if (len == 0 || len >= 32)
+               goto bad;
+
+       return 0;
+
+bad:
+       ERR(handle, "Invalid permission");
+       return -1;
+}
+
 static int validate_common_datum(sepol_handle_t *handle, const common_datum_t *common, validate_t flavors[])
 {
        if (validate_value(common->s.value, &flavors[SYM_COMMONS]))
                goto bad;
        if (common->permissions.table->nel == 0 || common->permissions.nprim > PERM_SYMTAB_SIZE)
                goto bad;
+       if (hashtab_map(common->permissions.table, validate_permission_wrapper, handle))
+               goto bad;
 
        return 0;
 
@@ -395,6 +413,8 @@ static int validate_class_datum(sepol_handle_t *handle, const class_datum_t *cla
                goto bad;
        if (class->permissions.nprim > PERM_SYMTAB_SIZE)
                goto bad;
+       if (hashtab_map(class->permissions.table, validate_permission_wrapper, handle))
+               goto bad;
        if (validate_constraint_nodes(handle, class->permissions.nprim, class->constraints, flavors))
                goto bad;
        if (validate_constraint_nodes(handle, 0, class->validatetrans, flavors))
--

2.43.0