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