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