security.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Security server interface.
  4. *
  5. * Author : Stephen Smalley, <sds@tycho.nsa.gov>
  6. *
  7. */
  8. #ifndef _SELINUX_SECURITY_H_
  9. #define _SELINUX_SECURITY_H_
  10. #include <linux/compiler.h>
  11. #include <linux/dcache.h>
  12. #include <linux/magic.h>
  13. #include <linux/types.h>
  14. #include <linux/refcount.h>
  15. #include <linux/workqueue.h>
  16. #include "flask.h"
  17. #define SECSID_NULL 0x00000000 /* unspecified SID */
  18. #define SECSID_WILD 0xffffffff /* wildcard SID */
  19. #define SECCLASS_NULL 0x0000 /* no class */
  20. /* Identify specific policy version changes */
  21. #define POLICYDB_VERSION_BASE 15
  22. #define POLICYDB_VERSION_BOOL 16
  23. #define POLICYDB_VERSION_IPV6 17
  24. #define POLICYDB_VERSION_NLCLASS 18
  25. #define POLICYDB_VERSION_VALIDATETRANS 19
  26. #define POLICYDB_VERSION_MLS 19
  27. #define POLICYDB_VERSION_AVTAB 20
  28. #define POLICYDB_VERSION_RANGETRANS 21
  29. #define POLICYDB_VERSION_POLCAP 22
  30. #define POLICYDB_VERSION_PERMISSIVE 23
  31. #define POLICYDB_VERSION_BOUNDARY 24
  32. #define POLICYDB_VERSION_FILENAME_TRANS 25
  33. #define POLICYDB_VERSION_ROLETRANS 26
  34. #define POLICYDB_VERSION_NEW_OBJECT_DEFAULTS 27
  35. #define POLICYDB_VERSION_DEFAULT_TYPE 28
  36. #define POLICYDB_VERSION_CONSTRAINT_NAMES 29
  37. #define POLICYDB_VERSION_XPERMS_IOCTL 30
  38. #define POLICYDB_VERSION_INFINIBAND 31
  39. /* Range of policy versions we understand*/
  40. #define POLICYDB_VERSION_MIN POLICYDB_VERSION_BASE
  41. #define POLICYDB_VERSION_MAX POLICYDB_VERSION_INFINIBAND
  42. /* Mask for just the mount related flags */
  43. #define SE_MNTMASK 0x0f
  44. /* Super block security struct flags for mount options */
  45. /* BE CAREFUL, these need to be the low order bits for selinux_get_mnt_opts */
  46. #define CONTEXT_MNT 0x01
  47. #define FSCONTEXT_MNT 0x02
  48. #define ROOTCONTEXT_MNT 0x04
  49. #define DEFCONTEXT_MNT 0x08
  50. #define SBLABEL_MNT 0x10
  51. /* Non-mount related flags */
  52. #define SE_SBINITIALIZED 0x0100
  53. #define SE_SBPROC 0x0200
  54. #define SE_SBGENFS 0x0400
  55. #define CONTEXT_STR "context="
  56. #define FSCONTEXT_STR "fscontext="
  57. #define ROOTCONTEXT_STR "rootcontext="
  58. #define DEFCONTEXT_STR "defcontext="
  59. #define LABELSUPP_STR "seclabel"
  60. struct netlbl_lsm_secattr;
  61. extern int selinux_enabled;
  62. /* Policy capabilities */
  63. enum {
  64. POLICYDB_CAPABILITY_NETPEER,
  65. POLICYDB_CAPABILITY_OPENPERM,
  66. POLICYDB_CAPABILITY_EXTSOCKCLASS,
  67. POLICYDB_CAPABILITY_ALWAYSNETWORK,
  68. POLICYDB_CAPABILITY_CGROUPSECLABEL,
  69. POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION,
  70. __POLICYDB_CAPABILITY_MAX
  71. };
  72. #define POLICYDB_CAPABILITY_MAX (__POLICYDB_CAPABILITY_MAX - 1)
  73. extern char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX];
  74. /*
  75. * type_datum properties
  76. * available at the kernel policy version >= POLICYDB_VERSION_BOUNDARY
  77. */
  78. #define TYPEDATUM_PROPERTY_PRIMARY 0x0001
  79. #define TYPEDATUM_PROPERTY_ATTRIBUTE 0x0002
  80. /* limitation of boundary depth */
  81. #define POLICYDB_BOUNDS_MAXDEPTH 4
  82. struct selinux_avc;
  83. struct selinux_ss;
  84. struct selinux_state {
  85. bool disabled;
  86. #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
  87. bool enforcing;
  88. #endif
  89. bool checkreqprot;
  90. bool initialized;
  91. bool policycap[__POLICYDB_CAPABILITY_MAX];
  92. struct selinux_avc *avc;
  93. struct selinux_ss *ss;
  94. };
  95. void selinux_ss_init(struct selinux_ss **ss);
  96. void selinux_avc_init(struct selinux_avc **avc);
  97. extern struct selinux_state selinux_state;
  98. #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
  99. static inline bool enforcing_enabled(struct selinux_state *state)
  100. {
  101. return state->enforcing;
  102. }
  103. static inline void enforcing_set(struct selinux_state *state, bool value)
  104. {
  105. state->enforcing = value;
  106. }
  107. #else
  108. static inline bool enforcing_enabled(struct selinux_state *state)
  109. {
  110. return true;
  111. }
  112. static inline void enforcing_set(struct selinux_state *state, bool value)
  113. {
  114. }
  115. #endif
  116. static inline bool selinux_policycap_netpeer(void)
  117. {
  118. struct selinux_state *state = &selinux_state;
  119. return state->policycap[POLICYDB_CAPABILITY_NETPEER];
  120. }
  121. static inline bool selinux_policycap_openperm(void)
  122. {
  123. struct selinux_state *state = &selinux_state;
  124. return state->policycap[POLICYDB_CAPABILITY_OPENPERM];
  125. }
  126. static inline bool selinux_policycap_extsockclass(void)
  127. {
  128. struct selinux_state *state = &selinux_state;
  129. return state->policycap[POLICYDB_CAPABILITY_EXTSOCKCLASS];
  130. }
  131. static inline bool selinux_policycap_alwaysnetwork(void)
  132. {
  133. struct selinux_state *state = &selinux_state;
  134. return state->policycap[POLICYDB_CAPABILITY_ALWAYSNETWORK];
  135. }
  136. static inline bool selinux_policycap_cgroupseclabel(void)
  137. {
  138. struct selinux_state *state = &selinux_state;
  139. return state->policycap[POLICYDB_CAPABILITY_CGROUPSECLABEL];
  140. }
  141. static inline bool selinux_policycap_nnp_nosuid_transition(void)
  142. {
  143. struct selinux_state *state = &selinux_state;
  144. return state->policycap[POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION];
  145. }
  146. int security_mls_enabled(struct selinux_state *state);
  147. int security_load_policy(struct selinux_state *state,
  148. void *data, size_t len);
  149. int security_read_policy(struct selinux_state *state,
  150. void **data, size_t *len);
  151. size_t security_policydb_len(struct selinux_state *state);
  152. int security_policycap_supported(struct selinux_state *state,
  153. unsigned int req_cap);
  154. #define SEL_VEC_MAX 32
  155. struct av_decision {
  156. u32 allowed;
  157. u32 auditallow;
  158. u32 auditdeny;
  159. u32 seqno;
  160. u32 flags;
  161. };
  162. #define XPERMS_ALLOWED 1
  163. #define XPERMS_AUDITALLOW 2
  164. #define XPERMS_DONTAUDIT 4
  165. #define security_xperm_set(perms, x) (perms[x >> 5] |= 1 << (x & 0x1f))
  166. #define security_xperm_test(perms, x) (1 & (perms[x >> 5] >> (x & 0x1f)))
  167. struct extended_perms_data {
  168. u32 p[8];
  169. };
  170. struct extended_perms_decision {
  171. u8 used;
  172. u8 driver;
  173. struct extended_perms_data *allowed;
  174. struct extended_perms_data *auditallow;
  175. struct extended_perms_data *dontaudit;
  176. };
  177. struct extended_perms {
  178. u16 len; /* length associated decision chain */
  179. struct extended_perms_data drivers; /* flag drivers that are used */
  180. };
  181. /* definitions of av_decision.flags */
  182. #define AVD_FLAGS_PERMISSIVE 0x0001
  183. void security_compute_av(struct selinux_state *state,
  184. u32 ssid, u32 tsid,
  185. u16 tclass, struct av_decision *avd,
  186. struct extended_perms *xperms);
  187. void security_compute_xperms_decision(struct selinux_state *state,
  188. u32 ssid, u32 tsid, u16 tclass,
  189. u8 driver,
  190. struct extended_perms_decision *xpermd);
  191. void security_compute_av_user(struct selinux_state *state,
  192. u32 ssid, u32 tsid,
  193. u16 tclass, struct av_decision *avd);
  194. int security_transition_sid(struct selinux_state *state,
  195. u32 ssid, u32 tsid, u16 tclass,
  196. const struct qstr *qstr, u32 *out_sid);
  197. int security_transition_sid_user(struct selinux_state *state,
  198. u32 ssid, u32 tsid, u16 tclass,
  199. const char *objname, u32 *out_sid);
  200. int security_member_sid(struct selinux_state *state, u32 ssid, u32 tsid,
  201. u16 tclass, u32 *out_sid);
  202. int security_change_sid(struct selinux_state *state, u32 ssid, u32 tsid,
  203. u16 tclass, u32 *out_sid);
  204. int security_sid_to_context(struct selinux_state *state, u32 sid,
  205. char **scontext, u32 *scontext_len);
  206. int security_sid_to_context_force(struct selinux_state *state,
  207. u32 sid, char **scontext, u32 *scontext_len);
  208. int security_context_to_sid(struct selinux_state *state,
  209. const char *scontext, u32 scontext_len,
  210. u32 *out_sid, gfp_t gfp);
  211. int security_context_str_to_sid(struct selinux_state *state,
  212. const char *scontext, u32 *out_sid, gfp_t gfp);
  213. int security_context_to_sid_default(struct selinux_state *state,
  214. const char *scontext, u32 scontext_len,
  215. u32 *out_sid, u32 def_sid, gfp_t gfp_flags);
  216. int security_context_to_sid_force(struct selinux_state *state,
  217. const char *scontext, u32 scontext_len,
  218. u32 *sid);
  219. int security_get_user_sids(struct selinux_state *state,
  220. u32 callsid, char *username,
  221. u32 **sids, u32 *nel);
  222. int security_port_sid(struct selinux_state *state,
  223. u8 protocol, u16 port, u32 *out_sid);
  224. int security_ib_pkey_sid(struct selinux_state *state,
  225. u64 subnet_prefix, u16 pkey_num, u32 *out_sid);
  226. int security_ib_endport_sid(struct selinux_state *state,
  227. const char *dev_name, u8 port_num, u32 *out_sid);
  228. int security_netif_sid(struct selinux_state *state,
  229. char *name, u32 *if_sid);
  230. int security_node_sid(struct selinux_state *state,
  231. u16 domain, void *addr, u32 addrlen,
  232. u32 *out_sid);
  233. int security_validate_transition(struct selinux_state *state,
  234. u32 oldsid, u32 newsid, u32 tasksid,
  235. u16 tclass);
  236. int security_validate_transition_user(struct selinux_state *state,
  237. u32 oldsid, u32 newsid, u32 tasksid,
  238. u16 tclass);
  239. int security_bounded_transition(struct selinux_state *state,
  240. u32 oldsid, u32 newsid);
  241. int security_sid_mls_copy(struct selinux_state *state,
  242. u32 sid, u32 mls_sid, u32 *new_sid);
  243. int security_net_peersid_resolve(struct selinux_state *state,
  244. u32 nlbl_sid, u32 nlbl_type,
  245. u32 xfrm_sid,
  246. u32 *peer_sid);
  247. int security_get_classes(struct selinux_state *state,
  248. char ***classes, int *nclasses);
  249. int security_get_permissions(struct selinux_state *state,
  250. char *class, char ***perms, int *nperms);
  251. int security_get_reject_unknown(struct selinux_state *state);
  252. int security_get_allow_unknown(struct selinux_state *state);
  253. #define SECURITY_FS_USE_XATTR 1 /* use xattr */
  254. #define SECURITY_FS_USE_TRANS 2 /* use transition SIDs, e.g. devpts/tmpfs */
  255. #define SECURITY_FS_USE_TASK 3 /* use task SIDs, e.g. pipefs/sockfs */
  256. #define SECURITY_FS_USE_GENFS 4 /* use the genfs support */
  257. #define SECURITY_FS_USE_NONE 5 /* no labeling support */
  258. #define SECURITY_FS_USE_MNTPOINT 6 /* use mountpoint labeling */
  259. #define SECURITY_FS_USE_NATIVE 7 /* use native label support */
  260. #define SECURITY_FS_USE_MAX 7 /* Highest SECURITY_FS_USE_XXX */
  261. int security_fs_use(struct selinux_state *state, struct super_block *sb);
  262. int security_genfs_sid(struct selinux_state *state,
  263. const char *fstype, char *name, u16 sclass,
  264. u32 *sid);
  265. #ifdef CONFIG_NETLABEL
  266. int security_netlbl_secattr_to_sid(struct selinux_state *state,
  267. struct netlbl_lsm_secattr *secattr,
  268. u32 *sid);
  269. int security_netlbl_sid_to_secattr(struct selinux_state *state,
  270. u32 sid,
  271. struct netlbl_lsm_secattr *secattr);
  272. #else
  273. static inline int security_netlbl_secattr_to_sid(struct selinux_state *state,
  274. struct netlbl_lsm_secattr *secattr,
  275. u32 *sid)
  276. {
  277. return -EIDRM;
  278. }
  279. static inline int security_netlbl_sid_to_secattr(struct selinux_state *state,
  280. u32 sid,
  281. struct netlbl_lsm_secattr *secattr)
  282. {
  283. return -ENOENT;
  284. }
  285. #endif /* CONFIG_NETLABEL */
  286. const char *security_get_initial_sid_context(u32 sid);
  287. /*
  288. * status notifier using mmap interface
  289. */
  290. extern struct page *selinux_kernel_status_page(struct selinux_state *state);
  291. #define SELINUX_KERNEL_STATUS_VERSION 1
  292. struct selinux_kernel_status {
  293. u32 version; /* version number of thie structure */
  294. u32 sequence; /* sequence number of seqlock logic */
  295. u32 enforcing; /* current setting of enforcing mode */
  296. u32 policyload; /* times of policy reloaded */
  297. u32 deny_unknown; /* current setting of deny_unknown */
  298. /*
  299. * The version > 0 supports above members.
  300. */
  301. } __packed;
  302. extern void selinux_status_update_setenforce(struct selinux_state *state,
  303. int enforcing);
  304. extern void selinux_status_update_policyload(struct selinux_state *state,
  305. int seqno);
  306. extern void selinux_complete_init(void);
  307. extern int selinux_disable(struct selinux_state *state);
  308. extern void exit_sel_fs(void);
  309. extern struct path selinux_null;
  310. extern struct vfsmount *selinuxfs_mount;
  311. extern void selnl_notify_setenforce(int val);
  312. extern void selnl_notify_policyload(u32 seqno);
  313. extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm);
  314. extern void avtab_cache_init(void);
  315. extern void ebitmap_cache_init(void);
  316. extern void hashtab_cache_init(void);
  317. #endif /* _SELINUX_SECURITY_H_ */