Skip to contentSkip to conversation

libsepol/cil: handle SID without assigned context when writing policy.conf

PATCH Superseded Christian Göttsche 3 participants 5 messages Feb 20, 2021 – Feb 24, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol/cil: handle SID without assigned context when writing policy.conf

CIL permits not assigning a context to a SID, e.g. to an unused initial SID, e.g. 'any_socket'.

When using the example policy from the SELinux Notebook,
https://github.com/SELinuxProject/selinux-notebook/blob/main/src/notebook-examples/cil-policy/cil-policy.cil,
secilc logs:

No context assigned to SID any_socket, omitting from policy at cil-policy.cil:166

But secil2conf segfaults when writing the policy.conf:

../cil/src/cil_policy.c:274:2: runtime error: member access within null pointer of type 'struct cil_context'

Only print the assigned context if actually available.

---
 libsepol/cil/src/cil_policy.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_policy.c +3 -1
index 30d507f1..863636c7 100644
@@ -1661,7 +1661,9 @@ static void cil_sid_contexts_to_policy(FILE *out, struct cil_list *sids, int mls
 	cil_list_for_each(i1, sids) {
 		sid = i1->data;
 		fprintf(out, "sid %s ", sid->datum.fqn);
-		cil_context_to_policy(out, sid->context, mls);
+		if (sid->context) {
+			cil_context_to_policy(out, sid->context, mls);
+		}
 		fprintf(out,"\n");
 	}
 }
Signature · 1 line
2.30.1
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Christian Göttsche

CIL permits not assigning a context to a SID, e.g. to an unused initial SID, e.g. 'any_socket'.

When using the example policy from the SELinux Notebook,
https://github.com/SELinuxProject/selinux-notebook/blob/main/src/notebook-examples/cil-policy/cil-policy.cil,
secilc logs:

No context assigned to SID any_socket, omitting from policy at cil-policy.cil:166

Quoted 7 lines

But secil2conf segfaults when writing the policy.conf:

../cil/src/cil_policy.c:274:2: runtime error: member access within null pointer of type 'struct cil_context'

Only print the assigned context if actually available.

Quoted 22 lines
---
 libsepol/cil/src/cil_policy.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_policy.c +3 -2
index 30d507f1..863636c7 100644
@@ -1661,7 +1661,9 @@ static void cil_sid_contexts_to_policy(FILE *out, struct cil_list *sids, int mls
        cil_list_for_each(i1, sids) {
                sid = i1->data;
                fprintf(out, "sid %s ", sid->datum.fqn);
-               cil_context_to_policy(out, sid->context, mls);
+               if (sid->context) {
+                       cil_context_to_policy(out, sid->context, mls);
+               }
                fprintf(out,"\n");
        }
 }
--

2.30.1

#2 lore raw

PATCH v2 1/1 libsepol/cil: handle SID without assigned context when writing policy.conf

In reply to Christian Göttsche

CIL permits not assigning a context to a SID, e.g. to an unused initial SID, e.g. 'any_socket'.

When using the example policy from the SELinux Notebook,
https://github.com/SELinuxProject/selinux-notebook/blob/main/src/notebook-examples/cil-policy/cil-policy.cil,
secilc logs:

No context assigned to SID any_socket, omitting from policy at cil-policy.cil:166

But secil2conf segfaults when writing the policy.conf:

../cil/src/cil_policy.c:274:2: runtime error: member access within null pointer of type 'struct cil_context'

Only print the sid context statement if a context was actually assigned. The sid declaration is still included via cil_sid_decls_to_policy().

---
v2:
Drop the statement completely in cil_sid_contexts_to_policy(), cause cil_sid_decls_to_policy() will have printed the context less declaration already.

 libsepol/cil/src/cil_policy.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
libsepol/cil/src/cil_policy.c +5 -3
index 74edb345..91e767b7 100644
@@ -1660,9 +1660,11 @@ static void cil_sid_contexts_to_policy(FILE *out, struct cil_list *sids, int mls
 
 	cil_list_for_each(i1, sids) {
 		sid = i1->data;
-		fprintf(out, "sid %s ", sid->datum.fqn);
-		cil_context_to_policy(out, sid->context, mls);
-		fprintf(out,"\n");
+		if (sid->context) {
+			fprintf(out, "sid %s ", sid->datum.fqn);
+			cil_context_to_policy(out, sid->context, mls);
+			fprintf(out,"\n");
+		}
 	}
 }
 
Signature · 1 line
2.30.1
Acked-by on v2 1/1
#3 lore raw
Quoted 10 lines · Christian Göttsche

CIL permits not assigning a context to a SID, e.g. to an unused initial SID, e.g. 'any_socket'.

When using the example policy from the SELinux Notebook,
https://github.com/SELinuxProject/selinux-notebook/blob/main/src/notebook-examples/cil-policy/cil-policy.cil,
secilc logs:

No context assigned to SID any_socket, omitting from policy at cil-policy.cil:166

Quoted 8 lines

But secil2conf segfaults when writing the policy.conf:

../cil/src/cil_policy.c:274:2: runtime error: member access within null pointer of type 'struct cil_context'

Only print the sid context statement if a context was actually assigned. The sid declaration is still included via cil_sid_decls_to_policy().

Oops, I should have noticed that. I was too focused on the segfault.

Quoted 31 lines

---
v2:
Drop the statement completely in cil_sid_contexts_to_policy(), cause cil_sid_decls_to_policy() will have printed the context less declaration already.

 libsepol/cil/src/cil_policy.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
libsepol/cil/src/cil_policy.c +5 -4
index 74edb345..91e767b7 100644
@@ -1660,9 +1660,11 @@ static void cil_sid_contexts_to_policy(FILE *out, struct cil_list *sids, int mls
 
        cil_list_for_each(i1, sids) {
                sid = i1->data;
-               fprintf(out, "sid %s ", sid->datum.fqn);
-               cil_context_to_policy(out, sid->context, mls);
-               fprintf(out,"\n");
+               if (sid->context) {
+                       fprintf(out, "sid %s ", sid->datum.fqn);
+                       cil_context_to_policy(out, sid->context, mls);
+                       fprintf(out,"\n");
+               }
        }
 }
 
--

2.30.1

#4 lore raw
Quoted 16 lines · James Carter
Quoted 14 lines · Christian Göttsche

CIL permits not assigning a context to a SID, e.g. to an unused initial SID, e.g. 'any_socket'.

When using the example policy from the SELinux Notebook,
https://github.com/SELinuxProject/selinux-notebook/blob/main/src/notebook-examples/cil-policy/cil-policy.cil,
secilc logs:

No context assigned to SID any_socket, omitting from policy at cil-policy.cil:166

But secil2conf segfaults when writing the policy.conf:

../cil/src/cil_policy.c:274:2: runtime error: member access within null pointer of type 'struct cil_context'

Quoted 8 lines
Quoted 4 lines

Only print the sid context statement if a context was actually assigned. The sid declaration is still included via cil_sid_decls_to_policy().

Oops, I should have noticed that. I was too focused on the segfault.

Merged, thanks!

Quoted 31 lines
Quoted 31 lines

---
v2:
Drop the statement completely in cil_sid_contexts_to_policy(), cause cil_sid_decls_to_policy() will have printed the context less declaration already.

 libsepol/cil/src/cil_policy.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
libsepol/cil/src/cil_policy.c +5 -4
index 74edb345..91e767b7 100644
@@ -1660,9 +1660,11 @@ static void cil_sid_contexts_to_policy(FILE *out, struct cil_list *sids, int mls
 
        cil_list_for_each(i1, sids) {
                sid = i1->data;
-               fprintf(out, "sid %s ", sid->datum.fqn);
-               cil_context_to_policy(out, sid->context, mls);
-               fprintf(out,"\n");
+               if (sid->context) {
+                       fprintf(out, "sid %s ", sid->datum.fqn);
+                       cil_context_to_policy(out, sid->context, mls);
+                       fprintf(out,"\n");
+               }
        }
 }
 
--

2.30.1