objsec.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Security-Enhanced Linux (SELinux) security module
  4. *
  5. * This file contains the SELinux security data structures for kernel objects.
  6. *
  7. * Author(s): Stephen Smalley, <stephen.smalley.work@gmail.com>
  8. * Chris Vance, <cvance@nai.com>
  9. * Wayne Salamon, <wsalamon@nai.com>
  10. * James Morris <jmorris@redhat.com>
  11. *
  12. * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
  13. * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
  14. * Copyright (C) 2016 Mellanox Technologies
  15. */
  16. #ifndef _SELINUX_OBJSEC_H_
  17. #define _SELINUX_OBJSEC_H_
  18. #include <linux/list.h>
  19. #include <linux/sched.h>
  20. #include <linux/fs.h>
  21. #include <linux/binfmts.h>
  22. #include <linux/in.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/lsm_hooks.h>
  25. #include <linux/msg.h>
  26. #include <net/net_namespace.h>
  27. #include <linux/bpf.h>
  28. #include "flask.h"
  29. #include "avc.h"
  30. struct avdc_entry {
  31. u32 isid; /* inode SID */
  32. u32 allowed; /* allowed permission bitmask */
  33. u32 audited; /* audited permission bitmask */
  34. bool permissive; /* AVC permissive flag */
  35. };
  36. struct cred_security_struct {
  37. u32 osid; /* SID prior to last execve */
  38. u32 sid; /* current SID */
  39. u32 exec_sid; /* exec SID */
  40. u32 create_sid; /* fscreate SID */
  41. u32 keycreate_sid; /* keycreate SID */
  42. u32 sockcreate_sid; /* fscreate SID */
  43. } __randomize_layout;
  44. struct task_security_struct {
  45. #define TSEC_AVDC_DIR_SIZE (1 << 2)
  46. struct {
  47. u32 sid; /* current SID for cached entries */
  48. u32 seqno; /* AVC sequence number */
  49. unsigned int dir_spot; /* dir cache index to check first */
  50. struct avdc_entry dir[TSEC_AVDC_DIR_SIZE]; /* dir entries */
  51. bool permissive_neveraudit; /* permissive and neveraudit */
  52. } avdcache;
  53. } __randomize_layout;
  54. static inline bool task_avdcache_permnoaudit(struct task_security_struct *tsec,
  55. u32 sid)
  56. {
  57. return (tsec->avdcache.permissive_neveraudit &&
  58. sid == tsec->avdcache.sid &&
  59. tsec->avdcache.seqno == avc_policy_seqno());
  60. }
  61. enum label_initialized {
  62. LABEL_INVALID, /* invalid or not initialized */
  63. LABEL_INITIALIZED, /* initialized */
  64. LABEL_PENDING
  65. };
  66. struct inode_security_struct {
  67. struct inode *inode; /* back pointer to inode object */
  68. struct list_head list; /* list of inode_security_struct */
  69. u32 task_sid; /* SID of creating task */
  70. u32 sid; /* SID of this object */
  71. u16 sclass; /* security class of this object */
  72. unsigned char initialized; /* initialization flag */
  73. spinlock_t lock;
  74. };
  75. struct file_security_struct {
  76. u32 sid; /* SID of open file description */
  77. u32 fown_sid; /* SID of file owner (for SIGIO) */
  78. u32 isid; /* SID of inode at the time of file open */
  79. u32 pseqno; /* Policy seqno at the time of file open */
  80. };
  81. struct superblock_security_struct {
  82. u32 sid; /* SID of file system superblock */
  83. u32 def_sid; /* default SID for labeling */
  84. u32 mntpoint_sid; /* SECURITY_FS_USE_MNTPOINT context for files */
  85. u32 creator_sid; /* SID of privileged process */
  86. unsigned short behavior; /* labeling behavior */
  87. unsigned short flags; /* which mount options were specified */
  88. struct mutex lock;
  89. struct list_head isec_head;
  90. spinlock_t isec_lock;
  91. };
  92. struct msg_security_struct {
  93. u32 sid; /* SID of message */
  94. };
  95. struct ipc_security_struct {
  96. u16 sclass; /* security class of this object */
  97. u32 sid; /* SID of IPC resource */
  98. };
  99. struct netif_security_struct {
  100. const struct net *ns; /* network namespace */
  101. int ifindex; /* device index */
  102. u32 sid; /* SID for this interface */
  103. };
  104. struct netnode_security_struct {
  105. union {
  106. __be32 ipv4; /* IPv4 node address */
  107. struct in6_addr ipv6; /* IPv6 node address */
  108. } addr;
  109. u32 sid; /* SID for this node */
  110. u16 family; /* address family */
  111. };
  112. struct netport_security_struct {
  113. u32 sid; /* SID for this node */
  114. u16 port; /* port number */
  115. u8 protocol; /* transport protocol */
  116. };
  117. struct sk_security_struct {
  118. #ifdef CONFIG_NETLABEL
  119. enum { /* NetLabel state */
  120. NLBL_UNSET = 0,
  121. NLBL_REQUIRE,
  122. NLBL_LABELED,
  123. NLBL_REQSKB,
  124. NLBL_CONNLABELED,
  125. } nlbl_state;
  126. struct netlbl_lsm_secattr *nlbl_secattr; /* NetLabel sec attributes */
  127. #endif
  128. u32 sid; /* SID of this object */
  129. u32 peer_sid; /* SID of peer */
  130. u16 sclass; /* sock security class */
  131. enum { /* SCTP association state */
  132. SCTP_ASSOC_UNSET = 0,
  133. SCTP_ASSOC_SET,
  134. } sctp_assoc_state;
  135. };
  136. struct tun_security_struct {
  137. u32 sid; /* SID for the tun device sockets */
  138. };
  139. struct key_security_struct {
  140. u32 sid; /* SID of key */
  141. };
  142. struct ib_security_struct {
  143. u32 sid; /* SID of the queue pair or MAD agent */
  144. };
  145. struct pkey_security_struct {
  146. u64 subnet_prefix; /* Port subnet prefix */
  147. u16 pkey; /* PKey number */
  148. u32 sid; /* SID of pkey */
  149. };
  150. struct bpf_security_struct {
  151. u32 sid; /* SID of bpf obj creator */
  152. u32 perms; /* permissions for allowed bpf token commands */
  153. u32 grantor_sid; /* SID of token grantor */
  154. };
  155. struct perf_event_security_struct {
  156. u32 sid; /* SID of perf_event obj creator */
  157. };
  158. extern struct lsm_blob_sizes selinux_blob_sizes;
  159. static inline struct cred_security_struct *selinux_cred(const struct cred *cred)
  160. {
  161. return cred->security + selinux_blob_sizes.lbs_cred;
  162. }
  163. static inline struct task_security_struct *
  164. selinux_task(const struct task_struct *task)
  165. {
  166. return task->security + selinux_blob_sizes.lbs_task;
  167. }
  168. static inline struct file_security_struct *selinux_file(const struct file *file)
  169. {
  170. return file->f_security + selinux_blob_sizes.lbs_file;
  171. }
  172. static inline struct inode_security_struct *
  173. selinux_inode(const struct inode *inode)
  174. {
  175. if (unlikely(!inode->i_security))
  176. return NULL;
  177. return inode->i_security + selinux_blob_sizes.lbs_inode;
  178. }
  179. static inline struct msg_security_struct *
  180. selinux_msg_msg(const struct msg_msg *msg_msg)
  181. {
  182. return msg_msg->security + selinux_blob_sizes.lbs_msg_msg;
  183. }
  184. static inline struct ipc_security_struct *
  185. selinux_ipc(const struct kern_ipc_perm *ipc)
  186. {
  187. return ipc->security + selinux_blob_sizes.lbs_ipc;
  188. }
  189. /*
  190. * get the subjective security ID of the current task
  191. */
  192. static inline u32 current_sid(void)
  193. {
  194. const struct cred_security_struct *crsec = selinux_cred(current_cred());
  195. return crsec->sid;
  196. }
  197. static inline struct superblock_security_struct *
  198. selinux_superblock(const struct super_block *superblock)
  199. {
  200. return superblock->s_security + selinux_blob_sizes.lbs_superblock;
  201. }
  202. #ifdef CONFIG_KEYS
  203. static inline struct key_security_struct *selinux_key(const struct key *key)
  204. {
  205. return key->security + selinux_blob_sizes.lbs_key;
  206. }
  207. #endif /* CONFIG_KEYS */
  208. static inline struct sk_security_struct *selinux_sock(const struct sock *sock)
  209. {
  210. return sock->sk_security + selinux_blob_sizes.lbs_sock;
  211. }
  212. static inline struct tun_security_struct *selinux_tun_dev(void *security)
  213. {
  214. return security + selinux_blob_sizes.lbs_tun_dev;
  215. }
  216. static inline struct ib_security_struct *selinux_ib(void *ib_sec)
  217. {
  218. return ib_sec + selinux_blob_sizes.lbs_ib;
  219. }
  220. static inline struct perf_event_security_struct *
  221. selinux_perf_event(void *perf_event)
  222. {
  223. return perf_event + selinux_blob_sizes.lbs_perf_event;
  224. }
  225. #ifdef CONFIG_BPF_SYSCALL
  226. static inline struct bpf_security_struct *
  227. selinux_bpf_map_security(struct bpf_map *map)
  228. {
  229. return map->security + selinux_blob_sizes.lbs_bpf_map;
  230. }
  231. static inline struct bpf_security_struct *
  232. selinux_bpf_prog_security(struct bpf_prog *prog)
  233. {
  234. return prog->aux->security + selinux_blob_sizes.lbs_bpf_prog;
  235. }
  236. static inline struct bpf_security_struct *
  237. selinux_bpf_token_security(struct bpf_token *token)
  238. {
  239. return token->security + selinux_blob_sizes.lbs_bpf_token;
  240. }
  241. #endif /* CONFIG_BPF_SYSCALL */
  242. #endif /* _SELINUX_OBJSEC_H_ */