policydb.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * A policy database (policydb) specifies the
  4. * configuration data for the security policy.
  5. *
  6. * Author : Stephen Smalley, <[email protected]>
  7. */
  8. /*
  9. * Updated: Trusted Computer Solutions, Inc. <[email protected]>
  10. *
  11. * Support for enhanced MLS infrastructure.
  12. *
  13. * Updated: Frank Mayer <[email protected]> and Karl MacMillan <[email protected]>
  14. *
  15. * Added conditional policy language extensions
  16. *
  17. * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
  18. * Copyright (C) 2003 - 2004 Tresys Technology, LLC
  19. */
  20. #ifndef _SS_POLICYDB_H_
  21. #define _SS_POLICYDB_H_
  22. #include "symtab.h"
  23. #include "avtab.h"
  24. #include "sidtab.h"
  25. #include "ebitmap.h"
  26. #include "mls_types.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. /* Options how a new object user, role, and type should be decided */
  53. #define DEFAULT_SOURCE 1
  54. #define DEFAULT_TARGET 2
  55. char default_user;
  56. char default_role;
  57. char default_type;
  58. /* Options how a new object range should be decided */
  59. #define DEFAULT_SOURCE_LOW 1
  60. #define DEFAULT_SOURCE_HIGH 2
  61. #define DEFAULT_SOURCE_LOW_HIGH 3
  62. #define DEFAULT_TARGET_LOW 4
  63. #define DEFAULT_TARGET_HIGH 5
  64. #define DEFAULT_TARGET_LOW_HIGH 6
  65. #define DEFAULT_GLBLUB 7
  66. char default_range;
  67. };
  68. /* Role attributes */
  69. struct role_datum {
  70. u32 value; /* internal role value */
  71. u32 bounds; /* boundary of role */
  72. struct ebitmap dominates; /* set of roles dominated by this role */
  73. struct ebitmap types; /* set of authorized types for role */
  74. };
  75. struct role_trans_key {
  76. u32 role; /* current role */
  77. u32 type; /* program executable type, or new object type */
  78. u32 tclass; /* process class, or new object class */
  79. };
  80. struct role_trans_datum {
  81. u32 new_role; /* new role */
  82. };
  83. struct filename_trans_key {
  84. u32 ttype; /* parent dir context */
  85. u16 tclass; /* class of new object */
  86. const char *name; /* last path component */
  87. };
  88. struct filename_trans_datum {
  89. struct ebitmap stypes; /* bitmap of source types for this otype */
  90. u32 otype; /* resulting type of new object */
  91. struct filename_trans_datum *next; /* record for next otype*/
  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. * type set preserves data needed to determine constraint info from
  136. * policy source. This is not used by the kernel policy but allows
  137. * utilities such as audit2allow to determine constraint denials.
  138. */
  139. struct type_set {
  140. struct ebitmap types;
  141. struct ebitmap negset;
  142. u32 flags;
  143. };
  144. /*
  145. * The configuration data includes security contexts for
  146. * initial SIDs, unlabeled file systems, TCP and UDP port numbers,
  147. * network interfaces, and nodes. This structure stores the
  148. * relevant data for one such entry. Entries of the same kind
  149. * (e.g. all initial SIDs) are linked together into a list.
  150. */
  151. struct ocontext {
  152. union {
  153. char *name; /* name of initial SID, fs, netif, fstype, path */
  154. struct {
  155. u8 protocol;
  156. u16 low_port;
  157. u16 high_port;
  158. } port; /* TCP or UDP port information */
  159. struct {
  160. u32 addr;
  161. u32 mask;
  162. } node; /* node information */
  163. struct {
  164. u32 addr[4];
  165. u32 mask[4];
  166. } node6; /* IPv6 node information */
  167. struct {
  168. u64 subnet_prefix;
  169. u16 low_pkey;
  170. u16 high_pkey;
  171. } ibpkey;
  172. struct {
  173. char *dev_name;
  174. u8 port;
  175. } ibendport;
  176. } u;
  177. union {
  178. u32 sclass; /* security class for genfs */
  179. u32 behavior; /* labeling behavior for fs_use */
  180. } v;
  181. struct context context[2]; /* security context(s) */
  182. u32 sid[2]; /* SID(s) */
  183. struct ocontext *next;
  184. };
  185. struct genfs {
  186. char *fstype;
  187. struct ocontext *head;
  188. struct genfs *next;
  189. };
  190. /* symbol table array indices */
  191. #define SYM_COMMONS 0
  192. #define SYM_CLASSES 1
  193. #define SYM_ROLES 2
  194. #define SYM_TYPES 3
  195. #define SYM_USERS 4
  196. #define SYM_BOOLS 5
  197. #define SYM_LEVELS 6
  198. #define SYM_CATS 7
  199. #define SYM_NUM 8
  200. /* object context array indices */
  201. #define OCON_ISID 0 /* initial SIDs */
  202. #define OCON_FS 1 /* unlabeled file systems */
  203. #define OCON_PORT 2 /* TCP and UDP port numbers */
  204. #define OCON_NETIF 3 /* network interfaces */
  205. #define OCON_NODE 4 /* nodes */
  206. #define OCON_FSUSE 5 /* fs_use */
  207. #define OCON_NODE6 6 /* IPv6 nodes */
  208. #define OCON_IBPKEY 7 /* Infiniband PKeys */
  209. #define OCON_IBENDPORT 8 /* Infiniband end ports */
  210. #define OCON_NUM 9
  211. /* The policy database */
  212. struct policydb {
  213. int mls_enabled;
  214. int android_netlink_route;
  215. int android_netlink_getneigh;
  216. /* symbol tables */
  217. struct symtab symtab[SYM_NUM];
  218. #define p_commons symtab[SYM_COMMONS]
  219. #define p_classes symtab[SYM_CLASSES]
  220. #define p_roles symtab[SYM_ROLES]
  221. #define p_types symtab[SYM_TYPES]
  222. #define p_users symtab[SYM_USERS]
  223. #define p_bools symtab[SYM_BOOLS]
  224. #define p_levels symtab[SYM_LEVELS]
  225. #define p_cats symtab[SYM_CATS]
  226. /* symbol names indexed by (value - 1) */
  227. char **sym_val_to_name[SYM_NUM];
  228. /* class, role, and user attributes indexed by (value - 1) */
  229. struct class_datum **class_val_to_struct;
  230. struct role_datum **role_val_to_struct;
  231. struct user_datum **user_val_to_struct;
  232. struct type_datum **type_val_to_struct;
  233. /* type enforcement access vectors and transitions */
  234. struct avtab te_avtab;
  235. /* role transitions */
  236. struct hashtab role_tr;
  237. /* file transitions with the last path component */
  238. /* quickly exclude lookups when parent ttype has no rules */
  239. struct ebitmap filename_trans_ttypes;
  240. /* actual set of filename_trans rules */
  241. struct hashtab filename_trans;
  242. /* only used if policyvers < POLICYDB_VERSION_COMP_FTRANS */
  243. u32 compat_filename_trans_count;
  244. /* bools indexed by (value - 1) */
  245. struct cond_bool_datum **bool_val_to_struct;
  246. /* type enforcement conditional access vectors and transitions */
  247. struct avtab te_cond_avtab;
  248. /* array indexing te_cond_avtab by conditional */
  249. struct cond_node *cond_list;
  250. u32 cond_list_len;
  251. /* role allows */
  252. struct role_allow *role_allow;
  253. /* security contexts of initial SIDs, unlabeled file systems,
  254. TCP or UDP port numbers, network interfaces and nodes */
  255. struct ocontext *ocontexts[OCON_NUM];
  256. /* security contexts for files in filesystems that cannot support
  257. a persistent label mapping or use another
  258. fixed labeling behavior. */
  259. struct genfs *genfs;
  260. /* range transitions table (range_trans_key -> mls_range) */
  261. struct hashtab range_tr;
  262. /* type -> attribute reverse mapping */
  263. struct ebitmap *type_attr_map_array;
  264. struct ebitmap policycaps;
  265. struct ebitmap permissive_map;
  266. /* length of this policy when it was loaded */
  267. size_t len;
  268. unsigned int policyvers;
  269. unsigned int reject_unknown : 1;
  270. unsigned int allow_unknown : 1;
  271. u16 process_class;
  272. u32 process_trans_perms;
  273. } __randomize_layout;
  274. extern void policydb_destroy(struct policydb *p);
  275. extern int policydb_load_isids(struct policydb *p, struct sidtab *s);
  276. extern int policydb_context_isvalid(struct policydb *p, struct context *c);
  277. extern int policydb_class_isvalid(struct policydb *p, unsigned int class);
  278. extern int policydb_type_isvalid(struct policydb *p, unsigned int type);
  279. extern int policydb_role_isvalid(struct policydb *p, unsigned int role);
  280. extern int policydb_read(struct policydb *p, void *fp);
  281. extern int policydb_write(struct policydb *p, void *fp);
  282. extern struct filename_trans_datum *policydb_filenametr_search(
  283. struct policydb *p, struct filename_trans_key *key);
  284. extern struct mls_range *policydb_rangetr_search(
  285. struct policydb *p, struct range_trans *key);
  286. extern struct role_trans_datum *policydb_roletr_search(
  287. struct policydb *p, struct role_trans_key *key);
  288. #define POLICYDB_CONFIG_MLS 1
  289. #define POLICYDB_CONFIG_ANDROID_NETLINK_ROUTE (1 << 31)
  290. #define POLICYDB_CONFIG_ANDROID_NETLINK_GETNEIGH (1 << 30)
  291. /* the config flags related to unknown classes/perms are bits 2 and 3 */
  292. #define REJECT_UNKNOWN 0x00000002
  293. #define ALLOW_UNKNOWN 0x00000004
  294. #define OBJECT_R "object_r"
  295. #define OBJECT_R_VAL 1
  296. #define POLICYDB_MAGIC SELINUX_MAGIC
  297. #define POLICYDB_STRING "SE Linux"
  298. struct policy_file {
  299. char *data;
  300. size_t len;
  301. };
  302. struct policy_data {
  303. struct policydb *p;
  304. void *fp;
  305. };
  306. static inline int next_entry(void *buf, struct policy_file *fp, size_t bytes)
  307. {
  308. if (bytes > fp->len)
  309. return -EINVAL;
  310. memcpy(buf, fp->data, bytes);
  311. fp->data += bytes;
  312. fp->len -= bytes;
  313. return 0;
  314. }
  315. static inline int put_entry(const void *buf, size_t bytes, int num, struct policy_file *fp)
  316. {
  317. size_t len = bytes * num;
  318. if (len > fp->len)
  319. return -EINVAL;
  320. memcpy(fp->data, buf, len);
  321. fp->data += len;
  322. fp->len -= len;
  323. return 0;
  324. }
  325. static inline char *sym_name(struct policydb *p, unsigned int sym_num, unsigned int element_nr)
  326. {
  327. return p->sym_val_to_name[sym_num][element_nr];
  328. }
  329. extern u16 string_to_security_class(struct policydb *p, const char *name);
  330. extern u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name);
  331. #endif /* _SS_POLICYDB_H_ */