libselinux: selinux_set_mapping: fix handling of unknown classes/perms
PATCH v2 1/1 libselinux: selinux_set_mapping: fix handling of unknown classes/perms
The libselinux selinux_set_mapping() implementation was never updated to handle unknown classes/permissions based on the policy handle_unknown flag. Update it and the internal mapping functions to gracefully handle unknown classes/permissions. Add a security_reject_unknown() interface to expose the corresponding selinuxfs node and use it when creating a mapping to decide whether to fail immediately or proceed.
This enables dbus-daemon and XSELinux, which use selinux_set_mapping(), to continue working with the dummy policy or other policies that lack their userspace class/permission definitions as long as the policy was built with -U allow.
---
v2 moves the call to security_deny_unknown() inside of map_decision(), thereby avoiding the overhead of calling it unless the caller has set a mapping.
libselinux/include/selinux/selinux.h | 5 +- libselinux/src/compute_av.c | 14 ++++-- libselinux/src/mapping.c | 70 ++++++++++++++++++++++------ libselinux/src/reject_unknown.c | 40 ++++++++++++++++ libselinux/src/selinux_internal.h | 1 + 5 files changed, 113 insertions(+), 17 deletions(-) create mode 100644 libselinux/src/reject_unknown.c
libselinux/include/selinux/selinux.h +4 -1
@@ -328,7 +328,10 @@ extern int security_getenforce(void);/* Set the enforce flag value. */ extern int security_setenforce (int value); -/* Get the behavior for undefined classes/permissions */ +/* Get the load-time behavior for undefined classes/permissions */ +extern int security_reject_unknown (void ); + +/* Get the runtime behavior for undefined classes/permissions */ extern int security_deny_unknown (void );/* Get the checkreqprot value */
libselinux/src/compute_av.c +11 -3
@@ -20,6 +20,7 @@ int security_compute_av_flags_raw(const char * scon,char *buf;size_t len;int fd, ret; +security_class_t kclass;if (!selinux_mnt) { errno =ENOENT ;
@@ -38,8 +39,9 @@ int security_compute_av_flags_raw(const char * scon, goto out; } + kclass =unmap_class (tclass);snprintf (buf, len,"%s %s %hu %x" , scon, tcon, -unmap_class (tclass),unmap_perm (tclass, requested)); + kclass,unmap_perm (tclass, requested)); ret =write (fd, buf,strlen (buf));if (ret <0 )
@@ -60,8 +62,14 @@ int security_compute_av_flags_raw(const char * scon, }else if (ret <6 ) avd->flags =0 ; -/* If tclass invalid, kernel sets avd according to deny_unknown flag */ -if (tclass !=0 ) +/* +* If the tclass could not be mapped to a kernel class at all, the +* kernel will have already set avd according to the +* handle_unknown flag and we do not need to do anything further. +* Otherwise, we must map the permissions within the returned +* avd to the userspace permission values. +*/ +if (kclass !=0 )map_decision (tclass, avd); ret =0 ;
libselinux/src/mapping.c +57 -13
120 lines not on this page · raw message
libselinux/src/reject_unknown.c new file +40 -0
@@ -0,0 +1,40 @@ +#include <unistd.h> +#include <sys/types.h> +#include <fcntl.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include "selinux_internal.h" +#include "policy.h" +#include <stdio.h> +#include <limits.h> + +int security_reject_unknown (void ) +{ +int fd, ret, reject_unknown =0 ; +char path[PATH_MAX ]; +char buf[20 ]; + +if (!selinux_mnt) { + errno =ENOENT ; +return -1 ; + } + +snprintf (path,sizeof (path),"%s/reject_unknown" , selinux_mnt); + fd =open (path,O_RDONLY |O_CLOEXEC ); +if (fd <0 ) +return -1 ; + +memset (buf,0 ,sizeof (buf)); + ret =read (fd, buf,sizeof (buf) -1 ); +close (fd); +if (ret <0 ) +return -1 ; + +if (sscanf (buf,"%d" , &reject_unknown) !=1 ) +return -1 ; + +return reject_unknown; +} + +hidden_def (security_reject_unknown);
libselinux/src/selinux_internal.h +1 -0
@@ -59,6 +59,7 @@ hidden_proto(selinux_mkload_policy) hidden_proto(security_getenforce) hidden_proto(security_setenforce) hidden_proto(security_deny_unknown) + hidden_proto(security_reject_unknown) hidden_proto(security_get_checkreqprot) hidden_proto(selinux_boolean_sub) hidden_proto(selinux_current_policy_path)
Signature · 1 line
2.20.1