projects/linux·blob master · refs

298 lines · 7.9 KiB

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