security.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Security server interface.
  4. *
  5. * Author : Stephen Smalley, <[email protected]>
  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/rcupdate.h>
  15. #include <linux/refcount.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/delay.h>
  18. #include <linux/printk.h>
  19. #include "flask.h"
  20. #include "policycap.h"
  21. #define SECSID_NULL 0x00000000 /* unspecified SID */
  22. #define SECSID_WILD 0xffffffff /* wildcard SID */
  23. #define SECCLASS_NULL 0x0000 /* no class */
  24. /* Identify specific policy version changes */
  25. #define POLICYDB_VERSION_BASE 15
  26. #define POLICYDB_VERSION_BOOL 16
  27. #define POLICYDB_VERSION_IPV6 17
  28. #define POLICYDB_VERSION_NLCLASS 18
  29. #define POLICYDB_VERSION_VALIDATETRANS 19
  30. #define POLICYDB_VERSION_MLS 19
  31. #define POLICYDB_VERSION_AVTAB 20
  32. #define POLICYDB_VERSION_RANGETRANS 21
  33. #define POLICYDB_VERSION_POLCAP 22
  34. #define POLICYDB_VERSION_PERMISSIVE 23
  35. #define POLICYDB_VERSION_BOUNDARY 24
  36. #define POLICYDB_VERSION_FILENAME_TRANS 25
  37. #define POLICYDB_VERSION_ROLETRANS 26
  38. #define POLICYDB_VERSION_NEW_OBJECT_DEFAULTS 27
  39. #define POLICYDB_VERSION_DEFAULT_TYPE 28
  40. #define POLICYDB_VERSION_CONSTRAINT_NAMES 29
  41. #define POLICYDB_VERSION_XPERMS_IOCTL 30
  42. #define POLICYDB_VERSION_INFINIBAND 31
  43. #define POLICYDB_VERSION_GLBLUB 32
  44. #define POLICYDB_VERSION_COMP_FTRANS 33 /* compressed filename transitions */
  45. /* Range of policy versions we understand*/
  46. #define POLICYDB_VERSION_MIN POLICYDB_VERSION_BASE
  47. #define POLICYDB_VERSION_MAX POLICYDB_VERSION_COMP_FTRANS
  48. /* Mask for just the mount related flags */
  49. #define SE_MNTMASK 0x0f
  50. /* Super block security struct flags for mount options */
  51. /* BE CAREFUL, these need to be the low order bits for selinux_get_mnt_opts */
  52. #define CONTEXT_MNT 0x01
  53. #define FSCONTEXT_MNT 0x02
  54. #define ROOTCONTEXT_MNT 0x04
  55. #define DEFCONTEXT_MNT 0x08
  56. #define SBLABEL_MNT 0x10
  57. /* Non-mount related flags */
  58. #define SE_SBINITIALIZED 0x0100
  59. #define SE_SBPROC 0x0200
  60. #define SE_SBGENFS 0x0400
  61. #define SE_SBGENFS_XATTR 0x0800
  62. #define CONTEXT_STR "context"
  63. #define FSCONTEXT_STR "fscontext"
  64. #define ROOTCONTEXT_STR "rootcontext"
  65. #define DEFCONTEXT_STR "defcontext"
  66. #define SECLABEL_STR "seclabel"
  67. struct netlbl_lsm_secattr;
  68. extern int selinux_enabled_boot;
  69. /*
  70. * type_datum properties
  71. * available at the kernel policy version >= POLICYDB_VERSION_BOUNDARY
  72. */
  73. #define TYPEDATUM_PROPERTY_PRIMARY 0x0001
  74. #define TYPEDATUM_PROPERTY_ATTRIBUTE 0x0002
  75. /* limitation of boundary depth */
  76. #define POLICYDB_BOUNDS_MAXDEPTH 4
  77. struct selinux_avc;
  78. struct selinux_policy;
  79. struct selinux_state {
  80. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  81. bool disabled;
  82. #endif
  83. #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
  84. bool enforcing;
  85. #endif
  86. bool checkreqprot;
  87. bool initialized;
  88. bool policycap[__POLICYDB_CAP_MAX];
  89. bool android_netlink_route;
  90. bool android_netlink_getneigh;
  91. struct page *status_page;
  92. struct mutex status_lock;
  93. struct selinux_avc *avc;
  94. struct selinux_policy __rcu *policy;
  95. struct mutex policy_mutex;
  96. } __randomize_layout;
  97. void selinux_avc_init(struct selinux_avc **avc);
  98. extern struct selinux_state selinux_state;
  99. static inline bool selinux_initialized(const struct selinux_state *state)
  100. {
  101. /* do a synchronized load to avoid race conditions */
  102. return smp_load_acquire(&state->initialized);
  103. }
  104. static inline void selinux_mark_initialized(struct selinux_state *state)
  105. {
  106. /* do a synchronized write to avoid race conditions */
  107. smp_store_release(&state->initialized, true);
  108. }
  109. #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
  110. static inline bool enforcing_enabled(struct selinux_state *state)
  111. {
  112. return READ_ONCE(state->enforcing);
  113. }
  114. static inline void enforcing_set(struct selinux_state *state, bool value)
  115. {
  116. WRITE_ONCE(state->enforcing, value);
  117. }
  118. #else
  119. static inline bool enforcing_enabled(struct selinux_state *state)
  120. {
  121. return true;
  122. }
  123. static inline void enforcing_set(struct selinux_state *state, bool value)
  124. {
  125. }
  126. #endif
  127. static inline bool checkreqprot_get(const struct selinux_state *state)
  128. {
  129. return READ_ONCE(state->checkreqprot);
  130. }
  131. static inline void checkreqprot_set(struct selinux_state *state, bool value)
  132. {
  133. if (value)
  134. pr_err("SELinux: https://github.com/SELinuxProject/selinux-kernel/wiki/DEPRECATE-checkreqprot\n");
  135. WRITE_ONCE(state->checkreqprot, value);
  136. }
  137. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  138. static inline bool selinux_disabled(struct selinux_state *state)
  139. {
  140. return READ_ONCE(state->disabled);
  141. }
  142. static inline void selinux_mark_disabled(struct selinux_state *state)
  143. {
  144. WRITE_ONCE(state->disabled, true);
  145. }
  146. #else
  147. static inline bool selinux_disabled(struct selinux_state *state)
  148. {
  149. return false;
  150. }
  151. #endif
  152. static inline bool selinux_policycap_netpeer(void)
  153. {
  154. struct selinux_state *state = &selinux_state;
  155. return READ_ONCE(state->policycap[POLICYDB_CAP_NETPEER]);
  156. }
  157. static inline bool selinux_policycap_openperm(void)
  158. {
  159. struct selinux_state *state = &selinux_state;
  160. return READ_ONCE(state->policycap[POLICYDB_CAP_OPENPERM]);
  161. }
  162. static inline bool selinux_policycap_extsockclass(void)
  163. {
  164. struct selinux_state *state = &selinux_state;
  165. return READ_ONCE(state->policycap[POLICYDB_CAP_EXTSOCKCLASS]);
  166. }
  167. static inline bool selinux_policycap_alwaysnetwork(void)
  168. {
  169. struct selinux_state *state = &selinux_state;
  170. return READ_ONCE(state->policycap[POLICYDB_CAP_ALWAYSNETWORK]);
  171. }
  172. static inline bool selinux_policycap_cgroupseclabel(void)
  173. {
  174. struct selinux_state *state = &selinux_state;
  175. return READ_ONCE(state->policycap[POLICYDB_CAP_CGROUPSECLABEL]);
  176. }
  177. static inline bool selinux_policycap_nnp_nosuid_transition(void)
  178. {
  179. struct selinux_state *state = &selinux_state;
  180. return READ_ONCE(state->policycap[POLICYDB_CAP_NNP_NOSUID_TRANSITION]);
  181. }
  182. static inline bool selinux_policycap_genfs_seclabel_symlinks(void)
  183. {
  184. struct selinux_state *state = &selinux_state;
  185. return READ_ONCE(state->policycap[POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS]);
  186. }
  187. static inline bool selinux_policycap_ioctl_skip_cloexec(void)
  188. {
  189. struct selinux_state *state = &selinux_state;
  190. return READ_ONCE(state->policycap[POLICYDB_CAP_IOCTL_SKIP_CLOEXEC]);
  191. }
  192. static inline bool selinux_android_nlroute_getlink(void)
  193. {
  194. struct selinux_state *state = &selinux_state;
  195. return state->android_netlink_route;
  196. }
  197. static inline bool selinux_android_nlroute_getneigh(void)
  198. {
  199. struct selinux_state *state = &selinux_state;
  200. return state->android_netlink_getneigh;
  201. }
  202. struct selinux_policy_convert_data;
  203. struct selinux_load_state {
  204. struct selinux_policy *policy;
  205. struct selinux_policy_convert_data *convert_data;
  206. };
  207. int security_mls_enabled(struct selinux_state *state);
  208. int security_load_policy(struct selinux_state *state,
  209. void *data, size_t len,
  210. struct selinux_load_state *load_state);
  211. void selinux_policy_commit(struct selinux_state *state,
  212. struct selinux_load_state *load_state);
  213. void selinux_policy_cancel(struct selinux_state *state,
  214. struct selinux_load_state *load_state);
  215. int security_read_policy(struct selinux_state *state,
  216. void **data, size_t *len);
  217. int security_read_state_kernel(struct selinux_state *state,
  218. void **data, size_t *len);
  219. int security_policycap_supported(struct selinux_state *state,
  220. unsigned int req_cap);
  221. #define SEL_VEC_MAX 32
  222. struct av_decision {
  223. u32 allowed;
  224. u32 auditallow;
  225. u32 auditdeny;
  226. u32 seqno;
  227. u32 flags;
  228. };
  229. #define XPERMS_ALLOWED 1
  230. #define XPERMS_AUDITALLOW 2
  231. #define XPERMS_DONTAUDIT 4
  232. #define security_xperm_set(perms, x) ((perms)[(x) >> 5] |= 1 << ((x) & 0x1f))
  233. #define security_xperm_test(perms, x) (1 & ((perms)[(x) >> 5] >> ((x) & 0x1f)))
  234. struct extended_perms_data {
  235. u32 p[8];
  236. };
  237. struct extended_perms_decision {
  238. u8 used;
  239. u8 driver;
  240. struct extended_perms_data *allowed;
  241. struct extended_perms_data *auditallow;
  242. struct extended_perms_data *dontaudit;
  243. };
  244. struct extended_perms {
  245. u16 len; /* length associated decision chain */
  246. struct extended_perms_data drivers; /* flag drivers that are used */
  247. };
  248. /* definitions of av_decision.flags */
  249. #define AVD_FLAGS_PERMISSIVE 0x0001
  250. void security_compute_av(struct selinux_state *state,
  251. u32 ssid, u32 tsid,
  252. u16 tclass, struct av_decision *avd,
  253. struct extended_perms *xperms);
  254. void security_compute_xperms_decision(struct selinux_state *state,
  255. u32 ssid, u32 tsid, u16 tclass,
  256. u8 driver,
  257. struct extended_perms_decision *xpermd);
  258. void security_compute_av_user(struct selinux_state *state,
  259. u32 ssid, u32 tsid,
  260. u16 tclass, struct av_decision *avd);
  261. int security_transition_sid(struct selinux_state *state,
  262. u32 ssid, u32 tsid, u16 tclass,
  263. const struct qstr *qstr, u32 *out_sid);
  264. int security_transition_sid_user(struct selinux_state *state,
  265. u32 ssid, u32 tsid, u16 tclass,
  266. const char *objname, u32 *out_sid);
  267. int security_member_sid(struct selinux_state *state, u32 ssid, u32 tsid,
  268. u16 tclass, u32 *out_sid);
  269. int security_change_sid(struct selinux_state *state, u32 ssid, u32 tsid,
  270. u16 tclass, u32 *out_sid);
  271. int security_sid_to_context(struct selinux_state *state, u32 sid,
  272. char **scontext, u32 *scontext_len);
  273. int security_sid_to_context_force(struct selinux_state *state,
  274. u32 sid, char **scontext, u32 *scontext_len);
  275. int security_sid_to_context_inval(struct selinux_state *state,
  276. u32 sid, char **scontext, u32 *scontext_len);
  277. int security_context_to_sid(struct selinux_state *state,
  278. const char *scontext, u32 scontext_len,
  279. u32 *out_sid, gfp_t gfp);
  280. int security_context_str_to_sid(struct selinux_state *state,
  281. const char *scontext, u32 *out_sid, gfp_t gfp);
  282. int security_context_to_sid_default(struct selinux_state *state,
  283. const char *scontext, u32 scontext_len,
  284. u32 *out_sid, u32 def_sid, gfp_t gfp_flags);
  285. int security_context_to_sid_force(struct selinux_state *state,
  286. const char *scontext, u32 scontext_len,
  287. u32 *sid);
  288. int security_get_user_sids(struct selinux_state *state,
  289. u32 callsid, char *username,
  290. u32 **sids, u32 *nel);
  291. int security_port_sid(struct selinux_state *state,
  292. u8 protocol, u16 port, u32 *out_sid);
  293. int security_ib_pkey_sid(struct selinux_state *state,
  294. u64 subnet_prefix, u16 pkey_num, u32 *out_sid);
  295. int security_ib_endport_sid(struct selinux_state *state,
  296. const char *dev_name, u8 port_num, u32 *out_sid);
  297. int security_netif_sid(struct selinux_state *state,
  298. char *name, u32 *if_sid);
  299. int security_node_sid(struct selinux_state *state,
  300. u16 domain, void *addr, u32 addrlen,
  301. u32 *out_sid);
  302. int security_validate_transition(struct selinux_state *state,
  303. u32 oldsid, u32 newsid, u32 tasksid,
  304. u16 tclass);
  305. int security_validate_transition_user(struct selinux_state *state,
  306. u32 oldsid, u32 newsid, u32 tasksid,
  307. u16 tclass);
  308. int security_bounded_transition(struct selinux_state *state,
  309. u32 oldsid, u32 newsid);
  310. int security_sid_mls_copy(struct selinux_state *state,
  311. u32 sid, u32 mls_sid, u32 *new_sid);
  312. int security_net_peersid_resolve(struct selinux_state *state,
  313. u32 nlbl_sid, u32 nlbl_type,
  314. u32 xfrm_sid,
  315. u32 *peer_sid);
  316. int security_get_classes(struct selinux_policy *policy,
  317. char ***classes, int *nclasses);
  318. int security_get_permissions(struct selinux_policy *policy,
  319. char *class, char ***perms, int *nperms);
  320. int security_get_reject_unknown(struct selinux_state *state);
  321. int security_get_allow_unknown(struct selinux_state *state);
  322. #define SECURITY_FS_USE_XATTR 1 /* use xattr */
  323. #define SECURITY_FS_USE_TRANS 2 /* use transition SIDs, e.g. devpts/tmpfs */
  324. #define SECURITY_FS_USE_TASK 3 /* use task SIDs, e.g. pipefs/sockfs */
  325. #define SECURITY_FS_USE_GENFS 4 /* use the genfs support */
  326. #define SECURITY_FS_USE_NONE 5 /* no labeling support */
  327. #define SECURITY_FS_USE_MNTPOINT 6 /* use mountpoint labeling */
  328. #define SECURITY_FS_USE_NATIVE 7 /* use native label support */
  329. #define SECURITY_FS_USE_MAX 7 /* Highest SECURITY_FS_USE_XXX */
  330. int security_fs_use(struct selinux_state *state, struct super_block *sb);
  331. int security_genfs_sid(struct selinux_state *state,
  332. const char *fstype, const char *path, u16 sclass,
  333. u32 *sid);
  334. int selinux_policy_genfs_sid(struct selinux_policy *policy,
  335. const char *fstype, const char *path, u16 sclass,
  336. u32 *sid);
  337. #ifdef CONFIG_NETLABEL
  338. int security_netlbl_secattr_to_sid(struct selinux_state *state,
  339. struct netlbl_lsm_secattr *secattr,
  340. u32 *sid);
  341. int security_netlbl_sid_to_secattr(struct selinux_state *state,
  342. u32 sid,
  343. struct netlbl_lsm_secattr *secattr);
  344. #else
  345. static inline int security_netlbl_secattr_to_sid(struct selinux_state *state,
  346. struct netlbl_lsm_secattr *secattr,
  347. u32 *sid)
  348. {
  349. return -EIDRM;
  350. }
  351. static inline int security_netlbl_sid_to_secattr(struct selinux_state *state,
  352. u32 sid,
  353. struct netlbl_lsm_secattr *secattr)
  354. {
  355. return -ENOENT;
  356. }
  357. #endif /* CONFIG_NETLABEL */
  358. const char *security_get_initial_sid_context(u32 sid);
  359. /*
  360. * status notifier using mmap interface
  361. */
  362. extern struct page *selinux_kernel_status_page(struct selinux_state *state);
  363. #define SELINUX_KERNEL_STATUS_VERSION 1
  364. struct selinux_kernel_status {
  365. u32 version; /* version number of the structure */
  366. u32 sequence; /* sequence number of seqlock logic */
  367. u32 enforcing; /* current setting of enforcing mode */
  368. u32 policyload; /* times of policy reloaded */
  369. u32 deny_unknown; /* current setting of deny_unknown */
  370. /*
  371. * The version > 0 supports above members.
  372. */
  373. } __packed;
  374. extern void selinux_status_update_setenforce(struct selinux_state *state,
  375. int enforcing);
  376. extern void selinux_status_update_policyload(struct selinux_state *state,
  377. int seqno);
  378. extern void selinux_complete_init(void);
  379. extern int selinux_disable(struct selinux_state *state);
  380. extern void exit_sel_fs(void);
  381. extern struct path selinux_null;
  382. extern void selnl_notify_setenforce(int val);
  383. extern void selnl_notify_policyload(u32 seqno);
  384. extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm);
  385. extern void avtab_cache_init(void);
  386. extern void ebitmap_cache_init(void);
  387. extern void hashtab_cache_init(void);
  388. extern int security_sidtab_hash_stats(struct selinux_state *state, char *page);
  389. extern void selinux_nlmsg_init(void);
  390. #endif /* _SELINUX_SECURITY_H_ */