policydb.h 8.1 KB

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