Skip to contentSkip to conversation

libsepol: add sepol_av_perm_to_string

PATCH Superseded Christian Göttsche 3 participants 10 messages Feb 4, 2022 – Apr 12, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libsepol: add sepol_av_perm_to_string

Add a wrapper around the utility function sepol_av_to_string() on the service internal policy. This allows callers to convert a permission bit set into a string representation without access to the internal policy structure.

---
 libsepol/include/sepol/policydb/services.h | 9 +++++++++
 libsepol/src/services.c                    | 6 ++++++
 2 files changed, 15 insertions(+)
libsepol/include/sepol/policydb/services.h +9 -0
index 048f8a5a..44de3863 100644
@@ -103,6 +103,15 @@ extern int sepol_string_to_av_perm(sepol_security_class_t tclass,
 					const char *perm_name,
 					sepol_access_vector_t *av);
 
+/*
+ * Return a string representation of the permission av bit associated with
+ * tclass.
+ * Returns a pointer to an internal buffer, overridden by the next call to
+ * this function or sepol_av_to_string().
+ */
+ extern const char *sepol_av_perm_to_string(sepol_security_class_t tclass,
+					sepol_access_vector_t av);
+
 /*
  * Compute a SID to use for labeling a new object in the 
  * class `tclass' based on a SID pair.  
libsepol/src/services.c +6 -0
index 7becfd1b..b2fb804e 100644
@@ -1233,6 +1233,12 @@ out:
 	return STATUS_ERR;
 }
 
+ const char *sepol_av_perm_to_string(sepol_security_class_t tclass,
+					sepol_access_vector_t av)
+{
+	return sepol_av_to_string(policydb, tclass, av);
+}
+
 /*
  * Write the security context string representation of 
  * the context associated with `sid' into a dynamically
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 libsepol: add policy utilities

These are similar to the libselinux utilities but operate on a binary policy instead of the running kernel. This allows to run them on SELinux disabled or even non Linux systems, e.g. for development or continuous integration.

Link the utilities statically with libsepol due to usages of non public exported interfaces.

sepol_check_access:
(similar to selinux_check_access)
Check access:

    $ sepol_check_access policy.bin staff_u:staff_r:gpg_t:s0 sysadm_u:sysadm_r:gpg_t:s0 process fork
    requested permission fork denied by constraint; reason:
    constrain process { fork setexec setfscreate setcurrent execmem execstack execheap setkeycreate setsockcreate } ((r1 == r2 -Fail-) ); Constraint DENIED
    constrain process { signull getsched getsession getpgid getcap getattr getrlimit } ((r1 == r2 -Fail-)  or (r1 != { staff_r user_r logadm_r apache2adm_r } -Fail-)  and (t1 == rbacproc_read -Fail-)  or (t1 == rbacproc_full -Fail-)  or (t1 == systemd_user_instance_domain -Fail-)  and (u2 == system_u -Fail-)  and (r2 == system_r -Fail-)  and (t2 == systemd_t -Fail-) ); Constraint DENIED
    constrain process { sigchld sigkill sigstop signal ptrace setsched setpgid setcap share setrlimit } ((r1 == r2 -Fail-)  or (r1 != { staff_r user_r logadm_r apache2adm_r } -Fail-)  and (t1 == rbacproc_full -Fail-)  or (t1 == systemd_user_instance_domain -Fail-)  and (u2 == system_u -Fail-)  and (r2 == system_r -Fail-)  and (t2 == systemd_t -Fail-) ); Constraint DENIED

sepol_compute_av:
(similar to compute_av)
Compute access vectors:

    $ sepol_compute_av policy.bin staff_u:staff_r:gpg_t:s0 staff_u:staff_r:gpg_t:s0 process
    allowed:     fork sigchld signull signal getsched setsched setpgid getcap setcap setrlimit
    decided:     fork transition sigchld sigkill sigstop signull signal ptrace getsched setsched getsession getpgid setpgid getcap setcap share getattr setexec setfscreate noatsecure siginh setrlimit rlimitinh dyntransition setcurrent execmem execstack execheap setkeycreate setsockcreate getrlimit
    auditallow:
    auditdeny:   fork transition sigchld sigkill sigstop signull signal ptrace getsched setsched getsession getpgid setpgid getcap setcap share getattr setexec setfscreate noatsecure siginh setrlimit rlimitinh dyntransition setcurrent execmem execstack execheap setkeycreate setsockcreate getrlimit

sepol_compute_member:
(similar to compute_member)
Compute a SID to use when selecting a member of a polyinstantiated object:

    $ sepol_compute_member policy.bin staff_u:staff_r:staff_t:s0 system_u:object_r:tmp_t:s0 dir
    system_u:object_r:user_tmp_t:s0

sepol_compute_relabel:
(similar to compute_relabel)
Compute a SID to use for relabeling an object:

    $ sepol_compute_relabel policy.bin staff_u:staff_r:staff_t:s0 system_u:object_r:tty_device_t:s0 chr_file
    staff_u:object_r:user_tty_device_t:s0
---
 libsepol/.gitignore                    |   4 +
 libsepol/utils/Makefile                |   2 +-
 libsepol/utils/sepol_check_access.c    | 130 +++++++++++++++++++++++++
 libsepol/utils/sepol_compute_av.c      |  66 +++++++++++++
 libsepol/utils/sepol_compute_member.c  |  64 ++++++++++++
 libsepol/utils/sepol_compute_relabel.c |  64 ++++++++++++
 6 files changed, 329 insertions(+), 1 deletion(-)
 create mode 100644 libsepol/utils/sepol_check_access.c
 create mode 100644 libsepol/utils/sepol_compute_av.c
 create mode 100644 libsepol/utils/sepol_compute_member.c
 create mode 100644 libsepol/utils/sepol_compute_relabel.c
libsepol/.gitignore +4 -0
index 77bb5911..68cbe9b0 100644
@@ -1,2 +1,6 @@
 utils/chkcon
+utils/sepol_check_access
+utils/sepol_compute_av
+utils/sepol_compute_member
+utils/sepol_compute_relabel
 libsepol.map
libsepol/utils/Makefile +1 -1
index 31932c11..8e89bc96 100644
@@ -5,7 +5,7 @@ BINDIR ?= $(PREFIX)/bin
 CFLAGS ?= -Wall -Werror
 override CFLAGS += -I../include
 override LDFLAGS += -L../src
-override LDLIBS += -lsepol
+override LDLIBS += ../src/libsepol.a
 
 TARGETS=$(patsubst %.c,%,$(sort $(wildcard *.c)))
 
libsepol/utils/sepol_check_access.c new file +130 -0

130 lines not on this page · raw message

libsepol/utils/sepol_compute_av.c new file +66 -0

66 lines not on this page · raw message

libsepol/utils/sepol_compute_member.c new file +64 -0

64 lines not on this page · raw message

libsepol/utils/sepol_compute_relabel.c new file +64 -0

64 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 2/2] libsepol: add policy utilities

Quoted 7 lines · Christian Göttsche

These are similar to the libselinux utilities but operate on a binary policy instead of the running kernel. This allows to run them on SELinux disabled or even non Linux systems, e.g. for development or continuous integration.

Link the utilities statically with libsepol due to usages of non public exported interfaces.

It would be nice to export these interfaces. I'd add them to setools.

Quoted 420 lines

sepol_check_access:
(similar to selinux_check_access)
Check access:

     $ sepol_check_access policy.bin staff_u:staff_r:gpg_t:s0 sysadm_u:sysadm_r:gpg_t:s0 process fork
     requested permission fork denied by constraint; reason:
     constrain process { fork setexec setfscreate setcurrent execmem execstack execheap setkeycreate setsockcreate } ((r1 == r2 -Fail-) ); Constraint DENIED
     constrain process { signull getsched getsession getpgid getcap getattr getrlimit } ((r1 == r2 -Fail-)  or (r1 != { staff_r user_r logadm_r apache2adm_r } -Fail-)  and (t1 == rbacproc_read -Fail-)  or (t1 == rbacproc_full -Fail-)  or (t1 == systemd_user_instance_domain -Fail-)  and (u2 == system_u -Fail-)  and (r2 == system_r -Fail-)  and (t2 == systemd_t -Fail-) ); Constraint DENIED
     constrain process { sigchld sigkill sigstop signal ptrace setsched setpgid setcap share setrlimit } ((r1 == r2 -Fail-)  or (r1 != { staff_r user_r logadm_r apache2adm_r } -Fail-)  and (t1 == rbacproc_full -Fail-)  or (t1 == systemd_user_instance_domain -Fail-)  and (u2 == system_u -Fail-)  and (r2 == system_r -Fail-)  and (t2 == systemd_t -Fail-) ); Constraint DENIED

sepol_compute_av:
(similar to compute_av)
Compute access vectors:

     $ sepol_compute_av policy.bin staff_u:staff_r:gpg_t:s0 staff_u:staff_r:gpg_t:s0 process
     allowed:     fork sigchld signull signal getsched setsched setpgid getcap setcap setrlimit
     decided:     fork transition sigchld sigkill sigstop signull signal ptrace getsched setsched getsession getpgid setpgid getcap setcap share getattr setexec setfscreate noatsecure siginh setrlimit rlimitinh dyntransition setcurrent execmem execstack execheap setkeycreate setsockcreate getrlimit
     auditallow:
     auditdeny:   fork transition sigchld sigkill sigstop signull signal ptrace getsched setsched getsession getpgid setpgid getcap setcap share getattr setexec setfscreate noatsecure siginh setrlimit rlimitinh dyntransition setcurrent execmem execstack execheap setkeycreate setsockcreate getrlimit

sepol_compute_member:
(similar to compute_member)
Compute a SID to use when selecting a member of a polyinstantiated object:

     $ sepol_compute_member policy.bin staff_u:staff_r:staff_t:s0 system_u:object_r:tmp_t:s0 dir
     system_u:object_r:user_tmp_t:s0

sepol_compute_relabel:
(similar to compute_relabel)
Compute a SID to use for relabeling an object:

     $ sepol_compute_relabel policy.bin staff_u:staff_r:staff_t:s0 system_u:object_r:tty_device_t:s0 chr_file
     staff_u:object_r:user_tty_device_t:s0
---
  libsepol/.gitignore                    |   4 +
  libsepol/utils/Makefile                |   2 +-
  libsepol/utils/sepol_check_access.c    | 130 +++++++++++++++++++++++++
  libsepol/utils/sepol_compute_av.c      |  66 +++++++++++++
  libsepol/utils/sepol_compute_member.c  |  64 ++++++++++++
  libsepol/utils/sepol_compute_relabel.c |  64 ++++++++++++
  6 files changed, 329 insertions(+), 1 deletion(-)

create mode 100644 libsepol/utils/sepol_check_access.c create mode 100644 libsepol/utils/sepol_compute_av.c create mode 100644 libsepol/utils/sepol_compute_member.c create mode 100644 libsepol/utils/sepol_compute_relabel.c

libsepol/.gitignore +4 -0
index 77bb5911..68cbe9b0 100644
@@ -1,2 +1,6 @@
  utils/chkcon
+utils/sepol_check_access
+utils/sepol_compute_av
+utils/sepol_compute_member
+utils/sepol_compute_relabel
  libsepol.map
libsepol/utils/Makefile +1 -1
index 31932c11..8e89bc96 100644
@@ -5,7 +5,7 @@ BINDIR ?= $(PREFIX)/bin
  CFLAGS ?= -Wall -Werror
  override CFLAGS += -I../include
  override LDFLAGS += -L../src
-override LDLIBS += -lsepol
+override LDLIBS += ../src/libsepol.a
 
  TARGETS=$(patsubst %.c,%,$(sort $(wildcard *.c)))
 
libsepol/utils/sepol_check_access.c new file +130 -0
new file mode 100644
index 00000000..d0470156
@@ -0,0 +1,130 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sepol/policydb/services.h>
+#include <sepol/sepol.h>
+
+
+int main(int argc, char *argv[])
+{
+	FILE *fp;
+	sepol_security_id_t ssid, tsid;
+	sepol_security_class_t tclass;
+	const char *permlist;
+	sepol_access_vector_t av;
+	struct sepol_av_decision avd;
+	unsigned int reason;
+	char *reason_buf;
+	int i;
+
+	if (argc != 6) {
+		printf("usage:  %s policy source_context target_context class permission[,permission2[,...]]\n", argv[0]);
+		return 1;
+	}
+
+	fp = fopen(argv[1], "r");
+	if (!fp) {
+		fprintf(stderr, "Can't open policy %s:  %s\n", argv[1], strerror(errno));
+		return 1;
+	}
+	if (sepol_set_policydb_from_file(fp) < 0) {
+		fprintf(stderr, "Error while processing policy %s:  %s\n", argv[1], strerror(errno));
+		fclose(fp);
+		return 1;
+	}
+	fclose(fp);
+
+	if (sepol_context_to_sid(argv[2], strlen(argv[2]), &ssid) < 0) {
+		fprintf(stderr, "Invalid source context %s\n", argv[2]);
+		return 1;
+	}
+
+	if (sepol_context_to_sid(argv[3], strlen(argv[3]), &tsid) < 0) {
+		fprintf(stderr, "Invalid target context %s\n", argv[3]);
+		return 1;
+	}
+
+	if (sepol_string_to_security_class(argv[4], &tclass) < 0) {
+		fprintf(stderr, "Invalid security class %s\n", argv[4]);
+		return 1;
+	}
+
+	permlist = argv[5];
+	do {
+		char *tmp = NULL;
+		const char *perm;
+		const char *delim = strchr(permlist, ',');
+
+		if (delim) {
+			tmp = strndup(permlist, delim - permlist);
+			if (!tmp) {
+				fprintf(stderr, "Failed to allocate memory:  %s\n", strerror(errno));
+				return 1;
+			}
+		}
+
+		perm = tmp ? tmp : permlist;
+
+		if (sepol_string_to_av_perm(tclass, perm, &av) < 0) {
+			fprintf(stderr, "Invalid permission %s for security class %s:  %s\n", perm, argv[4], strerror(errno));
+			free(tmp);
+			return 1;
+		}
+
+		free(tmp);
+
+		permlist = strchr(permlist, ',');
+	} while (permlist++);
+
+	if (av == 0) {
+		fprintf(stderr, "Empty permission set computed from %s\n", argv[5]);
+		return 1;
+	}
+	
+	if (sepol_compute_av_reason_buffer(ssid, tsid, tclass, av, &avd, &reason, &reason_buf, 0) < 0) {
+		fprintf(stderr, "Failed to compute av decision:  %s\n", strerror(errno));
+		return 1;
+	}
+
+	if ((avd.allowed & av) == av) {
+		printf("requested permission %s allowed\n", argv[5]);
+		free(reason_buf);
+		return 0;
+	}
+
+	printf("requested permission %s denied by ", argv[5]);
+	i = 0;
+	if (reason & SEPOL_COMPUTEAV_TE) {
+		printf("te-rule");
+		i++;
+	}
+	if (reason & SEPOL_COMPUTEAV_CONS) {
+		if (i > 0)
+			printf(", ");
+		printf("constraint");
+		i++;
+	}
+	if (reason & SEPOL_COMPUTEAV_RBAC) {
+		if (i > 0)
+			printf(", ");
+		printf("transition-constraint");
+		i++;
+	}
+	if (reason & SEPOL_COMPUTEAV_BOUNDS) {
+		if (i > 0)
+			printf(", ");
+		printf("type-bound");
+		//i++;
+	}
+
+	if ((reason & SEPOL_COMPUTEAV_CONS) && reason_buf)
+		printf("; reason:\n%s", reason_buf);
+
+	free(reason_buf);
+
+	printf("\n");
+
+	return 7;		
+}
libsepol/utils/sepol_compute_av.c new file +66 -0
new file mode 100644
index 00000000..d64dc31d
@@ -0,0 +1,66 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sepol/policydb/services.h>
+#include <sepol/sepol.h>
+
+
+int main(int argc, char *argv[])
+{
+	FILE *fp;
+	sepol_security_id_t ssid, tsid;
+	sepol_security_class_t tclass;
+	struct sepol_av_decision avd;
+	int rc;
+
+	if (argc != 5) {
+		printf("usage:  %s policy scontext tcontext tclass\n", argv[0]);
+		return 1;
+	}
+
+	fp = fopen(argv[1], "r");
+	if (!fp) {
+		fprintf(stderr, "Can't open policy %s:  %s\n", argv[1], strerror(errno));
+		return 1;
+	}
+	if (sepol_set_policydb_from_file(fp) < 0) {
+		fprintf(stderr, "Error while processing policy %s:  %s\n", argv[1], strerror(errno));
+		fclose(fp);
+		return 1;
+	}
+	fclose(fp);
+
+	if (sepol_context_to_sid(argv[2], strlen(argv[2]), &ssid) < 0) {
+		fprintf(stderr, "Invalid source context %s\n", argv[2]);
+		return 1;
+	}
+
+	if (sepol_context_to_sid(argv[3], strlen(argv[3]), &tsid) < 0) {
+		fprintf(stderr, "Invalid target context %s\n", argv[3]);
+		return 1;
+	}
+
+	if (sepol_string_to_security_class(argv[4], &tclass) < 0) {
+		fprintf(stderr, "Invalid security class %s\n", argv[4]);
+		return 1;
+	}
+
+	rc = sepol_compute_av(ssid, tsid, tclass, 0, &avd);
+	switch (rc) {
+	case 0:
+		printf("allowed:    %s\n", sepol_av_perm_to_string(tclass, avd.allowed));
+		printf("decided:    %s\n", sepol_av_perm_to_string(tclass, avd.decided));
+		printf("auditallow: %s\n", sepol_av_perm_to_string(tclass, avd.auditallow));
+		printf("auditdeny:  %s\n", sepol_av_perm_to_string(tclass, avd.auditdeny));
+		break;
+	case -EINVAL:
+		printf("Invalid request\n");
+		break;
+	default:
+		printf("Failed to compute av decision: %d\n", rc);
+	}
+
+	return rc != 0;
+}
libsepol/utils/sepol_compute_member.c new file +64 -0
new file mode 100644
index 00000000..3d67335d
@@ -0,0 +1,64 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sepol/policydb/services.h>
+#include <sepol/sepol.h>
+
+
+int main(int argc, char *argv[])
+{
+	FILE *fp;
+	sepol_security_id_t ssid, tsid, out_sid;
+	sepol_security_class_t tclass;
+	char *context;
+	size_t context_len;
+	
+	if (argc != 5) {
+		printf("usage:  %s policy scontext tcontext tclass\n", argv[0]);
+		return 1;
+	}
+
+	fp = fopen(argv[1], "r");
+	if (!fp) {
+		fprintf(stderr, "Can't open policy %s:  %s\n", argv[1], strerror(errno));
+		return 1;
+	}
+	if (sepol_set_policydb_from_file(fp) < 0) {
+		fprintf(stderr, "Error while processing policy %s:  %s\n", argv[1], strerror(errno));
+		fclose(fp);
+		return 1;
+	}
+	fclose(fp);
+
+	if (sepol_context_to_sid(argv[2], strlen(argv[2]), &ssid) < 0) {
+		fprintf(stderr, "Invalid source context %s\n", argv[2]);
+		return 1;
+	}
+
+	if (sepol_context_to_sid(argv[3], strlen(argv[3]), &tsid) < 0) {
+		fprintf(stderr, "Invalid target context %s\n", argv[3]);
+		return 1;
+	}
+
+	if (sepol_string_to_security_class(argv[4], &tclass) < 0) {
+		fprintf(stderr, "Invalid security class %s\n", argv[4]);
+		return 1;
+	}
+
+	if (sepol_member_sid(ssid, tsid, tclass, &out_sid) < 0) {
+		fprintf(stderr, "Failed to compute member sid:  %s\n", strerror(errno));
+		return 1;
+	}
+	
+	if (sepol_sid_to_context(out_sid, &context, &context_len) < 0) {
+		fprintf(stderr, "Failed to convert sid %u:  %s\n", out_sid, strerror(errno));
+		return 1;
+	}
+
+	printf("%s\n", context);
+	free(context);
+
+	return 0;
+}
libsepol/utils/sepol_compute_relabel.c new file +64 -0
new file mode 100644
index 00000000..db664ce8
@@ -0,0 +1,64 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sepol/policydb/services.h>
+#include <sepol/sepol.h>
+
+
+int main(int argc, char *argv[])
+{
+	FILE *fp;
+	sepol_security_id_t ssid, tsid, out_sid;
+	sepol_security_class_t tclass;
+	char *context;
+	size_t context_len;
+	
+	if (argc != 5) {
+		printf("usage:  %s policy scontext tcontext tclass\n", argv[0]);
+		return 1;
+	}
+
+	fp = fopen(argv[1], "r");
+	if (!fp) {
+		fprintf(stderr, "Can't open policy %s:  %s\n", argv[1], strerror(errno));
+		return 1;
+	}
+	if (sepol_set_policydb_from_file(fp) < 0) {
+		fprintf(stderr, "Error while processing policy %s:  %s\n", argv[1], strerror(errno));
+		fclose(fp);
+		return 1;
+	}
+	fclose(fp);
+
+	if (sepol_context_to_sid(argv[2], strlen(argv[2]), &ssid) < 0) {
+		fprintf(stderr, "Invalid source context %s\n", argv[2]);
+		return 1;
+	}
+
+	if (sepol_context_to_sid(argv[3], strlen(argv[3]), &tsid) < 0) {
+		fprintf(stderr, "Invalid target context %s\n", argv[3]);
+		return 1;
+	}
+
+	if (sepol_string_to_security_class(argv[4], &tclass) < 0) {
+		fprintf(stderr, "Invalid security class %s\n", argv[4]);
+		return 1;
+	}
+
+	if (sepol_change_sid(ssid, tsid, tclass, &out_sid) < 0) {
+		fprintf(stderr, "Failed to compute changed sid:  %s\n", strerror(errno));
+		return 1;
+	}
+	
+	if (sepol_sid_to_context(out_sid, &context, &context_len) < 0) {
+		fprintf(stderr, "Failed to convert sid %u:  %s\n", out_sid, strerror(errno));
+		return 1;
+	}
+
+	printf("%s\n", context);
+	free(context);
+
+	return 0;
+}
Signature · 1 line
Chris PeBenito
#3 lore raw

RFC PATCH v2 1/4 libsepol: add sepol_av_perm_to_string

In reply to Christian Göttsche

Add a wrapper around the utility function sepol_av_to_string() on the service internal policy. This allows callers to convert a permission bit set into a string representation without access to the internal policy structure.

---
 libsepol/include/sepol/policydb/services.h | 9 +++++++++
 libsepol/src/services.c                    | 6 ++++++
 2 files changed, 15 insertions(+)
libsepol/include/sepol/policydb/services.h +9 -0
index 048f8a5a..44de3863 100644
@@ -103,6 +103,15 @@ extern int sepol_string_to_av_perm(sepol_security_class_t tclass,
 					const char *perm_name,
 					sepol_access_vector_t *av);
 
+/*
+ * Return a string representation of the permission av bit associated with
+ * tclass.
+ * Returns a pointer to an internal buffer, overridden by the next call to
+ * this function or sepol_av_to_string().
+ */
+ extern const char *sepol_av_perm_to_string(sepol_security_class_t tclass,
+					sepol_access_vector_t av);
+
 /*
  * Compute a SID to use for labeling a new object in the 
  * class `tclass' based on a SID pair.  
libsepol/src/services.c +6 -0
index 7becfd1b..b2fb804e 100644
@@ -1233,6 +1233,12 @@ out:
 	return STATUS_ERR;
 }
 
+ const char *sepol_av_perm_to_string(sepol_security_class_t tclass,
+					sepol_access_vector_t av)
+{
+	return sepol_av_to_string(policydb, tclass, av);
+}
+
 /*
  * Write the security context string representation of 
  * the context associated with `sid' into a dynamically
Signature · 1 line
2.35.1
#4 lore raw

RFC PATCH v2 2/4 libsepol: introduce sepol_const_security_context_t typedef

The typedef sepol_security_context_t is used for contexts. For the read-only input parameter in sepol_context_to_sid()
const sepol_security_context_t is used as type, which does not expand to the expected const char* but char *const.

Introduce a corresponding typedef for const char*.

---
 libsepol/include/sepol/policydb/flask_types.h | 1 +
 libsepol/include/sepol/policydb/services.h    | 2 +-
 libsepol/src/context.c                        | 2 +-
 libsepol/src/services.c                       | 2 +-
 4 files changed, 4 insertions(+), 3 deletions(-)
libsepol/include/sepol/policydb/flask_types.h +1 -0
index 7bec5129..02c22eac 100644
@@ -27,6 +27,7 @@ extern "C" {
  * understanding of the security policy.
  */
 typedef char *sepol_security_context_t;
+typedef const char *sepol_const_security_context_t;
 
 /*
  * An access vector (AV) is a collection of related permissions
libsepol/include/sepol/policydb/services.h +1 -1
index 44de3863..bcb0930f 100644
@@ -155,7 +155,7 @@ extern int sepol_sid_to_context(sepol_security_id_t sid,	/* IN */
  * Return a SID associated with the security context that
  * has the string representation specified by `scontext'.
  */
-extern int sepol_context_to_sid(const sepol_security_context_t scontext,	/* IN */
+extern int sepol_context_to_sid(sepol_const_security_context_t scontext,	/* IN */
 				size_t scontext_len,	/* IN */
 				sepol_security_id_t * out_sid);	/* OUT */
 
libsepol/src/context.c +1 -1
index e81b28c6..5cc90afb 100644
@@ -22,7 +22,7 @@ int policydb_context_isvalid(const policydb_t * p, const context_struct_t * c)
 int sepol_check_context(const char *context)
 {
 
-	return sepol_context_to_sid((const sepol_security_context_t)context,
+	return sepol_context_to_sid(context,
 				    strlen(context) + 1, NULL);
 }
 
libsepol/src/services.c +1 -1
index b2fb804e..c3897c91 100644
@@ -1269,7 +1269,7 @@ int sepol_sid_to_context(sepol_security_id_t sid,
  * Return a SID associated with the security context that
  * has the string representation specified by `scontext'.
  */
-int sepol_context_to_sid(const sepol_security_context_t scontext,
+int sepol_context_to_sid(sepol_const_security_context_t scontext,
 				size_t scontext_len, sepol_security_id_t * sid)
 {
 
Signature · 1 line
2.35.1
#5 lore raw

RFC PATCH v2 3/4 libsepol: export functions for policy analysis

In reply to Christian Göttsche

Export functions needed for converting security identifiers from and to strings and functions computing security server decisions. These can be used to debug or run tests on binary policies without running on a SELinux enabled kernel.

TODO:
These functions have currently a non consistent return behavior: some are returning -1 on failure and set errno most but not all of the time, some return a negative errno like value.
Maybe this should be addressed before exporting them?

---
 libsepol/src/libsepol.map.in | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
libsepol/src/libsepol.map.in +15 -0
index 0e05d606..844924fc 100644
@@ -274,3 +274,18 @@ LIBSEPOL_3.0 {
 	cil_write_resolve_ast;
 	cil_set_qualified_names;
 } LIBSEPOL_1.1;
+
+LIBSEPOL_3.4 {
+  global:
+	sepol_av_perm_to_string;
+	sepol_change_sid;
+	sepol_compute_av;
+	sepol_compute_av_reason;
+	sepol_compute_av_reason_buffer;
+	sepol_context_to_sid;
+	sepol_member_sid;
+	sepol_sid_to_context;
+	sepol_string_to_av_perm;
+	sepol_string_to_security_class;
+	sepol_validate_transition_reason_buffer;
+} LIBSEPOL_3.0;
Signature · 1 line
2.35.1
#6 lore raw

Re: [RFC PATCH v2 3/4] libsepol: export functions for policy analysis

Quoted 41 lines · Christian Göttsche

Export functions needed for converting security identifiers from and to strings and functions computing security server decisions. These can be used to debug or run tests on binary policies without running on a SELinux enabled kernel.

TODO:
These functions have currently a non consistent return behavior: some are returning -1 on failure and set errno most but not all of the time, some return a negative errno like value.
Maybe this should be addressed before exporting them?

---
 libsepol/src/libsepol.map.in | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
libsepol/src/libsepol.map.in +15 -1
index 0e05d606..844924fc 100644
@@ -274,3 +274,18 @@ LIBSEPOL_3.0 {
        cil_write_resolve_ast;
        cil_set_qualified_names;
 } LIBSEPOL_1.1;
+
+LIBSEPOL_3.4 {
+  global:
+       sepol_av_perm_to_string;
+       sepol_change_sid;
+       sepol_compute_av;
+       sepol_compute_av_reason;
+       sepol_compute_av_reason_buffer;
+       sepol_context_to_sid;
+       sepol_member_sid;
+       sepol_sid_to_context;
+       sepol_string_to_av_perm;
+       sepol_string_to_security_class;
+       sepol_validate_transition_reason_buffer;
+} LIBSEPOL_3.0;
--
Quoted 2 lines

2.35.1

I am a bit conflicted. I like this patch series and see the value in exporting these functions, but I really don't want to export functions from include/sepol/policydb/. We have three already that have slipped
through: expand_module_avrules(), sepol_polcap_getnum(), and sepol_polcap_getname(). Keeping the exported functions in include/sepol makes it easier to see what is exported.

At least in this case the data structures involved aren't going to cause problems later as sepol_security_id_t and sepol_access_vector_t are both unit32_t, sepol_security_class_t is a unit16_t, and even struct sepol_av_decision consists of sepol_access_vector_t and uint32_t members. We don't want external programs strongly relying on the details of the data structures in include/sepol/policydb. It was kind of a mess when the named transitions were moved from a list to a hash table.

Since most of these functions are used in either checkpolicy or audit2why (or both), it is probably fine to export these, but I would appreciate any thoughts that Chris and others might have.

Jim

#7 lore raw

RFC PATCH v2 4/4 libsepol: add policy utilities

In reply to Christian Göttsche

These are similar to the libselinux utilities but operate on a binary policy instead of the running kernel. This allows to run them on SELinux disabled or even non Linux systems, e.g. for development or continuous integration.

sepol_check_access:
(similar to selinux_check_access)
Check access:

    $ sepol_check_access policy.bin staff_u:staff_r:gpg_t:s0 sysadm_u:sysadm_r:gpg_t:s0 process fork
    requested permission fork denied by constraint; reason:
    constrain process { fork setexec setfscreate setcurrent execmem execstack execheap setkeycreate setsockcreate } ((r1 == r2 -Fail-) ); Constraint DENIED
    constrain process { signull getsched getsession getpgid getcap getattr getrlimit } ((r1 == r2 -Fail-)  or (r1 != { staff_r user_r logadm_r apache2adm_r } -Fail-)  and (t1 == rbacproc_read -Fail-)  or (t1 == rbacproc_full -Fail-)  or (t1 == systemd_user_instance_domain -Fail-)  and (u2 == system_u -Fail-)  and (r2 == system_r -Fail-)  and (t2 == systemd_t -Fail-) ); Constraint DENIED
    constrain process { sigchld sigkill sigstop signal ptrace setsched setpgid setcap share setrlimit } ((r1 == r2 -Fail-)  or (r1 != { staff_r user_r logadm_r apache2adm_r } -Fail-)  and (t1 == rbacproc_full -Fail-)  or (t1 == systemd_user_instance_domain -Fail-)  and (u2 == system_u -Fail-)  and (r2 == system_r -Fail-)  and (t2 == systemd_t -Fail-) ); Constraint DENIED

sepol_compute_av:
(similar to compute_av)
Compute access vectors:

    $ sepol_compute_av policy.bin staff_u:staff_r:gpg_t:s0 staff_u:staff_r:gpg_t:s0 process
    allowed:     fork sigchld signull signal getsched setsched setpgid getcap setcap setrlimit
    decided:     fork transition sigchld sigkill sigstop signull signal ptrace getsched setsched getsession getpgid setpgid getcap setcap share getattr setexec setfscreate noatsecure siginh setrlimit rlimitinh dyntransition setcurrent execmem execstack execheap setkeycreate setsockcreate getrlimit
    auditallow:
    auditdeny:   fork transition sigchld sigkill sigstop signull signal ptrace getsched setsched getsession getpgid setpgid getcap setcap share getattr setexec setfscreate noatsecure siginh setrlimit rlimitinh dyntransition setcurrent execmem execstack execheap setkeycreate setsockcreate getrlimit

sepol_compute_member:
(similar to compute_member)
Compute a SID to use when selecting a member of a polyinstantiated object:

    $ sepol_compute_member policy.bin staff_u:staff_r:staff_t:s0 system_u:object_r:tmp_t:s0 dir
    system_u:object_r:user_tmp_t:s0

sepol_compute_relabel:
(similar to compute_relabel)
Compute a SID to use for relabeling an object:

    $ sepol_compute_relabel policy.bin staff_u:staff_r:staff_t:s0 system_u:object_r:tty_device_t:s0 chr_file
    staff_u:object_r:user_tty_device_t:s0

sepol_validate_transition:
(similar to validatetrans)
Compute a validatetrans decision:

  $ sepol_validate_transition policy.bin system_u:object_r:user_tmp_t:s0 system_u:object_r:shadow_t:s0 file staff_u:staff_r:staff_t:s0
  allowed

---
v2:
link dynamically after exporting all the necessary functions

---
 libsepol/.gitignore                        |   5 +
 libsepol/utils/sepol_check_access.c        | 130 +++++++++++++++++++++
 libsepol/utils/sepol_compute_av.c          |  66 +++++++++++
 libsepol/utils/sepol_compute_member.c      |  64 ++++++++++
 libsepol/utils/sepol_compute_relabel.c     |  64 ++++++++++
 libsepol/utils/sepol_validate_transition.c |  74 ++++++++++++
 6 files changed, 403 insertions(+)
 create mode 100644 libsepol/utils/sepol_check_access.c
 create mode 100644 libsepol/utils/sepol_compute_av.c
 create mode 100644 libsepol/utils/sepol_compute_member.c
 create mode 100644 libsepol/utils/sepol_compute_relabel.c
 create mode 100644 libsepol/utils/sepol_validate_transition.c
libsepol/.gitignore +5 -0
index 77bb5911..abfb603b 100644
@@ -1,2 +1,7 @@
 utils/chkcon
+utils/sepol_check_access
+utils/sepol_compute_av
+utils/sepol_compute_member
+utils/sepol_compute_relabel
+utils/sepol_validate_transition
 libsepol.map
libsepol/utils/sepol_check_access.c new file +130 -0

130 lines not on this page · raw message

libsepol/utils/sepol_compute_av.c new file +66 -0

66 lines not on this page · raw message

libsepol/utils/sepol_compute_member.c new file +64 -0

64 lines not on this page · raw message

libsepol/utils/sepol_compute_relabel.c new file +64 -0

64 lines not on this page · raw message

libsepol/utils/sepol_validate_transition.c new file +74 -0

74 lines not on this page · raw message

Signature · 1 line
2.35.1
Acked-by on v2 1/4
#8 lore raw
In reply to Christian Göttsche
Quoted 7 lines · Christian Göttsche

Add a wrapper around the utility function sepol_av_to_string() on the service internal policy. This allows callers to convert a permission bit set into a string representation without access to the internal policy structure.

After thinking about it, I am ok with this series. All of the functions that are being exported are used by checkpolicy or audit2why and are from services.c.

So for these four patches:

Quoted 45 lines
---
 libsepol/include/sepol/policydb/services.h | 9 +++++++++
 libsepol/src/services.c                    | 6 ++++++
 2 files changed, 15 insertions(+)
libsepol/include/sepol/policydb/services.h +9 -0
index 048f8a5a..44de3863 100644
@@ -103,6 +103,15 @@ extern int sepol_string_to_av_perm(sepol_security_class_t tclass,
                                        const char *perm_name,
                                        sepol_access_vector_t *av);
 
+/*
+ * Return a string representation of the permission av bit associated with
+ * tclass.
+ * Returns a pointer to an internal buffer, overridden by the next call to
+ * this function or sepol_av_to_string().
+ */
+ extern const char *sepol_av_perm_to_string(sepol_security_class_t tclass,
+                                       sepol_access_vector_t av);
+
 /*
  * Compute a SID to use for labeling a new object in the
  * class `tclass' based on a SID pair.
libsepol/src/services.c +6 -1
index 7becfd1b..b2fb804e 100644
@@ -1233,6 +1233,12 @@ out:
        return STATUS_ERR;
 }
 
+ const char *sepol_av_perm_to_string(sepol_security_class_t tclass,
+                                       sepol_access_vector_t av)
+{
+       return sepol_av_to_string(policydb, tclass, av);
+}
+
 /*
  * Write the security context string representation of
  * the context associated with `sid' into a dynamically
--

2.35.1

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

Add a wrapper around the utility function sepol_av_to_string() on the service internal policy. This allows callers to convert a permission bit set into a string representation without access to the internal policy structure.

Quoted 9 lines
Quoted 1 line

After thinking about it, I am ok with this series. All of the functions that are being exported are used by checkpolicy or audit2why and are from services.c.

So for these four patches:

Merged.
Thanks,
Jim

Quoted 45 lines
Quoted 45 lines
---
 libsepol/include/sepol/policydb/services.h | 9 +++++++++
 libsepol/src/services.c                    | 6 ++++++
 2 files changed, 15 insertions(+)
libsepol/include/sepol/policydb/services.h +9 -0
index 048f8a5a..44de3863 100644
@@ -103,6 +103,15 @@ extern int sepol_string_to_av_perm(sepol_security_class_t tclass,
                                        const char *perm_name,
                                        sepol_access_vector_t *av);
 
+/*
+ * Return a string representation of the permission av bit associated with
+ * tclass.
+ * Returns a pointer to an internal buffer, overridden by the next call to
+ * this function or sepol_av_to_string().
+ */
+ extern const char *sepol_av_perm_to_string(sepol_security_class_t tclass,
+                                       sepol_access_vector_t av);
+
 /*
  * Compute a SID to use for labeling a new object in the
  * class `tclass' based on a SID pair.
libsepol/src/services.c +6 -1
index 7becfd1b..b2fb804e 100644
@@ -1233,6 +1233,12 @@ out:
        return STATUS_ERR;
 }
 
+ const char *sepol_av_perm_to_string(sepol_security_class_t tclass,
+                                       sepol_access_vector_t av)
+{
+       return sepol_av_to_string(policydb, tclass, av);
+}
+
 /*
  * Write the security context string representation of
  * the context associated with `sid' into a dynamically
--

2.35.1