policydb.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * A policy database (policydb) specifies the
  3. * configuration data for the security policy.
  4. *
  5. * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
  6. */
  7. /*
  8. * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
  9. *
  10. * Support for enhanced MLS infrastructure.
  11. *
  12. * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
  13. *
  14. * Added conditional policy language extensions
  15. *
  16. * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
  17. * Copyright (C) 2003 - 2004 Tresys Technology, LLC
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation, version 2.
  21. */
  22. #ifndef _SS_POLICYDB_H_
  23. #define _SS_POLICYDB_H_
  24. #include <linux/flex_array.h>
  25. #include "symtab.h"
  26. #include "avtab.h"
  27. #include "sidtab.h"
  28. #include "ebitmap.h"
  29. #include "mls_types.h"
  30. #include "context.h"
  31. #include "constraint.h"
  32. /*
  33. * A datum type is defined for each kind of symbol
  34. * in the configuration data: individual permissions,
  35. * common prefixes for access vectors, classes,
  36. * users, roles, types, sensitivities, categories, etc.
  37. */
  38. /* Permission attributes */
  39. struct perm_datum {
  40. u32 value; /* permission bit + 1 */
  41. };
  42. /* Attributes of a common prefix for access vectors */
  43. struct common_datum {
  44. u32 value; /* internal common value */
  45. struct symtab permissions; /* common permissions */
  46. };
  47. /* Class attributes */
  48. struct class_datum {
  49. u32 value; /* class value */
  50. char *comkey; /* common name */
  51. struct common_datum *comdatum; /* common datum */
  52. struct symtab permissions; /* class-specific permission symbol table */
  53. struct constraint_node *constraints; /* constraints on class permissions */
  54. struct constraint_node *validatetrans; /* special transition rules */
  55. };
  56. /* Role attributes */
  57. struct role_datum {
  58. u32 value; /* internal role value */
  59. u32 bounds; /* boundary of role */
  60. struct ebitmap dominates; /* set of roles dominated by this role */
  61. struct ebitmap types; /* set of authorized types for role */
  62. };
  63. struct role_trans {
  64. u32 role; /* current role */
  65. u32 type; /* program executable type */
  66. u32 new_role; /* new role */
  67. struct role_trans *next;
  68. };
  69. struct role_allow {
  70. u32 role; /* current role */
  71. u32 new_role; /* new role */
  72. struct role_allow *next;
  73. };
  74. /* Type attributes */
  75. struct type_datum {
  76. u32 value; /* internal type value */
  77. u32 bounds; /* boundary of type */
  78. unsigned char primary; /* primary name? */
  79. unsigned char attribute;/* attribute ?*/
  80. };
  81. /* User attributes */
  82. struct user_datum {
  83. u32 value; /* internal user value */
  84. u32 bounds; /* bounds of user */
  85. struct ebitmap roles; /* set of authorized roles for user */
  86. struct mls_range range; /* MLS range (min - max) for user */
  87. struct mls_level dfltlevel; /* default login MLS level for user */
  88. };
  89. /* Sensitivity attributes */
  90. struct level_datum {
  91. struct mls_level *level; /* sensitivity and associated categories */
  92. unsigned char isalias; /* is this sensitivity an alias for another? */
  93. };
  94. /* Category attributes */
  95. struct cat_datum {
  96. u32 value; /* internal category bit + 1 */
  97. unsigned char isalias; /* is this category an alias for another? */
  98. };
  99. struct range_trans {
  100. u32 source_type;
  101. u32 target_type;
  102. u32 target_class;
  103. };
  104. /* Boolean data type */
  105. struct cond_bool_datum {
  106. __u32 value; /* internal type value */
  107. int state;
  108. };
  109. struct cond_node;
  110. /*
  111. * The configuration data includes security contexts for
  112. * initial SIDs, unlabeled file systems, TCP and UDP port numbers,
  113. * network interfaces, and nodes. This structure stores the
  114. * relevant data for one such entry. Entries of the same kind
  115. * (e.g. all initial SIDs) are linked together into a list.
  116. */
  117. struct ocontext {
  118. union {
  119. char *name; /* name of initial SID, fs, netif, fstype, path */
  120. struct {
  121. u8 protocol;
  122. u16 low_port;
  123. u16 high_port;
  124. } port; /* TCP or UDP port information */
  125. struct {
  126. u32 addr;
  127. u32 mask;
  128. } node; /* node information */
  129. struct {
  130. u32 addr[4];
  131. u32 mask[4];
  132. } node6; /* IPv6 node information */
  133. } u;
  134. union {
  135. u32 sclass; /* security class for genfs */
  136. u32 behavior; /* labeling behavior for fs_use */
  137. } v;
  138. struct context context[2]; /* security context(s) */
  139. u32 sid[2]; /* SID(s) */
  140. struct ocontext *next;
  141. };
  142. struct genfs {
  143. char *fstype;
  144. struct ocontext *head;
  145. struct genfs *next;
  146. };
  147. /* symbol table array indices */
  148. #define SYM_COMMONS 0
  149. #define SYM_CLASSES 1
  150. #define SYM_ROLES 2
  151. #define SYM_TYPES 3
  152. #define SYM_USERS 4
  153. #define SYM_BOOLS 5
  154. #define SYM_LEVELS 6
  155. #define SYM_CATS 7
  156. #define SYM_NUM 8
  157. /* object context array indices */
  158. #define OCON_ISID 0 /* initial SIDs */
  159. #define OCON_FS 1 /* unlabeled file systems */
  160. #define OCON_PORT 2 /* TCP and UDP port numbers */
  161. #define OCON_NETIF 3 /* network interfaces */
  162. #define OCON_NODE 4 /* nodes */
  163. #define OCON_FSUSE 5 /* fs_use */
  164. #define OCON_NODE6 6 /* IPv6 nodes */
  165. #define OCON_NUM 7
  166. /* The policy database */
  167. struct policydb {
  168. int mls_enabled;
  169. /* symbol tables */
  170. struct symtab symtab[SYM_NUM];
  171. #define p_commons symtab[SYM_COMMONS]
  172. #define p_classes symtab[SYM_CLASSES]
  173. #define p_roles symtab[SYM_ROLES]
  174. #define p_types symtab[SYM_TYPES]
  175. #define p_users symtab[SYM_USERS]
  176. #define p_bools symtab[SYM_BOOLS]
  177. #define p_levels symtab[SYM_LEVELS]
  178. #define p_cats symtab[SYM_CATS]
  179. /* symbol names indexed by (value - 1) */
  180. struct flex_array *sym_val_to_name[SYM_NUM];
  181. /* class, role, and user attributes indexed by (value - 1) */
  182. struct class_datum **class_val_to_struct;
  183. struct role_datum **role_val_to_struct;
  184. struct user_datum **user_val_to_struct;
  185. struct flex_array *type_val_to_struct_array;
  186. /* type enforcement access vectors and transitions */
  187. struct avtab te_avtab;
  188. /* role transitions */
  189. struct role_trans *role_tr;
  190. /* bools indexed by (value - 1) */
  191. struct cond_bool_datum **bool_val_to_struct;
  192. /* type enforcement conditional access vectors and transitions */
  193. struct avtab te_cond_avtab;
  194. /* linked list indexing te_cond_avtab by conditional */
  195. struct cond_node *cond_list;
  196. /* role allows */
  197. struct role_allow *role_allow;
  198. /* security contexts of initial SIDs, unlabeled file systems,
  199. TCP or UDP port numbers, network interfaces and nodes */
  200. struct ocontext *ocontexts[OCON_NUM];
  201. /* security contexts for files in filesystems that cannot support
  202. a persistent label mapping or use another
  203. fixed labeling behavior. */
  204. struct genfs *genfs;
  205. /* range transitions table (range_trans_key -> mls_range) */
  206. struct hashtab *range_tr;
  207. /* type -> attribute reverse mapping */
  208. struct flex_array *type_attr_map_array;
  209. struct ebitmap policycaps;
  210. struct ebitmap permissive_map;
  211. /* length of this policy when it was loaded */
  212. size_t len;
  213. unsigned int policyvers;
  214. unsigned int reject_unknown : 1;
  215. unsigned int allow_unknown : 1;
  216. u16 process_class;
  217. u32 process_trans_perms;
  218. };
  219. extern void policydb_destroy(struct policydb *p);
  220. extern int policydb_load_isids(struct policydb *p, struct sidtab *s);
  221. extern int policydb_context_isvalid(struct policydb *p, struct context *c);
  222. extern int policydb_class_isvalid(struct policydb *p, unsigned int class);
  223. extern int policydb_type_isvalid(struct policydb *p, unsigned int type);
  224. extern int policydb_role_isvalid(struct policydb *p, unsigned int role);
  225. extern int policydb_read(struct policydb *p, void *fp);
  226. extern int policydb_write(struct policydb *p, void *fp);
  227. #define PERM_SYMTAB_SIZE 32
  228. #define POLICYDB_CONFIG_MLS 1
  229. /* the config flags related to unknown classes/perms are bits 2 and 3 */
  230. #define REJECT_UNKNOWN 0x00000002
  231. #define ALLOW_UNKNOWN 0x00000004
  232. #define OBJECT_R "object_r"
  233. #define OBJECT_R_VAL 1
  234. #define POLICYDB_MAGIC SELINUX_MAGIC
  235. #define POLICYDB_STRING "SE Linux"
  236. struct policy_file {
  237. char *data;
  238. size_t len;
  239. };
  240. struct policy_data {
  241. struct policydb *p;
  242. void *fp;
  243. };
  244. static inline int next_entry(void *buf, struct policy_file *fp, size_t bytes)
  245. {
  246. if (bytes > fp->len)
  247. return -EINVAL;
  248. memcpy(buf, fp->data, bytes);
  249. fp->data += bytes;
  250. fp->len -= bytes;
  251. return 0;
  252. }
  253. static inline int put_entry(void *buf, size_t bytes, int num, struct policy_file *fp)
  254. {
  255. size_t len = bytes * num;
  256. memcpy(fp->data, buf, len);
  257. fp->data += len;
  258. fp->len -= len;
  259. return 0;
  260. }
  261. static inline char *sym_name(struct policydb *p, unsigned int sym_num, unsigned int element_nr)
  262. {
  263. struct flex_array *fa = p->sym_val_to_name[sym_num];
  264. return flex_array_get_ptr(fa, element_nr);
  265. }
  266. extern u16 string_to_security_class(struct policydb *p, const char *name);
  267. extern u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name);
  268. #endif /* _SS_POLICYDB_H_ */