security.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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_ss;
  83. struct selinux_state {
  84. bool disabled;
  85. #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
  86. bool enforcing;
  87. #endif
  88. bool checkreqprot;
  89. bool initialized;
  90. bool policycap[__POLICYDB_CAPABILITY_MAX];
  91. struct selinux_ss *ss;
  92. };
  93. void selinux_ss_init(struct selinux_ss **ss);
  94. extern struct selinux_state selinux_state;
  95. #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
  96. static inline bool is_enforcing(struct selinux_state *state)
  97. {
  98. return state->enforcing;
  99. }
  100. static inline void set_enforcing(struct selinux_state *state, bool value)
  101. {
  102. state->enforcing = value;
  103. }
  104. #else
  105. static inline bool is_enforcing(struct selinux_state *state)
  106. {
  107. return true;
  108. }
  109. static inline void set_enforcing(struct selinux_state *state, bool value)
  110. {
  111. }
  112. #endif
  113. static inline bool selinux_policycap_netpeer(void)
  114. {
  115. struct selinux_state *state = &selinux_state;
  116. return state->policycap[POLICYDB_CAPABILITY_NETPEER];
  117. }
  118. static inline bool selinux_policycap_openperm(void)
  119. {
  120. struct selinux_state *state = &selinux_state;
  121. return state->policycap[POLICYDB_CAPABILITY_OPENPERM];
  122. }
  123. static inline bool selinux_policycap_extsockclass(void)
  124. {
  125. struct selinux_state *state = &selinux_state;
  126. return state->policycap[POLICYDB_CAPABILITY_EXTSOCKCLASS];
  127. }
  128. static inline bool selinux_policycap_alwaysnetwork(void)
  129. {
  130. struct selinux_state *state = &selinux_state;
  131. return state->policycap[POLICYDB_CAPABILITY_ALWAYSNETWORK];
  132. }
  133. static inline bool selinux_policycap_cgroupseclabel(void)
  134. {
  135. struct selinux_state *state = &selinux_state;
  136. return state->policycap[POLICYDB_CAPABILITY_CGROUPSECLABEL];
  137. }
  138. static inline bool selinux_policycap_nnp_nosuid_transition(void)
  139. {
  140. struct selinux_state *state = &selinux_state;
  141. return state->policycap[POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION];
  142. }
  143. int security_mls_enabled(struct selinux_state *state);
  144. int security_load_policy(struct selinux_state *state,
  145. void *data, size_t len);
  146. int security_read_policy(struct selinux_state *state,
  147. void **data, size_t *len);
  148. size_t security_policydb_len(struct selinux_state *state);
  149. int security_policycap_supported(struct selinux_state *state,
  150. unsigned int req_cap);
  151. #define SEL_VEC_MAX 32
  152. struct av_decision {
  153. u32 allowed;
  154. u32 auditallow;
  155. u32 auditdeny;
  156. u32 seqno;
  157. u32 flags;
  158. };
  159. #define XPERMS_ALLOWED 1
  160. #define XPERMS_AUDITALLOW 2
  161. #define XPERMS_DONTAUDIT 4
  162. #define security_xperm_set(perms, x) (perms[x >> 5] |= 1 << (x & 0x1f))
  163. #define security_xperm_test(perms, x) (1 & (perms[x >> 5] >> (x & 0x1f)))
  164. struct extended_perms_data {
  165. u32 p[8];
  166. };
  167. struct extended_perms_decision {
  168. u8 used;
  169. u8 driver;
  170. struct extended_perms_data *allowed;
  171. struct extended_perms_data *auditallow;
  172. struct extended_perms_data *dontaudit;
  173. };
  174. struct extended_perms {
  175. u16 len; /* length associated decision chain */
  176. struct extended_perms_data drivers; /* flag drivers that are used */
  177. };
  178. /* definitions of av_decision.flags */
  179. #define AVD_FLAGS_PERMISSIVE 0x0001
  180. void security_compute_av(struct selinux_state *state,
  181. u32 ssid, u32 tsid,
  182. u16 tclass, struct av_decision *avd,
  183. struct extended_perms *xperms);
  184. void security_compute_xperms_decision(struct selinux_state *state,
  185. u32 ssid, u32 tsid, u16 tclass,
  186. u8 driver,
  187. struct extended_perms_decision *xpermd);
  188. void security_compute_av_user(struct selinux_state *state,
  189. u32 ssid, u32 tsid,
  190. u16 tclass, struct av_decision *avd);
  191. int security_transition_sid(struct selinux_state *state,
  192. u32 ssid, u32 tsid, u16 tclass,
  193. const struct qstr *qstr, u32 *out_sid);
  194. int security_transition_sid_user(struct selinux_state *state,
  195. u32 ssid, u32 tsid, u16 tclass,
  196. const char *objname, u32 *out_sid);
  197. int security_member_sid(struct selinux_state *state, u32 ssid, u32 tsid,
  198. u16 tclass, u32 *out_sid);
  199. int security_change_sid(struct selinux_state *state, u32 ssid, u32 tsid,
  200. u16 tclass, u32 *out_sid);
  201. int security_sid_to_context(struct selinux_state *state, u32 sid,
  202. char **scontext, u32 *scontext_len);
  203. int security_sid_to_context_force(struct selinux_state *state,
  204. u32 sid, char **scontext, u32 *scontext_len);
  205. int security_context_to_sid(struct selinux_state *state,
  206. const char *scontext, u32 scontext_len,
  207. u32 *out_sid, gfp_t gfp);
  208. int security_context_str_to_sid(struct selinux_state *state,
  209. const char *scontext, u32 *out_sid, gfp_t gfp);
  210. int security_context_to_sid_default(struct selinux_state *state,
  211. const char *scontext, u32 scontext_len,
  212. u32 *out_sid, u32 def_sid, gfp_t gfp_flags);
  213. int security_context_to_sid_force(struct selinux_state *state,
  214. const char *scontext, u32 scontext_len,
  215. u32 *sid);
  216. int security_get_user_sids(struct selinux_state *state,
  217. u32 callsid, char *username,
  218. u32 **sids, u32 *nel);
  219. int security_port_sid(struct selinux_state *state,
  220. u8 protocol, u16 port, u32 *out_sid);
  221. int security_ib_pkey_sid(struct selinux_state *state,
  222. u64 subnet_prefix, u16 pkey_num, u32 *out_sid);
  223. int security_ib_endport_sid(struct selinux_state *state,
  224. const char *dev_name, u8 port_num, u32 *out_sid);
  225. int security_netif_sid(struct selinux_state *state,
  226. char *name, u32 *if_sid);
  227. int security_node_sid(struct selinux_state *state,
  228. u16 domain, void *addr, u32 addrlen,
  229. u32 *out_sid);
  230. int security_validate_transition(struct selinux_state *state,
  231. u32 oldsid, u32 newsid, u32 tasksid,
  232. u16 tclass);
  233. int security_validate_transition_user(struct selinux_state *state,
  234. u32 oldsid, u32 newsid, u32 tasksid,
  235. u16 tclass);
  236. int security_bounded_transition(struct selinux_state *state,
  237. u32 oldsid, u32 newsid);
  238. int security_sid_mls_copy(struct selinux_state *state,
  239. u32 sid, u32 mls_sid, u32 *new_sid);
  240. int security_net_peersid_resolve(struct selinux_state *state,
  241. u32 nlbl_sid, u32 nlbl_type,
  242. u32 xfrm_sid,
  243. u32 *peer_sid);
  244. int security_get_classes(struct selinux_state *state,
  245. char ***classes, int *nclasses);
  246. int security_get_permissions(struct selinux_state *state,
  247. char *class, char ***perms, int *nperms);
  248. int security_get_reject_unknown(struct selinux_state *state);
  249. int security_get_allow_unknown(struct selinux_state *state);
  250. #define SECURITY_FS_USE_XATTR 1 /* use xattr */
  251. #define SECURITY_FS_USE_TRANS 2 /* use transition SIDs, e.g. devpts/tmpfs */
  252. #define SECURITY_FS_USE_TASK 3 /* use task SIDs, e.g. pipefs/sockfs */
  253. #define SECURITY_FS_USE_GENFS 4 /* use the genfs support */
  254. #define SECURITY_FS_USE_NONE 5 /* no labeling support */
  255. #define SECURITY_FS_USE_MNTPOINT 6 /* use mountpoint labeling */
  256. #define SECURITY_FS_USE_NATIVE 7 /* use native label support */
  257. #define SECURITY_FS_USE_MAX 7 /* Highest SECURITY_FS_USE_XXX */
  258. int security_fs_use(struct selinux_state *state, struct super_block *sb);
  259. int security_genfs_sid(struct selinux_state *state,
  260. const char *fstype, char *name, u16 sclass,
  261. u32 *sid);
  262. #ifdef CONFIG_NETLABEL
  263. int security_netlbl_secattr_to_sid(struct selinux_state *state,
  264. struct netlbl_lsm_secattr *secattr,
  265. u32 *sid);
  266. int security_netlbl_sid_to_secattr(struct selinux_state *state,
  267. u32 sid,
  268. struct netlbl_lsm_secattr *secattr);
  269. #else
  270. static inline int security_netlbl_secattr_to_sid(struct selinux_state *state,
  271. struct netlbl_lsm_secattr *secattr,
  272. u32 *sid)
  273. {
  274. return -EIDRM;
  275. }
  276. static inline int security_netlbl_sid_to_secattr(struct selinux_state *state,
  277. u32 sid,
  278. struct netlbl_lsm_secattr *secattr)
  279. {
  280. return -ENOENT;
  281. }
  282. #endif /* CONFIG_NETLABEL */
  283. const char *security_get_initial_sid_context(u32 sid);
  284. /*
  285. * status notifier using mmap interface
  286. */
  287. extern struct page *selinux_kernel_status_page(struct selinux_state *state);
  288. #define SELINUX_KERNEL_STATUS_VERSION 1
  289. struct selinux_kernel_status {
  290. u32 version; /* version number of thie structure */
  291. u32 sequence; /* sequence number of seqlock logic */
  292. u32 enforcing; /* current setting of enforcing mode */
  293. u32 policyload; /* times of policy reloaded */
  294. u32 deny_unknown; /* current setting of deny_unknown */
  295. /*
  296. * The version > 0 supports above members.
  297. */
  298. } __packed;
  299. extern void selinux_status_update_setenforce(struct selinux_state *state,
  300. int enforcing);
  301. extern void selinux_status_update_policyload(struct selinux_state *state,
  302. int seqno);
  303. extern void selinux_complete_init(void);
  304. extern int selinux_disable(struct selinux_state *state);
  305. extern void exit_sel_fs(void);
  306. extern struct path selinux_null;
  307. extern struct vfsmount *selinuxfs_mount;
  308. extern void selnl_notify_setenforce(int val);
  309. extern void selnl_notify_policyload(u32 seqno);
  310. extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm);
  311. extern void avtab_cache_init(void);
  312. extern void ebitmap_cache_init(void);
  313. extern void hashtab_cache_init(void);
  314. #endif /* _SELINUX_SECURITY_H_ */