1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Security-Enhanced Linux (SELinux) security module
4 *
5 * This file contains the SELinux hook function implementations.
6 *
7 * Authors: 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-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
14 * Eric Paris <eparis@redhat.com>
15 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
16 * <dgoeddel@trustedcs.com>
17 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
18 * Paul Moore <paul@paul-moore.com>
19 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
20 * Yuichi Nakamura <ynakam@hitachisoft.jp>
21 * Copyright (C) 2016 Mellanox Technologies
22 */
23
24 #include <linux/init.h>
25 #include <linux/kd.h>
26 #include <linux/kernel.h>
27 #include <linux/kernel_read_file.h>
28 #include <linux/errno.h>
29 #include <linux/sched/signal.h>
30 #include <linux/sched/task.h>
31 #include <linux/lsm_hooks.h>
32 #include <linux/xattr.h>
33 #include <linux/capability.h>
34 #include <linux/unistd.h>
35 #include <linux/mm.h>
36 #include <linux/mman.h>
37 #include <linux/slab.h>
38 #include <linux/pagemap.h>
39 #include <linux/proc_fs.h>
40 #include <linux/swap.h>
41 #include <linux/spinlock.h>
42 #include <linux/syscalls.h>
43 #include <linux/dcache.h>
44 #include <linux/file.h>
45 #include <linux/fdtable.h>
46 #include <linux/namei.h>
47 #include <linux/mount.h>
48 #include <linux/fs_context.h>
49 #include <linux/fs_parser.h>
50 #include <linux/netfilter_ipv4.h>
51 #include <linux/netfilter_ipv6.h>
52 #include <linux/tty.h>
53 #include <net/icmp.h>
54 #include <net/ip.h> /* for local_port_range[] */
55 #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
56 #include <net/inet_connection_sock.h>
57 #include <net/net_namespace.h>
58 #include <net/netlabel.h>
59 #include <linux/uaccess.h>
60 #include <asm/ioctls.h>
61 #include <linux/atomic.h>
62 #include <linux/bitops.h>
63 #include <linux/interrupt.h>
64 #include <linux/netdevice.h> /* for network interface checks */
65 #include <net/netlink.h>
66 #include <linux/tcp.h>
67 #include <linux/udp.h>
68 #include <linux/sctp.h>
69 #include <net/sctp/structs.h>
70 #include <linux/quota.h>
71 #include <linux/un.h> /* for Unix socket types */
72 #include <net/af_unix.h> /* for Unix socket types */
73 #include <linux/parser.h>
74 #include <linux/nfs_mount.h>
75 #include <net/ipv6.h>
76 #include <linux/hugetlb.h>
77 #include <linux/personality.h>
78 #include <linux/audit.h>
79 #include <linux/string.h>
80 #include <linux/mutex.h>
81 #include <linux/posix-timers.h>
82 #include <linux/syslog.h>
83 #include <linux/user_namespace.h>
84 #include <linux/export.h>
85 #include <linux/msg.h>
86 #include <linux/shm.h>
87 #include <uapi/linux/shm.h>
88 #include <linux/bpf.h>
89 #include <linux/kernfs.h>
90 #include <linux/stringhash.h> /* for hashlen_string() */
91 #include <uapi/linux/mount.h>
92 #include <linux/fsnotify.h>
93 #include <linux/fanotify.h>
94 #include <linux/io_uring/cmd.h>
95 #include <uapi/linux/lsm.h>
96 #include <linux/memfd.h>
97 #include <uapi/linux/inet_diag.h>
98
99 #include "initcalls.h"
100 #include "avc.h"
101 #include "objsec.h"
102 #include "netif.h"
103 #include "netnode.h"
104 #include "netport.h"
105 #include "ibpkey.h"
106 #include "xfrm.h"
107 #include "netlabel.h"
108 #include "audit.h"
109 #include "avc_ss.h"
110 #include "ima.h"
111
112 #define SELINUX_INODE_INIT_XATTRS 1
113
114 struct selinux_state selinux_state;
115
116 /* SECMARK reference count */
117 static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
118
119 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
120 static int selinux_enforcing_boot __initdata;
121
enforcing_setup(char * str)122 static int __init enforcing_setup(char *str)
123 {
124 unsigned long enforcing;
125 if (!kstrtoul(str, 0, &enforcing))
126 selinux_enforcing_boot = enforcing ? 1 : 0;
127 return 1;
128 }
129 __setup("enforcing=", enforcing_setup);
130 #else
131 #define selinux_enforcing_boot 1
132 #endif
133
134 int selinux_enabled_boot __initdata = 1;
135 #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
selinux_enabled_setup(char * str)136 static int __init selinux_enabled_setup(char *str)
137 {
138 unsigned long enabled;
139 if (!kstrtoul(str, 0, &enabled))
140 selinux_enabled_boot = enabled ? 1 : 0;
141 return 1;
142 }
143 __setup("selinux=", selinux_enabled_setup);
144 #endif
145
checkreqprot_setup(char * str)146 static int __init checkreqprot_setup(char *str)
147 {
148 unsigned long checkreqprot;
149
150 if (!kstrtoul(str, 0, &checkreqprot)) {
151 if (checkreqprot)
152 pr_err("SELinux: checkreqprot set to 1 via kernel parameter. This is no longer supported.\n");
153 }
154 return 1;
155 }
156 __setup("checkreqprot=", checkreqprot_setup);
157
158 /**
159 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
160 *
161 * Description:
162 * This function checks the SECMARK reference counter to see if any SECMARK
163 * targets are currently configured, if the reference counter is greater than
164 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
165 * enabled, false (0) if SECMARK is disabled. If the always_check_network
166 * policy capability is enabled, SECMARK is always considered enabled.
167 *
168 */
selinux_secmark_enabled(void)169 static int selinux_secmark_enabled(void)
170 {
171 return (selinux_policycap_alwaysnetwork() ||
172 atomic_read(&selinux_secmark_refcount));
173 }
174
175 /**
176 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
177 *
178 * Description:
179 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
180 * (1) if any are enabled or false (0) if neither are enabled. If the
181 * always_check_network policy capability is enabled, peer labeling
182 * is always considered enabled.
183 *
184 */
selinux_peerlbl_enabled(void)185 static int selinux_peerlbl_enabled(void)
186 {
187 return (selinux_policycap_alwaysnetwork() ||
188 netlbl_enabled() || selinux_xfrm_enabled());
189 }
190
selinux_netcache_avc_callback(u32 event)191 static int selinux_netcache_avc_callback(u32 event)
192 {
193 if (event == AVC_CALLBACK_RESET) {
194 sel_netif_flush();
195 sel_netnode_flush();
196 sel_netport_flush();
197 synchronize_net();
198 }
199 return 0;
200 }
201
selinux_lsm_notifier_avc_callback(u32 event)202 static int selinux_lsm_notifier_avc_callback(u32 event)
203 {
204 if (event == AVC_CALLBACK_RESET) {
205 sel_ib_pkey_flush();
206 call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
207 }
208
209 return 0;
210 }
211
212 /*
213 * initialise the security for the init task
214 */
cred_init_security(void)215 static void cred_init_security(void)
216 {
217 struct cred_security_struct *crsec;
218
219 /* NOTE: the lsm framework zeros out the buffer on allocation */
220
221 crsec = selinux_cred(unrcu_pointer(current->real_cred));
222 crsec->osid = crsec->sid = SECINITSID_KERNEL;
223 }
224
225 /*
226 * get the security ID of a set of credentials
227 */
cred_sid(const struct cred * cred)228 static inline u32 cred_sid(const struct cred *cred)
229 {
230 const struct cred_security_struct *crsec;
231
232 crsec = selinux_cred(cred);
233 return crsec->sid;
234 }
235
__ad_net_init(struct common_audit_data * ad,struct lsm_network_audit * net,int ifindex,struct sock * sk,u16 family)236 static void __ad_net_init(struct common_audit_data *ad,
237 struct lsm_network_audit *net,
238 int ifindex, struct sock *sk, u16 family)
239 {
240 ad->type = LSM_AUDIT_DATA_NET;
241 ad->u.net = net;
242 net->netif = ifindex;
243 net->sk = sk;
244 net->family = family;
245 }
246
ad_net_init_from_sk(struct common_audit_data * ad,struct lsm_network_audit * net,struct sock * sk)247 static void ad_net_init_from_sk(struct common_audit_data *ad,
248 struct lsm_network_audit *net,
249 struct sock *sk)
250 {
251 __ad_net_init(ad, net, 0, sk, 0);
252 }
253
ad_net_init_from_iif(struct common_audit_data * ad,struct lsm_network_audit * net,int ifindex,u16 family)254 static void ad_net_init_from_iif(struct common_audit_data *ad,
255 struct lsm_network_audit *net,
256 int ifindex, u16 family)
257 {
258 __ad_net_init(ad, net, ifindex, NULL, family);
259 }
260
261 /*
262 * get the objective security ID of a task
263 */
task_sid_obj(const struct task_struct * task)264 static inline u32 task_sid_obj(const struct task_struct *task)
265 {
266 u32 sid;
267
268 rcu_read_lock();
269 sid = cred_sid(__task_cred(task));
270 rcu_read_unlock();
271 return sid;
272 }
273
274 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
275
276 /*
277 * Try reloading inode security labels that have been marked as invalid. The
278 * @may_sleep parameter indicates when sleeping and thus reloading labels is
279 * allowed; when set to false, returns -ECHILD when the label is
280 * invalid. The @dentry parameter should be set to a dentry of the inode.
281 */
__inode_security_revalidate(struct inode * inode,struct dentry * dentry,bool may_sleep)282 static int __inode_security_revalidate(struct inode *inode,
283 struct dentry *dentry,
284 bool may_sleep)
285 {
286 if (!selinux_initialized())
287 return 0;
288
289 if (may_sleep)
290 might_sleep();
291 else
292 return -ECHILD;
293
294 /*
295 * Check to ensure that an inode's SELinux state is valid and try
296 * reloading the inode security label if necessary. This will fail if
297 * @dentry is NULL and no dentry for this inode can be found; in that
298 * case, continue using the old label.
299 */
300 inode_doinit_with_dentry(inode, dentry);
301 return 0;
302 }
303
inode_security_novalidate(struct inode * inode)304 static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
305 {
306 return selinux_inode(inode);
307 }
308
inode_security_rcu(struct inode * inode,bool rcu)309 static inline struct inode_security_struct *inode_security_rcu(struct inode *inode,
310 bool rcu)
311 {
312 int rc;
313 struct inode_security_struct *isec = selinux_inode(inode);
314
315 /* check below is racy, but revalidate will recheck with lock held */
316 if (data_race(likely(isec->initialized == LABEL_INITIALIZED)))
317 return isec;
318 rc = __inode_security_revalidate(inode, NULL, !rcu);
319 if (rc)
320 return ERR_PTR(rc);
321 return isec;
322 }
323
324 /*
325 * Get the security label of an inode.
326 */
inode_security(struct inode * inode)327 static inline struct inode_security_struct *inode_security(struct inode *inode)
328 {
329 struct inode_security_struct *isec = selinux_inode(inode);
330
331 /* check below is racy, but revalidate will recheck with lock held */
332 if (data_race(likely(isec->initialized == LABEL_INITIALIZED)))
333 return isec;
334 __inode_security_revalidate(inode, NULL, true);
335 return isec;
336 }
337
backing_inode_security_novalidate(struct dentry * dentry)338 static inline struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
339 {
340 return selinux_inode(d_backing_inode(dentry));
341 }
342
343 /*
344 * Get the security label of a dentry's backing inode.
345 */
backing_inode_security(struct dentry * dentry)346 static inline struct inode_security_struct *backing_inode_security(struct dentry *dentry)
347 {
348 struct inode *inode = d_backing_inode(dentry);
349 struct inode_security_struct *isec = selinux_inode(inode);
350
351 /* check below is racy, but revalidate will recheck with lock held */
352 if (data_race(likely(isec->initialized == LABEL_INITIALIZED)))
353 return isec;
354 __inode_security_revalidate(inode, dentry, true);
355 return isec;
356 }
357
inode_free_security(struct inode * inode)358 static void inode_free_security(struct inode *inode)
359 {
360 struct inode_security_struct *isec = selinux_inode(inode);
361 struct superblock_security_struct *sbsec;
362
363 if (!isec)
364 return;
365 sbsec = selinux_superblock(inode->i_sb);
366 /*
367 * As not all inode security structures are in a list, we check for
368 * empty list outside of the lock to make sure that we won't waste
369 * time taking a lock doing nothing.
370 *
371 * The list_del_init() function can be safely called more than once.
372 * It should not be possible for this function to be called with
373 * concurrent list_add(), but for better safety against future changes
374 * in the code, we use list_empty_careful() here.
375 */
376 if (!list_empty_careful(&isec->list)) {
377 spin_lock(&sbsec->isec_lock);
378 list_del_init(&isec->list);
379 spin_unlock(&sbsec->isec_lock);
380 }
381 }
382
383 struct selinux_mnt_opts {
384 u32 fscontext_sid;
385 u32 context_sid;
386 u32 rootcontext_sid;
387 u32 defcontext_sid;
388 };
389
selinux_free_mnt_opts(void * mnt_opts)390 static void selinux_free_mnt_opts(void *mnt_opts)
391 {
392 kfree(mnt_opts);
393 }
394
395 enum {
396 Opt_error = -1,
397 Opt_context = 0,
398 Opt_defcontext = 1,
399 Opt_fscontext = 2,
400 Opt_rootcontext = 3,
401 Opt_seclabel = 4,
402 };
403
404 #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
405 static const struct {
406 const char *name;
407 int len;
408 int opt;
409 bool has_arg;
410 } tokens[] = {
411 A(context, true),
412 A(fscontext, true),
413 A(defcontext, true),
414 A(rootcontext, true),
415 A(seclabel, false),
416 };
417 #undef A
418
match_opt_prefix(char * s,int l,char ** arg)419 static int match_opt_prefix(char *s, int l, char **arg)
420 {
421 unsigned int i;
422
423 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
424 size_t len = tokens[i].len;
425 if (len > l || memcmp(s, tokens[i].name, len))
426 continue;
427 if (tokens[i].has_arg) {
428 if (len == l || s[len] != '=')
429 continue;
430 *arg = s + len + 1;
431 } else if (len != l)
432 continue;
433 return tokens[i].opt;
434 }
435 return Opt_error;
436 }
437
438 #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
439
may_context_mount_sb_relabel(u32 sid,struct superblock_security_struct * sbsec,const struct cred * cred)440 static int may_context_mount_sb_relabel(u32 sid,
441 struct superblock_security_struct *sbsec,
442 const struct cred *cred)
443 {
444 const struct cred_security_struct *crsec = selinux_cred(cred);
445 int rc;
446
447 rc = avc_has_perm(crsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
448 FILESYSTEM__RELABELFROM, NULL);
449 if (rc)
450 return rc;
451
452 rc = avc_has_perm(crsec->sid, sid, SECCLASS_FILESYSTEM,
453 FILESYSTEM__RELABELTO, NULL);
454 return rc;
455 }
456
may_context_mount_inode_relabel(u32 sid,struct superblock_security_struct * sbsec,const struct cred * cred)457 static int may_context_mount_inode_relabel(u32 sid,
458 struct superblock_security_struct *sbsec,
459 const struct cred *cred)
460 {
461 const struct cred_security_struct *crsec = selinux_cred(cred);
462 int rc;
463 rc = avc_has_perm(crsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
464 FILESYSTEM__RELABELFROM, NULL);
465 if (rc)
466 return rc;
467
468 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
469 FILESYSTEM__ASSOCIATE, NULL);
470 return rc;
471 }
472
selinux_is_genfs_special_handling(struct super_block * sb)473 static int selinux_is_genfs_special_handling(struct super_block *sb)
474 {
475 /* Special handling. Genfs but also in-core setxattr handler */
476 return !strcmp(sb->s_type->name, "sysfs") ||
477 !strcmp(sb->s_type->name, "pstore") ||
478 !strcmp(sb->s_type->name, "debugfs") ||
479 !strcmp(sb->s_type->name, "tracefs") ||
480 !strcmp(sb->s_type->name, "rootfs") ||
481 (selinux_policycap_cgroupseclabel() &&
482 (!strcmp(sb->s_type->name, "cgroup") ||
483 !strcmp(sb->s_type->name, "cgroup2"))) ||
484 (selinux_policycap_functionfs_seclabel() &&
485 !strcmp(sb->s_type->name, "functionfs"));
486 }
487
selinux_is_sblabel_mnt(struct super_block * sb)488 static int selinux_is_sblabel_mnt(struct super_block *sb)
489 {
490 struct superblock_security_struct *sbsec = selinux_superblock(sb);
491
492 /*
493 * IMPORTANT: Double-check logic in this function when adding a new
494 * SECURITY_FS_USE_* definition!
495 */
496 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
497
498 switch (sbsec->behavior) {
499 case SECURITY_FS_USE_XATTR:
500 case SECURITY_FS_USE_TRANS:
501 case SECURITY_FS_USE_TASK:
502 case SECURITY_FS_USE_NATIVE:
503 return 1;
504
505 case SECURITY_FS_USE_GENFS:
506 return selinux_is_genfs_special_handling(sb);
507
508 /* Never allow relabeling on context mounts */
509 case SECURITY_FS_USE_MNTPOINT:
510 case SECURITY_FS_USE_NONE:
511 default:
512 return 0;
513 }
514 }
515
sb_check_xattr_support(struct super_block * sb)516 static int sb_check_xattr_support(struct super_block *sb)
517 {
518 struct superblock_security_struct *sbsec = selinux_superblock(sb);
519 struct dentry *root = sb->s_root;
520 struct inode *root_inode = d_backing_inode(root);
521 u32 sid;
522 int rc;
523
524 /*
525 * Make sure that the xattr handler exists and that no
526 * error other than -ENODATA is returned by getxattr on
527 * the root directory. -ENODATA is ok, as this may be
528 * the first boot of the SELinux kernel before we have
529 * assigned xattr values to the filesystem.
530 */
531 if (!(root_inode->i_opflags & IOP_XATTR)) {
532 pr_warn("SELinux: (dev %s, type %s) has no xattr support\n",
533 sb->s_id, sb->s_type->name);
534 goto fallback;
535 }
536
537 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
538 if (rc < 0 && rc != -ENODATA) {
539 if (rc == -EOPNOTSUPP) {
540 pr_warn("SELinux: (dev %s, type %s) has no security xattr handler\n",
541 sb->s_id, sb->s_type->name);
542 goto fallback;
543 } else {
544 pr_warn("SELinux: (dev %s, type %s) getxattr errno %d\n",
545 sb->s_id, sb->s_type->name, -rc);
546 return rc;
547 }
548 }
549 return 0;
550
551 fallback:
552 /* No xattr support - try to fallback to genfs if possible. */
553 rc = security_genfs_sid(sb->s_type->name, "/",
554 SECCLASS_DIR, &sid);
555 if (rc)
556 return -EOPNOTSUPP;
557
558 pr_warn("SELinux: (dev %s, type %s) falling back to genfs\n",
559 sb->s_id, sb->s_type->name);
560 sbsec->behavior = SECURITY_FS_USE_GENFS;
561 sbsec->sid = sid;
562 return 0;
563 }
564
sb_finish_set_opts(struct super_block * sb)565 static int sb_finish_set_opts(struct super_block *sb)
566 {
567 struct superblock_security_struct *sbsec = selinux_superblock(sb);
568 struct dentry *root = sb->s_root;
569 struct inode *root_inode = d_backing_inode(root);
570 int rc = 0;
571
572 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
573 rc = sb_check_xattr_support(sb);
574 if (rc)
575 return rc;
576 }
577
578 sbsec->flags |= SE_SBINITIALIZED;
579
580 /*
581 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
582 * leave the flag untouched because sb_clone_mnt_opts might be handing
583 * us a superblock that needs the flag to be cleared.
584 */
585 if (selinux_is_sblabel_mnt(sb))
586 sbsec->flags |= SBLABEL_MNT;
587 else
588 sbsec->flags &= ~SBLABEL_MNT;
589
590 /* Initialize the root inode. */
591 rc = inode_doinit_with_dentry(root_inode, root);
592
593 /* Initialize any other inodes associated with the superblock, e.g.
594 inodes created prior to initial policy load or inodes created
595 during get_sb by a pseudo filesystem that directly
596 populates itself. */
597 spin_lock(&sbsec->isec_lock);
598 while (!list_empty(&sbsec->isec_head)) {
599 struct inode_security_struct *isec =
600 list_first_entry(&sbsec->isec_head,
601 struct inode_security_struct, list);
602 struct inode *inode = isec->inode;
603 list_del_init(&isec->list);
604 spin_unlock(&sbsec->isec_lock);
605 inode = igrab(inode);
606 if (inode) {
607 if (!IS_PRIVATE(inode))
608 inode_doinit_with_dentry(inode, NULL);
609 iput(inode);
610 }
611 spin_lock(&sbsec->isec_lock);
612 }
613 spin_unlock(&sbsec->isec_lock);
614 return rc;
615 }
616
bad_option(struct superblock_security_struct * sbsec,char flag,u32 old_sid,u32 new_sid)617 static int bad_option(struct superblock_security_struct *sbsec, char flag,
618 u32 old_sid, u32 new_sid)
619 {
620 char mnt_flags = sbsec->flags & SE_MNTMASK;
621
622 /* check if the old mount command had the same options */
623 if (sbsec->flags & SE_SBINITIALIZED)
624 if (!(sbsec->flags & flag) ||
625 (old_sid != new_sid))
626 return 1;
627
628 /* check if we were passed the same options twice,
629 * aka someone passed context=a,context=b
630 */
631 if (!(sbsec->flags & SE_SBINITIALIZED))
632 if (mnt_flags & flag)
633 return 1;
634 return 0;
635 }
636
637 /*
638 * Allow filesystems with binary mount data to explicitly set mount point
639 * labeling information.
640 */
selinux_set_mnt_opts(struct super_block * sb,void * mnt_opts,unsigned long kern_flags,unsigned long * set_kern_flags)641 static int selinux_set_mnt_opts(struct super_block *sb,
642 void *mnt_opts,
643 unsigned long kern_flags,
644 unsigned long *set_kern_flags)
645 {
646 const struct cred *cred = current_cred();
647 struct superblock_security_struct *sbsec = selinux_superblock(sb);
648 struct dentry *root = sb->s_root;
649 struct selinux_mnt_opts *opts = mnt_opts;
650 struct inode_security_struct *root_isec;
651 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
652 u32 defcontext_sid = 0;
653 int rc = 0;
654
655 /*
656 * Specifying internal flags without providing a place to
657 * place the results is not allowed
658 */
659 if (kern_flags && !set_kern_flags)
660 return -EINVAL;
661
662 mutex_lock(&sbsec->lock);
663
664 if (!selinux_initialized()) {
665 if (!opts) {
666 /* Defer initialization until selinux_complete_init,
667 after the initial policy is loaded and the security
668 server is ready to handle calls. */
669 if (kern_flags & SECURITY_LSM_NATIVE_LABELS) {
670 sbsec->flags |= SE_SBNATIVE;
671 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
672 }
673 goto out;
674 }
675 rc = -EINVAL;
676 pr_warn("SELinux: Unable to set superblock options "
677 "before the security server is initialized\n");
678 goto out;
679 }
680
681 /*
682 * Binary mount data FS will come through this function twice. Once
683 * from an explicit call and once from the generic calls from the vfs.
684 * Since the generic VFS calls will not contain any security mount data
685 * we need to skip the double mount verification.
686 *
687 * This does open a hole in which we will not notice if the first
688 * mount using this sb set explicit options and a second mount using
689 * this sb does not set any security options. (The first options
690 * will be used for both mounts)
691 */
692 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
693 && !opts)
694 goto out;
695
696 root_isec = backing_inode_security_novalidate(root);
697
698 /*
699 * parse the mount options, check if they are valid sids.
700 * also check if someone is trying to mount the same sb more
701 * than once with different security options.
702 */
703 if (opts) {
704 if (opts->fscontext_sid) {
705 fscontext_sid = opts->fscontext_sid;
706 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
707 fscontext_sid))
708 goto out_double_mount;
709 sbsec->flags |= FSCONTEXT_MNT;
710 }
711 if (opts->context_sid) {
712 context_sid = opts->context_sid;
713 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
714 context_sid))
715 goto out_double_mount;
716 sbsec->flags |= CONTEXT_MNT;
717 }
718 if (opts->rootcontext_sid) {
719 rootcontext_sid = opts->rootcontext_sid;
720 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
721 rootcontext_sid))
722 goto out_double_mount;
723 sbsec->flags |= ROOTCONTEXT_MNT;
724 }
725 if (opts->defcontext_sid) {
726 defcontext_sid = opts->defcontext_sid;
727 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
728 defcontext_sid))
729 goto out_double_mount;
730 sbsec->flags |= DEFCONTEXT_MNT;
731 }
732 }
733
734 if (sbsec->flags & SE_SBINITIALIZED) {
735 /* previously mounted with options, but not on this attempt? */
736 if ((sbsec->flags & SE_MNTMASK) && !opts)
737 goto out_double_mount;
738 rc = 0;
739 goto out;
740 }
741
742 sbsec->creator_sid = current_sid();
743
744 if (strcmp(sb->s_type->name, "proc") == 0)
745 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
746
747 if (!strcmp(sb->s_type->name, "debugfs") ||
748 !strcmp(sb->s_type->name, "tracefs") ||
749 !strcmp(sb->s_type->name, "binder") ||
750 !strcmp(sb->s_type->name, "bpf") ||
751 !strcmp(sb->s_type->name, "pstore") ||
752 !strcmp(sb->s_type->name, "securityfs") ||
753 (selinux_policycap_functionfs_seclabel() &&
754 !strcmp(sb->s_type->name, "functionfs")))
755 sbsec->flags |= SE_SBGENFS;
756
757 if (!strcmp(sb->s_type->name, "sysfs") ||
758 !strcmp(sb->s_type->name, "cgroup") ||
759 !strcmp(sb->s_type->name, "cgroup2"))
760 sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR;
761
762 if (!sbsec->behavior) {
763 /*
764 * Determine the labeling behavior to use for this
765 * filesystem type.
766 */
767 rc = security_fs_use(sb);
768 if (rc) {
769 pr_warn("%s: security_fs_use(%s) returned %d\n",
770 __func__, sb->s_type->name, rc);
771 goto out;
772 }
773 }
774
775 /*
776 * If this is a user namespace mount and the filesystem type is not
777 * explicitly whitelisted, then no contexts are allowed on the command
778 * line and security labels must be ignored.
779 */
780 if (sb->s_user_ns != &init_user_ns &&
781 strcmp(sb->s_type->name, "tmpfs") &&
782 strcmp(sb->s_type->name, "ramfs") &&
783 strcmp(sb->s_type->name, "devpts") &&
784 strcmp(sb->s_type->name, "overlay")) {
785 if (context_sid || fscontext_sid || rootcontext_sid ||
786 defcontext_sid) {
787 rc = -EACCES;
788 goto out;
789 }
790 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
791 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
792 rc = security_transition_sid(current_sid(),
793 current_sid(),
794 SECCLASS_FILE, NULL,
795 &sbsec->mntpoint_sid);
796 if (rc)
797 goto out;
798 }
799 goto out_set_opts;
800 }
801
802 /* sets the context of the superblock for the fs being mounted. */
803 if (fscontext_sid) {
804 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
805 if (rc)
806 goto out;
807
808 sbsec->sid = fscontext_sid;
809 }
810
811 /*
812 * Switch to using mount point labeling behavior.
813 * sets the label used on all file below the mountpoint, and will set
814 * the superblock context if not already set.
815 */
816 if (sbsec->flags & SE_SBNATIVE) {
817 /*
818 * This means we are initializing a superblock that has been
819 * mounted before the SELinux was initialized and the
820 * filesystem requested native labeling. We had already
821 * returned SECURITY_LSM_NATIVE_LABELS in *set_kern_flags
822 * in the original mount attempt, so now we just need to set
823 * the SECURITY_FS_USE_NATIVE behavior.
824 */
825 sbsec->behavior = SECURITY_FS_USE_NATIVE;
826 } else if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
827 sbsec->behavior = SECURITY_FS_USE_NATIVE;
828 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
829 }
830
831 if (context_sid) {
832 if (!fscontext_sid) {
833 rc = may_context_mount_sb_relabel(context_sid, sbsec,
834 cred);
835 if (rc)
836 goto out;
837 sbsec->sid = context_sid;
838 } else {
839 rc = may_context_mount_inode_relabel(context_sid, sbsec,
840 cred);
841 if (rc)
842 goto out;
843 }
844 if (!rootcontext_sid)
845 rootcontext_sid = context_sid;
846
847 sbsec->mntpoint_sid = context_sid;
848 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
849 }
850
851 if (rootcontext_sid) {
852 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
853 cred);
854 if (rc)
855 goto out;
856
857 root_isec->sid = rootcontext_sid;
858 root_isec->initialized = LABEL_INITIALIZED;
859 }
860
861 if (defcontext_sid) {
862 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
863 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
864 rc = -EINVAL;
865 pr_warn("SELinux: defcontext option is "
866 "invalid for this filesystem type\n");
867 goto out;
868 }
869
870 if (defcontext_sid != sbsec->def_sid) {
871 rc = may_context_mount_inode_relabel(defcontext_sid,
872 sbsec, cred);
873 if (rc)
874 goto out;
875 }
876
877 sbsec->def_sid = defcontext_sid;
878 }
879
880 out_set_opts:
881 rc = sb_finish_set_opts(sb);
882 out:
883 mutex_unlock(&sbsec->lock);
884 return rc;
885 out_double_mount:
886 rc = -EINVAL;
887 pr_warn("SELinux: mount invalid. Same superblock, different "
888 "security settings for (dev %s, type %s)\n", sb->s_id,
889 sb->s_type->name);
890 goto out;
891 }
892
selinux_cmp_sb_context(const struct super_block * oldsb,const struct super_block * newsb)893 static int selinux_cmp_sb_context(const struct super_block *oldsb,
894 const struct super_block *newsb)
895 {
896 struct superblock_security_struct *old = selinux_superblock(oldsb);
897 struct superblock_security_struct *new = selinux_superblock(newsb);
898 char oldflags = old->flags & SE_MNTMASK;
899 char newflags = new->flags & SE_MNTMASK;
900
901 if (oldflags != newflags)
902 goto mismatch;
903 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
904 goto mismatch;
905 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
906 goto mismatch;
907 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
908 goto mismatch;
909 if (oldflags & ROOTCONTEXT_MNT) {
910 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
911 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
912 if (oldroot->sid != newroot->sid)
913 goto mismatch;
914 }
915 if (old->creator_sid != new->creator_sid)
916 goto mismatch;
917 return 0;
918 mismatch:
919 pr_warn("SELinux: mount invalid. Same superblock, "
920 "different security settings for (dev %s, "
921 "type %s)\n", newsb->s_id, newsb->s_type->name);
922 return -EBUSY;
923 }
924
selinux_sb_clone_mnt_opts(const struct super_block * oldsb,struct super_block * newsb,unsigned long kern_flags,unsigned long * set_kern_flags)925 static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
926 struct super_block *newsb,
927 unsigned long kern_flags,
928 unsigned long *set_kern_flags)
929 {
930 int rc = 0;
931 const struct superblock_security_struct *oldsbsec =
932 selinux_superblock(oldsb);
933 struct superblock_security_struct *newsbsec = selinux_superblock(newsb);
934
935 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
936 int set_context = (oldsbsec->flags & CONTEXT_MNT);
937 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
938
939 /*
940 * Specifying internal flags without providing a place to
941 * place the results is not allowed.
942 */
943 if (kern_flags && !set_kern_flags)
944 return -EINVAL;
945
946 mutex_lock(&newsbsec->lock);
947
948 /*
949 * if the parent was able to be mounted it clearly had no special lsm
950 * mount options. thus we can safely deal with this superblock later
951 */
952 if (!selinux_initialized()) {
953 if (kern_flags & SECURITY_LSM_NATIVE_LABELS) {
954 newsbsec->flags |= SE_SBNATIVE;
955 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
956 }
957 goto out;
958 }
959
960 /* how can we clone if the old one wasn't set up?? */
961 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
962
963 /* if fs is reusing a sb, make sure that the contexts match */
964 if (newsbsec->flags & SE_SBINITIALIZED) {
965 mutex_unlock(&newsbsec->lock);
966 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
967 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
968 return selinux_cmp_sb_context(oldsb, newsb);
969 }
970
971 newsbsec->flags = oldsbsec->flags;
972
973 newsbsec->sid = oldsbsec->sid;
974 newsbsec->def_sid = oldsbsec->def_sid;
975 newsbsec->behavior = oldsbsec->behavior;
976 newsbsec->creator_sid = oldsbsec->creator_sid;
977
978 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
979 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
980 rc = security_fs_use(newsb);
981 if (rc)
982 goto out;
983 }
984
985 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
986 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
987 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
988 }
989
990 if (set_context) {
991 u32 sid = oldsbsec->mntpoint_sid;
992
993 if (!set_fscontext)
994 newsbsec->sid = sid;
995 if (!set_rootcontext) {
996 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
997 newisec->sid = sid;
998 }
999 newsbsec->mntpoint_sid = sid;
1000 }
1001 if (set_rootcontext) {
1002 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
1003 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
1004
1005 newisec->sid = oldisec->sid;
1006 }
1007
1008 sb_finish_set_opts(newsb);
1009 out:
1010 mutex_unlock(&newsbsec->lock);
1011 return rc;
1012 }
1013
1014 /*
1015 * NOTE: the caller is responsible for freeing the memory even if on error.
1016 */
selinux_add_opt(int token,const char * s,void ** mnt_opts)1017 static int selinux_add_opt(int token, const char *s, void **mnt_opts)
1018 {
1019 struct selinux_mnt_opts *opts = *mnt_opts;
1020 u32 *dst_sid;
1021 int rc;
1022
1023 if (token == Opt_seclabel)
1024 /* eaten and completely ignored */
1025 return 0;
1026 if (!s)
1027 return -EINVAL;
1028
1029 if (!selinux_initialized()) {
1030 pr_warn("SELinux: Unable to set superblock options before the security server is initialized\n");
1031 return -EINVAL;
1032 }
1033
1034 if (!opts) {
1035 opts = kzalloc_obj(*opts);
1036 if (!opts)
1037 return -ENOMEM;
1038 *mnt_opts = opts;
1039 }
1040
1041 switch (token) {
1042 case Opt_context:
1043 if (opts->context_sid || opts->defcontext_sid)
1044 goto err;
1045 dst_sid = &opts->context_sid;
1046 break;
1047 case Opt_fscontext:
1048 if (opts->fscontext_sid)
1049 goto err;
1050 dst_sid = &opts->fscontext_sid;
1051 break;
1052 case Opt_rootcontext:
1053 if (opts->rootcontext_sid)
1054 goto err;
1055 dst_sid = &opts->rootcontext_sid;
1056 break;
1057 case Opt_defcontext:
1058 if (opts->context_sid || opts->defcontext_sid)
1059 goto err;
1060 dst_sid = &opts->defcontext_sid;
1061 break;
1062 default:
1063 WARN_ON(1);
1064 return -EINVAL;
1065 }
1066 rc = security_context_str_to_sid(s, dst_sid, GFP_KERNEL);
1067 if (rc)
1068 pr_warn("SELinux: security_context_str_to_sid (%s) failed with errno=%d\n",
1069 s, rc);
1070 return rc;
1071
1072 err:
1073 pr_warn(SEL_MOUNT_FAIL_MSG);
1074 return -EINVAL;
1075 }
1076
show_sid(struct seq_file * m,u32 sid)1077 static int show_sid(struct seq_file *m, u32 sid)
1078 {
1079 char *context = NULL;
1080 u32 len;
1081 int rc;
1082
1083 rc = security_sid_to_context(sid, &context, &len);
1084 if (!rc) {
1085 bool has_comma = strchr(context, ',');
1086
1087 seq_putc(m, '=');
1088 if (has_comma)
1089 seq_putc(m, '\"');
1090 seq_escape(m, context, "\"\n\\");
1091 if (has_comma)
1092 seq_putc(m, '\"');
1093 }
1094 kfree(context);
1095 return rc;
1096 }
1097
selinux_sb_show_options(struct seq_file * m,struct super_block * sb)1098 static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1099 {
1100 struct superblock_security_struct *sbsec = selinux_superblock(sb);
1101 int rc;
1102
1103 if (!(sbsec->flags & SE_SBINITIALIZED))
1104 return 0;
1105
1106 if (!selinux_initialized())
1107 return 0;
1108
1109 if (sbsec->flags & FSCONTEXT_MNT) {
1110 seq_putc(m, ',');
1111 seq_puts(m, FSCONTEXT_STR);
1112 rc = show_sid(m, sbsec->sid);
1113 if (rc)
1114 return rc;
1115 }
1116 if (sbsec->flags & CONTEXT_MNT) {
1117 seq_putc(m, ',');
1118 seq_puts(m, CONTEXT_STR);
1119 rc = show_sid(m, sbsec->mntpoint_sid);
1120 if (rc)
1121 return rc;
1122 }
1123 if (sbsec->flags & DEFCONTEXT_MNT) {
1124 seq_putc(m, ',');
1125 seq_puts(m, DEFCONTEXT_STR);
1126 rc = show_sid(m, sbsec->def_sid);
1127 if (rc)
1128 return rc;
1129 }
1130 if (sbsec->flags & ROOTCONTEXT_MNT) {
1131 struct dentry *root = sb->s_root;
1132 struct inode_security_struct *isec = backing_inode_security(root);
1133 seq_putc(m, ',');
1134 seq_puts(m, ROOTCONTEXT_STR);
1135 rc = show_sid(m, isec->sid);
1136 if (rc)
1137 return rc;
1138 }
1139 if (sbsec->flags & SBLABEL_MNT) {
1140 seq_putc(m, ',');
1141 seq_puts(m, SECLABEL_STR);
1142 }
1143 return 0;
1144 }
1145
inode_mode_to_security_class(umode_t mode)1146 static inline u16 inode_mode_to_security_class(umode_t mode)
1147 {
1148 switch (mode & S_IFMT) {
1149 case S_IFSOCK:
1150 return SECCLASS_SOCK_FILE;
1151 case S_IFLNK:
1152 return SECCLASS_LNK_FILE;
1153 case S_IFREG:
1154 return SECCLASS_FILE;
1155 case S_IFBLK:
1156 return SECCLASS_BLK_FILE;
1157 case S_IFDIR:
1158 return SECCLASS_DIR;
1159 case S_IFCHR:
1160 return SECCLASS_CHR_FILE;
1161 case S_IFIFO:
1162 return SECCLASS_FIFO_FILE;
1163
1164 }
1165
1166 return SECCLASS_FILE;
1167 }
1168
default_protocol_stream(int protocol)1169 static inline int default_protocol_stream(int protocol)
1170 {
1171 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP ||
1172 protocol == IPPROTO_MPTCP);
1173 }
1174
default_protocol_dgram(int protocol)1175 static inline int default_protocol_dgram(int protocol)
1176 {
1177 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1178 }
1179
socket_type_to_security_class(int family,int type,int protocol)1180 static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1181 {
1182 bool extsockclass = selinux_policycap_extsockclass();
1183
1184 switch (family) {
1185 case PF_UNIX:
1186 switch (type) {
1187 case SOCK_STREAM:
1188 case SOCK_SEQPACKET:
1189 return SECCLASS_UNIX_STREAM_SOCKET;
1190 case SOCK_DGRAM:
1191 case SOCK_RAW:
1192 return SECCLASS_UNIX_DGRAM_SOCKET;
1193 }
1194 break;
1195 case PF_INET:
1196 case PF_INET6:
1197 switch (type) {
1198 case SOCK_STREAM:
1199 case SOCK_SEQPACKET:
1200 if (default_protocol_stream(protocol))
1201 return SECCLASS_TCP_SOCKET;
1202 else if (extsockclass && protocol == IPPROTO_SCTP)
1203 return SECCLASS_SCTP_SOCKET;
1204 else
1205 return SECCLASS_RAWIP_SOCKET;
1206 case SOCK_DGRAM:
1207 if (default_protocol_dgram(protocol))
1208 return SECCLASS_UDP_SOCKET;
1209 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1210 protocol == IPPROTO_ICMPV6))
1211 return SECCLASS_ICMP_SOCKET;
1212 else
1213 return SECCLASS_RAWIP_SOCKET;
1214 default:
1215 return SECCLASS_RAWIP_SOCKET;
1216 }
1217 break;
1218 case PF_NETLINK:
1219 switch (protocol) {
1220 case NETLINK_ROUTE:
1221 return SECCLASS_NETLINK_ROUTE_SOCKET;
1222 case NETLINK_SOCK_DIAG:
1223 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1224 case NETLINK_NFLOG:
1225 return SECCLASS_NETLINK_NFLOG_SOCKET;
1226 case NETLINK_XFRM:
1227 return SECCLASS_NETLINK_XFRM_SOCKET;
1228 case NETLINK_SELINUX:
1229 return SECCLASS_NETLINK_SELINUX_SOCKET;
1230 case NETLINK_ISCSI:
1231 return SECCLASS_NETLINK_ISCSI_SOCKET;
1232 case NETLINK_AUDIT:
1233 return SECCLASS_NETLINK_AUDIT_SOCKET;
1234 case NETLINK_FIB_LOOKUP:
1235 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1236 case NETLINK_CONNECTOR:
1237 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1238 case NETLINK_NETFILTER:
1239 return SECCLASS_NETLINK_NETFILTER_SOCKET;
1240 case NETLINK_DNRTMSG:
1241 return SECCLASS_NETLINK_DNRT_SOCKET;
1242 case NETLINK_KOBJECT_UEVENT:
1243 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
1244 case NETLINK_GENERIC:
1245 return SECCLASS_NETLINK_GENERIC_SOCKET;
1246 case NETLINK_SCSITRANSPORT:
1247 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1248 case NETLINK_RDMA:
1249 return SECCLASS_NETLINK_RDMA_SOCKET;
1250 case NETLINK_CRYPTO:
1251 return SECCLASS_NETLINK_CRYPTO_SOCKET;
1252 default:
1253 return SECCLASS_NETLINK_SOCKET;
1254 }
1255 case PF_PACKET:
1256 return SECCLASS_PACKET_SOCKET;
1257 case PF_KEY:
1258 return SECCLASS_KEY_SOCKET;
1259 case PF_APPLETALK:
1260 return SECCLASS_APPLETALK_SOCKET;
1261 }
1262
1263 if (extsockclass) {
1264 switch (family) {
1265 case PF_AX25:
1266 return SECCLASS_AX25_SOCKET;
1267 case PF_IPX:
1268 return SECCLASS_IPX_SOCKET;
1269 case PF_NETROM:
1270 return SECCLASS_NETROM_SOCKET;
1271 case PF_ATMPVC:
1272 return SECCLASS_ATMPVC_SOCKET;
1273 case PF_X25:
1274 return SECCLASS_X25_SOCKET;
1275 case PF_ROSE:
1276 return SECCLASS_ROSE_SOCKET;
1277 case PF_DECnet:
1278 return SECCLASS_DECNET_SOCKET;
1279 case PF_ATMSVC:
1280 return SECCLASS_ATMSVC_SOCKET;
1281 case PF_RDS:
1282 return SECCLASS_RDS_SOCKET;
1283 case PF_IRDA:
1284 return SECCLASS_IRDA_SOCKET;
1285 case PF_PPPOX:
1286 return SECCLASS_PPPOX_SOCKET;
1287 case PF_LLC:
1288 return SECCLASS_LLC_SOCKET;
1289 case PF_CAN:
1290 return SECCLASS_CAN_SOCKET;
1291 case PF_TIPC:
1292 return SECCLASS_TIPC_SOCKET;
1293 case PF_BLUETOOTH:
1294 return SECCLASS_BLUETOOTH_SOCKET;
1295 case PF_IUCV:
1296 return SECCLASS_IUCV_SOCKET;
1297 case PF_RXRPC:
1298 return SECCLASS_RXRPC_SOCKET;
1299 case PF_ISDN:
1300 return SECCLASS_ISDN_SOCKET;
1301 case PF_PHONET:
1302 return SECCLASS_PHONET_SOCKET;
1303 case PF_IEEE802154:
1304 return SECCLASS_IEEE802154_SOCKET;
1305 case PF_CAIF:
1306 return SECCLASS_CAIF_SOCKET;
1307 case PF_ALG:
1308 return SECCLASS_ALG_SOCKET;
1309 case PF_NFC:
1310 return SECCLASS_NFC_SOCKET;
1311 case PF_VSOCK:
1312 return SECCLASS_VSOCK_SOCKET;
1313 case PF_KCM:
1314 return SECCLASS_KCM_SOCKET;
1315 case PF_QIPCRTR:
1316 return SECCLASS_QIPCRTR_SOCKET;
1317 case PF_SMC:
1318 return SECCLASS_SMC_SOCKET;
1319 case PF_XDP:
1320 return SECCLASS_XDP_SOCKET;
1321 case PF_MCTP:
1322 return SECCLASS_MCTP_SOCKET;
1323 #if PF_MAX > 46
1324 #error New address family defined, please update this function.
1325 #endif
1326 }
1327 }
1328
1329 return SECCLASS_SOCKET;
1330 }
1331
selinux_genfs_get_sid(struct dentry * dentry,u16 tclass,u16 flags,u32 * sid)1332 static int selinux_genfs_get_sid(struct dentry *dentry,
1333 u16 tclass,
1334 u16 flags,
1335 u32 *sid)
1336 {
1337 int rc;
1338 struct super_block *sb = dentry->d_sb;
1339 char *buffer, *path;
1340
1341 buffer = kmalloc(PATH_MAX, GFP_KERNEL);
1342 if (!buffer)
1343 return -ENOMEM;
1344
1345 path = dentry_path_raw(dentry, buffer, PATH_MAX);
1346 if (IS_ERR(path))
1347 rc = PTR_ERR(path);
1348 else {
1349 if (flags & SE_SBPROC) {
1350 /* each process gets a /proc/PID/ entry. Strip off the
1351 * PID part to get a valid selinux labeling.
1352 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1353 while (path[1] >= '0' && path[1] <= '9') {
1354 path[1] = '/';
1355 path++;
1356 }
1357 }
1358 rc = security_genfs_sid(sb->s_type->name,
1359 path, tclass, sid);
1360 if (rc == -ENOENT) {
1361 /* No match in policy, mark as unlabeled. */
1362 *sid = SECINITSID_UNLABELED;
1363 rc = 0;
1364 }
1365 }
1366 kfree(buffer);
1367 return rc;
1368 }
1369
inode_doinit_use_xattr(struct inode * inode,struct dentry * dentry,u32 def_sid,u32 * sid)1370 static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry,
1371 u32 def_sid, u32 *sid)
1372 {
1373 #define INITCONTEXTLEN 255
1374 char *context;
1375 unsigned int len;
1376 int rc;
1377
1378 len = INITCONTEXTLEN;
1379 context = kmalloc(len + 1, GFP_NOFS);
1380 if (!context)
1381 return -ENOMEM;
1382
1383 context[len] = '\0';
1384 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1385 if (rc == -ERANGE) {
1386 kfree(context);
1387
1388 /* Need a larger buffer. Query for the right size. */
1389 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1390 if (rc < 0)
1391 return rc;
1392
1393 len = rc;
1394 context = kmalloc(len + 1, GFP_NOFS);
1395 if (!context)
1396 return -ENOMEM;
1397
1398 context[len] = '\0';
1399 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX,
1400 context, len);
1401 }
1402 if (rc < 0) {
1403 kfree(context);
1404 if (rc != -ENODATA) {
1405 pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%llu\n",
1406 __func__, -rc, inode->i_sb->s_id, inode->i_ino);
1407 return rc;
1408 }
1409 *sid = def_sid;
1410 return 0;
1411 }
1412
1413 rc = security_context_to_sid_default(context, rc, sid,
1414 def_sid, GFP_NOFS);
1415 if (rc) {
1416 char *dev = inode->i_sb->s_id;
1417 u64 ino = inode->i_ino;
1418
1419 if (rc == -EINVAL) {
1420 pr_notice_ratelimited("SELinux: inode=%llu on dev=%s was found to have an invalid context=%s. This indicates you may need to relabel the inode or the filesystem in question.\n",
1421 ino, dev, context);
1422 } else {
1423 pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%llu\n",
1424 __func__, context, -rc, dev, ino);
1425 }
1426 }
1427 kfree(context);
1428 return 0;
1429 }
1430
1431 /* The inode's security attributes must be initialized before first use. */
inode_doinit_with_dentry(struct inode * inode,struct dentry * opt_dentry)1432 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1433 {
1434 struct superblock_security_struct *sbsec = NULL;
1435 struct inode_security_struct *isec = selinux_inode(inode);
1436 u32 task_sid, sid = 0;
1437 u16 sclass;
1438 struct dentry *dentry;
1439 int rc = 0;
1440
1441 /* check below is racy, but we will recheck with lock held */
1442 if (data_race(isec->initialized == LABEL_INITIALIZED))
1443 return 0;
1444
1445 spin_lock(&isec->lock);
1446 if (isec->initialized == LABEL_INITIALIZED)
1447 goto out_unlock;
1448
1449 if (isec->sclass == SECCLASS_FILE)
1450 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1451
1452 sbsec = selinux_superblock(inode->i_sb);
1453 if (!(sbsec->flags & SE_SBINITIALIZED)) {
1454 /* Defer initialization until selinux_complete_init,
1455 after the initial policy is loaded and the security
1456 server is ready to handle calls. */
1457 spin_lock(&sbsec->isec_lock);
1458 if (list_empty(&isec->list))
1459 list_add(&isec->list, &sbsec->isec_head);
1460 spin_unlock(&sbsec->isec_lock);
1461 goto out_unlock;
1462 }
1463
1464 sclass = isec->sclass;
1465 task_sid = isec->task_sid;
1466 sid = isec->sid;
1467 isec->initialized = LABEL_PENDING;
1468 spin_unlock(&isec->lock);
1469
1470 switch (sbsec->behavior) {
1471 /*
1472 * In case of SECURITY_FS_USE_NATIVE we need to re-fetch the labels
1473 * via xattr when called from delayed_superblock_init().
1474 */
1475 case SECURITY_FS_USE_NATIVE:
1476 case SECURITY_FS_USE_XATTR:
1477 if (!(inode->i_opflags & IOP_XATTR)) {
1478 sid = sbsec->def_sid;
1479 break;
1480 }
1481 /* Need a dentry, since the xattr API requires one.
1482 Life would be simpler if we could just pass the inode. */
1483 if (opt_dentry) {
1484 /* Called from d_instantiate or d_splice_alias. */
1485 dentry = dget(opt_dentry);
1486 } else {
1487 /*
1488 * Called from selinux_complete_init, try to find a dentry.
1489 * Some filesystems really want a connected one, so try
1490 * that first. We could split SECURITY_FS_USE_XATTR in
1491 * two, depending upon that...
1492 */
1493 dentry = d_find_alias(inode);
1494 if (!dentry)
1495 dentry = d_find_any_alias(inode);
1496 }
1497 if (!dentry) {
1498 /*
1499 * this is can be hit on boot when a file is accessed
1500 * before the policy is loaded. When we load policy we
1501 * may find inodes that have no dentry on the
1502 * sbsec->isec_head list. No reason to complain as these
1503 * will get fixed up the next time we go through
1504 * inode_doinit with a dentry, before these inodes could
1505 * be used again by userspace.
1506 */
1507 goto out_invalid;
1508 }
1509
1510 rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid,
1511 &sid);
1512 dput(dentry);
1513 if (rc)
1514 goto out;
1515 break;
1516 case SECURITY_FS_USE_TASK:
1517 sid = task_sid;
1518 break;
1519 case SECURITY_FS_USE_TRANS:
1520 /* Default to the fs SID. */
1521 sid = sbsec->sid;
1522
1523 /* Try to obtain a transition SID. */
1524 rc = security_transition_sid(task_sid, sid,
1525 sclass, NULL, &sid);
1526 if (rc)
1527 goto out;
1528 break;
1529 case SECURITY_FS_USE_MNTPOINT:
1530 sid = sbsec->mntpoint_sid;
1531 break;
1532 default:
1533 /* Default to the fs superblock SID. */
1534 sid = sbsec->sid;
1535
1536 if ((sbsec->flags & SE_SBGENFS) &&
1537 (!S_ISLNK(inode->i_mode) ||
1538 selinux_policycap_genfs_seclabel_symlinks())) {
1539 /* We must have a dentry to determine the label on
1540 * procfs inodes */
1541 if (opt_dentry) {
1542 /* Called from d_instantiate or
1543 * d_splice_alias. */
1544 dentry = dget(opt_dentry);
1545 } else {
1546 /* Called from selinux_complete_init, try to
1547 * find a dentry. Some filesystems really want
1548 * a connected one, so try that first.
1549 */
1550 dentry = d_find_alias(inode);
1551 if (!dentry)
1552 dentry = d_find_any_alias(inode);
1553 }
1554 /*
1555 * This can be hit on boot when a file is accessed
1556 * before the policy is loaded. When we load policy we
1557 * may find inodes that have no dentry on the
1558 * sbsec->isec_head list. No reason to complain as
1559 * these will get fixed up the next time we go through
1560 * inode_doinit() with a dentry, before these inodes
1561 * could be used again by userspace.
1562 */
1563 if (!dentry)
1564 goto out_invalid;
1565 rc = selinux_genfs_get_sid(dentry, sclass,
1566 sbsec->flags, &sid);
1567 if (rc) {
1568 dput(dentry);
1569 goto out;
1570 }
1571
1572 if ((sbsec->flags & SE_SBGENFS_XATTR) &&
1573 (inode->i_opflags & IOP_XATTR)) {
1574 rc = inode_doinit_use_xattr(inode, dentry,
1575 sid, &sid);
1576 if (rc) {
1577 dput(dentry);
1578 goto out;
1579 }
1580 }
1581 dput(dentry);
1582 }
1583 break;
1584 }
1585
1586 out:
1587 spin_lock(&isec->lock);
1588 if (isec->initialized == LABEL_PENDING) {
1589 if (rc) {
1590 isec->initialized = LABEL_INVALID;
1591 goto out_unlock;
1592 }
1593 isec->initialized = LABEL_INITIALIZED;
1594 isec->sid = sid;
1595 }
1596
1597 out_unlock:
1598 spin_unlock(&isec->lock);
1599 return rc;
1600
1601 out_invalid:
1602 spin_lock(&isec->lock);
1603 if (isec->initialized == LABEL_PENDING) {
1604 isec->initialized = LABEL_INVALID;
1605 isec->sid = sid;
1606 }
1607 spin_unlock(&isec->lock);
1608 return 0;
1609 }
1610
1611 /* Convert a Linux signal to an access vector. */
signal_to_av(int sig)1612 static inline u32 signal_to_av(int sig)
1613 {
1614 u32 perm = 0;
1615
1616 switch (sig) {
1617 case SIGCHLD:
1618 /* Commonly granted from child to parent. */
1619 perm = PROCESS__SIGCHLD;
1620 break;
1621 case SIGKILL:
1622 /* Cannot be caught or ignored */
1623 perm = PROCESS__SIGKILL;
1624 break;
1625 case SIGSTOP:
1626 /* Cannot be caught or ignored */
1627 perm = PROCESS__SIGSTOP;
1628 break;
1629 default:
1630 /* All other signals. */
1631 perm = PROCESS__SIGNAL;
1632 break;
1633 }
1634
1635 return perm;
1636 }
1637
1638 #if CAP_LAST_CAP > 63
1639 #error Fix SELinux to handle capabilities > 63.
1640 #endif
1641
1642 /* Check whether a task is allowed to use a capability. */
cred_has_capability(const struct cred * cred,int cap,unsigned int opts,bool initns)1643 static int cred_has_capability(const struct cred *cred,
1644 int cap, unsigned int opts, bool initns)
1645 {
1646 struct common_audit_data ad;
1647 struct av_decision avd;
1648 u16 sclass;
1649 u32 sid = cred_sid(cred);
1650 u32 av = CAP_TO_MASK(cap);
1651 int rc;
1652
1653 ad.type = LSM_AUDIT_DATA_CAP;
1654 ad.u.cap = cap;
1655
1656 switch (CAP_TO_INDEX(cap)) {
1657 case 0:
1658 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
1659 break;
1660 case 1:
1661 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
1662 break;
1663 default:
1664 pr_err("SELinux: out of range capability %d\n", cap);
1665 return -EINVAL;
1666 }
1667
1668 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
1669 if (!(opts & CAP_OPT_NOAUDIT)) {
1670 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
1671 if (rc2)
1672 return rc2;
1673 }
1674 return rc;
1675 }
1676
1677 /* Check whether a task has a particular permission to an inode.
1678 The 'adp' parameter is optional and allows other audit
1679 data to be passed (e.g. the dentry). */
inode_has_perm(const struct cred * cred,struct inode * inode,u32 perms,struct common_audit_data * adp)1680 static int inode_has_perm(const struct cred *cred,
1681 struct inode *inode,
1682 u32 perms,
1683 struct common_audit_data *adp)
1684 {
1685 struct inode_security_struct *isec;
1686 u32 sid;
1687
1688 if (unlikely(IS_PRIVATE(inode)))
1689 return 0;
1690
1691 sid = cred_sid(cred);
1692 isec = selinux_inode(inode);
1693
1694 return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
1695 }
1696
1697 /* Same as inode_has_perm, but pass explicit audit data containing
1698 the dentry to help the auditing code to more easily generate the
1699 pathname if needed. */
dentry_has_perm(const struct cred * cred,struct dentry * dentry,u32 av)1700 static inline int dentry_has_perm(const struct cred *cred,
1701 struct dentry *dentry,
1702 u32 av)
1703 {
1704 struct common_audit_data ad;
1705 struct inode *inode = d_backing_inode(dentry);
1706 struct inode_security_struct *isec = selinux_inode(inode);
1707
1708 ad.type = LSM_AUDIT_DATA_DENTRY;
1709 ad.u.dentry = dentry;
1710 /* check below is racy, but revalidate will recheck with lock held */
1711 if (data_race(unlikely(isec->initialized != LABEL_INITIALIZED)))
1712 __inode_security_revalidate(inode, dentry, true);
1713 return inode_has_perm(cred, inode, av, &ad);
1714 }
1715
1716 /* Same as inode_has_perm, but pass explicit audit data containing
1717 the path to help the auditing code to more easily generate the
1718 pathname if needed. */
path_has_perm(const struct cred * cred,const struct path * path,u32 av)1719 static inline int path_has_perm(const struct cred *cred,
1720 const struct path *path,
1721 u32 av)
1722 {
1723 struct common_audit_data ad;
1724 struct inode *inode = d_backing_inode(path->dentry);
1725 struct inode_security_struct *isec = selinux_inode(inode);
1726
1727 ad.type = LSM_AUDIT_DATA_PATH;
1728 ad.u.path = *path;
1729 /* check below is racy, but revalidate will recheck with lock held */
1730 if (data_race(unlikely(isec->initialized != LABEL_INITIALIZED)))
1731 __inode_security_revalidate(inode, path->dentry, true);
1732 return inode_has_perm(cred, inode, av, &ad);
1733 }
1734
1735 /* Same as path_has_perm, but uses the inode from the file struct. */
file_path_has_perm(const struct cred * cred,struct file * file,u32 av)1736 static inline int file_path_has_perm(const struct cred *cred,
1737 struct file *file,
1738 u32 av)
1739 {
1740 struct common_audit_data ad;
1741
1742 ad.type = LSM_AUDIT_DATA_FILE;
1743 ad.u.file = file;
1744 return inode_has_perm(cred, file_inode(file), av, &ad);
1745 }
1746
1747 #ifdef CONFIG_BPF_SYSCALL
1748 static int bpf_fd_pass(const struct file *file, u32 sid);
1749 #endif
1750
__file_has_perm(const struct cred * cred,const struct file * file,u32 av,bool bf_user_file)1751 static int __file_has_perm(const struct cred *cred, const struct file *file,
1752 u32 av, bool bf_user_file)
1753
1754 {
1755 struct common_audit_data ad;
1756 struct inode *inode;
1757 u32 ssid = cred_sid(cred);
1758 u32 tsid_fd;
1759 int rc;
1760
1761 if (bf_user_file) {
1762 struct backing_file_security_struct *bfsec;
1763 const struct path *path;
1764
1765 if (WARN_ON(!(file->f_mode & FMODE_BACKING)))
1766 return -EIO;
1767
1768 bfsec = selinux_backing_file(file);
1769 path = backing_file_user_path(file);
1770 tsid_fd = bfsec->uf_sid;
1771 inode = d_inode(path->dentry);
1772
1773 ad.type = LSM_AUDIT_DATA_PATH;
1774 ad.u.path = *path;
1775 } else {
1776 struct file_security_struct *fsec = selinux_file(file);
1777
1778 tsid_fd = fsec->sid;
1779 inode = file_inode(file);
1780
1781 ad.type = LSM_AUDIT_DATA_FILE;
1782 ad.u.file = file;
1783 }
1784
1785 if (ssid != tsid_fd) {
1786 rc = avc_has_perm(ssid, tsid_fd, SECCLASS_FD, FD__USE, &ad);
1787 if (rc)
1788 return rc;
1789 }
1790
1791 #ifdef CONFIG_BPF_SYSCALL
1792 /* regardless of backing vs user file, use the underlying file here */
1793 rc = bpf_fd_pass(file, ssid);
1794 if (rc)
1795 return rc;
1796 #endif
1797
1798 /* av is zero if only checking access to the descriptor. */
1799 if (av)
1800 return inode_has_perm(cred, inode, av, &ad);
1801
1802 return 0;
1803 }
1804
1805 /* Check whether a task can use an open file descriptor to
1806 access an inode in a given way. Check access to the
1807 descriptor itself, and then use dentry_has_perm to
1808 check a particular permission to the file.
1809 Access to the descriptor is implicitly granted if it
1810 has the same SID as the process. If av is zero, then
1811 access to the file is not checked, e.g. for cases
1812 where only the descriptor is affected like seek. */
file_has_perm(const struct cred * cred,const struct file * file,u32 av)1813 static inline int file_has_perm(const struct cred *cred,
1814 const struct file *file, u32 av)
1815 {
1816 return __file_has_perm(cred, file, av, false);
1817 }
1818
1819 /*
1820 * Determine the label for an inode that might be unioned.
1821 */
1822 static int
selinux_determine_inode_label(const struct cred_security_struct * crsec,struct inode * dir,const struct qstr * name,u16 tclass,u32 * _new_isid)1823 selinux_determine_inode_label(const struct cred_security_struct *crsec,
1824 struct inode *dir,
1825 const struct qstr *name, u16 tclass,
1826 u32 *_new_isid)
1827 {
1828 const struct superblock_security_struct *sbsec =
1829 selinux_superblock(dir->i_sb);
1830
1831 if ((sbsec->flags & SE_SBINITIALIZED) &&
1832 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1833 *_new_isid = sbsec->mntpoint_sid;
1834 } else if ((sbsec->flags & SBLABEL_MNT) &&
1835 crsec->create_sid) {
1836 *_new_isid = crsec->create_sid;
1837 } else {
1838 const struct inode_security_struct *dsec = inode_security(dir);
1839 return security_transition_sid(crsec->sid,
1840 dsec->sid, tclass,
1841 name, _new_isid);
1842 }
1843
1844 return 0;
1845 }
1846
1847 /* Check whether a task can create a file. */
may_create(struct inode * dir,struct dentry * dentry,u16 tclass)1848 static int may_create(struct inode *dir,
1849 struct dentry *dentry,
1850 u16 tclass)
1851 {
1852 const struct cred_security_struct *crsec = selinux_cred(current_cred());
1853 struct inode_security_struct *dsec;
1854 struct superblock_security_struct *sbsec;
1855 u32 sid, newsid;
1856 struct common_audit_data ad;
1857 int rc;
1858
1859 dsec = inode_security(dir);
1860 sbsec = selinux_superblock(dir->i_sb);
1861
1862 sid = crsec->sid;
1863
1864 ad.type = LSM_AUDIT_DATA_DENTRY;
1865 ad.u.dentry = dentry;
1866
1867 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
1868 DIR__ADD_NAME | DIR__SEARCH,
1869 &ad);
1870 if (rc)
1871 return rc;
1872
1873 rc = selinux_determine_inode_label(crsec, dir, &dentry->d_name, tclass,
1874 &newsid);
1875 if (rc)
1876 return rc;
1877
1878 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
1879 if (rc)
1880 return rc;
1881
1882 return avc_has_perm(newsid, sbsec->sid,
1883 SECCLASS_FILESYSTEM,
1884 FILESYSTEM__ASSOCIATE, &ad);
1885 }
1886
1887 #define MAY_LINK 0
1888 #define MAY_UNLINK 1
1889 #define MAY_RMDIR 2
1890
1891 /* Check whether a task can link, unlink, or rmdir a file/directory. */
may_link(struct inode * dir,struct dentry * dentry,int kind)1892 static int may_link(struct inode *dir,
1893 struct dentry *dentry,
1894 int kind)
1895
1896 {
1897 struct inode_security_struct *dsec, *isec;
1898 struct common_audit_data ad;
1899 u32 sid = current_sid();
1900 u32 av;
1901 int rc;
1902
1903 dsec = inode_security(dir);
1904 isec = backing_inode_security(dentry);
1905
1906 ad.type = LSM_AUDIT_DATA_DENTRY;
1907 ad.u.dentry = dentry;
1908
1909 av = DIR__SEARCH;
1910 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1911 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
1912 if (rc)
1913 return rc;
1914
1915 switch (kind) {
1916 case MAY_LINK:
1917 av = FILE__LINK;
1918 break;
1919 case MAY_UNLINK:
1920 av = FILE__UNLINK;
1921 break;
1922 case MAY_RMDIR:
1923 av = DIR__RMDIR;
1924 break;
1925 default:
1926 pr_warn("SELinux: %s: unrecognized kind %d\n",
1927 __func__, kind);
1928 return 0;
1929 }
1930
1931 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
1932 return rc;
1933 }
1934
may_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)1935 static inline int may_rename(struct inode *old_dir,
1936 struct dentry *old_dentry,
1937 struct inode *new_dir,
1938 struct dentry *new_dentry)
1939 {
1940 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
1941 struct common_audit_data ad;
1942 u32 sid = current_sid();
1943 u32 av;
1944 int old_is_dir, new_is_dir;
1945 int rc;
1946
1947 old_dsec = inode_security(old_dir);
1948 old_isec = backing_inode_security(old_dentry);
1949 old_is_dir = d_is_dir(old_dentry);
1950 new_dsec = inode_security(new_dir);
1951
1952 ad.type = LSM_AUDIT_DATA_DENTRY;
1953
1954 ad.u.dentry = old_dentry;
1955 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
1956 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1957 if (rc)
1958 return rc;
1959 rc = avc_has_perm(sid, old_isec->sid,
1960 old_isec->sclass, FILE__RENAME, &ad);
1961 if (rc)
1962 return rc;
1963 if (old_is_dir && new_dir != old_dir) {
1964 rc = avc_has_perm(sid, old_isec->sid,
1965 old_isec->sclass, DIR__REPARENT, &ad);
1966 if (rc)
1967 return rc;
1968 }
1969
1970 ad.u.dentry = new_dentry;
1971 av = DIR__ADD_NAME | DIR__SEARCH;
1972 if (d_is_positive(new_dentry))
1973 av |= DIR__REMOVE_NAME;
1974 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
1975 if (rc)
1976 return rc;
1977 if (d_is_positive(new_dentry)) {
1978 new_isec = backing_inode_security(new_dentry);
1979 new_is_dir = d_is_dir(new_dentry);
1980 rc = avc_has_perm(sid, new_isec->sid,
1981 new_isec->sclass,
1982 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1983 if (rc)
1984 return rc;
1985 }
1986
1987 return 0;
1988 }
1989
1990 /* Check whether a task can perform a filesystem operation. */
superblock_has_perm(const struct cred * cred,const struct super_block * sb,u32 perms,struct common_audit_data * ad)1991 static int superblock_has_perm(const struct cred *cred,
1992 const struct super_block *sb,
1993 u32 perms,
1994 struct common_audit_data *ad)
1995 {
1996 struct superblock_security_struct *sbsec;
1997 u32 sid = cred_sid(cred);
1998
1999 sbsec = selinux_superblock(sb);
2000 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
2001 }
2002
2003 /* Convert a Linux mode and permission mask to an access vector. */
file_mask_to_av(int mode,int mask)2004 static inline u32 file_mask_to_av(int mode, int mask)
2005 {
2006 u32 av = 0;
2007
2008 if (!S_ISDIR(mode)) {
2009 if (mask & MAY_EXEC)
2010 av |= FILE__EXECUTE;
2011 if (mask & MAY_READ)
2012 av |= FILE__READ;
2013
2014 if (mask & MAY_APPEND)
2015 av |= FILE__APPEND;
2016 else if (mask & MAY_WRITE)
2017 av |= FILE__WRITE;
2018
2019 } else {
2020 if (mask & MAY_EXEC)
2021 av |= DIR__SEARCH;
2022 if (mask & MAY_WRITE)
2023 av |= DIR__WRITE;
2024 if (mask & MAY_READ)
2025 av |= DIR__READ;
2026 }
2027
2028 return av;
2029 }
2030
2031 /* Convert a Linux file to an access vector. */
file_to_av(const struct file * file)2032 static inline u32 file_to_av(const struct file *file)
2033 {
2034 u32 av = 0;
2035
2036 if (file->f_mode & FMODE_READ)
2037 av |= FILE__READ;
2038 if (file->f_mode & FMODE_WRITE) {
2039 if (file->f_flags & O_APPEND)
2040 av |= FILE__APPEND;
2041 else
2042 av |= FILE__WRITE;
2043 }
2044 if (!av) {
2045 /*
2046 * Special file opened with flags 3 for ioctl-only use.
2047 */
2048 av = FILE__IOCTL;
2049 }
2050
2051 return av;
2052 }
2053
2054 /*
2055 * Convert a file to an access vector and include the correct
2056 * open permission.
2057 */
open_file_to_av(struct file * file)2058 static inline u32 open_file_to_av(struct file *file)
2059 {
2060 u32 av = file_to_av(file);
2061 struct inode *inode = file_inode(file);
2062
2063 if (selinux_policycap_openperm() &&
2064 inode->i_sb->s_magic != SOCKFS_MAGIC)
2065 av |= FILE__OPEN;
2066
2067 return av;
2068 }
2069
2070 /* Hook functions begin here. */
2071
selinux_binder_set_context_mgr(const struct cred * mgr)2072 static int selinux_binder_set_context_mgr(const struct cred *mgr)
2073 {
2074 return avc_has_perm(current_sid(), cred_sid(mgr), SECCLASS_BINDER,
2075 BINDER__SET_CONTEXT_MGR, NULL);
2076 }
2077
selinux_binder_transaction(const struct cred * from,const struct cred * to)2078 static int selinux_binder_transaction(const struct cred *from,
2079 const struct cred *to)
2080 {
2081 u32 mysid = current_sid();
2082 u32 fromsid = cred_sid(from);
2083 u32 tosid = cred_sid(to);
2084 int rc;
2085
2086 if (mysid != fromsid) {
2087 rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER,
2088 BINDER__IMPERSONATE, NULL);
2089 if (rc)
2090 return rc;
2091 }
2092
2093 return avc_has_perm(fromsid, tosid,
2094 SECCLASS_BINDER, BINDER__CALL, NULL);
2095 }
2096
selinux_binder_transfer_binder(const struct cred * from,const struct cred * to)2097 static int selinux_binder_transfer_binder(const struct cred *from,
2098 const struct cred *to)
2099 {
2100 return avc_has_perm(cred_sid(from), cred_sid(to),
2101 SECCLASS_BINDER, BINDER__TRANSFER,
2102 NULL);
2103 }
2104
selinux_binder_transfer_file(const struct cred * from,const struct cred * to,const struct file * file)2105 static int selinux_binder_transfer_file(const struct cred *from,
2106 const struct cred *to,
2107 const struct file *file)
2108 {
2109 u32 sid = cred_sid(to);
2110 struct file_security_struct *fsec = selinux_file(file);
2111 struct dentry *dentry = file->f_path.dentry;
2112 struct inode_security_struct *isec;
2113 struct common_audit_data ad;
2114 int rc;
2115
2116 ad.type = LSM_AUDIT_DATA_PATH;
2117 ad.u.path = file->f_path;
2118
2119 if (sid != fsec->sid) {
2120 rc = avc_has_perm(sid, fsec->sid,
2121 SECCLASS_FD,
2122 FD__USE,
2123 &ad);
2124 if (rc)
2125 return rc;
2126 }
2127
2128 #ifdef CONFIG_BPF_SYSCALL
2129 rc = bpf_fd_pass(file, sid);
2130 if (rc)
2131 return rc;
2132 #endif
2133
2134 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2135 return 0;
2136
2137 isec = backing_inode_security(dentry);
2138 return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
2139 &ad);
2140 }
2141
selinux_ptrace_access_check(struct task_struct * child,unsigned int mode)2142 static int selinux_ptrace_access_check(struct task_struct *child,
2143 unsigned int mode)
2144 {
2145 u32 sid = current_sid();
2146 u32 csid = task_sid_obj(child);
2147
2148 if (mode & PTRACE_MODE_READ)
2149 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ,
2150 NULL);
2151
2152 return avc_has_perm(sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE,
2153 NULL);
2154 }
2155
selinux_ptrace_traceme(struct task_struct * parent)2156 static int selinux_ptrace_traceme(struct task_struct *parent)
2157 {
2158 return avc_has_perm(task_sid_obj(parent), task_sid_obj(current),
2159 SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
2160 }
2161
selinux_capget(const struct task_struct * target,kernel_cap_t * effective,kernel_cap_t * inheritable,kernel_cap_t * permitted)2162 static int selinux_capget(const struct task_struct *target, kernel_cap_t *effective,
2163 kernel_cap_t *inheritable, kernel_cap_t *permitted)
2164 {
2165 return avc_has_perm(current_sid(), task_sid_obj(target),
2166 SECCLASS_PROCESS, PROCESS__GETCAP, NULL);
2167 }
2168
selinux_capset(struct cred * new,const struct cred * old,const kernel_cap_t * effective,const kernel_cap_t * inheritable,const kernel_cap_t * permitted)2169 static int selinux_capset(struct cred *new, const struct cred *old,
2170 const kernel_cap_t *effective,
2171 const kernel_cap_t *inheritable,
2172 const kernel_cap_t *permitted)
2173 {
2174 return avc_has_perm(cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
2175 PROCESS__SETCAP, NULL);
2176 }
2177
2178 /*
2179 * (This comment used to live with the selinux_task_setuid hook,
2180 * which was removed).
2181 *
2182 * Since setuid only affects the current process, and since the SELinux
2183 * controls are not based on the Linux identity attributes, SELinux does not
2184 * need to control this operation. However, SELinux does control the use of
2185 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2186 */
2187
selinux_capable(const struct cred * cred,struct user_namespace * ns,int cap,unsigned int opts)2188 static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2189 int cap, unsigned int opts)
2190 {
2191 return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
2192 }
2193
selinux_quotactl(int cmds,int type,int id,const struct super_block * sb)2194 static int selinux_quotactl(int cmds, int type, int id, const struct super_block *sb)
2195 {
2196 const struct cred *cred = current_cred();
2197 int rc = 0;
2198
2199 if (!sb)
2200 return 0;
2201
2202 switch (cmds) {
2203 case Q_SYNC:
2204 case Q_QUOTAON:
2205 case Q_QUOTAOFF:
2206 case Q_SETINFO:
2207 case Q_SETQUOTA:
2208 case Q_XQUOTAOFF:
2209 case Q_XQUOTAON:
2210 case Q_XSETQLIM:
2211 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
2212 break;
2213 case Q_GETFMT:
2214 case Q_GETINFO:
2215 case Q_GETQUOTA:
2216 case Q_XGETQUOTA:
2217 case Q_XGETQSTAT:
2218 case Q_XGETQSTATV:
2219 case Q_XGETNEXTQUOTA:
2220 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
2221 break;
2222 default:
2223 rc = 0; /* let the kernel handle invalid cmds */
2224 break;
2225 }
2226 return rc;
2227 }
2228
selinux_quota_on(struct dentry * dentry)2229 static int selinux_quota_on(struct dentry *dentry)
2230 {
2231 const struct cred *cred = current_cred();
2232
2233 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
2234 }
2235
selinux_syslog(int type)2236 static int selinux_syslog(int type)
2237 {
2238 switch (type) {
2239 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2240 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
2241 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
2242 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
2243 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2244 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2245 /* Set level of messages printed to console */
2246 case SYSLOG_ACTION_CONSOLE_LEVEL:
2247 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
2248 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2249 NULL);
2250 }
2251 /* All other syslog types */
2252 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
2253 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
2254 }
2255
2256 /*
2257 * Check permission for allocating a new virtual mapping. Returns
2258 * 0 if permission is granted, negative error code if not.
2259 *
2260 * Do not audit the selinux permission check, as this is applied to all
2261 * processes that allocate mappings.
2262 */
selinux_vm_enough_memory(struct mm_struct * mm,long pages)2263 static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
2264 {
2265 return cred_has_capability(current_cred(), CAP_SYS_ADMIN,
2266 CAP_OPT_NOAUDIT, true);
2267 }
2268
2269 /* binprm security operations */
2270
ptrace_parent_sid(void)2271 static u32 ptrace_parent_sid(void)
2272 {
2273 u32 sid = 0;
2274 struct task_struct *tracer;
2275
2276 rcu_read_lock();
2277 tracer = ptrace_parent(current);
2278 if (tracer)
2279 sid = task_sid_obj(tracer);
2280 rcu_read_unlock();
2281
2282 return sid;
2283 }
2284
check_nnp_nosuid(const struct linux_binprm * bprm,const struct cred_security_struct * old_crsec,const struct cred_security_struct * new_crsec)2285 static int check_nnp_nosuid(const struct linux_binprm *bprm,
2286 const struct cred_security_struct *old_crsec,
2287 const struct cred_security_struct *new_crsec)
2288 {
2289 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
2290 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
2291 int rc;
2292 u32 av;
2293
2294 if (!nnp && !nosuid)
2295 return 0; /* neither NNP nor nosuid */
2296
2297 if (new_crsec->sid == old_crsec->sid)
2298 return 0; /* No change in credentials */
2299
2300 /*
2301 * If the policy enables the nnp_nosuid_transition policy capability,
2302 * then we permit transitions under NNP or nosuid if the
2303 * policy allows the corresponding permission between
2304 * the old and new contexts.
2305 */
2306 if (selinux_policycap_nnp_nosuid_transition()) {
2307 av = 0;
2308 if (nnp)
2309 av |= PROCESS2__NNP_TRANSITION;
2310 if (nosuid)
2311 av |= PROCESS2__NOSUID_TRANSITION;
2312 rc = avc_has_perm(old_crsec->sid, new_crsec->sid,
2313 SECCLASS_PROCESS2, av, NULL);
2314 if (!rc)
2315 return 0;
2316 }
2317
2318 /*
2319 * We also permit NNP or nosuid transitions to bounded SIDs,
2320 * i.e. SIDs that are guaranteed to only be allowed a subset
2321 * of the permissions of the current SID.
2322 */
2323 rc = security_bounded_transition(old_crsec->sid,
2324 new_crsec->sid);
2325 if (!rc)
2326 return 0;
2327
2328 /*
2329 * On failure, preserve the errno values for NNP vs nosuid.
2330 * NNP: Operation not permitted for caller.
2331 * nosuid: Permission denied to file.
2332 */
2333 if (nnp)
2334 return -EPERM;
2335 return -EACCES;
2336 }
2337
selinux_bprm_creds_for_exec(struct linux_binprm * bprm)2338 static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
2339 {
2340 const struct cred_security_struct *old_crsec;
2341 struct cred_security_struct *new_crsec;
2342 struct inode_security_struct *isec;
2343 struct common_audit_data ad;
2344 struct inode *inode = file_inode(bprm->file);
2345 int rc;
2346
2347 /* SELinux context only depends on initial program or script and not
2348 * the script interpreter */
2349
2350 old_crsec = selinux_cred(current_cred());
2351 new_crsec = selinux_cred(bprm->cred);
2352 isec = inode_security(inode);
2353
2354 if (WARN_ON(isec->sclass != SECCLASS_FILE &&
2355 isec->sclass != SECCLASS_MEMFD_FILE))
2356 return -EACCES;
2357
2358 /* Default to the current task SID. */
2359 new_crsec->sid = old_crsec->sid;
2360 new_crsec->osid = old_crsec->sid;
2361
2362 /* Reset fs, key, and sock SIDs on execve. */
2363 new_crsec->create_sid = 0;
2364 new_crsec->keycreate_sid = 0;
2365 new_crsec->sockcreate_sid = 0;
2366
2367 /*
2368 * Before policy is loaded, label any task outside kernel space
2369 * as SECINITSID_INIT, so that any userspace tasks surviving from
2370 * early boot end up with a label different from SECINITSID_KERNEL
2371 * (if the policy chooses to set SECINITSID_INIT != SECINITSID_KERNEL).
2372 */
2373 if (!selinux_initialized()) {
2374 new_crsec->sid = SECINITSID_INIT;
2375 /* also clear the exec_sid just in case */
2376 new_crsec->exec_sid = 0;
2377 return 0;
2378 }
2379
2380 if (old_crsec->exec_sid) {
2381 new_crsec->sid = old_crsec->exec_sid;
2382 /* Reset exec SID on execve. */
2383 new_crsec->exec_sid = 0;
2384
2385 /* Fail on NNP or nosuid if not an allowed transition. */
2386 rc = check_nnp_nosuid(bprm, old_crsec, new_crsec);
2387 if (rc)
2388 return rc;
2389 } else {
2390 /* Check for a default transition on this program. */
2391 rc = security_transition_sid(old_crsec->sid,
2392 isec->sid, SECCLASS_PROCESS, NULL,
2393 &new_crsec->sid);
2394 if (rc)
2395 return rc;
2396
2397 /*
2398 * Fallback to old SID on NNP or nosuid if not an allowed
2399 * transition.
2400 */
2401 rc = check_nnp_nosuid(bprm, old_crsec, new_crsec);
2402 if (rc)
2403 new_crsec->sid = old_crsec->sid;
2404 }
2405
2406 ad.type = LSM_AUDIT_DATA_FILE;
2407 ad.u.file = bprm->file;
2408
2409 if (new_crsec->sid == old_crsec->sid) {
2410 rc = avc_has_perm(old_crsec->sid, isec->sid, isec->sclass,
2411 FILE__EXECUTE_NO_TRANS, &ad);
2412 if (rc)
2413 return rc;
2414 } else {
2415 /* Check permissions for the transition. */
2416 rc = avc_has_perm(old_crsec->sid, new_crsec->sid,
2417 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2418 if (rc)
2419 return rc;
2420
2421 rc = avc_has_perm(new_crsec->sid, isec->sid, isec->sclass,
2422 FILE__ENTRYPOINT, &ad);
2423 if (rc)
2424 return rc;
2425
2426 /* Check for shared state */
2427 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2428 rc = avc_has_perm(old_crsec->sid, new_crsec->sid,
2429 SECCLASS_PROCESS, PROCESS__SHARE,
2430 NULL);
2431 if (rc)
2432 return -EPERM;
2433 }
2434
2435 /* Make sure that anyone attempting to ptrace over a task that
2436 * changes its SID has the appropriate permit */
2437 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
2438 u32 ptsid = ptrace_parent_sid();
2439 if (ptsid != 0) {
2440 rc = avc_has_perm(ptsid, new_crsec->sid,
2441 SECCLASS_PROCESS,
2442 PROCESS__PTRACE, NULL);
2443 if (rc)
2444 return -EPERM;
2445 }
2446 }
2447
2448 /* Clear any possibly unsafe personality bits on exec: */
2449 bprm->per_clear |= PER_CLEAR_ON_SETID;
2450
2451 /* Enable secure mode for SIDs transitions unless
2452 the noatsecure permission is granted between
2453 the two SIDs, i.e. ahp returns 0. */
2454 rc = avc_has_perm(old_crsec->sid, new_crsec->sid,
2455 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2456 NULL);
2457 bprm->secureexec |= !!rc;
2458 }
2459
2460 return 0;
2461 }
2462
match_file(const void * p,struct file * file,unsigned fd)2463 static int match_file(const void *p, struct file *file, unsigned fd)
2464 {
2465 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2466 }
2467
2468 /* Derived from fs/exec.c:flush_old_files. */
flush_unauthorized_files(const struct cred * cred,struct files_struct * files)2469 static inline void flush_unauthorized_files(const struct cred *cred,
2470 struct files_struct *files)
2471 {
2472 struct file *file, *devnull = NULL;
2473 struct tty_struct *tty;
2474 int drop_tty = 0;
2475 unsigned n;
2476
2477 tty = get_current_tty();
2478 if (tty) {
2479 spin_lock(&tty->files_lock);
2480 if (!list_empty(&tty->tty_files)) {
2481 struct tty_file_private *file_priv;
2482
2483 /* Revalidate access to controlling tty.
2484 Use file_path_has_perm on the tty path directly
2485 rather than using file_has_perm, as this particular
2486 open file may belong to another process and we are
2487 only interested in the inode-based check here. */
2488 file_priv = list_first_entry(&tty->tty_files,
2489 struct tty_file_private, list);
2490 file = file_priv->file;
2491 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
2492 drop_tty = 1;
2493 }
2494 spin_unlock(&tty->files_lock);
2495 tty_kref_put(tty);
2496 }
2497 /* Reset controlling tty. */
2498 if (drop_tty)
2499 no_tty();
2500
2501 /* Revalidate access to inherited open files. */
2502 n = iterate_fd(files, 0, match_file, cred);
2503 if (!n) /* none found? */
2504 return;
2505
2506 devnull = dentry_open(&selinux_null, O_RDWR, cred);
2507 if (IS_ERR(devnull))
2508 devnull = NULL;
2509 /* replace all the matching ones with this */
2510 do {
2511 replace_fd(n - 1, devnull, 0);
2512 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2513 if (devnull)
2514 fput(devnull);
2515 }
2516
2517 /*
2518 * Prepare a process for imminent new credential changes due to exec
2519 */
selinux_bprm_committing_creds(const struct linux_binprm * bprm)2520 static void selinux_bprm_committing_creds(const struct linux_binprm *bprm)
2521 {
2522 struct cred_security_struct *new_crsec;
2523 struct rlimit *rlim, *initrlim;
2524 int rc, i;
2525
2526 new_crsec = selinux_cred(bprm->cred);
2527 if (new_crsec->sid == new_crsec->osid)
2528 return;
2529
2530 /* Close files for which the new task SID is not authorized. */
2531 flush_unauthorized_files(bprm->cred, current->files);
2532
2533 /* Always clear parent death signal on SID transitions. */
2534 current->pdeath_signal = 0;
2535
2536 /* Check whether the new SID can inherit resource limits from the old
2537 * SID. If not, reset all soft limits to the lower of the current
2538 * task's hard limit and the init task's soft limit.
2539 *
2540 * Note that the setting of hard limits (even to lower them) can be
2541 * controlled by the setrlimit check. The inclusion of the init task's
2542 * soft limit into the computation is to avoid resetting soft limits
2543 * higher than the default soft limit for cases where the default is
2544 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2545 */
2546 rc = avc_has_perm(new_crsec->osid, new_crsec->sid, SECCLASS_PROCESS,
2547 PROCESS__RLIMITINH, NULL);
2548 if (rc) {
2549 /* protect against do_prlimit() */
2550 task_lock(current);
2551 for (i = 0; i < RLIM_NLIMITS; i++) {
2552 rlim = current->signal->rlim + i;
2553 initrlim = init_task.signal->rlim + i;
2554 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2555 }
2556 task_unlock(current);
2557 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2558 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2559 }
2560 }
2561
2562 /*
2563 * Clean up the process immediately after the installation of new credentials
2564 * due to exec
2565 */
selinux_bprm_committed_creds(const struct linux_binprm * bprm)2566 static void selinux_bprm_committed_creds(const struct linux_binprm *bprm)
2567 {
2568 const struct cred_security_struct *crsec = selinux_cred(current_cred());
2569 u32 osid, sid;
2570 int rc;
2571
2572 osid = crsec->osid;
2573 sid = crsec->sid;
2574
2575 if (sid == osid)
2576 return;
2577
2578 /* Check whether the new SID can inherit signal state from the old SID.
2579 * If not, clear itimers to avoid subsequent signal generation and
2580 * flush and unblock signals.
2581 *
2582 * This must occur _after_ the task SID has been updated so that any
2583 * kill done after the flush will be checked against the new SID.
2584 */
2585 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
2586 if (rc) {
2587 clear_itimer();
2588
2589 spin_lock_irq(&unrcu_pointer(current->sighand)->siglock);
2590 if (!fatal_signal_pending(current)) {
2591 flush_sigqueue(¤t->pending);
2592 flush_sigqueue(¤t->signal->shared_pending);
2593 flush_signal_handlers(current, 1);
2594 sigemptyset(¤t->blocked);
2595 recalc_sigpending();
2596 }
2597 spin_unlock_irq(&unrcu_pointer(current->sighand)->siglock);
2598 }
2599
2600 /* Wake up the parent if it is waiting so that it can recheck
2601 * wait permission to the new task SID. */
2602 read_lock(&tasklist_lock);
2603 __wake_up_parent(current, unrcu_pointer(current->real_parent));
2604 read_unlock(&tasklist_lock);
2605 }
2606
2607 /* superblock security operations */
2608
selinux_sb_alloc_security(struct super_block * sb)2609 static int selinux_sb_alloc_security(struct super_block *sb)
2610 {
2611 struct superblock_security_struct *sbsec = selinux_superblock(sb);
2612
2613 mutex_init(&sbsec->lock);
2614 INIT_LIST_HEAD(&sbsec->isec_head);
2615 spin_lock_init(&sbsec->isec_lock);
2616 sbsec->sid = SECINITSID_UNLABELED;
2617 sbsec->def_sid = SECINITSID_FILE;
2618 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
2619 sbsec->creator_sid = SECINITSID_UNLABELED;
2620
2621 return 0;
2622 }
2623
opt_len(const char * s)2624 static inline int opt_len(const char *s)
2625 {
2626 bool open_quote = false;
2627 int len;
2628 char c;
2629
2630 for (len = 0; (c = s[len]) != '\0'; len++) {
2631 if (c == '"')
2632 open_quote = !open_quote;
2633 if (c == ',' && !open_quote)
2634 break;
2635 }
2636 return len;
2637 }
2638
selinux_sb_eat_lsm_opts(char * options,void ** mnt_opts)2639 static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
2640 {
2641 char *from = options;
2642 char *to = options;
2643 bool first = true;
2644 int rc;
2645
2646 while (1) {
2647 int len = opt_len(from);
2648 int token;
2649 char *arg = NULL;
2650
2651 token = match_opt_prefix(from, len, &arg);
2652
2653 if (token != Opt_error) {
2654 char *p, *q;
2655
2656 /* strip quotes */
2657 if (arg) {
2658 for (p = q = arg; p < from + len; p++) {
2659 char c = *p;
2660 if (c != '"')
2661 *q++ = c;
2662 }
2663 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2664 if (!arg) {
2665 rc = -ENOMEM;
2666 goto free_opt;
2667 }
2668 }
2669 rc = selinux_add_opt(token, arg, mnt_opts);
2670 kfree(arg);
2671 arg = NULL;
2672 if (unlikely(rc)) {
2673 goto free_opt;
2674 }
2675 } else {
2676 if (!first) { // copy with preceding comma
2677 from--;
2678 len++;
2679 }
2680 if (to != from)
2681 memmove(to, from, len);
2682 to += len;
2683 first = false;
2684 }
2685 if (!from[len])
2686 break;
2687 from += len + 1;
2688 }
2689 *to = '\0';
2690 return 0;
2691
2692 free_opt:
2693 if (*mnt_opts) {
2694 selinux_free_mnt_opts(*mnt_opts);
2695 *mnt_opts = NULL;
2696 }
2697 return rc;
2698 }
2699
selinux_sb_mnt_opts_compat(struct super_block * sb,void * mnt_opts)2700 static int selinux_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts)
2701 {
2702 struct selinux_mnt_opts *opts = mnt_opts;
2703 struct superblock_security_struct *sbsec = selinux_superblock(sb);
2704
2705 /*
2706 * Superblock not initialized (i.e. no options) - reject if any
2707 * options specified, otherwise accept.
2708 */
2709 if (!(sbsec->flags & SE_SBINITIALIZED))
2710 return opts ? 1 : 0;
2711
2712 /*
2713 * Superblock initialized and no options specified - reject if
2714 * superblock has any options set, otherwise accept.
2715 */
2716 if (!opts)
2717 return (sbsec->flags & SE_MNTMASK) ? 1 : 0;
2718
2719 if (opts->fscontext_sid) {
2720 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
2721 opts->fscontext_sid))
2722 return 1;
2723 }
2724 if (opts->context_sid) {
2725 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
2726 opts->context_sid))
2727 return 1;
2728 }
2729 if (opts->rootcontext_sid) {
2730 struct inode_security_struct *root_isec;
2731
2732 root_isec = backing_inode_security(sb->s_root);
2733 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
2734 opts->rootcontext_sid))
2735 return 1;
2736 }
2737 if (opts->defcontext_sid) {
2738 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
2739 opts->defcontext_sid))
2740 return 1;
2741 }
2742 return 0;
2743 }
2744
selinux_sb_remount(struct super_block * sb,void * mnt_opts)2745 static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
2746 {
2747 struct selinux_mnt_opts *opts = mnt_opts;
2748 struct superblock_security_struct *sbsec = selinux_superblock(sb);
2749
2750 if (!(sbsec->flags & SE_SBINITIALIZED))
2751 return 0;
2752
2753 if (!opts)
2754 return 0;
2755
2756 if (opts->fscontext_sid) {
2757 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
2758 opts->fscontext_sid))
2759 goto out_bad_option;
2760 }
2761 if (opts->context_sid) {
2762 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
2763 opts->context_sid))
2764 goto out_bad_option;
2765 }
2766 if (opts->rootcontext_sid) {
2767 struct inode_security_struct *root_isec;
2768 root_isec = backing_inode_security(sb->s_root);
2769 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
2770 opts->rootcontext_sid))
2771 goto out_bad_option;
2772 }
2773 if (opts->defcontext_sid) {
2774 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
2775 opts->defcontext_sid))
2776 goto out_bad_option;
2777 }
2778 return 0;
2779
2780 out_bad_option:
2781 pr_warn("SELinux: unable to change security options "
2782 "during remount (dev %s, type=%s)\n", sb->s_id,
2783 sb->s_type->name);
2784 return -EINVAL;
2785 }
2786
selinux_sb_kern_mount(const struct super_block * sb)2787 static int selinux_sb_kern_mount(const struct super_block *sb)
2788 {
2789 const struct cred *cred = current_cred();
2790 struct common_audit_data ad;
2791
2792 ad.type = LSM_AUDIT_DATA_DENTRY;
2793 ad.u.dentry = sb->s_root;
2794 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
2795 }
2796
selinux_sb_statfs(struct dentry * dentry)2797 static int selinux_sb_statfs(struct dentry *dentry)
2798 {
2799 const struct cred *cred = current_cred();
2800 struct common_audit_data ad;
2801
2802 ad.type = LSM_AUDIT_DATA_DENTRY;
2803 ad.u.dentry = dentry->d_sb->s_root;
2804 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
2805 }
2806
selinux_mount(const char * dev_name,const struct path * path,const char * type,unsigned long flags,void * data)2807 static int selinux_mount(const char *dev_name,
2808 const struct path *path,
2809 const char *type,
2810 unsigned long flags,
2811 void *data)
2812 {
2813 const struct cred *cred = current_cred();
2814
2815 if (flags & MS_REMOUNT)
2816 return superblock_has_perm(cred, path->dentry->d_sb,
2817 FILESYSTEM__REMOUNT, NULL);
2818 else
2819 return path_has_perm(cred, path, FILE__MOUNTON);
2820 }
2821
selinux_move_mount(const struct path * from_path,const struct path * to_path)2822 static int selinux_move_mount(const struct path *from_path,
2823 const struct path *to_path)
2824 {
2825 const struct cred *cred = current_cred();
2826
2827 return path_has_perm(cred, to_path, FILE__MOUNTON);
2828 }
2829
selinux_umount(struct vfsmount * mnt,int flags)2830 static int selinux_umount(struct vfsmount *mnt, int flags)
2831 {
2832 const struct cred *cred = current_cred();
2833
2834 return superblock_has_perm(cred, mnt->mnt_sb,
2835 FILESYSTEM__UNMOUNT, NULL);
2836 }
2837
selinux_fs_context_submount(struct fs_context * fc,struct super_block * reference)2838 static int selinux_fs_context_submount(struct fs_context *fc,
2839 struct super_block *reference)
2840 {
2841 const struct superblock_security_struct *sbsec = selinux_superblock(reference);
2842 struct selinux_mnt_opts *opts;
2843
2844 /*
2845 * Ensure that fc->security remains NULL when no options are set
2846 * as expected by selinux_set_mnt_opts().
2847 */
2848 if (!(sbsec->flags & (FSCONTEXT_MNT|CONTEXT_MNT|DEFCONTEXT_MNT)))
2849 return 0;
2850
2851 opts = kzalloc_obj(*opts);
2852 if (!opts)
2853 return -ENOMEM;
2854
2855 if (sbsec->flags & FSCONTEXT_MNT)
2856 opts->fscontext_sid = sbsec->sid;
2857 if (sbsec->flags & CONTEXT_MNT)
2858 opts->context_sid = sbsec->mntpoint_sid;
2859 if (sbsec->flags & DEFCONTEXT_MNT)
2860 opts->defcontext_sid = sbsec->def_sid;
2861 fc->security = opts;
2862 return 0;
2863 }
2864
selinux_fs_context_dup(struct fs_context * fc,struct fs_context * src_fc)2865 static int selinux_fs_context_dup(struct fs_context *fc,
2866 struct fs_context *src_fc)
2867 {
2868 const struct selinux_mnt_opts *src = src_fc->security;
2869
2870 if (!src)
2871 return 0;
2872
2873 fc->security = kmemdup(src, sizeof(*src), GFP_KERNEL);
2874 return fc->security ? 0 : -ENOMEM;
2875 }
2876
2877 static const struct fs_parameter_spec selinux_fs_parameters[] = {
2878 fsparam_string(CONTEXT_STR, Opt_context),
2879 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
2880 fsparam_string(FSCONTEXT_STR, Opt_fscontext),
2881 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
2882 fsparam_flag (SECLABEL_STR, Opt_seclabel),
2883 {}
2884 };
2885
selinux_fs_context_parse_param(struct fs_context * fc,struct fs_parameter * param)2886 static int selinux_fs_context_parse_param(struct fs_context *fc,
2887 struct fs_parameter *param)
2888 {
2889 struct fs_parse_result result;
2890 int opt;
2891
2892 opt = fs_parse(fc, selinux_fs_parameters, param, &result);
2893 if (opt < 0)
2894 return opt;
2895
2896 return selinux_add_opt(opt, param->string, &fc->security);
2897 }
2898
2899 /* inode security operations */
2900
selinux_inode_alloc_security(struct inode * inode)2901 static int selinux_inode_alloc_security(struct inode *inode)
2902 {
2903 struct inode_security_struct *isec = selinux_inode(inode);
2904 u32 sid = current_sid();
2905
2906 spin_lock_init(&isec->lock);
2907 INIT_LIST_HEAD(&isec->list);
2908 isec->inode = inode;
2909 isec->sid = SECINITSID_UNLABELED;
2910 isec->sclass = SECCLASS_FILE;
2911 isec->task_sid = sid;
2912 isec->initialized = LABEL_INVALID;
2913
2914 return 0;
2915 }
2916
selinux_inode_free_security(struct inode * inode)2917 static void selinux_inode_free_security(struct inode *inode)
2918 {
2919 inode_free_security(inode);
2920 }
2921
selinux_dentry_init_security(struct dentry * dentry,int mode,const struct qstr * name,const char ** xattr_name,struct lsm_context * cp)2922 static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2923 const struct qstr *name,
2924 const char **xattr_name,
2925 struct lsm_context *cp)
2926 {
2927 u32 newsid;
2928 int rc;
2929
2930 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
2931 d_inode(dentry->d_parent), name,
2932 inode_mode_to_security_class(mode),
2933 &newsid);
2934 if (rc)
2935 return rc;
2936
2937 if (xattr_name)
2938 *xattr_name = XATTR_NAME_SELINUX;
2939
2940 cp->id = LSM_ID_SELINUX;
2941 return security_sid_to_context(newsid, &cp->context, &cp->len);
2942 }
2943
selinux_dentry_create_files_as(struct dentry * dentry,int mode,const struct qstr * name,const struct cred * old,struct cred * new)2944 static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2945 const struct qstr *name,
2946 const struct cred *old,
2947 struct cred *new)
2948 {
2949 u32 newsid;
2950 int rc;
2951 struct cred_security_struct *crsec;
2952
2953 rc = selinux_determine_inode_label(selinux_cred(old),
2954 d_inode(dentry->d_parent), name,
2955 inode_mode_to_security_class(mode),
2956 &newsid);
2957 if (rc)
2958 return rc;
2959
2960 crsec = selinux_cred(new);
2961 crsec->create_sid = newsid;
2962 return 0;
2963 }
2964
selinux_inode_init_security(struct inode * inode,struct inode * dir,const struct qstr * qstr,struct xattr * xattrs,int * xattr_count)2965 static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
2966 const struct qstr *qstr,
2967 struct xattr *xattrs, int *xattr_count)
2968 {
2969 const struct cred_security_struct *crsec = selinux_cred(current_cred());
2970 struct superblock_security_struct *sbsec;
2971 struct xattr *xattr;
2972 u32 newsid, clen;
2973 u16 newsclass;
2974 int rc;
2975 char *context;
2976
2977 sbsec = selinux_superblock(dir->i_sb);
2978
2979 if (!selinux_initialized() ||
2980 !(sbsec->flags & SBLABEL_MNT))
2981 return -EOPNOTSUPP;
2982
2983 newsid = crsec->create_sid;
2984 newsclass = inode_mode_to_security_class(inode->i_mode);
2985 rc = selinux_determine_inode_label(crsec, dir, qstr, newsclass, &newsid);
2986 if (rc)
2987 return rc;
2988
2989 /* Possibly defer initialization to selinux_complete_init. */
2990 if (sbsec->flags & SE_SBINITIALIZED) {
2991 struct inode_security_struct *isec = selinux_inode(inode);
2992 isec->sclass = newsclass;
2993 isec->sid = newsid;
2994 isec->initialized = LABEL_INITIALIZED;
2995 }
2996
2997 xattr = lsm_get_xattr_slot(xattrs, xattr_count);
2998 if (xattr) {
2999 rc = security_sid_to_context_force(newsid,
3000 &context, &clen);
3001 if (rc)
3002 return rc;
3003 xattr->value = context;
3004 xattr->value_len = clen;
3005 xattr->name = XATTR_SELINUX_SUFFIX;
3006 }
3007
3008 return 0;
3009 }
3010
selinux_inode_init_security_anon(struct inode * inode,const struct qstr * name,const struct inode * context_inode)3011 static int selinux_inode_init_security_anon(struct inode *inode,
3012 const struct qstr *name,
3013 const struct inode *context_inode)
3014 {
3015 u32 sid = current_sid();
3016 struct common_audit_data ad;
3017 struct inode_security_struct *isec;
3018 int rc;
3019 bool is_memfd = false;
3020
3021 if (unlikely(!selinux_initialized()))
3022 return 0;
3023
3024 if (name != NULL && name->name != NULL &&
3025 !strcmp(name->name, MEMFD_ANON_NAME)) {
3026 if (!selinux_policycap_memfd_class())
3027 return 0;
3028 is_memfd = true;
3029 }
3030
3031 isec = selinux_inode(inode);
3032
3033 /*
3034 * We only get here once per ephemeral inode. The inode has
3035 * been initialized via inode_alloc_security but is otherwise
3036 * untouched.
3037 */
3038
3039 if (context_inode) {
3040 struct inode_security_struct *context_isec =
3041 selinux_inode(context_inode);
3042 if (context_isec->initialized != LABEL_INITIALIZED) {
3043 pr_err("SELinux: context_inode is not initialized\n");
3044 return -EACCES;
3045 }
3046
3047 isec->sclass = context_isec->sclass;
3048 isec->sid = context_isec->sid;
3049 } else {
3050 if (is_memfd)
3051 isec->sclass = SECCLASS_MEMFD_FILE;
3052 else
3053 isec->sclass = SECCLASS_ANON_INODE;
3054 rc = security_transition_sid(
3055 sid, sid,
3056 isec->sclass, name, &isec->sid);
3057 if (rc)
3058 return rc;
3059 }
3060
3061 isec->initialized = LABEL_INITIALIZED;
3062 /*
3063 * Now that we've initialized security, check whether we're
3064 * allowed to actually create this type of anonymous inode.
3065 */
3066
3067 ad.type = LSM_AUDIT_DATA_ANONINODE;
3068 ad.u.anonclass = name ? (const char *)name->name : "?";
3069
3070 return avc_has_perm(sid,
3071 isec->sid,
3072 isec->sclass,
3073 FILE__CREATE,
3074 &ad);
3075 }
3076
selinux_inode_create(struct inode * dir,struct dentry * dentry,umode_t mode)3077 static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
3078 {
3079 return may_create(dir, dentry, SECCLASS_FILE);
3080 }
3081
selinux_inode_link(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry)3082 static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3083 {
3084 return may_link(dir, old_dentry, MAY_LINK);
3085 }
3086
selinux_inode_unlink(struct inode * dir,struct dentry * dentry)3087 static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
3088 {
3089 return may_link(dir, dentry, MAY_UNLINK);
3090 }
3091
selinux_inode_symlink(struct inode * dir,struct dentry * dentry,const char * name)3092 static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
3093 {
3094 return may_create(dir, dentry, SECCLASS_LNK_FILE);
3095 }
3096
selinux_inode_mkdir(struct inode * dir,struct dentry * dentry,umode_t mask)3097 static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
3098 {
3099 return may_create(dir, dentry, SECCLASS_DIR);
3100 }
3101
selinux_inode_rmdir(struct inode * dir,struct dentry * dentry)3102 static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
3103 {
3104 return may_link(dir, dentry, MAY_RMDIR);
3105 }
3106
selinux_inode_mknod(struct inode * dir,struct dentry * dentry,umode_t mode,dev_t dev)3107 static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3108 {
3109 return may_create(dir, dentry, inode_mode_to_security_class(mode));
3110 }
3111
selinux_inode_rename(struct inode * old_inode,struct dentry * old_dentry,struct inode * new_inode,struct dentry * new_dentry)3112 static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
3113 struct inode *new_inode, struct dentry *new_dentry)
3114 {
3115 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
3116 }
3117
selinux_inode_readlink(struct dentry * dentry)3118 static int selinux_inode_readlink(struct dentry *dentry)
3119 {
3120 const struct cred *cred = current_cred();
3121
3122 return dentry_has_perm(cred, dentry, FILE__READ);
3123 }
3124
selinux_inode_follow_link(struct dentry * dentry,struct inode * inode,bool rcu)3125 static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
3126 bool rcu)
3127 {
3128 struct common_audit_data ad;
3129 struct inode_security_struct *isec;
3130 u32 sid = current_sid();
3131
3132 ad.type = LSM_AUDIT_DATA_DENTRY;
3133 ad.u.dentry = dentry;
3134 isec = inode_security_rcu(inode, rcu);
3135 if (IS_ERR(isec))
3136 return PTR_ERR(isec);
3137
3138 return avc_has_perm(sid, isec->sid, isec->sclass, FILE__READ, &ad);
3139 }
3140
audit_inode_permission(struct inode * inode,u32 perms,u32 audited,u32 denied,int result)3141 static noinline int audit_inode_permission(struct inode *inode,
3142 u32 perms, u32 audited, u32 denied,
3143 int result)
3144 {
3145 struct common_audit_data ad;
3146 struct inode_security_struct *isec = selinux_inode(inode);
3147
3148 ad.type = LSM_AUDIT_DATA_INODE;
3149 ad.u.inode = inode;
3150
3151 return slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
3152 audited, denied, result, &ad);
3153 }
3154
3155 /**
3156 * task_avdcache_reset - Reset the task's AVD cache
3157 * @tsec: the task's security state
3158 *
3159 * Clear the task's AVD cache in @tsec and reset it to the current policy's
3160 * and task's info.
3161 */
task_avdcache_reset(struct task_security_struct * tsec)3162 static inline void task_avdcache_reset(struct task_security_struct *tsec)
3163 {
3164 memset(&tsec->avdcache.dir, 0, sizeof(tsec->avdcache.dir));
3165 tsec->avdcache.sid = current_sid();
3166 tsec->avdcache.seqno = avc_policy_seqno();
3167 tsec->avdcache.dir_spot = TSEC_AVDC_DIR_SIZE - 1;
3168 }
3169
3170 /**
3171 * task_avdcache_search - Search the task's AVD cache
3172 * @tsec: the task's security state
3173 * @isec: the inode to search for in the cache
3174 * @avdc: matching avd cache entry returned to the caller
3175 *
3176 * Search @tsec for a AVD cache entry that matches @isec and return it to the
3177 * caller via @avdc. Returns 0 if a match is found, negative values otherwise.
3178 */
task_avdcache_search(struct task_security_struct * tsec,struct inode_security_struct * isec,struct avdc_entry ** avdc)3179 static inline int task_avdcache_search(struct task_security_struct *tsec,
3180 struct inode_security_struct *isec,
3181 struct avdc_entry **avdc)
3182 {
3183 int orig, iter;
3184
3185 /* focused on path walk optimization, only cache directories */
3186 if (isec->sclass != SECCLASS_DIR)
3187 return -ENOENT;
3188
3189 if (unlikely(current_sid() != tsec->avdcache.sid ||
3190 tsec->avdcache.seqno != avc_policy_seqno())) {
3191 task_avdcache_reset(tsec);
3192 return -ENOENT;
3193 }
3194
3195 orig = iter = tsec->avdcache.dir_spot;
3196 do {
3197 if (tsec->avdcache.dir[iter].isid == isec->sid) {
3198 /* cache hit */
3199 tsec->avdcache.dir_spot = iter;
3200 *avdc = &tsec->avdcache.dir[iter];
3201 return 0;
3202 }
3203 iter = (iter - 1) & (TSEC_AVDC_DIR_SIZE - 1);
3204 } while (iter != orig);
3205
3206 return -ENOENT;
3207 }
3208
3209 /**
3210 * task_avdcache_update - Update the task's AVD cache
3211 * @tsec: the task's security state
3212 * @isec: the inode associated with the cache entry
3213 * @avd: the AVD to cache
3214 *
3215 * Update the AVD cache in @tsec with the @avd info associated
3216 * with @isec.
3217 */
task_avdcache_update(struct task_security_struct * tsec,struct inode_security_struct * isec,struct av_decision * avd)3218 static inline void task_avdcache_update(struct task_security_struct *tsec,
3219 struct inode_security_struct *isec,
3220 struct av_decision *avd)
3221 {
3222 int spot;
3223
3224 /* focused on path walk optimization, only cache directories */
3225 if (isec->sclass != SECCLASS_DIR)
3226 return;
3227
3228 /* update cache */
3229 spot = (tsec->avdcache.dir_spot + 1) & (TSEC_AVDC_DIR_SIZE - 1);
3230 tsec->avdcache.dir_spot = spot;
3231 tsec->avdcache.dir[spot].isid = isec->sid;
3232 tsec->avdcache.dir[spot].avd = *avd;
3233 tsec->avdcache.permissive_neveraudit =
3234 (avd->flags == (AVD_FLAGS_PERMISSIVE|AVD_FLAGS_NEVERAUDIT));
3235 }
3236
3237 /**
3238 * selinux_inode_permission - Check if the current task can access an inode
3239 * @inode: the inode that is being accessed
3240 * @requested: the accesses being requested
3241 *
3242 * Check if the current task is allowed to access @inode according to
3243 * @requested. Returns 0 if allowed, negative values otherwise.
3244 */
selinux_inode_permission(struct inode * inode,int requested)3245 static int selinux_inode_permission(struct inode *inode, int requested)
3246 {
3247 int mask;
3248 u32 perms;
3249 u32 sid = current_sid();
3250 struct task_security_struct *tsec;
3251 struct inode_security_struct *isec;
3252 struct avdc_entry *avdc;
3253 struct av_decision avd, *avdp = &avd;
3254 int rc, rc2;
3255 u32 audited, denied;
3256
3257 mask = requested & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3258
3259 /* No permission to check. Existence test. */
3260 if (!mask)
3261 return 0;
3262
3263 tsec = selinux_task(current);
3264 if (task_avdcache_permnoaudit(tsec, sid))
3265 return 0;
3266
3267 isec = inode_security_rcu(inode, requested & MAY_NOT_BLOCK);
3268 if (IS_ERR(isec))
3269 return PTR_ERR(isec);
3270 perms = file_mask_to_av(inode->i_mode, mask);
3271
3272 rc = task_avdcache_search(tsec, isec, &avdc);
3273 if (likely(!rc)) {
3274 /* Cache hit. */
3275 avdp = &avdc->avd;
3276 denied = perms & ~avdp->allowed;
3277 if (unlikely(denied) && enforcing_enabled() &&
3278 !(avdp->flags & AVD_FLAGS_PERMISSIVE))
3279 rc = -EACCES;
3280 } else {
3281 /* Cache miss. */
3282 rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass,
3283 perms, 0, avdp);
3284 task_avdcache_update(tsec, isec, avdp);
3285 }
3286
3287 audited = avc_audit_required(perms, avdp, rc,
3288 (requested & MAY_ACCESS) ?
3289 FILE__AUDIT_ACCESS : 0, &denied);
3290 if (likely(!audited))
3291 return rc;
3292
3293 rc2 = audit_inode_permission(inode, perms, audited, denied, rc);
3294 if (rc2)
3295 return rc2;
3296
3297 return rc;
3298 }
3299
selinux_inode_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)3300 static int selinux_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
3301 struct iattr *iattr)
3302 {
3303 const struct cred *cred = current_cred();
3304 struct inode *inode = d_backing_inode(dentry);
3305 unsigned int ia_valid = iattr->ia_valid;
3306 u32 av = FILE__WRITE;
3307
3308 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3309 if (ia_valid & ATTR_FORCE) {
3310 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3311 ATTR_FORCE);
3312 if (!ia_valid)
3313 return 0;
3314 }
3315
3316 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3317 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
3318 return dentry_has_perm(cred, dentry, FILE__SETATTR);
3319
3320 if (selinux_policycap_openperm() &&
3321 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3322 (ia_valid & ATTR_SIZE) &&
3323 !(ia_valid & ATTR_FILE))
3324 av |= FILE__OPEN;
3325
3326 return dentry_has_perm(cred, dentry, av);
3327 }
3328
selinux_inode_getattr(const struct path * path)3329 static int selinux_inode_getattr(const struct path *path)
3330 {
3331 struct task_security_struct *tsec;
3332
3333 tsec = selinux_task(current);
3334
3335 if (task_avdcache_permnoaudit(tsec, current_sid()))
3336 return 0;
3337
3338 return path_has_perm(current_cred(), path, FILE__GETATTR);
3339 }
3340
has_cap_mac_admin(bool audit)3341 static bool has_cap_mac_admin(bool audit)
3342 {
3343 const struct cred *cred = current_cred();
3344 unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
3345
3346 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
3347 return false;
3348 if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
3349 return false;
3350 return true;
3351 }
3352
3353 /**
3354 * selinux_inode_xattr_skipcap - Skip the xattr capability checks?
3355 * @name: name of the xattr
3356 *
3357 * Returns 1 to indicate that SELinux "owns" the access control rights to xattrs
3358 * named @name; the LSM layer should avoid enforcing any traditional
3359 * capability based access controls on this xattr. Returns 0 to indicate that
3360 * SELinux does not "own" the access control rights to xattrs named @name and is
3361 * deferring to the LSM layer for further access controls, including capability
3362 * based controls.
3363 */
selinux_inode_xattr_skipcap(const char * name)3364 static int selinux_inode_xattr_skipcap(const char *name)
3365 {
3366 /* require capability check if not a selinux xattr */
3367 return !strcmp(name, XATTR_NAME_SELINUX);
3368 }
3369
selinux_inode_setxattr(struct mnt_idmap * idmap,struct dentry * dentry,const char * name,const void * value,size_t size,int flags)3370 static int selinux_inode_setxattr(struct mnt_idmap *idmap,
3371 struct dentry *dentry, const char *name,
3372 const void *value, size_t size, int flags)
3373 {
3374 struct inode *inode = d_backing_inode(dentry);
3375 struct inode_security_struct *isec;
3376 struct superblock_security_struct *sbsec;
3377 struct common_audit_data ad;
3378 u32 newsid, sid = current_sid();
3379 int rc = 0;
3380
3381 /* if not a selinux xattr, only check the ordinary setattr perm */
3382 if (strcmp(name, XATTR_NAME_SELINUX))
3383 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3384
3385 if (!selinux_initialized())
3386 return (inode_owner_or_capable(idmap, inode) ? 0 : -EPERM);
3387
3388 sbsec = selinux_superblock(inode->i_sb);
3389 if (!(sbsec->flags & SBLABEL_MNT))
3390 return -EOPNOTSUPP;
3391
3392 if (!inode_owner_or_capable(idmap, inode))
3393 return -EPERM;
3394
3395 ad.type = LSM_AUDIT_DATA_DENTRY;
3396 ad.u.dentry = dentry;
3397
3398 isec = backing_inode_security(dentry);
3399 rc = avc_has_perm(sid, isec->sid, isec->sclass,
3400 FILE__RELABELFROM, &ad);
3401 if (rc)
3402 return rc;
3403
3404 rc = security_context_to_sid(value, size, &newsid,
3405 GFP_KERNEL);
3406 if (rc == -EINVAL) {
3407 if (!has_cap_mac_admin(true)) {
3408 struct audit_buffer *ab;
3409 size_t audit_size;
3410
3411 /* We strip a nul only if it is at the end, otherwise the
3412 * context contains a nul and we should audit that */
3413 if (value) {
3414 const char *str = value;
3415
3416 if (str[size - 1] == '\0')
3417 audit_size = size - 1;
3418 else
3419 audit_size = size;
3420 } else {
3421 audit_size = 0;
3422 }
3423 ab = audit_log_start(audit_context(),
3424 GFP_ATOMIC, AUDIT_SELINUX_ERR);
3425 if (!ab)
3426 return rc;
3427 audit_log_format(ab, "op=setxattr invalid_context=");
3428 audit_log_n_untrustedstring(ab, value, audit_size);
3429 audit_log_end(ab);
3430
3431 return rc;
3432 }
3433 rc = security_context_to_sid_force(value,
3434 size, &newsid);
3435 }
3436 if (rc)
3437 return rc;
3438
3439 rc = avc_has_perm(sid, newsid, isec->sclass,
3440 FILE__RELABELTO, &ad);
3441 if (rc)
3442 return rc;
3443
3444 rc = security_validate_transition(isec->sid, newsid,
3445 sid, isec->sclass);
3446 if (rc)
3447 return rc;
3448
3449 return avc_has_perm(newsid,
3450 sbsec->sid,
3451 SECCLASS_FILESYSTEM,
3452 FILESYSTEM__ASSOCIATE,
3453 &ad);
3454 }
3455
selinux_inode_set_acl(struct mnt_idmap * idmap,struct dentry * dentry,const char * acl_name,struct posix_acl * kacl)3456 static int selinux_inode_set_acl(struct mnt_idmap *idmap,
3457 struct dentry *dentry, const char *acl_name,
3458 struct posix_acl *kacl)
3459 {
3460 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3461 }
3462
selinux_inode_get_acl(struct mnt_idmap * idmap,struct dentry * dentry,const char * acl_name)3463 static int selinux_inode_get_acl(struct mnt_idmap *idmap,
3464 struct dentry *dentry, const char *acl_name)
3465 {
3466 return dentry_has_perm(current_cred(), dentry, FILE__GETATTR);
3467 }
3468
selinux_inode_remove_acl(struct mnt_idmap * idmap,struct dentry * dentry,const char * acl_name)3469 static int selinux_inode_remove_acl(struct mnt_idmap *idmap,
3470 struct dentry *dentry, const char *acl_name)
3471 {
3472 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3473 }
3474
selinux_inode_post_setxattr(struct dentry * dentry,const char * name,const void * value,size_t size,int flags)3475 static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
3476 const void *value, size_t size,
3477 int flags)
3478 {
3479 struct inode *inode = d_backing_inode(dentry);
3480 struct inode_security_struct *isec;
3481 u32 newsid;
3482 int rc;
3483
3484 if (strcmp(name, XATTR_NAME_SELINUX)) {
3485 /* Not an attribute we recognize, so nothing to do. */
3486 return;
3487 }
3488
3489 if (!selinux_initialized()) {
3490 /* If we haven't even been initialized, then we can't validate
3491 * against a policy, so leave the label as invalid. It may
3492 * resolve to a valid label on the next revalidation try if
3493 * we've since initialized.
3494 */
3495 return;
3496 }
3497
3498 rc = security_context_to_sid_force(value, size,
3499 &newsid);
3500 if (rc) {
3501 pr_err("SELinux: unable to map context to SID"
3502 "for (%s, %llu), rc=%d\n",
3503 inode->i_sb->s_id, inode->i_ino, -rc);
3504 return;
3505 }
3506
3507 isec = backing_inode_security(dentry);
3508 spin_lock(&isec->lock);
3509 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3510 isec->sid = newsid;
3511 isec->initialized = LABEL_INITIALIZED;
3512 spin_unlock(&isec->lock);
3513 }
3514
selinux_inode_getxattr(struct dentry * dentry,const char * name)3515 static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
3516 {
3517 const struct cred *cred = current_cred();
3518
3519 return dentry_has_perm(cred, dentry, FILE__GETATTR);
3520 }
3521
selinux_inode_listxattr(struct dentry * dentry)3522 static int selinux_inode_listxattr(struct dentry *dentry)
3523 {
3524 const struct cred *cred = current_cred();
3525
3526 return dentry_has_perm(cred, dentry, FILE__GETATTR);
3527 }
3528
selinux_inode_removexattr(struct mnt_idmap * idmap,struct dentry * dentry,const char * name)3529 static int selinux_inode_removexattr(struct mnt_idmap *idmap,
3530 struct dentry *dentry, const char *name)
3531 {
3532 /* if not a selinux xattr, only check the ordinary setattr perm */
3533 if (strcmp(name, XATTR_NAME_SELINUX))
3534 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3535
3536 if (!selinux_initialized())
3537 return 0;
3538
3539 /* No one is allowed to remove a SELinux security label.
3540 You can change the label, but all data must be labeled. */
3541 return -EACCES;
3542 }
3543
selinux_inode_file_setattr(struct dentry * dentry,struct file_kattr * fa)3544 static int selinux_inode_file_setattr(struct dentry *dentry,
3545 struct file_kattr *fa)
3546 {
3547 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3548 }
3549
selinux_inode_file_getattr(struct dentry * dentry,struct file_kattr * fa)3550 static int selinux_inode_file_getattr(struct dentry *dentry,
3551 struct file_kattr *fa)
3552 {
3553 return dentry_has_perm(current_cred(), dentry, FILE__GETATTR);
3554 }
3555
selinux_path_notify(const struct path * path,u64 mask,unsigned int obj_type)3556 static int selinux_path_notify(const struct path *path, u64 mask,
3557 unsigned int obj_type)
3558 {
3559 int ret;
3560 u32 perm;
3561
3562 struct common_audit_data ad;
3563
3564 ad.type = LSM_AUDIT_DATA_PATH;
3565 ad.u.path = *path;
3566
3567 /*
3568 * Set permission needed based on the type of mark being set.
3569 * Performs an additional check for sb watches.
3570 */
3571 switch (obj_type) {
3572 case FSNOTIFY_OBJ_TYPE_VFSMOUNT:
3573 perm = FILE__WATCH_MOUNT;
3574 break;
3575 case FSNOTIFY_OBJ_TYPE_SB:
3576 perm = FILE__WATCH_SB;
3577 ret = superblock_has_perm(current_cred(), path->dentry->d_sb,
3578 FILESYSTEM__WATCH, &ad);
3579 if (ret)
3580 return ret;
3581 break;
3582 case FSNOTIFY_OBJ_TYPE_INODE:
3583 perm = FILE__WATCH;
3584 break;
3585 case FSNOTIFY_OBJ_TYPE_MNTNS:
3586 perm = FILE__WATCH_MOUNTNS;
3587 break;
3588 default:
3589 return -EINVAL;
3590 }
3591
3592 /* blocking watches require the file:watch_with_perm permission */
3593 if (mask & (ALL_FSNOTIFY_PERM_EVENTS))
3594 perm |= FILE__WATCH_WITH_PERM;
3595
3596 /* watches on read-like events need the file:watch_reads permission */
3597 if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_PRE_ACCESS |
3598 FS_CLOSE_NOWRITE))
3599 perm |= FILE__WATCH_READS;
3600
3601 return path_has_perm(current_cred(), path, perm);
3602 }
3603
3604 /*
3605 * Copy the inode security context value to the user.
3606 *
3607 * Permission check is handled by selinux_inode_getxattr hook.
3608 */
selinux_inode_getsecurity(struct mnt_idmap * idmap,struct inode * inode,const char * name,void ** buffer,bool alloc)3609 static int selinux_inode_getsecurity(struct mnt_idmap *idmap,
3610 struct inode *inode, const char *name,
3611 void **buffer, bool alloc)
3612 {
3613 u32 size;
3614 int error;
3615 char *context = NULL;
3616 struct inode_security_struct *isec;
3617
3618 /*
3619 * If we're not initialized yet, then we can't validate contexts, so
3620 * just let vfs_getxattr fall back to using the on-disk xattr.
3621 */
3622 if (!selinux_initialized() ||
3623 strcmp(name, XATTR_SELINUX_SUFFIX))
3624 return -EOPNOTSUPP;
3625
3626 /*
3627 * If the caller has CAP_MAC_ADMIN, then get the raw context
3628 * value even if it is not defined by current policy; otherwise,
3629 * use the in-core value under current policy.
3630 * Use the non-auditing forms of the permission checks since
3631 * getxattr may be called by unprivileged processes commonly
3632 * and lack of permission just means that we fall back to the
3633 * in-core context value, not a denial.
3634 */
3635 isec = inode_security(inode);
3636 if (has_cap_mac_admin(false))
3637 error = security_sid_to_context_force(isec->sid, &context,
3638 &size);
3639 else
3640 error = security_sid_to_context(isec->sid,
3641 &context, &size);
3642 if (error)
3643 return error;
3644 error = size;
3645 if (alloc) {
3646 *buffer = context;
3647 goto out_nofree;
3648 }
3649 kfree(context);
3650 out_nofree:
3651 return error;
3652 }
3653
selinux_inode_setsecurity(struct inode * inode,const char * name,const void * value,size_t size,int flags)3654 static int selinux_inode_setsecurity(struct inode *inode, const char *name,
3655 const void *value, size_t size, int flags)
3656 {
3657 struct inode_security_struct *isec = inode_security_novalidate(inode);
3658 struct superblock_security_struct *sbsec;
3659 u32 newsid;
3660 int rc;
3661
3662 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3663 return -EOPNOTSUPP;
3664
3665 sbsec = selinux_superblock(inode->i_sb);
3666 if (!(sbsec->flags & SBLABEL_MNT))
3667 return -EOPNOTSUPP;
3668
3669 if (!value || !size)
3670 return -EACCES;
3671
3672 rc = security_context_to_sid(value, size, &newsid,
3673 GFP_KERNEL);
3674 if (rc)
3675 return rc;
3676
3677 spin_lock(&isec->lock);
3678 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3679 isec->sid = newsid;
3680 isec->initialized = LABEL_INITIALIZED;
3681 spin_unlock(&isec->lock);
3682 return 0;
3683 }
3684
selinux_inode_listsecurity(struct inode * inode,char ** buffer,ssize_t * remaining_size)3685 static int selinux_inode_listsecurity(struct inode *inode, char **buffer,
3686 ssize_t *remaining_size)
3687 {
3688 if (!selinux_initialized())
3689 return 0;
3690 return xattr_list_one(buffer, remaining_size, XATTR_NAME_SELINUX);
3691 }
3692
selinux_inode_getlsmprop(struct inode * inode,struct lsm_prop * prop)3693 static void selinux_inode_getlsmprop(struct inode *inode, struct lsm_prop *prop)
3694 {
3695 struct inode_security_struct *isec = inode_security_novalidate(inode);
3696
3697 prop->selinux.secid = isec->sid;
3698 }
3699
selinux_inode_copy_up(struct dentry * src,struct cred ** new)3700 static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3701 {
3702 struct lsm_prop prop;
3703 struct cred_security_struct *crsec;
3704 struct cred *new_creds = *new;
3705
3706 if (new_creds == NULL) {
3707 new_creds = prepare_creds();
3708 if (!new_creds)
3709 return -ENOMEM;
3710 }
3711
3712 crsec = selinux_cred(new_creds);
3713 /* Get label from overlay inode and set it in create_sid */
3714 selinux_inode_getlsmprop(d_inode(src), &prop);
3715 crsec->create_sid = prop.selinux.secid;
3716 *new = new_creds;
3717 return 0;
3718 }
3719
selinux_inode_copy_up_xattr(struct dentry * dentry,const char * name)3720 static int selinux_inode_copy_up_xattr(struct dentry *dentry, const char *name)
3721 {
3722 /* The copy_up hook above sets the initial context on an inode, but we
3723 * don't then want to overwrite it by blindly copying all the lower
3724 * xattrs up. Instead, filter out SELinux-related xattrs following
3725 * policy load.
3726 */
3727 if (selinux_initialized() && !strcmp(name, XATTR_NAME_SELINUX))
3728 return -ECANCELED; /* Discard */
3729 /*
3730 * Any other attribute apart from SELINUX is not claimed, supported
3731 * by selinux.
3732 */
3733 return -EOPNOTSUPP;
3734 }
3735
3736 /* kernfs node operations */
3737
selinux_kernfs_init_security(struct kernfs_node * kn_dir,struct kernfs_node * kn)3738 static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
3739 struct kernfs_node *kn)
3740 {
3741 const struct cred_security_struct *crsec = selinux_cred(current_cred());
3742 u32 parent_sid, newsid, clen;
3743 int rc;
3744 char *context;
3745
3746 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0);
3747 if (rc == -ENODATA)
3748 return 0;
3749 else if (rc < 0)
3750 return rc;
3751
3752 clen = (u32)rc;
3753 context = kmalloc(clen, GFP_KERNEL);
3754 if (!context)
3755 return -ENOMEM;
3756
3757 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen);
3758 if (rc < 0) {
3759 kfree(context);
3760 return rc;
3761 }
3762
3763 rc = security_context_to_sid(context, clen, &parent_sid,
3764 GFP_KERNEL);
3765 kfree(context);
3766 if (rc)
3767 return rc;
3768
3769 if (crsec->create_sid) {
3770 newsid = crsec->create_sid;
3771 } else {
3772 u16 secclass = inode_mode_to_security_class(kn->mode);
3773 const char *kn_name;
3774 struct qstr q;
3775
3776 /* kn is fresh, can't be renamed, name goes not away */
3777 kn_name = rcu_dereference_check(kn->name, true);
3778 q.name = kn_name;
3779 q.hash_len = hashlen_string(kn_dir, kn_name);
3780
3781 rc = security_transition_sid(crsec->sid,
3782 parent_sid, secclass, &q,
3783 &newsid);
3784 if (rc)
3785 return rc;
3786 }
3787
3788 rc = security_sid_to_context_force(newsid,
3789 &context, &clen);
3790 if (rc)
3791 return rc;
3792
3793 rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen,
3794 XATTR_CREATE);
3795 kfree(context);
3796 return rc;
3797 }
3798
3799
3800 /* file security operations */
3801
selinux_revalidate_file_permission(struct file * file,int mask)3802 static int selinux_revalidate_file_permission(struct file *file, int mask)
3803 {
3804 const struct cred *cred = current_cred();
3805 struct inode *inode = file_inode(file);
3806
3807 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3808 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3809 mask |= MAY_APPEND;
3810
3811 return file_has_perm(cred, file,
3812 file_mask_to_av(inode->i_mode, mask));
3813 }
3814
selinux_file_permission(struct file * file,int mask)3815 static int selinux_file_permission(struct file *file, int mask)
3816 {
3817 struct inode *inode = file_inode(file);
3818 struct file_security_struct *fsec = selinux_file(file);
3819 struct inode_security_struct *isec;
3820 u32 sid = current_sid();
3821
3822 if (!mask)
3823 /* No permission to check. Existence test. */
3824 return 0;
3825
3826 isec = inode_security(inode);
3827 if (sid == fsec->sid && fsec->isid == isec->sid &&
3828 fsec->pseqno == avc_policy_seqno())
3829 /* No change since file_open check. */
3830 return 0;
3831
3832 return selinux_revalidate_file_permission(file, mask);
3833 }
3834
selinux_file_alloc_security(struct file * file)3835 static int selinux_file_alloc_security(struct file *file)
3836 {
3837 struct file_security_struct *fsec = selinux_file(file);
3838 u32 sid = current_sid();
3839
3840 fsec->sid = sid;
3841 fsec->fown_sid = sid;
3842
3843 return 0;
3844 }
3845
selinux_backing_file_alloc(struct file * backing_file,const struct file * user_file)3846 static int selinux_backing_file_alloc(struct file *backing_file,
3847 const struct file *user_file)
3848 {
3849 struct backing_file_security_struct *bfsec;
3850
3851 bfsec = selinux_backing_file(backing_file);
3852 bfsec->uf_sid = selinux_file(user_file)->sid;
3853
3854 return 0;
3855 }
3856
3857 /*
3858 * Check whether a task has the ioctl permission and cmd
3859 * operation to an inode.
3860 */
ioctl_has_perm(const struct cred * cred,struct file * file,u32 requested,u16 cmd)3861 static int ioctl_has_perm(const struct cred *cred, struct file *file,
3862 u32 requested, u16 cmd)
3863 {
3864 struct common_audit_data ad;
3865 struct file_security_struct *fsec = selinux_file(file);
3866 struct inode *inode = file_inode(file);
3867 struct inode_security_struct *isec;
3868 struct lsm_ioctlop_audit ioctl;
3869 u32 ssid = cred_sid(cred);
3870 int rc;
3871 u8 driver = cmd >> 8;
3872 u8 xperm = cmd & 0xff;
3873
3874 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3875 ad.u.op = &ioctl;
3876 ad.u.op->cmd = cmd;
3877 ad.u.op->path = file->f_path;
3878
3879 if (ssid != fsec->sid) {
3880 rc = avc_has_perm(ssid, fsec->sid,
3881 SECCLASS_FD,
3882 FD__USE,
3883 &ad);
3884 if (rc)
3885 goto out;
3886 }
3887
3888 if (unlikely(IS_PRIVATE(inode)))
3889 return 0;
3890
3891 isec = inode_security(inode);
3892 rc = avc_has_extended_perms(ssid, isec->sid, isec->sclass, requested,
3893 driver, AVC_EXT_IOCTL, xperm, &ad);
3894 out:
3895 return rc;
3896 }
3897
selinux_file_ioctl(struct file * file,unsigned int cmd,unsigned long arg)3898 static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3899 unsigned long arg)
3900 {
3901 const struct cred *cred = current_cred();
3902 int error = 0;
3903
3904 switch (cmd) {
3905 case FIONREAD:
3906 case FIBMAP:
3907 case FIGETBSZ:
3908 case FS_IOC_GETFLAGS:
3909 case FS_IOC_GETVERSION:
3910 error = file_has_perm(cred, file, FILE__GETATTR);
3911 break;
3912
3913 case FS_IOC_SETFLAGS:
3914 case FS_IOC_SETVERSION:
3915 error = file_has_perm(cred, file, FILE__SETATTR);
3916 break;
3917
3918 /* sys_ioctl() checks */
3919 case FIONBIO:
3920 case FIOASYNC:
3921 error = file_has_perm(cred, file, 0);
3922 break;
3923
3924 case KDSKBENT:
3925 case KDSKBSENT:
3926 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3927 CAP_OPT_NONE, true);
3928 break;
3929
3930 case FIOCLEX:
3931 case FIONCLEX:
3932 if (!selinux_policycap_ioctl_skip_cloexec())
3933 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
3934 break;
3935
3936 /* default case assumes that the command will go
3937 * to the file's ioctl() function.
3938 */
3939 default:
3940 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
3941 }
3942 return error;
3943 }
3944
selinux_file_ioctl_compat(struct file * file,unsigned int cmd,unsigned long arg)3945 static int selinux_file_ioctl_compat(struct file *file, unsigned int cmd,
3946 unsigned long arg)
3947 {
3948 /*
3949 * If we are in a 64-bit kernel running 32-bit userspace, we need to
3950 * make sure we don't compare 32-bit flags to 64-bit flags.
3951 */
3952 switch (cmd) {
3953 case FS_IOC32_GETFLAGS:
3954 cmd = FS_IOC_GETFLAGS;
3955 break;
3956 case FS_IOC32_SETFLAGS:
3957 cmd = FS_IOC_SETFLAGS;
3958 break;
3959 case FS_IOC32_GETVERSION:
3960 cmd = FS_IOC_GETVERSION;
3961 break;
3962 case FS_IOC32_SETVERSION:
3963 cmd = FS_IOC_SETVERSION;
3964 break;
3965 default:
3966 break;
3967 }
3968
3969 return selinux_file_ioctl(file, cmd, arg);
3970 }
3971
3972 static int default_noexec __ro_after_init;
3973
__file_map_prot_check(const struct file * file,unsigned long prot,bool shared,bool mounter_check,bool bf_user_file)3974 static int __file_map_prot_check(const struct file *file, unsigned long prot,
3975 bool shared, bool mounter_check,
3976 bool bf_user_file)
3977 {
3978 struct inode *inode = NULL;
3979 bool prot_exec = prot & PROT_EXEC;
3980 bool prot_write = prot & PROT_WRITE;
3981
3982 if (file) {
3983 if (bf_user_file)
3984 inode = d_inode(backing_file_user_path(file)->dentry);
3985 else
3986 inode = file_inode(file);
3987 }
3988
3989 if (!mounter_check && default_noexec && prot_exec &&
3990 (!file || IS_PRIVATE(inode) || (!shared && prot_write))) {
3991 int rc;
3992 u32 sid = current_sid();
3993
3994 /*
3995 * We are making executable an anonymous mapping or a private
3996 * file mapping that will also be writable.
3997 */
3998 rc = avc_has_perm(sid, sid, SECCLASS_PROCESS, PROCESS__EXECMEM,
3999 NULL);
4000 if (rc)
4001 return rc;
4002 }
4003
4004 if (file) {
4005 const struct cred *cred = mounter_check ?
4006 file->f_cred : current_cred();
4007 /* "read" always possible, "write" only if shared */
4008 u32 av = FILE__READ;
4009 if (shared && prot_write)
4010 av |= FILE__WRITE;
4011 if (prot_exec)
4012 av |= FILE__EXECUTE;
4013
4014 return __file_has_perm(cred, file, av, bf_user_file);
4015 }
4016
4017 return 0;
4018 }
4019
file_map_prot_check(const struct file * file,unsigned long prot,bool shared,bool mounter_check)4020 static inline int file_map_prot_check(const struct file *file,
4021 unsigned long prot, bool shared,
4022 bool mounter_check)
4023 {
4024 return __file_map_prot_check(file, prot, shared, mounter_check, false);
4025 }
4026
selinux_mmap_addr(unsigned long addr)4027 static int selinux_mmap_addr(unsigned long addr)
4028 {
4029 int rc = 0;
4030
4031 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
4032 u32 sid = current_sid();
4033 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
4034 MEMPROTECT__MMAP_ZERO, NULL);
4035 }
4036
4037 return rc;
4038 }
4039
selinux_mmap_file_common(struct file * file,unsigned long prot,bool shared,bool mounter_check)4040 static int selinux_mmap_file_common(struct file *file, unsigned long prot,
4041 bool shared, bool mounter_check)
4042 {
4043 if (file) {
4044 int rc;
4045 struct common_audit_data ad;
4046 const struct cred *cred = mounter_check ?
4047 file->f_cred : current_cred();
4048
4049 ad.type = LSM_AUDIT_DATA_FILE;
4050 ad.u.file = file;
4051 rc = inode_has_perm(cred, file_inode(file), FILE__MAP, &ad);
4052 if (rc)
4053 return rc;
4054 }
4055
4056 return file_map_prot_check(file, prot, shared, mounter_check);
4057 }
4058
selinux_mmap_file(struct file * file,unsigned long reqprot __always_unused,unsigned long prot,unsigned long flags)4059 static int selinux_mmap_file(struct file *file,
4060 unsigned long reqprot __always_unused,
4061 unsigned long prot, unsigned long flags)
4062 {
4063 return selinux_mmap_file_common(file, prot,
4064 (flags & MAP_TYPE) == MAP_SHARED,
4065 false);
4066 }
4067
4068 /**
4069 * selinux_mmap_backing_file - Check mmap permissions on a backing file
4070 * @vma: memory region
4071 * @backing_file: stacked filesystem backing file
4072 * @user_file: user visible file
4073 *
4074 * This is called after selinux_mmap_file() on stacked filesystems, and it
4075 * is this function's responsibility to verify access to @backing_file and
4076 * setup the SELinux state for possible later use in the mprotect() code path.
4077 *
4078 * By the time this function is called, mmap() access to @user_file has already
4079 * been authorized and @vma->vm_file has been set to point to @backing_file.
4080 *
4081 * Return zero on success, negative values otherwise.
4082 */
selinux_mmap_backing_file(struct vm_area_struct * vma,struct file * backing_file,struct file * user_file __always_unused)4083 static int selinux_mmap_backing_file(struct vm_area_struct *vma,
4084 struct file *backing_file,
4085 struct file *user_file __always_unused)
4086 {
4087 unsigned long prot = 0;
4088
4089 /* translate vma->vm_flags perms into PROT perms */
4090 if (vma->vm_flags & VM_READ)
4091 prot |= PROT_READ;
4092 if (vma->vm_flags & VM_WRITE)
4093 prot |= PROT_WRITE;
4094 if (vma->vm_flags & VM_EXEC)
4095 prot |= PROT_EXEC;
4096
4097 return selinux_mmap_file_common(backing_file, prot,
4098 vma->vm_flags & VM_SHARED,
4099 true);
4100 }
4101
selinux_file_mprotect(struct vm_area_struct * vma,unsigned long reqprot __always_unused,unsigned long prot)4102 static int selinux_file_mprotect(struct vm_area_struct *vma,
4103 unsigned long reqprot __always_unused,
4104 unsigned long prot)
4105 {
4106 int rc;
4107 const struct cred *cred = current_cred();
4108 u32 sid = cred_sid(cred);
4109 const struct file *file = vma->vm_file;
4110 bool backing_file;
4111 bool shared = vma->vm_flags & VM_SHARED;
4112
4113 /* check if we need to trigger the "backing files are awful" mode */
4114 backing_file = file && (file->f_mode & FMODE_BACKING);
4115
4116 if (default_noexec &&
4117 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
4118 /*
4119 * We don't use the vma_is_initial_heap() helper as it has
4120 * a history of problems and is currently broken on systems
4121 * where there is no heap, e.g. brk == start_brk. Before
4122 * replacing the conditional below with vma_is_initial_heap(),
4123 * or something similar, please ensure that the logic is the
4124 * same as what we have below or you have tested every possible
4125 * corner case you can think to test.
4126 */
4127 if (vma->vm_start >= vma->vm_mm->start_brk &&
4128 vma->vm_end <= vma->vm_mm->brk) {
4129 rc = avc_has_perm(sid, sid, SECCLASS_PROCESS,
4130 PROCESS__EXECHEAP, NULL);
4131 if (rc)
4132 return rc;
4133 } else if (!file && (vma_is_initial_stack(vma) ||
4134 vma_is_stack_for_current(vma))) {
4135 rc = avc_has_perm(sid, sid, SECCLASS_PROCESS,
4136 PROCESS__EXECSTACK, NULL);
4137 if (rc)
4138 return rc;
4139 } else if (file && vma->anon_vma) {
4140 /*
4141 * We are making executable a file mapping that has
4142 * had some COW done. Since pages might have been
4143 * written, check ability to execute the possibly
4144 * modified content. This typically should only
4145 * occur for text relocations.
4146 */
4147 rc = __file_has_perm(cred, file, FILE__EXECMOD,
4148 backing_file);
4149 if (rc)
4150 return rc;
4151 if (backing_file) {
4152 rc = file_has_perm(file->f_cred, file,
4153 FILE__EXECMOD);
4154 if (rc)
4155 return rc;
4156 }
4157 }
4158 }
4159
4160 rc = __file_map_prot_check(file, prot, shared, false, backing_file);
4161 if (rc)
4162 return rc;
4163 if (backing_file) {
4164 rc = file_map_prot_check(file, prot, shared, true);
4165 if (rc)
4166 return rc;
4167 }
4168
4169 return 0;
4170 }
4171
selinux_file_lock(struct file * file,unsigned int cmd)4172 static int selinux_file_lock(struct file *file, unsigned int cmd)
4173 {
4174 const struct cred *cred = current_cred();
4175
4176 return file_has_perm(cred, file, FILE__LOCK);
4177 }
4178
selinux_file_fcntl(struct file * file,unsigned int cmd,unsigned long arg)4179 static int selinux_file_fcntl(struct file *file, unsigned int cmd,
4180 unsigned long arg)
4181 {
4182 const struct cred *cred = current_cred();
4183 int err = 0;
4184
4185 switch (cmd) {
4186 case F_SETFL:
4187 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
4188 err = file_has_perm(cred, file, FILE__WRITE);
4189 break;
4190 }
4191 fallthrough;
4192 case F_SETOWN:
4193 case F_SETSIG:
4194 case F_GETFL:
4195 case F_GETOWN:
4196 case F_GETSIG:
4197 case F_GETOWNER_UIDS:
4198 /* Just check FD__USE permission */
4199 err = file_has_perm(cred, file, 0);
4200 break;
4201 case F_GETLK:
4202 case F_SETLK:
4203 case F_SETLKW:
4204 case F_OFD_GETLK:
4205 case F_OFD_SETLK:
4206 case F_OFD_SETLKW:
4207 #if BITS_PER_LONG == 32
4208 case F_GETLK64:
4209 case F_SETLK64:
4210 case F_SETLKW64:
4211 #endif
4212 err = file_has_perm(cred, file, FILE__LOCK);
4213 break;
4214 }
4215
4216 return err;
4217 }
4218
selinux_file_set_fowner(struct file * file)4219 static void selinux_file_set_fowner(struct file *file)
4220 {
4221 struct file_security_struct *fsec;
4222
4223 fsec = selinux_file(file);
4224 fsec->fown_sid = current_sid();
4225 }
4226
selinux_file_send_sigiotask(struct task_struct * tsk,struct fown_struct * fown,int signum)4227 static int selinux_file_send_sigiotask(struct task_struct *tsk,
4228 struct fown_struct *fown, int signum)
4229 {
4230 struct file *file;
4231 u32 sid = task_sid_obj(tsk);
4232 u32 perm;
4233 struct file_security_struct *fsec;
4234
4235 /* struct fown_struct is never outside the context of a struct file */
4236 file = fown->file;
4237
4238 fsec = selinux_file(file);
4239
4240 if (!signum)
4241 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
4242 else
4243 perm = signal_to_av(signum);
4244
4245 return avc_has_perm(fsec->fown_sid, sid,
4246 SECCLASS_PROCESS, perm, NULL);
4247 }
4248
selinux_file_receive(struct file * file)4249 static int selinux_file_receive(struct file *file)
4250 {
4251 const struct cred *cred = current_cred();
4252
4253 return file_has_perm(cred, file, file_to_av(file));
4254 }
4255
selinux_file_open(struct file * file)4256 static int selinux_file_open(struct file *file)
4257 {
4258 struct file_security_struct *fsec;
4259 struct inode_security_struct *isec;
4260
4261 fsec = selinux_file(file);
4262 isec = inode_security(file_inode(file));
4263 /*
4264 * Save inode label and policy sequence number
4265 * at open-time so that selinux_file_permission
4266 * can determine whether revalidation is necessary.
4267 * Task label is already saved in the file security
4268 * struct as its SID.
4269 */
4270 fsec->isid = isec->sid;
4271 fsec->pseqno = avc_policy_seqno();
4272 /*
4273 * Since the inode label or policy seqno may have changed
4274 * between the selinux_inode_permission check and the saving
4275 * of state above, recheck that access is still permitted.
4276 * Otherwise, access might never be revalidated against the
4277 * new inode label or new policy.
4278 * This check is not redundant - do not remove.
4279 */
4280 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
4281 }
4282
4283 /* task security operations */
4284
selinux_task_alloc(struct task_struct * task,u64 clone_flags)4285 static int selinux_task_alloc(struct task_struct *task,
4286 u64 clone_flags)
4287 {
4288 u32 sid = current_sid();
4289 struct task_security_struct *old_tsec = selinux_task(current);
4290 struct task_security_struct *new_tsec = selinux_task(task);
4291
4292 *new_tsec = *old_tsec;
4293 return avc_has_perm(sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
4294 }
4295
4296 /*
4297 * prepare a new set of credentials for modification
4298 */
selinux_cred_prepare(struct cred * new,const struct cred * old,gfp_t gfp)4299 static int selinux_cred_prepare(struct cred *new, const struct cred *old,
4300 gfp_t gfp)
4301 {
4302 const struct cred_security_struct *old_crsec = selinux_cred(old);
4303 struct cred_security_struct *crsec = selinux_cred(new);
4304
4305 *crsec = *old_crsec;
4306 return 0;
4307 }
4308
4309 /*
4310 * transfer the SELinux data to a blank set of creds
4311 */
selinux_cred_transfer(struct cred * new,const struct cred * old)4312 static void selinux_cred_transfer(struct cred *new, const struct cred *old)
4313 {
4314 const struct cred_security_struct *old_crsec = selinux_cred(old);
4315 struct cred_security_struct *crsec = selinux_cred(new);
4316
4317 *crsec = *old_crsec;
4318 }
4319
selinux_cred_getsecid(const struct cred * c,u32 * secid)4320 static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
4321 {
4322 *secid = cred_sid(c);
4323 }
4324
selinux_cred_getlsmprop(const struct cred * c,struct lsm_prop * prop)4325 static void selinux_cred_getlsmprop(const struct cred *c, struct lsm_prop *prop)
4326 {
4327 prop->selinux.secid = cred_sid(c);
4328 }
4329
4330 /*
4331 * set the security data for a kernel service
4332 * - all the creation contexts are set to unlabelled
4333 */
selinux_kernel_act_as(struct cred * new,u32 secid)4334 static int selinux_kernel_act_as(struct cred *new, u32 secid)
4335 {
4336 struct cred_security_struct *crsec = selinux_cred(new);
4337 u32 sid = current_sid();
4338 int ret;
4339
4340 ret = avc_has_perm(sid, secid,
4341 SECCLASS_KERNEL_SERVICE,
4342 KERNEL_SERVICE__USE_AS_OVERRIDE,
4343 NULL);
4344 if (ret == 0) {
4345 crsec->sid = secid;
4346 crsec->create_sid = 0;
4347 crsec->keycreate_sid = 0;
4348 crsec->sockcreate_sid = 0;
4349 }
4350 return ret;
4351 }
4352
4353 /*
4354 * set the file creation context in a security record to the same as the
4355 * objective context of the specified inode
4356 */
selinux_kernel_create_files_as(struct cred * new,struct inode * inode)4357 static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
4358 {
4359 struct inode_security_struct *isec = inode_security(inode);
4360 struct cred_security_struct *crsec = selinux_cred(new);
4361 u32 sid = current_sid();
4362 int ret;
4363
4364 ret = avc_has_perm(sid, isec->sid,
4365 SECCLASS_KERNEL_SERVICE,
4366 KERNEL_SERVICE__CREATE_FILES_AS,
4367 NULL);
4368
4369 if (ret == 0)
4370 crsec->create_sid = isec->sid;
4371 return ret;
4372 }
4373
selinux_kernel_module_request(char * kmod_name)4374 static int selinux_kernel_module_request(char *kmod_name)
4375 {
4376 struct common_audit_data ad;
4377
4378 ad.type = LSM_AUDIT_DATA_KMOD;
4379 ad.u.kmod_name = kmod_name;
4380
4381 return avc_has_perm(current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
4382 SYSTEM__MODULE_REQUEST, &ad);
4383 }
4384
selinux_kernel_load_from_file(struct file * file,u32 requested)4385 static int selinux_kernel_load_from_file(struct file *file, u32 requested)
4386 {
4387 struct common_audit_data ad;
4388 struct inode_security_struct *isec;
4389 struct file_security_struct *fsec;
4390 u32 sid = current_sid();
4391 int rc;
4392
4393 if (file == NULL)
4394 return avc_has_perm(sid, sid, SECCLASS_SYSTEM, requested, NULL);
4395
4396 ad.type = LSM_AUDIT_DATA_FILE;
4397 ad.u.file = file;
4398
4399 fsec = selinux_file(file);
4400 if (sid != fsec->sid) {
4401 rc = avc_has_perm(sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
4402 if (rc)
4403 return rc;
4404 }
4405
4406 isec = inode_security(file_inode(file));
4407 return avc_has_perm(sid, isec->sid, SECCLASS_SYSTEM, requested, &ad);
4408 }
4409
selinux_kernel_read_file(struct file * file,enum kernel_read_file_id id,bool contents)4410 static int selinux_kernel_read_file(struct file *file,
4411 enum kernel_read_file_id id,
4412 bool contents)
4413 {
4414 int rc = 0;
4415
4416 BUILD_BUG_ON_MSG(READING_MAX_ID > 8,
4417 "New kernel_read_file_id introduced; update SELinux!");
4418
4419 switch (id) {
4420 case READING_FIRMWARE:
4421 rc = selinux_kernel_load_from_file(file, SYSTEM__FIRMWARE_LOAD);
4422 break;
4423 case READING_MODULE:
4424 case READING_MODULE_COMPRESSED:
4425 rc = selinux_kernel_load_from_file(file, SYSTEM__MODULE_LOAD);
4426 break;
4427 case READING_KEXEC_IMAGE:
4428 rc = selinux_kernel_load_from_file(file,
4429 SYSTEM__KEXEC_IMAGE_LOAD);
4430 break;
4431 case READING_KEXEC_INITRAMFS:
4432 rc = selinux_kernel_load_from_file(file,
4433 SYSTEM__KEXEC_INITRAMFS_LOAD);
4434 break;
4435 case READING_POLICY:
4436 rc = selinux_kernel_load_from_file(file, SYSTEM__POLICY_LOAD);
4437 break;
4438 case READING_X509_CERTIFICATE:
4439 rc = selinux_kernel_load_from_file(file,
4440 SYSTEM__X509_CERTIFICATE_LOAD);
4441 break;
4442 default:
4443 break;
4444 }
4445
4446 return rc;
4447 }
4448
selinux_kernel_load_data(enum kernel_load_data_id id,bool contents)4449 static int selinux_kernel_load_data(enum kernel_load_data_id id, bool contents)
4450 {
4451 int rc = 0;
4452
4453 BUILD_BUG_ON_MSG(LOADING_MAX_ID > 8,
4454 "New kernel_load_data_id introduced; update SELinux!");
4455
4456 switch (id) {
4457 case LOADING_FIRMWARE:
4458 rc = selinux_kernel_load_from_file(NULL, SYSTEM__FIRMWARE_LOAD);
4459 break;
4460 case LOADING_MODULE:
4461 rc = selinux_kernel_load_from_file(NULL, SYSTEM__MODULE_LOAD);
4462 break;
4463 case LOADING_KEXEC_IMAGE:
4464 rc = selinux_kernel_load_from_file(NULL,
4465 SYSTEM__KEXEC_IMAGE_LOAD);
4466 break;
4467 case LOADING_KEXEC_INITRAMFS:
4468 rc = selinux_kernel_load_from_file(NULL,
4469 SYSTEM__KEXEC_INITRAMFS_LOAD);
4470 break;
4471 case LOADING_POLICY:
4472 rc = selinux_kernel_load_from_file(NULL,
4473 SYSTEM__POLICY_LOAD);
4474 break;
4475 case LOADING_X509_CERTIFICATE:
4476 rc = selinux_kernel_load_from_file(NULL,
4477 SYSTEM__X509_CERTIFICATE_LOAD);
4478 break;
4479 default:
4480 break;
4481 }
4482
4483 return rc;
4484 }
4485
selinux_task_setpgid(struct task_struct * p,pid_t pgid)4486 static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
4487 {
4488 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4489 PROCESS__SETPGID, NULL);
4490 }
4491
selinux_task_getpgid(struct task_struct * p)4492 static int selinux_task_getpgid(struct task_struct *p)
4493 {
4494 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4495 PROCESS__GETPGID, NULL);
4496 }
4497
selinux_task_getsid(struct task_struct * p)4498 static int selinux_task_getsid(struct task_struct *p)
4499 {
4500 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4501 PROCESS__GETSESSION, NULL);
4502 }
4503
selinux_current_getlsmprop_subj(struct lsm_prop * prop)4504 static void selinux_current_getlsmprop_subj(struct lsm_prop *prop)
4505 {
4506 prop->selinux.secid = current_sid();
4507 }
4508
selinux_task_getlsmprop_obj(struct task_struct * p,struct lsm_prop * prop)4509 static void selinux_task_getlsmprop_obj(struct task_struct *p,
4510 struct lsm_prop *prop)
4511 {
4512 prop->selinux.secid = task_sid_obj(p);
4513 }
4514
selinux_task_setnice(struct task_struct * p,int nice)4515 static int selinux_task_setnice(struct task_struct *p, int nice)
4516 {
4517 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4518 PROCESS__SETSCHED, NULL);
4519 }
4520
selinux_task_setioprio(struct task_struct * p,int ioprio)4521 static int selinux_task_setioprio(struct task_struct *p, int ioprio)
4522 {
4523 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4524 PROCESS__SETSCHED, NULL);
4525 }
4526
selinux_task_getioprio(struct task_struct * p)4527 static int selinux_task_getioprio(struct task_struct *p)
4528 {
4529 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4530 PROCESS__GETSCHED, NULL);
4531 }
4532
selinux_task_prlimit(const struct cred * cred,const struct cred * tcred,unsigned int flags)4533 static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4534 unsigned int flags)
4535 {
4536 u32 av = 0;
4537
4538 if (!flags)
4539 return 0;
4540 if (flags & LSM_PRLIMIT_WRITE)
4541 av |= PROCESS__SETRLIMIT;
4542 if (flags & LSM_PRLIMIT_READ)
4543 av |= PROCESS__GETRLIMIT;
4544 return avc_has_perm(cred_sid(cred), cred_sid(tcred),
4545 SECCLASS_PROCESS, av, NULL);
4546 }
4547
selinux_task_setrlimit(struct task_struct * p,unsigned int resource,struct rlimit * new_rlim)4548 static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4549 struct rlimit *new_rlim)
4550 {
4551 struct rlimit *old_rlim = p->signal->rlim + resource;
4552
4553 /* Control the ability to change the hard limit (whether
4554 lowering or raising it), so that the hard limit can
4555 later be used as a safe reset point for the soft limit
4556 upon context transitions. See selinux_bprm_committing_creds. */
4557 if (old_rlim->rlim_max != new_rlim->rlim_max)
4558 return avc_has_perm(current_sid(), task_sid_obj(p),
4559 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
4560
4561 return 0;
4562 }
4563
selinux_task_setscheduler(struct task_struct * p)4564 static int selinux_task_setscheduler(struct task_struct *p)
4565 {
4566 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4567 PROCESS__SETSCHED, NULL);
4568 }
4569
selinux_task_getscheduler(struct task_struct * p)4570 static int selinux_task_getscheduler(struct task_struct *p)
4571 {
4572 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4573 PROCESS__GETSCHED, NULL);
4574 }
4575
selinux_task_movememory(struct task_struct * p)4576 static int selinux_task_movememory(struct task_struct *p)
4577 {
4578 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4579 PROCESS__SETSCHED, NULL);
4580 }
4581
selinux_task_kill(struct task_struct * p,struct kernel_siginfo * info,int sig,const struct cred * cred)4582 static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
4583 int sig, const struct cred *cred)
4584 {
4585 u32 secid;
4586 u32 perm;
4587
4588 if (!sig)
4589 perm = PROCESS__SIGNULL; /* null signal; existence test */
4590 else
4591 perm = signal_to_av(sig);
4592 if (!cred)
4593 secid = current_sid();
4594 else
4595 secid = cred_sid(cred);
4596 return avc_has_perm(secid, task_sid_obj(p), SECCLASS_PROCESS, perm, NULL);
4597 }
4598
selinux_task_to_inode(struct task_struct * p,struct inode * inode)4599 static void selinux_task_to_inode(struct task_struct *p,
4600 struct inode *inode)
4601 {
4602 struct inode_security_struct *isec = selinux_inode(inode);
4603 u32 sid = task_sid_obj(p);
4604
4605 spin_lock(&isec->lock);
4606 isec->sclass = inode_mode_to_security_class(inode->i_mode);
4607 isec->sid = sid;
4608 isec->initialized = LABEL_INITIALIZED;
4609 spin_unlock(&isec->lock);
4610 }
4611
selinux_userns_create(const struct cred * cred)4612 static int selinux_userns_create(const struct cred *cred)
4613 {
4614 u32 sid = current_sid();
4615
4616 return avc_has_perm(sid, sid, SECCLASS_USER_NAMESPACE,
4617 USER_NAMESPACE__CREATE, NULL);
4618 }
4619
4620 /* Returns error only if unable to parse addresses */
selinux_parse_skb_ipv4(struct sk_buff * skb,struct common_audit_data * ad,u8 * proto)4621 static int selinux_parse_skb_ipv4(struct sk_buff *skb,
4622 struct common_audit_data *ad, u8 *proto)
4623 {
4624 int offset, ihlen, ret = -EINVAL;
4625 struct iphdr _iph, *ih;
4626
4627 offset = skb_network_offset(skb);
4628 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4629 if (ih == NULL)
4630 goto out;
4631
4632 ihlen = ih->ihl * 4;
4633 if (ihlen < sizeof(_iph))
4634 goto out;
4635
4636 ad->u.net->v4info.saddr = ih->saddr;
4637 ad->u.net->v4info.daddr = ih->daddr;
4638 ret = 0;
4639
4640 if (proto)
4641 *proto = ih->protocol;
4642
4643 switch (ih->protocol) {
4644 case IPPROTO_TCP: {
4645 struct tcphdr _tcph, *th;
4646
4647 if (ntohs(ih->frag_off) & IP_OFFSET)
4648 break;
4649
4650 offset += ihlen;
4651 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4652 if (th == NULL)
4653 break;
4654
4655 ad->u.net->sport = th->source;
4656 ad->u.net->dport = th->dest;
4657 break;
4658 }
4659
4660 case IPPROTO_UDP: {
4661 struct udphdr _udph, *uh;
4662
4663 if (ntohs(ih->frag_off) & IP_OFFSET)
4664 break;
4665
4666 offset += ihlen;
4667 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4668 if (uh == NULL)
4669 break;
4670
4671 ad->u.net->sport = uh->source;
4672 ad->u.net->dport = uh->dest;
4673 break;
4674 }
4675
4676 #if IS_ENABLED(CONFIG_IP_SCTP)
4677 case IPPROTO_SCTP: {
4678 struct sctphdr _sctph, *sh;
4679
4680 if (ntohs(ih->frag_off) & IP_OFFSET)
4681 break;
4682
4683 offset += ihlen;
4684 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4685 if (sh == NULL)
4686 break;
4687
4688 ad->u.net->sport = sh->source;
4689 ad->u.net->dport = sh->dest;
4690 break;
4691 }
4692 #endif
4693 default:
4694 break;
4695 }
4696 out:
4697 return ret;
4698 }
4699
4700 #if IS_ENABLED(CONFIG_IPV6)
4701
4702 /* Returns error only if unable to parse addresses */
selinux_parse_skb_ipv6(struct sk_buff * skb,struct common_audit_data * ad,u8 * proto)4703 static int selinux_parse_skb_ipv6(struct sk_buff *skb,
4704 struct common_audit_data *ad, u8 *proto)
4705 {
4706 u8 nexthdr;
4707 int ret = -EINVAL, offset;
4708 struct ipv6hdr _ipv6h, *ip6;
4709 __be16 frag_off;
4710
4711 offset = skb_network_offset(skb);
4712 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4713 if (ip6 == NULL)
4714 goto out;
4715
4716 ad->u.net->v6info.saddr = ip6->saddr;
4717 ad->u.net->v6info.daddr = ip6->daddr;
4718 ret = 0;
4719
4720 nexthdr = ip6->nexthdr;
4721 offset += sizeof(_ipv6h);
4722 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
4723 if (offset < 0)
4724 goto out;
4725
4726 if (proto)
4727 *proto = nexthdr;
4728
4729 switch (nexthdr) {
4730 case IPPROTO_TCP: {
4731 struct tcphdr _tcph, *th;
4732
4733 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4734 if (th == NULL)
4735 break;
4736
4737 ad->u.net->sport = th->source;
4738 ad->u.net->dport = th->dest;
4739 break;
4740 }
4741
4742 case IPPROTO_UDP: {
4743 struct udphdr _udph, *uh;
4744
4745 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4746 if (uh == NULL)
4747 break;
4748
4749 ad->u.net->sport = uh->source;
4750 ad->u.net->dport = uh->dest;
4751 break;
4752 }
4753
4754 #if IS_ENABLED(CONFIG_IP_SCTP)
4755 case IPPROTO_SCTP: {
4756 struct sctphdr _sctph, *sh;
4757
4758 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4759 if (sh == NULL)
4760 break;
4761
4762 ad->u.net->sport = sh->source;
4763 ad->u.net->dport = sh->dest;
4764 break;
4765 }
4766 #endif
4767 /* includes fragments */
4768 default:
4769 break;
4770 }
4771 out:
4772 return ret;
4773 }
4774
4775 #endif /* IPV6 */
4776
selinux_parse_skb(struct sk_buff * skb,struct common_audit_data * ad,char ** _addrp,int src,u8 * proto)4777 static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
4778 char **_addrp, int src, u8 *proto)
4779 {
4780 char *addrp;
4781 int ret;
4782
4783 switch (ad->u.net->family) {
4784 case PF_INET:
4785 ret = selinux_parse_skb_ipv4(skb, ad, proto);
4786 if (ret)
4787 goto parse_error;
4788 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4789 &ad->u.net->v4info.daddr);
4790 goto okay;
4791
4792 #if IS_ENABLED(CONFIG_IPV6)
4793 case PF_INET6:
4794 ret = selinux_parse_skb_ipv6(skb, ad, proto);
4795 if (ret)
4796 goto parse_error;
4797 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4798 &ad->u.net->v6info.daddr);
4799 goto okay;
4800 #endif /* IPV6 */
4801 default:
4802 addrp = NULL;
4803 goto okay;
4804 }
4805
4806 parse_error:
4807 pr_warn(
4808 "SELinux: failure in selinux_parse_skb(),"
4809 " unable to parse packet\n");
4810 return ret;
4811
4812 okay:
4813 if (_addrp)
4814 *_addrp = addrp;
4815 return 0;
4816 }
4817
4818 /**
4819 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
4820 * @skb: the packet
4821 * @family: protocol family
4822 * @sid: the packet's peer label SID
4823 *
4824 * Description:
4825 * Check the various different forms of network peer labeling and determine
4826 * the peer label/SID for the packet; most of the magic actually occurs in
4827 * the security server function security_net_peersid_cmp(). The function
4828 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4829 * or -EACCES if @sid is invalid due to inconsistencies with the different
4830 * peer labels.
4831 *
4832 */
selinux_skb_peerlbl_sid(struct sk_buff * skb,u16 family,u32 * sid)4833 static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
4834 {
4835 int err;
4836 u32 xfrm_sid;
4837 u32 nlbl_sid;
4838 u32 nlbl_type;
4839
4840 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
4841 if (unlikely(err))
4842 return -EACCES;
4843 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4844 if (unlikely(err))
4845 return -EACCES;
4846
4847 err = security_net_peersid_resolve(nlbl_sid,
4848 nlbl_type, xfrm_sid, sid);
4849 if (unlikely(err)) {
4850 pr_warn(
4851 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4852 " unable to determine packet's peer label\n");
4853 return -EACCES;
4854 }
4855
4856 return 0;
4857 }
4858
4859 /**
4860 * selinux_conn_sid - Determine the child socket label for a connection
4861 * @sk_sid: the parent socket's SID
4862 * @skb_sid: the packet's SID
4863 * @conn_sid: the resulting connection SID
4864 *
4865 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4866 * combined with the MLS information from @skb_sid in order to create
4867 * @conn_sid. If @skb_sid is not valid then @conn_sid is simply a copy
4868 * of @sk_sid. Returns zero on success, negative values on failure.
4869 *
4870 */
selinux_conn_sid(u32 sk_sid,u32 skb_sid,u32 * conn_sid)4871 static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4872 {
4873 int err = 0;
4874
4875 if (skb_sid != SECSID_NULL)
4876 err = security_sid_mls_copy(sk_sid, skb_sid,
4877 conn_sid);
4878 else
4879 *conn_sid = sk_sid;
4880
4881 return err;
4882 }
4883
4884 /* socket security operations */
4885
socket_sockcreate_sid(const struct cred_security_struct * crsec,u16 secclass,u32 * socksid)4886 static int socket_sockcreate_sid(const struct cred_security_struct *crsec,
4887 u16 secclass, u32 *socksid)
4888 {
4889 if (crsec->sockcreate_sid > SECSID_NULL) {
4890 *socksid = crsec->sockcreate_sid;
4891 return 0;
4892 }
4893
4894 return security_transition_sid(crsec->sid, crsec->sid,
4895 secclass, NULL, socksid);
4896 }
4897
sock_skip_has_perm(u32 sid)4898 static bool sock_skip_has_perm(u32 sid)
4899 {
4900 if (sid == SECINITSID_KERNEL)
4901 return true;
4902
4903 /*
4904 * Before POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT, sockets that
4905 * inherited the kernel context from early boot used to be skipped
4906 * here, so preserve that behavior unless the capability is set.
4907 *
4908 * By setting the capability the policy signals that it is ready
4909 * for this quirk to be fixed. Note that sockets created by a kernel
4910 * thread or a usermode helper executed without a transition will
4911 * still be skipped in this check regardless of the policycap
4912 * setting.
4913 */
4914 if (!selinux_policycap_userspace_initial_context() &&
4915 sid == SECINITSID_INIT)
4916 return true;
4917 return false;
4918 }
4919
4920
sock_has_perm(struct sock * sk,u32 perms)4921 static int sock_has_perm(struct sock *sk, u32 perms)
4922 {
4923 struct sk_security_struct *sksec = selinux_sock(sk);
4924 struct common_audit_data ad;
4925 struct lsm_network_audit net;
4926
4927 if (sock_skip_has_perm(sksec->sid))
4928 return 0;
4929
4930 ad_net_init_from_sk(&ad, &net, sk);
4931
4932 return avc_has_perm(current_sid(), sksec->sid, sksec->sclass, perms,
4933 &ad);
4934 }
4935
selinux_socket_create(int family,int type,int protocol,int kern)4936 static int selinux_socket_create(int family, int type,
4937 int protocol, int kern)
4938 {
4939 const struct cred_security_struct *crsec = selinux_cred(current_cred());
4940 u32 newsid;
4941 u16 secclass;
4942 int rc;
4943
4944 if (kern)
4945 return 0;
4946
4947 secclass = socket_type_to_security_class(family, type, protocol);
4948 rc = socket_sockcreate_sid(crsec, secclass, &newsid);
4949 if (rc)
4950 return rc;
4951
4952 return avc_has_perm(crsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
4953 }
4954
selinux_socket_post_create(struct socket * sock,int family,int type,int protocol,int kern)4955 static int selinux_socket_post_create(struct socket *sock, int family,
4956 int type, int protocol, int kern)
4957 {
4958 const struct cred_security_struct *crsec = selinux_cred(current_cred());
4959 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
4960 struct sk_security_struct *sksec;
4961 u16 sclass = socket_type_to_security_class(family, type, protocol);
4962 u32 sid = SECINITSID_KERNEL;
4963 int err = 0;
4964
4965 if (!kern) {
4966 err = socket_sockcreate_sid(crsec, sclass, &sid);
4967 if (err)
4968 return err;
4969 }
4970
4971 isec->sclass = sclass;
4972 isec->sid = sid;
4973 isec->initialized = LABEL_INITIALIZED;
4974
4975 if (sock->sk) {
4976 sksec = selinux_sock(sock->sk);
4977 sksec->sclass = sclass;
4978 sksec->sid = sid;
4979 /* Allows detection of the first association on this socket */
4980 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4981 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4982
4983 err = selinux_netlbl_socket_post_create(sock->sk, family);
4984 }
4985
4986 return err;
4987 }
4988
selinux_socket_socketpair(struct socket * socka,struct socket * sockb)4989 static int selinux_socket_socketpair(struct socket *socka,
4990 struct socket *sockb)
4991 {
4992 struct sk_security_struct *sksec_a = selinux_sock(socka->sk);
4993 struct sk_security_struct *sksec_b = selinux_sock(sockb->sk);
4994
4995 sksec_a->peer_sid = sksec_b->sid;
4996 sksec_b->peer_sid = sksec_a->sid;
4997
4998 return 0;
4999 }
5000
5001 /* Range of port numbers used to automatically bind.
5002 Need to determine whether we should perform a name_bind
5003 permission check between the socket and the port number. */
5004
__selinux_socket_bind(struct sock * sk,struct sockaddr * address,int addrlen)5005 static int __selinux_socket_bind(struct sock *sk, struct sockaddr *address, int addrlen)
5006 {
5007 struct sk_security_struct *sksec = selinux_sock(sk);
5008 u16 family;
5009 int err;
5010
5011 err = sock_has_perm(sk, SOCKET__BIND);
5012 if (err)
5013 goto out;
5014
5015 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
5016 family = sk->sk_family;
5017 if (family == PF_INET || family == PF_INET6) {
5018 char *addrp;
5019 struct common_audit_data ad;
5020 struct lsm_network_audit net = {0,};
5021 struct sockaddr_in *addr4 = NULL;
5022 struct sockaddr_in6 *addr6 = NULL;
5023 u16 family_sa;
5024 unsigned short snum;
5025 u32 sid, node_perm;
5026
5027 /*
5028 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
5029 * that validates multiple binding addresses. Because of this
5030 * need to check address->sa_family as it is possible to have
5031 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
5032 */
5033 if (addrlen < offsetofend(struct sockaddr, sa_family))
5034 return -EINVAL;
5035 family_sa = address->sa_family;
5036 switch (family_sa) {
5037 case AF_UNSPEC:
5038 case AF_INET:
5039 if (addrlen < sizeof(struct sockaddr_in))
5040 return -EINVAL;
5041 addr4 = (struct sockaddr_in *)address;
5042 if (family_sa == AF_UNSPEC) {
5043 if (family == PF_INET6) {
5044 /* Length check from inet6_bind_sk() */
5045 if (addrlen < SIN6_LEN_RFC2133)
5046 return -EINVAL;
5047 /* Family check from __inet6_bind() */
5048 goto err_af;
5049 }
5050 /* see __inet_bind(), we only want to allow
5051 * AF_UNSPEC if the address is INADDR_ANY
5052 */
5053 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
5054 goto err_af;
5055 family_sa = AF_INET;
5056 }
5057 snum = ntohs(addr4->sin_port);
5058 addrp = (char *)&addr4->sin_addr.s_addr;
5059 break;
5060 case AF_INET6:
5061 if (addrlen < SIN6_LEN_RFC2133)
5062 return -EINVAL;
5063 addr6 = (struct sockaddr_in6 *)address;
5064 snum = ntohs(addr6->sin6_port);
5065 addrp = (char *)&addr6->sin6_addr.s6_addr;
5066 break;
5067 default:
5068 goto err_af;
5069 }
5070
5071 ad.type = LSM_AUDIT_DATA_NET;
5072 ad.u.net = &net;
5073 ad.u.net->sport = htons(snum);
5074 ad.u.net->family = family_sa;
5075
5076 if (snum) {
5077 int low, high;
5078
5079 inet_get_local_port_range(sock_net(sk), &low, &high);
5080
5081 if (inet_port_requires_bind_service(sock_net(sk), snum) ||
5082 snum < low || snum > high) {
5083 err = sel_netport_sid(sk->sk_protocol,
5084 snum, &sid);
5085 if (err)
5086 goto out;
5087 err = avc_has_perm(sksec->sid, sid,
5088 sksec->sclass,
5089 SOCKET__NAME_BIND, &ad);
5090 if (err)
5091 goto out;
5092 }
5093 }
5094
5095 switch (sksec->sclass) {
5096 case SECCLASS_TCP_SOCKET:
5097 node_perm = TCP_SOCKET__NODE_BIND;
5098 break;
5099
5100 case SECCLASS_UDP_SOCKET:
5101 node_perm = UDP_SOCKET__NODE_BIND;
5102 break;
5103
5104 case SECCLASS_SCTP_SOCKET:
5105 node_perm = SCTP_SOCKET__NODE_BIND;
5106 break;
5107
5108 default:
5109 node_perm = RAWIP_SOCKET__NODE_BIND;
5110 break;
5111 }
5112
5113 err = sel_netnode_sid(addrp, family_sa, &sid);
5114 if (err)
5115 goto out;
5116
5117 if (family_sa == AF_INET)
5118 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
5119 else
5120 ad.u.net->v6info.saddr = addr6->sin6_addr;
5121
5122 err = avc_has_perm(sksec->sid, sid,
5123 sksec->sclass, node_perm, &ad);
5124 if (err)
5125 goto out;
5126 }
5127 out:
5128 return err;
5129 err_af:
5130 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
5131 if (sk->sk_protocol == IPPROTO_SCTP)
5132 return -EINVAL;
5133 return -EAFNOSUPPORT;
5134 }
5135
selinux_socket_bind(struct socket * sock,struct sockaddr * address,int addrlen)5136 static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
5137 {
5138 return __selinux_socket_bind(sock->sk, address, addrlen);
5139 }
5140
5141 /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
5142 * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
5143 */
selinux_socket_connect_helper(struct sock * sk,struct sockaddr * address,int addrlen)5144 static int selinux_socket_connect_helper(struct sock *sk,
5145 struct sockaddr *address, int addrlen)
5146 {
5147 struct sk_security_struct *sksec = selinux_sock(sk);
5148 int err;
5149
5150 err = sock_has_perm(sk, SOCKET__CONNECT);
5151 if (err)
5152 return err;
5153 if (addrlen < offsetofend(struct sockaddr, sa_family))
5154 return -EINVAL;
5155
5156 /* connect(AF_UNSPEC) has special handling, as it is a documented
5157 * way to disconnect the socket
5158 */
5159 if (address->sa_family == AF_UNSPEC)
5160 return 0;
5161
5162 /*
5163 * If a TCP or SCTP socket, check name_connect permission
5164 * for the port.
5165 */
5166 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
5167 sksec->sclass == SECCLASS_SCTP_SOCKET) {
5168 struct common_audit_data ad;
5169 struct lsm_network_audit net = {0,};
5170 struct sockaddr_in *addr4 = NULL;
5171 struct sockaddr_in6 *addr6 = NULL;
5172 unsigned short snum;
5173 u32 sid, perm;
5174
5175 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
5176 * that validates multiple connect addresses. Because of this
5177 * need to check address->sa_family as it is possible to have
5178 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
5179 */
5180 switch (address->sa_family) {
5181 case AF_INET:
5182 addr4 = (struct sockaddr_in *)address;
5183 if (addrlen < sizeof(struct sockaddr_in))
5184 return -EINVAL;
5185 snum = ntohs(addr4->sin_port);
5186 break;
5187 case AF_INET6:
5188 addr6 = (struct sockaddr_in6 *)address;
5189 if (addrlen < SIN6_LEN_RFC2133)
5190 return -EINVAL;
5191 snum = ntohs(addr6->sin6_port);
5192 break;
5193 default:
5194 /* Note that SCTP services expect -EINVAL, whereas
5195 * others expect -EAFNOSUPPORT.
5196 */
5197 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
5198 return -EINVAL;
5199 else
5200 return -EAFNOSUPPORT;
5201 }
5202
5203 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
5204 if (err)
5205 return err;
5206
5207 switch (sksec->sclass) {
5208 case SECCLASS_TCP_SOCKET:
5209 perm = TCP_SOCKET__NAME_CONNECT;
5210 break;
5211 case SECCLASS_SCTP_SOCKET:
5212 perm = SCTP_SOCKET__NAME_CONNECT;
5213 break;
5214 }
5215
5216 ad.type = LSM_AUDIT_DATA_NET;
5217 ad.u.net = &net;
5218 ad.u.net->dport = htons(snum);
5219 ad.u.net->family = address->sa_family;
5220 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
5221 if (err)
5222 return err;
5223 }
5224
5225 return 0;
5226 }
5227
5228 /* Supports connect(2), see comments in selinux_socket_connect_helper() */
selinux_socket_connect(struct socket * sock,struct sockaddr * address,int addrlen)5229 static int selinux_socket_connect(struct socket *sock,
5230 struct sockaddr *address, int addrlen)
5231 {
5232 int err;
5233 struct sock *sk = sock->sk;
5234
5235 err = selinux_socket_connect_helper(sk, address, addrlen);
5236 if (err)
5237 return err;
5238
5239 return selinux_netlbl_socket_connect(sk, address);
5240 }
5241
selinux_socket_listen(struct socket * sock,int backlog)5242 static int selinux_socket_listen(struct socket *sock, int backlog)
5243 {
5244 return sock_has_perm(sock->sk, SOCKET__LISTEN);
5245 }
5246
selinux_socket_accept(struct socket * sock,struct socket * newsock)5247 static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
5248 {
5249 int err;
5250 struct inode_security_struct *isec;
5251 struct inode_security_struct *newisec;
5252 u16 sclass;
5253 u32 sid;
5254
5255 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
5256 if (err)
5257 return err;
5258
5259 isec = inode_security_novalidate(SOCK_INODE(sock));
5260 spin_lock(&isec->lock);
5261 sclass = isec->sclass;
5262 sid = isec->sid;
5263 spin_unlock(&isec->lock);
5264
5265 newisec = inode_security_novalidate(SOCK_INODE(newsock));
5266 newisec->sclass = sclass;
5267 newisec->sid = sid;
5268 newisec->initialized = LABEL_INITIALIZED;
5269
5270 return 0;
5271 }
5272
selinux_socket_sendmsg(struct socket * sock,struct msghdr * msg,int size)5273 static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
5274 int size)
5275 {
5276 int rc;
5277 struct sockaddr *const addr = msg->msg_name;
5278 const int addrlen = msg->msg_namelen;
5279
5280 rc = sock_has_perm(sock->sk, SOCKET__WRITE);
5281 if (rc)
5282 return rc;
5283
5284 if (addr && (msg->msg_flags & MSG_FASTOPEN) &&
5285 (sk_is_tcp(sock->sk) ||
5286 (sk_is_inet(sock->sk) && sock->sk->sk_type == SOCK_STREAM &&
5287 sock->sk->sk_protocol == IPPROTO_MPTCP))) {
5288 rc = selinux_socket_connect(sock, addr, addrlen);
5289 if (rc)
5290 return rc;
5291 }
5292
5293 return 0;
5294 }
5295
selinux_socket_recvmsg(struct socket * sock,struct msghdr * msg,int size,int flags)5296 static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
5297 int size, int flags)
5298 {
5299 return sock_has_perm(sock->sk, SOCKET__READ);
5300 }
5301
selinux_socket_getsockname(struct socket * sock)5302 static int selinux_socket_getsockname(struct socket *sock)
5303 {
5304 return sock_has_perm(sock->sk, SOCKET__GETATTR);
5305 }
5306
selinux_socket_getpeername(struct socket * sock)5307 static int selinux_socket_getpeername(struct socket *sock)
5308 {
5309 return sock_has_perm(sock->sk, SOCKET__GETATTR);
5310 }
5311
selinux_socket_setsockopt(struct socket * sock,int level,int optname)5312 static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
5313 {
5314 int err;
5315
5316 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
5317 if (err)
5318 return err;
5319
5320 return selinux_netlbl_socket_setsockopt(sock, level, optname);
5321 }
5322
selinux_socket_getsockopt(struct socket * sock,int level,int optname)5323 static int selinux_socket_getsockopt(struct socket *sock, int level,
5324 int optname)
5325 {
5326 return sock_has_perm(sock->sk, SOCKET__GETOPT);
5327 }
5328
selinux_socket_shutdown(struct socket * sock,int how)5329 static int selinux_socket_shutdown(struct socket *sock, int how)
5330 {
5331 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
5332 }
5333
selinux_socket_unix_stream_connect(struct sock * sock,struct sock * other,struct sock * newsk)5334 static int selinux_socket_unix_stream_connect(struct sock *sock,
5335 struct sock *other,
5336 struct sock *newsk)
5337 {
5338 struct sk_security_struct *sksec_sock = selinux_sock(sock);
5339 struct sk_security_struct *sksec_other = selinux_sock(other);
5340 struct sk_security_struct *sksec_new = selinux_sock(newsk);
5341 struct common_audit_data ad;
5342 struct lsm_network_audit net;
5343 int err;
5344
5345 ad_net_init_from_sk(&ad, &net, other);
5346
5347 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
5348 sksec_other->sclass,
5349 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
5350 if (err)
5351 return err;
5352
5353 /* server child socket */
5354 sksec_new->peer_sid = sksec_sock->sid;
5355 err = security_sid_mls_copy(sksec_other->sid,
5356 sksec_sock->sid, &sksec_new->sid);
5357 if (err)
5358 return err;
5359
5360 /* connecting socket */
5361 sksec_sock->peer_sid = sksec_new->sid;
5362
5363 return 0;
5364 }
5365
selinux_socket_unix_may_send(struct socket * sock,struct socket * other)5366 static int selinux_socket_unix_may_send(struct socket *sock,
5367 struct socket *other)
5368 {
5369 struct sk_security_struct *ssec = selinux_sock(sock->sk);
5370 struct sk_security_struct *osec = selinux_sock(other->sk);
5371 struct common_audit_data ad;
5372 struct lsm_network_audit net;
5373
5374 ad_net_init_from_sk(&ad, &net, other->sk);
5375
5376 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
5377 &ad);
5378 }
5379
selinux_inet_sys_rcv_skb(struct net * ns,int ifindex,char * addrp,u16 family,u32 peer_sid,struct common_audit_data * ad)5380 static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
5381 char *addrp, u16 family, u32 peer_sid,
5382 struct common_audit_data *ad)
5383 {
5384 int err;
5385 u32 if_sid;
5386 u32 node_sid;
5387
5388 err = sel_netif_sid(ns, ifindex, &if_sid);
5389 if (err)
5390 return err;
5391 err = avc_has_perm(peer_sid, if_sid,
5392 SECCLASS_NETIF, NETIF__INGRESS, ad);
5393 if (err)
5394 return err;
5395
5396 err = sel_netnode_sid(addrp, family, &node_sid);
5397 if (err)
5398 return err;
5399 return avc_has_perm(peer_sid, node_sid,
5400 SECCLASS_NODE, NODE__RECVFROM, ad);
5401 }
5402
selinux_sock_rcv_skb_compat(struct sock * sk,struct sk_buff * skb,u16 family)5403 static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
5404 u16 family)
5405 {
5406 int err = 0;
5407 struct sk_security_struct *sksec = selinux_sock(sk);
5408 u32 sk_sid = sksec->sid;
5409 struct common_audit_data ad;
5410 struct lsm_network_audit net;
5411 char *addrp;
5412
5413 ad_net_init_from_iif(&ad, &net, skb->skb_iif, family);
5414 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
5415 if (err)
5416 return err;
5417
5418 if (selinux_secmark_enabled()) {
5419 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
5420 PACKET__RECV, &ad);
5421 if (err)
5422 return err;
5423 }
5424
5425 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
5426 if (err)
5427 return err;
5428 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
5429
5430 return err;
5431 }
5432
selinux_socket_sock_rcv_skb(struct sock * sk,struct sk_buff * skb)5433 static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
5434 {
5435 int err, peerlbl_active, secmark_active;
5436 struct sk_security_struct *sksec = selinux_sock(sk);
5437 u16 family = sk->sk_family;
5438 u32 sk_sid = sksec->sid;
5439 struct common_audit_data ad;
5440 struct lsm_network_audit net;
5441 char *addrp;
5442
5443 if (family != PF_INET && family != PF_INET6)
5444 return 0;
5445
5446 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
5447 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5448 family = PF_INET;
5449
5450 /* If any sort of compatibility mode is enabled then handoff processing
5451 * to the selinux_sock_rcv_skb_compat() function to deal with the
5452 * special handling. We do this in an attempt to keep this function
5453 * as fast and as clean as possible. */
5454 if (!selinux_policycap_netpeer())
5455 return selinux_sock_rcv_skb_compat(sk, skb, family);
5456
5457 secmark_active = selinux_secmark_enabled();
5458 peerlbl_active = selinux_peerlbl_enabled();
5459 if (!secmark_active && !peerlbl_active)
5460 return 0;
5461
5462 ad_net_init_from_iif(&ad, &net, skb->skb_iif, family);
5463 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
5464 if (err)
5465 return err;
5466
5467 if (peerlbl_active) {
5468 u32 peer_sid;
5469
5470 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
5471 if (err)
5472 return err;
5473 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
5474 addrp, family, peer_sid, &ad);
5475 if (err) {
5476 selinux_netlbl_err(skb, family, err, 0);
5477 return err;
5478 }
5479 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
5480 PEER__RECV, &ad);
5481 if (err) {
5482 selinux_netlbl_err(skb, family, err, 0);
5483 return err;
5484 }
5485 }
5486
5487 if (secmark_active) {
5488 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
5489 PACKET__RECV, &ad);
5490 if (err)
5491 return err;
5492 }
5493
5494 return err;
5495 }
5496
selinux_socket_getpeersec_stream(struct socket * sock,sockptr_t optval,sockptr_t optlen,unsigned int len)5497 static int selinux_socket_getpeersec_stream(struct socket *sock,
5498 sockptr_t optval, sockptr_t optlen,
5499 unsigned int len)
5500 {
5501 int err = 0;
5502 char *scontext = NULL;
5503 u32 scontext_len;
5504 struct sk_security_struct *sksec = selinux_sock(sock->sk);
5505 u32 peer_sid = SECSID_NULL;
5506
5507 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
5508 sksec->sclass == SECCLASS_TCP_SOCKET ||
5509 sksec->sclass == SECCLASS_SCTP_SOCKET)
5510 peer_sid = sksec->peer_sid;
5511 if (peer_sid == SECSID_NULL)
5512 return -ENOPROTOOPT;
5513
5514 err = security_sid_to_context(peer_sid, &scontext,
5515 &scontext_len);
5516 if (err)
5517 return err;
5518 if (scontext_len > len) {
5519 err = -ERANGE;
5520 goto out_len;
5521 }
5522
5523 if (copy_to_sockptr(optval, scontext, scontext_len))
5524 err = -EFAULT;
5525 out_len:
5526 if (copy_to_sockptr(optlen, &scontext_len, sizeof(scontext_len)))
5527 err = -EFAULT;
5528 kfree(scontext);
5529 return err;
5530 }
5531
selinux_socket_getpeersec_dgram(struct socket * sock,struct sk_buff * skb,u32 * secid)5532 static int selinux_socket_getpeersec_dgram(struct socket *sock,
5533 struct sk_buff *skb, u32 *secid)
5534 {
5535 u32 peer_secid = SECSID_NULL;
5536 u16 family;
5537
5538 if (skb && skb->protocol == htons(ETH_P_IP))
5539 family = PF_INET;
5540 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5541 family = PF_INET6;
5542 else if (sock)
5543 family = sock->sk->sk_family;
5544 else {
5545 *secid = SECSID_NULL;
5546 return -EINVAL;
5547 }
5548
5549 if (sock && family == PF_UNIX) {
5550 struct inode_security_struct *isec;
5551 isec = inode_security_novalidate(SOCK_INODE(sock));
5552 peer_secid = isec->sid;
5553 } else if (skb)
5554 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
5555
5556 *secid = peer_secid;
5557 if (peer_secid == SECSID_NULL)
5558 return -ENOPROTOOPT;
5559 return 0;
5560 }
5561
selinux_sk_alloc_security(struct sock * sk,int family,gfp_t priority)5562 static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
5563 {
5564 struct sk_security_struct *sksec = selinux_sock(sk);
5565
5566 sksec->peer_sid = SECINITSID_UNLABELED;
5567 sksec->sid = SECINITSID_UNLABELED;
5568 sksec->sclass = SECCLASS_SOCKET;
5569 selinux_netlbl_sk_security_reset(sksec);
5570
5571 return 0;
5572 }
5573
selinux_sk_free_security(struct sock * sk)5574 static void selinux_sk_free_security(struct sock *sk)
5575 {
5576 struct sk_security_struct *sksec = selinux_sock(sk);
5577
5578 selinux_netlbl_sk_security_free(sksec);
5579 }
5580
selinux_sk_clone_security(const struct sock * sk,struct sock * newsk)5581 static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5582 {
5583 struct sk_security_struct *sksec = selinux_sock(sk);
5584 struct sk_security_struct *newsksec = selinux_sock(newsk);
5585
5586 newsksec->sid = sksec->sid;
5587 newsksec->peer_sid = sksec->peer_sid;
5588 newsksec->sclass = sksec->sclass;
5589
5590 selinux_netlbl_sk_security_reset(newsksec);
5591 }
5592
selinux_sk_getsecid(const struct sock * sk,u32 * secid)5593 static void selinux_sk_getsecid(const struct sock *sk, u32 *secid)
5594 {
5595 if (!sk)
5596 *secid = SECINITSID_ANY_SOCKET;
5597 else {
5598 const struct sk_security_struct *sksec = selinux_sock(sk);
5599
5600 *secid = sksec->sid;
5601 }
5602 }
5603
selinux_sock_graft(struct sock * sk,struct socket * parent)5604 static void selinux_sock_graft(struct sock *sk, struct socket *parent)
5605 {
5606 struct inode_security_struct *isec =
5607 inode_security_novalidate(SOCK_INODE(parent));
5608 struct sk_security_struct *sksec = selinux_sock(sk);
5609
5610 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5611 sk->sk_family == PF_UNIX)
5612 isec->sid = sksec->sid;
5613 sksec->sclass = isec->sclass;
5614 }
5615
5616 /*
5617 * Determines peer_secid for the asoc and updates socket's peer label
5618 * if it's the first association on the socket.
5619 */
selinux_sctp_process_new_assoc(struct sctp_association * asoc,struct sk_buff * skb)5620 static int selinux_sctp_process_new_assoc(struct sctp_association *asoc,
5621 struct sk_buff *skb)
5622 {
5623 struct sock *sk = asoc->base.sk;
5624 u16 family = sk->sk_family;
5625 struct sk_security_struct *sksec = selinux_sock(sk);
5626 struct common_audit_data ad;
5627 struct lsm_network_audit net;
5628 int err;
5629
5630 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5631 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5632 family = PF_INET;
5633
5634 if (selinux_peerlbl_enabled()) {
5635 asoc->peer_secid = SECSID_NULL;
5636
5637 /* This will return peer_sid = SECSID_NULL if there are
5638 * no peer labels, see security_net_peersid_resolve().
5639 */
5640 err = selinux_skb_peerlbl_sid(skb, family, &asoc->peer_secid);
5641 if (err)
5642 return err;
5643
5644 if (asoc->peer_secid == SECSID_NULL)
5645 asoc->peer_secid = SECINITSID_UNLABELED;
5646 } else {
5647 asoc->peer_secid = SECINITSID_UNLABELED;
5648 }
5649
5650 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5651 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5652
5653 /* Here as first association on socket. As the peer SID
5654 * was allowed by peer recv (and the netif/node checks),
5655 * then it is approved by policy and used as the primary
5656 * peer SID for getpeercon(3).
5657 */
5658 sksec->peer_sid = asoc->peer_secid;
5659 } else if (sksec->peer_sid != asoc->peer_secid) {
5660 /* Other association peer SIDs are checked to enforce
5661 * consistency among the peer SIDs.
5662 */
5663 ad_net_init_from_sk(&ad, &net, asoc->base.sk);
5664 err = avc_has_perm(sksec->peer_sid, asoc->peer_secid,
5665 sksec->sclass, SCTP_SOCKET__ASSOCIATION,
5666 &ad);
5667 if (err)
5668 return err;
5669 }
5670 return 0;
5671 }
5672
5673 /* Called whenever SCTP receives an INIT or COOKIE ECHO chunk. This
5674 * happens on an incoming connect(2), sctp_connectx(3) or
5675 * sctp_sendmsg(3) (with no association already present).
5676 */
selinux_sctp_assoc_request(struct sctp_association * asoc,struct sk_buff * skb)5677 static int selinux_sctp_assoc_request(struct sctp_association *asoc,
5678 struct sk_buff *skb)
5679 {
5680 struct sk_security_struct *sksec = selinux_sock(asoc->base.sk);
5681 u32 conn_sid;
5682 int err;
5683
5684 if (!selinux_policycap_extsockclass())
5685 return 0;
5686
5687 err = selinux_sctp_process_new_assoc(asoc, skb);
5688 if (err)
5689 return err;
5690
5691 /* Compute the MLS component for the connection and store
5692 * the information in asoc. This will be used by SCTP TCP type
5693 * sockets and peeled off connections as they cause a new
5694 * socket to be generated. selinux_sctp_sk_clone() will then
5695 * plug this into the new socket.
5696 */
5697 err = selinux_conn_sid(sksec->sid, asoc->peer_secid, &conn_sid);
5698 if (err)
5699 return err;
5700
5701 asoc->secid = conn_sid;
5702
5703 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5704 return selinux_netlbl_sctp_assoc_request(asoc, skb);
5705 }
5706
5707 /* Called when SCTP receives a COOKIE ACK chunk as the final
5708 * response to an association request (initited by us).
5709 */
selinux_sctp_assoc_established(struct sctp_association * asoc,struct sk_buff * skb)5710 static int selinux_sctp_assoc_established(struct sctp_association *asoc,
5711 struct sk_buff *skb)
5712 {
5713 struct sk_security_struct *sksec = selinux_sock(asoc->base.sk);
5714
5715 if (!selinux_policycap_extsockclass())
5716 return 0;
5717
5718 /* Inherit secid from the parent socket - this will be picked up
5719 * by selinux_sctp_sk_clone() if the association gets peeled off
5720 * into a new socket.
5721 */
5722 asoc->secid = sksec->sid;
5723
5724 return selinux_sctp_process_new_assoc(asoc, skb);
5725 }
5726
5727 /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5728 * based on their @optname.
5729 */
selinux_sctp_bind_connect(struct sock * sk,int optname,struct sockaddr * address,int addrlen)5730 static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5731 struct sockaddr *address,
5732 int addrlen)
5733 {
5734 int len, err = 0, walk_size = 0;
5735 void *addr_buf;
5736 struct sockaddr *addr;
5737
5738 if (!selinux_policycap_extsockclass())
5739 return 0;
5740
5741 /* Process one or more addresses that may be IPv4 or IPv6 */
5742 addr_buf = address;
5743
5744 while (walk_size < addrlen) {
5745 if (walk_size + sizeof(sa_family_t) > addrlen)
5746 return -EINVAL;
5747
5748 addr = addr_buf;
5749 switch (addr->sa_family) {
5750 case AF_UNSPEC:
5751 case AF_INET:
5752 len = sizeof(struct sockaddr_in);
5753 break;
5754 case AF_INET6:
5755 len = sizeof(struct sockaddr_in6);
5756 break;
5757 default:
5758 return -EINVAL;
5759 }
5760
5761 if (walk_size + len > addrlen)
5762 return -EINVAL;
5763
5764 err = -EINVAL;
5765 switch (optname) {
5766 /* Bind checks */
5767 case SCTP_PRIMARY_ADDR:
5768 case SCTP_SET_PEER_PRIMARY_ADDR:
5769 case SCTP_SOCKOPT_BINDX_ADD:
5770 err = __selinux_socket_bind(sk, addr, len);
5771 break;
5772 /* Connect checks */
5773 case SCTP_SOCKOPT_CONNECTX:
5774 case SCTP_PARAM_SET_PRIMARY:
5775 case SCTP_PARAM_ADD_IP:
5776 case SCTP_SENDMSG_CONNECT:
5777 err = selinux_socket_connect_helper(sk, addr, len);
5778 if (err)
5779 return err;
5780
5781 /* As selinux_sctp_bind_connect() is called by the
5782 * SCTP protocol layer, the socket is already locked,
5783 * therefore selinux_netlbl_socket_connect_locked()
5784 * is called here. The situations handled are:
5785 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5786 * whenever a new IP address is added or when a new
5787 * primary address is selected.
5788 * Note that an SCTP connect(2) call happens before
5789 * the SCTP protocol layer and is handled via
5790 * selinux_socket_connect().
5791 */
5792 err = selinux_netlbl_socket_connect_locked(sk, addr);
5793 break;
5794 }
5795
5796 if (err)
5797 return err;
5798
5799 addr_buf += len;
5800 walk_size += len;
5801 }
5802
5803 return 0;
5804 }
5805
5806 /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
selinux_sctp_sk_clone(struct sctp_association * asoc,struct sock * sk,struct sock * newsk)5807 static void selinux_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
5808 struct sock *newsk)
5809 {
5810 struct sk_security_struct *sksec = selinux_sock(sk);
5811 struct sk_security_struct *newsksec = selinux_sock(newsk);
5812
5813 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5814 * the non-sctp clone version.
5815 */
5816 if (!selinux_policycap_extsockclass())
5817 return selinux_sk_clone_security(sk, newsk);
5818
5819 newsksec->sid = asoc->secid;
5820 newsksec->peer_sid = asoc->peer_secid;
5821 newsksec->sclass = sksec->sclass;
5822 selinux_netlbl_sctp_sk_clone(sk, newsk);
5823 }
5824
selinux_mptcp_add_subflow(struct sock * sk,struct sock * ssk)5825 static int selinux_mptcp_add_subflow(struct sock *sk, struct sock *ssk)
5826 {
5827 struct sk_security_struct *ssksec = selinux_sock(ssk);
5828 struct sk_security_struct *sksec = selinux_sock(sk);
5829
5830 ssksec->sclass = sksec->sclass;
5831 ssksec->sid = sksec->sid;
5832
5833 /* replace the existing subflow label deleting the existing one
5834 * and re-recreating a new label using the updated context
5835 */
5836 selinux_netlbl_sk_security_free(ssksec);
5837 return selinux_netlbl_socket_post_create(ssk, ssk->sk_family);
5838 }
5839
selinux_inet_conn_request(const struct sock * sk,struct sk_buff * skb,struct request_sock * req)5840 static int selinux_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
5841 struct request_sock *req)
5842 {
5843 struct sk_security_struct *sksec = selinux_sock(sk);
5844 int err;
5845 u16 family = req->rsk_ops->family;
5846 u32 connsid;
5847 u32 peersid;
5848
5849 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
5850 if (err)
5851 return err;
5852 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5853 if (err)
5854 return err;
5855 req->secid = connsid;
5856 req->peer_secid = peersid;
5857
5858 return selinux_netlbl_inet_conn_request(req, family);
5859 }
5860
selinux_inet_csk_clone(struct sock * newsk,const struct request_sock * req)5861 static void selinux_inet_csk_clone(struct sock *newsk,
5862 const struct request_sock *req)
5863 {
5864 struct sk_security_struct *newsksec = selinux_sock(newsk);
5865
5866 newsksec->sid = req->secid;
5867 newsksec->peer_sid = req->peer_secid;
5868 /* NOTE: Ideally, we should also get the isec->sid for the
5869 new socket in sync, but we don't have the isec available yet.
5870 So we will wait until sock_graft to do it, by which
5871 time it will have been created and available. */
5872
5873 /* We don't need to take any sort of lock here as we are the only
5874 * thread with access to newsksec */
5875 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
5876 }
5877
selinux_inet_conn_established(struct sock * sk,struct sk_buff * skb)5878 static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
5879 {
5880 u16 family = sk->sk_family;
5881 struct sk_security_struct *sksec = selinux_sock(sk);
5882
5883 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5884 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5885 family = PF_INET;
5886
5887 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
5888 }
5889
selinux_secmark_relabel_packet(u32 sid)5890 static int selinux_secmark_relabel_packet(u32 sid)
5891 {
5892 return avc_has_perm(current_sid(), sid, SECCLASS_PACKET, PACKET__RELABELTO,
5893 NULL);
5894 }
5895
selinux_secmark_refcount_inc(void)5896 static void selinux_secmark_refcount_inc(void)
5897 {
5898 atomic_inc(&selinux_secmark_refcount);
5899 }
5900
selinux_secmark_refcount_dec(void)5901 static void selinux_secmark_refcount_dec(void)
5902 {
5903 atomic_dec(&selinux_secmark_refcount);
5904 }
5905
selinux_req_classify_flow(const struct request_sock * req,struct flowi_common * flic)5906 static void selinux_req_classify_flow(const struct request_sock *req,
5907 struct flowi_common *flic)
5908 {
5909 flic->flowic_secid = req->secid;
5910 }
5911
selinux_tun_dev_alloc_security(void * security)5912 static int selinux_tun_dev_alloc_security(void *security)
5913 {
5914 struct tun_security_struct *tunsec = selinux_tun_dev(security);
5915
5916 tunsec->sid = current_sid();
5917 return 0;
5918 }
5919
selinux_tun_dev_create(void)5920 static int selinux_tun_dev_create(void)
5921 {
5922 u32 sid = current_sid();
5923
5924 /* we aren't taking into account the "sockcreate" SID since the socket
5925 * that is being created here is not a socket in the traditional sense,
5926 * instead it is a private sock, accessible only to the kernel, and
5927 * representing a wide range of network traffic spanning multiple
5928 * connections unlike traditional sockets - check the TUN driver to
5929 * get a better understanding of why this socket is special */
5930
5931 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
5932 NULL);
5933 }
5934
selinux_tun_dev_attach_queue(void * security)5935 static int selinux_tun_dev_attach_queue(void *security)
5936 {
5937 struct tun_security_struct *tunsec = selinux_tun_dev(security);
5938
5939 return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
5940 TUN_SOCKET__ATTACH_QUEUE, NULL);
5941 }
5942
selinux_tun_dev_attach(struct sock * sk,void * security)5943 static int selinux_tun_dev_attach(struct sock *sk, void *security)
5944 {
5945 struct tun_security_struct *tunsec = selinux_tun_dev(security);
5946 struct sk_security_struct *sksec = selinux_sock(sk);
5947
5948 /* we don't currently perform any NetLabel based labeling here and it
5949 * isn't clear that we would want to do so anyway; while we could apply
5950 * labeling without the support of the TUN user the resulting labeled
5951 * traffic from the other end of the connection would almost certainly
5952 * cause confusion to the TUN user that had no idea network labeling
5953 * protocols were being used */
5954
5955 sksec->sid = tunsec->sid;
5956 sksec->sclass = SECCLASS_TUN_SOCKET;
5957
5958 return 0;
5959 }
5960
selinux_tun_dev_open(void * security)5961 static int selinux_tun_dev_open(void *security)
5962 {
5963 struct tun_security_struct *tunsec = selinux_tun_dev(security);
5964 u32 sid = current_sid();
5965 int err;
5966
5967 err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
5968 TUN_SOCKET__RELABELFROM, NULL);
5969 if (err)
5970 return err;
5971 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
5972 TUN_SOCKET__RELABELTO, NULL);
5973 if (err)
5974 return err;
5975 tunsec->sid = sid;
5976
5977 return 0;
5978 }
5979
5980 #ifdef CONFIG_NETFILTER
5981
selinux_ip_forward(void * priv,struct sk_buff * skb,const struct nf_hook_state * state)5982 static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb,
5983 const struct nf_hook_state *state)
5984 {
5985 int ifindex;
5986 u16 family;
5987 char *addrp;
5988 u32 peer_sid;
5989 struct common_audit_data ad;
5990 struct lsm_network_audit net;
5991 int secmark_active, peerlbl_active;
5992
5993 if (!selinux_policycap_netpeer())
5994 return NF_ACCEPT;
5995
5996 secmark_active = selinux_secmark_enabled();
5997 peerlbl_active = selinux_peerlbl_enabled();
5998 if (!secmark_active && !peerlbl_active)
5999 return NF_ACCEPT;
6000
6001 family = state->pf;
6002 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
6003 return NF_DROP;
6004
6005 ifindex = state->in->ifindex;
6006 ad_net_init_from_iif(&ad, &net, ifindex, family);
6007 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
6008 return NF_DROP;
6009
6010 if (peerlbl_active) {
6011 int err;
6012
6013 err = selinux_inet_sys_rcv_skb(state->net, ifindex,
6014 addrp, family, peer_sid, &ad);
6015 if (err) {
6016 selinux_netlbl_err(skb, family, err, 1);
6017 return NF_DROP;
6018 }
6019 }
6020
6021 if (secmark_active)
6022 if (avc_has_perm(peer_sid, skb->secmark,
6023 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
6024 return NF_DROP;
6025
6026 if (netlbl_enabled())
6027 /* we do this in the FORWARD path and not the POST_ROUTING
6028 * path because we want to make sure we apply the necessary
6029 * labeling before IPsec is applied so we can leverage AH
6030 * protection */
6031 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
6032 return NF_DROP;
6033
6034 return NF_ACCEPT;
6035 }
6036
selinux_ip_output(void * priv,struct sk_buff * skb,const struct nf_hook_state * state)6037 static unsigned int selinux_ip_output(void *priv, struct sk_buff *skb,
6038 const struct nf_hook_state *state)
6039 {
6040 struct sock *sk;
6041 u32 sid;
6042
6043 if (!netlbl_enabled())
6044 return NF_ACCEPT;
6045
6046 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
6047 * because we want to make sure we apply the necessary labeling
6048 * before IPsec is applied so we can leverage AH protection */
6049 sk = skb_to_full_sk(skb);
6050 if (sk) {
6051 struct sk_security_struct *sksec;
6052
6053 if (sk_listener(sk))
6054 /* if the socket is the listening state then this
6055 * packet is a SYN-ACK packet which means it needs to
6056 * be labeled based on the connection/request_sock and
6057 * not the parent socket. unfortunately, we can't
6058 * lookup the request_sock yet as it isn't queued on
6059 * the parent socket until after the SYN-ACK is sent.
6060 * the "solution" is to simply pass the packet as-is
6061 * as any IP option based labeling should be copied
6062 * from the initial connection request (in the IP
6063 * layer). it is far from ideal, but until we get a
6064 * security label in the packet itself this is the
6065 * best we can do. */
6066 return NF_ACCEPT;
6067
6068 /* standard practice, label using the parent socket */
6069 sksec = selinux_sock(sk);
6070 sid = sksec->sid;
6071 } else
6072 sid = SECINITSID_KERNEL;
6073 if (selinux_netlbl_skbuff_setsid(skb, state->pf, sid) != 0)
6074 return NF_DROP;
6075
6076 return NF_ACCEPT;
6077 }
6078
6079
selinux_ip_postroute_compat(struct sk_buff * skb,const struct nf_hook_state * state)6080 static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
6081 const struct nf_hook_state *state)
6082 {
6083 struct sock *sk;
6084 struct sk_security_struct *sksec;
6085 struct common_audit_data ad;
6086 struct lsm_network_audit net;
6087 u8 proto = 0;
6088
6089 sk = skb_to_full_sk(skb);
6090 if (sk == NULL)
6091 return NF_ACCEPT;
6092 sksec = selinux_sock(sk);
6093
6094 ad_net_init_from_iif(&ad, &net, state->out->ifindex, state->pf);
6095 if (selinux_parse_skb(skb, &ad, NULL, 0, &proto))
6096 return NF_DROP;
6097
6098 if (selinux_secmark_enabled())
6099 if (avc_has_perm(sksec->sid, skb->secmark,
6100 SECCLASS_PACKET, PACKET__SEND, &ad))
6101 return NF_DROP_ERR(-ECONNREFUSED);
6102
6103 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
6104 return NF_DROP_ERR(-ECONNREFUSED);
6105
6106 return NF_ACCEPT;
6107 }
6108
selinux_ip_postroute(void * priv,struct sk_buff * skb,const struct nf_hook_state * state)6109 static unsigned int selinux_ip_postroute(void *priv,
6110 struct sk_buff *skb,
6111 const struct nf_hook_state *state)
6112 {
6113 u16 family;
6114 u32 secmark_perm;
6115 u32 peer_sid;
6116 int ifindex;
6117 struct sock *sk;
6118 struct common_audit_data ad;
6119 struct lsm_network_audit net;
6120 char *addrp;
6121 int secmark_active, peerlbl_active;
6122
6123 /* If any sort of compatibility mode is enabled then handoff processing
6124 * to the selinux_ip_postroute_compat() function to deal with the
6125 * special handling. We do this in an attempt to keep this function
6126 * as fast and as clean as possible. */
6127 if (!selinux_policycap_netpeer())
6128 return selinux_ip_postroute_compat(skb, state);
6129
6130 secmark_active = selinux_secmark_enabled();
6131 peerlbl_active = selinux_peerlbl_enabled();
6132 if (!secmark_active && !peerlbl_active)
6133 return NF_ACCEPT;
6134
6135 sk = skb_to_full_sk(skb);
6136
6137 #ifdef CONFIG_XFRM
6138 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
6139 * packet transformation so allow the packet to pass without any checks
6140 * since we'll have another chance to perform access control checks
6141 * when the packet is on it's final way out.
6142 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
6143 * is NULL, in this case go ahead and apply access control.
6144 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
6145 * TCP listening state we cannot wait until the XFRM processing
6146 * is done as we will miss out on the SA label if we do;
6147 * unfortunately, this means more work, but it is only once per
6148 * connection. */
6149 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
6150 !(sk && sk_listener(sk)))
6151 return NF_ACCEPT;
6152 #endif
6153
6154 family = state->pf;
6155 if (sk == NULL) {
6156 /* Without an associated socket the packet is either coming
6157 * from the kernel or it is being forwarded; check the packet
6158 * to determine which and if the packet is being forwarded
6159 * query the packet directly to determine the security label. */
6160 if (skb->skb_iif) {
6161 secmark_perm = PACKET__FORWARD_OUT;
6162 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
6163 return NF_DROP;
6164 } else {
6165 secmark_perm = PACKET__SEND;
6166 peer_sid = SECINITSID_KERNEL;
6167 }
6168 } else if (sk_listener(sk)) {
6169 /* Locally generated packet but the associated socket is in the
6170 * listening state which means this is a SYN-ACK packet. In
6171 * this particular case the correct security label is assigned
6172 * to the connection/request_sock but unfortunately we can't
6173 * query the request_sock as it isn't queued on the parent
6174 * socket until after the SYN-ACK packet is sent; the only
6175 * viable choice is to regenerate the label like we do in
6176 * selinux_inet_conn_request(). See also selinux_ip_output()
6177 * for similar problems. */
6178 u32 skb_sid;
6179 struct sk_security_struct *sksec;
6180
6181 sksec = selinux_sock(sk);
6182 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
6183 return NF_DROP;
6184 /* At this point, if the returned skb peerlbl is SECSID_NULL
6185 * and the packet has been through at least one XFRM
6186 * transformation then we must be dealing with the "final"
6187 * form of labeled IPsec packet; since we've already applied
6188 * all of our access controls on this packet we can safely
6189 * pass the packet. */
6190 if (skb_sid == SECSID_NULL) {
6191 switch (family) {
6192 case PF_INET:
6193 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
6194 return NF_ACCEPT;
6195 break;
6196 case PF_INET6:
6197 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
6198 return NF_ACCEPT;
6199 break;
6200 default:
6201 return NF_DROP_ERR(-ECONNREFUSED);
6202 }
6203 }
6204 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
6205 return NF_DROP;
6206 secmark_perm = PACKET__SEND;
6207 } else {
6208 /* Locally generated packet, fetch the security label from the
6209 * associated socket. */
6210 struct sk_security_struct *sksec = selinux_sock(sk);
6211 peer_sid = sksec->sid;
6212 secmark_perm = PACKET__SEND;
6213 }
6214
6215 ifindex = state->out->ifindex;
6216 ad_net_init_from_iif(&ad, &net, ifindex, family);
6217 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
6218 return NF_DROP;
6219
6220 if (secmark_active)
6221 if (avc_has_perm(peer_sid, skb->secmark,
6222 SECCLASS_PACKET, secmark_perm, &ad))
6223 return NF_DROP_ERR(-ECONNREFUSED);
6224
6225 if (peerlbl_active) {
6226 u32 if_sid;
6227 u32 node_sid;
6228
6229 if (sel_netif_sid(state->net, ifindex, &if_sid))
6230 return NF_DROP;
6231 if (avc_has_perm(peer_sid, if_sid,
6232 SECCLASS_NETIF, NETIF__EGRESS, &ad))
6233 return NF_DROP_ERR(-ECONNREFUSED);
6234
6235 if (sel_netnode_sid(addrp, family, &node_sid))
6236 return NF_DROP;
6237 if (avc_has_perm(peer_sid, node_sid,
6238 SECCLASS_NODE, NODE__SENDTO, &ad))
6239 return NF_DROP_ERR(-ECONNREFUSED);
6240 }
6241
6242 return NF_ACCEPT;
6243 }
6244 #endif /* CONFIG_NETFILTER */
6245
nlmsg_sock_has_extended_perms(struct sock * sk,u32 perms,u16 nlmsg_type)6246 static int nlmsg_sock_has_extended_perms(struct sock *sk, u32 perms, u16 nlmsg_type)
6247 {
6248 struct sk_security_struct *sksec = selinux_sock(sk);
6249 struct common_audit_data ad;
6250 u8 driver;
6251 u8 xperm;
6252
6253 if (sock_skip_has_perm(sksec->sid))
6254 return 0;
6255
6256 ad.type = LSM_AUDIT_DATA_NLMSGTYPE;
6257 ad.u.nlmsg_type = nlmsg_type;
6258
6259 driver = nlmsg_type >> 8;
6260 xperm = nlmsg_type & 0xff;
6261
6262 return avc_has_extended_perms(current_sid(), sksec->sid, sksec->sclass,
6263 perms, driver, AVC_EXT_NLMSG, xperm, &ad);
6264 }
6265
selinux_netlink_send(struct sock * sk,struct sk_buff * skb)6266 static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
6267 {
6268 int rc = 0;
6269 unsigned int msg_len;
6270 unsigned int data_len = skb->len;
6271 unsigned char *data = skb->data;
6272 struct nlmsghdr *nlh;
6273 struct sk_security_struct *sksec = selinux_sock(sk);
6274 u16 sclass = sksec->sclass;
6275 u32 perm;
6276
6277 while (data_len >= nlmsg_total_size(0)) {
6278 nlh = (struct nlmsghdr *)data;
6279
6280 /* NOTE: the nlmsg_len field isn't reliably set by some netlink
6281 * users which means we can't reject skb's with bogus
6282 * length fields; our solution is to follow what
6283 * netlink_rcv_skb() does and simply skip processing at
6284 * messages with length fields that are clearly junk
6285 */
6286 if (nlh->nlmsg_len < NLMSG_HDRLEN || nlh->nlmsg_len > data_len)
6287 return 0;
6288
6289 rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm);
6290 if (rc == 0) {
6291 if (selinux_policycap_netlink_xperm()) {
6292 rc = nlmsg_sock_has_extended_perms(
6293 sk, perm, nlh->nlmsg_type);
6294 } else {
6295 rc = sock_has_perm(sk, perm);
6296 }
6297 if (rc)
6298 return rc;
6299 } else if (rc == -EINVAL) {
6300 /* -EINVAL is a missing msg/perm mapping */
6301 if (sclass == SECCLASS_NETLINK_TCPDIAG_SOCKET &&
6302 nlh->nlmsg_type == DCCPDIAG_GETSOCK)
6303 pr_warn_once("SELinux: DCCP has been removed, pid=%d comm=%s\n",
6304 task_pid_nr(current), current->comm);
6305 else
6306 pr_warn_ratelimited("SELinux: unrecognized netlink"
6307 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
6308 " pid=%d comm=%s\n",
6309 sk->sk_protocol, nlh->nlmsg_type,
6310 secclass_map[sclass - 1].name,
6311 task_pid_nr(current), current->comm);
6312 if (enforcing_enabled() &&
6313 !security_get_allow_unknown())
6314 return rc;
6315 rc = 0;
6316 } else if (rc == -ENOENT) {
6317 /* -ENOENT is a missing socket/class mapping, ignore */
6318 rc = 0;
6319 } else {
6320 return rc;
6321 }
6322
6323 /* move to the next message after applying netlink padding */
6324 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
6325 if (msg_len >= data_len)
6326 return 0;
6327 data_len -= msg_len;
6328 data += msg_len;
6329 }
6330
6331 return rc;
6332 }
6333
ipc_init_security(struct ipc_security_struct * isec,u16 sclass)6334 static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
6335 {
6336 isec->sclass = sclass;
6337 isec->sid = current_sid();
6338 }
6339
ipc_has_perm(struct kern_ipc_perm * ipc_perms,u32 perms)6340 static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
6341 u32 perms)
6342 {
6343 struct ipc_security_struct *isec;
6344 struct common_audit_data ad;
6345 u32 sid = current_sid();
6346
6347 isec = selinux_ipc(ipc_perms);
6348
6349 ad.type = LSM_AUDIT_DATA_IPC;
6350 ad.u.ipc_id = ipc_perms->key;
6351
6352 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
6353 }
6354
selinux_msg_msg_alloc_security(struct msg_msg * msg)6355 static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
6356 {
6357 struct msg_security_struct *msec;
6358
6359 msec = selinux_msg_msg(msg);
6360 msec->sid = SECINITSID_UNLABELED;
6361
6362 return 0;
6363 }
6364
6365 /* message queue security operations */
selinux_msg_queue_alloc_security(struct kern_ipc_perm * msq)6366 static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
6367 {
6368 struct ipc_security_struct *isec;
6369 struct common_audit_data ad;
6370 u32 sid = current_sid();
6371
6372 isec = selinux_ipc(msq);
6373 ipc_init_security(isec, SECCLASS_MSGQ);
6374
6375 ad.type = LSM_AUDIT_DATA_IPC;
6376 ad.u.ipc_id = msq->key;
6377
6378 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
6379 MSGQ__CREATE, &ad);
6380 }
6381
selinux_msg_queue_associate(struct kern_ipc_perm * msq,int msqflg)6382 static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
6383 {
6384 struct ipc_security_struct *isec;
6385 struct common_audit_data ad;
6386 u32 sid = current_sid();
6387
6388 isec = selinux_ipc(msq);
6389
6390 ad.type = LSM_AUDIT_DATA_IPC;
6391 ad.u.ipc_id = msq->key;
6392
6393 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
6394 MSGQ__ASSOCIATE, &ad);
6395 }
6396
selinux_msg_queue_msgctl(struct kern_ipc_perm * msq,int cmd)6397 static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
6398 {
6399 u32 perms;
6400
6401 switch (cmd) {
6402 case IPC_INFO:
6403 case MSG_INFO:
6404 /* No specific object, just general system-wide information. */
6405 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
6406 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
6407 case IPC_STAT:
6408 case MSG_STAT:
6409 case MSG_STAT_ANY:
6410 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
6411 break;
6412 case IPC_SET:
6413 perms = MSGQ__SETATTR;
6414 break;
6415 case IPC_RMID:
6416 perms = MSGQ__DESTROY;
6417 break;
6418 default:
6419 return 0;
6420 }
6421
6422 return ipc_has_perm(msq, perms);
6423 }
6424
selinux_msg_queue_msgsnd(struct kern_ipc_perm * msq,struct msg_msg * msg,int msqflg)6425 static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
6426 {
6427 struct ipc_security_struct *isec;
6428 struct msg_security_struct *msec;
6429 struct common_audit_data ad;
6430 u32 sid = current_sid();
6431 int rc;
6432
6433 isec = selinux_ipc(msq);
6434 msec = selinux_msg_msg(msg);
6435
6436 /*
6437 * First time through, need to assign label to the message
6438 */
6439 if (msec->sid == SECINITSID_UNLABELED) {
6440 /*
6441 * Compute new sid based on current process and
6442 * message queue this message will be stored in
6443 */
6444 rc = security_transition_sid(sid, isec->sid,
6445 SECCLASS_MSG, NULL, &msec->sid);
6446 if (rc)
6447 return rc;
6448 }
6449
6450 ad.type = LSM_AUDIT_DATA_IPC;
6451 ad.u.ipc_id = msq->key;
6452
6453 /* Can this process write to the queue? */
6454 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
6455 MSGQ__WRITE, &ad);
6456 if (!rc)
6457 /* Can this process send the message */
6458 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
6459 MSG__SEND, &ad);
6460 if (!rc)
6461 /* Can the message be put in the queue? */
6462 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
6463 MSGQ__ENQUEUE, &ad);
6464
6465 return rc;
6466 }
6467
selinux_msg_queue_msgrcv(struct kern_ipc_perm * msq,struct msg_msg * msg,struct task_struct * target,long type,int mode)6468 static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
6469 struct task_struct *target,
6470 long type, int mode)
6471 {
6472 struct ipc_security_struct *isec;
6473 struct msg_security_struct *msec;
6474 struct common_audit_data ad;
6475 u32 sid = task_sid_obj(target);
6476 int rc;
6477
6478 isec = selinux_ipc(msq);
6479 msec = selinux_msg_msg(msg);
6480
6481 ad.type = LSM_AUDIT_DATA_IPC;
6482 ad.u.ipc_id = msq->key;
6483
6484 rc = avc_has_perm(sid, isec->sid,
6485 SECCLASS_MSGQ, MSGQ__READ, &ad);
6486 if (!rc)
6487 rc = avc_has_perm(sid, msec->sid,
6488 SECCLASS_MSG, MSG__RECEIVE, &ad);
6489 return rc;
6490 }
6491
6492 /* Shared Memory security operations */
selinux_shm_alloc_security(struct kern_ipc_perm * shp)6493 static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
6494 {
6495 struct ipc_security_struct *isec;
6496 struct common_audit_data ad;
6497 u32 sid = current_sid();
6498
6499 isec = selinux_ipc(shp);
6500 ipc_init_security(isec, SECCLASS_SHM);
6501
6502 ad.type = LSM_AUDIT_DATA_IPC;
6503 ad.u.ipc_id = shp->key;
6504
6505 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
6506 SHM__CREATE, &ad);
6507 }
6508
selinux_shm_associate(struct kern_ipc_perm * shp,int shmflg)6509 static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
6510 {
6511 struct ipc_security_struct *isec;
6512 struct common_audit_data ad;
6513 u32 sid = current_sid();
6514
6515 isec = selinux_ipc(shp);
6516
6517 ad.type = LSM_AUDIT_DATA_IPC;
6518 ad.u.ipc_id = shp->key;
6519
6520 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
6521 SHM__ASSOCIATE, &ad);
6522 }
6523
6524 /* Note, at this point, shp is locked down */
selinux_shm_shmctl(struct kern_ipc_perm * shp,int cmd)6525 static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
6526 {
6527 u32 perms;
6528
6529 switch (cmd) {
6530 case IPC_INFO:
6531 case SHM_INFO:
6532 /* No specific object, just general system-wide information. */
6533 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
6534 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
6535 case IPC_STAT:
6536 case SHM_STAT:
6537 case SHM_STAT_ANY:
6538 perms = SHM__GETATTR | SHM__ASSOCIATE;
6539 break;
6540 case IPC_SET:
6541 perms = SHM__SETATTR;
6542 break;
6543 case SHM_LOCK:
6544 case SHM_UNLOCK:
6545 perms = SHM__LOCK;
6546 break;
6547 case IPC_RMID:
6548 perms = SHM__DESTROY;
6549 break;
6550 default:
6551 return 0;
6552 }
6553
6554 return ipc_has_perm(shp, perms);
6555 }
6556
selinux_shm_shmat(struct kern_ipc_perm * shp,char __user * shmaddr,int shmflg)6557 static int selinux_shm_shmat(struct kern_ipc_perm *shp,
6558 char __user *shmaddr, int shmflg)
6559 {
6560 u32 perms;
6561
6562 if (shmflg & SHM_RDONLY)
6563 perms = SHM__READ;
6564 else
6565 perms = SHM__READ | SHM__WRITE;
6566
6567 return ipc_has_perm(shp, perms);
6568 }
6569
6570 /* Semaphore security operations */
selinux_sem_alloc_security(struct kern_ipc_perm * sma)6571 static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
6572 {
6573 struct ipc_security_struct *isec;
6574 struct common_audit_data ad;
6575 u32 sid = current_sid();
6576
6577 isec = selinux_ipc(sma);
6578 ipc_init_security(isec, SECCLASS_SEM);
6579
6580 ad.type = LSM_AUDIT_DATA_IPC;
6581 ad.u.ipc_id = sma->key;
6582
6583 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
6584 SEM__CREATE, &ad);
6585 }
6586
selinux_sem_associate(struct kern_ipc_perm * sma,int semflg)6587 static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
6588 {
6589 struct ipc_security_struct *isec;
6590 struct common_audit_data ad;
6591 u32 sid = current_sid();
6592
6593 isec = selinux_ipc(sma);
6594
6595 ad.type = LSM_AUDIT_DATA_IPC;
6596 ad.u.ipc_id = sma->key;
6597
6598 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
6599 SEM__ASSOCIATE, &ad);
6600 }
6601
6602 /* Note, at this point, sma is locked down */
selinux_sem_semctl(struct kern_ipc_perm * sma,int cmd)6603 static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
6604 {
6605 int err;
6606 u32 perms;
6607
6608 switch (cmd) {
6609 case IPC_INFO:
6610 case SEM_INFO:
6611 /* No specific object, just general system-wide information. */
6612 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
6613 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
6614 case GETPID:
6615 case GETNCNT:
6616 case GETZCNT:
6617 perms = SEM__GETATTR;
6618 break;
6619 case GETVAL:
6620 case GETALL:
6621 perms = SEM__READ;
6622 break;
6623 case SETVAL:
6624 case SETALL:
6625 perms = SEM__WRITE;
6626 break;
6627 case IPC_RMID:
6628 perms = SEM__DESTROY;
6629 break;
6630 case IPC_SET:
6631 perms = SEM__SETATTR;
6632 break;
6633 case IPC_STAT:
6634 case SEM_STAT:
6635 case SEM_STAT_ANY:
6636 perms = SEM__GETATTR | SEM__ASSOCIATE;
6637 break;
6638 default:
6639 return 0;
6640 }
6641
6642 err = ipc_has_perm(sma, perms);
6643 return err;
6644 }
6645
selinux_sem_semop(struct kern_ipc_perm * sma,struct sembuf * sops,unsigned nsops,int alter)6646 static int selinux_sem_semop(struct kern_ipc_perm *sma,
6647 struct sembuf *sops, unsigned nsops, int alter)
6648 {
6649 u32 perms;
6650
6651 if (alter)
6652 perms = SEM__READ | SEM__WRITE;
6653 else
6654 perms = SEM__READ;
6655
6656 return ipc_has_perm(sma, perms);
6657 }
6658
selinux_ipc_permission(struct kern_ipc_perm * ipcp,short flag)6659 static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6660 {
6661 u32 av = 0;
6662
6663 av = 0;
6664 if (flag & S_IRUGO)
6665 av |= IPC__UNIX_READ;
6666 if (flag & S_IWUGO)
6667 av |= IPC__UNIX_WRITE;
6668
6669 if (av == 0)
6670 return 0;
6671
6672 return ipc_has_perm(ipcp, av);
6673 }
6674
selinux_ipc_getlsmprop(struct kern_ipc_perm * ipcp,struct lsm_prop * prop)6675 static void selinux_ipc_getlsmprop(struct kern_ipc_perm *ipcp,
6676 struct lsm_prop *prop)
6677 {
6678 struct ipc_security_struct *isec = selinux_ipc(ipcp);
6679 prop->selinux.secid = isec->sid;
6680 }
6681
selinux_d_instantiate(struct dentry * dentry,struct inode * inode)6682 static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
6683 {
6684 if (inode)
6685 inode_doinit_with_dentry(inode, dentry);
6686 }
6687
selinux_lsm_getattr(unsigned int attr,struct task_struct * p,char ** value)6688 static int selinux_lsm_getattr(unsigned int attr, struct task_struct *p,
6689 char **value)
6690 {
6691 const struct cred_security_struct *crsec;
6692 int error;
6693 u32 sid;
6694 u32 len;
6695
6696 rcu_read_lock();
6697 crsec = selinux_cred(__task_cred(p));
6698 if (p != current) {
6699 error = avc_has_perm(current_sid(), crsec->sid,
6700 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6701 if (error)
6702 goto err_unlock;
6703 }
6704 switch (attr) {
6705 case LSM_ATTR_CURRENT:
6706 sid = crsec->sid;
6707 break;
6708 case LSM_ATTR_PREV:
6709 sid = crsec->osid;
6710 break;
6711 case LSM_ATTR_EXEC:
6712 sid = crsec->exec_sid;
6713 break;
6714 case LSM_ATTR_FSCREATE:
6715 sid = crsec->create_sid;
6716 break;
6717 case LSM_ATTR_KEYCREATE:
6718 sid = crsec->keycreate_sid;
6719 break;
6720 case LSM_ATTR_SOCKCREATE:
6721 sid = crsec->sockcreate_sid;
6722 break;
6723 default:
6724 error = -EOPNOTSUPP;
6725 goto err_unlock;
6726 }
6727 rcu_read_unlock();
6728
6729 if (sid == SECSID_NULL) {
6730 *value = NULL;
6731 return 0;
6732 }
6733
6734 error = security_sid_to_context(sid, value, &len);
6735 if (error)
6736 return error;
6737 return len;
6738
6739 err_unlock:
6740 rcu_read_unlock();
6741 return error;
6742 }
6743
selinux_lsm_setattr(u64 attr,void * value,size_t size)6744 static int selinux_lsm_setattr(u64 attr, void *value, size_t size)
6745 {
6746 struct cred_security_struct *crsec;
6747 struct cred *new;
6748 u32 mysid = current_sid(), sid = 0, ptsid;
6749 int error;
6750 char *str = value;
6751
6752 /*
6753 * Basic control over ability to set these attributes at all.
6754 */
6755 switch (attr) {
6756 case LSM_ATTR_EXEC:
6757 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
6758 PROCESS__SETEXEC, NULL);
6759 break;
6760 case LSM_ATTR_FSCREATE:
6761 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
6762 PROCESS__SETFSCREATE, NULL);
6763 break;
6764 case LSM_ATTR_KEYCREATE:
6765 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
6766 PROCESS__SETKEYCREATE, NULL);
6767 break;
6768 case LSM_ATTR_SOCKCREATE:
6769 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
6770 PROCESS__SETSOCKCREATE, NULL);
6771 break;
6772 case LSM_ATTR_CURRENT:
6773 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
6774 PROCESS__SETCURRENT, NULL);
6775 break;
6776 default:
6777 error = -EOPNOTSUPP;
6778 break;
6779 }
6780 if (error)
6781 return error;
6782
6783 /* Obtain a SID for the context, if one was specified. */
6784 if (size && str[0] && str[0] != '\n') {
6785 if (str[size-1] == '\n') {
6786 str[size-1] = 0;
6787 size--;
6788 }
6789 error = security_context_to_sid(value, size,
6790 &sid, GFP_KERNEL);
6791 if (error == -EINVAL && attr == LSM_ATTR_FSCREATE) {
6792 if (!has_cap_mac_admin(true)) {
6793 struct audit_buffer *ab;
6794 size_t audit_size;
6795
6796 /* We strip a nul only if it is at the end,
6797 * otherwise the context contains a nul and
6798 * we should audit that */
6799 if (str[size - 1] == '\0')
6800 audit_size = size - 1;
6801 else
6802 audit_size = size;
6803 ab = audit_log_start(audit_context(),
6804 GFP_ATOMIC,
6805 AUDIT_SELINUX_ERR);
6806 if (!ab)
6807 return error;
6808 audit_log_format(ab, "op=fscreate invalid_context=");
6809 audit_log_n_untrustedstring(ab, value,
6810 audit_size);
6811 audit_log_end(ab);
6812
6813 return error;
6814 }
6815 error = security_context_to_sid_force(value, size,
6816 &sid);
6817 }
6818 if (error)
6819 return error;
6820 }
6821
6822 new = prepare_creds();
6823 if (!new)
6824 return -ENOMEM;
6825
6826 /* Permission checking based on the specified context is
6827 performed during the actual operation (execve,
6828 open/mkdir/...), when we know the full context of the
6829 operation. See selinux_bprm_creds_for_exec for the execve
6830 checks and may_create for the file creation checks. The
6831 operation will then fail if the context is not permitted. */
6832 crsec = selinux_cred(new);
6833 if (attr == LSM_ATTR_EXEC) {
6834 crsec->exec_sid = sid;
6835 } else if (attr == LSM_ATTR_FSCREATE) {
6836 crsec->create_sid = sid;
6837 } else if (attr == LSM_ATTR_KEYCREATE) {
6838 if (sid) {
6839 error = avc_has_perm(mysid, sid,
6840 SECCLASS_KEY, KEY__CREATE, NULL);
6841 if (error)
6842 goto abort_change;
6843 }
6844 crsec->keycreate_sid = sid;
6845 } else if (attr == LSM_ATTR_SOCKCREATE) {
6846 crsec->sockcreate_sid = sid;
6847 } else if (attr == LSM_ATTR_CURRENT) {
6848 error = -EINVAL;
6849 if (sid == 0)
6850 goto abort_change;
6851
6852 if (!current_is_single_threaded()) {
6853 error = security_bounded_transition(crsec->sid, sid);
6854 if (error)
6855 goto abort_change;
6856 }
6857
6858 /* Check permissions for the transition. */
6859 error = avc_has_perm(crsec->sid, sid, SECCLASS_PROCESS,
6860 PROCESS__DYNTRANSITION, NULL);
6861 if (error)
6862 goto abort_change;
6863
6864 /* Check for ptracing, and update the task SID if ok.
6865 Otherwise, leave SID unchanged and fail. */
6866 ptsid = ptrace_parent_sid();
6867 if (ptsid != 0) {
6868 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
6869 PROCESS__PTRACE, NULL);
6870 if (error)
6871 goto abort_change;
6872 }
6873
6874 crsec->sid = sid;
6875 } else {
6876 error = -EINVAL;
6877 goto abort_change;
6878 }
6879
6880 commit_creds(new);
6881 return size;
6882
6883 abort_change:
6884 abort_creds(new);
6885 return error;
6886 }
6887
6888 /**
6889 * selinux_getselfattr - Get SELinux current task attributes
6890 * @attr: the requested attribute
6891 * @ctx: buffer to receive the result
6892 * @size: buffer size (input), buffer size used (output)
6893 * @flags: unused
6894 *
6895 * Fill the passed user space @ctx with the details of the requested
6896 * attribute.
6897 *
6898 * Returns the number of attributes on success, an error code otherwise.
6899 * There will only ever be one attribute.
6900 */
selinux_getselfattr(unsigned int attr,struct lsm_ctx __user * ctx,u32 * size,u32 flags)6901 static int selinux_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx,
6902 u32 *size, u32 flags)
6903 {
6904 int rc;
6905 char *val = NULL;
6906 int val_len;
6907
6908 val_len = selinux_lsm_getattr(attr, current, &val);
6909 if (val_len < 0)
6910 return val_len;
6911 rc = lsm_fill_user_ctx(ctx, size, val, val_len, LSM_ID_SELINUX, 0);
6912 kfree(val);
6913 return (!rc ? 1 : rc);
6914 }
6915
selinux_setselfattr(unsigned int attr,struct lsm_ctx * ctx,u32 size,u32 flags)6916 static int selinux_setselfattr(unsigned int attr, struct lsm_ctx *ctx,
6917 u32 size, u32 flags)
6918 {
6919 int rc;
6920
6921 rc = selinux_lsm_setattr(attr, ctx->ctx, ctx->ctx_len);
6922 if (rc > 0)
6923 return 0;
6924 return rc;
6925 }
6926
selinux_getprocattr(struct task_struct * p,const char * name,char ** value)6927 static int selinux_getprocattr(struct task_struct *p,
6928 const char *name, char **value)
6929 {
6930 unsigned int attr = lsm_name_to_attr(name);
6931 int rc;
6932
6933 if (attr) {
6934 rc = selinux_lsm_getattr(attr, p, value);
6935 if (rc != -EOPNOTSUPP)
6936 return rc;
6937 }
6938
6939 return -EINVAL;
6940 }
6941
selinux_setprocattr(const char * name,void * value,size_t size)6942 static int selinux_setprocattr(const char *name, void *value, size_t size)
6943 {
6944 int attr = lsm_name_to_attr(name);
6945
6946 if (attr)
6947 return selinux_lsm_setattr(attr, value, size);
6948 return -EINVAL;
6949 }
6950
selinux_ismaclabel(const char * name)6951 static int selinux_ismaclabel(const char *name)
6952 {
6953 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6954 }
6955
selinux_secid_to_secctx(u32 secid,struct lsm_context * cp)6956 static int selinux_secid_to_secctx(u32 secid, struct lsm_context *cp)
6957 {
6958 u32 seclen;
6959 int ret;
6960
6961 if (cp) {
6962 cp->id = LSM_ID_SELINUX;
6963 ret = security_sid_to_context(secid, &cp->context, &cp->len);
6964 if (ret < 0)
6965 return ret;
6966 return cp->len;
6967 }
6968 ret = security_sid_to_context(secid, NULL, &seclen);
6969 if (ret < 0)
6970 return ret;
6971 return seclen;
6972 }
6973
selinux_lsmprop_to_secctx(struct lsm_prop * prop,struct lsm_context * cp)6974 static int selinux_lsmprop_to_secctx(struct lsm_prop *prop,
6975 struct lsm_context *cp)
6976 {
6977 return selinux_secid_to_secctx(prop->selinux.secid, cp);
6978 }
6979
selinux_secctx_to_secid(const char * secdata,u32 seclen,u32 * secid)6980 static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
6981 {
6982 return security_context_to_sid(secdata, seclen,
6983 secid, GFP_KERNEL);
6984 }
6985
selinux_release_secctx(struct lsm_context * cp)6986 static void selinux_release_secctx(struct lsm_context *cp)
6987 {
6988 if (cp->id == LSM_ID_SELINUX) {
6989 kfree(cp->context);
6990 cp->context = NULL;
6991 cp->id = LSM_ID_UNDEF;
6992 }
6993 }
6994
selinux_inode_invalidate_secctx(struct inode * inode)6995 static void selinux_inode_invalidate_secctx(struct inode *inode)
6996 {
6997 struct inode_security_struct *isec = selinux_inode(inode);
6998
6999 spin_lock(&isec->lock);
7000 isec->initialized = LABEL_INVALID;
7001 spin_unlock(&isec->lock);
7002 }
7003
7004 /*
7005 * called with inode->i_mutex locked
7006 */
selinux_inode_notifysecctx(struct inode * inode,void * ctx,u32 ctxlen)7007 static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
7008 {
7009 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
7010 ctx, ctxlen, 0);
7011 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
7012 return rc == -EOPNOTSUPP ? 0 : rc;
7013 }
7014
7015 /*
7016 * called with inode->i_mutex locked
7017 */
selinux_inode_setsecctx(struct dentry * dentry,void * ctx,u32 ctxlen)7018 static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
7019 {
7020 return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX,
7021 ctx, ctxlen, 0, NULL);
7022 }
7023
selinux_inode_getsecctx(struct inode * inode,struct lsm_context * cp)7024 static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp)
7025 {
7026 int len;
7027 len = selinux_inode_getsecurity(&nop_mnt_idmap, inode,
7028 XATTR_SELINUX_SUFFIX,
7029 (void **)&cp->context, true);
7030 if (len < 0)
7031 return len;
7032 cp->len = len;
7033 cp->id = LSM_ID_SELINUX;
7034 return 0;
7035 }
7036 #ifdef CONFIG_KEYS
7037
selinux_key_alloc(struct key * k,const struct cred * cred,unsigned long flags)7038 static int selinux_key_alloc(struct key *k, const struct cred *cred,
7039 unsigned long flags)
7040 {
7041 const struct cred_security_struct *crsec;
7042 struct key_security_struct *ksec = selinux_key(k);
7043
7044 crsec = selinux_cred(cred);
7045 if (crsec->keycreate_sid)
7046 ksec->sid = crsec->keycreate_sid;
7047 else
7048 ksec->sid = crsec->sid;
7049
7050 return 0;
7051 }
7052
selinux_key_permission(key_ref_t key_ref,const struct cred * cred,enum key_need_perm need_perm)7053 static int selinux_key_permission(key_ref_t key_ref,
7054 const struct cred *cred,
7055 enum key_need_perm need_perm)
7056 {
7057 struct key *key;
7058 struct key_security_struct *ksec;
7059 u32 perm, sid;
7060
7061 switch (need_perm) {
7062 case KEY_NEED_VIEW:
7063 perm = KEY__VIEW;
7064 break;
7065 case KEY_NEED_READ:
7066 perm = KEY__READ;
7067 break;
7068 case KEY_NEED_WRITE:
7069 perm = KEY__WRITE;
7070 break;
7071 case KEY_NEED_SEARCH:
7072 perm = KEY__SEARCH;
7073 break;
7074 case KEY_NEED_LINK:
7075 perm = KEY__LINK;
7076 break;
7077 case KEY_NEED_SETATTR:
7078 perm = KEY__SETATTR;
7079 break;
7080 case KEY_NEED_UNLINK:
7081 case KEY_SYSADMIN_OVERRIDE:
7082 case KEY_AUTHTOKEN_OVERRIDE:
7083 case KEY_DEFER_PERM_CHECK:
7084 return 0;
7085 default:
7086 WARN_ON(1);
7087 return -EPERM;
7088
7089 }
7090
7091 sid = cred_sid(cred);
7092 key = key_ref_to_ptr(key_ref);
7093 ksec = selinux_key(key);
7094
7095 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
7096 }
7097
selinux_key_getsecurity(struct key * key,char ** _buffer)7098 static int selinux_key_getsecurity(struct key *key, char **_buffer)
7099 {
7100 struct key_security_struct *ksec = selinux_key(key);
7101 char *context = NULL;
7102 unsigned len;
7103 int rc;
7104
7105 rc = security_sid_to_context(ksec->sid,
7106 &context, &len);
7107 if (!rc)
7108 rc = len;
7109 *_buffer = context;
7110 return rc;
7111 }
7112
7113 #ifdef CONFIG_KEY_NOTIFICATIONS
selinux_watch_key(struct key * key)7114 static int selinux_watch_key(struct key *key)
7115 {
7116 struct key_security_struct *ksec = selinux_key(key);
7117 u32 sid = current_sid();
7118
7119 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, KEY__VIEW, NULL);
7120 }
7121 #endif
7122 #endif
7123
7124 #ifdef CONFIG_SECURITY_INFINIBAND
selinux_ib_pkey_access(void * ib_sec,u64 subnet_prefix,u16 pkey_val)7125 static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
7126 {
7127 struct common_audit_data ad;
7128 int err;
7129 u32 sid = 0;
7130 struct ib_security_struct *sec = ib_sec;
7131 struct lsm_ibpkey_audit ibpkey;
7132
7133 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
7134 if (err)
7135 return err;
7136
7137 ad.type = LSM_AUDIT_DATA_IBPKEY;
7138 ibpkey.subnet_prefix = subnet_prefix;
7139 ibpkey.pkey = pkey_val;
7140 ad.u.ibpkey = &ibpkey;
7141 return avc_has_perm(sec->sid, sid,
7142 SECCLASS_INFINIBAND_PKEY,
7143 INFINIBAND_PKEY__ACCESS, &ad);
7144 }
7145
selinux_ib_endport_manage_subnet(void * ib_sec,const char * dev_name,u8 port_num)7146 static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
7147 u8 port_num)
7148 {
7149 struct common_audit_data ad;
7150 int err;
7151 u32 sid = 0;
7152 struct ib_security_struct *sec = ib_sec;
7153 struct lsm_ibendport_audit ibendport;
7154
7155 err = security_ib_endport_sid(dev_name, port_num,
7156 &sid);
7157
7158 if (err)
7159 return err;
7160
7161 ad.type = LSM_AUDIT_DATA_IBENDPORT;
7162 ibendport.dev_name = dev_name;
7163 ibendport.port = port_num;
7164 ad.u.ibendport = &ibendport;
7165 return avc_has_perm(sec->sid, sid,
7166 SECCLASS_INFINIBAND_ENDPORT,
7167 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
7168 }
7169
selinux_ib_alloc_security(void * ib_sec)7170 static int selinux_ib_alloc_security(void *ib_sec)
7171 {
7172 struct ib_security_struct *sec = selinux_ib(ib_sec);
7173
7174 sec->sid = current_sid();
7175 return 0;
7176 }
7177 #endif
7178
7179 #ifdef CONFIG_BPF_SYSCALL
selinux_bpf(int cmd,union bpf_attr * attr,unsigned int size,bool kernel)7180 static int selinux_bpf(int cmd, union bpf_attr *attr,
7181 unsigned int size, bool kernel)
7182 {
7183 u32 sid = current_sid();
7184 int ret;
7185
7186 if (selinux_policycap_bpf_token_perms())
7187 return 0;
7188
7189 switch (cmd) {
7190 case BPF_MAP_CREATE:
7191 ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
7192 NULL);
7193 break;
7194 case BPF_PROG_LOAD:
7195 ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
7196 NULL);
7197 break;
7198 default:
7199 ret = 0;
7200 break;
7201 }
7202
7203 return ret;
7204 }
7205
bpf_map_fmode_to_av(fmode_t fmode)7206 static u32 bpf_map_fmode_to_av(fmode_t fmode)
7207 {
7208 u32 av = 0;
7209
7210 if (fmode & FMODE_READ)
7211 av |= BPF__MAP_READ;
7212 if (fmode & FMODE_WRITE)
7213 av |= BPF__MAP_WRITE;
7214 return av;
7215 }
7216
7217 /* This function will check the file pass through unix socket or binder to see
7218 * if it is a bpf related object. And apply corresponding checks on the bpf
7219 * object based on the type. The bpf maps and programs, not like other files and
7220 * socket, are using a shared anonymous inode inside the kernel as their inode.
7221 * So checking that inode cannot identify if the process have privilege to
7222 * access the bpf object and that's why we have to add this additional check in
7223 * selinux_file_receive and selinux_binder_transfer_files.
7224 */
bpf_fd_pass(const struct file * file,u32 sid)7225 static int bpf_fd_pass(const struct file *file, u32 sid)
7226 {
7227 struct bpf_security_struct *bpfsec;
7228 struct bpf_prog *prog;
7229 struct bpf_map *map;
7230 int ret;
7231
7232 if (file->f_op == &bpf_map_fops) {
7233 map = file->private_data;
7234 bpfsec = selinux_bpf_map_security(map);
7235 ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
7236 bpf_map_fmode_to_av(file->f_mode), NULL);
7237 if (ret)
7238 return ret;
7239 } else if (file->f_op == &bpf_prog_fops) {
7240 prog = file->private_data;
7241 bpfsec = selinux_bpf_prog_security(prog);
7242 ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
7243 BPF__PROG_RUN, NULL);
7244 if (ret)
7245 return ret;
7246 }
7247 return 0;
7248 }
7249
selinux_bpf_map(struct bpf_map * map,fmode_t fmode)7250 static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
7251 {
7252 u32 sid = current_sid();
7253 struct bpf_security_struct *bpfsec;
7254
7255 bpfsec = selinux_bpf_map_security(map);
7256 return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
7257 bpf_map_fmode_to_av(fmode), NULL);
7258 }
7259
selinux_bpf_prog(struct bpf_prog * prog)7260 static int selinux_bpf_prog(struct bpf_prog *prog)
7261 {
7262 u32 sid = current_sid();
7263 struct bpf_security_struct *bpfsec;
7264
7265 bpfsec = selinux_bpf_prog_security(prog);
7266 return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
7267 BPF__PROG_RUN, NULL);
7268 }
7269
selinux_bpffs_creator_sid(u32 fd)7270 static u32 selinux_bpffs_creator_sid(u32 fd)
7271 {
7272 struct path path;
7273 struct super_block *sb;
7274 struct superblock_security_struct *sbsec;
7275
7276 CLASS(fd, f)(fd);
7277
7278 if (fd_empty(f))
7279 return SECSID_NULL;
7280
7281 path = fd_file(f)->f_path;
7282 sb = path.dentry->d_sb;
7283 sbsec = selinux_superblock(sb);
7284
7285 return sbsec->creator_sid;
7286 }
7287
selinux_bpf_map_create(struct bpf_map * map,union bpf_attr * attr,struct bpf_token * token,bool kernel)7288 static int selinux_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
7289 struct bpf_token *token, bool kernel)
7290 {
7291 struct bpf_security_struct *bpfsec;
7292 u32 ssid;
7293
7294 bpfsec = selinux_bpf_map_security(map);
7295 bpfsec->sid = current_sid();
7296
7297 if (!token)
7298 ssid = bpfsec->sid;
7299 else
7300 ssid = selinux_bpffs_creator_sid(attr->map_token_fd);
7301
7302 return avc_has_perm(ssid, bpfsec->sid, SECCLASS_BPF, BPF__MAP_CREATE,
7303 NULL);
7304 }
7305
selinux_bpf_prog_load(struct bpf_prog * prog,union bpf_attr * attr,struct bpf_token * token,bool kernel)7306 static int selinux_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
7307 struct bpf_token *token, bool kernel)
7308 {
7309 struct bpf_security_struct *bpfsec;
7310 u32 ssid;
7311
7312 bpfsec = selinux_bpf_prog_security(prog);
7313 bpfsec->sid = current_sid();
7314
7315 if (!token)
7316 ssid = bpfsec->sid;
7317 else
7318 ssid = selinux_bpffs_creator_sid(attr->prog_token_fd);
7319
7320 return avc_has_perm(ssid, bpfsec->sid, SECCLASS_BPF, BPF__PROG_LOAD,
7321 NULL);
7322 }
7323
7324 #define bpf_token_cmd(T, C) \
7325 ((T)->allowed_cmds & (1ULL << (C)))
7326
selinux_bpf_token_create(struct bpf_token * token,union bpf_attr * attr,const struct path * path)7327 static int selinux_bpf_token_create(struct bpf_token *token,
7328 union bpf_attr *attr,
7329 const struct path *path)
7330 {
7331 struct bpf_security_struct *bpfsec;
7332 u32 sid = selinux_bpffs_creator_sid(attr->token_create.bpffs_fd);
7333 int err;
7334
7335 bpfsec = selinux_bpf_token_security(token);
7336 bpfsec->sid = current_sid();
7337 bpfsec->grantor_sid = sid;
7338
7339 bpfsec->perms = 0;
7340 /**
7341 * 'token->allowed_cmds' is a bit mask of allowed commands
7342 * Convert the BPF command enum to a bitmask representing its position
7343 * in the allowed_cmds bitmap.
7344 */
7345 if (bpf_token_cmd(token, BPF_MAP_CREATE)) {
7346 err = avc_has_perm(bpfsec->sid, sid, SECCLASS_BPF,
7347 BPF__MAP_CREATE_AS, NULL);
7348 if (err)
7349 return err;
7350 bpfsec->perms |= BPF__MAP_CREATE;
7351 }
7352 if (bpf_token_cmd(token, BPF_PROG_LOAD)) {
7353 err = avc_has_perm(bpfsec->sid, sid, SECCLASS_BPF,
7354 BPF__PROG_LOAD_AS, NULL);
7355 if (err)
7356 return err;
7357 bpfsec->perms |= BPF__PROG_LOAD;
7358 }
7359
7360 return 0;
7361 }
7362
selinux_bpf_token_cmd(const struct bpf_token * token,enum bpf_cmd cmd)7363 static int selinux_bpf_token_cmd(const struct bpf_token *token,
7364 enum bpf_cmd cmd)
7365 {
7366 struct bpf_security_struct *bpfsec;
7367
7368 bpfsec = token->security;
7369 switch (cmd) {
7370 case BPF_MAP_CREATE:
7371 if (!(bpfsec->perms & BPF__MAP_CREATE))
7372 return -EACCES;
7373 break;
7374 case BPF_PROG_LOAD:
7375 if (!(bpfsec->perms & BPF__PROG_LOAD))
7376 return -EACCES;
7377 break;
7378 default:
7379 break;
7380 }
7381
7382 return 0;
7383 }
7384
selinux_bpf_token_capable(const struct bpf_token * token,int cap)7385 static int selinux_bpf_token_capable(const struct bpf_token *token, int cap)
7386 {
7387 u16 sclass;
7388 struct bpf_security_struct *bpfsec = token->security;
7389 bool initns = (token->userns == &init_user_ns);
7390 u32 av = CAP_TO_MASK(cap);
7391
7392 switch (CAP_TO_INDEX(cap)) {
7393 case 0:
7394 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
7395 break;
7396 case 1:
7397 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
7398 break;
7399 default:
7400 pr_err("SELinux: out of range capability %d\n", cap);
7401 return -EINVAL;
7402 }
7403
7404 return avc_has_perm(current_sid(), bpfsec->grantor_sid, sclass, av,
7405 NULL);
7406 }
7407 #endif
7408
7409 #ifdef CONFIG_PERF_EVENTS
selinux_perf_event_open(int type)7410 static int selinux_perf_event_open(int type)
7411 {
7412 u32 requested, sid = current_sid();
7413
7414 if (type == PERF_SECURITY_OPEN)
7415 requested = PERF_EVENT__OPEN;
7416 else if (type == PERF_SECURITY_CPU)
7417 requested = PERF_EVENT__CPU;
7418 else if (type == PERF_SECURITY_KERNEL)
7419 requested = PERF_EVENT__KERNEL;
7420 else if (type == PERF_SECURITY_TRACEPOINT)
7421 requested = PERF_EVENT__TRACEPOINT;
7422 else
7423 return -EINVAL;
7424
7425 return avc_has_perm(sid, sid, SECCLASS_PERF_EVENT,
7426 requested, NULL);
7427 }
7428
selinux_perf_event_alloc(struct perf_event * event)7429 static int selinux_perf_event_alloc(struct perf_event *event)
7430 {
7431 struct perf_event_security_struct *perfsec;
7432
7433 perfsec = selinux_perf_event(event->security);
7434 perfsec->sid = current_sid();
7435
7436 return 0;
7437 }
7438
selinux_perf_event_read(struct perf_event * event)7439 static int selinux_perf_event_read(struct perf_event *event)
7440 {
7441 struct perf_event_security_struct *perfsec = event->security;
7442 u32 sid = current_sid();
7443
7444 return avc_has_perm(sid, perfsec->sid,
7445 SECCLASS_PERF_EVENT, PERF_EVENT__READ, NULL);
7446 }
7447
selinux_perf_event_write(struct perf_event * event)7448 static int selinux_perf_event_write(struct perf_event *event)
7449 {
7450 struct perf_event_security_struct *perfsec = event->security;
7451 u32 sid = current_sid();
7452
7453 return avc_has_perm(sid, perfsec->sid,
7454 SECCLASS_PERF_EVENT, PERF_EVENT__WRITE, NULL);
7455 }
7456 #endif
7457
7458 #ifdef CONFIG_IO_URING
7459 /**
7460 * selinux_uring_override_creds - check the requested cred override
7461 * @new: the target creds
7462 *
7463 * Check to see if the current task is allowed to override it's credentials
7464 * to service an io_uring operation.
7465 */
selinux_uring_override_creds(const struct cred * new)7466 static int selinux_uring_override_creds(const struct cred *new)
7467 {
7468 return avc_has_perm(current_sid(), cred_sid(new),
7469 SECCLASS_IO_URING, IO_URING__OVERRIDE_CREDS, NULL);
7470 }
7471
7472 /**
7473 * selinux_uring_sqpoll - check if a io_uring polling thread can be created
7474 *
7475 * Check to see if the current task is allowed to create a new io_uring
7476 * kernel polling thread.
7477 */
selinux_uring_sqpoll(void)7478 static int selinux_uring_sqpoll(void)
7479 {
7480 u32 sid = current_sid();
7481
7482 return avc_has_perm(sid, sid,
7483 SECCLASS_IO_URING, IO_URING__SQPOLL, NULL);
7484 }
7485
7486 /**
7487 * selinux_uring_cmd - check if IORING_OP_URING_CMD is allowed
7488 * @ioucmd: the io_uring command structure
7489 *
7490 * Check to see if the current domain is allowed to execute an
7491 * IORING_OP_URING_CMD against the device/file specified in @ioucmd.
7492 *
7493 */
selinux_uring_cmd(struct io_uring_cmd * ioucmd)7494 static int selinux_uring_cmd(struct io_uring_cmd *ioucmd)
7495 {
7496 struct file *file = ioucmd->file;
7497 struct inode *inode = file_inode(file);
7498 struct inode_security_struct *isec = selinux_inode(inode);
7499 struct common_audit_data ad;
7500
7501 ad.type = LSM_AUDIT_DATA_FILE;
7502 ad.u.file = file;
7503
7504 return avc_has_perm(current_sid(), isec->sid,
7505 SECCLASS_IO_URING, IO_URING__CMD, &ad);
7506 }
7507
7508 /**
7509 * selinux_uring_allowed - check if io_uring_setup() can be called
7510 *
7511 * Check to see if the current task is allowed to call io_uring_setup().
7512 */
selinux_uring_allowed(void)7513 static int selinux_uring_allowed(void)
7514 {
7515 u32 sid = current_sid();
7516
7517 return avc_has_perm(sid, sid, SECCLASS_IO_URING, IO_URING__ALLOWED,
7518 NULL);
7519 }
7520 #endif /* CONFIG_IO_URING */
7521
7522 static const struct lsm_id selinux_lsmid = {
7523 .name = "selinux",
7524 .id = LSM_ID_SELINUX,
7525 };
7526
7527 struct lsm_blob_sizes selinux_blob_sizes __ro_after_init = {
7528 .lbs_cred = sizeof(struct cred_security_struct),
7529 .lbs_task = sizeof(struct task_security_struct),
7530 .lbs_file = sizeof(struct file_security_struct),
7531 .lbs_backing_file = sizeof(struct backing_file_security_struct),
7532 .lbs_inode = sizeof(struct inode_security_struct),
7533 .lbs_ipc = sizeof(struct ipc_security_struct),
7534 .lbs_key = sizeof(struct key_security_struct),
7535 .lbs_msg_msg = sizeof(struct msg_security_struct),
7536 #ifdef CONFIG_PERF_EVENTS
7537 .lbs_perf_event = sizeof(struct perf_event_security_struct),
7538 #endif
7539 .lbs_sock = sizeof(struct sk_security_struct),
7540 .lbs_superblock = sizeof(struct superblock_security_struct),
7541 .lbs_xattr_count = SELINUX_INODE_INIT_XATTRS,
7542 .lbs_tun_dev = sizeof(struct tun_security_struct),
7543 .lbs_ib = sizeof(struct ib_security_struct),
7544 .lbs_bpf_map = sizeof(struct bpf_security_struct),
7545 .lbs_bpf_prog = sizeof(struct bpf_security_struct),
7546 .lbs_bpf_token = sizeof(struct bpf_security_struct),
7547 };
7548
7549 /*
7550 * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order:
7551 * 1. any hooks that don't belong to (2.) or (3.) below,
7552 * 2. hooks that both access structures allocated by other hooks, and allocate
7553 * structures that can be later accessed by other hooks (mostly "cloning"
7554 * hooks),
7555 * 3. hooks that only allocate structures that can be later accessed by other
7556 * hooks ("allocating" hooks).
7557 *
7558 * Please follow block comment delimiters in the list to keep this order.
7559 */
7560 static struct security_hook_list selinux_hooks[] __ro_after_init = {
7561 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
7562 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
7563 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
7564 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
7565
7566 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
7567 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
7568 LSM_HOOK_INIT(capget, selinux_capget),
7569 LSM_HOOK_INIT(capset, selinux_capset),
7570 LSM_HOOK_INIT(capable, selinux_capable),
7571 LSM_HOOK_INIT(quotactl, selinux_quotactl),
7572 LSM_HOOK_INIT(quota_on, selinux_quota_on),
7573 LSM_HOOK_INIT(syslog, selinux_syslog),
7574 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
7575
7576 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
7577
7578 LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec),
7579 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
7580 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
7581
7582 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
7583 LSM_HOOK_INIT(sb_mnt_opts_compat, selinux_sb_mnt_opts_compat),
7584 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
7585 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
7586 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
7587 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
7588 LSM_HOOK_INIT(sb_mount, selinux_mount),
7589 LSM_HOOK_INIT(sb_umount, selinux_umount),
7590 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
7591 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
7592
7593 LSM_HOOK_INIT(move_mount, selinux_move_mount),
7594
7595 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
7596 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
7597
7598 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
7599 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
7600 LSM_HOOK_INIT(inode_init_security_anon, selinux_inode_init_security_anon),
7601 LSM_HOOK_INIT(inode_create, selinux_inode_create),
7602 LSM_HOOK_INIT(inode_link, selinux_inode_link),
7603 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
7604 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
7605 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
7606 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
7607 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
7608 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
7609 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
7610 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
7611 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
7612 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
7613 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
7614 LSM_HOOK_INIT(inode_xattr_skipcap, selinux_inode_xattr_skipcap),
7615 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
7616 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
7617 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
7618 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
7619 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
7620 LSM_HOOK_INIT(inode_file_getattr, selinux_inode_file_getattr),
7621 LSM_HOOK_INIT(inode_file_setattr, selinux_inode_file_setattr),
7622 LSM_HOOK_INIT(inode_set_acl, selinux_inode_set_acl),
7623 LSM_HOOK_INIT(inode_get_acl, selinux_inode_get_acl),
7624 LSM_HOOK_INIT(inode_remove_acl, selinux_inode_remove_acl),
7625 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
7626 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
7627 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
7628 LSM_HOOK_INIT(inode_getlsmprop, selinux_inode_getlsmprop),
7629 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
7630 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
7631 LSM_HOOK_INIT(path_notify, selinux_path_notify),
7632
7633 LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security),
7634
7635 LSM_HOOK_INIT(file_permission, selinux_file_permission),
7636 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
7637 LSM_HOOK_INIT(backing_file_alloc, selinux_backing_file_alloc),
7638 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
7639 LSM_HOOK_INIT(file_ioctl_compat, selinux_file_ioctl_compat),
7640 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
7641 LSM_HOOK_INIT(mmap_backing_file, selinux_mmap_backing_file),
7642 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
7643 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
7644 LSM_HOOK_INIT(file_lock, selinux_file_lock),
7645 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
7646 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
7647 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
7648 LSM_HOOK_INIT(file_receive, selinux_file_receive),
7649
7650 LSM_HOOK_INIT(file_open, selinux_file_open),
7651
7652 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
7653 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
7654 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
7655 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
7656 LSM_HOOK_INIT(cred_getlsmprop, selinux_cred_getlsmprop),
7657 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
7658 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
7659 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
7660 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
7661 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
7662 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
7663 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
7664 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
7665 LSM_HOOK_INIT(current_getlsmprop_subj, selinux_current_getlsmprop_subj),
7666 LSM_HOOK_INIT(task_getlsmprop_obj, selinux_task_getlsmprop_obj),
7667 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
7668 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
7669 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
7670 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
7671 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
7672 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
7673 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
7674 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
7675 LSM_HOOK_INIT(task_kill, selinux_task_kill),
7676 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
7677 LSM_HOOK_INIT(userns_create, selinux_userns_create),
7678
7679 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
7680 LSM_HOOK_INIT(ipc_getlsmprop, selinux_ipc_getlsmprop),
7681
7682 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
7683 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
7684 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
7685 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
7686
7687 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
7688 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
7689 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
7690
7691 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
7692 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
7693 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
7694
7695 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
7696
7697 LSM_HOOK_INIT(getselfattr, selinux_getselfattr),
7698 LSM_HOOK_INIT(setselfattr, selinux_setselfattr),
7699 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
7700 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
7701
7702 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
7703 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
7704 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
7705 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
7706 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
7707 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
7708
7709 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
7710 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
7711
7712 LSM_HOOK_INIT(socket_create, selinux_socket_create),
7713 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
7714 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
7715 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
7716 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
7717 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
7718 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
7719 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
7720 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
7721 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
7722 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
7723 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
7724 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
7725 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
7726 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
7727 LSM_HOOK_INIT(socket_getpeersec_stream,
7728 selinux_socket_getpeersec_stream),
7729 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
7730 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
7731 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
7732 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
7733 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
7734 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
7735 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
7736 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
7737 LSM_HOOK_INIT(sctp_assoc_established, selinux_sctp_assoc_established),
7738 LSM_HOOK_INIT(mptcp_add_subflow, selinux_mptcp_add_subflow),
7739 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
7740 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
7741 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
7742 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
7743 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
7744 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
7745 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
7746 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
7747 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
7748 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
7749 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
7750 #ifdef CONFIG_SECURITY_INFINIBAND
7751 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
7752 LSM_HOOK_INIT(ib_endport_manage_subnet,
7753 selinux_ib_endport_manage_subnet),
7754 #endif
7755 #ifdef CONFIG_SECURITY_NETWORK_XFRM
7756 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
7757 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
7758 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
7759 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
7760 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
7761 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
7762 selinux_xfrm_state_pol_flow_match),
7763 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
7764 #endif
7765
7766 #ifdef CONFIG_KEYS
7767 LSM_HOOK_INIT(key_permission, selinux_key_permission),
7768 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
7769 #ifdef CONFIG_KEY_NOTIFICATIONS
7770 LSM_HOOK_INIT(watch_key, selinux_watch_key),
7771 #endif
7772 #endif
7773
7774 #ifdef CONFIG_AUDIT
7775 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
7776 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
7777 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
7778 #endif
7779
7780 #ifdef CONFIG_BPF_SYSCALL
7781 LSM_HOOK_INIT(bpf, selinux_bpf),
7782 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7783 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
7784 #endif
7785
7786 #ifdef CONFIG_PERF_EVENTS
7787 LSM_HOOK_INIT(perf_event_open, selinux_perf_event_open),
7788 LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read),
7789 LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write),
7790 #endif
7791
7792 #ifdef CONFIG_IO_URING
7793 LSM_HOOK_INIT(uring_override_creds, selinux_uring_override_creds),
7794 LSM_HOOK_INIT(uring_sqpoll, selinux_uring_sqpoll),
7795 LSM_HOOK_INIT(uring_cmd, selinux_uring_cmd),
7796 LSM_HOOK_INIT(uring_allowed, selinux_uring_allowed),
7797 #endif
7798
7799 /*
7800 * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE
7801 */
7802 LSM_HOOK_INIT(fs_context_submount, selinux_fs_context_submount),
7803 LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
7804 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
7805 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
7806 #ifdef CONFIG_SECURITY_NETWORK_XFRM
7807 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
7808 #endif
7809
7810 /*
7811 * PUT "ALLOCATING" HOOKS HERE
7812 */
7813 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
7814 LSM_HOOK_INIT(msg_queue_alloc_security,
7815 selinux_msg_queue_alloc_security),
7816 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
7817 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
7818 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
7819 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
7820 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
7821 LSM_HOOK_INIT(lsmprop_to_secctx, selinux_lsmprop_to_secctx),
7822 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
7823 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
7824 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
7825 #ifdef CONFIG_SECURITY_INFINIBAND
7826 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
7827 #endif
7828 #ifdef CONFIG_SECURITY_NETWORK_XFRM
7829 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
7830 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
7831 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
7832 selinux_xfrm_state_alloc_acquire),
7833 #endif
7834 #ifdef CONFIG_KEYS
7835 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
7836 #endif
7837 #ifdef CONFIG_AUDIT
7838 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7839 #endif
7840 #ifdef CONFIG_BPF_SYSCALL
7841 LSM_HOOK_INIT(bpf_map_create, selinux_bpf_map_create),
7842 LSM_HOOK_INIT(bpf_prog_load, selinux_bpf_prog_load),
7843 LSM_HOOK_INIT(bpf_token_create, selinux_bpf_token_create),
7844 LSM_HOOK_INIT(bpf_token_cmd, selinux_bpf_token_cmd),
7845 LSM_HOOK_INIT(bpf_token_capable, selinux_bpf_token_capable),
7846 #endif
7847 #ifdef CONFIG_PERF_EVENTS
7848 LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc),
7849 #endif
7850 };
7851
selinux_init(void)7852 static __init int selinux_init(void)
7853 {
7854 vma_flags_t data_default_flags = VMA_DATA_DEFAULT_FLAGS;
7855
7856 pr_info("SELinux: Initializing.\n");
7857
7858 memset(&selinux_state, 0, sizeof(selinux_state));
7859 enforcing_set(selinux_enforcing_boot);
7860 selinux_avc_init();
7861 mutex_init(&selinux_state.status_lock);
7862 mutex_init(&selinux_state.policy_mutex);
7863
7864 /* Set the security state for the initial task. */
7865 cred_init_security();
7866
7867 /* Inform the audit system that secctx is used */
7868 audit_cfg_lsm(&selinux_lsmid,
7869 AUDIT_CFG_LSM_SECCTX_SUBJECT |
7870 AUDIT_CFG_LSM_SECCTX_OBJECT);
7871
7872 default_noexec = !vma_flags_test(&data_default_flags, VMA_EXEC_BIT);
7873 if (!default_noexec)
7874 pr_notice("SELinux: virtual memory is executable by default\n");
7875
7876 avc_init();
7877
7878 avtab_cache_init();
7879
7880 ebitmap_cache_init();
7881
7882 hashtab_cache_init();
7883
7884 selinux_ima_config_len_init();
7885
7886 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks),
7887 &selinux_lsmid);
7888
7889 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7890 panic("SELinux: Unable to register AVC netcache callback\n");
7891
7892 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7893 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7894
7895 if (avc_add_callback(selinux_audit_rule_avc_callback,
7896 AVC_CALLBACK_RESET))
7897 panic("SELinux: Unable to register AVC audit callback\n");
7898
7899 if (selinux_enforcing_boot)
7900 pr_debug("SELinux: Starting in enforcing mode\n");
7901 else
7902 pr_debug("SELinux: Starting in permissive mode\n");
7903
7904 fs_validate_description("selinux", selinux_fs_parameters);
7905
7906 return 0;
7907 }
7908
delayed_superblock_init(struct super_block * sb,void * unused)7909 static void delayed_superblock_init(struct super_block *sb, void *unused)
7910 {
7911 selinux_set_mnt_opts(sb, NULL, 0, NULL);
7912 }
7913
selinux_complete_init(void)7914 void selinux_complete_init(void)
7915 {
7916 pr_debug("SELinux: Completing initialization.\n");
7917
7918 /* Set up any superblocks initialized prior to the policy load. */
7919 pr_debug("SELinux: Setting up existing superblocks.\n");
7920 iterate_supers(delayed_superblock_init, NULL);
7921 }
7922
7923 /* SELinux requires early initialization in order to label
7924 all processes and objects when they are created. */
7925 DEFINE_LSM(selinux) = {
7926 .id = &selinux_lsmid,
7927 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
7928 .enabled = &selinux_enabled_boot,
7929 .blobs = &selinux_blob_sizes,
7930 .init = selinux_init,
7931 .initcall_device = selinux_initcall,
7932 };
7933
7934 #if defined(CONFIG_NETFILTER)
7935 static const struct nf_hook_ops selinux_nf_ops[] = {
7936 {
7937 .hook = selinux_ip_postroute,
7938 .pf = NFPROTO_IPV4,
7939 .hooknum = NF_INET_POST_ROUTING,
7940 .priority = NF_IP_PRI_SELINUX_LAST,
7941 },
7942 {
7943 .hook = selinux_ip_forward,
7944 .pf = NFPROTO_IPV4,
7945 .hooknum = NF_INET_FORWARD,
7946 .priority = NF_IP_PRI_SELINUX_FIRST,
7947 },
7948 {
7949 .hook = selinux_ip_output,
7950 .pf = NFPROTO_IPV4,
7951 .hooknum = NF_INET_LOCAL_OUT,
7952 .priority = NF_IP_PRI_SELINUX_FIRST,
7953 },
7954 #if IS_ENABLED(CONFIG_IPV6)
7955 {
7956 .hook = selinux_ip_postroute,
7957 .pf = NFPROTO_IPV6,
7958 .hooknum = NF_INET_POST_ROUTING,
7959 .priority = NF_IP6_PRI_SELINUX_LAST,
7960 },
7961 {
7962 .hook = selinux_ip_forward,
7963 .pf = NFPROTO_IPV6,
7964 .hooknum = NF_INET_FORWARD,
7965 .priority = NF_IP6_PRI_SELINUX_FIRST,
7966 },
7967 {
7968 .hook = selinux_ip_output,
7969 .pf = NFPROTO_IPV6,
7970 .hooknum = NF_INET_LOCAL_OUT,
7971 .priority = NF_IP6_PRI_SELINUX_FIRST,
7972 },
7973 #endif /* IPV6 */
7974 };
7975
selinux_nf_register(struct net * net)7976 static int __net_init selinux_nf_register(struct net *net)
7977 {
7978 return nf_register_net_hooks(net, selinux_nf_ops,
7979 ARRAY_SIZE(selinux_nf_ops));
7980 }
7981
selinux_nf_unregister(struct net * net)7982 static void __net_exit selinux_nf_unregister(struct net *net)
7983 {
7984 nf_unregister_net_hooks(net, selinux_nf_ops,
7985 ARRAY_SIZE(selinux_nf_ops));
7986 }
7987
7988 static struct pernet_operations selinux_net_ops = {
7989 .init = selinux_nf_register,
7990 .exit = selinux_nf_unregister,
7991 };
7992
selinux_nf_ip_init(void)7993 int __init selinux_nf_ip_init(void)
7994 {
7995 int err;
7996
7997 if (!selinux_enabled_boot)
7998 return 0;
7999
8000 pr_debug("SELinux: Registering netfilter hooks\n");
8001
8002 err = register_pernet_subsys(&selinux_net_ops);
8003 if (err)
8004 panic("SELinux: register_pernet_subsys: error %d\n", err);
8005
8006 return 0;
8007 }
8008 #endif /* CONFIG_NETFILTER */
8009