policydb.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. /* Options how a new object user, role, and type should be decided */
  56. #define DEFAULT_SOURCE 1
  57. #define DEFAULT_TARGET 2
  58. char default_user;
  59. char default_role;
  60. char default_type;
  61. /* Options how a new object range should be decided */
  62. #define DEFAULT_SOURCE_LOW 1
  63. #define DEFAULT_SOURCE_HIGH 2
  64. #define DEFAULT_SOURCE_LOW_HIGH 3
  65. #define DEFAULT_TARGET_LOW 4
  66. #define DEFAULT_TARGET_HIGH 5
  67. #define DEFAULT_TARGET_LOW_HIGH 6
  68. char default_range;
  69. };
  70. /* Role attributes */
  71. struct role_datum {
  72. u32 value; /* internal role value */
  73. u32 bounds; /* boundary of role */
  74. struct ebitmap dominates; /* set of roles dominated by this role */
  75. struct ebitmap types; /* set of authorized types for role */
  76. };
  77. struct role_trans {
  78. u32 role; /* current role */
  79. u32 type; /* program executable type, or new object type */
  80. u32 tclass; /* process class, or new object class */
  81. u32 new_role; /* new role */
  82. struct role_trans *next;
  83. };
  84. struct filename_trans {
  85. u32 stype; /* current process */
  86. u32 ttype; /* parent dir context */
  87. u16 tclass; /* class of new object */
  88. const char *name; /* last path component */
  89. };
  90. struct filename_trans_datum {
  91. u32 otype; /* expected of new object */
  92. };
  93. struct role_allow {
  94. u32 role; /* current role */
  95. u32 new_role; /* new role */
  96. struct role_allow *next;
  97. };
  98. /* Type attributes */
  99. struct type_datum {
  100. u32 value; /* internal type value */
  101. u32 bounds; /* boundary of type */
  102. unsigned char primary; /* primary name? */
  103. unsigned char attribute;/* attribute ?*/
  104. };
  105. /* User attributes */
  106. struct user_datum {
  107. u32 value; /* internal user value */
  108. u32 bounds; /* bounds of user */
  109. struct ebitmap roles; /* set of authorized roles for user */
  110. struct mls_range range; /* MLS range (min - max) for user */
  111. struct mls_level dfltlevel; /* default login MLS level for user */
  112. };
  113. /* Sensitivity attributes */
  114. struct level_datum {
  115. struct mls_level *level; /* sensitivity and associated categories */
  116. unsigned char isalias; /* is this sensitivity an alias for another? */
  117. };
  118. /* Category attributes */
  119. struct cat_datum {
  120. u32 value; /* internal category bit + 1 */
  121. unsigned char isalias; /* is this category an alias for another? */
  122. };
  123. struct range_trans {
  124. u32 source_type;
  125. u32 target_type;
  126. u32 target_class;
  127. };
  128. /* Boolean data type */
  129. struct cond_bool_datum {
  130. __u32 value; /* internal type value */
  131. int state;
  132. };
  133. struct cond_node;
  134. /*
  135. * The configuration data includes security contexts for
  136. * initial SIDs, unlabeled file systems, TCP and UDP port numbers,
  137. * network interfaces, and nodes. This structure stores the
  138. * relevant data for one such entry. Entries of the same kind
  139. * (e.g. all initial SIDs) are linked together into a list.
  140. */
  141. struct ocontext {
  142. union {
  143. char *name; /* name of initial SID, fs, netif, fstype, path */
  144. struct {
  145. u8 protocol;
  146. u16 low_port;
  147. u16 high_port;
  148. } port; /* TCP or UDP port information */
  149. struct {
  150. u32 addr;
  151. u32 mask;
  152. } node; /* node information */
  153. struct {
  154. u32 addr[4];
  155. u32 mask[4];
  156. } node6; /* IPv6 node information */
  157. } u;
  158. union {
  159. u32 sclass; /* security class for genfs */
  160. u32 behavior; /* labeling behavior for fs_use */
  161. } v;
  162. struct context context[2]; /* security context(s) */
  163. u32 sid[2]; /* SID(s) */
  164. struct ocontext *next;
  165. };
  166. struct genfs {
  167. char *fstype;
  168. struct ocontext *head;
  169. struct genfs *next;
  170. };
  171. /* symbol table array indices */
  172. #define SYM_COMMONS 0
  173. #define SYM_CLASSES 1
  174. #define SYM_ROLES 2
  175. #define SYM_TYPES 3
  176. #define SYM_USERS 4
  177. #define SYM_BOOLS 5
  178. #define SYM_LEVELS 6
  179. #define SYM_CATS 7
  180. #define SYM_NUM 8
  181. /* object context array indices */
  182. #define OCON_ISID 0 /* initial SIDs */
  183. #define OCON_FS 1 /* unlabeled file systems */
  184. #define OCON_PORT 2 /* TCP and UDP port numbers */
  185. #define OCON_NETIF 3 /* network interfaces */
  186. #define OCON_NODE 4 /* nodes */
  187. #define OCON_FSUSE 5 /* fs_use */
  188. #define OCON_NODE6 6 /* IPv6 nodes */
  189. #define OCON_NUM 7
  190. /* The policy database */
  191. struct policydb {
  192. int mls_enabled;
  193. /* symbol tables */
  194. struct symtab symtab[SYM_NUM];
  195. #define p_commons symtab[SYM_COMMONS]
  196. #define p_classes symtab[SYM_CLASSES]
  197. #define p_roles symtab[SYM_ROLES]
  198. #define p_types symtab[SYM_TYPES]
  199. #define p_users symtab[SYM_USERS]
  200. #define p_bools symtab[SYM_BOOLS]
  201. #define p_levels symtab[SYM_LEVELS]
  202. #define p_cats symtab[SYM_CATS]
  203. /* symbol names indexed by (value - 1) */
  204. struct flex_array *sym_val_to_name[SYM_NUM];
  205. /* class, role, and user attributes indexed by (value - 1) */
  206. struct class_datum **class_val_to_struct;
  207. struct role_datum **role_val_to_struct;
  208. struct user_datum **user_val_to_struct;
  209. struct flex_array *type_val_to_struct_array;
  210. /* type enforcement access vectors and transitions */
  211. struct avtab te_avtab;
  212. /* role transitions */
  213. struct role_trans *role_tr;
  214. /* file transitions with the last path component */
  215. /* quickly exclude lookups when parent ttype has no rules */
  216. struct ebitmap filename_trans_ttypes;
  217. /* actual set of filename_trans rules */
  218. struct hashtab *filename_trans;
  219. /* bools indexed by (value - 1) */
  220. struct cond_bool_datum **bool_val_to_struct;
  221. /* type enforcement conditional access vectors and transitions */
  222. struct avtab te_cond_avtab;
  223. /* linked list indexing te_cond_avtab by conditional */
  224. struct cond_node *cond_list;
  225. /* role allows */
  226. struct role_allow *role_allow;
  227. /* security contexts of initial SIDs, unlabeled file systems,
  228. TCP or UDP port numbers, network interfaces and nodes */
  229. struct ocontext *ocontexts[OCON_NUM];
  230. /* security contexts for files in filesystems that cannot support
  231. a persistent label mapping or use another
  232. fixed labeling behavior. */
  233. struct genfs *genfs;
  234. /* range transitions table (range_trans_key -> mls_range) */
  235. struct hashtab *range_tr;
  236. /* type -> attribute reverse mapping */
  237. struct flex_array *type_attr_map_array;
  238. struct ebitmap policycaps;
  239. struct ebitmap permissive_map;
  240. /* length of this policy when it was loaded */
  241. size_t len;
  242. unsigned int policyvers;
  243. unsigned int reject_unknown : 1;
  244. unsigned int allow_unknown : 1;
  245. u16 process_class;
  246. u32 process_trans_perms;
  247. };
  248. extern void policydb_destroy(struct policydb *p);
  249. extern int policydb_load_isids(struct policydb *p, struct sidtab *s);
  250. extern int policydb_context_isvalid(struct policydb *p, struct context *c);
  251. extern int policydb_class_isvalid(struct policydb *p, unsigned int class);
  252. extern int policydb_type_isvalid(struct policydb *p, unsigned int type);
  253. extern int policydb_role_isvalid(struct policydb *p, unsigned int role);
  254. extern int policydb_read(struct policydb *p, void *fp);
  255. extern int policydb_write(struct policydb *p, void *fp);
  256. #define PERM_SYMTAB_SIZE 32
  257. #define POLICYDB_CONFIG_MLS 1
  258. /* the config flags related to unknown classes/perms are bits 2 and 3 */
  259. #define REJECT_UNKNOWN 0x00000002
  260. #define ALLOW_UNKNOWN 0x00000004
  261. #define OBJECT_R "object_r"
  262. #define OBJECT_R_VAL 1
  263. #define POLICYDB_MAGIC SELINUX_MAGIC
  264. #define POLICYDB_STRING "SE Linux"
  265. struct policy_file {
  266. char *data;
  267. size_t len;
  268. };
  269. struct policy_data {
  270. struct policydb *p;
  271. void *fp;
  272. };
  273. static inline int next_entry(void *buf, struct policy_file *fp, size_t bytes)
  274. {
  275. if (bytes > fp->len)
  276. return -EINVAL;
  277. memcpy(buf, fp->data, bytes);
  278. fp->data += bytes;
  279. fp->len -= bytes;
  280. return 0;
  281. }
  282. static inline int put_entry(const void *buf, size_t bytes, int num, struct policy_file *fp)
  283. {
  284. size_t len = bytes * num;
  285. memcpy(fp->data, buf, len);
  286. fp->data += len;
  287. fp->len -= len;
  288. return 0;
  289. }
  290. static inline char *sym_name(struct policydb *p, unsigned int sym_num, unsigned int element_nr)
  291. {
  292. struct flex_array *fa = p->sym_val_to_name[sym_num];
  293. return flex_array_get_ptr(fa, element_nr);
  294. }
  295. extern u16 string_to_security_class(struct policydb *p, const char *name);
  296. extern u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name);
  297. #endif /* _SS_POLICYDB_H_ */