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