Skip to contentSkip to conversation

libsepol: Write "NO_IDENTIFIER" for empty constraint expression

PATCH In review James Carter 2 participants 4 messages Mar 16, 2021 – Mar 18, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libsepol: Write "NO_IDENTIFIER" for empty constraint expression

If a role attribute with no roles associated with it is used in a constraint expression, then the role bitmap will be empty. This is not a problem for the kernel, but does cause problems when converting a kernel policy to policy.conf.

When creating a policy.conf from a kernel policy, if an empty bitmap is encountered, use the string "NO_IDENTIFIER". An error will occur if an attempt is made to compile the resulting policy, but this is better than exiting with an error without creating a policy.conf.

---
 libsepol/src/kernel_to_conf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/kernel_to_conf.c +1 -1
index a22f196d..263f9639 100644
@@ -186,7 +186,7 @@ static char *constraint_expr_to_str(struct policydb *pdb, struct constraint_expr
 					names = ebitmap_to_str(&curr->names, pdb->p_role_val_to_name, 1);
 				}
 				if (!names) {
-					goto exit;
+					names = strdup("NO_IDENTIFIER");
 				}
 				new_val = create_str("%s %s %s", 3, attr1, op, names);
 				free(names);
Signature · 1 line
2.26.2
#1 lore raw

PATCH 2/2 libsepol: Enclose identifier lists in constraint expressions

When writing a policy.conf from a kernel policy, if there are multiple users, roles, or types, then the list needs to be enclosed by "{" and "}".

When writing a constraint expression, check to see if there are multiple identifiers in the names string and enclose the list with "{" and "}" if there are.

---
 libsepol/src/kernel_to_conf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
libsepol/src/kernel_to_conf.c +5 -1
index 263f9639..d385819e 100644
@@ -188,7 +188,11 @@ static char *constraint_expr_to_str(struct policydb *pdb, struct constraint_expr
 				if (!names) {
 					names = strdup("NO_IDENTIFIER");
 				}
-				new_val = create_str("%s %s %s", 3, attr1, op, names);
+				if (strchr(names, ' ')) {
+					new_val = create_str("%s %s { %s }", 3, attr1, op, names);
+				} else {
+					new_val = create_str("%s %s %s", 3, attr1, op, names);
+				}
 				free(names);
 			}
 		} else {
Signature · 1 line
2.26.2
Acked-by on v1 1/2
#2 lore raw
In reply to James Carter
Quoted 6 lines · James Carter

If a role attribute with no roles associated with it is used in a constraint expression, then the role bitmap will be empty. This is not a problem for the kernel, but does cause problems when converting a kernel policy to policy.conf.

Quoted 6 lines

When creating a policy.conf from a kernel policy, if an empty bitmap is encountered, use the string "NO_IDENTIFIER". An error will occur if an attempt is made to compile the resulting policy, but this is better than exiting with an error without creating a policy.conf.

For these 2 patches:

Quoted 20 lines
---
 libsepol/src/kernel_to_conf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/kernel_to_conf.c +1 -2
index a22f196d..263f9639 100644
@@ -186,7 +186,7 @@ static char *constraint_expr_to_str(struct policydb *pdb, struct constraint_expr
                                        names = ebitmap_to_str(&curr->names, pdb->p_role_val_to_name, 1);
                                }
                                if (!names) {
-                                       goto exit;
+                                       names = strdup("NO_IDENTIFIER");
                                }
                                new_val = create_str("%s %s %s", 3, attr1, op, names);
                                free(names);
--

2.26.2

#3 lore raw
Quoted 13 lines · Nicolas Iooss
Quoted 11 lines · James Carter

If a role attribute with no roles associated with it is used in a constraint expression, then the role bitmap will be empty. This is not a problem for the kernel, but does cause problems when converting a kernel policy to policy.conf.

When creating a policy.conf from a kernel policy, if an empty bitmap is encountered, use the string "NO_IDENTIFIER". An error will occur if an attempt is made to compile the resulting policy, but this is better than exiting with an error without creating a policy.conf.

Quoted 5 lines
Quoted 1 line

For these 2 patches:

These 2 patches have been merged.
Thanks,
Jim

Quoted 21 lines
Quoted 20 lines
---
 libsepol/src/kernel_to_conf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/kernel_to_conf.c +1 -2
index a22f196d..263f9639 100644
@@ -186,7 +186,7 @@ static char *constraint_expr_to_str(struct policydb *pdb, struct constraint_expr
                                        names = ebitmap_to_str(&curr->names, pdb->p_role_val_to_name, 1);
                                }
                                if (!names) {
-                                       goto exit;
+                                       names = strdup("NO_IDENTIFIER");
                                }
                                new_val = create_str("%s %s %s", 3, attr1, op, names);
                                free(names);
--

2.26.2