selinux: dynamic class/perm discovery
PATCH v5 1/2 selinux: dynamic class/perm discovery
Modify SELinux to dynamically discover class and permission values upon policy load, based on the dynamic object class/perm discovery logic from libselinux. A mapping is created between kernel-private class and permission indices used outside the security server and the policy values used within the security server.
The mappings are only applied upon kernel-internal computations; similar mappings for the private indices of userspace object managers is handled on a per-object manager basis by the userspace AVC. The interfaces for compute_av and transition_sid are split for kernel vs. userspace; the userspace functions are distinguished by a _user suffix.
The kernel-private class indices are no longer tied to the policy values and thus do not need to skip indices for userspace classes; thus the kernel class index values are compressed. The flask.h definitions were regenerated by deleting the userspace classes from refpolicy's definitions and then regenerating the headers. Going forward, we can just maintain the flask.h, av_permissions.h, and classmap.h definitions separately from policy as they are no longer tied to the policy values. The next patch introduces a utility to automate generation of flask.h and av_permissions.h from the classmap.h definitions.
The older kernel class and permission string tables are removed and replaced by a single security class mapping table that is walked at policy load to generate the mapping. The old kernel class validation logic is completely replaced by the mapping logic.
The handle unknown logic is reworked. reject_unknown=1 is handled when the mappings are computed at policy load time, similar to the old handling by the class validation logic. allow_unknown=1 is handled when computing and mapping decisions - if the permission was not able to be mapped (i.e. undefined, mapped to zero), then it is automatically added to the allowed vector. If the class was not able to be mapped (i.e. undefined, mapped to zero), then all permissions are allowed for it if allow_unknown=1.
avc_audit leverages the new security class mapping table to lookup the class and permission names from the kernel-private indices.
The mdp program is updated to use the new table when generating the class definitions and allow rules for a minimal boot policy for the kernel. It should be noted that this policy will not include any userspace classes, nor will its policy index values for the kernel classes correspond with the ones in refpolicy (they will instead match the kernel-private indices).
---
This version changes the security server to lookup or map the policy values for classes and permissions it uses internally, like the process class and process transition permissions, rather than using the kernel-private indices. This is necessary to allow the kernel definitions to diverge from the policy definitions for those classes and permissions. Also the classmap.h definitions are refined to make the relationships among the file and socket classes more explicit and to split out the common ipc permissions.
scripts/selinux/mdp/mdp.c | 151 ++----- security/selinux/avc.c | 76 +--- security/selinux/include/av_inherit.h | 34 -- security/selinux/include/av_perm_to_string.h | 183 -------- security/selinux/include/av_permissions.h | 44 +- security/selinux/include/avc_ss.h | 21 +- security/selinux/include/class_to_string.h | 80 ---- security/selinux/include/classmap.h | 150 ++++++ security/selinux/include/common_perm_to_string.h | 58 --- security/selinux/include/flask.h | 40 +- security/selinux/include/security.h | 13 +- security/selinux/selinuxfs.c | 4 +- security/selinux/ss/mls.c | 2 +- security/selinux/ss/policydb.c | 47 ++- security/selinux/ss/policydb.h | 7 +- security/selinux/ss/services.c | 540 ++++++++++++---------- 16 files changed, 583 insertions(+), 867 deletions(-) delete mode 100644 security/selinux/include/av_inherit.h delete mode 100644 security/selinux/include/av_perm_to_string.h delete mode 100644 security/selinux/include/class_to_string.h create mode 100644 security/selinux/include/classmap.h delete mode 100644 security/selinux/include/common_perm_to_string.h
scripts/selinux/mdp/mdp.c +28 -123
@@ -29,86 +29,27 @@#include <unistd.h> #include <string.h> -#include "flask.h" -static void usage (char *name) {printf ("usage: %s [-m] policy_file context_file\n" , name);exit (1 ); } -static void find_common_name (char *cname,char *dest,int len) -{ -char *start, *end; - - start =strchr (cname,'_' )+1 ; - end =strchr (start,'_' ); -if (!start || !end || start-cname > len || end-start > len) { -printf ("Error with commons defines\n" ); -exit (1 ); - } -strncpy (dest, start, end-start); - dest[end-start] ='\0' ; -} - -#define S_ (x) x, -static char *classlist[] = { -#include "class_to_string.h" -NULL +/* Class/perm mapping support */ +struct security_class_mapping { +const char *name; +const char *perms[sizeof (unsigned ) *8 +1 ]; }; -#undef S_ +#include "classmap.h" #include "initial_sid_to_string.h" -#define TB_ (x) char *x[] = { -#define TE_ (x) NULL }; -#define S_ (x) x, -#include "common_perm_to_string.h" -#undef TB_ -#undef TE_ -#undef S_ - -struct common { -char *cname; -char **perms; -}; -struct common common[] = { -#define TB_(x) { #x, x }, -#define S_(x) -#define TE_ (x) -#include "common_perm_to_string.h" -#undef TB_ -#undef TE_ -#undef S_ -}; - -#define S_ (x, y, z) {x, #y}, -struct av_inherit { -int class; -char *common; -}; -struct av_inherit av_inherit[] = { -#include "av_inherit.h" -}; -#undef S_ - -#include "av_permissions.h" -#define S_ (x, y, z) {x, y, z}, -struct av_perms { -int class; -int perm_i; -char *perm_s; -}; -struct av_perms av_perms[] = { -#include "av_perm_to_string.h" -}; -#undef S_ -int main (int argc,char *argv[]) {int i, j, mls =0 ; +int initial_sid_to_string_len;char **arg, *polout, *ctxout; -int classlist_len, initial_sid_to_string_len; +FILE *fout;if (argc <3 )
@@ -127,64 +68,25 @@ int main(int argc, char *argv[])usage (argv[0 ]); } - classlist_len =sizeof (classlist) /sizeof (char *);/* print out the classes */ -for (i=1 ; i < classlist_len; i++) { -if (classlist[i]) -fprintf (fout,"class %s\n" , classlist[i]); -else -fprintf (fout,"class user%d\n" , i); - } +for (i =0 ; secclass_map[i].name; i++) +fprintf (fout,"class %s\n" , secclass_map[i].name);fprintf (fout,"\n" ); initial_sid_to_string_len =sizeof (initial_sid_to_string) /sizeof (char *);/* print out the sids */ -for (i=1 ; i < initial_sid_to_string_len; i++) +for (i =1 ; i < initial_sid_to_string_len; i++)fprintf (fout,"sid %s\n" , initial_sid_to_string[i]);fprintf (fout,"\n" ); -/* print out the commons */ -for (i=0 ; i<sizeof (common)/sizeof (struct common ); i++) { -char cname[101 ]; -find_common_name (common[i].cname, cname,100 ); - cname[100 ] ='\0' ; -fprintf (fout,"common %s\n{\n" , cname); -for (j=0 ; common[i].perms[j]; j++) -fprintf (fout,"\t%s\n" , common[i].perms[j]); -fprintf (fout,"}\n\n" ); - } -fprintf (fout,"\n" ); -/* print out the class permissions */ -for (i=1 ; i < classlist_len; i++) { -if (classlist[i]) { -int firstperm =-1 , numperms =0 ; - -fprintf (fout,"class %s\n" , classlist[i]); -/* does it inherit from a common? */ -for (j=0 ; j <sizeof (av_inherit)/sizeof (struct av_inherit ); j++) -if (av_inherit[j].class == i) -fprintf (fout,"inherits %s\n" , av_inherit[j].common); - -for (j=0 ; j <sizeof (av_perms)/sizeof (struct av_perms ); j++) { -if (av_perms[j].class == i) { -if (firstperm ==-1 ) - firstperm = j; - numperms++; - } - } -if (!numperms) { -fprintf (fout,"\n" ); -continue ; - } - -fprintf (fout,"{\n" ); -/* print out the av_perms */ -for (j=0 ; j < numperms; j++) { -fprintf (fout,"\t%s\n" , av_perms[firstperm+j].perm_s); - } -fprintf (fout,"}\n\n" ); - } +for (i =0 ; secclass_map[i].name; i++) { +struct security_class_mapping *map = &secclass_map[i]; +fprintf (fout,"class %s\n" , map->name); +fprintf (fout,"{\n" ); +for (j =0 ; map->perms[j]; j++) +fprintf (fout,"\t%s\n" , map->perms[j]); +fprintf (fout,"}\n\n" ); }fprintf (fout,"\n" );
@@ -197,31 +99,34 @@ int main(int argc, char *argv[])/* types, roles, and allows */ fprintf (fout,"type base_t;\n" );fprintf (fout,"role base_r types { base_t };\n" ); -for (i=1 ; i < classlist_len; i++) { -if (classlist[i]) -fprintf (fout,"allow base_t base_t:%s *;\n" , classlist[i]); -else -fprintf (fout,"allow base_t base_t:user%d *;\n" , i); - } +for (i =0 ; secclass_map[i].name; i++) +fprintf (fout,"allow base_t base_t:%s *;\n" , + secclass_map[i].name);fprintf (fout,"user user_u roles { base_r };\n" );fprintf (fout,"\n" );/* default sids */ -for (i=1 ; i < initial_sid_to_string_len; i++) +for (i =1 ; i < initial_sid_to_string_len; i++)fprintf (fout,"sid %s user_u:base_r:base_t\n" , initial_sid_to_string[i]);fprintf (fout,"\n" ); -fprintf (fout,"fs_use_xattr ext2 user_u:base_r:base_t;\n" );fprintf (fout,"fs_use_xattr ext3 user_u:base_r:base_t;\n" ); +fprintf (fout,"fs_use_xattr ext4 user_u:base_r:base_t;\n" );fprintf (fout,"fs_use_xattr jfs user_u:base_r:base_t;\n" );fprintf (fout,"fs_use_xattr xfs user_u:base_r:base_t;\n" );fprintf (fout,"fs_use_xattr reiserfs user_u:base_r:base_t;\n" ); +fprintf (fout,"fs_use_xattr jffs2 user_u:base_r:base_t;\n" ); +fprintf (fout,"fs_use_xattr gfs2 user_u:base_r:base_t;\n" ); +fprintf (fout,"fs_use_xattr lustre user_u:base_r:base_t;\n" ); +fprintf (fout,"fs_use_task eventpollfs user_u:base_r:base_t;\n" );fprintf (fout,"fs_use_task pipefs user_u:base_r:base_t;\n" );fprintf (fout,"fs_use_task sockfs user_u:base_r:base_t;\n" ); +fprintf (fout,"fs_use_trans mqueue user_u:base_r:base_t;\n" );fprintf (fout,"fs_use_trans devpts user_u:base_r:base_t;\n" ); +fprintf (fout,"fs_use_trans hugetlbfs user_u:base_r:base_t;\n" );fprintf (fout,"fs_use_trans tmpfs user_u:base_r:base_t;\n" );fprintf (fout,"fs_use_trans shm user_u:base_r:base_t;\n" );
security/selinux/avc.c +8 -68
111 lines not on this page · raw message
security/selinux/include/av_inherit.h deleted +0 -34
@@ -1,34 +0,0 @@ -/* This file is automatically generated. Do not edit. */ - S_(SECCLASS_DIR, file, 0x00020000UL) - S_(SECCLASS_FILE, file, 0x00020000UL) - S_(SECCLASS_LNK_FILE, file, 0x00020000UL) - S_(SECCLASS_CHR_FILE, file, 0x00020000UL) - S_(SECCLASS_BLK_FILE, file, 0x00020000UL) - S_(SECCLASS_SOCK_FILE, file, 0x00020000UL) - S_(SECCLASS_FIFO_FILE, file, 0x00020000UL) - S_(SECCLASS_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_TCP_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_UDP_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_RAWIP_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_NETLINK_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_PACKET_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_KEY_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_UNIX_STREAM_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_UNIX_DGRAM_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_TUN_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_IPC, ipc, 0x00000200UL) - S_(SECCLASS_SEM, ipc, 0x00000200UL) - S_(SECCLASS_MSGQ, ipc, 0x00000200UL) - S_(SECCLASS_SHM, ipc, 0x00000200UL) - S_(SECCLASS_NETLINK_ROUTE_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_NETLINK_FIREWALL_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_NETLINK_TCPDIAG_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_NETLINK_NFLOG_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_NETLINK_XFRM_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_NETLINK_SELINUX_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_NETLINK_AUDIT_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_NETLINK_IP6FW_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_NETLINK_DNRT_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET, socket, 0x00400000UL) - S_(SECCLASS_APPLETALK_SOCKET, socket, 0x00400000UL) -S_ (SECCLASS_DCCP_SOCKET , socket,0x00400000UL )
security/selinux/include/av_perm_to_string.h deleted +0 -183
183 lines not on this page · raw message
security/selinux/include/av_permissions.h +22 -22
53 lines not on this page · raw message
security/selinux/include/avc_ss.h +4 -17
@@ -10,26 +10,13 @@int avc_ss_reset (u32 seqno); -struct av_perm_to_string { -u16 tclass; -u32 value; +/* Class/perm mapping support */ +struct security_class_mapping {const char *name; +const char *perms[sizeof (u32) *8 +1 ]; }; -struct av_inherit { -const char **common_pts; -u32 common_base; -u16 tclass; -}; - -struct selinux_class_perm { -const struct av_perm_to_string *av_perm_to_string; -u32 av_pts_len; -u32 cts_len; -const char **class_to_string; -const struct av_inherit *av_inherit; -u32 av_inherit_len; -}; +extern struct security_class_mapping secclass_map[];#endif /* _SELINUX_AVC_SS_H_ */
security/selinux/include/class_to_string.h deleted +0 -80
80 lines not on this page · raw message
security/selinux/include/classmap.h new file +150 -0
150 lines not on this page · raw message
security/selinux/include/common_perm_to_string.h deleted +0 -58
58 lines not on this page · raw message
security/selinux/include/flask.h +20 -20
@@ -34,26 +34,26 @@#define SECCLASS_MSGQ 27#define SECCLASS_SHM 28#define SECCLASS_IPC 29 -#define SECCLASS_NETLINK_ROUTE_SOCKET 43 -#define SECCLASS_NETLINK_FIREWALL_SOCKET 44 -#define SECCLASS_NETLINK_TCPDIAG_SOCKET 45 -#define SECCLASS_NETLINK_NFLOG_SOCKET 46 -#define SECCLASS_NETLINK_XFRM_SOCKET 47 -#define SECCLASS_NETLINK_SELINUX_SOCKET 48 -#define SECCLASS_NETLINK_AUDIT_SOCKET 49 -#define SECCLASS_NETLINK_IP6FW_SOCKET 50 -#define SECCLASS_NETLINK_DNRT_SOCKET 51 -#define SECCLASS_ASSOCIATION 54 -#define SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET 55 -#define SECCLASS_APPLETALK_SOCKET 56 -#define SECCLASS_PACKET 57 -#define SECCLASS_KEY 58 -#define SECCLASS_DCCP_SOCKET 60 -#define SECCLASS_MEMPROTECT 61 -#define SECCLASS_PEER 68 -#define SECCLASS_CAPABILITY2 69 -#define SECCLASS_KERNEL_SERVICE 74 -#define SECCLASS_TUN_SOCKET 75 +#define SECCLASS_NETLINK_ROUTE_SOCKET 30 +#define SECCLASS_NETLINK_FIREWALL_SOCKET 31 +#define SECCLASS_NETLINK_TCPDIAG_SOCKET 32 +#define SECCLASS_NETLINK_NFLOG_SOCKET 33 +#define SECCLASS_NETLINK_XFRM_SOCKET 34 +#define SECCLASS_NETLINK_SELINUX_SOCKET 35 +#define SECCLASS_NETLINK_AUDIT_SOCKET 36 +#define SECCLASS_NETLINK_IP6FW_SOCKET 37 +#define SECCLASS_NETLINK_DNRT_SOCKET 38 +#define SECCLASS_ASSOCIATION 39 +#define SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET 40 +#define SECCLASS_APPLETALK_SOCKET 41 +#define SECCLASS_PACKET 42 +#define SECCLASS_KEY 43 +#define SECCLASS_DCCP_SOCKET 44 +#define SECCLASS_MEMPROTECT 45 +#define SECCLASS_PEER 46 +#define SECCLASS_CAPABILITY2 47 +#define SECCLASS_KERNEL_SERVICE 48 +#define SECCLASS_TUN_SOCKET 49/* * Security identifier indices for initial entities
security/selinux/include/security.h +10 -3
@@ -97,11 +97,18 @@ struct av_decision {#define AVD_FLAGS_PERMISSIVE 0x0001int security_compute_av (u32 ssid,u32 tsid, -u16 tclass,u32 requested, -struct av_decision *avd); +u16 tclass,u32 requested, +struct av_decision *avd); + +int security_compute_av_user (u32 ssid,u32 tsid, +u16 tclass,u32 requested, +struct av_decision *avd);int security_transition_sid (u32 ssid,u32 tsid, -u16 tclass,u32 *out_sid); +u16 tclass,u32 *out_sid); + +int security_transition_sid_user (u32 ssid,u32 tsid, +u16 tclass,u32 *out_sid);int security_member_sid (u32 ssid,u32 tsid,u16 tclass,u32 *out_sid);
security/selinux/selinuxfs.c +2 -2
@@ -522,7 +522,7 @@ static ssize_t sel_write_access(struct file *file, char *buf, size_t size)if (length <0 ) goto out2; - length =security_compute_av (ssid, tsid, tclass, req, &avd); + length =security_compute_av_user (ssid, tsid, tclass, req, &avd);if (length <0 ) goto out2;
@@ -571,7 +571,7 @@ static ssize_t sel_write_create(struct file *file, char *buf, size_t size)if (length <0 ) goto out2; - length =security_transition_sid (ssid, tsid, tclass, &newsid); + length =security_transition_sid_user (ssid, tsid, tclass, &newsid);if (length <0 ) goto out2;
security/selinux/ss/mls.c +1 -1
@@ -532,7 +532,7 @@ int mls_compute_sid(struct context *scontext, }/* Fallthrough */ case AVTAB_CHANGE : -if (tclass ==SECCLASS_PROCESS ) +if (tclass == policydb.process_class)/* Use the process MLS attributes. */ return mls_context_cpy (newcontext, scontext);else
security/selinux/ss/policydb.c +45 -2
71 lines not on this page · raw message
security/selinux/ss/policydb.h +6 -1
@@ -254,7 +254,9 @@ struct policydb { unsigned int reject_unknown : 1; unsigned int allow_unknown : 1; -u32 *undefined_perms; + +u16 process_class; +u32 process_trans_perms; };extern void policydb_destroy (struct policydb *p);
@@ -295,5 +297,8 @@ static inline int next_entry(void *buf, struct policy_file *fp, size_t bytes) return 0; } +extern u16 string_to_security_class (struct policydb *p,const char *name); +extern u32 string_to_av_perm (struct policydb *p,u16 tclass,const char *name); +#endif /* _SS_POLICYDB_H_ */
security/selinux/ss/services.c +287 -253
757 lines not on this page · raw message
Signature · 12 lines
1.6.2.5 -- Stephen Smalley National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
PATCH v5 2/2 selinux: generate flask headers during kernel build
Add a simple utility (scripts/selinux/genheaders) and invoke it to generate the kernel-private class and permission indices in flask.h and av_permissions.h automatically during the kernel build from the security class mapping definitions in classmap.h. Adding new kernel classes and permissions can then be done just by adding them to classmap.h.
---
This patch is unchanged from v4 but re-posted for completeness.
scripts/selinux/Makefile | 4 +- scripts/selinux/genheaders/Makefile | 5 + scripts/selinux/genheaders/genheaders.c | 118 ++++ security/selinux/Makefile | 10 +- security/selinux/include/av_permissions.h | 870 ----------------------------- security/selinux/include/flask.h | 91 --- security/selinux/ss/Makefile | 2 +- 7 files changed, 135 insertions(+), 965 deletions(-) create mode 100644 scripts/selinux/genheaders/Makefile create mode 100644 scripts/selinux/genheaders/genheaders.c delete mode 100644 security/selinux/include/av_permissions.h delete mode 100644 security/selinux/include/flask.h
scripts/selinux/Makefile +2 -2
@@ -1,2 +1,2 @@ -subdir-y := mdp -subdir- += mdp +subdir-y := mdp genheaders +subdir- += mdp genheaders
scripts/selinux/genheaders/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +hostprogs-y := genheaders +HOST_EXTRACFLAGS += -Isecurity/selinux/include + +always := $(hostprogs-y) +clean-files := $(hostprogs-y)
scripts/selinux/genheaders/genheaders.c new file +118 -0
118 lines not on this page · raw message
security/selinux/Makefile +9 -1
@@ -18,5 +18,13 @@ selinux-$(CONFIG_SECURITY_NETWORK_XFRM) += xfrm.o selinux-$(CONFIG_NETLABEL) += netlabel.o -EXTRA_CFLAGS += -Isecurity/selinux/include +EXTRA_CFLAGS += -Isecurity/selinux -Isecurity/selinux/include +$(obj)/avc.o: $(obj)/flask.h + +quiet_cmd_flask = GEN $(obj)/flask.h $(obj)/av_permissions.h + cmd_flask = scripts/selinux/genheaders/genheaders $(obj)/flask.h $(obj)/av_permissions.h + +targets += flask.h +$(obj)/flask.h: $(src)/include/classmap.h FORCE + $(call if_changed,flask)
security/selinux/include/av_permissions.h deleted +0 -870
870 lines not on this page · raw message
security/selinux/include/flask.h deleted +0 -91
91 lines not on this page · raw message
security/selinux/ss/Makefile +1 -1
@@ -2,7 +2,7 @@ # Makefile for building the SELinux security server as part of the kernel tree. # -EXTRA_CFLAGS += -Isecurity/selinux/include +EXTRA_CFLAGS += -Isecurity/selinux -Isecurity/selinux/include obj-y := ss.o ss-y := ebitmap.o hashtab.o symtab.o sidtab.o avtab.o policydb.o services.o conditional.o mls.o
Signature · 12 lines
1.6.2.5 -- Stephen Smalley National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 5 lines · Stephen Smalley
Modify SELinux to dynamically discover class and permission values upon policy load, based on the dynamic object class/perm discovery logic from libselinux. A mapping is created between kernel-private class and permission indices used outside the security server and the policy values used within the security server.
Both applied togit://git.kernel.org/pub/scm/linux/kernel/git/jmorris/security-testing-2.6#next
Signature · 7 lines
James Morris <[email protected]> -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.