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