Branch data Line data Source code
1 : : /*
2 : : * NSA Security-Enhanced Linux (SELinux) security module
3 : : *
4 : : * This file contains the SELinux hook function implementations.
5 : : *
6 : : * Authors: Stephen Smalley, <sds@epoch.ncsc.mil>
7 : : * Chris Vance, <cvance@nai.com>
8 : : * Wayne Salamon, <wsalamon@nai.com>
9 : : * James Morris <jmorris@redhat.com>
10 : : *
11 : : * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
12 : : * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
13 : : * Eric Paris <eparis@redhat.com>
14 : : * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
15 : : * <dgoeddel@trustedcs.com>
16 : : * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
17 : : * Paul Moore <paul@paul-moore.com>
18 : : * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
19 : : * Yuichi Nakamura <ynakam@hitachisoft.jp>
20 : : *
21 : : * This program is free software; you can redistribute it and/or modify
22 : : * it under the terms of the GNU General Public License version 2,
23 : : * as published by the Free Software Foundation.
24 : : */
25 : :
26 : : #include <linux/init.h>
27 : : #include <linux/kd.h>
28 : : #include <linux/kernel.h>
29 : : #include <linux/tracehook.h>
30 : : #include <linux/errno.h>
31 : : #include <linux/sched.h>
32 : : #include <linux/security.h>
33 : : #include <linux/xattr.h>
34 : : #include <linux/capability.h>
35 : : #include <linux/unistd.h>
36 : : #include <linux/mm.h>
37 : : #include <linux/mman.h>
38 : : #include <linux/slab.h>
39 : : #include <linux/pagemap.h>
40 : : #include <linux/proc_fs.h>
41 : : #include <linux/swap.h>
42 : : #include <linux/spinlock.h>
43 : : #include <linux/syscalls.h>
44 : : #include <linux/dcache.h>
45 : : #include <linux/file.h>
46 : : #include <linux/fdtable.h>
47 : : #include <linux/namei.h>
48 : : #include <linux/mount.h>
49 : : #include <linux/netfilter_ipv4.h>
50 : : #include <linux/netfilter_ipv6.h>
51 : : #include <linux/tty.h>
52 : : #include <net/icmp.h>
53 : : #include <net/ip.h> /* for local_port_range[] */
54 : : #include <net/sock.h>
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/quota.h>
70 : : #include <linux/un.h> /* for Unix socket types */
71 : : #include <net/af_unix.h> /* for Unix socket types */
72 : : #include <linux/parser.h>
73 : : #include <linux/nfs_mount.h>
74 : : #include <net/ipv6.h>
75 : : #include <linux/hugetlb.h>
76 : : #include <linux/personality.h>
77 : : #include <linux/audit.h>
78 : : #include <linux/string.h>
79 : : #include <linux/selinux.h>
80 : : #include <linux/mutex.h>
81 : : #include <linux/posix-timers.h>
82 : : #include <linux/syslog.h>
83 : : #include <linux/user_namespace.h>
84 : : #include <linux/export.h>
85 : : #include <linux/msg.h>
86 : : #include <linux/shm.h>
87 : :
88 : : #include "avc.h"
89 : : #include "objsec.h"
90 : : #include "netif.h"
91 : : #include "netnode.h"
92 : : #include "netport.h"
93 : : #include "xfrm.h"
94 : : #include "netlabel.h"
95 : : #include "audit.h"
96 : : #include "avc_ss.h"
97 : :
98 : : extern struct security_operations *security_ops;
99 : :
100 : : /* SECMARK reference count */
101 : : static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
102 : :
103 : : #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
104 : : int selinux_enforcing;
105 : :
106 : 0 : static int __init enforcing_setup(char *str)
107 : : {
108 : : unsigned long enforcing;
109 [ # # ]: 0 : if (!strict_strtoul(str, 0, &enforcing))
110 : 0 : selinux_enforcing = enforcing ? 1 : 0;
111 : 0 : return 1;
112 : : }
113 : : __setup("enforcing=", enforcing_setup);
114 : : #endif
115 : :
116 : : #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
117 : : int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
118 : :
119 : : static int __init selinux_enabled_setup(char *str)
120 : : {
121 : : unsigned long enabled;
122 : : if (!strict_strtoul(str, 0, &enabled))
123 : : selinux_enabled = enabled ? 1 : 0;
124 : : return 1;
125 : : }
126 : : __setup("selinux=", selinux_enabled_setup);
127 : : #else
128 : : int selinux_enabled = 1;
129 : : #endif
130 : :
131 : : static struct kmem_cache *sel_inode_cache;
132 : :
133 : : /**
134 : : * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
135 : : *
136 : : * Description:
137 : : * This function checks the SECMARK reference counter to see if any SECMARK
138 : : * targets are currently configured, if the reference counter is greater than
139 : : * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
140 : : * enabled, false (0) if SECMARK is disabled. If the always_check_network
141 : : * policy capability is enabled, SECMARK is always considered enabled.
142 : : *
143 : : */
144 : : static int selinux_secmark_enabled(void)
145 : : {
146 [ # # ][ # # ]: 0 : return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount));
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
147 : : }
148 : :
149 : : /**
150 : : * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
151 : : *
152 : : * Description:
153 : : * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
154 : : * (1) if any are enabled or false (0) if neither are enabled. If the
155 : : * always_check_network policy capability is enabled, peer labeling
156 : : * is always considered enabled.
157 : : *
158 : : */
159 : : static int selinux_peerlbl_enabled(void)
160 : : {
161 [ # # ][ # # ]: 0 : return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled());
[ # # ][ # # ]
[ # # ][ # # ]
162 : : }
163 : :
164 : : /*
165 : : * initialise the security for the init task
166 : : */
167 : 0 : static void cred_init_security(void)
168 : : {
169 : 0 : struct cred *cred = (struct cred *) current->real_cred;
170 : : struct task_security_struct *tsec;
171 : :
172 : : tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
173 [ # # ]: 0 : if (!tsec)
174 : 0 : panic("SELinux: Failed to initialize initial task.\n");
175 : :
176 : 0 : tsec->osid = tsec->sid = SECINITSID_KERNEL;
177 : 0 : cred->security = tsec;
178 : 0 : }
179 : :
180 : : /*
181 : : * get the security ID of a set of credentials
182 : : */
183 : : static inline u32 cred_sid(const struct cred *cred)
184 : : {
185 : : const struct task_security_struct *tsec;
186 : :
187 : : tsec = cred->security;
188 : 0 : return tsec->sid;
189 : : }
190 : :
191 : : /*
192 : : * get the objective security ID of a task
193 : : */
194 : : static inline u32 task_sid(const struct task_struct *task)
195 : : {
196 : : u32 sid;
197 : :
198 : : rcu_read_lock();
199 : 0 : sid = cred_sid(__task_cred(task));
200 : : rcu_read_unlock();
201 : : return sid;
202 : : }
203 : :
204 : : /*
205 : : * get the subjective security ID of the current task
206 : : */
207 : : static inline u32 current_sid(void)
208 : : {
209 : 0 : const struct task_security_struct *tsec = current_security();
210 : :
211 : 0 : return tsec->sid;
212 : : }
213 : :
214 : : /* Allocate and free functions for each kind of security blob. */
215 : :
216 : 0 : static int inode_alloc_security(struct inode *inode)
217 : : {
218 : : struct inode_security_struct *isec;
219 : : u32 sid = current_sid();
220 : :
221 : 0 : isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
222 [ # # ]: 0 : if (!isec)
223 : : return -ENOMEM;
224 : :
225 : 0 : mutex_init(&isec->lock);
226 : 0 : INIT_LIST_HEAD(&isec->list);
227 : 0 : isec->inode = inode;
228 : 0 : isec->sid = SECINITSID_UNLABELED;
229 : 0 : isec->sclass = SECCLASS_FILE;
230 : 0 : isec->task_sid = sid;
231 : 0 : inode->i_security = isec;
232 : :
233 : 0 : return 0;
234 : : }
235 : :
236 : 0 : static void inode_free_rcu(struct rcu_head *head)
237 : : {
238 : : struct inode_security_struct *isec;
239 : :
240 : 0 : isec = container_of(head, struct inode_security_struct, rcu);
241 : 0 : kmem_cache_free(sel_inode_cache, isec);
242 : 0 : }
243 : :
244 : 0 : static void inode_free_security(struct inode *inode)
245 : : {
246 : 0 : struct inode_security_struct *isec = inode->i_security;
247 : 0 : struct superblock_security_struct *sbsec = inode->i_sb->s_security;
248 : :
249 : : spin_lock(&sbsec->isec_lock);
250 [ # # ]: 0 : if (!list_empty(&isec->list))
251 : : list_del_init(&isec->list);
252 : : spin_unlock(&sbsec->isec_lock);
253 : :
254 : : /*
255 : : * The inode may still be referenced in a path walk and
256 : : * a call to selinux_inode_permission() can be made
257 : : * after inode_free_security() is called. Ideally, the VFS
258 : : * wouldn't do this, but fixing that is a much harder
259 : : * job. For now, simply free the i_security via RCU, and
260 : : * leave the current inode->i_security pointer intact.
261 : : * The inode will be freed after the RCU grace period too.
262 : : */
263 : 0 : call_rcu(&isec->rcu, inode_free_rcu);
264 : 0 : }
265 : :
266 : 0 : static int file_alloc_security(struct file *file)
267 : : {
268 : : struct file_security_struct *fsec;
269 : : u32 sid = current_sid();
270 : :
271 : : fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
272 [ # # ]: 0 : if (!fsec)
273 : : return -ENOMEM;
274 : :
275 : 0 : fsec->sid = sid;
276 : 0 : fsec->fown_sid = sid;
277 : 0 : file->f_security = fsec;
278 : :
279 : : return 0;
280 : : }
281 : :
282 : : static void file_free_security(struct file *file)
283 : : {
284 : 0 : struct file_security_struct *fsec = file->f_security;
285 : 0 : file->f_security = NULL;
286 : 0 : kfree(fsec);
287 : : }
288 : :
289 : 0 : static int superblock_alloc_security(struct super_block *sb)
290 : : {
291 : : struct superblock_security_struct *sbsec;
292 : :
293 : : sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
294 [ # # ]: 0 : if (!sbsec)
295 : : return -ENOMEM;
296 : :
297 : 0 : mutex_init(&sbsec->lock);
298 : 0 : INIT_LIST_HEAD(&sbsec->isec_head);
299 : 0 : spin_lock_init(&sbsec->isec_lock);
300 : 0 : sbsec->sb = sb;
301 : 0 : sbsec->sid = SECINITSID_UNLABELED;
302 : 0 : sbsec->def_sid = SECINITSID_FILE;
303 : 0 : sbsec->mntpoint_sid = SECINITSID_UNLABELED;
304 : 0 : sb->s_security = sbsec;
305 : :
306 : 0 : return 0;
307 : : }
308 : :
309 : : static void superblock_free_security(struct super_block *sb)
310 : : {
311 : 0 : struct superblock_security_struct *sbsec = sb->s_security;
312 : 0 : sb->s_security = NULL;
313 : 0 : kfree(sbsec);
314 : : }
315 : :
316 : : /* The file system's label must be initialized prior to use. */
317 : :
318 : : static const char *labeling_behaviors[7] = {
319 : : "uses xattr",
320 : : "uses transition SIDs",
321 : : "uses task SIDs",
322 : : "uses genfs_contexts",
323 : : "not configured for labeling",
324 : : "uses mountpoint labeling",
325 : : "uses native labeling",
326 : : };
327 : :
328 : : static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
329 : :
330 : : static inline int inode_doinit(struct inode *inode)
331 : : {
332 : 0 : return inode_doinit_with_dentry(inode, NULL);
333 : : }
334 : :
335 : : enum {
336 : : Opt_error = -1,
337 : : Opt_context = 1,
338 : : Opt_fscontext = 2,
339 : : Opt_defcontext = 3,
340 : : Opt_rootcontext = 4,
341 : : Opt_labelsupport = 5,
342 : : Opt_nextmntopt = 6,
343 : : };
344 : :
345 : : #define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1)
346 : :
347 : : static const match_table_t tokens = {
348 : : {Opt_context, CONTEXT_STR "%s"},
349 : : {Opt_fscontext, FSCONTEXT_STR "%s"},
350 : : {Opt_defcontext, DEFCONTEXT_STR "%s"},
351 : : {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
352 : : {Opt_labelsupport, LABELSUPP_STR},
353 : : {Opt_error, NULL},
354 : : };
355 : :
356 : : #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
357 : :
358 : 0 : static int may_context_mount_sb_relabel(u32 sid,
359 : : struct superblock_security_struct *sbsec,
360 : : const struct cred *cred)
361 : : {
362 : 0 : const struct task_security_struct *tsec = cred->security;
363 : : int rc;
364 : :
365 : 0 : rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
366 : : FILESYSTEM__RELABELFROM, NULL);
367 [ # # ]: 0 : if (rc)
368 : : return rc;
369 : :
370 : 0 : rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
371 : : FILESYSTEM__RELABELTO, NULL);
372 : : return rc;
373 : : }
374 : :
375 : 0 : static int may_context_mount_inode_relabel(u32 sid,
376 : : struct superblock_security_struct *sbsec,
377 : : const struct cred *cred)
378 : : {
379 : 0 : const struct task_security_struct *tsec = cred->security;
380 : : int rc;
381 : 0 : rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
382 : : FILESYSTEM__RELABELFROM, NULL);
383 [ # # ]: 0 : if (rc)
384 : : return rc;
385 : :
386 : 0 : rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
387 : : FILESYSTEM__ASSOCIATE, NULL);
388 : : return rc;
389 : : }
390 : :
391 : 0 : static int selinux_is_sblabel_mnt(struct super_block *sb)
392 : : {
393 : 0 : struct superblock_security_struct *sbsec = sb->s_security;
394 : :
395 [ # # ]: 0 : if (sbsec->behavior == SECURITY_FS_USE_XATTR ||
396 : 0 : sbsec->behavior == SECURITY_FS_USE_TRANS ||
397 : : sbsec->behavior == SECURITY_FS_USE_TASK)
398 : : return 1;
399 : :
400 : : /* Special handling for sysfs. Is genfs but also has setxattr handler*/
401 [ # # ]: 0 : if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
402 : : return 1;
403 : :
404 : : /*
405 : : * Special handling for rootfs. Is genfs but supports
406 : : * setting SELinux context on in-core inodes.
407 : : */
408 [ # # ]: 0 : if (strncmp(sb->s_type->name, "rootfs", sizeof("rootfs")) == 0)
409 : : return 1;
410 : :
411 : : return 0;
412 : : }
413 : :
414 : 0 : static int sb_finish_set_opts(struct super_block *sb)
415 : : {
416 : 0 : struct superblock_security_struct *sbsec = sb->s_security;
417 : 0 : struct dentry *root = sb->s_root;
418 : 0 : struct inode *root_inode = root->d_inode;
419 : : int rc = 0;
420 : :
421 [ # # ]: 0 : if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
422 : : /* Make sure that the xattr handler exists and that no
423 : : error other than -ENODATA is returned by getxattr on
424 : : the root directory. -ENODATA is ok, as this may be
425 : : the first boot of the SELinux kernel before we have
426 : : assigned xattr values to the filesystem. */
427 [ # # ]: 0 : if (!root_inode->i_op->getxattr) {
428 : 0 : printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
429 : 0 : "xattr support\n", sb->s_id, sb->s_type->name);
430 : : rc = -EOPNOTSUPP;
431 : 0 : goto out;
432 : : }
433 : 0 : rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
434 [ # # ]: 0 : if (rc < 0 && rc != -ENODATA) {
435 [ # # ]: 0 : if (rc == -EOPNOTSUPP)
436 : 0 : printk(KERN_WARNING "SELinux: (dev %s, type "
437 : : "%s) has no security xattr handler\n",
438 : 0 : sb->s_id, sb->s_type->name);
439 : : else
440 : 0 : printk(KERN_WARNING "SELinux: (dev %s, type "
441 : 0 : "%s) getxattr errno %d\n", sb->s_id,
442 : 0 : sb->s_type->name, -rc);
443 : : goto out;
444 : : }
445 : : }
446 : :
447 [ # # ]: 0 : if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
448 : 0 : printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
449 : 0 : sb->s_id, sb->s_type->name);
450 : : else
451 : 0 : printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
452 : 0 : sb->s_id, sb->s_type->name,
453 : 0 : labeling_behaviors[sbsec->behavior-1]);
454 : :
455 : 0 : sbsec->flags |= SE_SBINITIALIZED;
456 [ # # ]: 0 : if (selinux_is_sblabel_mnt(sb))
457 : 0 : sbsec->flags |= SBLABEL_MNT;
458 : :
459 : : /* Initialize the root inode. */
460 : 0 : rc = inode_doinit_with_dentry(root_inode, root);
461 : :
462 : : /* Initialize any other inodes associated with the superblock, e.g.
463 : : inodes created prior to initial policy load or inodes created
464 : : during get_sb by a pseudo filesystem that directly
465 : : populates itself. */
466 : : spin_lock(&sbsec->isec_lock);
467 : : next_inode:
468 [ # # ]: 0 : if (!list_empty(&sbsec->isec_head)) {
469 : : struct inode_security_struct *isec =
470 : : list_entry(sbsec->isec_head.next,
471 : : struct inode_security_struct, list);
472 : 0 : struct inode *inode = isec->inode;
473 : : spin_unlock(&sbsec->isec_lock);
474 : 0 : inode = igrab(inode);
475 [ # # ]: 0 : if (inode) {
476 [ # # ]: 0 : if (!IS_PRIVATE(inode))
477 : : inode_doinit(inode);
478 : 0 : iput(inode);
479 : : }
480 : : spin_lock(&sbsec->isec_lock);
481 : 0 : list_del_init(&isec->list);
482 : : goto next_inode;
483 : : }
484 : : spin_unlock(&sbsec->isec_lock);
485 : : out:
486 : 0 : return rc;
487 : : }
488 : :
489 : : /*
490 : : * This function should allow an FS to ask what it's mount security
491 : : * options were so it can use those later for submounts, displaying
492 : : * mount options, or whatever.
493 : : */
494 : 0 : static int selinux_get_mnt_opts(const struct super_block *sb,
495 : : struct security_mnt_opts *opts)
496 : : {
497 : : int rc = 0, i;
498 : 0 : struct superblock_security_struct *sbsec = sb->s_security;
499 : 0 : char *context = NULL;
500 : : u32 len;
501 : : char tmp;
502 : :
503 : : security_init_mnt_opts(opts);
504 : :
505 [ # # ]: 0 : if (!(sbsec->flags & SE_SBINITIALIZED))
506 : : return -EINVAL;
507 : :
508 [ # # ]: 0 : if (!ss_initialized)
509 : : return -EINVAL;
510 : :
511 : : /* make sure we always check enough bits to cover the mask */
512 : : BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
513 : :
514 : 0 : tmp = sbsec->flags & SE_MNTMASK;
515 : : /* count the number of mount options for this sb */
516 [ # # ]: 0 : for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
517 [ # # ]: 0 : if (tmp & 0x01)
518 : 0 : opts->num_mnt_opts++;
519 : 0 : tmp >>= 1;
520 : : }
521 : : /* Check if the Label support flag is set */
522 [ # # ]: 0 : if (sbsec->flags & SBLABEL_MNT)
523 : 0 : opts->num_mnt_opts++;
524 : :
525 : 0 : opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
526 [ # # ]: 0 : if (!opts->mnt_opts) {
527 : : rc = -ENOMEM;
528 : : goto out_free;
529 : : }
530 : :
531 : 0 : opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
532 [ # # ]: 0 : if (!opts->mnt_opts_flags) {
533 : : rc = -ENOMEM;
534 : : goto out_free;
535 : : }
536 : :
537 : : i = 0;
538 [ # # ]: 0 : if (sbsec->flags & FSCONTEXT_MNT) {
539 : 0 : rc = security_sid_to_context(sbsec->sid, &context, &len);
540 [ # # ]: 0 : if (rc)
541 : : goto out_free;
542 : 0 : opts->mnt_opts[i] = context;
543 : 0 : opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
544 : : }
545 [ # # ]: 0 : if (sbsec->flags & CONTEXT_MNT) {
546 : 0 : rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
547 [ # # ]: 0 : if (rc)
548 : : goto out_free;
549 : 0 : opts->mnt_opts[i] = context;
550 : 0 : opts->mnt_opts_flags[i++] = CONTEXT_MNT;
551 : : }
552 [ # # ]: 0 : if (sbsec->flags & DEFCONTEXT_MNT) {
553 : 0 : rc = security_sid_to_context(sbsec->def_sid, &context, &len);
554 [ # # ]: 0 : if (rc)
555 : : goto out_free;
556 : 0 : opts->mnt_opts[i] = context;
557 : 0 : opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
558 : : }
559 [ # # ]: 0 : if (sbsec->flags & ROOTCONTEXT_MNT) {
560 : 0 : struct inode *root = sbsec->sb->s_root->d_inode;
561 : 0 : struct inode_security_struct *isec = root->i_security;
562 : :
563 : 0 : rc = security_sid_to_context(isec->sid, &context, &len);
564 [ # # ]: 0 : if (rc)
565 : : goto out_free;
566 : 0 : opts->mnt_opts[i] = context;
567 : 0 : opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
568 : : }
569 [ # # ]: 0 : if (sbsec->flags & SBLABEL_MNT) {
570 : 0 : opts->mnt_opts[i] = NULL;
571 : 0 : opts->mnt_opts_flags[i++] = SBLABEL_MNT;
572 : : }
573 : :
574 [ # # ]: 0 : BUG_ON(i != opts->num_mnt_opts);
575 : :
576 : : return 0;
577 : :
578 : : out_free:
579 : : security_free_mnt_opts(opts);
580 : : return rc;
581 : : }
582 : :
583 : : static int bad_option(struct superblock_security_struct *sbsec, char flag,
584 : : u32 old_sid, u32 new_sid)
585 : : {
586 : 0 : char mnt_flags = sbsec->flags & SE_MNTMASK;
587 : :
588 : : /* check if the old mount command had the same options */
589 [ # # ][ # # ]: 0 : if (sbsec->flags & SE_SBINITIALIZED)
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
590 [ # # ][ # # ]: 0 : if (!(sbsec->flags & flag) ||
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
591 : : (old_sid != new_sid))
592 : : return 1;
593 : :
594 : : /* check if we were passed the same options twice,
595 : : * aka someone passed context=a,context=b
596 : : */
597 [ # # ][ # # ]: 0 : if (!(sbsec->flags & SE_SBINITIALIZED))
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
598 [ # # ][ # # ]: 0 : if (mnt_flags & flag)
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
599 : : return 1;
600 : : return 0;
601 : : }
602 : :
603 : : /*
604 : : * Allow filesystems with binary mount data to explicitly set mount point
605 : : * labeling information.
606 : : */
607 : 0 : static int selinux_set_mnt_opts(struct super_block *sb,
608 : : struct security_mnt_opts *opts,
609 : : unsigned long kern_flags,
610 : : unsigned long *set_kern_flags)
611 : : {
612 : 0 : const struct cred *cred = current_cred();
613 : : int rc = 0, i;
614 : 0 : struct superblock_security_struct *sbsec = sb->s_security;
615 : 0 : const char *name = sb->s_type->name;
616 : 0 : struct inode *inode = sbsec->sb->s_root->d_inode;
617 : 0 : struct inode_security_struct *root_isec = inode->i_security;
618 : : u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
619 : : u32 defcontext_sid = 0;
620 : 0 : char **mount_options = opts->mnt_opts;
621 : 0 : int *flags = opts->mnt_opts_flags;
622 : 0 : int num_opts = opts->num_mnt_opts;
623 : :
624 : 0 : mutex_lock(&sbsec->lock);
625 : :
626 [ # # ]: 0 : if (!ss_initialized) {
627 [ # # ]: 0 : if (!num_opts) {
628 : : /* Defer initialization until selinux_complete_init,
629 : : after the initial policy is loaded and the security
630 : : server is ready to handle calls. */
631 : : goto out;
632 : : }
633 : : rc = -EINVAL;
634 : 0 : printk(KERN_WARNING "SELinux: Unable to set superblock options "
635 : : "before the security server is initialized\n");
636 : 0 : goto out;
637 : : }
638 [ # # ]: 0 : if (kern_flags && !set_kern_flags) {
639 : : /* Specifying internal flags without providing a place to
640 : : * place the results is not allowed */
641 : : rc = -EINVAL;
642 : : goto out;
643 : : }
644 : :
645 : : /*
646 : : * Binary mount data FS will come through this function twice. Once
647 : : * from an explicit call and once from the generic calls from the vfs.
648 : : * Since the generic VFS calls will not contain any security mount data
649 : : * we need to skip the double mount verification.
650 : : *
651 : : * This does open a hole in which we will not notice if the first
652 : : * mount using this sb set explict options and a second mount using
653 : : * this sb does not set any security options. (The first options
654 : : * will be used for both mounts)
655 : : */
656 [ # # ][ # # ]: 0 : if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
657 [ # # ]: 0 : && (num_opts == 0))
658 : : goto out;
659 : :
660 : : /*
661 : : * parse the mount options, check if they are valid sids.
662 : : * also check if someone is trying to mount the same sb more
663 : : * than once with different security options.
664 : : */
665 [ # # ]: 0 : for (i = 0; i < num_opts; i++) {
666 : : u32 sid;
667 : :
668 [ # # ]: 0 : if (flags[i] == SBLABEL_MNT)
669 : 0 : continue;
670 : 0 : rc = security_context_to_sid(mount_options[i],
671 : 0 : strlen(mount_options[i]), &sid, GFP_KERNEL);
672 [ # # ]: 0 : if (rc) {
673 : 0 : printk(KERN_WARNING "SELinux: security_context_to_sid"
674 : : "(%s) failed for (dev %s, type %s) errno=%d\n",
675 : 0 : mount_options[i], sb->s_id, name, rc);
676 : 0 : goto out;
677 : : }
678 [ # # # # : 0 : switch (flags[i]) {
# ]
679 : : case FSCONTEXT_MNT:
680 : 0 : fscontext_sid = sid;
681 : :
682 [ # # ]: 0 : if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
683 : : fscontext_sid))
684 : : goto out_double_mount;
685 : :
686 : 0 : sbsec->flags |= FSCONTEXT_MNT;
687 : 0 : break;
688 : : case CONTEXT_MNT:
689 : 0 : context_sid = sid;
690 : :
691 [ # # ]: 0 : if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
692 : : context_sid))
693 : : goto out_double_mount;
694 : :
695 : 0 : sbsec->flags |= CONTEXT_MNT;
696 : 0 : break;
697 : : case ROOTCONTEXT_MNT:
698 : 0 : rootcontext_sid = sid;
699 : :
700 [ # # ]: 0 : if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
701 : : rootcontext_sid))
702 : : goto out_double_mount;
703 : :
704 : 0 : sbsec->flags |= ROOTCONTEXT_MNT;
705 : :
706 : 0 : break;
707 : : case DEFCONTEXT_MNT:
708 : 0 : defcontext_sid = sid;
709 : :
710 [ # # ]: 0 : if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
711 : : defcontext_sid))
712 : : goto out_double_mount;
713 : :
714 : 0 : sbsec->flags |= DEFCONTEXT_MNT;
715 : :
716 : 0 : break;
717 : : default:
718 : : rc = -EINVAL;
719 : : goto out;
720 : : }
721 : : }
722 : :
723 [ # # ]: 0 : if (sbsec->flags & SE_SBINITIALIZED) {
724 : : /* previously mounted with options, but not on this attempt? */
725 [ # # ][ # # ]: 0 : if ((sbsec->flags & SE_MNTMASK) && !num_opts)
726 : : goto out_double_mount;
727 : : rc = 0;
728 : : goto out;
729 : : }
730 : :
731 [ # # ]: 0 : if (strcmp(sb->s_type->name, "proc") == 0)
732 : 0 : sbsec->flags |= SE_SBPROC;
733 : :
734 [ # # ]: 0 : if (!sbsec->behavior) {
735 : : /*
736 : : * Determine the labeling behavior to use for this
737 : : * filesystem type.
738 : : */
739 : 0 : rc = security_fs_use(sb);
740 [ # # ]: 0 : if (rc) {
741 : 0 : printk(KERN_WARNING
742 : : "%s: security_fs_use(%s) returned %d\n",
743 : 0 : __func__, sb->s_type->name, rc);
744 : 0 : goto out;
745 : : }
746 : : }
747 : : /* sets the context of the superblock for the fs being mounted. */
748 [ # # ]: 0 : if (fscontext_sid) {
749 : 0 : rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
750 [ # # ]: 0 : if (rc)
751 : : goto out;
752 : :
753 : 0 : sbsec->sid = fscontext_sid;
754 : : }
755 : :
756 : : /*
757 : : * Switch to using mount point labeling behavior.
758 : : * sets the label used on all file below the mountpoint, and will set
759 : : * the superblock context if not already set.
760 : : */
761 [ # # ][ # # ]: 0 : if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
762 : 0 : sbsec->behavior = SECURITY_FS_USE_NATIVE;
763 : 0 : *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
764 : : }
765 : :
766 [ # # ]: 0 : if (context_sid) {
767 [ # # ]: 0 : if (!fscontext_sid) {
768 : 0 : rc = may_context_mount_sb_relabel(context_sid, sbsec,
769 : : cred);
770 [ # # ]: 0 : if (rc)
771 : : goto out;
772 : 0 : sbsec->sid = context_sid;
773 : : } else {
774 : 0 : rc = may_context_mount_inode_relabel(context_sid, sbsec,
775 : : cred);
776 [ # # ]: 0 : if (rc)
777 : : goto out;
778 : : }
779 [ # # ]: 0 : if (!rootcontext_sid)
780 : : rootcontext_sid = context_sid;
781 : :
782 : 0 : sbsec->mntpoint_sid = context_sid;
783 : 0 : sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
784 : : }
785 : :
786 [ # # ]: 0 : if (rootcontext_sid) {
787 : 0 : rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
788 : : cred);
789 [ # # ]: 0 : if (rc)
790 : : goto out;
791 : :
792 : 0 : root_isec->sid = rootcontext_sid;
793 : 0 : root_isec->initialized = 1;
794 : : }
795 : :
796 [ # # ]: 0 : if (defcontext_sid) {
797 [ # # ]: 0 : if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
798 : : sbsec->behavior != SECURITY_FS_USE_NATIVE) {
799 : : rc = -EINVAL;
800 : 0 : printk(KERN_WARNING "SELinux: defcontext option is "
801 : : "invalid for this filesystem type\n");
802 : 0 : goto out;
803 : : }
804 : :
805 [ # # ]: 0 : if (defcontext_sid != sbsec->def_sid) {
806 : 0 : rc = may_context_mount_inode_relabel(defcontext_sid,
807 : : sbsec, cred);
808 [ # # ]: 0 : if (rc)
809 : : goto out;
810 : : }
811 : :
812 : 0 : sbsec->def_sid = defcontext_sid;
813 : : }
814 : :
815 : 0 : rc = sb_finish_set_opts(sb);
816 : : out:
817 : 0 : mutex_unlock(&sbsec->lock);
818 : 0 : return rc;
819 : : out_double_mount:
820 : : rc = -EINVAL;
821 : 0 : printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
822 : 0 : "security settings for (dev %s, type %s)\n", sb->s_id, name);
823 : 0 : goto out;
824 : : }
825 : :
826 : 0 : static int selinux_cmp_sb_context(const struct super_block *oldsb,
827 : : const struct super_block *newsb)
828 : : {
829 : 0 : struct superblock_security_struct *old = oldsb->s_security;
830 : 0 : struct superblock_security_struct *new = newsb->s_security;
831 : 0 : char oldflags = old->flags & SE_MNTMASK;
832 : 0 : char newflags = new->flags & SE_MNTMASK;
833 : :
834 [ # # ]: 0 : if (oldflags != newflags)
835 : : goto mismatch;
836 [ # # ][ # # ]: 0 : if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
837 : : goto mismatch;
838 [ # # ][ # # ]: 0 : if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
839 : : goto mismatch;
840 [ # # ][ # # ]: 0 : if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
841 : : goto mismatch;
842 [ # # ]: 0 : if (oldflags & ROOTCONTEXT_MNT) {
843 : 0 : struct inode_security_struct *oldroot = oldsb->s_root->d_inode->i_security;
844 : 0 : struct inode_security_struct *newroot = newsb->s_root->d_inode->i_security;
845 [ # # ]: 0 : if (oldroot->sid != newroot->sid)
846 : : goto mismatch;
847 : : }
848 : : return 0;
849 : : mismatch:
850 : 0 : printk(KERN_WARNING "SELinux: mount invalid. Same superblock, "
851 : : "different security settings for (dev %s, "
852 : 0 : "type %s)\n", newsb->s_id, newsb->s_type->name);
853 : : return -EBUSY;
854 : : }
855 : :
856 : 0 : static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
857 : : struct super_block *newsb)
858 : : {
859 : 0 : const struct superblock_security_struct *oldsbsec = oldsb->s_security;
860 : 0 : struct superblock_security_struct *newsbsec = newsb->s_security;
861 : :
862 : 0 : int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
863 : 0 : int set_context = (oldsbsec->flags & CONTEXT_MNT);
864 : 0 : int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
865 : :
866 : : /*
867 : : * if the parent was able to be mounted it clearly had no special lsm
868 : : * mount options. thus we can safely deal with this superblock later
869 : : */
870 [ # # ]: 0 : if (!ss_initialized)
871 : : return 0;
872 : :
873 : : /* how can we clone if the old one wasn't set up?? */
874 [ # # ]: 0 : BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
875 : :
876 : : /* if fs is reusing a sb, make sure that the contexts match */
877 [ # # ]: 0 : if (newsbsec->flags & SE_SBINITIALIZED)
878 : 0 : return selinux_cmp_sb_context(oldsb, newsb);
879 : :
880 : 0 : mutex_lock(&newsbsec->lock);
881 : :
882 : 0 : newsbsec->flags = oldsbsec->flags;
883 : :
884 : 0 : newsbsec->sid = oldsbsec->sid;
885 : 0 : newsbsec->def_sid = oldsbsec->def_sid;
886 : 0 : newsbsec->behavior = oldsbsec->behavior;
887 : :
888 [ # # ]: 0 : if (set_context) {
889 : 0 : u32 sid = oldsbsec->mntpoint_sid;
890 : :
891 [ # # ]: 0 : if (!set_fscontext)
892 : 0 : newsbsec->sid = sid;
893 [ # # ]: 0 : if (!set_rootcontext) {
894 : 0 : struct inode *newinode = newsb->s_root->d_inode;
895 : 0 : struct inode_security_struct *newisec = newinode->i_security;
896 : 0 : newisec->sid = sid;
897 : : }
898 : 0 : newsbsec->mntpoint_sid = sid;
899 : : }
900 [ # # ]: 0 : if (set_rootcontext) {
901 : 0 : const struct inode *oldinode = oldsb->s_root->d_inode;
902 : 0 : const struct inode_security_struct *oldisec = oldinode->i_security;
903 : 0 : struct inode *newinode = newsb->s_root->d_inode;
904 : 0 : struct inode_security_struct *newisec = newinode->i_security;
905 : :
906 : 0 : newisec->sid = oldisec->sid;
907 : : }
908 : :
909 : 0 : sb_finish_set_opts(newsb);
910 : 0 : mutex_unlock(&newsbsec->lock);
911 : 0 : return 0;
912 : : }
913 : :
914 : 0 : static int selinux_parse_opts_str(char *options,
915 : : struct security_mnt_opts *opts)
916 : : {
917 : : char *p;
918 : : char *context = NULL, *defcontext = NULL;
919 : : char *fscontext = NULL, *rootcontext = NULL;
920 : : int rc, num_mnt_opts = 0;
921 : :
922 : 0 : opts->num_mnt_opts = 0;
923 : :
924 : : /* Standard string-based options. */
925 [ # # ]: 0 : while ((p = strsep(&options, "|")) != NULL) {
926 : : int token;
927 : : substring_t args[MAX_OPT_ARGS];
928 : :
929 [ # # ]: 0 : if (!*p)
930 : 0 : continue;
931 : :
932 : 0 : token = match_token(p, tokens, args);
933 : :
934 [ # # # # : 0 : switch (token) {
# # ]
935 : : case Opt_context:
936 [ # # ]: 0 : if (context || defcontext) {
937 : : rc = -EINVAL;
938 : 0 : printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
939 : 0 : goto out_err;
940 : : }
941 : 0 : context = match_strdup(&args[0]);
942 [ # # ]: 0 : if (!context) {
943 : : rc = -ENOMEM;
944 : : goto out_err;
945 : : }
946 : : break;
947 : :
948 : : case Opt_fscontext:
949 [ # # ]: 0 : if (fscontext) {
950 : : rc = -EINVAL;
951 : 0 : printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
952 : 0 : goto out_err;
953 : : }
954 : 0 : fscontext = match_strdup(&args[0]);
955 [ # # ]: 0 : if (!fscontext) {
956 : : rc = -ENOMEM;
957 : : goto out_err;
958 : : }
959 : : break;
960 : :
961 : : case Opt_rootcontext:
962 [ # # ]: 0 : if (rootcontext) {
963 : : rc = -EINVAL;
964 : 0 : printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
965 : 0 : goto out_err;
966 : : }
967 : 0 : rootcontext = match_strdup(&args[0]);
968 [ # # ]: 0 : if (!rootcontext) {
969 : : rc = -ENOMEM;
970 : : goto out_err;
971 : : }
972 : : break;
973 : :
974 : : case Opt_defcontext:
975 [ # # ]: 0 : if (context || defcontext) {
976 : : rc = -EINVAL;
977 : 0 : printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
978 : 0 : goto out_err;
979 : : }
980 : 0 : defcontext = match_strdup(&args[0]);
981 [ # # ]: 0 : if (!defcontext) {
982 : : rc = -ENOMEM;
983 : : goto out_err;
984 : : }
985 : : break;
986 : : case Opt_labelsupport:
987 : : break;
988 : : default:
989 : : rc = -EINVAL;
990 : 0 : printk(KERN_WARNING "SELinux: unknown mount option\n");
991 : 0 : goto out_err;
992 : :
993 : : }
994 : : }
995 : :
996 : : rc = -ENOMEM;
997 : 0 : opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
998 [ # # ]: 0 : if (!opts->mnt_opts)
999 : : goto out_err;
1000 : :
1001 : 0 : opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
1002 [ # # ]: 0 : if (!opts->mnt_opts_flags) {
1003 : 0 : kfree(opts->mnt_opts);
1004 : 0 : goto out_err;
1005 : : }
1006 : :
1007 [ # # ]: 0 : if (fscontext) {
1008 : 0 : opts->mnt_opts[num_mnt_opts] = fscontext;
1009 : 0 : opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
1010 : : }
1011 [ # # ]: 0 : if (context) {
1012 : 0 : opts->mnt_opts[num_mnt_opts] = context;
1013 : 0 : opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
1014 : : }
1015 [ # # ]: 0 : if (rootcontext) {
1016 : 0 : opts->mnt_opts[num_mnt_opts] = rootcontext;
1017 : 0 : opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
1018 : : }
1019 [ # # ]: 0 : if (defcontext) {
1020 : 0 : opts->mnt_opts[num_mnt_opts] = defcontext;
1021 : 0 : opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
1022 : : }
1023 : :
1024 : 0 : opts->num_mnt_opts = num_mnt_opts;
1025 : 0 : return 0;
1026 : :
1027 : : out_err:
1028 : 0 : kfree(context);
1029 : 0 : kfree(defcontext);
1030 : 0 : kfree(fscontext);
1031 : 0 : kfree(rootcontext);
1032 : 0 : return rc;
1033 : : }
1034 : : /*
1035 : : * string mount options parsing and call set the sbsec
1036 : : */
1037 : 0 : static int superblock_doinit(struct super_block *sb, void *data)
1038 : : {
1039 : : int rc = 0;
1040 : : char *options = data;
1041 : : struct security_mnt_opts opts;
1042 : :
1043 : : security_init_mnt_opts(&opts);
1044 : :
1045 [ # # ]: 0 : if (!data)
1046 : : goto out;
1047 : :
1048 [ # # ]: 0 : BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
1049 : :
1050 : 0 : rc = selinux_parse_opts_str(options, &opts);
1051 [ # # ]: 0 : if (rc)
1052 : : goto out_err;
1053 : :
1054 : : out:
1055 : 0 : rc = selinux_set_mnt_opts(sb, &opts, 0, NULL);
1056 : :
1057 : : out_err:
1058 : : security_free_mnt_opts(&opts);
1059 : 0 : return rc;
1060 : : }
1061 : :
1062 : 0 : static void selinux_write_opts(struct seq_file *m,
1063 : : struct security_mnt_opts *opts)
1064 : : {
1065 : : int i;
1066 : : char *prefix;
1067 : :
1068 [ # # ]: 0 : for (i = 0; i < opts->num_mnt_opts; i++) {
1069 : : char *has_comma;
1070 : :
1071 [ # # ]: 0 : if (opts->mnt_opts[i])
1072 : 0 : has_comma = strchr(opts->mnt_opts[i], ',');
1073 : : else
1074 : : has_comma = NULL;
1075 : :
1076 [ # # # # : 0 : switch (opts->mnt_opts_flags[i]) {
# # ]
1077 : : case CONTEXT_MNT:
1078 : : prefix = CONTEXT_STR;
1079 : : break;
1080 : : case FSCONTEXT_MNT:
1081 : : prefix = FSCONTEXT_STR;
1082 : 0 : break;
1083 : : case ROOTCONTEXT_MNT:
1084 : : prefix = ROOTCONTEXT_STR;
1085 : 0 : break;
1086 : : case DEFCONTEXT_MNT:
1087 : : prefix = DEFCONTEXT_STR;
1088 : 0 : break;
1089 : : case SBLABEL_MNT:
1090 : 0 : seq_putc(m, ',');
1091 : 0 : seq_puts(m, LABELSUPP_STR);
1092 : 0 : continue;
1093 : : default:
1094 : 0 : BUG();
1095 : : return;
1096 : : };
1097 : : /* we need a comma before each option */
1098 : 0 : seq_putc(m, ',');
1099 : 0 : seq_puts(m, prefix);
1100 [ # # ]: 0 : if (has_comma)
1101 : 0 : seq_putc(m, '\"');
1102 : 0 : seq_puts(m, opts->mnt_opts[i]);
1103 [ # # ]: 0 : if (has_comma)
1104 : 0 : seq_putc(m, '\"');
1105 : : }
1106 : 0 : }
1107 : :
1108 : 0 : static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1109 : : {
1110 : : struct security_mnt_opts opts;
1111 : : int rc;
1112 : :
1113 : 0 : rc = selinux_get_mnt_opts(sb, &opts);
1114 [ # # ]: 0 : if (rc) {
1115 : : /* before policy load we may get EINVAL, don't show anything */
1116 [ # # ]: 0 : if (rc == -EINVAL)
1117 : : rc = 0;
1118 : 0 : return rc;
1119 : : }
1120 : :
1121 : 0 : selinux_write_opts(m, &opts);
1122 : :
1123 : : security_free_mnt_opts(&opts);
1124 : :
1125 : 0 : return rc;
1126 : : }
1127 : :
1128 : : static inline u16 inode_mode_to_security_class(umode_t mode)
1129 : : {
1130 [ # # # # : 0 : switch (mode & S_IFMT) {
# # # # ]
[ # # # #
# # # # ]
[ # # # #
# # # # ]
[ # # # #
# # # # ]
[ # # # #
# # # # ]
[ # # # #
# # # # ]
[ # # # #
# # # # ]
[ # # # #
# # # # ]
[ # # # #
# # # # ]
1131 : : case S_IFSOCK:
1132 : : return SECCLASS_SOCK_FILE;
1133 : : case S_IFLNK:
1134 : : return SECCLASS_LNK_FILE;
1135 : : case S_IFREG:
1136 : : return SECCLASS_FILE;
1137 : : case S_IFBLK:
1138 : : return SECCLASS_BLK_FILE;
1139 : : case S_IFDIR:
1140 : : return SECCLASS_DIR;
1141 : : case S_IFCHR:
1142 : : return SECCLASS_CHR_FILE;
1143 : : case S_IFIFO:
1144 : : return SECCLASS_FIFO_FILE;
1145 : :
1146 : : }
1147 : :
1148 : : return SECCLASS_FILE;
1149 : : }
1150 : :
1151 : : static inline int default_protocol_stream(int protocol)
1152 : : {
1153 : 0 : return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1154 : : }
1155 : :
1156 : : static inline int default_protocol_dgram(int protocol)
1157 : : {
1158 : 0 : return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1159 : : }
1160 : :
1161 : : static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1162 : : {
1163 [ # # # # : 0 : switch (family) {
# # # ][ #
# # # # #
# ]
1164 : : case PF_UNIX:
1165 [ # # # ]: 0 : switch (type) {
[ # # # ]
1166 : : case SOCK_STREAM:
1167 : : case SOCK_SEQPACKET:
1168 : : return SECCLASS_UNIX_STREAM_SOCKET;
1169 : : case SOCK_DGRAM:
1170 : : return SECCLASS_UNIX_DGRAM_SOCKET;
1171 : : }
1172 : : break;
1173 : : case PF_INET:
1174 : : case PF_INET6:
1175 [ # # # # ]: 0 : switch (type) {
[ # # # # ]
1176 : : case SOCK_STREAM:
1177 [ # # ][ # # ]: 0 : if (default_protocol_stream(protocol))
1178 : : return SECCLASS_TCP_SOCKET;
1179 : : else
1180 : : return SECCLASS_RAWIP_SOCKET;
1181 : : case SOCK_DGRAM:
1182 [ # # ][ # # ]: 0 : if (default_protocol_dgram(protocol))
[ # # ][ # # ]
1183 : : return SECCLASS_UDP_SOCKET;
1184 : : else
1185 : : return SECCLASS_RAWIP_SOCKET;
1186 : : case SOCK_DCCP:
1187 : : return SECCLASS_DCCP_SOCKET;
1188 : : default:
1189 : : return SECCLASS_RAWIP_SOCKET;
1190 : : }
1191 : : break;
1192 : : case PF_NETLINK:
1193 : : switch (protocol) {
1194 : : case NETLINK_ROUTE:
1195 : : return SECCLASS_NETLINK_ROUTE_SOCKET;
1196 : : case NETLINK_FIREWALL:
1197 : : return SECCLASS_NETLINK_FIREWALL_SOCKET;
1198 : : case NETLINK_SOCK_DIAG:
1199 : : return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1200 : : case NETLINK_NFLOG:
1201 : : return SECCLASS_NETLINK_NFLOG_SOCKET;
1202 : : case NETLINK_XFRM:
1203 : : return SECCLASS_NETLINK_XFRM_SOCKET;
1204 : : case NETLINK_SELINUX:
1205 : : return SECCLASS_NETLINK_SELINUX_SOCKET;
1206 : : case NETLINK_AUDIT:
1207 : : return SECCLASS_NETLINK_AUDIT_SOCKET;
1208 : : case NETLINK_IP6_FW:
1209 : : return SECCLASS_NETLINK_IP6FW_SOCKET;
1210 : : case NETLINK_DNRTMSG:
1211 : : return SECCLASS_NETLINK_DNRT_SOCKET;
1212 : : case NETLINK_KOBJECT_UEVENT:
1213 : : return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
1214 : : default:
1215 : : return SECCLASS_NETLINK_SOCKET;
1216 : : }
1217 : : case PF_PACKET:
1218 : : return SECCLASS_PACKET_SOCKET;
1219 : : case PF_KEY:
1220 : : return SECCLASS_KEY_SOCKET;
1221 : : case PF_APPLETALK:
1222 : : return SECCLASS_APPLETALK_SOCKET;
1223 : : }
1224 : :
1225 : : return SECCLASS_SOCKET;
1226 : : }
1227 : :
1228 : : #ifdef CONFIG_PROC_FS
1229 : 0 : static int selinux_proc_get_sid(struct dentry *dentry,
1230 : : u16 tclass,
1231 : : u32 *sid)
1232 : : {
1233 : : int rc;
1234 : : char *buffer, *path;
1235 : :
1236 : 0 : buffer = (char *)__get_free_page(GFP_KERNEL);
1237 [ # # ]: 0 : if (!buffer)
1238 : : return -ENOMEM;
1239 : :
1240 : 0 : path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1241 [ # # ]: 0 : if (IS_ERR(path))
1242 : : rc = PTR_ERR(path);
1243 : : else {
1244 : : /* each process gets a /proc/PID/ entry. Strip off the
1245 : : * PID part to get a valid selinux labeling.
1246 : : * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1247 [ # # ]: 0 : while (path[1] >= '0' && path[1] <= '9') {
1248 : 0 : path[1] = '/';
1249 : 0 : path++;
1250 : : }
1251 : 0 : rc = security_genfs_sid("proc", path, tclass, sid);
1252 : : }
1253 : 0 : free_page((unsigned long)buffer);
1254 : 0 : return rc;
1255 : : }
1256 : : #else
1257 : : static int selinux_proc_get_sid(struct dentry *dentry,
1258 : : u16 tclass,
1259 : : u32 *sid)
1260 : : {
1261 : : return -EINVAL;
1262 : : }
1263 : : #endif
1264 : :
1265 : : /* The inode's security attributes must be initialized before first use. */
1266 : 0 : static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1267 : : {
1268 : : struct superblock_security_struct *sbsec = NULL;
1269 : 0 : struct inode_security_struct *isec = inode->i_security;
1270 : : u32 sid;
1271 : : struct dentry *dentry;
1272 : : #define INITCONTEXTLEN 255
1273 : : char *context = NULL;
1274 : : unsigned len = 0;
1275 : : int rc = 0;
1276 : :
1277 [ # # ]: 0 : if (isec->initialized)
1278 : : goto out;
1279 : :
1280 : 0 : mutex_lock(&isec->lock);
1281 [ # # ]: 0 : if (isec->initialized)
1282 : : goto out_unlock;
1283 : :
1284 : 0 : sbsec = inode->i_sb->s_security;
1285 [ # # ]: 0 : if (!(sbsec->flags & SE_SBINITIALIZED)) {
1286 : : /* Defer initialization until selinux_complete_init,
1287 : : after the initial policy is loaded and the security
1288 : : server is ready to handle calls. */
1289 : : spin_lock(&sbsec->isec_lock);
1290 [ # # ]: 0 : if (list_empty(&isec->list))
1291 : 0 : list_add(&isec->list, &sbsec->isec_head);
1292 : : spin_unlock(&sbsec->isec_lock);
1293 : : goto out_unlock;
1294 : : }
1295 : :
1296 [ # # # # : 0 : switch (sbsec->behavior) {
# # ]
1297 : : case SECURITY_FS_USE_NATIVE:
1298 : : break;
1299 : : case SECURITY_FS_USE_XATTR:
1300 [ # # ]: 0 : if (!inode->i_op->getxattr) {
1301 : 0 : isec->sid = sbsec->def_sid;
1302 : 0 : break;
1303 : : }
1304 : :
1305 : : /* Need a dentry, since the xattr API requires one.
1306 : : Life would be simpler if we could just pass the inode. */
1307 [ # # ]: 0 : if (opt_dentry) {
1308 : : /* Called from d_instantiate or d_splice_alias. */
1309 : : dentry = dget(opt_dentry);
1310 : : } else {
1311 : : /* Called from selinux_complete_init, try to find a dentry. */
1312 : 0 : dentry = d_find_alias(inode);
1313 : : }
1314 [ # # ]: 0 : if (!dentry) {
1315 : : /*
1316 : : * this is can be hit on boot when a file is accessed
1317 : : * before the policy is loaded. When we load policy we
1318 : : * may find inodes that have no dentry on the
1319 : : * sbsec->isec_head list. No reason to complain as these
1320 : : * will get fixed up the next time we go through
1321 : : * inode_doinit with a dentry, before these inodes could
1322 : : * be used again by userspace.
1323 : : */
1324 : : goto out_unlock;
1325 : : }
1326 : :
1327 : : len = INITCONTEXTLEN;
1328 : : context = kmalloc(len+1, GFP_NOFS);
1329 [ # # ]: 0 : if (!context) {
1330 : : rc = -ENOMEM;
1331 : 0 : dput(dentry);
1332 : 0 : goto out_unlock;
1333 : : }
1334 : 0 : context[len] = '\0';
1335 : 0 : rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1336 : : context, len);
1337 [ # # ]: 0 : if (rc == -ERANGE) {
1338 : 0 : kfree(context);
1339 : :
1340 : : /* Need a larger buffer. Query for the right size. */
1341 : 0 : rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1342 : : NULL, 0);
1343 [ # # ]: 0 : if (rc < 0) {
1344 : 0 : dput(dentry);
1345 : 0 : goto out_unlock;
1346 : : }
1347 : 0 : len = rc;
1348 : 0 : context = kmalloc(len+1, GFP_NOFS);
1349 [ # # ]: 0 : if (!context) {
1350 : : rc = -ENOMEM;
1351 : 0 : dput(dentry);
1352 : 0 : goto out_unlock;
1353 : : }
1354 : 0 : context[len] = '\0';
1355 : 0 : rc = inode->i_op->getxattr(dentry,
1356 : : XATTR_NAME_SELINUX,
1357 : : context, len);
1358 : : }
1359 : 0 : dput(dentry);
1360 [ # # ]: 0 : if (rc < 0) {
1361 [ # # ]: 0 : if (rc != -ENODATA) {
1362 : 0 : printk(KERN_WARNING "SELinux: %s: getxattr returned "
1363 : : "%d for dev=%s ino=%ld\n", __func__,
1364 : 0 : -rc, inode->i_sb->s_id, inode->i_ino);
1365 : 0 : kfree(context);
1366 : 0 : goto out_unlock;
1367 : : }
1368 : : /* Map ENODATA to the default file SID */
1369 : 0 : sid = sbsec->def_sid;
1370 : : rc = 0;
1371 : : } else {
1372 : 0 : rc = security_context_to_sid_default(context, rc, &sid,
1373 : : sbsec->def_sid,
1374 : : GFP_NOFS);
1375 [ # # ]: 0 : if (rc) {
1376 : 0 : char *dev = inode->i_sb->s_id;
1377 : 0 : unsigned long ino = inode->i_ino;
1378 : :
1379 [ # # ]: 0 : if (rc == -EINVAL) {
1380 [ # # ]: 0 : if (printk_ratelimit())
1381 : 0 : printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1382 : : "context=%s. This indicates you may need to relabel the inode or the "
1383 : : "filesystem in question.\n", ino, dev, context);
1384 : : } else {
1385 : 0 : printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1386 : : "returned %d for dev=%s ino=%ld\n",
1387 : : __func__, context, -rc, dev, ino);
1388 : : }
1389 : 0 : kfree(context);
1390 : : /* Leave with the unlabeled SID */
1391 : : rc = 0;
1392 : 0 : break;
1393 : : }
1394 : : }
1395 : 0 : kfree(context);
1396 : 0 : isec->sid = sid;
1397 : 0 : break;
1398 : : case SECURITY_FS_USE_TASK:
1399 : 0 : isec->sid = isec->task_sid;
1400 : 0 : break;
1401 : : case SECURITY_FS_USE_TRANS:
1402 : : /* Default to the fs SID. */
1403 : 0 : isec->sid = sbsec->sid;
1404 : :
1405 : : /* Try to obtain a transition SID. */
1406 : 0 : isec->sclass = inode_mode_to_security_class(inode->i_mode);
1407 : 0 : rc = security_transition_sid(isec->task_sid, sbsec->sid,
1408 : : isec->sclass, NULL, &sid);
1409 [ # # ]: 0 : if (rc)
1410 : : goto out_unlock;
1411 : 0 : isec->sid = sid;
1412 : 0 : break;
1413 : : case SECURITY_FS_USE_MNTPOINT:
1414 : 0 : isec->sid = sbsec->mntpoint_sid;
1415 : 0 : break;
1416 : : default:
1417 : : /* Default to the fs superblock SID. */
1418 : 0 : isec->sid = sbsec->sid;
1419 : :
1420 [ # # ][ # # ]: 0 : if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
1421 [ # # ]: 0 : if (opt_dentry) {
1422 : 0 : isec->sclass = inode_mode_to_security_class(inode->i_mode);
1423 : 0 : rc = selinux_proc_get_sid(opt_dentry,
1424 : : isec->sclass,
1425 : : &sid);
1426 [ # # ]: 0 : if (rc)
1427 : : goto out_unlock;
1428 : 0 : isec->sid = sid;
1429 : : }
1430 : : }
1431 : : break;
1432 : : }
1433 : :
1434 : 0 : isec->initialized = 1;
1435 : :
1436 : : out_unlock:
1437 : 0 : mutex_unlock(&isec->lock);
1438 : : out:
1439 [ # # ]: 0 : if (isec->sclass == SECCLASS_FILE)
1440 : 0 : isec->sclass = inode_mode_to_security_class(inode->i_mode);
1441 : 0 : return rc;
1442 : : }
1443 : :
1444 : : /* Convert a Linux signal to an access vector. */
1445 : : static inline u32 signal_to_av(int sig)
1446 : : {
1447 : : u32 perm = 0;
1448 : :
1449 [ # # ][ # # ]: 0 : switch (sig) {
1450 : : case SIGCHLD:
1451 : : /* Commonly granted from child to parent. */
1452 : : perm = PROCESS__SIGCHLD;
1453 : : break;
1454 : : case SIGKILL:
1455 : : /* Cannot be caught or ignored */
1456 : : perm = PROCESS__SIGKILL;
1457 : : break;
1458 : : case SIGSTOP:
1459 : : /* Cannot be caught or ignored */
1460 : : perm = PROCESS__SIGSTOP;
1461 : : break;
1462 : : default:
1463 : : /* All other signals. */
1464 : : perm = PROCESS__SIGNAL;
1465 : : break;
1466 : : }
1467 : :
1468 : : return perm;
1469 : : }
1470 : :
1471 : : /*
1472 : : * Check permission between a pair of credentials
1473 : : * fork check, ptrace check, etc.
1474 : : */
1475 : 0 : static int cred_has_perm(const struct cred *actor,
1476 : 0 : const struct cred *target,
1477 : : u32 perms)
1478 : : {
1479 : : u32 asid = cred_sid(actor), tsid = cred_sid(target);
1480 : :
1481 : 0 : return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1482 : : }
1483 : :
1484 : : /*
1485 : : * Check permission between a pair of tasks, e.g. signal checks,
1486 : : * fork check, ptrace check, etc.
1487 : : * tsk1 is the actor and tsk2 is the target
1488 : : * - this uses the default subjective creds of tsk1
1489 : : */
1490 : 0 : static int task_has_perm(const struct task_struct *tsk1,
1491 : : const struct task_struct *tsk2,
1492 : : u32 perms)
1493 : : {
1494 : : const struct task_security_struct *__tsec1, *__tsec2;
1495 : : u32 sid1, sid2;
1496 : :
1497 : : rcu_read_lock();
1498 : 0 : __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1499 : 0 : __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1500 : : rcu_read_unlock();
1501 : 0 : return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
1502 : : }
1503 : :
1504 : : /*
1505 : : * Check permission between current and another task, e.g. signal checks,
1506 : : * fork check, ptrace check, etc.
1507 : : * current is the actor and tsk2 is the target
1508 : : * - this uses current's subjective creds
1509 : : */
1510 : 0 : static int current_has_perm(const struct task_struct *tsk,
1511 : : u32 perms)
1512 : : {
1513 : : u32 sid, tsid;
1514 : :
1515 : : sid = current_sid();
1516 : : tsid = task_sid(tsk);
1517 : 0 : return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1518 : : }
1519 : :
1520 : : #if CAP_LAST_CAP > 63
1521 : : #error Fix SELinux to handle capabilities > 63.
1522 : : #endif
1523 : :
1524 : : /* Check whether a task is allowed to use a capability. */
1525 : 0 : static int cred_has_capability(const struct cred *cred,
1526 : : int cap, int audit)
1527 : : {
1528 : : struct common_audit_data ad;
1529 : : struct av_decision avd;
1530 : : u16 sclass;
1531 : : u32 sid = cred_sid(cred);
1532 : 0 : u32 av = CAP_TO_MASK(cap);
1533 : : int rc;
1534 : :
1535 : 0 : ad.type = LSM_AUDIT_DATA_CAP;
1536 : 0 : ad.u.cap = cap;
1537 : :
1538 [ # # # ]: 0 : switch (CAP_TO_INDEX(cap)) {
1539 : : case 0:
1540 : : sclass = SECCLASS_CAPABILITY;
1541 : : break;
1542 : : case 1:
1543 : : sclass = SECCLASS_CAPABILITY2;
1544 : 0 : break;
1545 : : default:
1546 : 0 : printk(KERN_ERR
1547 : : "SELinux: out of range capability %d\n", cap);
1548 : 0 : BUG();
1549 : : return -EINVAL;
1550 : : }
1551 : :
1552 : 0 : rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
1553 [ # # ]: 0 : if (audit == SECURITY_CAP_AUDIT) {
1554 : : int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
1555 [ # # ]: 0 : if (rc2)
1556 : : return rc2;
1557 : : }
1558 : 0 : return rc;
1559 : : }
1560 : :
1561 : : /* Check whether a task is allowed to use a system operation. */
1562 : 0 : static int task_has_system(struct task_struct *tsk,
1563 : : u32 perms)
1564 : : {
1565 : : u32 sid = task_sid(tsk);
1566 : :
1567 : 0 : return avc_has_perm(sid, SECINITSID_KERNEL,
1568 : : SECCLASS_SYSTEM, perms, NULL);
1569 : : }
1570 : :
1571 : : /* Check whether a task has a particular permission to an inode.
1572 : : The 'adp' parameter is optional and allows other audit
1573 : : data to be passed (e.g. the dentry). */
1574 : 0 : static int inode_has_perm(const struct cred *cred,
1575 : : struct inode *inode,
1576 : : u32 perms,
1577 : : struct common_audit_data *adp)
1578 : : {
1579 : : struct inode_security_struct *isec;
1580 : : u32 sid;
1581 : :
1582 : : validate_creds(cred);
1583 : :
1584 [ # # ]: 0 : if (unlikely(IS_PRIVATE(inode)))
1585 : : return 0;
1586 : :
1587 : : sid = cred_sid(cred);
1588 : 0 : isec = inode->i_security;
1589 : :
1590 : 0 : return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
1591 : : }
1592 : :
1593 : : /* Same as inode_has_perm, but pass explicit audit data containing
1594 : : the dentry to help the auditing code to more easily generate the
1595 : : pathname if needed. */
1596 : : static inline int dentry_has_perm(const struct cred *cred,
1597 : : struct dentry *dentry,
1598 : : u32 av)
1599 : : {
1600 : 0 : struct inode *inode = dentry->d_inode;
1601 : : struct common_audit_data ad;
1602 : :
1603 : 0 : ad.type = LSM_AUDIT_DATA_DENTRY;
1604 : 0 : ad.u.dentry = dentry;
1605 : 0 : return inode_has_perm(cred, inode, av, &ad);
1606 : : }
1607 : :
1608 : : /* Same as inode_has_perm, but pass explicit audit data containing
1609 : : the path to help the auditing code to more easily generate the
1610 : : pathname if needed. */
1611 : : static inline int path_has_perm(const struct cred *cred,
1612 : : struct path *path,
1613 : : u32 av)
1614 : : {
1615 : 0 : struct inode *inode = path->dentry->d_inode;
1616 : : struct common_audit_data ad;
1617 : :
1618 : 0 : ad.type = LSM_AUDIT_DATA_PATH;
1619 : 0 : ad.u.path = *path;
1620 : 0 : return inode_has_perm(cred, inode, av, &ad);
1621 : : }
1622 : :
1623 : : /* Same as path_has_perm, but uses the inode from the file struct. */
1624 : : static inline int file_path_has_perm(const struct cred *cred,
1625 : 0 : struct file *file,
1626 : : u32 av)
1627 : : {
1628 : : struct common_audit_data ad;
1629 : :
1630 : 0 : ad.type = LSM_AUDIT_DATA_PATH;
1631 : 0 : ad.u.path = file->f_path;
1632 : 0 : return inode_has_perm(cred, file_inode(file), av, &ad);
1633 : : }
1634 : :
1635 : : /* Check whether a task can use an open file descriptor to
1636 : : access an inode in a given way. Check access to the
1637 : : descriptor itself, and then use dentry_has_perm to
1638 : : check a particular permission to the file.
1639 : : Access to the descriptor is implicitly granted if it
1640 : : has the same SID as the process. If av is zero, then
1641 : : access to the file is not checked, e.g. for cases
1642 : : where only the descriptor is affected like seek. */
1643 : 0 : static int file_has_perm(const struct cred *cred,
1644 : 0 : struct file *file,
1645 : : u32 av)
1646 : : {
1647 : 0 : struct file_security_struct *fsec = file->f_security;
1648 : 0 : struct inode *inode = file_inode(file);
1649 : : struct common_audit_data ad;
1650 : : u32 sid = cred_sid(cred);
1651 : : int rc;
1652 : :
1653 : 0 : ad.type = LSM_AUDIT_DATA_PATH;
1654 : 0 : ad.u.path = file->f_path;
1655 : :
1656 [ # # ]: 0 : if (sid != fsec->sid) {
1657 : 0 : rc = avc_has_perm(sid, fsec->sid,
1658 : : SECCLASS_FD,
1659 : : FD__USE,
1660 : : &ad);
1661 [ # # ]: 0 : if (rc)
1662 : : goto out;
1663 : : }
1664 : :
1665 : : /* av is zero if only checking access to the descriptor. */
1666 : : rc = 0;
1667 [ # # ]: 0 : if (av)
1668 : 0 : rc = inode_has_perm(cred, inode, av, &ad);
1669 : :
1670 : : out:
1671 : 0 : return rc;
1672 : : }
1673 : :
1674 : : /* Check whether a task can create a file. */
1675 : 0 : static int may_create(struct inode *dir,
1676 : : struct dentry *dentry,
1677 : : u16 tclass)
1678 : : {
1679 : 0 : const struct task_security_struct *tsec = current_security();
1680 : : struct inode_security_struct *dsec;
1681 : : struct superblock_security_struct *sbsec;
1682 : : u32 sid, newsid;
1683 : : struct common_audit_data ad;
1684 : : int rc;
1685 : :
1686 : 0 : dsec = dir->i_security;
1687 : 0 : sbsec = dir->i_sb->s_security;
1688 : :
1689 : 0 : sid = tsec->sid;
1690 : 0 : newsid = tsec->create_sid;
1691 : :
1692 : 0 : ad.type = LSM_AUDIT_DATA_DENTRY;
1693 : 0 : ad.u.dentry = dentry;
1694 : :
1695 : 0 : rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
1696 : : DIR__ADD_NAME | DIR__SEARCH,
1697 : : &ad);
1698 [ # # ]: 0 : if (rc)
1699 : : return rc;
1700 : :
1701 [ # # ][ # # ]: 0 : if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
1702 : 0 : rc = security_transition_sid(sid, dsec->sid, tclass,
1703 : 0 : &dentry->d_name, &newsid);
1704 [ # # ]: 0 : if (rc)
1705 : : return rc;
1706 : : }
1707 : :
1708 : 0 : rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
1709 [ # # ]: 0 : if (rc)
1710 : : return rc;
1711 : :
1712 : 0 : return avc_has_perm(newsid, sbsec->sid,
1713 : : SECCLASS_FILESYSTEM,
1714 : : FILESYSTEM__ASSOCIATE, &ad);
1715 : : }
1716 : :
1717 : : /* Check whether a task can create a key. */
1718 : 0 : static int may_create_key(u32 ksid,
1719 : : struct task_struct *ctx)
1720 : : {
1721 : : u32 sid = task_sid(ctx);
1722 : :
1723 : 0 : return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
1724 : : }
1725 : :
1726 : : #define MAY_LINK 0
1727 : : #define MAY_UNLINK 1
1728 : : #define MAY_RMDIR 2
1729 : :
1730 : : /* Check whether a task can link, unlink, or rmdir a file/directory. */
1731 : 0 : static int may_link(struct inode *dir,
1732 : : struct dentry *dentry,
1733 : : int kind)
1734 : :
1735 : : {
1736 : : struct inode_security_struct *dsec, *isec;
1737 : : struct common_audit_data ad;
1738 : : u32 sid = current_sid();
1739 : : u32 av;
1740 : : int rc;
1741 : :
1742 : 0 : dsec = dir->i_security;
1743 : 0 : isec = dentry->d_inode->i_security;
1744 : :
1745 : 0 : ad.type = LSM_AUDIT_DATA_DENTRY;
1746 : 0 : ad.u.dentry = dentry;
1747 : :
1748 : : av = DIR__SEARCH;
1749 [ # # ]: 0 : av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1750 : 0 : rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
1751 [ # # ]: 0 : if (rc)
1752 : : return rc;
1753 : :
1754 [ # # # # ]: 0 : switch (kind) {
1755 : : case MAY_LINK:
1756 : : av = FILE__LINK;
1757 : : break;
1758 : : case MAY_UNLINK:
1759 : : av = FILE__UNLINK;
1760 : : break;
1761 : : case MAY_RMDIR:
1762 : : av = DIR__RMDIR;
1763 : : break;
1764 : : default:
1765 : 0 : printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1766 : : __func__, kind);
1767 : : return 0;
1768 : : }
1769 : :
1770 : 0 : rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
1771 : : return rc;
1772 : : }
1773 : :
1774 : : static inline int may_rename(struct inode *old_dir,
1775 : : struct dentry *old_dentry,
1776 : : struct inode *new_dir,
1777 : : struct dentry *new_dentry)
1778 : : {
1779 : : struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
1780 : : struct common_audit_data ad;
1781 : : u32 sid = current_sid();
1782 : : u32 av;
1783 : : int old_is_dir, new_is_dir;
1784 : : int rc;
1785 : :
1786 : 0 : old_dsec = old_dir->i_security;
1787 : 0 : old_isec = old_dentry->d_inode->i_security;
1788 : 0 : old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1789 : 0 : new_dsec = new_dir->i_security;
1790 : :
1791 : 0 : ad.type = LSM_AUDIT_DATA_DENTRY;
1792 : :
1793 : 0 : ad.u.dentry = old_dentry;
1794 : 0 : rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
1795 : : DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1796 [ # # ]: 0 : if (rc)
1797 : : return rc;
1798 : 0 : rc = avc_has_perm(sid, old_isec->sid,
1799 : : old_isec->sclass, FILE__RENAME, &ad);
1800 [ # # ]: 0 : if (rc)
1801 : : return rc;
1802 [ # # ]: 0 : if (old_is_dir && new_dir != old_dir) {
1803 : 0 : rc = avc_has_perm(sid, old_isec->sid,
1804 : : old_isec->sclass, DIR__REPARENT, &ad);
1805 [ # # ]: 0 : if (rc)
1806 : : return rc;
1807 : : }
1808 : :
1809 : 0 : ad.u.dentry = new_dentry;
1810 : : av = DIR__ADD_NAME | DIR__SEARCH;
1811 [ # # ]: 0 : if (new_dentry->d_inode)
1812 : : av |= DIR__REMOVE_NAME;
1813 : 0 : rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
1814 [ # # ]: 0 : if (rc)
1815 : : return rc;
1816 [ # # ]: 0 : if (new_dentry->d_inode) {
1817 : 0 : new_isec = new_dentry->d_inode->i_security;
1818 : 0 : new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
1819 [ # # ]: 0 : rc = avc_has_perm(sid, new_isec->sid,
1820 : : new_isec->sclass,
1821 : : (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1822 [ # # ]: 0 : if (rc)
1823 : : return rc;
1824 : : }
1825 : :
1826 : : return 0;
1827 : : }
1828 : :
1829 : : /* Check whether a task can perform a filesystem operation. */
1830 : 0 : static int superblock_has_perm(const struct cred *cred,
1831 : : struct super_block *sb,
1832 : : u32 perms,
1833 : : struct common_audit_data *ad)
1834 : : {
1835 : : struct superblock_security_struct *sbsec;
1836 : : u32 sid = cred_sid(cred);
1837 : :
1838 : : sbsec = sb->s_security;
1839 : 0 : return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
1840 : : }
1841 : :
1842 : : /* Convert a Linux mode and permission mask to an access vector. */
1843 : : static inline u32 file_mask_to_av(int mode, int mask)
1844 : : {
1845 : : u32 av = 0;
1846 : :
1847 [ # # ][ # # ]: 0 : if (!S_ISDIR(mode)) {
1848 [ # # ][ # # ]: 0 : if (mask & MAY_EXEC)
1849 : : av |= FILE__EXECUTE;
1850 [ # # ][ # # ]: 0 : if (mask & MAY_READ)
1851 : 0 : av |= FILE__READ;
1852 : :
1853 [ # # ][ # # ]: 0 : if (mask & MAY_APPEND)
1854 : 0 : av |= FILE__APPEND;
1855 [ # # ][ # # ]: 0 : else if (mask & MAY_WRITE)
1856 : 0 : av |= FILE__WRITE;
1857 : :
1858 : : } else {
1859 [ # # ][ # # ]: 0 : if (mask & MAY_EXEC)
1860 : : av |= DIR__SEARCH;
1861 [ # # ][ # # ]: 0 : if (mask & MAY_WRITE)
1862 : 0 : av |= DIR__WRITE;
1863 [ # # ][ # # ]: 0 : if (mask & MAY_READ)
1864 : 0 : av |= DIR__READ;
1865 : : }
1866 : :
1867 : : return av;
1868 : : }
1869 : :
1870 : : /* Convert a Linux file to an access vector. */
1871 : : static inline u32 file_to_av(struct file *file)
1872 : : {
1873 : : u32 av = 0;
1874 : :
1875 [ # # ][ # # ]: 0 : if (file->f_mode & FMODE_READ)
[ # # ][ # # ]
1876 : : av |= FILE__READ;
1877 [ # # ][ # # ]: 0 : if (file->f_mode & FMODE_WRITE) {
[ # # ][ # # ]
1878 [ # # ][ # # ]: 0 : if (file->f_flags & O_APPEND)
[ # # ][ # # ]
1879 : 0 : av |= FILE__APPEND;
1880 : : else
1881 : 0 : av |= FILE__WRITE;
1882 : : }
1883 [ # # ][ # # ]: 0 : if (!av) {
[ # # ][ # # ]
1884 : : /*
1885 : : * Special file opened with flags 3 for ioctl-only use.
1886 : : */
1887 : : av = FILE__IOCTL;
1888 : : }
1889 : :
1890 : : return av;
1891 : : }
1892 : :
1893 : : /*
1894 : : * Convert a file to an access vector and include the correct open
1895 : : * open permission.
1896 : : */
1897 : 0 : static inline u32 open_file_to_av(struct file *file)
1898 : : {
1899 : : u32 av = file_to_av(file);
1900 : :
1901 [ # # ]: 0 : if (selinux_policycap_openperm)
1902 : 0 : av |= FILE__OPEN;
1903 : :
1904 : : return av;
1905 : : }
1906 : :
1907 : : /* Hook functions begin here. */
1908 : :
1909 : 0 : static int selinux_binder_set_context_mgr(struct task_struct *mgr)
1910 : : {
1911 : : u32 mysid = current_sid();
1912 : : u32 mgrsid = task_sid(mgr);
1913 : :
1914 : 0 : return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, BINDER__SET_CONTEXT_MGR, NULL);
1915 : : }
1916 : :
1917 : 0 : static int selinux_binder_transaction(struct task_struct *from, struct task_struct *to)
1918 : : {
1919 : : u32 mysid = current_sid();
1920 : : u32 fromsid = task_sid(from);
1921 : : u32 tosid = task_sid(to);
1922 : : int rc;
1923 : :
1924 [ # # ]: 0 : if (mysid != fromsid) {
1925 : 0 : rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, BINDER__IMPERSONATE, NULL);
1926 [ # # ]: 0 : if (rc)
1927 : : return rc;
1928 : : }
1929 : :
1930 : 0 : return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, NULL);
1931 : : }
1932 : :
1933 : 0 : static int selinux_binder_transfer_binder(struct task_struct *from, struct task_struct *to)
1934 : : {
1935 : : u32 fromsid = task_sid(from);
1936 : : u32 tosid = task_sid(to);
1937 : 0 : return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, NULL);
1938 : : }
1939 : :
1940 : 0 : static int selinux_binder_transfer_file(struct task_struct *from, struct task_struct *to, struct file *file)
1941 : : {
1942 : : u32 sid = task_sid(to);
1943 : 0 : struct file_security_struct *fsec = file->f_security;
1944 : 0 : struct inode *inode = file->f_path.dentry->d_inode;
1945 : 0 : struct inode_security_struct *isec = inode->i_security;
1946 : : struct common_audit_data ad;
1947 : 0 : struct selinux_audit_data sad = {0,};
1948 : : int rc;
1949 : :
1950 : 0 : ad.type = LSM_AUDIT_DATA_PATH;
1951 : 0 : ad.u.path = file->f_path;
1952 : 0 : ad.selinux_audit_data = &sad;
1953 : :
1954 [ # # ]: 0 : if (sid != fsec->sid) {
1955 : 0 : rc = avc_has_perm(sid, fsec->sid,
1956 : : SECCLASS_FD,
1957 : : FD__USE,
1958 : : &ad);
1959 [ # # ]: 0 : if (rc)
1960 : : return rc;
1961 : : }
1962 : :
1963 [ # # ]: 0 : if (unlikely(IS_PRIVATE(inode)))
1964 : : return 0;
1965 : :
1966 : 0 : return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
1967 : : &ad);
1968 : : }
1969 : :
1970 : 0 : static int selinux_ptrace_access_check(struct task_struct *child,
1971 : : unsigned int mode)
1972 : : {
1973 : : int rc;
1974 : :
1975 : 0 : rc = cap_ptrace_access_check(child, mode);
1976 [ # # ]: 0 : if (rc)
1977 : : return rc;
1978 : :
1979 [ # # ]: 0 : if (mode & PTRACE_MODE_READ) {
1980 : : u32 sid = current_sid();
1981 : : u32 csid = task_sid(child);
1982 : 0 : return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
1983 : : }
1984 : :
1985 : 0 : return current_has_perm(child, PROCESS__PTRACE);
1986 : : }
1987 : :
1988 : 0 : static int selinux_ptrace_traceme(struct task_struct *parent)
1989 : : {
1990 : : int rc;
1991 : :
1992 : 0 : rc = cap_ptrace_traceme(parent);
1993 [ # # ]: 0 : if (rc)
1994 : : return rc;
1995 : :
1996 : 0 : return task_has_perm(parent, current, PROCESS__PTRACE);
1997 : : }
1998 : :
1999 : 0 : static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
2000 : : kernel_cap_t *inheritable, kernel_cap_t *permitted)
2001 : : {
2002 : : int error;
2003 : :
2004 : 0 : error = current_has_perm(target, PROCESS__GETCAP);
2005 [ # # ]: 0 : if (error)
2006 : : return error;
2007 : :
2008 : 0 : return cap_capget(target, effective, inheritable, permitted);
2009 : : }
2010 : :
2011 : 0 : static int selinux_capset(struct cred *new, const struct cred *old,
2012 : : const kernel_cap_t *effective,
2013 : : const kernel_cap_t *inheritable,
2014 : : const kernel_cap_t *permitted)
2015 : : {
2016 : : int error;
2017 : :
2018 : 0 : error = cap_capset(new, old,
2019 : : effective, inheritable, permitted);
2020 [ # # ]: 0 : if (error)
2021 : : return error;
2022 : :
2023 : 0 : return cred_has_perm(old, new, PROCESS__SETCAP);
2024 : : }
2025 : :
2026 : : /*
2027 : : * (This comment used to live with the selinux_task_setuid hook,
2028 : : * which was removed).
2029 : : *
2030 : : * Since setuid only affects the current process, and since the SELinux
2031 : : * controls are not based on the Linux identity attributes, SELinux does not
2032 : : * need to control this operation. However, SELinux does control the use of
2033 : : * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2034 : : */
2035 : :
2036 : 0 : static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2037 : : int cap, int audit)
2038 : : {
2039 : : int rc;
2040 : :
2041 : 0 : rc = cap_capable(cred, ns, cap, audit);
2042 [ # # ]: 0 : if (rc)
2043 : : return rc;
2044 : :
2045 : 0 : return cred_has_capability(cred, cap, audit);
2046 : : }
2047 : :
2048 : 0 : static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2049 : : {
2050 : 0 : const struct cred *cred = current_cred();
2051 : : int rc = 0;
2052 : :
2053 [ # # ][ # # ]: 0 : if (!sb)
[ # # ][ # # ]
2054 : : return 0;
2055 : :
2056 : : switch (cmds) {
2057 : : case Q_SYNC:
2058 : : case Q_QUOTAON:
2059 : : case Q_QUOTAOFF:
2060 : : case Q_SETINFO:
2061 : : case Q_SETQUOTA:
2062 : : rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
2063 : 0 : break;
2064 : : case Q_GETFMT:
2065 : : case Q_GETINFO:
2066 : : case Q_GETQUOTA:
2067 : : rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
2068 : 0 : break;
2069 : : default:
2070 : : rc = 0; /* let the kernel handle invalid cmds */
2071 : : break;
2072 : : }
2073 : 0 : return rc;
2074 : : }
2075 : :
2076 : 0 : static int selinux_quota_on(struct dentry *dentry)
2077 : : {
2078 : 0 : const struct cred *cred = current_cred();
2079 : :
2080 : 0 : return dentry_has_perm(cred, dentry, FILE__QUOTAON);
2081 : : }
2082 : :
2083 : 0 : static int selinux_syslog(int type)
2084 : : {
2085 : : int rc;
2086 : :
2087 [ # # # ]: 0 : switch (type) {
2088 : : case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2089 : : case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
2090 : 0 : rc = task_has_system(current, SYSTEM__SYSLOG_READ);
2091 : 0 : break;
2092 : : case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2093 : : case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2094 : : /* Set level of messages printed to console */
2095 : : case SYSLOG_ACTION_CONSOLE_LEVEL:
2096 : 0 : rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
2097 : 0 : break;
2098 : : case SYSLOG_ACTION_CLOSE: /* Close log */
2099 : : case SYSLOG_ACTION_OPEN: /* Open log */
2100 : : case SYSLOG_ACTION_READ: /* Read from log */
2101 : : case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2102 : : case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
2103 : : default:
2104 : 0 : rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2105 : 0 : break;
2106 : : }
2107 : 0 : return rc;
2108 : : }
2109 : :
2110 : : /*
2111 : : * Check that a process has enough memory to allocate a new virtual
2112 : : * mapping. 0 means there is enough memory for the allocation to
2113 : : * succeed and -ENOMEM implies there is not.
2114 : : *
2115 : : * Do not audit the selinux permission check, as this is applied to all
2116 : : * processes that allocate mappings.
2117 : : */
2118 : 0 : static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
2119 : : {
2120 : : int rc, cap_sys_admin = 0;
2121 : :
2122 : 0 : rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
2123 : : SECURITY_CAP_NOAUDIT);
2124 [ # # ]: 0 : if (rc == 0)
2125 : : cap_sys_admin = 1;
2126 : :
2127 : 0 : return __vm_enough_memory(mm, pages, cap_sys_admin);
2128 : : }
2129 : :
2130 : : /* binprm security operations */
2131 : :
2132 : 0 : static int selinux_bprm_set_creds(struct linux_binprm *bprm)
2133 : : {
2134 : : const struct task_security_struct *old_tsec;
2135 : : struct task_security_struct *new_tsec;
2136 : : struct inode_security_struct *isec;
2137 : : struct common_audit_data ad;
2138 : 0 : struct inode *inode = file_inode(bprm->file);
2139 : : int rc;
2140 : :
2141 : 0 : rc = cap_bprm_set_creds(bprm);
2142 [ # # ]: 0 : if (rc)
2143 : : return rc;
2144 : :
2145 : : /* SELinux context only depends on initial program or script and not
2146 : : * the script interpreter */
2147 [ # # ]: 0 : if (bprm->cred_prepared)
2148 : : return 0;
2149 : :
2150 : 0 : old_tsec = current_security();
2151 : 0 : new_tsec = bprm->cred->security;
2152 : 0 : isec = inode->i_security;
2153 : :
2154 : : /* Default to the current task SID. */
2155 : 0 : new_tsec->sid = old_tsec->sid;
2156 : 0 : new_tsec->osid = old_tsec->sid;
2157 : :
2158 : : /* Reset fs, key, and sock SIDs on execve. */
2159 : 0 : new_tsec->create_sid = 0;
2160 : 0 : new_tsec->keycreate_sid = 0;
2161 : 0 : new_tsec->sockcreate_sid = 0;
2162 : :
2163 [ # # ]: 0 : if (old_tsec->exec_sid) {
2164 : 0 : new_tsec->sid = old_tsec->exec_sid;
2165 : : /* Reset exec SID on execve. */
2166 : 0 : new_tsec->exec_sid = 0;
2167 : :
2168 : : /*
2169 : : * Minimize confusion: if no_new_privs and a transition is
2170 : : * explicitly requested, then fail the exec.
2171 : : */
2172 [ # # ]: 0 : if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2173 : : return -EPERM;
2174 : : } else {
2175 : : /* Check for a default transition on this program. */
2176 : 0 : rc = security_transition_sid(old_tsec->sid, isec->sid,
2177 : : SECCLASS_PROCESS, NULL,
2178 : : &new_tsec->sid);
2179 [ # # ]: 0 : if (rc)
2180 : : return rc;
2181 : : }
2182 : :
2183 : 0 : ad.type = LSM_AUDIT_DATA_PATH;
2184 : 0 : ad.u.path = bprm->file->f_path;
2185 : :
2186 [ # # ][ # # ]: 0 : if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2187 : 0 : (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
2188 : 0 : new_tsec->sid = old_tsec->sid;
2189 : :
2190 [ # # ]: 0 : if (new_tsec->sid == old_tsec->sid) {
2191 : 0 : rc = avc_has_perm(old_tsec->sid, isec->sid,
2192 : : SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2193 [ # # ]: 0 : if (rc)
2194 : 0 : return rc;
2195 : : } else {
2196 : : /* Check permissions for the transition. */
2197 : 0 : rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2198 : : SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2199 [ # # ]: 0 : if (rc)
2200 : : return rc;
2201 : :
2202 : 0 : rc = avc_has_perm(new_tsec->sid, isec->sid,
2203 : : SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2204 [ # # ]: 0 : if (rc)
2205 : : return rc;
2206 : :
2207 : : /* Check for shared state */
2208 [ # # ]: 0 : if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2209 : 0 : rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2210 : : SECCLASS_PROCESS, PROCESS__SHARE,
2211 : : NULL);
2212 [ # # ]: 0 : if (rc)
2213 : : return -EPERM;
2214 : : }
2215 : :
2216 : : /* Make sure that anyone attempting to ptrace over a task that
2217 : : * changes its SID has the appropriate permit */
2218 [ # # ]: 0 : if (bprm->unsafe &
2219 : : (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2220 : : struct task_struct *tracer;
2221 : : struct task_security_struct *sec;
2222 : : u32 ptsid = 0;
2223 : :
2224 : : rcu_read_lock();
2225 : 0 : tracer = ptrace_parent(current);
2226 [ # # ]: 0 : if (likely(tracer != NULL)) {
2227 : 0 : sec = __task_cred(tracer)->security;
2228 : 0 : ptsid = sec->sid;
2229 : : }
2230 : : rcu_read_unlock();
2231 : :
2232 [ # # ]: 0 : if (ptsid != 0) {
2233 : 0 : rc = avc_has_perm(ptsid, new_tsec->sid,
2234 : : SECCLASS_PROCESS,
2235 : : PROCESS__PTRACE, NULL);
2236 [ # # ]: 0 : if (rc)
2237 : : return -EPERM;
2238 : : }
2239 : : }
2240 : :
2241 : : /* Clear any possibly unsafe personality bits on exec: */
2242 : 0 : bprm->per_clear |= PER_CLEAR_ON_SETID;
2243 : : }
2244 : :
2245 : : return 0;
2246 : : }
2247 : :
2248 : 0 : static int selinux_bprm_secureexec(struct linux_binprm *bprm)
2249 : : {
2250 : 0 : const struct task_security_struct *tsec = current_security();
2251 : : u32 sid, osid;
2252 : : int atsecure = 0;
2253 : :
2254 : 0 : sid = tsec->sid;
2255 : 0 : osid = tsec->osid;
2256 : :
2257 [ # # ]: 0 : if (osid != sid) {
2258 : : /* Enable secure mode for SIDs transitions unless
2259 : : the noatsecure permission is granted between
2260 : : the two SIDs, i.e. ahp returns 0. */
2261 : 0 : atsecure = avc_has_perm(osid, sid,
2262 : : SECCLASS_PROCESS,
2263 : : PROCESS__NOATSECURE, NULL);
2264 : : }
2265 : :
2266 [ # # ][ # # ]: 0 : return (atsecure || cap_bprm_secureexec(bprm));
2267 : : }
2268 : :
2269 : 0 : static int match_file(const void *p, struct file *file, unsigned fd)
2270 : : {
2271 [ # # ]: 0 : return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2272 : : }
2273 : :
2274 : : /* Derived from fs/exec.c:flush_old_files. */
2275 : : static inline void flush_unauthorized_files(const struct cred *cred,
2276 : : struct files_struct *files)
2277 : : {
2278 : : struct file *file, *devnull = NULL;
2279 : : struct tty_struct *tty;
2280 : : int drop_tty = 0;
2281 : : unsigned n;
2282 : :
2283 : 0 : tty = get_current_tty();
2284 [ # # ]: 0 : if (tty) {
2285 : : spin_lock(&tty_files_lock);
2286 [ # # ]: 0 : if (!list_empty(&tty->tty_files)) {
2287 : : struct tty_file_private *file_priv;
2288 : :
2289 : : /* Revalidate access to controlling tty.
2290 : : Use file_path_has_perm on the tty path directly
2291 : : rather than using file_has_perm, as this particular
2292 : : open file may belong to another process and we are
2293 : : only interested in the inode-based check here. */
2294 : : file_priv = list_first_entry(&tty->tty_files,
2295 : : struct tty_file_private, list);
2296 : 0 : file = file_priv->file;
2297 [ # # ]: 0 : if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
2298 : : drop_tty = 1;
2299 : : }
2300 : : spin_unlock(&tty_files_lock);
2301 : 0 : tty_kref_put(tty);
2302 : : }
2303 : : /* Reset controlling tty. */
2304 [ # # ]: 0 : if (drop_tty)
2305 : 0 : no_tty();
2306 : :
2307 : : /* Revalidate access to inherited open files. */
2308 : 0 : n = iterate_fd(files, 0, match_file, cred);
2309 [ # # ]: 0 : if (!n) /* none found? */
2310 : : return;
2311 : :
2312 : 0 : devnull = dentry_open(&selinux_null, O_RDWR, cred);
2313 [ # # ]: 0 : if (IS_ERR(devnull))
2314 : : devnull = NULL;
2315 : : /* replace all the matching ones with this */
2316 : : do {
2317 : 0 : replace_fd(n - 1, devnull, 0);
2318 [ # # ]: 0 : } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2319 [ # # ]: 0 : if (devnull)
2320 : 0 : fput(devnull);
2321 : : }
2322 : :
2323 : : /*
2324 : : * Prepare a process for imminent new credential changes due to exec
2325 : : */
2326 : 0 : static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
2327 : : {
2328 : : struct task_security_struct *new_tsec;
2329 : : struct rlimit *rlim, *initrlim;
2330 : : int rc, i;
2331 : :
2332 : 0 : new_tsec = bprm->cred->security;
2333 [ # # ]: 0 : if (new_tsec->sid == new_tsec->osid)
2334 : 0 : return;
2335 : :
2336 : : /* Close files for which the new task SID is not authorized. */
2337 : 0 : flush_unauthorized_files(bprm->cred, current->files);
2338 : :
2339 : : /* Always clear parent death signal on SID transitions. */
2340 : 0 : current->pdeath_signal = 0;
2341 : :
2342 : : /* Check whether the new SID can inherit resource limits from the old
2343 : : * SID. If not, reset all soft limits to the lower of the current
2344 : : * task's hard limit and the init task's soft limit.
2345 : : *
2346 : : * Note that the setting of hard limits (even to lower them) can be
2347 : : * controlled by the setrlimit check. The inclusion of the init task's
2348 : : * soft limit into the computation is to avoid resetting soft limits
2349 : : * higher than the default soft limit for cases where the default is
2350 : : * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2351 : : */
2352 : 0 : rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2353 : : PROCESS__RLIMITINH, NULL);
2354 [ # # ]: 0 : if (rc) {
2355 : : /* protect against do_prlimit() */
2356 : 0 : task_lock(current);
2357 [ # # ]: 0 : for (i = 0; i < RLIM_NLIMITS; i++) {
2358 : 0 : rlim = current->signal->rlim + i;
2359 : 0 : initrlim = init_task.signal->rlim + i;
2360 : 0 : rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2361 : : }
2362 : 0 : task_unlock(current);
2363 : 0 : update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2364 : : }
2365 : : }
2366 : :
2367 : : /*
2368 : : * Clean up the process immediately after the installation of new credentials
2369 : : * due to exec
2370 : : */
2371 : 0 : static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2372 : : {
2373 : 0 : const struct task_security_struct *tsec = current_security();
2374 : : struct itimerval itimer;
2375 : : u32 osid, sid;
2376 : : int rc, i;
2377 : :
2378 : 0 : osid = tsec->osid;
2379 : 0 : sid = tsec->sid;
2380 : :
2381 [ # # ]: 0 : if (sid == osid)
2382 : 0 : return;
2383 : :
2384 : : /* Check whether the new SID can inherit signal state from the old SID.
2385 : : * If not, clear itimers to avoid subsequent signal generation and
2386 : : * flush and unblock signals.
2387 : : *
2388 : : * This must occur _after_ the task SID has been updated so that any
2389 : : * kill done after the flush will be checked against the new SID.
2390 : : */
2391 : 0 : rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
2392 [ # # ]: 0 : if (rc) {
2393 : 0 : memset(&itimer, 0, sizeof itimer);
2394 [ # # ]: 0 : for (i = 0; i < 3; i++)
2395 : 0 : do_setitimer(i, &itimer, NULL);
2396 : 0 : spin_lock_irq(¤t->sighand->siglock);
2397 [ # # ]: 0 : if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
2398 : 0 : __flush_signals(current);
2399 : 0 : flush_signal_handlers(current, 1);
2400 : 0 : sigemptyset(¤t->blocked);
2401 : : }
2402 : 0 : spin_unlock_irq(¤t->sighand->siglock);
2403 : : }
2404 : :
2405 : : /* Wake up the parent if it is waiting so that it can recheck
2406 : : * wait permission to the new task SID. */
2407 : 0 : read_lock(&tasklist_lock);
2408 : 0 : __wake_up_parent(current, current->real_parent);
2409 : : read_unlock(&tasklist_lock);
2410 : : }
2411 : :
2412 : : /* superblock security operations */
2413 : :
2414 : 0 : static int selinux_sb_alloc_security(struct super_block *sb)
2415 : : {
2416 : 0 : return superblock_alloc_security(sb);
2417 : : }
2418 : :
2419 : 0 : static void selinux_sb_free_security(struct super_block *sb)
2420 : : {
2421 : : superblock_free_security(sb);
2422 : 0 : }
2423 : :
2424 : : static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2425 : : {
2426 [ # # ][ # # ]: 0 : if (plen > olen)
[ # # ][ # # ]
[ # # ]
2427 : : return 0;
2428 : :
2429 : 0 : return !memcmp(prefix, option, plen);
2430 : : }
2431 : :
2432 : : static inline int selinux_option(char *option, int len)
2433 : : {
2434 [ # # ]: 0 : return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2435 [ # # ]: 0 : match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2436 [ # # ]: 0 : match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
2437 [ # # ][ # # ]: 0 : match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2438 : : match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
2439 : : }
2440 : :
2441 : : static inline void take_option(char **to, char *from, int *first, int len)
2442 : : {
2443 [ # # ]: 0 : if (!*first) {
2444 : 0 : **to = ',';
2445 : 0 : *to += 1;
2446 : : } else
2447 : : *first = 0;
2448 : 0 : memcpy(*to, from, len);
2449 : 0 : *to += len;
2450 : : }
2451 : :
2452 : : static inline void take_selinux_option(char **to, char *from, int *first,
2453 : : int len)
2454 : : {
2455 : : int current_size = 0;
2456 : :
2457 [ # # ]: 0 : if (!*first) {
2458 : 0 : **to = '|';
2459 : 0 : *to += 1;
2460 : : } else
2461 : : *first = 0;
2462 : :
2463 [ # # ]: 0 : while (current_size < len) {
2464 [ # # ]: 0 : if (*from != '"') {
2465 : 0 : **to = *from;
2466 : 0 : *to += 1;
2467 : : }
2468 : 0 : from += 1;
2469 : 0 : current_size += 1;
2470 : : }
2471 : : }
2472 : :
2473 : 0 : static int selinux_sb_copy_data(char *orig, char *copy)
2474 : : {
2475 : : int fnosec, fsec, rc = 0;
2476 : : char *in_save, *in_curr, *in_end;
2477 : : char *sec_curr, *nosec_save, *nosec;
2478 : : int open_quote = 0;
2479 : :
2480 : : in_curr = orig;
2481 : : sec_curr = copy;
2482 : :
2483 : 0 : nosec = (char *)get_zeroed_page(GFP_KERNEL);
2484 [ # # ]: 0 : if (!nosec) {
2485 : : rc = -ENOMEM;
2486 : : goto out;
2487 : : }
2488 : :
2489 : : nosec_save = nosec;
2490 : : fnosec = fsec = 1;
2491 : : in_save = in_end = orig;
2492 : :
2493 : : do {
2494 [ # # ]: 0 : if (*in_end == '"')
2495 : 0 : open_quote = !open_quote;
2496 [ # # ][ # # ]: 0 : if ((*in_end == ',' && open_quote == 0) ||
[ # # ]
2497 : : *in_end == '\0') {
2498 : 0 : int len = in_end - in_curr;
2499 : :
2500 [ # # ]: 0 : if (selinux_option(in_curr, len))
2501 : : take_selinux_option(&sec_curr, in_curr, &fsec, len);
2502 : : else
2503 : : take_option(&nosec, in_curr, &fnosec, len);
2504 : :
2505 : 0 : in_curr = in_end + 1;
2506 : : }
2507 [ # # ]: 0 : } while (*in_end++);
2508 : :
2509 : 0 : strcpy(in_save, nosec_save);
2510 : 0 : free_page((unsigned long)nosec_save);
2511 : : out:
2512 : 0 : return rc;
2513 : : }
2514 : :
2515 : 0 : static int selinux_sb_remount(struct super_block *sb, void *data)
2516 : : {
2517 : : int rc, i, *flags;
2518 : : struct security_mnt_opts opts;
2519 : : char *secdata, **mount_options;
2520 : 0 : struct superblock_security_struct *sbsec = sb->s_security;
2521 : :
2522 [ # # ]: 0 : if (!(sbsec->flags & SE_SBINITIALIZED))
2523 : : return 0;
2524 : :
2525 [ # # ]: 0 : if (!data)
2526 : : return 0;
2527 : :
2528 [ # # ]: 0 : if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2529 : : return 0;
2530 : :
2531 : : security_init_mnt_opts(&opts);
2532 : : secdata = alloc_secdata();
2533 [ # # ]: 0 : if (!secdata)
2534 : : return -ENOMEM;
2535 : 0 : rc = selinux_sb_copy_data(data, secdata);
2536 [ # # ]: 0 : if (rc)
2537 : : goto out_free_secdata;
2538 : :
2539 : 0 : rc = selinux_parse_opts_str(secdata, &opts);
2540 [ # # ]: 0 : if (rc)
2541 : : goto out_free_secdata;
2542 : :
2543 : 0 : mount_options = opts.mnt_opts;
2544 : 0 : flags = opts.mnt_opts_flags;
2545 : :
2546 [ # # ]: 0 : for (i = 0; i < opts.num_mnt_opts; i++) {
2547 : : u32 sid;
2548 : : size_t len;
2549 : :
2550 [ # # ]: 0 : if (flags[i] == SBLABEL_MNT)
2551 : 0 : continue;
2552 : 0 : len = strlen(mount_options[i]);
2553 : 0 : rc = security_context_to_sid(mount_options[i], len, &sid,
2554 : : GFP_KERNEL);
2555 [ # # ]: 0 : if (rc) {
2556 : 0 : printk(KERN_WARNING "SELinux: security_context_to_sid"
2557 : : "(%s) failed for (dev %s, type %s) errno=%d\n",
2558 : 0 : mount_options[i], sb->s_id, sb->s_type->name, rc);
2559 : 0 : goto out_free_opts;
2560 : : }
2561 : : rc = -EINVAL;
2562 [ # # # # : 0 : switch (flags[i]) {
# ]
2563 : : case FSCONTEXT_MNT:
2564 [ # # ]: 0 : if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2565 : : goto out_bad_option;
2566 : : break;
2567 : : case CONTEXT_MNT:
2568 [ # # ]: 0 : if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2569 : : goto out_bad_option;
2570 : : break;
2571 : : case ROOTCONTEXT_MNT: {
2572 : : struct inode_security_struct *root_isec;
2573 : 0 : root_isec = sb->s_root->d_inode->i_security;
2574 : :
2575 [ # # ]: 0 : if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2576 : : goto out_bad_option;
2577 : : break;
2578 : : }
2579 : : case DEFCONTEXT_MNT:
2580 [ # # ]: 0 : if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2581 : : goto out_bad_option;
2582 : : break;
2583 : : default:
2584 : : goto out_free_opts;
2585 : : }
2586 : : }
2587 : :
2588 : : rc = 0;
2589 : : out_free_opts:
2590 : : security_free_mnt_opts(&opts);
2591 : : out_free_secdata:
2592 : : free_secdata(secdata);
2593 : 0 : return rc;
2594 : : out_bad_option:
2595 : 0 : printk(KERN_WARNING "SELinux: unable to change security options "
2596 : 0 : "during remount (dev %s, type=%s)\n", sb->s_id,
2597 : 0 : sb->s_type->name);
2598 : 0 : goto out_free_opts;
2599 : : }
2600 : :
2601 : 0 : static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
2602 : : {
2603 : 0 : const struct cred *cred = current_cred();
2604 : : struct common_audit_data ad;
2605 : : int rc;
2606 : :
2607 : 0 : rc = superblock_doinit(sb, data);
2608 [ # # ]: 0 : if (rc)
2609 : : return rc;
2610 : :
2611 : : /* Allow all mounts performed by the kernel */
2612 [ # # ]: 0 : if (flags & MS_KERNMOUNT)
2613 : : return 0;
2614 : :
2615 : 0 : ad.type = LSM_AUDIT_DATA_DENTRY;
2616 : 0 : ad.u.dentry = sb->s_root;
2617 : 0 : return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
2618 : : }
2619 : :
2620 : 0 : static int selinux_sb_statfs(struct dentry *dentry)
2621 : : {
2622 : 0 : const struct cred *cred = current_cred();
2623 : : struct common_audit_data ad;
2624 : :
2625 : 0 : ad.type = LSM_AUDIT_DATA_DENTRY;
2626 : 0 : ad.u.dentry = dentry->d_sb->s_root;
2627 : 0 : return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
2628 : : }
2629 : :
2630 : 0 : static int selinux_mount(const char *dev_name,
2631 : : struct path *path,
2632 : : const char *type,
2633 : : unsigned long flags,
2634 : : void *data)
2635 : : {
2636 : 0 : const struct cred *cred = current_cred();
2637 : :
2638 [ # # ]: 0 : if (flags & MS_REMOUNT)
2639 : 0 : return superblock_has_perm(cred, path->dentry->d_sb,
2640 : : FILESYSTEM__REMOUNT, NULL);
2641 : : else
2642 : 0 : return path_has_perm(cred, path, FILE__MOUNTON);
2643 : : }
2644 : :
2645 : 0 : static int selinux_umount(struct vfsmount *mnt, int flags)
2646 : : {
2647 : 0 : const struct cred *cred = current_cred();
2648 : :
2649 : 0 : return superblock_has_perm(cred, mnt->mnt_sb,
2650 : : FILESYSTEM__UNMOUNT, NULL);
2651 : : }
2652 : :
2653 : : /* inode security operations */
2654 : :
2655 : 0 : static int selinux_inode_alloc_security(struct inode *inode)
2656 : : {
2657 : 0 : return inode_alloc_security(inode);
2658 : : }
2659 : :
2660 : 0 : static void selinux_inode_free_security(struct inode *inode)
2661 : : {
2662 : 0 : inode_free_security(inode);
2663 : 0 : }
2664 : :
2665 : 0 : static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2666 : : struct qstr *name, void **ctx,
2667 : : u32 *ctxlen)
2668 : : {
2669 : 0 : const struct cred *cred = current_cred();
2670 : : struct task_security_struct *tsec;
2671 : : struct inode_security_struct *dsec;
2672 : : struct superblock_security_struct *sbsec;
2673 : 0 : struct inode *dir = dentry->d_parent->d_inode;
2674 : : u32 newsid;
2675 : : int rc;
2676 : :
2677 : 0 : tsec = cred->security;
2678 : 0 : dsec = dir->i_security;
2679 : 0 : sbsec = dir->i_sb->s_security;
2680 : :
2681 [ # # ][ # # ]: 0 : if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
2682 : 0 : newsid = tsec->create_sid;
2683 : : } else {
2684 : 0 : rc = security_transition_sid(tsec->sid, dsec->sid,
2685 : : inode_mode_to_security_class(mode),
2686 : : name,
2687 : : &newsid);
2688 [ # # ]: 0 : if (rc) {
2689 : 0 : printk(KERN_WARNING
2690 : : "%s: security_transition_sid failed, rc=%d\n",
2691 : : __func__, -rc);
2692 : 0 : return rc;
2693 : : }
2694 : : }
2695 : :
2696 : 0 : return security_sid_to_context(newsid, (char **)ctx, ctxlen);
2697 : : }
2698 : :
2699 : 0 : static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
2700 : : const struct qstr *qstr,
2701 : : const char **name,
2702 : : void **value, size_t *len)
2703 : : {
2704 : 0 : const struct task_security_struct *tsec = current_security();
2705 : : struct inode_security_struct *dsec;
2706 : : struct superblock_security_struct *sbsec;
2707 : : u32 sid, newsid, clen;
2708 : : int rc;
2709 : : char *context;
2710 : :
2711 : 0 : dsec = dir->i_security;
2712 : 0 : sbsec = dir->i_sb->s_security;
2713 : :
2714 : 0 : sid = tsec->sid;
2715 : 0 : newsid = tsec->create_sid;
2716 : :
2717 [ # # ][ # # ]: 0 : if ((sbsec->flags & SE_SBINITIALIZED) &&
2718 : 0 : (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2719 : 0 : newsid = sbsec->mntpoint_sid;
2720 [ # # ][ # # ]: 0 : else if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
2721 : 0 : rc = security_transition_sid(sid, dsec->sid,
2722 : : inode_mode_to_security_class(inode->i_mode),
2723 : : qstr, &newsid);
2724 [ # # ]: 0 : if (rc) {
2725 : 0 : printk(KERN_WARNING "%s: "
2726 : : "security_transition_sid failed, rc=%d (dev=%s "
2727 : : "ino=%ld)\n",
2728 : : __func__,
2729 : 0 : -rc, inode->i_sb->s_id, inode->i_ino);
2730 : 0 : return rc;
2731 : : }
2732 : : }
2733 : :
2734 : : /* Possibly defer initialization to selinux_complete_init. */
2735 [ # # ]: 0 : if (sbsec->flags & SE_SBINITIALIZED) {
2736 : 0 : struct inode_security_struct *isec = inode->i_security;
2737 : 0 : isec->sclass = inode_mode_to_security_class(inode->i_mode);
2738 : 0 : isec->sid = newsid;
2739 : 0 : isec->initialized = 1;
2740 : : }
2741 : :
2742 [ # # ][ # # ]: 0 : if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT))
2743 : : return -EOPNOTSUPP;
2744 : :
2745 [ # # ]: 0 : if (name)
2746 : 0 : *name = XATTR_SELINUX_SUFFIX;
2747 : :
2748 [ # # ]: 0 : if (value && len) {
2749 : 0 : rc = security_sid_to_context_force(newsid, &context, &clen);
2750 [ # # ]: 0 : if (rc)
2751 : : return rc;
2752 : 0 : *value = context;
2753 : 0 : *len = clen;
2754 : : }
2755 : :
2756 : : return 0;
2757 : : }
2758 : :
2759 : 0 : static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
2760 : : {
2761 : 0 : return may_create(dir, dentry, SECCLASS_FILE);
2762 : : }
2763 : :
2764 : 0 : static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2765 : : {
2766 : 0 : return may_link(dir, old_dentry, MAY_LINK);
2767 : : }
2768 : :
2769 : 0 : static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2770 : : {
2771 : 0 : return may_link(dir, dentry, MAY_UNLINK);
2772 : : }
2773 : :
2774 : 0 : static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2775 : : {
2776 : 0 : return may_create(dir, dentry, SECCLASS_LNK_FILE);
2777 : : }
2778 : :
2779 : 0 : static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
2780 : : {
2781 : 0 : return may_create(dir, dentry, SECCLASS_DIR);
2782 : : }
2783 : :
2784 : 0 : static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2785 : : {
2786 : 0 : return may_link(dir, dentry, MAY_RMDIR);
2787 : : }
2788 : :
2789 : 0 : static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
2790 : : {
2791 : 0 : return may_create(dir, dentry, inode_mode_to_security_class(mode));
2792 : : }
2793 : :
2794 : 0 : static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
2795 : : struct inode *new_inode, struct dentry *new_dentry)
2796 : : {
2797 : 0 : return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2798 : : }
2799 : :
2800 : 0 : static int selinux_inode_readlink(struct dentry *dentry)
2801 : : {
2802 : 0 : const struct cred *cred = current_cred();
2803 : :
2804 : 0 : return dentry_has_perm(cred, dentry, FILE__READ);
2805 : : }
2806 : :
2807 : 0 : static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2808 : : {
2809 : 0 : const struct cred *cred = current_cred();
2810 : :
2811 : 0 : return dentry_has_perm(cred, dentry, FILE__READ);
2812 : : }
2813 : :
2814 : 0 : static noinline int audit_inode_permission(struct inode *inode,
2815 : : u32 perms, u32 audited, u32 denied,
2816 : : unsigned flags)
2817 : : {
2818 : : struct common_audit_data ad;
2819 : 0 : struct inode_security_struct *isec = inode->i_security;
2820 : : int rc;
2821 : :
2822 : 0 : ad.type = LSM_AUDIT_DATA_INODE;
2823 : 0 : ad.u.inode = inode;
2824 : :
2825 : 0 : rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
2826 : : audited, denied, &ad, flags);
2827 [ # # ]: 0 : if (rc)
2828 : 0 : return rc;
2829 : : return 0;
2830 : : }
2831 : :
2832 : 0 : static int selinux_inode_permission(struct inode *inode, int mask)
2833 : : {
2834 : 0 : const struct cred *cred = current_cred();
2835 : : u32 perms;
2836 : : bool from_access;
2837 : 0 : unsigned flags = mask & MAY_NOT_BLOCK;
2838 : : struct inode_security_struct *isec;
2839 : : u32 sid;
2840 : : struct av_decision avd;
2841 : : int rc, rc2;
2842 : : u32 audited, denied;
2843 : :
2844 : 0 : from_access = mask & MAY_ACCESS;
2845 : 0 : mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2846 : :
2847 : : /* No permission to check. Existence test. */
2848 [ # # ]: 0 : if (!mask)
2849 : : return 0;
2850 : :
2851 : : validate_creds(cred);
2852 : :
2853 [ # # ]: 0 : if (unlikely(IS_PRIVATE(inode)))
2854 : : return 0;
2855 : :
2856 : 0 : perms = file_mask_to_av(inode->i_mode, mask);
2857 : :
2858 : : sid = cred_sid(cred);
2859 : 0 : isec = inode->i_security;
2860 : :
2861 : 0 : rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
2862 [ # # ]: 0 : audited = avc_audit_required(perms, &avd, rc,
2863 : : from_access ? FILE__AUDIT_ACCESS : 0,
2864 : : &denied);
2865 [ # # ]: 0 : if (likely(!audited))
2866 : : return rc;
2867 : :
2868 : 0 : rc2 = audit_inode_permission(inode, perms, audited, denied, flags);
2869 [ # # ]: 0 : if (rc2)
2870 : : return rc2;
2871 : 0 : return rc;
2872 : : }
2873 : :
2874 : 0 : static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2875 : : {
2876 : 0 : const struct cred *cred = current_cred();
2877 : 0 : unsigned int ia_valid = iattr->ia_valid;
2878 : : __u32 av = FILE__WRITE;
2879 : :
2880 : : /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2881 [ # # ]: 0 : if (ia_valid & ATTR_FORCE) {
2882 : 0 : ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2883 : : ATTR_FORCE);
2884 [ # # ]: 0 : if (!ia_valid)
2885 : : return 0;
2886 : : }
2887 : :
2888 [ # # ]: 0 : if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2889 : : ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
2890 : 0 : return dentry_has_perm(cred, dentry, FILE__SETATTR);
2891 : :
2892 [ # # ][ # # ]: 0 : if (selinux_policycap_openperm && (ia_valid & ATTR_SIZE))
2893 : : av |= FILE__OPEN;
2894 : :
2895 : 0 : return dentry_has_perm(cred, dentry, av);
2896 : : }
2897 : :
2898 : 0 : static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2899 : : {
2900 : 0 : const struct cred *cred = current_cred();
2901 : : struct path path;
2902 : :
2903 : : path.dentry = dentry;
2904 : : path.mnt = mnt;
2905 : :
2906 : 0 : return path_has_perm(cred, &path, FILE__GETATTR);
2907 : : }
2908 : :
2909 : 0 : static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
2910 : : {
2911 : 0 : const struct cred *cred = current_cred();
2912 : :
2913 [ # # ]: 0 : if (!strncmp(name, XATTR_SECURITY_PREFIX,
2914 : : sizeof XATTR_SECURITY_PREFIX - 1)) {
2915 [ # # ]: 0 : if (!strcmp(name, XATTR_NAME_CAPS)) {
2916 [ # # ]: 0 : if (!capable(CAP_SETFCAP))
2917 : : return -EPERM;
2918 [ # # ]: 0 : } else if (!capable(CAP_SYS_ADMIN)) {
2919 : : /* A different attribute in the security namespace.
2920 : : Restrict to administrator. */
2921 : : return -EPERM;
2922 : : }
2923 : : }
2924 : :
2925 : : /* Not an attribute we recognize, so just check the
2926 : : ordinary setattr permission. */
2927 : 0 : return dentry_has_perm(cred, dentry, FILE__SETATTR);
2928 : : }
2929 : :
2930 : 0 : static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2931 : : const void *value, size_t size, int flags)
2932 : : {
2933 : 0 : struct inode *inode = dentry->d_inode;
2934 : 0 : struct inode_security_struct *isec = inode->i_security;
2935 : : struct superblock_security_struct *sbsec;
2936 : : struct common_audit_data ad;
2937 : : u32 newsid, sid = current_sid();
2938 : : int rc = 0;
2939 : :
2940 [ # # ]: 0 : if (strcmp(name, XATTR_NAME_SELINUX))
2941 : 0 : return selinux_inode_setotherxattr(dentry, name);
2942 : :
2943 : 0 : sbsec = inode->i_sb->s_security;
2944 [ # # ]: 0 : if (!(sbsec->flags & SBLABEL_MNT))
2945 : : return -EOPNOTSUPP;
2946 : :
2947 [ # # ]: 0 : if (!inode_owner_or_capable(inode))
2948 : : return -EPERM;
2949 : :
2950 : 0 : ad.type = LSM_AUDIT_DATA_DENTRY;
2951 : 0 : ad.u.dentry = dentry;
2952 : :
2953 : 0 : rc = avc_has_perm(sid, isec->sid, isec->sclass,
2954 : : FILE__RELABELFROM, &ad);
2955 [ # # ]: 0 : if (rc)
2956 : : return rc;
2957 : :
2958 : 0 : rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL);
2959 [ # # ]: 0 : if (rc == -EINVAL) {
2960 [ # # ]: 0 : if (!capable(CAP_MAC_ADMIN)) {
2961 : : struct audit_buffer *ab;
2962 : : size_t audit_size;
2963 : : const char *str;
2964 : :
2965 : : /* We strip a nul only if it is at the end, otherwise the
2966 : : * context contains a nul and we should audit that */
2967 [ # # ]: 0 : if (value) {
2968 : : str = value;
2969 [ # # ]: 0 : if (str[size - 1] == '\0')
2970 : : audit_size = size - 1;
2971 : : else
2972 : : audit_size = size;
2973 : : } else {
2974 : : str = "";
2975 : : audit_size = 0;
2976 : : }
2977 : 0 : ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
2978 : 0 : audit_log_format(ab, "op=setxattr invalid_context=");
2979 : 0 : audit_log_n_untrustedstring(ab, value, audit_size);
2980 : 0 : audit_log_end(ab);
2981 : :
2982 : 0 : return rc;
2983 : : }
2984 : 0 : rc = security_context_to_sid_force(value, size, &newsid);
2985 : : }
2986 [ # # ]: 0 : if (rc)
2987 : : return rc;
2988 : :
2989 : 0 : rc = avc_has_perm(sid, newsid, isec->sclass,
2990 : : FILE__RELABELTO, &ad);
2991 [ # # ]: 0 : if (rc)
2992 : : return rc;
2993 : :
2994 : 0 : rc = security_validate_transition(isec->sid, newsid, sid,
2995 : : isec->sclass);
2996 [ # # ]: 0 : if (rc)
2997 : : return rc;
2998 : :
2999 : 0 : return avc_has_perm(newsid,
3000 : : sbsec->sid,
3001 : : SECCLASS_FILESYSTEM,
3002 : : FILESYSTEM__ASSOCIATE,
3003 : : &ad);
3004 : : }
3005 : :
3006 : 0 : static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
3007 : : const void *value, size_t size,
3008 : : int flags)
3009 : : {
3010 : 0 : struct inode *inode = dentry->d_inode;
3011 : 0 : struct inode_security_struct *isec = inode->i_security;
3012 : : u32 newsid;
3013 : : int rc;
3014 : :
3015 [ # # ]: 0 : if (strcmp(name, XATTR_NAME_SELINUX)) {
3016 : : /* Not an attribute we recognize, so nothing to do. */
3017 : : return;
3018 : : }
3019 : :
3020 : 0 : rc = security_context_to_sid_force(value, size, &newsid);
3021 [ # # ]: 0 : if (rc) {
3022 : 0 : printk(KERN_ERR "SELinux: unable to map context to SID"
3023 : : "for (%s, %lu), rc=%d\n",
3024 : 0 : inode->i_sb->s_id, inode->i_ino, -rc);
3025 : 0 : return;
3026 : : }
3027 : :
3028 : 0 : isec->sclass = inode_mode_to_security_class(inode->i_mode);
3029 : 0 : isec->sid = newsid;
3030 : 0 : isec->initialized = 1;
3031 : :
3032 : 0 : return;
3033 : : }
3034 : :
3035 : 0 : static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
3036 : : {
3037 : 0 : const struct cred *cred = current_cred();
3038 : :
3039 : 0 : return dentry_has_perm(cred, dentry, FILE__GETATTR);
3040 : : }
3041 : :
3042 : 0 : static int selinux_inode_listxattr(struct dentry *dentry)
3043 : : {
3044 : 0 : const struct cred *cred = current_cred();
3045 : :
3046 : 0 : return dentry_has_perm(cred, dentry, FILE__GETATTR);
3047 : : }
3048 : :
3049 : 0 : static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
3050 : : {
3051 [ # # ]: 0 : if (strcmp(name, XATTR_NAME_SELINUX))
3052 : 0 : return selinux_inode_setotherxattr(dentry, name);
3053 : :
3054 : : /* No one is allowed to remove a SELinux security label.
3055 : : You can change the label, but all data must be labeled. */
3056 : : return -EACCES;
3057 : : }
3058 : :
3059 : : /*
3060 : : * Copy the inode security context value to the user.
3061 : : *
3062 : : * Permission check is handled by selinux_inode_getxattr hook.
3063 : : */
3064 : 0 : static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
3065 : : {
3066 : : u32 size;
3067 : : int error;
3068 : 0 : char *context = NULL;
3069 : 0 : struct inode_security_struct *isec = inode->i_security;
3070 : :
3071 [ # # ]: 0 : if (strcmp(name, XATTR_SELINUX_SUFFIX))
3072 : : return -EOPNOTSUPP;
3073 : :
3074 : : /*
3075 : : * If the caller has CAP_MAC_ADMIN, then get the raw context
3076 : : * value even if it is not defined by current policy; otherwise,
3077 : : * use the in-core value under current policy.
3078 : : * Use the non-auditing forms of the permission checks since
3079 : : * getxattr may be called by unprivileged processes commonly
3080 : : * and lack of permission just means that we fall back to the
3081 : : * in-core context value, not a denial.
3082 : : */
3083 : 0 : error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
3084 : : SECURITY_CAP_NOAUDIT);
3085 [ # # ]: 0 : if (!error)
3086 : 0 : error = security_sid_to_context_force(isec->sid, &context,
3087 : : &size);
3088 : : else
3089 : 0 : error = security_sid_to_context(isec->sid, &context, &size);
3090 [ # # ]: 0 : if (error)
3091 : : return error;
3092 : 0 : error = size;
3093 [ # # ]: 0 : if (alloc) {
3094 : 0 : *buffer = context;
3095 : 0 : goto out_nofree;
3096 : : }
3097 : 0 : kfree(context);
3098 : : out_nofree:
3099 : 0 : return error;
3100 : : }
3101 : :
3102 : 0 : static int selinux_inode_setsecurity(struct inode *inode, const char *name,
3103 : : const void *value, size_t size, int flags)
3104 : : {
3105 : 0 : struct inode_security_struct *isec = inode->i_security;
3106 : : u32 newsid;
3107 : : int rc;
3108 : :
3109 [ # # ]: 0 : if (strcmp(name, XATTR_SELINUX_SUFFIX))
3110 : : return -EOPNOTSUPP;
3111 : :
3112 [ # # ]: 0 : if (!value || !size)
3113 : : return -EACCES;
3114 : :
3115 : 0 : rc = security_context_to_sid((void *)value, size, &newsid, GFP_KERNEL);
3116 [ # # ]: 0 : if (rc)
3117 : : return rc;
3118 : :
3119 : 0 : isec->sclass = inode_mode_to_security_class(inode->i_mode);
3120 : 0 : isec->sid = newsid;
3121 : 0 : isec->initialized = 1;
3122 : 0 : return 0;
3123 : : }
3124 : :
3125 : 0 : static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3126 : : {
3127 : : const int len = sizeof(XATTR_NAME_SELINUX);
3128 [ # # ]: 0 : if (buffer && len <= buffer_size)
3129 : 0 : memcpy(buffer, XATTR_NAME_SELINUX, len);
3130 : 0 : return len;
3131 : : }
3132 : :
3133 : 0 : static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
3134 : : {
3135 : 0 : struct inode_security_struct *isec = inode->i_security;
3136 : 0 : *secid = isec->sid;
3137 : 0 : }
3138 : :
3139 : : /* file security operations */
3140 : :
3141 : 0 : static int selinux_revalidate_file_permission(struct file *file, int mask)
3142 : : {
3143 : 0 : const struct cred *cred = current_cred();
3144 : : struct inode *inode = file_inode(file);
3145 : :
3146 : : /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3147 [ # # ][ # # ]: 0 : if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3148 : 0 : mask |= MAY_APPEND;
3149 : :
3150 : 0 : return file_has_perm(cred, file,
3151 : 0 : file_mask_to_av(inode->i_mode, mask));
3152 : : }
3153 : :
3154 : 0 : static int selinux_file_permission(struct file *file, int mask)
3155 : : {
3156 : : struct inode *inode = file_inode(file);
3157 : 0 : struct file_security_struct *fsec = file->f_security;
3158 : 0 : struct inode_security_struct *isec = inode->i_security;
3159 : : u32 sid = current_sid();
3160 : :
3161 [ # # ]: 0 : if (!mask)
3162 : : /* No permission to check. Existence test. */
3163 : : return 0;
3164 : :
3165 [ # # ]: 0 : if (sid == fsec->sid && fsec->isid == isec->sid &&
[ # # # # ]
3166 : 0 : fsec->pseqno == avc_policy_seqno())
3167 : : /* No change since file_open check. */
3168 : : return 0;
3169 : :
3170 : 0 : return selinux_revalidate_file_permission(file, mask);
3171 : : }
3172 : :
3173 : 0 : static int selinux_file_alloc_security(struct file *file)
3174 : : {
3175 : 0 : return file_alloc_security(file);
3176 : : }
3177 : :
3178 : 0 : static void selinux_file_free_security(struct file *file)
3179 : : {
3180 : : file_free_security(file);
3181 : 0 : }
3182 : :
3183 : 0 : static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3184 : : unsigned long arg)
3185 : : {
3186 : 0 : const struct cred *cred = current_cred();
3187 : : int error = 0;
3188 : :
3189 [ # # # # : 0 : switch (cmd) {
# ]
3190 : : case FIONREAD:
3191 : : /* fall through */
3192 : : case FIBMAP:
3193 : : /* fall through */
3194 : : case FIGETBSZ:
3195 : : /* fall through */
3196 : : case FS_IOC_GETFLAGS:
3197 : : /* fall through */
3198 : : case FS_IOC_GETVERSION:
3199 : 0 : error = file_has_perm(cred, file, FILE__GETATTR);
3200 : 0 : break;
3201 : :
3202 : : case FS_IOC_SETFLAGS:
3203 : : /* fall through */
3204 : : case FS_IOC_SETVERSION:
3205 : 0 : error = file_has_perm(cred, file, FILE__SETATTR);
3206 : 0 : break;
3207 : :
3208 : : /* sys_ioctl() checks */
3209 : : case FIONBIO:
3210 : : /* fall through */
3211 : : case FIOASYNC:
3212 : 0 : error = file_has_perm(cred, file, 0);
3213 : 0 : break;
3214 : :
3215 : : case KDSKBENT:
3216 : : case KDSKBSENT:
3217 : 0 : error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3218 : : SECURITY_CAP_AUDIT);
3219 : 0 : break;
3220 : :
3221 : : /* default case assumes that the command will go
3222 : : * to the file's ioctl() function.
3223 : : */
3224 : : default:
3225 : 0 : error = file_has_perm(cred, file, FILE__IOCTL);
3226 : : }
3227 : 0 : return error;
3228 : : }
3229 : :
3230 : : static int default_noexec;
3231 : :
3232 : 0 : static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3233 : : {
3234 : 0 : const struct cred *cred = current_cred();
3235 : : int rc = 0;
3236 : :
3237 [ # # ][ # # ]: 0 : if (default_noexec &&
3238 [ # # ][ # # ]: 0 : (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
[ # # ]
3239 : : /*
3240 : : * We are making executable an anonymous mapping or a
3241 : : * private file mapping that will also be writable.
3242 : : * This has an additional check.
3243 : : */
3244 : : rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
3245 [ # # ]: 0 : if (rc)
3246 : : goto error;
3247 : : }
3248 : :
3249 [ # # ]: 0 : if (file) {
3250 : : /* read access is always possible with a mapping */
3251 : : u32 av = FILE__READ;
3252 : :
3253 : : /* write access only matters if the mapping is shared */
3254 [ # # ][ # # ]: 0 : if (shared && (prot & PROT_WRITE))
3255 : : av |= FILE__WRITE;
3256 : :
3257 [ # # ]: 0 : if (prot & PROT_EXEC)
3258 : 0 : av |= FILE__EXECUTE;
3259 : :
3260 : 0 : return file_has_perm(cred, file, av);
3261 : : }
3262 : :
3263 : : error:
3264 : 0 : return rc;
3265 : : }
3266 : :
3267 : 0 : static int selinux_mmap_addr(unsigned long addr)
3268 : : {
3269 : : int rc = 0;
3270 : : u32 sid = current_sid();
3271 : :
3272 : : /*
3273 : : * notice that we are intentionally putting the SELinux check before
3274 : : * the secondary cap_file_mmap check. This is such a likely attempt
3275 : : * at bad behaviour/exploit that we always want to get the AVC, even
3276 : : * if DAC would have also denied the operation.
3277 : : */
3278 : : if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3279 : : rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3280 : : MEMPROTECT__MMAP_ZERO, NULL);
3281 : : if (rc)
3282 : : return rc;
3283 : : }
3284 : :
3285 : : /* do DAC check on address space usage */
3286 : 0 : return cap_mmap_addr(addr);
3287 : : }
3288 : :
3289 : 0 : static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3290 : : unsigned long prot, unsigned long flags)
3291 : : {
3292 [ # # ]: 0 : if (selinux_checkreqprot)
3293 : : prot = reqprot;
3294 : :
3295 : 0 : return file_map_prot_check(file, prot,
3296 : 0 : (flags & MAP_TYPE) == MAP_SHARED);
3297 : : }
3298 : :
3299 : 0 : static int selinux_file_mprotect(struct vm_area_struct *vma,
3300 : : unsigned long reqprot,
3301 : : unsigned long prot)
3302 : : {
3303 : 0 : const struct cred *cred = current_cred();
3304 : :
3305 [ # # ]: 0 : if (selinux_checkreqprot)
3306 : : prot = reqprot;
3307 : :
3308 [ # # ][ # # ]: 0 : if (default_noexec &&
3309 [ # # ]: 0 : (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3310 : : int rc = 0;
3311 [ # # ][ # # ]: 0 : if (vma->vm_start >= vma->vm_mm->start_brk &&
3312 : 0 : vma->vm_end <= vma->vm_mm->brk) {
3313 : 0 : rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
3314 [ # # ][ # # ]: 0 : } else if (!vma->vm_file &&
3315 [ # # ]: 0 : vma->vm_start <= vma->vm_mm->start_stack &&
3316 : 0 : vma->vm_end >= vma->vm_mm->start_stack) {
3317 : 0 : rc = current_has_perm(current, PROCESS__EXECSTACK);
3318 [ # # ][ # # ]: 0 : } else if (vma->vm_file && vma->anon_vma) {
3319 : : /*
3320 : : * We are making executable a file mapping that has
3321 : : * had some COW done. Since pages might have been
3322 : : * written, check ability to execute the possibly
3323 : : * modified content. This typically should only
3324 : : * occur for text relocations.
3325 : : */
3326 : 0 : rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3327 : : }
3328 [ # # ]: 0 : if (rc)
3329 : : return rc;
3330 : : }
3331 : :
3332 : 0 : return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3333 : : }
3334 : :
3335 : 0 : static int selinux_file_lock(struct file *file, unsigned int cmd)
3336 : : {
3337 : 0 : const struct cred *cred = current_cred();
3338 : :
3339 : 0 : return file_has_perm(cred, file, FILE__LOCK);
3340 : : }
3341 : :
3342 : 0 : static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3343 : : unsigned long arg)
3344 : : {
3345 [ # # ][ # # ]: 0 : const struct cred *cred = current_cred();
[ # # ][ # # ]
3346 : : int err = 0;
3347 : :
3348 : : switch (cmd) {
3349 : : case F_SETFL:
3350 [ # # ][ # # ]: 0 : if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
3351 : 0 : err = file_has_perm(cred, file, FILE__WRITE);
3352 : 0 : break;
3353 : : }
3354 : : /* fall through */
3355 : : case F_SETOWN:
3356 : : case F_SETSIG:
3357 : : case F_GETFL:
3358 : : case F_GETOWN:
3359 : : case F_GETSIG:
3360 : : case F_GETOWNER_UIDS:
3361 : : /* Just check FD__USE permission */
3362 : 0 : err = file_has_perm(cred, file, 0);
3363 : 0 : break;
3364 : : case F_GETLK:
3365 : : case F_SETLK:
3366 : : case F_SETLKW:
3367 : : #if BITS_PER_LONG == 32
3368 : : case F_GETLK64:
3369 : : case F_SETLK64:
3370 : : case F_SETLKW64:
3371 : : #endif
3372 : 0 : err = file_has_perm(cred, file, FILE__LOCK);
3373 : 0 : break;
3374 : : }
3375 : :
3376 : 0 : return err;
3377 : : }
3378 : :
3379 : 0 : static int selinux_file_set_fowner(struct file *file)
3380 : : {
3381 : : struct file_security_struct *fsec;
3382 : :
3383 : 0 : fsec = file->f_security;
3384 : 0 : fsec->fown_sid = current_sid();
3385 : :
3386 : 0 : return 0;
3387 : : }
3388 : :
3389 : 0 : static int selinux_file_send_sigiotask(struct task_struct *tsk,
3390 : : struct fown_struct *fown, int signum)
3391 : : {
3392 : : struct file *file;
3393 : : u32 sid = task_sid(tsk);
3394 : : u32 perm;
3395 : : struct file_security_struct *fsec;
3396 : :
3397 : : /* struct fown_struct is never outside the context of a struct file */
3398 : : file = container_of(fown, struct file, f_owner);
3399 : :
3400 : 0 : fsec = file->f_security;
3401 : :
3402 [ # # ]: 0 : if (!signum)
3403 : : perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3404 : : else
3405 : : perm = signal_to_av(signum);
3406 : :
3407 : 0 : return avc_has_perm(fsec->fown_sid, sid,
3408 : : SECCLASS_PROCESS, perm, NULL);
3409 : : }
3410 : :
3411 : 0 : static int selinux_file_receive(struct file *file)
3412 : : {
3413 : 0 : const struct cred *cred = current_cred();
3414 : :
3415 : 0 : return file_has_perm(cred, file, file_to_av(file));
3416 : : }
3417 : :
3418 : 0 : static int selinux_file_open(struct file *file, const struct cred *cred)
3419 : : {
3420 : : struct file_security_struct *fsec;
3421 : : struct inode_security_struct *isec;
3422 : :
3423 : 0 : fsec = file->f_security;
3424 : 0 : isec = file_inode(file)->i_security;
3425 : : /*
3426 : : * Save inode label and policy sequence number
3427 : : * at open-time so that selinux_file_permission
3428 : : * can determine whether revalidation is necessary.
3429 : : * Task label is already saved in the file security
3430 : : * struct as its SID.
3431 : : */
3432 : 0 : fsec->isid = isec->sid;
3433 : 0 : fsec->pseqno = avc_policy_seqno();
3434 : : /*
3435 : : * Since the inode label or policy seqno may have changed
3436 : : * between the selinux_inode_permission check and the saving
3437 : : * of state above, recheck that access is still permitted.
3438 : : * Otherwise, access might never be revalidated against the
3439 : : * new inode label or new policy.
3440 : : * This check is not redundant - do not remove.
3441 : : */
3442 : 0 : return file_path_has_perm(cred, file, open_file_to_av(file));
3443 : : }
3444 : :
3445 : : /* task security operations */
3446 : :
3447 : 0 : static int selinux_task_create(unsigned long clone_flags)
3448 : : {
3449 : 0 : return current_has_perm(current, PROCESS__FORK);
3450 : : }
3451 : :
3452 : : /*
3453 : : * allocate the SELinux part of blank credentials
3454 : : */
3455 : 0 : static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3456 : : {
3457 : : struct task_security_struct *tsec;
3458 : :
3459 : : tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3460 [ # # ]: 0 : if (!tsec)
3461 : : return -ENOMEM;
3462 : :
3463 : 0 : cred->security = tsec;
3464 : 0 : return 0;
3465 : : }
3466 : :
3467 : : /*
3468 : : * detach and free the LSM part of a set of credentials
3469 : : */
3470 : 0 : static void selinux_cred_free(struct cred *cred)
3471 : : {
3472 : 0 : struct task_security_struct *tsec = cred->security;
3473 : :
3474 : : /*
3475 : : * cred->security == NULL if security_cred_alloc_blank() or
3476 : : * security_prepare_creds() returned an error.
3477 : : */
3478 [ # # ][ # # ]: 0 : BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
3479 : 0 : cred->security = (void *) 0x7UL;
3480 : 0 : kfree(tsec);
3481 : 0 : }
3482 : :
3483 : : /*
3484 : : * prepare a new set of credentials for modification
3485 : : */
3486 : 0 : static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3487 : : gfp_t gfp)
3488 : : {
3489 : : const struct task_security_struct *old_tsec;
3490 : : struct task_security_struct *tsec;
3491 : :
3492 : 0 : old_tsec = old->security;
3493 : :
3494 : 0 : tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3495 [ # # ]: 0 : if (!tsec)
3496 : : return -ENOMEM;
3497 : :
3498 : 0 : new->security = tsec;
3499 : 0 : return 0;
3500 : : }
3501 : :
3502 : : /*
3503 : : * transfer the SELinux data to a blank set of creds
3504 : : */
3505 : 0 : static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3506 : : {
3507 : 0 : const struct task_security_struct *old_tsec = old->security;
3508 : 0 : struct task_security_struct *tsec = new->security;
3509 : :
3510 : 0 : *tsec = *old_tsec;
3511 : 0 : }
3512 : :
3513 : : /*
3514 : : * set the security data for a kernel service
3515 : : * - all the creation contexts are set to unlabelled
3516 : : */
3517 : 0 : static int selinux_kernel_act_as(struct cred *new, u32 secid)
3518 : : {
3519 : 0 : struct task_security_struct *tsec = new->security;
3520 : : u32 sid = current_sid();
3521 : : int ret;
3522 : :
3523 : 0 : ret = avc_has_perm(sid, secid,
3524 : : SECCLASS_KERNEL_SERVICE,
3525 : : KERNEL_SERVICE__USE_AS_OVERRIDE,
3526 : : NULL);
3527 [ # # ]: 0 : if (ret == 0) {
3528 : 0 : tsec->sid = secid;
3529 : 0 : tsec->create_sid = 0;
3530 : 0 : tsec->keycreate_sid = 0;
3531 : 0 : tsec->sockcreate_sid = 0;
3532 : : }
3533 : 0 : return ret;
3534 : : }
3535 : :
3536 : : /*
3537 : : * set the file creation context in a security record to the same as the
3538 : : * objective context of the specified inode
3539 : : */
3540 : 0 : static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3541 : : {
3542 : 0 : struct inode_security_struct *isec = inode->i_security;
3543 : 0 : struct task_security_struct *tsec = new->security;
3544 : : u32 sid = current_sid();
3545 : : int ret;
3546 : :
3547 : 0 : ret = avc_has_perm(sid, isec->sid,
3548 : : SECCLASS_KERNEL_SERVICE,
3549 : : KERNEL_SERVICE__CREATE_FILES_AS,
3550 : : NULL);
3551 : :
3552 [ # # ]: 0 : if (ret == 0)
3553 : 0 : tsec->create_sid = isec->sid;
3554 : 0 : return ret;
3555 : : }
3556 : :
3557 : 0 : static int selinux_kernel_module_request(char *kmod_name)
3558 : : {
3559 : : u32 sid;
3560 : : struct common_audit_data ad;
3561 : :
3562 : 0 : sid = task_sid(current);
3563 : :
3564 : 0 : ad.type = LSM_AUDIT_DATA_KMOD;
3565 : 0 : ad.u.kmod_name = kmod_name;
3566 : :
3567 : 0 : return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3568 : : SYSTEM__MODULE_REQUEST, &ad);
3569 : : }
3570 : :
3571 : 0 : static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3572 : : {
3573 : 0 : return current_has_perm(p, PROCESS__SETPGID);
3574 : : }
3575 : :
3576 : 0 : static int selinux_task_getpgid(struct task_struct *p)
3577 : : {
3578 : 0 : return current_has_perm(p, PROCESS__GETPGID);
3579 : : }
3580 : :
3581 : 0 : static int selinux_task_getsid(struct task_struct *p)
3582 : : {
3583 : 0 : return current_has_perm(p, PROCESS__GETSESSION);
3584 : : }
3585 : :
3586 : 0 : static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3587 : : {
3588 : 0 : *secid = task_sid(p);
3589 : 0 : }
3590 : :
3591 : 0 : static int selinux_task_setnice(struct task_struct *p, int nice)
3592 : : {
3593 : : int rc;
3594 : :
3595 : 0 : rc = cap_task_setnice(p, nice);
3596 [ # # ]: 0 : if (rc)
3597 : : return rc;
3598 : :
3599 : 0 : return current_has_perm(p, PROCESS__SETSCHED);
3600 : : }
3601 : :
3602 : 0 : static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3603 : : {
3604 : : int rc;
3605 : :
3606 : 0 : rc = cap_task_setioprio(p, ioprio);
3607 [ # # ]: 0 : if (rc)
3608 : : return rc;
3609 : :
3610 : 0 : return current_has_perm(p, PROCESS__SETSCHED);
3611 : : }
3612 : :
3613 : 0 : static int selinux_task_getioprio(struct task_struct *p)
3614 : : {
3615 : 0 : return current_has_perm(p, PROCESS__GETSCHED);
3616 : : }
3617 : :
3618 : 0 : static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3619 : : struct rlimit *new_rlim)
3620 : : {
3621 : 0 : struct rlimit *old_rlim = p->signal->rlim + resource;
3622 : :
3623 : : /* Control the ability to change the hard limit (whether
3624 : : lowering or raising it), so that the hard limit can
3625 : : later be used as a safe reset point for the soft limit
3626 : : upon context transitions. See selinux_bprm_committing_creds. */
3627 [ # # ]: 0 : if (old_rlim->rlim_max != new_rlim->rlim_max)
3628 : 0 : return current_has_perm(p, PROCESS__SETRLIMIT);
3629 : :
3630 : : return 0;
3631 : : }
3632 : :
3633 : 0 : static int selinux_task_setscheduler(struct task_struct *p)
3634 : : {
3635 : : int rc;
3636 : :
3637 : 0 : rc = cap_task_setscheduler(p);
3638 [ # # ]: 0 : if (rc)
3639 : : return rc;
3640 : :
3641 : 0 : return current_has_perm(p, PROCESS__SETSCHED);
3642 : : }
3643 : :
3644 : 0 : static int selinux_task_getscheduler(struct task_struct *p)
3645 : : {
3646 : 0 : return current_has_perm(p, PROCESS__GETSCHED);
3647 : : }
3648 : :
3649 : 0 : static int selinux_task_movememory(struct task_struct *p)
3650 : : {
3651 : 0 : return current_has_perm(p, PROCESS__SETSCHED);
3652 : : }
3653 : :
3654 : 0 : static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3655 : : int sig, u32 secid)
3656 : : {
3657 : : u32 perm;
3658 : : int rc;
3659 : :
3660 [ # # ]: 0 : if (!sig)
3661 : : perm = PROCESS__SIGNULL; /* null signal; existence test */
3662 : : else
3663 : : perm = signal_to_av(sig);
3664 [ # # ]: 0 : if (secid)
3665 : 0 : rc = avc_has_perm(secid, task_sid(p),
3666 : : SECCLASS_PROCESS, perm, NULL);
3667 : : else
3668 : 0 : rc = current_has_perm(p, perm);
3669 : 0 : return rc;
3670 : : }
3671 : :
3672 : 0 : static int selinux_task_wait(struct task_struct *p)
3673 : : {
3674 : 0 : return task_has_perm(p, current, PROCESS__SIGCHLD);
3675 : : }
3676 : :
3677 : 0 : static void selinux_task_to_inode(struct task_struct *p,
3678 : : struct inode *inode)
3679 : : {
3680 : 0 : struct inode_security_struct *isec = inode->i_security;
3681 : : u32 sid = task_sid(p);
3682 : :
3683 : 0 : isec->sid = sid;
3684 : 0 : isec->initialized = 1;
3685 : 0 : }
3686 : :
3687 : : /* Returns error only if unable to parse addresses */
3688 : 0 : static int selinux_parse_skb_ipv4(struct sk_buff *skb,
3689 : : struct common_audit_data *ad, u8 *proto)
3690 : : {
3691 : : int offset, ihlen, ret = -EINVAL;
3692 : : struct iphdr _iph, *ih;
3693 : :
3694 : : offset = skb_network_offset(skb);
3695 : : ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3696 [ # # ]: 0 : if (ih == NULL)
3697 : : goto out;
3698 : :
3699 : 0 : ihlen = ih->ihl * 4;
3700 [ # # ]: 0 : if (ihlen < sizeof(_iph))
3701 : : goto out;
3702 : :
3703 : 0 : ad->u.net->v4info.saddr = ih->saddr;
3704 : 0 : ad->u.net->v4info.daddr = ih->daddr;
3705 : : ret = 0;
3706 : :
3707 [ # # ]: 0 : if (proto)
3708 : 0 : *proto = ih->protocol;
3709 : :
3710 [ # # # # ]: 0 : switch (ih->protocol) {
3711 : : case IPPROTO_TCP: {
3712 : : struct tcphdr _tcph, *th;
3713 : :
3714 [ # # ][ # # ]: 0 : if (ntohs(ih->frag_off) & IP_OFFSET)
3715 : : break;
3716 : :
3717 : 0 : offset += ihlen;
3718 : : th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3719 [ # # ]: 0 : if (th == NULL)
3720 : : break;
3721 : :
3722 : 0 : ad->u.net->sport = th->source;
3723 : 0 : ad->u.net->dport = th->dest;
3724 : : break;
3725 : : }
3726 : :
3727 : : case IPPROTO_UDP: {
3728 : : struct udphdr _udph, *uh;
3729 : :
3730 [ # # ][ # # ]: 0 : if (ntohs(ih->frag_off) & IP_OFFSET)
3731 : : break;
3732 : :
3733 : 0 : offset += ihlen;
3734 : : uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3735 [ # # ]: 0 : if (uh == NULL)
3736 : : break;
3737 : :
3738 : 0 : ad->u.net->sport = uh->source;
3739 : 0 : ad->u.net->dport = uh->dest;
3740 : : break;
3741 : : }
3742 : :
3743 : : case IPPROTO_DCCP: {
3744 : : struct dccp_hdr _dccph, *dh;
3745 : :
3746 [ # # ][ # # ]: 0 : if (ntohs(ih->frag_off) & IP_OFFSET)
3747 : : break;
3748 : :
3749 : 0 : offset += ihlen;
3750 : : dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3751 [ # # ]: 0 : if (dh == NULL)
3752 : : break;
3753 : :
3754 : 0 : ad->u.net->sport = dh->dccph_sport;
3755 : 0 : ad->u.net->dport = dh->dccph_dport;
3756 : : break;
3757 : : }
3758 : :
3759 : : default:
3760 : : break;
3761 : : }
3762 : : out:
3763 : 0 : return ret;
3764 : : }
3765 : :
3766 : : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3767 : :
3768 : : /* Returns error only if unable to parse addresses */
3769 : 0 : static int selinux_parse_skb_ipv6(struct sk_buff *skb,
3770 : : struct common_audit_data *ad, u8 *proto)
3771 : : {
3772 : : u8 nexthdr;
3773 : : int ret = -EINVAL, offset;
3774 : : struct ipv6hdr _ipv6h, *ip6;
3775 : : __be16 frag_off;
3776 : :
3777 : : offset = skb_network_offset(skb);
3778 : : ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3779 [ # # ]: 0 : if (ip6 == NULL)
3780 : : goto out;
3781 : :
3782 : 0 : ad->u.net->v6info.saddr = ip6->saddr;
3783 : 0 : ad->u.net->v6info.daddr = ip6->daddr;
3784 : : ret = 0;
3785 : :
3786 : 0 : nexthdr = ip6->nexthdr;
3787 : 0 : offset += sizeof(_ipv6h);
3788 : 0 : offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3789 [ # # ]: 0 : if (offset < 0)
3790 : : goto out;
3791 : :
3792 [ # # ]: 0 : if (proto)
3793 : 0 : *proto = nexthdr;
3794 : :
3795 [ # # # # ]: 0 : switch (nexthdr) {
3796 : : case IPPROTO_TCP: {
3797 : : struct tcphdr _tcph, *th;
3798 : :
3799 : : th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3800 [ # # ]: 0 : if (th == NULL)
3801 : : break;
3802 : :
3803 : 0 : ad->u.net->sport = th->source;
3804 : 0 : ad->u.net->dport = th->dest;
3805 : : break;
3806 : : }
3807 : :
3808 : : case IPPROTO_UDP: {
3809 : : struct udphdr _udph, *uh;
3810 : :
3811 : : uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3812 [ # # ]: 0 : if (uh == NULL)
3813 : : break;
3814 : :
3815 : 0 : ad->u.net->sport = uh->source;
3816 : 0 : ad->u.net->dport = uh->dest;
3817 : : break;
3818 : : }
3819 : :
3820 : : case IPPROTO_DCCP: {
3821 : : struct dccp_hdr _dccph, *dh;
3822 : :
3823 : : dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3824 [ # # ]: 0 : if (dh == NULL)
3825 : : break;
3826 : :
3827 : 0 : ad->u.net->sport = dh->dccph_sport;
3828 : 0 : ad->u.net->dport = dh->dccph_dport;
3829 : : break;
3830 : : }
3831 : :
3832 : : /* includes fragments */
3833 : : default:
3834 : : break;
3835 : : }
3836 : : out:
3837 : 0 : return ret;
3838 : : }
3839 : :
3840 : : #endif /* IPV6 */
3841 : :
3842 : 0 : static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
3843 : : char **_addrp, int src, u8 *proto)
3844 : : {
3845 : : char *addrp;
3846 : : int ret;
3847 : :
3848 [ # # # ]: 0 : switch (ad->u.net->family) {
3849 : : case PF_INET:
3850 : 0 : ret = selinux_parse_skb_ipv4(skb, ad, proto);
3851 [ # # ]: 0 : if (ret)
3852 : : goto parse_error;
3853 [ # # ]: 0 : addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3854 : 0 : &ad->u.net->v4info.daddr);
3855 : 0 : goto okay;
3856 : :
3857 : : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3858 : : case PF_INET6:
3859 : 0 : ret = selinux_parse_skb_ipv6(skb, ad, proto);
3860 [ # # ]: 0 : if (ret)
3861 : : goto parse_error;
3862 [ # # ]: 0 : addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3863 : 0 : &ad->u.net->v6info.daddr);
3864 : 0 : goto okay;
3865 : : #endif /* IPV6 */
3866 : : default:
3867 : : addrp = NULL;
3868 : : goto okay;
3869 : : }
3870 : :
3871 : : parse_error:
3872 : 0 : printk(KERN_WARNING
3873 : : "SELinux: failure in selinux_parse_skb(),"
3874 : : " unable to parse packet\n");
3875 : 0 : return ret;
3876 : :
3877 : : okay:
3878 [ # # ]: 0 : if (_addrp)
3879 : 0 : *_addrp = addrp;
3880 : : return 0;
3881 : : }
3882 : :
3883 : : /**
3884 : : * selinux_skb_peerlbl_sid - Determine the peer label of a packet
3885 : : * @skb: the packet
3886 : : * @family: protocol family
3887 : : * @sid: the packet's peer label SID
3888 : : *
3889 : : * Description:
3890 : : * Check the various different forms of network peer labeling and determine
3891 : : * the peer label/SID for the packet; most of the magic actually occurs in
3892 : : * the security server function security_net_peersid_cmp(). The function
3893 : : * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3894 : : * or -EACCES if @sid is invalid due to inconsistencies with the different
3895 : : * peer labels.
3896 : : *
3897 : : */
3898 : 0 : static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
3899 : : {
3900 : : int err;
3901 : : u32 xfrm_sid;
3902 : : u32 nlbl_sid;
3903 : : u32 nlbl_type;
3904 : :
3905 : : err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
3906 : : if (unlikely(err))
3907 : : return -EACCES;
3908 : 0 : err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
3909 [ # # ]: 0 : if (unlikely(err))
3910 : : return -EACCES;
3911 : :
3912 : 0 : err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3913 [ # # ]: 0 : if (unlikely(err)) {
3914 : 0 : printk(KERN_WARNING
3915 : : "SELinux: failure in selinux_skb_peerlbl_sid(),"
3916 : : " unable to determine packet's peer label\n");
3917 : 0 : return -EACCES;
3918 : : }
3919 : :
3920 : : return 0;
3921 : : }
3922 : :
3923 : : /**
3924 : : * selinux_conn_sid - Determine the child socket label for a connection
3925 : : * @sk_sid: the parent socket's SID
3926 : : * @skb_sid: the packet's SID
3927 : : * @conn_sid: the resulting connection SID
3928 : : *
3929 : : * If @skb_sid is valid then the user:role:type information from @sk_sid is
3930 : : * combined with the MLS information from @skb_sid in order to create
3931 : : * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
3932 : : * of @sk_sid. Returns zero on success, negative values on failure.
3933 : : *
3934 : : */
3935 : : static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
3936 : : {
3937 : : int err = 0;
3938 : :
3939 [ # # ][ # # ]: 0 : if (skb_sid != SECSID_NULL)
3940 : 0 : err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
3941 : : else
3942 : 0 : *conn_sid = sk_sid;
3943 : :
3944 : : return err;
3945 : : }
3946 : :
3947 : : /* socket security operations */
3948 : :
3949 : : static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3950 : : u16 secclass, u32 *socksid)
3951 : : {
3952 [ # # ][ # # ]: 0 : if (tsec->sockcreate_sid > SECSID_NULL) {
3953 : 0 : *socksid = tsec->sockcreate_sid;
3954 : : return 0;
3955 : : }
3956 : :
3957 : 0 : return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3958 : : socksid);
3959 : : }
3960 : :
3961 : 0 : static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
3962 : : {
3963 : 0 : struct sk_security_struct *sksec = sk->sk_security;
3964 : : struct common_audit_data ad;
3965 : 0 : struct lsm_network_audit net = {0,};
3966 : : u32 tsid = task_sid(task);
3967 : :
3968 [ # # ]: 0 : if (sksec->sid == SECINITSID_KERNEL)
3969 : : return 0;
3970 : :
3971 : 0 : ad.type = LSM_AUDIT_DATA_NET;
3972 : 0 : ad.u.net = &net;
3973 : 0 : ad.u.net->sk = sk;
3974 : :
3975 : 0 : return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
3976 : : }
3977 : :
3978 : 0 : static int selinux_socket_create(int family, int type,
3979 : : int protocol, int kern)
3980 : : {
3981 : 0 : const struct task_security_struct *tsec = current_security();
3982 : : u32 newsid;
3983 : : u16 secclass;
3984 : : int rc;
3985 : :
3986 [ # # ]: 0 : if (kern)
3987 : : return 0;
3988 : :
3989 : : secclass = socket_type_to_security_class(family, type, protocol);
3990 : : rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3991 [ # # ]: 0 : if (rc)
3992 : : return rc;
3993 : :
3994 : 0 : return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
3995 : : }
3996 : :
3997 : 0 : static int selinux_socket_post_create(struct socket *sock, int family,
3998 : : int type, int protocol, int kern)
3999 : : {
4000 : 0 : const struct task_security_struct *tsec = current_security();
4001 : 0 : struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
4002 : : struct sk_security_struct *sksec;
4003 : : int err = 0;
4004 : :
4005 : 0 : isec->sclass = socket_type_to_security_class(family, type, protocol);
4006 : :
4007 [ # # ]: 0 : if (kern)
4008 : 0 : isec->sid = SECINITSID_KERNEL;
4009 : : else {
4010 : 0 : err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
4011 [ # # ]: 0 : if (err)
4012 : : return err;
4013 : : }
4014 : :
4015 : 0 : isec->initialized = 1;
4016 : :
4017 [ # # ]: 0 : if (sock->sk) {
4018 : 0 : sksec = sock->sk->sk_security;
4019 : 0 : sksec->sid = isec->sid;
4020 : 0 : sksec->sclass = isec->sclass;
4021 : 0 : err = selinux_netlbl_socket_post_create(sock->sk, family);
4022 : : }
4023 : :
4024 : 0 : return err;
4025 : : }
4026 : :
4027 : : /* Range of port numbers used to automatically bind.
4028 : : Need to determine whether we should perform a name_bind
4029 : : permission check between the socket and the port number. */
4030 : :
4031 : 0 : static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4032 : : {
4033 : 0 : struct sock *sk = sock->sk;
4034 : : u16 family;
4035 : : int err;
4036 : :
4037 : 0 : err = sock_has_perm(current, sk, SOCKET__BIND);
4038 [ # # ]: 0 : if (err)
4039 : : goto out;
4040 : :
4041 : : /*
4042 : : * If PF_INET or PF_INET6, check name_bind permission for the port.
4043 : : * Multiple address binding for SCTP is not supported yet: we just
4044 : : * check the first address now.
4045 : : */
4046 : 0 : family = sk->sk_family;
4047 [ # # ]: 0 : if (family == PF_INET || family == PF_INET6) {
4048 : : char *addrp;
4049 : 0 : struct sk_security_struct *sksec = sk->sk_security;
4050 : : struct common_audit_data ad;
4051 : 0 : struct lsm_network_audit net = {0,};
4052 : : struct sockaddr_in *addr4 = NULL;
4053 : : struct sockaddr_in6 *addr6 = NULL;
4054 : : unsigned short snum;
4055 : : u32 sid, node_perm;
4056 : :
4057 [ # # ]: 0 : if (family == PF_INET) {
4058 : : addr4 = (struct sockaddr_in *)address;
4059 [ # # ]: 0 : snum = ntohs(addr4->sin_port);
4060 : 0 : addrp = (char *)&addr4->sin_addr.s_addr;
4061 : : } else {
4062 : : addr6 = (struct sockaddr_in6 *)address;
4063 [ # # ]: 0 : snum = ntohs(addr6->sin6_port);
4064 : 0 : addrp = (char *)&addr6->sin6_addr.s6_addr;
4065 : : }
4066 : :
4067 [ # # ]: 0 : if (snum) {
4068 : : int low, high;
4069 : :
4070 : 0 : inet_get_local_port_range(sock_net(sk), &low, &high);
4071 : :
4072 [ # # ][ # # ]: 0 : if (snum < max(PROT_SOCK, low) || snum > high) {
4073 : 0 : err = sel_netport_sid(sk->sk_protocol,
4074 : : snum, &sid);
4075 [ # # ]: 0 : if (err)
4076 : : goto out;
4077 : 0 : ad.type = LSM_AUDIT_DATA_NET;
4078 : 0 : ad.u.net = &net;
4079 [ # # ]: 0 : ad.u.net->sport = htons(snum);
4080 : 0 : ad.u.net->family = family;
4081 : 0 : err = avc_has_perm(sksec->sid, sid,
4082 : : sksec->sclass,
4083 : : SOCKET__NAME_BIND, &ad);
4084 [ # # ]: 0 : if (err)
4085 : : goto out;
4086 : : }
4087 : : }
4088 : :
4089 [ # # # # ]: 0 : switch (sksec->sclass) {
4090 : : case SECCLASS_TCP_SOCKET:
4091 : : node_perm = TCP_SOCKET__NODE_BIND;
4092 : : break;
4093 : :
4094 : : case SECCLASS_UDP_SOCKET:
4095 : : node_perm = UDP_SOCKET__NODE_BIND;
4096 : 0 : break;
4097 : :
4098 : : case SECCLASS_DCCP_SOCKET:
4099 : : node_perm = DCCP_SOCKET__NODE_BIND;
4100 : 0 : break;
4101 : :
4102 : : default:
4103 : : node_perm = RAWIP_SOCKET__NODE_BIND;
4104 : 0 : break;
4105 : : }
4106 : :
4107 : 0 : err = sel_netnode_sid(addrp, family, &sid);
4108 [ # # ]: 0 : if (err)
4109 : : goto out;
4110 : :
4111 : 0 : ad.type = LSM_AUDIT_DATA_NET;
4112 : 0 : ad.u.net = &net;
4113 [ # # ]: 0 : ad.u.net->sport = htons(snum);
4114 : 0 : ad.u.net->family = family;
4115 : :
4116 [ # # ]: 0 : if (family == PF_INET)
4117 : 0 : ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
4118 : : else
4119 : 0 : ad.u.net->v6info.saddr = addr6->sin6_addr;
4120 : :
4121 : 0 : err = avc_has_perm(sksec->sid, sid,
4122 : : sksec->sclass, node_perm, &ad);
4123 [ # # ]: 0 : if (err)
4124 : : goto out;
4125 : : }
4126 : : out:
4127 : 0 : return err;
4128 : : }
4129 : :
4130 : 0 : static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
4131 : : {
4132 : 0 : struct sock *sk = sock->sk;
4133 : 0 : struct sk_security_struct *sksec = sk->sk_security;
4134 : : int err;
4135 : :
4136 : 0 : err = sock_has_perm(current, sk, SOCKET__CONNECT);
4137 [ # # ]: 0 : if (err)
4138 : : return err;
4139 : :
4140 : : /*
4141 : : * If a TCP or DCCP socket, check name_connect permission for the port.
4142 : : */
4143 [ # # ]: 0 : if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4144 : : sksec->sclass == SECCLASS_DCCP_SOCKET) {
4145 : : struct common_audit_data ad;
4146 : 0 : struct lsm_network_audit net = {0,};
4147 : : struct sockaddr_in *addr4 = NULL;
4148 : : struct sockaddr_in6 *addr6 = NULL;
4149 : : unsigned short snum;
4150 : : u32 sid, perm;
4151 : :
4152 [ # # ]: 0 : if (sk->sk_family == PF_INET) {
4153 : : addr4 = (struct sockaddr_in *)address;
4154 [ # # ]: 0 : if (addrlen < sizeof(struct sockaddr_in))
4155 : 0 : return -EINVAL;
4156 [ # # ]: 0 : snum = ntohs(addr4->sin_port);
4157 : : } else {
4158 : : addr6 = (struct sockaddr_in6 *)address;
4159 [ # # ]: 0 : if (addrlen < SIN6_LEN_RFC2133)
4160 : : return -EINVAL;
4161 [ # # ]: 0 : snum = ntohs(addr6->sin6_port);
4162 : : }
4163 : :
4164 : 0 : err = sel_netport_sid(sk->sk_protocol, snum, &sid);
4165 [ # # ]: 0 : if (err)
4166 : : goto out;
4167 : :
4168 [ # # ]: 0 : perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
4169 : : TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
4170 : :
4171 : 0 : ad.type = LSM_AUDIT_DATA_NET;
4172 : 0 : ad.u.net = &net;
4173 [ # # ]: 0 : ad.u.net->dport = htons(snum);
4174 : 0 : ad.u.net->family = sk->sk_family;
4175 : 0 : err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
4176 [ # # ]: 0 : if (err)
4177 : : goto out;
4178 : : }
4179 : :
4180 : 0 : err = selinux_netlbl_socket_connect(sk, address);
4181 : :
4182 : : out:
4183 : 0 : return err;
4184 : : }
4185 : :
4186 : 0 : static int selinux_socket_listen(struct socket *sock, int backlog)
4187 : : {
4188 : 0 : return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
4189 : : }
4190 : :
4191 : 0 : static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4192 : : {
4193 : : int err;
4194 : : struct inode_security_struct *isec;
4195 : : struct inode_security_struct *newisec;
4196 : :
4197 : 0 : err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
4198 [ # # ]: 0 : if (err)
4199 : : return err;
4200 : :
4201 : 0 : newisec = SOCK_INODE(newsock)->i_security;
4202 : :
4203 : 0 : isec = SOCK_INODE(sock)->i_security;
4204 : 0 : newisec->sclass = isec->sclass;
4205 : 0 : newisec->sid = isec->sid;
4206 : 0 : newisec->initialized = 1;
4207 : :
4208 : 0 : return 0;
4209 : : }
4210 : :
4211 : 0 : static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
4212 : : int size)
4213 : : {
4214 : 0 : return sock_has_perm(current, sock->sk, SOCKET__WRITE);
4215 : : }
4216 : :
4217 : 0 : static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4218 : : int size, int flags)
4219 : : {
4220 : 0 : return sock_has_perm(current, sock->sk, SOCKET__READ);
4221 : : }
4222 : :
4223 : 0 : static int selinux_socket_getsockname(struct socket *sock)
4224 : : {
4225 : 0 : return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
4226 : : }
4227 : :
4228 : 0 : static int selinux_socket_getpeername(struct socket *sock)
4229 : : {
4230 : 0 : return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
4231 : : }
4232 : :
4233 : 0 : static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
4234 : : {
4235 : : int err;
4236 : :
4237 : 0 : err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
4238 [ # # ]: 0 : if (err)
4239 : : return err;
4240 : :
4241 : 0 : return selinux_netlbl_socket_setsockopt(sock, level, optname);
4242 : : }
4243 : :
4244 : 0 : static int selinux_socket_getsockopt(struct socket *sock, int level,
4245 : : int optname)
4246 : : {
4247 : 0 : return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
4248 : : }
4249 : :
4250 : 0 : static int selinux_socket_shutdown(struct socket *sock, int how)
4251 : : {
4252 : 0 : return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
4253 : : }
4254 : :
4255 : 0 : static int selinux_socket_unix_stream_connect(struct sock *sock,
4256 : : struct sock *other,
4257 : : struct sock *newsk)
4258 : : {
4259 : 0 : struct sk_security_struct *sksec_sock = sock->sk_security;
4260 : 0 : struct sk_security_struct *sksec_other = other->sk_security;
4261 : 0 : struct sk_security_struct *sksec_new = newsk->sk_security;
4262 : : struct common_audit_data ad;
4263 : 0 : struct lsm_network_audit net = {0,};
4264 : : int err;
4265 : :
4266 : 0 : ad.type = LSM_AUDIT_DATA_NET;
4267 : 0 : ad.u.net = &net;
4268 : 0 : ad.u.net->sk = other;
4269 : :
4270 : 0 : err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4271 : : sksec_other->sclass,
4272 : : UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4273 [ # # ]: 0 : if (err)
4274 : : return err;
4275 : :
4276 : : /* server child socket */
4277 : 0 : sksec_new->peer_sid = sksec_sock->sid;
4278 : 0 : err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4279 : : &sksec_new->sid);
4280 [ # # ]: 0 : if (err)
4281 : : return err;
4282 : :
4283 : : /* connecting socket */
4284 : 0 : sksec_sock->peer_sid = sksec_new->sid;
4285 : :
4286 : 0 : return 0;
4287 : : }
4288 : :
4289 : 0 : static int selinux_socket_unix_may_send(struct socket *sock,
4290 : : struct socket *other)
4291 : : {
4292 : 0 : struct sk_security_struct *ssec = sock->sk->sk_security;
4293 : 0 : struct sk_security_struct *osec = other->sk->sk_security;
4294 : : struct common_audit_data ad;
4295 : 0 : struct lsm_network_audit net = {0,};
4296 : :
4297 : 0 : ad.type = LSM_AUDIT_DATA_NET;
4298 : 0 : ad.u.net = &net;
4299 : 0 : ad.u.net->sk = other->sk;
4300 : :
4301 : 0 : return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4302 : : &ad);
4303 : : }
4304 : :
4305 : 0 : static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4306 : : u32 peer_sid,
4307 : : struct common_audit_data *ad)
4308 : : {
4309 : : int err;
4310 : : u32 if_sid;
4311 : : u32 node_sid;
4312 : :
4313 : 0 : err = sel_netif_sid(ifindex, &if_sid);
4314 [ # # ]: 0 : if (err)
4315 : : return err;
4316 : 0 : err = avc_has_perm(peer_sid, if_sid,
4317 : : SECCLASS_NETIF, NETIF__INGRESS, ad);
4318 [ # # ]: 0 : if (err)
4319 : : return err;
4320 : :
4321 : 0 : err = sel_netnode_sid(addrp, family, &node_sid);
4322 [ # # ]: 0 : if (err)
4323 : : return err;
4324 : 0 : return avc_has_perm(peer_sid, node_sid,
4325 : : SECCLASS_NODE, NODE__RECVFROM, ad);
4326 : : }
4327 : :
4328 : 0 : static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4329 : : u16 family)
4330 : : {
4331 : : int err = 0;
4332 : 0 : struct sk_security_struct *sksec = sk->sk_security;
4333 : 0 : u32 sk_sid = sksec->sid;
4334 : : struct common_audit_data ad;
4335 : 0 : struct lsm_network_audit net = {0,};
4336 : : char *addrp;
4337 : :
4338 : 0 : ad.type = LSM_AUDIT_DATA_NET;
4339 : 0 : ad.u.net = &net;
4340 : 0 : ad.u.net->netif = skb->skb_iif;
4341 : 0 : ad.u.net->family = family;
4342 : 0 : err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4343 [ # # ]: 0 : if (err)
4344 : : return err;
4345 : :
4346 [ # # ]: 0 : if (selinux_secmark_enabled()) {
4347 : 0 : err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4348 : : PACKET__RECV, &ad);
4349 [ # # ]: 0 : if (err)
4350 : : return err;
4351 : : }
4352 : :
4353 : 0 : err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4354 [ # # ]: 0 : if (err)
4355 : 0 : return err;
4356 : : err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4357 : :
4358 : : return err;
4359 : : }
4360 : :
4361 : 0 : static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4362 : : {
4363 : : int err;
4364 : 0 : struct sk_security_struct *sksec = sk->sk_security;
4365 : 0 : u16 family = sk->sk_family;
4366 : 0 : u32 sk_sid = sksec->sid;
4367 : : struct common_audit_data ad;
4368 : 0 : struct lsm_network_audit net = {0,};
4369 : : char *addrp;
4370 : : u8 secmark_active;
4371 : : u8 peerlbl_active;
4372 : :
4373 [ # # ]: 0 : if (family != PF_INET && family != PF_INET6)
4374 : : return 0;
4375 : :
4376 : : /* Handle mapped IPv4 packets arriving via IPv6 sockets */
4377 [ # # ][ # # ]: 0 : if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4378 : : family = PF_INET;
4379 : :
4380 : : /* If any sort of compatibility mode is enabled then handoff processing
4381 : : * to the selinux_sock_rcv_skb_compat() function to deal with the
4382 : : * special handling. We do this in an attempt to keep this function
4383 : : * as fast and as clean as possible. */
4384 [ # # ]: 0 : if (!selinux_policycap_netpeer)
4385 : 0 : return selinux_sock_rcv_skb_compat(sk, skb, family);
4386 : :
4387 : 0 : secmark_active = selinux_secmark_enabled();
4388 : 0 : peerlbl_active = selinux_peerlbl_enabled();
4389 [ # # ]: 0 : if (!secmark_active && !peerlbl_active)
4390 : : return 0;
4391 : :
4392 : 0 : ad.type = LSM_AUDIT_DATA_NET;
4393 : 0 : ad.u.net = &net;
4394 : 0 : ad.u.net->netif = skb->skb_iif;
4395 : 0 : ad.u.net->family = family;
4396 : 0 : err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4397 [ # # ]: 0 : if (err)
4398 : : return err;
4399 : :
4400 [ # # ]: 0 : if (peerlbl_active) {
4401 : : u32 peer_sid;
4402 : :
4403 : 0 : err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4404 [ # # ]: 0 : if (err)
4405 : 0 : return err;
4406 : 0 : err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
4407 : : peer_sid, &ad);
4408 [ # # ]: 0 : if (err) {
4409 : 0 : selinux_netlbl_err(skb, err, 0);
4410 : 0 : return err;
4411 : : }
4412 : 0 : err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4413 : : PEER__RECV, &ad);
4414 [ # # ]: 0 : if (err) {
4415 : 0 : selinux_netlbl_err(skb, err, 0);
4416 : 0 : return err;
4417 : : }
4418 : : }
4419 : :
4420 [ # # ]: 0 : if (secmark_active) {
4421 : 0 : err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4422 : : PACKET__RECV, &ad);
4423 [ # # ]: 0 : if (err)
4424 : : return err;
4425 : : }
4426 : :
4427 : 0 : return err;
4428 : : }
4429 : :
4430 : 0 : static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4431 : : int __user *optlen, unsigned len)
4432 : : {
4433 : : int err = 0;
4434 : : char *scontext;
4435 : : u32 scontext_len;
4436 : 0 : struct sk_security_struct *sksec = sock->sk->sk_security;
4437 : : u32 peer_sid = SECSID_NULL;
4438 : :
4439 [ # # ]: 0 : if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4440 : : sksec->sclass == SECCLASS_TCP_SOCKET)
4441 : 0 : peer_sid = sksec->peer_sid;
4442 [ # # ]: 0 : if (peer_sid == SECSID_NULL)
4443 : : return -ENOPROTOOPT;
4444 : :
4445 : 0 : err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
4446 [ # # ]: 0 : if (err)
4447 : : return err;
4448 : :
4449 [ # # ]: 0 : if (scontext_len > len) {
4450 : : err = -ERANGE;
4451 : : goto out_len;
4452 : : }
4453 : :
4454 [ # # ]: 0 : if (copy_to_user(optval, scontext, scontext_len))
4455 : : err = -EFAULT;
4456 : :
4457 : : out_len:
4458 [ # # ]: 0 : if (put_user(scontext_len, optlen))
4459 : : err = -EFAULT;
4460 : 0 : kfree(scontext);
4461 : 0 : return err;
4462 : : }
4463 : :
4464 : 0 : static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
4465 : : {
4466 : 0 : u32 peer_secid = SECSID_NULL;
4467 : : u16 family;
4468 : :
4469 [ # # ][ # # ]: 0 : if (skb && skb->protocol == htons(ETH_P_IP))
4470 : : family = PF_INET;
4471 [ # # ][ # # ]: 0 : else if (skb && skb->protocol == htons(ETH_P_IPV6))
4472 : : family = PF_INET6;
4473 [ # # ]: 0 : else if (sock)
4474 : 0 : family = sock->sk->sk_family;
4475 : : else
4476 : : goto out;
4477 : :
4478 [ # # ]: 0 : if (sock && family == PF_UNIX)
4479 : : selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
4480 [ # # ]: 0 : else if (skb)
4481 : 0 : selinux_skb_peerlbl_sid(skb, family, &peer_secid);
4482 : :
4483 : : out:
4484 : 0 : *secid = peer_secid;
4485 [ # # ]: 0 : if (peer_secid == SECSID_NULL)
4486 : : return -EINVAL;
4487 : 0 : return 0;
4488 : : }
4489 : :
4490 : 0 : static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
4491 : : {
4492 : : struct sk_security_struct *sksec;
4493 : :
4494 : : sksec = kzalloc(sizeof(*sksec), priority);
4495 [ # # ]: 0 : if (!sksec)
4496 : : return -ENOMEM;
4497 : :
4498 : 0 : sksec->peer_sid = SECINITSID_UNLABELED;
4499 : 0 : sksec->sid = SECINITSID_UNLABELED;
4500 : 0 : selinux_netlbl_sk_security_reset(sksec);
4501 : 0 : sk->sk_security = sksec;
4502 : :
4503 : 0 : return 0;
4504 : : }
4505 : :
4506 : 0 : static void selinux_sk_free_security(struct sock *sk)
4507 : : {
4508 : 0 : struct sk_security_struct *sksec = sk->sk_security;
4509 : :
4510 : 0 : sk->sk_security = NULL;
4511 : 0 : selinux_netlbl_sk_security_free(sksec);
4512 : 0 : kfree(sksec);
4513 : 0 : }
4514 : :
4515 : 0 : static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4516 : : {
4517 : 0 : struct sk_security_struct *sksec = sk->sk_security;
4518 : 0 : struct sk_security_struct *newsksec = newsk->sk_security;
4519 : :
4520 : 0 : newsksec->sid = sksec->sid;
4521 : 0 : newsksec->peer_sid = sksec->peer_sid;
4522 : 0 : newsksec->sclass = sksec->sclass;
4523 : :
4524 : 0 : selinux_netlbl_sk_security_reset(newsksec);
4525 : 0 : }
4526 : :
4527 : 0 : static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
4528 : : {
4529 [ # # ]: 0 : if (!sk)
4530 : 0 : *secid = SECINITSID_ANY_SOCKET;
4531 : : else {
4532 : 0 : struct sk_security_struct *sksec = sk->sk_security;
4533 : :
4534 : 0 : *secid = sksec->sid;
4535 : : }
4536 : 0 : }
4537 : :
4538 : 0 : static void selinux_sock_graft(struct sock *sk, struct socket *parent)
4539 : : {
4540 : 0 : struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4541 : 0 : struct sk_security_struct *sksec = sk->sk_security;
4542 : :
4543 [ # # ][ # # ]: 0 : if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4544 : : sk->sk_family == PF_UNIX)
4545 : 0 : isec->sid = sksec->sid;
4546 : 0 : sksec->sclass = isec->sclass;
4547 : 0 : }
4548 : :
4549 : 0 : static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4550 : : struct request_sock *req)
4551 : : {
4552 : 0 : struct sk_security_struct *sksec = sk->sk_security;
4553 : : int err;
4554 : 0 : u16 family = req->rsk_ops->family;
4555 : : u32 connsid;
4556 : : u32 peersid;
4557 : :
4558 : 0 : err = selinux_skb_peerlbl_sid(skb, family, &peersid);
4559 [ # # ]: 0 : if (err)
4560 : : return err;
4561 : 0 : err = selinux_conn_sid(sksec->sid, peersid, &connsid);
4562 [ # # ]: 0 : if (err)
4563 : : return err;
4564 : 0 : req->secid = connsid;
4565 : 0 : req->peer_secid = peersid;
4566 : :
4567 : 0 : return selinux_netlbl_inet_conn_request(req, family);
4568 : : }
4569 : :
4570 : 0 : static void selinux_inet_csk_clone(struct sock *newsk,
4571 : : const struct request_sock *req)
4572 : : {
4573 : 0 : struct sk_security_struct *newsksec = newsk->sk_security;
4574 : :
4575 : 0 : newsksec->sid = req->secid;
4576 : 0 : newsksec->peer_sid = req->peer_secid;
4577 : : /* NOTE: Ideally, we should also get the isec->sid for the
4578 : : new socket in sync, but we don't have the isec available yet.
4579 : : So we will wait until sock_graft to do it, by which
4580 : : time it will have been created and available. */
4581 : :
4582 : : /* We don't need to take any sort of lock here as we are the only
4583 : : * thread with access to newsksec */
4584 : 0 : selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
4585 : 0 : }
4586 : :
4587 : 0 : static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
4588 : : {
4589 : 0 : u16 family = sk->sk_family;
4590 : 0 : struct sk_security_struct *sksec = sk->sk_security;
4591 : :
4592 : : /* handle mapped IPv4 packets arriving via IPv6 sockets */
4593 [ # # ][ # # ]: 0 : if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4594 : : family = PF_INET;
4595 : :
4596 : 0 : selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
4597 : 0 : }
4598 : :
4599 : 0 : static void selinux_skb_owned_by(struct sk_buff *skb, struct sock *sk)
4600 : : {
4601 : : skb_set_owner_w(skb, sk);
4602 : 0 : }
4603 : :
4604 : 0 : static int selinux_secmark_relabel_packet(u32 sid)
4605 : : {
4606 : : const struct task_security_struct *__tsec;
4607 : : u32 tsid;
4608 : :
4609 : 0 : __tsec = current_security();
4610 : 0 : tsid = __tsec->sid;
4611 : :
4612 : 0 : return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4613 : : }
4614 : :
4615 : 0 : static void selinux_secmark_refcount_inc(void)
4616 : : {
4617 : : atomic_inc(&selinux_secmark_refcount);
4618 : 0 : }
4619 : :
4620 : 0 : static void selinux_secmark_refcount_dec(void)
4621 : : {
4622 : : atomic_dec(&selinux_secmark_refcount);
4623 : 0 : }
4624 : :
4625 : 0 : static void selinux_req_classify_flow(const struct request_sock *req,
4626 : : struct flowi *fl)
4627 : : {
4628 : 0 : fl->flowi_secid = req->secid;
4629 : 0 : }
4630 : :
4631 : 0 : static int selinux_tun_dev_alloc_security(void **security)
4632 : : {
4633 : : struct tun_security_struct *tunsec;
4634 : :
4635 : : tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
4636 [ # # ]: 0 : if (!tunsec)
4637 : : return -ENOMEM;
4638 : 0 : tunsec->sid = current_sid();
4639 : :
4640 : 0 : *security = tunsec;
4641 : 0 : return 0;
4642 : : }
4643 : :
4644 : 0 : static void selinux_tun_dev_free_security(void *security)
4645 : : {
4646 : 0 : kfree(security);
4647 : 0 : }
4648 : :
4649 : 0 : static int selinux_tun_dev_create(void)
4650 : : {
4651 : : u32 sid = current_sid();
4652 : :
4653 : : /* we aren't taking into account the "sockcreate" SID since the socket
4654 : : * that is being created here is not a socket in the traditional sense,
4655 : : * instead it is a private sock, accessible only to the kernel, and
4656 : : * representing a wide range of network traffic spanning multiple
4657 : : * connections unlike traditional sockets - check the TUN driver to
4658 : : * get a better understanding of why this socket is special */
4659 : :
4660 : 0 : return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4661 : : NULL);
4662 : : }
4663 : :
4664 : 0 : static int selinux_tun_dev_attach_queue(void *security)
4665 : : {
4666 : : struct tun_security_struct *tunsec = security;
4667 : :
4668 : 0 : return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
4669 : : TUN_SOCKET__ATTACH_QUEUE, NULL);
4670 : : }
4671 : :
4672 : 0 : static int selinux_tun_dev_attach(struct sock *sk, void *security)
4673 : : {
4674 : : struct tun_security_struct *tunsec = security;
4675 : 0 : struct sk_security_struct *sksec = sk->sk_security;
4676 : :
4677 : : /* we don't currently perform any NetLabel based labeling here and it
4678 : : * isn't clear that we would want to do so anyway; while we could apply
4679 : : * labeling without the support of the TUN user the resulting labeled
4680 : : * traffic from the other end of the connection would almost certainly
4681 : : * cause confusion to the TUN user that had no idea network labeling
4682 : : * protocols were being used */
4683 : :
4684 : 0 : sksec->sid = tunsec->sid;
4685 : 0 : sksec->sclass = SECCLASS_TUN_SOCKET;
4686 : :
4687 : 0 : return 0;
4688 : : }
4689 : :
4690 : 0 : static int selinux_tun_dev_open(void *security)
4691 : : {
4692 : : struct tun_security_struct *tunsec = security;
4693 : : u32 sid = current_sid();
4694 : : int err;
4695 : :
4696 : 0 : err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
4697 : : TUN_SOCKET__RELABELFROM, NULL);
4698 [ # # ]: 0 : if (err)
4699 : : return err;
4700 : 0 : err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4701 : : TUN_SOCKET__RELABELTO, NULL);
4702 [ # # ]: 0 : if (err)
4703 : : return err;
4704 : 0 : tunsec->sid = sid;
4705 : :
4706 : 0 : return 0;
4707 : : }
4708 : :
4709 : 0 : static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4710 : : {
4711 : : int err = 0;
4712 : : u32 perm;
4713 : : struct nlmsghdr *nlh;
4714 : 0 : struct sk_security_struct *sksec = sk->sk_security;
4715 : :
4716 [ # # ]: 0 : if (skb->len < NLMSG_HDRLEN) {
4717 : : err = -EINVAL;
4718 : : goto out;
4719 : : }
4720 : : nlh = nlmsg_hdr(skb);
4721 : :
4722 : 0 : err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
4723 [ # # ]: 0 : if (err) {
4724 [ # # ]: 0 : if (err == -EINVAL) {
4725 : 0 : audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
4726 : : "SELinux: unrecognized netlink message"
4727 : : " type=%hu for sclass=%hu\n",
4728 : 0 : nlh->nlmsg_type, sksec->sclass);
4729 [ # # ][ # # ]: 0 : if (!selinux_enforcing || security_get_allow_unknown())
4730 : : err = 0;
4731 : : }
4732 : :
4733 : : /* Ignore */
4734 [ # # ]: 0 : if (err == -ENOENT)
4735 : : err = 0;
4736 : : goto out;
4737 : : }
4738 : :
4739 : 0 : err = sock_has_perm(current, sk, perm);
4740 : : out:
4741 : 0 : return err;
4742 : : }
4743 : :
4744 : : #ifdef CONFIG_NETFILTER
4745 : :
4746 : 0 : static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4747 : : u16 family)
4748 : : {
4749 : : int err;
4750 : : char *addrp;
4751 : : u32 peer_sid;
4752 : : struct common_audit_data ad;
4753 : 0 : struct lsm_network_audit net = {0,};
4754 : : u8 secmark_active;
4755 : : u8 netlbl_active;
4756 : : u8 peerlbl_active;
4757 : :
4758 [ # # ]: 0 : if (!selinux_policycap_netpeer)
4759 : : return NF_ACCEPT;
4760 : :
4761 : 0 : secmark_active = selinux_secmark_enabled();
4762 : 0 : netlbl_active = netlbl_enabled();
4763 : 0 : peerlbl_active = selinux_peerlbl_enabled();
4764 [ # # ]: 0 : if (!secmark_active && !peerlbl_active)
4765 : : return NF_ACCEPT;
4766 : :
4767 [ # # ]: 0 : if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4768 : : return NF_DROP;
4769 : :
4770 : 0 : ad.type = LSM_AUDIT_DATA_NET;
4771 : 0 : ad.u.net = &net;
4772 : 0 : ad.u.net->netif = ifindex;
4773 : 0 : ad.u.net->family = family;
4774 [ # # ]: 0 : if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4775 : : return NF_DROP;
4776 : :
4777 [ # # ]: 0 : if (peerlbl_active) {
4778 : 0 : err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4779 : : peer_sid, &ad);
4780 [ # # ]: 0 : if (err) {
4781 : 0 : selinux_netlbl_err(skb, err, 1);
4782 : 0 : return NF_DROP;
4783 : : }
4784 : : }
4785 : :
4786 [ # # ]: 0 : if (secmark_active)
4787 [ # # ]: 0 : if (avc_has_perm(peer_sid, skb->secmark,
4788 : : SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4789 : : return NF_DROP;
4790 : :
4791 [ # # ]: 0 : if (netlbl_active)
4792 : : /* we do this in the FORWARD path and not the POST_ROUTING
4793 : : * path because we want to make sure we apply the necessary
4794 : : * labeling before IPsec is applied so we can leverage AH
4795 : : * protection */
4796 [ # # ]: 0 : if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4797 : : return NF_DROP;
4798 : :
4799 : : return NF_ACCEPT;
4800 : : }
4801 : :
4802 : 0 : static unsigned int selinux_ipv4_forward(const struct nf_hook_ops *ops,
4803 : : struct sk_buff *skb,
4804 : : const struct net_device *in,
4805 : : const struct net_device *out,
4806 : : int (*okfn)(struct sk_buff *))
4807 : : {
4808 : 0 : return selinux_ip_forward(skb, in->ifindex, PF_INET);
4809 : : }
4810 : :
4811 : : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4812 : 0 : static unsigned int selinux_ipv6_forward(const struct nf_hook_ops *ops,
4813 : : struct sk_buff *skb,
4814 : : const struct net_device *in,
4815 : : const struct net_device *out,
4816 : : int (*okfn)(struct sk_buff *))
4817 : : {
4818 : 0 : return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4819 : : }
4820 : : #endif /* IPV6 */
4821 : :
4822 : 0 : static unsigned int selinux_ip_output(struct sk_buff *skb,
4823 : : u16 family)
4824 : : {
4825 : : struct sock *sk;
4826 : : u32 sid;
4827 : :
4828 [ # # ]: 0 : if (!netlbl_enabled())
4829 : : return NF_ACCEPT;
4830 : :
4831 : : /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4832 : : * because we want to make sure we apply the necessary labeling
4833 : : * before IPsec is applied so we can leverage AH protection */
4834 : 0 : sk = skb->sk;
4835 [ # # ]: 0 : if (sk) {
4836 : : struct sk_security_struct *sksec;
4837 : :
4838 [ # # ]: 0 : if (sk->sk_state == TCP_LISTEN)
4839 : : /* if the socket is the listening state then this
4840 : : * packet is a SYN-ACK packet which means it needs to
4841 : : * be labeled based on the connection/request_sock and
4842 : : * not the parent socket. unfortunately, we can't
4843 : : * lookup the request_sock yet as it isn't queued on
4844 : : * the parent socket until after the SYN-ACK is sent.
4845 : : * the "solution" is to simply pass the packet as-is
4846 : : * as any IP option based labeling should be copied
4847 : : * from the initial connection request (in the IP
4848 : : * layer). it is far from ideal, but until we get a
4849 : : * security label in the packet itself this is the
4850 : : * best we can do. */
4851 : : return NF_ACCEPT;
4852 : :
4853 : : /* standard practice, label using the parent socket */
4854 : 0 : sksec = sk->sk_security;
4855 : 0 : sid = sksec->sid;
4856 : : } else
4857 : : sid = SECINITSID_KERNEL;
4858 [ # # ]: 0 : if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4859 : : return NF_DROP;
4860 : :
4861 : 0 : return NF_ACCEPT;
4862 : : }
4863 : :
4864 : 0 : static unsigned int selinux_ipv4_output(const struct nf_hook_ops *ops,
4865 : : struct sk_buff *skb,
4866 : : const struct net_device *in,
4867 : : const struct net_device *out,
4868 : : int (*okfn)(struct sk_buff *))
4869 : : {
4870 : 0 : return selinux_ip_output(skb, PF_INET);
4871 : : }
4872 : :
4873 : 0 : static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4874 : : int ifindex,
4875 : : u16 family)
4876 : : {
4877 : 0 : struct sock *sk = skb->sk;
4878 : : struct sk_security_struct *sksec;
4879 : : struct common_audit_data ad;
4880 : 0 : struct lsm_network_audit net = {0,};
4881 : : char *addrp;
4882 : : u8 proto;
4883 : :
4884 [ # # ]: 0 : if (sk == NULL)
4885 : : return NF_ACCEPT;
4886 : 0 : sksec = sk->sk_security;
4887 : :
4888 : 0 : ad.type = LSM_AUDIT_DATA_NET;
4889 : 0 : ad.u.net = &net;
4890 : 0 : ad.u.net->netif = ifindex;
4891 : 0 : ad.u.net->family = family;
4892 [ # # ]: 0 : if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4893 : : return NF_DROP;
4894 : :
4895 [ # # ]: 0 : if (selinux_secmark_enabled())
4896 [ # # ]: 0 : if (avc_has_perm(sksec->sid, skb->secmark,
4897 : : SECCLASS_PACKET, PACKET__SEND, &ad))
4898 : : return NF_DROP_ERR(-ECONNREFUSED);
4899 : :
4900 : : if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4901 : : return NF_DROP_ERR(-ECONNREFUSED);
4902 : :
4903 : : return NF_ACCEPT;
4904 : : }
4905 : :
4906 : 0 : static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4907 : : u16 family)
4908 : : {
4909 : : u32 secmark_perm;
4910 : : u32 peer_sid;
4911 : : struct sock *sk;
4912 : : struct common_audit_data ad;
4913 : 0 : struct lsm_network_audit net = {0,};
4914 : : char *addrp;
4915 : : u8 secmark_active;
4916 : : u8 peerlbl_active;
4917 : :
4918 : : /* If any sort of compatibility mode is enabled then handoff processing
4919 : : * to the selinux_ip_postroute_compat() function to deal with the
4920 : : * special handling. We do this in an attempt to keep this function
4921 : : * as fast and as clean as possible. */
4922 [ # # ]: 0 : if (!selinux_policycap_netpeer)
4923 : 0 : return selinux_ip_postroute_compat(skb, ifindex, family);
4924 : :
4925 : 0 : secmark_active = selinux_secmark_enabled();
4926 : 0 : peerlbl_active = selinux_peerlbl_enabled();
4927 [ # # ]: 0 : if (!secmark_active && !peerlbl_active)
4928 : : return NF_ACCEPT;
4929 : :
4930 : 0 : sk = skb->sk;
4931 : :
4932 : : #ifdef CONFIG_XFRM
4933 : : /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4934 : : * packet transformation so allow the packet to pass without any checks
4935 : : * since we'll have another chance to perform access control checks
4936 : : * when the packet is on it's final way out.
4937 : : * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4938 : : * is NULL, in this case go ahead and apply access control.
4939 : : * NOTE: if this is a local socket (skb->sk != NULL) that is in the
4940 : : * TCP listening state we cannot wait until the XFRM processing
4941 : : * is done as we will miss out on the SA label if we do;
4942 : : * unfortunately, this means more work, but it is only once per
4943 : : * connection. */
4944 [ # # ][ # # ]: 0 : if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
[ # # ]
4945 [ # # ]: 0 : !(sk != NULL && sk->sk_state == TCP_LISTEN))
4946 : : return NF_ACCEPT;
4947 : : #endif
4948 : :
4949 [ # # ]: 0 : if (sk == NULL) {
4950 : : /* Without an associated socket the packet is either coming
4951 : : * from the kernel or it is being forwarded; check the packet
4952 : : * to determine which and if the packet is being forwarded
4953 : : * query the packet directly to determine the security label. */
4954 [ # # ]: 0 : if (skb->skb_iif) {
4955 : : secmark_perm = PACKET__FORWARD_OUT;
4956 [ # # ]: 0 : if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
4957 : : return NF_DROP;
4958 : : } else {
4959 : : secmark_perm = PACKET__SEND;
4960 : 0 : peer_sid = SECINITSID_KERNEL;
4961 : : }
4962 [ # # ]: 0 : } else if (sk->sk_state == TCP_LISTEN) {
4963 : : /* Locally generated packet but the associated socket is in the
4964 : : * listening state which means this is a SYN-ACK packet. In
4965 : : * this particular case the correct security label is assigned
4966 : : * to the connection/request_sock but unfortunately we can't
4967 : : * query the request_sock as it isn't queued on the parent
4968 : : * socket until after the SYN-ACK packet is sent; the only
4969 : : * viable choice is to regenerate the label like we do in
4970 : : * selinux_inet_conn_request(). See also selinux_ip_output()
4971 : : * for similar problems. */
4972 : : u32 skb_sid;
4973 : 0 : struct sk_security_struct *sksec = sk->sk_security;
4974 [ # # ]: 0 : if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
4975 : 0 : return NF_DROP;
4976 : : /* At this point, if the returned skb peerlbl is SECSID_NULL
4977 : : * and the packet has been through at least one XFRM
4978 : : * transformation then we must be dealing with the "final"
4979 : : * form of labeled IPsec packet; since we've already applied
4980 : : * all of our access controls on this packet we can safely
4981 : : * pass the packet. */
4982 [ # # ]: 0 : if (skb_sid == SECSID_NULL) {
4983 [ # # # ]: 0 : switch (family) {
4984 : : case PF_INET:
4985 [ # # ]: 0 : if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
4986 : : return NF_ACCEPT;
4987 : : break;
4988 : : case PF_INET6:
4989 [ # # ]: 0 : if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
4990 : : return NF_ACCEPT;
4991 : : default:
4992 : 0 : return NF_DROP_ERR(-ECONNREFUSED);
4993 : : }
4994 : : }
4995 [ # # ]: 0 : if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
4996 : : return NF_DROP;
4997 : : secmark_perm = PACKET__SEND;
4998 : : } else {
4999 : : /* Locally generated packet, fetch the security label from the
5000 : : * associated socket. */
5001 : 0 : struct sk_security_struct *sksec = sk->sk_security;
5002 : 0 : peer_sid = sksec->sid;
5003 : : secmark_perm = PACKET__SEND;
5004 : : }
5005 : :
5006 : 0 : ad.type = LSM_AUDIT_DATA_NET;
5007 : 0 : ad.u.net = &net;
5008 : 0 : ad.u.net->netif = ifindex;
5009 : 0 : ad.u.net->family = family;
5010 [ # # ]: 0 : if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
5011 : : return NF_DROP;
5012 : :
5013 [ # # ]: 0 : if (secmark_active)
5014 [ # # ]: 0 : if (avc_has_perm(peer_sid, skb->secmark,
5015 : : SECCLASS_PACKET, secmark_perm, &ad))
5016 : : return NF_DROP_ERR(-ECONNREFUSED);
5017 : :
5018 [ # # ]: 0 : if (peerlbl_active) {
5019 : : u32 if_sid;
5020 : : u32 node_sid;
5021 : :
5022 [ # # ]: 0 : if (sel_netif_sid(ifindex, &if_sid))
5023 : 0 : return NF_DROP;
5024 [ # # ]: 0 : if (avc_has_perm(peer_sid, if_sid,
5025 : : SECCLASS_NETIF, NETIF__EGRESS, &ad))
5026 : : return NF_DROP_ERR(-ECONNREFUSED);
5027 : :
5028 [ # # ]: 0 : if (sel_netnode_sid(addrp, family, &node_sid))
5029 : : return NF_DROP;
5030 [ # # ]: 0 : if (avc_has_perm(peer_sid, node_sid,
5031 : : SECCLASS_NODE, NODE__SENDTO, &ad))
5032 : : return NF_DROP_ERR(-ECONNREFUSED);
5033 : : }
5034 : :
5035 : : return NF_ACCEPT;
5036 : : }
5037 : :
5038 : 0 : static unsigned int selinux_ipv4_postroute(const struct nf_hook_ops *ops,
5039 : : struct sk_buff *skb,
5040 : : const struct net_device *in,
5041 : : const struct net_device *out,
5042 : : int (*okfn)(struct sk_buff *))
5043 : : {
5044 : 0 : return selinux_ip_postroute(skb, out->ifindex, PF_INET);
5045 : : }
5046 : :
5047 : : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5048 : 0 : static unsigned int selinux_ipv6_postroute(const struct nf_hook_ops *ops,
5049 : : struct sk_buff *skb,
5050 : : const struct net_device *in,
5051 : : const struct net_device *out,
5052 : : int (*okfn)(struct sk_buff *))
5053 : : {
5054 : 0 : return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
5055 : : }
5056 : : #endif /* IPV6 */
5057 : :
5058 : : #endif /* CONFIG_NETFILTER */
5059 : :
5060 : 0 : static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5061 : : {
5062 : : int err;
5063 : :
5064 : 0 : err = cap_netlink_send(sk, skb);
5065 [ # # ]: 0 : if (err)
5066 : : return err;
5067 : :
5068 : 0 : return selinux_nlmsg_perm(sk, skb);
5069 : : }
5070 : :
5071 : 0 : static int ipc_alloc_security(struct task_struct *task,
5072 : : struct kern_ipc_perm *perm,
5073 : : u16 sclass)
5074 : : {
5075 : : struct ipc_security_struct *isec;
5076 : : u32 sid;
5077 : :
5078 : : isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
5079 [ # # ]: 0 : if (!isec)
5080 : : return -ENOMEM;
5081 : :
5082 : : sid = task_sid(task);
5083 : 0 : isec->sclass = sclass;
5084 : 0 : isec->sid = sid;
5085 : 0 : perm->security = isec;
5086 : :
5087 : : return 0;
5088 : : }
5089 : :
5090 : : static void ipc_free_security(struct kern_ipc_perm *perm)
5091 : : {
5092 : 0 : struct ipc_security_struct *isec = perm->security;
5093 : 0 : perm->security = NULL;
5094 : 0 : kfree(isec);
5095 : : }
5096 : :
5097 : 0 : static int msg_msg_alloc_security(struct msg_msg *msg)
5098 : : {
5099 : : struct msg_security_struct *msec;
5100 : :
5101 : : msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
5102 [ # # ]: 0 : if (!msec)
5103 : : return -ENOMEM;
5104 : :
5105 : 0 : msec->sid = SECINITSID_UNLABELED;
5106 : 0 : msg->security = msec;
5107 : :
5108 : : return 0;
5109 : : }
5110 : :
5111 : : static void msg_msg_free_security(struct msg_msg *msg)
5112 : : {
5113 : 0 : struct msg_security_struct *msec = msg->security;
5114 : :
5115 : 0 : msg->security = NULL;
5116 : 0 : kfree(msec);
5117 : : }
5118 : :
5119 : 0 : static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
5120 : : u32 perms)
5121 : : {
5122 : : struct ipc_security_struct *isec;
5123 : : struct common_audit_data ad;
5124 : : u32 sid = current_sid();
5125 : :
5126 : 0 : isec = ipc_perms->security;
5127 : :
5128 : 0 : ad.type = LSM_AUDIT_DATA_IPC;
5129 : 0 : ad.u.ipc_id = ipc_perms->key;
5130 : :
5131 : 0 : return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
5132 : : }
5133 : :
5134 : 0 : static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5135 : : {
5136 : 0 : return msg_msg_alloc_security(msg);
5137 : : }
5138 : :
5139 : 0 : static void selinux_msg_msg_free_security(struct msg_msg *msg)
5140 : : {
5141 : : msg_msg_free_security(msg);
5142 : 0 : }
5143 : :
5144 : : /* message queue security operations */
5145 : 0 : static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
5146 : : {
5147 : : struct ipc_security_struct *isec;
5148 : : struct common_audit_data ad;
5149 : : u32 sid = current_sid();
5150 : : int rc;
5151 : :
5152 : 0 : rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
5153 [ # # ]: 0 : if (rc)
5154 : : return rc;
5155 : :
5156 : 0 : isec = msq->q_perm.security;
5157 : :
5158 : 0 : ad.type = LSM_AUDIT_DATA_IPC;
5159 : 0 : ad.u.ipc_id = msq->q_perm.key;
5160 : :
5161 : 0 : rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
5162 : : MSGQ__CREATE, &ad);
5163 [ # # ]: 0 : if (rc) {
5164 : : ipc_free_security(&msq->q_perm);
5165 : 0 : return rc;
5166 : : }
5167 : : return 0;
5168 : : }
5169 : :
5170 : 0 : static void selinux_msg_queue_free_security(struct msg_queue *msq)
5171 : : {
5172 : : ipc_free_security(&msq->q_perm);
5173 : 0 : }
5174 : :
5175 : 0 : static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
5176 : : {
5177 : : struct ipc_security_struct *isec;
5178 : : struct common_audit_data ad;
5179 : : u32 sid = current_sid();
5180 : :
5181 : 0 : isec = msq->q_perm.security;
5182 : :
5183 : 0 : ad.type = LSM_AUDIT_DATA_IPC;
5184 : 0 : ad.u.ipc_id = msq->q_perm.key;
5185 : :
5186 : 0 : return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
5187 : : MSGQ__ASSOCIATE, &ad);
5188 : : }
5189 : :
5190 : 0 : static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5191 : : {
5192 : : int err;
5193 : : int perms;
5194 : :
5195 [ # # # # : 0 : switch (cmd) {
# ]
5196 : : case IPC_INFO:
5197 : : case MSG_INFO:
5198 : : /* No specific object, just general system-wide information. */
5199 : 0 : return task_has_system(current, SYSTEM__IPC_INFO);
5200 : : case IPC_STAT:
5201 : : case MSG_STAT:
5202 : : perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5203 : : break;
5204 : : case IPC_SET:
5205 : : perms = MSGQ__SETATTR;
5206 : 0 : break;
5207 : : case IPC_RMID:
5208 : : perms = MSGQ__DESTROY;
5209 : 0 : break;
5210 : : default:
5211 : : return 0;
5212 : : }
5213 : :
5214 : 0 : err = ipc_has_perm(&msq->q_perm, perms);
5215 : 0 : return err;
5216 : : }
5217 : :
5218 : 0 : static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5219 : : {
5220 : : struct ipc_security_struct *isec;
5221 : : struct msg_security_struct *msec;
5222 : : struct common_audit_data ad;
5223 : : u32 sid = current_sid();
5224 : : int rc;
5225 : :
5226 : 0 : isec = msq->q_perm.security;
5227 : 0 : msec = msg->security;
5228 : :
5229 : : /*
5230 : : * First time through, need to assign label to the message
5231 : : */
5232 [ # # ]: 0 : if (msec->sid == SECINITSID_UNLABELED) {
5233 : : /*
5234 : : * Compute new sid based on current process and
5235 : : * message queue this message will be stored in
5236 : : */
5237 : 0 : rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
5238 : : NULL, &msec->sid);
5239 [ # # ]: 0 : if (rc)
5240 : : return rc;
5241 : : }
5242 : :
5243 : 0 : ad.type = LSM_AUDIT_DATA_IPC;
5244 : 0 : ad.u.ipc_id = msq->q_perm.key;
5245 : :
5246 : : /* Can this process write to the queue? */
5247 : 0 : rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
5248 : : MSGQ__WRITE, &ad);
5249 [ # # ]: 0 : if (!rc)
5250 : : /* Can this process send the message */
5251 : 0 : rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5252 : : MSG__SEND, &ad);
5253 [ # # ]: 0 : if (!rc)
5254 : : /* Can the message be put in the queue? */
5255 : 0 : rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5256 : : MSGQ__ENQUEUE, &ad);
5257 : :
5258 : 0 : return rc;
5259 : : }
5260 : :
5261 : 0 : static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5262 : : struct task_struct *target,
5263 : : long type, int mode)
5264 : : {
5265 : : struct ipc_security_struct *isec;
5266 : : struct msg_security_struct *msec;
5267 : : struct common_audit_data ad;
5268 : : u32 sid = task_sid(target);
5269 : : int rc;
5270 : :
5271 : 0 : isec = msq->q_perm.security;
5272 : 0 : msec = msg->security;
5273 : :
5274 : 0 : ad.type = LSM_AUDIT_DATA_IPC;
5275 : 0 : ad.u.ipc_id = msq->q_perm.key;
5276 : :
5277 : 0 : rc = avc_has_perm(sid, isec->sid,
5278 : : SECCLASS_MSGQ, MSGQ__READ, &ad);
5279 [ # # ]: 0 : if (!rc)
5280 : 0 : rc = avc_has_perm(sid, msec->sid,
5281 : : SECCLASS_MSG, MSG__RECEIVE, &ad);
5282 : 0 : return rc;
5283 : : }
5284 : :
5285 : : /* Shared Memory security operations */
5286 : 0 : static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5287 : : {
5288 : : struct ipc_security_struct *isec;
5289 : : struct common_audit_data ad;
5290 : : u32 sid = current_sid();
5291 : : int rc;
5292 : :
5293 : 0 : rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5294 [ # # ]: 0 : if (rc)
5295 : : return rc;
5296 : :
5297 : 0 : isec = shp->shm_perm.security;
5298 : :
5299 : 0 : ad.type = LSM_AUDIT_DATA_IPC;
5300 : 0 : ad.u.ipc_id = shp->shm_perm.key;
5301 : :
5302 : 0 : rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
5303 : : SHM__CREATE, &ad);
5304 [ # # ]: 0 : if (rc) {
5305 : : ipc_free_security(&shp->shm_perm);
5306 : 0 : return rc;
5307 : : }
5308 : : return 0;
5309 : : }
5310 : :
5311 : 0 : static void selinux_shm_free_security(struct shmid_kernel *shp)
5312 : : {
5313 : : ipc_free_security(&shp->shm_perm);
5314 : 0 : }
5315 : :
5316 : 0 : static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5317 : : {
5318 : : struct ipc_security_struct *isec;
5319 : : struct common_audit_data ad;
5320 : : u32 sid = current_sid();
5321 : :
5322 : 0 : isec = shp->shm_perm.security;
5323 : :
5324 : 0 : ad.type = LSM_AUDIT_DATA_IPC;
5325 : 0 : ad.u.ipc_id = shp->shm_perm.key;
5326 : :
5327 : 0 : return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
5328 : : SHM__ASSOCIATE, &ad);
5329 : : }
5330 : :
5331 : : /* Note, at this point, shp is locked down */
5332 : 0 : static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5333 : : {
5334 : : int perms;
5335 : : int err;
5336 : :
5337 [ # # # # : 0 : switch (cmd) {
# # ]
5338 : : case IPC_INFO:
5339 : : case SHM_INFO:
5340 : : /* No specific object, just general system-wide information. */
5341 : 0 : return task_has_system(current, SYSTEM__IPC_INFO);
5342 : : case IPC_STAT:
5343 : : case SHM_STAT:
5344 : : perms = SHM__GETATTR | SHM__ASSOCIATE;
5345 : : break;
5346 : : case IPC_SET:
5347 : : perms = SHM__SETATTR;
5348 : 0 : break;
5349 : : case SHM_LOCK:
5350 : : case SHM_UNLOCK:
5351 : : perms = SHM__LOCK;
5352 : 0 : break;
5353 : : case IPC_RMID:
5354 : : perms = SHM__DESTROY;
5355 : 0 : break;
5356 : : default:
5357 : : return 0;
5358 : : }
5359 : :
5360 : 0 : err = ipc_has_perm(&shp->shm_perm, perms);
5361 : 0 : return err;
5362 : : }
5363 : :
5364 : 0 : static int selinux_shm_shmat(struct shmid_kernel *shp,
5365 : : char __user *shmaddr, int shmflg)
5366 : : {
5367 : : u32 perms;
5368 : :
5369 [ # # ]: 0 : if (shmflg & SHM_RDONLY)
5370 : : perms = SHM__READ;
5371 : : else
5372 : : perms = SHM__READ | SHM__WRITE;
5373 : :
5374 : 0 : return ipc_has_perm(&shp->shm_perm, perms);
5375 : : }
5376 : :
5377 : : /* Semaphore security operations */
5378 : 0 : static int selinux_sem_alloc_security(struct sem_array *sma)
5379 : : {
5380 : : struct ipc_security_struct *isec;
5381 : : struct common_audit_data ad;
5382 : : u32 sid = current_sid();
5383 : : int rc;
5384 : :
5385 : 0 : rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5386 [ # # ]: 0 : if (rc)
5387 : : return rc;
5388 : :
5389 : 0 : isec = sma->sem_perm.security;
5390 : :
5391 : 0 : ad.type = LSM_AUDIT_DATA_IPC;
5392 : 0 : ad.u.ipc_id = sma->sem_perm.key;
5393 : :
5394 : 0 : rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
5395 : : SEM__CREATE, &ad);
5396 [ # # ]: 0 : if (rc) {
5397 : : ipc_free_security(&sma->sem_perm);
5398 : 0 : return rc;
5399 : : }
5400 : : return 0;
5401 : : }
5402 : :
5403 : 0 : static void selinux_sem_free_security(struct sem_array *sma)
5404 : : {
5405 : : ipc_free_security(&sma->sem_perm);
5406 : 0 : }
5407 : :
5408 : 0 : static int selinux_sem_associate(struct sem_array *sma, int semflg)
5409 : : {
5410 : : struct ipc_security_struct *isec;
5411 : : struct common_audit_data ad;
5412 : : u32 sid = current_sid();
5413 : :
5414 : 0 : isec = sma->sem_perm.security;
5415 : :
5416 : 0 : ad.type = LSM_AUDIT_DATA_IPC;
5417 : 0 : ad.u.ipc_id = sma->sem_perm.key;
5418 : :
5419 : 0 : return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
5420 : : SEM__ASSOCIATE, &ad);
5421 : : }
5422 : :
5423 : : /* Note, at this point, sma is locked down */
5424 : 0 : static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5425 : : {
5426 : : int err;
5427 : : u32 perms;
5428 : :
5429 [ # # # # : 0 : switch (cmd) {
# # # # ]
5430 : : case IPC_INFO:
5431 : : case SEM_INFO:
5432 : : /* No specific object, just general system-wide information. */
5433 : 0 : return task_has_system(current, SYSTEM__IPC_INFO);
5434 : : case GETPID:
5435 : : case GETNCNT:
5436 : : case GETZCNT:
5437 : : perms = SEM__GETATTR;
5438 : : break;
5439 : : case GETVAL:
5440 : : case GETALL:
5441 : : perms = SEM__READ;
5442 : 0 : break;
5443 : : case SETVAL:
5444 : : case SETALL:
5445 : : perms = SEM__WRITE;
5446 : 0 : break;
5447 : : case IPC_RMID:
5448 : : perms = SEM__DESTROY;
5449 : 0 : break;
5450 : : case IPC_SET:
5451 : : perms = SEM__SETATTR;
5452 : 0 : break;
5453 : : case IPC_STAT:
5454 : : case SEM_STAT:
5455 : : perms = SEM__GETATTR | SEM__ASSOCIATE;
5456 : 0 : break;
5457 : : default:
5458 : : return 0;
5459 : : }
5460 : :
5461 : 0 : err = ipc_has_perm(&sma->sem_perm, perms);
5462 : 0 : return err;
5463 : : }
5464 : :
5465 : 0 : static int selinux_sem_semop(struct sem_array *sma,
5466 : : struct sembuf *sops, unsigned nsops, int alter)
5467 : : {
5468 : : u32 perms;
5469 : :
5470 [ # # ]: 0 : if (alter)
5471 : : perms = SEM__READ | SEM__WRITE;
5472 : : else
5473 : : perms = SEM__READ;
5474 : :
5475 : 0 : return ipc_has_perm(&sma->sem_perm, perms);
5476 : : }
5477 : :
5478 : 0 : static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5479 : : {
5480 : : u32 av = 0;
5481 : :
5482 : : av = 0;
5483 [ # # ]: 0 : if (flag & S_IRUGO)
5484 : : av |= IPC__UNIX_READ;
5485 [ # # ]: 0 : if (flag & S_IWUGO)
5486 : 0 : av |= IPC__UNIX_WRITE;
5487 : :
5488 [ # # ]: 0 : if (av == 0)
5489 : : return 0;
5490 : :
5491 : 0 : return ipc_has_perm(ipcp, av);
5492 : : }
5493 : :
5494 : 0 : static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5495 : : {
5496 : 0 : struct ipc_security_struct *isec = ipcp->security;
5497 : 0 : *secid = isec->sid;
5498 : 0 : }
5499 : :
5500 : 0 : static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
5501 : : {
5502 [ # # ]: 0 : if (inode)
5503 : 0 : inode_doinit_with_dentry(inode, dentry);
5504 : 0 : }
5505 : :
5506 : 0 : static int selinux_getprocattr(struct task_struct *p,
5507 : : char *name, char **value)
5508 : : {
5509 : : const struct task_security_struct *__tsec;
5510 : : u32 sid;
5511 : : int error;
5512 : : unsigned len;
5513 : :
5514 [ # # ]: 0 : if (current != p) {
5515 : 0 : error = current_has_perm(p, PROCESS__GETATTR);
5516 [ # # ]: 0 : if (error)
5517 : : return error;
5518 : : }
5519 : :
5520 : : rcu_read_lock();
5521 : 0 : __tsec = __task_cred(p)->security;
5522 : :
5523 [ # # ]: 0 : if (!strcmp(name, "current"))
5524 : 0 : sid = __tsec->sid;
5525 [ # # ]: 0 : else if (!strcmp(name, "prev"))
5526 : 0 : sid = __tsec->osid;
5527 [ # # ]: 0 : else if (!strcmp(name, "exec"))
5528 : 0 : sid = __tsec->exec_sid;
5529 [ # # ]: 0 : else if (!strcmp(name, "fscreate"))
5530 : 0 : sid = __tsec->create_sid;
5531 [ # # ]: 0 : else if (!strcmp(name, "keycreate"))
5532 : 0 : sid = __tsec->keycreate_sid;
5533 [ # # ]: 0 : else if (!strcmp(name, "sockcreate"))
5534 : 0 : sid = __tsec->sockcreate_sid;
5535 : : else
5536 : : goto invalid;
5537 : : rcu_read_unlock();
5538 : :
5539 [ # # ]: 0 : if (!sid)
5540 : : return 0;
5541 : :
5542 : 0 : error = security_sid_to_context(sid, value, &len);
5543 [ # # ]: 0 : if (error)
5544 : : return error;
5545 : 0 : return len;
5546 : :
5547 : : invalid:
5548 : : rcu_read_unlock();
5549 : 0 : return -EINVAL;
5550 : : }
5551 : :
5552 : 0 : static int selinux_setprocattr(struct task_struct *p,
5553 : : char *name, void *value, size_t size)
5554 : : {
5555 : : struct task_security_struct *tsec;
5556 : : struct task_struct *tracer;
5557 : : struct cred *new;
5558 : 0 : u32 sid = 0, ptsid;
5559 : : int error;
5560 : : char *str = value;
5561 : :
5562 [ # # ]: 0 : if (current != p) {
5563 : : /* SELinux only allows a process to change its own
5564 : : security attributes. */
5565 : : return -EACCES;
5566 : : }
5567 : :
5568 : : /*
5569 : : * Basic control over ability to set these attributes at all.
5570 : : * current == p, but we'll pass them separately in case the
5571 : : * above restriction is ever removed.
5572 : : */
5573 [ # # ]: 0 : if (!strcmp(name, "exec"))
5574 : 0 : error = current_has_perm(p, PROCESS__SETEXEC);
5575 [ # # ]: 0 : else if (!strcmp(name, "fscreate"))
5576 : 0 : error = current_has_perm(p, PROCESS__SETFSCREATE);
5577 [ # # ]: 0 : else if (!strcmp(name, "keycreate"))
5578 : 0 : error = current_has_perm(p, PROCESS__SETKEYCREATE);
5579 [ # # ]: 0 : else if (!strcmp(name, "sockcreate"))
5580 : 0 : error = current_has_perm(p, PROCESS__SETSOCKCREATE);
5581 [ # # ]: 0 : else if (!strcmp(name, "current"))
5582 : 0 : error = current_has_perm(p, PROCESS__SETCURRENT);
5583 : : else
5584 : : error = -EINVAL;
5585 [ # # ]: 0 : if (error)
5586 : : return error;
5587 : :
5588 : : /* Obtain a SID for the context, if one was specified. */
5589 [ # # ][ # # ]: 0 : if (size && str[1] && str[1] != '\n') {
[ # # ]
5590 [ # # ]: 0 : if (str[size-1] == '\n') {
5591 : 0 : str[size-1] = 0;
5592 : : size--;
5593 : : }
5594 : 0 : error = security_context_to_sid(value, size, &sid, GFP_KERNEL);
5595 [ # # ][ # # ]: 0 : if (error == -EINVAL && !strcmp(name, "fscreate")) {
5596 [ # # ]: 0 : if (!capable(CAP_MAC_ADMIN)) {
5597 : : struct audit_buffer *ab;
5598 : : size_t audit_size;
5599 : :
5600 : : /* We strip a nul only if it is at the end, otherwise the
5601 : : * context contains a nul and we should audit that */
5602 [ # # ]: 0 : if (str[size - 1] == '\0')
5603 : : audit_size = size - 1;
5604 : : else
5605 : : audit_size = size;
5606 : 0 : ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5607 : 0 : audit_log_format(ab, "op=fscreate invalid_context=");
5608 : 0 : audit_log_n_untrustedstring(ab, value, audit_size);
5609 : 0 : audit_log_end(ab);
5610 : :
5611 : 0 : return error;
5612 : : }
5613 : 0 : error = security_context_to_sid_force(value, size,
5614 : : &sid);
5615 : : }
5616 [ # # ]: 0 : if (error)
5617 : : return error;
5618 : : }
5619 : :
5620 : 0 : new = prepare_creds();
5621 [ # # ]: 0 : if (!new)
5622 : : return -ENOMEM;
5623 : :
5624 : : /* Permission checking based on the specified context is
5625 : : performed during the actual operation (execve,
5626 : : open/mkdir/...), when we know the full context of the
5627 : : operation. See selinux_bprm_set_creds for the execve
5628 : : checks and may_create for the file creation checks. The
5629 : : operation will then fail if the context is not permitted. */
5630 : 0 : tsec = new->security;
5631 [ # # ]: 0 : if (!strcmp(name, "exec")) {
5632 : 0 : tsec->exec_sid = sid;
5633 [ # # ]: 0 : } else if (!strcmp(name, "fscreate")) {
5634 : 0 : tsec->create_sid = sid;
5635 [ # # ]: 0 : } else if (!strcmp(name, "keycreate")) {
5636 : 0 : error = may_create_key(sid, p);
5637 [ # # ]: 0 : if (error)
5638 : : goto abort_change;
5639 : 0 : tsec->keycreate_sid = sid;
5640 [ # # ]: 0 : } else if (!strcmp(name, "sockcreate")) {
5641 : 0 : tsec->sockcreate_sid = sid;
5642 [ # # ]: 0 : } else if (!strcmp(name, "current")) {
5643 : : error = -EINVAL;
5644 [ # # ]: 0 : if (sid == 0)
5645 : : goto abort_change;
5646 : :
5647 : : /* Only allow single threaded processes to change context */
5648 : : error = -EPERM;
5649 [ # # ]: 0 : if (!current_is_single_threaded()) {
5650 : 0 : error = security_bounded_transition(tsec->sid, sid);
5651 [ # # ]: 0 : if (error)
5652 : : goto abort_change;
5653 : : }
5654 : :
5655 : : /* Check permissions for the transition. */
5656 : 0 : error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
5657 : : PROCESS__DYNTRANSITION, NULL);
5658 [ # # ]: 0 : if (error)
5659 : : goto abort_change;
5660 : :
5661 : : /* Check for ptracing, and update the task SID if ok.
5662 : : Otherwise, leave SID unchanged and fail. */
5663 : : ptsid = 0;
5664 : : rcu_read_lock();
5665 : : tracer = ptrace_parent(p);
5666 [ # # ]: 0 : if (tracer)
5667 : : ptsid = task_sid(tracer);
5668 : : rcu_read_unlock();
5669 : :
5670 [ # # ]: 0 : if (tracer) {
5671 : 0 : error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5672 : : PROCESS__PTRACE, NULL);
5673 [ # # ]: 0 : if (error)
5674 : : goto abort_change;
5675 : : }
5676 : :
5677 : 0 : tsec->sid = sid;
5678 : : } else {
5679 : : error = -EINVAL;
5680 : : goto abort_change;
5681 : : }
5682 : :
5683 : 0 : commit_creds(new);
5684 : 0 : return size;
5685 : :
5686 : : abort_change:
5687 : 0 : abort_creds(new);
5688 : 0 : return error;
5689 : : }
5690 : :
5691 : 0 : static int selinux_ismaclabel(const char *name)
5692 : : {
5693 : 0 : return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
5694 : : }
5695 : :
5696 : 0 : static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5697 : : {
5698 : 0 : return security_sid_to_context(secid, secdata, seclen);
5699 : : }
5700 : :
5701 : 0 : static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
5702 : : {
5703 : 0 : return security_context_to_sid(secdata, seclen, secid, GFP_KERNEL);
5704 : : }
5705 : :
5706 : 0 : static void selinux_release_secctx(char *secdata, u32 seclen)
5707 : : {
5708 : 0 : kfree(secdata);
5709 : 0 : }
5710 : :
5711 : : /*
5712 : : * called with inode->i_mutex locked
5713 : : */
5714 : 0 : static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5715 : : {
5716 : 0 : return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5717 : : }
5718 : :
5719 : : /*
5720 : : * called with inode->i_mutex locked
5721 : : */
5722 : 0 : static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5723 : : {
5724 : 0 : return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5725 : : }
5726 : :
5727 : 0 : static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5728 : : {
5729 : : int len = 0;
5730 : 0 : len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5731 : : ctx, true);
5732 [ # # ]: 0 : if (len < 0)
5733 : : return len;
5734 : 0 : *ctxlen = len;
5735 : 0 : return 0;
5736 : : }
5737 : : #ifdef CONFIG_KEYS
5738 : :
5739 : 0 : static int selinux_key_alloc(struct key *k, const struct cred *cred,
5740 : : unsigned long flags)
5741 : : {
5742 : : const struct task_security_struct *tsec;
5743 : : struct key_security_struct *ksec;
5744 : :
5745 : : ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5746 [ # # ]: 0 : if (!ksec)
5747 : : return -ENOMEM;
5748 : :
5749 : 0 : tsec = cred->security;
5750 [ # # ]: 0 : if (tsec->keycreate_sid)
5751 : 0 : ksec->sid = tsec->keycreate_sid;
5752 : : else
5753 : 0 : ksec->sid = tsec->sid;
5754 : :
5755 : 0 : k->security = ksec;
5756 : 0 : return 0;
5757 : : }
5758 : :
5759 : 0 : static void selinux_key_free(struct key *k)
5760 : : {
5761 : 0 : struct key_security_struct *ksec = k->security;
5762 : :
5763 : 0 : k->security = NULL;
5764 : 0 : kfree(ksec);
5765 : 0 : }
5766 : :
5767 : 0 : static int selinux_key_permission(key_ref_t key_ref,
5768 : 0 : const struct cred *cred,
5769 : : key_perm_t perm)
5770 : : {
5771 : : struct key *key;
5772 : : struct key_security_struct *ksec;
5773 : : u32 sid;
5774 : :
5775 : : /* if no specific permissions are requested, we skip the
5776 : : permission check. No serious, additional covert channels
5777 : : appear to be created. */
5778 [ # # ]: 0 : if (perm == 0)
5779 : : return 0;
5780 : :
5781 : : sid = cred_sid(cred);
5782 : :
5783 : : key = key_ref_to_ptr(key_ref);
5784 : 0 : ksec = key->security;
5785 : :
5786 : 0 : return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
5787 : : }
5788 : :
5789 : 0 : static int selinux_key_getsecurity(struct key *key, char **_buffer)
5790 : : {
5791 : 0 : struct key_security_struct *ksec = key->security;
5792 : 0 : char *context = NULL;
5793 : : unsigned len;
5794 : : int rc;
5795 : :
5796 : 0 : rc = security_sid_to_context(ksec->sid, &context, &len);
5797 [ # # ]: 0 : if (!rc)
5798 : 0 : rc = len;
5799 : 0 : *_buffer = context;
5800 : 0 : return rc;
5801 : : }
5802 : :
5803 : : #endif
5804 : :
5805 : : static struct security_operations selinux_ops = {
5806 : : .name = "selinux",
5807 : :
5808 : : .binder_set_context_mgr = selinux_binder_set_context_mgr,
5809 : : .binder_transaction = selinux_binder_transaction,
5810 : : .binder_transfer_binder = selinux_binder_transfer_binder,
5811 : : .binder_transfer_file = selinux_binder_transfer_file,
5812 : :
5813 : : .ptrace_access_check = selinux_ptrace_access_check,
5814 : : .ptrace_traceme = selinux_ptrace_traceme,
5815 : : .capget = selinux_capget,
5816 : : .capset = selinux_capset,
5817 : : .capable = selinux_capable,
5818 : : .quotactl = selinux_quotactl,
5819 : : .quota_on = selinux_quota_on,
5820 : : .syslog = selinux_syslog,
5821 : : .vm_enough_memory = selinux_vm_enough_memory,
5822 : :
5823 : : .netlink_send = selinux_netlink_send,
5824 : :
5825 : : .bprm_set_creds = selinux_bprm_set_creds,
5826 : : .bprm_committing_creds = selinux_bprm_committing_creds,
5827 : : .bprm_committed_creds = selinux_bprm_committed_creds,
5828 : : .bprm_secureexec = selinux_bprm_secureexec,
5829 : :
5830 : : .sb_alloc_security = selinux_sb_alloc_security,
5831 : : .sb_free_security = selinux_sb_free_security,
5832 : : .sb_copy_data = selinux_sb_copy_data,
5833 : : .sb_remount = selinux_sb_remount,
5834 : : .sb_kern_mount = selinux_sb_kern_mount,
5835 : : .sb_show_options = selinux_sb_show_options,
5836 : : .sb_statfs = selinux_sb_statfs,
5837 : : .sb_mount = selinux_mount,
5838 : : .sb_umount = selinux_umount,
5839 : : .sb_set_mnt_opts = selinux_set_mnt_opts,
5840 : : .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
5841 : : .sb_parse_opts_str = selinux_parse_opts_str,
5842 : :
5843 : : .dentry_init_security = selinux_dentry_init_security,
5844 : :
5845 : : .inode_alloc_security = selinux_inode_alloc_security,
5846 : : .inode_free_security = selinux_inode_free_security,
5847 : : .inode_init_security = selinux_inode_init_security,
5848 : : .inode_create = selinux_inode_create,
5849 : : .inode_link = selinux_inode_link,
5850 : : .inode_unlink = selinux_inode_unlink,
5851 : : .inode_symlink = selinux_inode_symlink,
5852 : : .inode_mkdir = selinux_inode_mkdir,
5853 : : .inode_rmdir = selinux_inode_rmdir,
5854 : : .inode_mknod = selinux_inode_mknod,
5855 : : .inode_rename = selinux_inode_rename,
5856 : : .inode_readlink = selinux_inode_readlink,
5857 : : .inode_follow_link = selinux_inode_follow_link,
5858 : : .inode_permission = selinux_inode_permission,
5859 : : .inode_setattr = selinux_inode_setattr,
5860 : : .inode_getattr = selinux_inode_getattr,
5861 : : .inode_setxattr = selinux_inode_setxattr,
5862 : : .inode_post_setxattr = selinux_inode_post_setxattr,
5863 : : .inode_getxattr = selinux_inode_getxattr,
5864 : : .inode_listxattr = selinux_inode_listxattr,
5865 : : .inode_removexattr = selinux_inode_removexattr,
5866 : : .inode_getsecurity = selinux_inode_getsecurity,
5867 : : .inode_setsecurity = selinux_inode_setsecurity,
5868 : : .inode_listsecurity = selinux_inode_listsecurity,
5869 : : .inode_getsecid = selinux_inode_getsecid,
5870 : :
5871 : : .file_permission = selinux_file_permission,
5872 : : .file_alloc_security = selinux_file_alloc_security,
5873 : : .file_free_security = selinux_file_free_security,
5874 : : .file_ioctl = selinux_file_ioctl,
5875 : : .mmap_file = selinux_mmap_file,
5876 : : .mmap_addr = selinux_mmap_addr,
5877 : : .file_mprotect = selinux_file_mprotect,
5878 : : .file_lock = selinux_file_lock,
5879 : : .file_fcntl = selinux_file_fcntl,
5880 : : .file_set_fowner = selinux_file_set_fowner,
5881 : : .file_send_sigiotask = selinux_file_send_sigiotask,
5882 : : .file_receive = selinux_file_receive,
5883 : :
5884 : : .file_open = selinux_file_open,
5885 : :
5886 : : .task_create = selinux_task_create,
5887 : : .cred_alloc_blank = selinux_cred_alloc_blank,
5888 : : .cred_free = selinux_cred_free,
5889 : : .cred_prepare = selinux_cred_prepare,
5890 : : .cred_transfer = selinux_cred_transfer,
5891 : : .kernel_act_as = selinux_kernel_act_as,
5892 : : .kernel_create_files_as = selinux_kernel_create_files_as,
5893 : : .kernel_module_request = selinux_kernel_module_request,
5894 : : .task_setpgid = selinux_task_setpgid,
5895 : : .task_getpgid = selinux_task_getpgid,
5896 : : .task_getsid = selinux_task_getsid,
5897 : : .task_getsecid = selinux_task_getsecid,
5898 : : .task_setnice = selinux_task_setnice,
5899 : : .task_setioprio = selinux_task_setioprio,
5900 : : .task_getioprio = selinux_task_getioprio,
5901 : : .task_setrlimit = selinux_task_setrlimit,
5902 : : .task_setscheduler = selinux_task_setscheduler,
5903 : : .task_getscheduler = selinux_task_getscheduler,
5904 : : .task_movememory = selinux_task_movememory,
5905 : : .task_kill = selinux_task_kill,
5906 : : .task_wait = selinux_task_wait,
5907 : : .task_to_inode = selinux_task_to_inode,
5908 : :
5909 : : .ipc_permission = selinux_ipc_permission,
5910 : : .ipc_getsecid = selinux_ipc_getsecid,
5911 : :
5912 : : .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5913 : : .msg_msg_free_security = selinux_msg_msg_free_security,
5914 : :
5915 : : .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5916 : : .msg_queue_free_security = selinux_msg_queue_free_security,
5917 : : .msg_queue_associate = selinux_msg_queue_associate,
5918 : : .msg_queue_msgctl = selinux_msg_queue_msgctl,
5919 : : .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5920 : : .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5921 : :
5922 : : .shm_alloc_security = selinux_shm_alloc_security,
5923 : : .shm_free_security = selinux_shm_free_security,
5924 : : .shm_associate = selinux_shm_associate,
5925 : : .shm_shmctl = selinux_shm_shmctl,
5926 : : .shm_shmat = selinux_shm_shmat,
5927 : :
5928 : : .sem_alloc_security = selinux_sem_alloc_security,
5929 : : .sem_free_security = selinux_sem_free_security,
5930 : : .sem_associate = selinux_sem_associate,
5931 : : .sem_semctl = selinux_sem_semctl,
5932 : : .sem_semop = selinux_sem_semop,
5933 : :
5934 : : .d_instantiate = selinux_d_instantiate,
5935 : :
5936 : : .getprocattr = selinux_getprocattr,
5937 : : .setprocattr = selinux_setprocattr,
5938 : :
5939 : : .ismaclabel = selinux_ismaclabel,
5940 : : .secid_to_secctx = selinux_secid_to_secctx,
5941 : : .secctx_to_secid = selinux_secctx_to_secid,
5942 : : .release_secctx = selinux_release_secctx,
5943 : : .inode_notifysecctx = selinux_inode_notifysecctx,
5944 : : .inode_setsecctx = selinux_inode_setsecctx,
5945 : : .inode_getsecctx = selinux_inode_getsecctx,
5946 : :
5947 : : .unix_stream_connect = selinux_socket_unix_stream_connect,
5948 : : .unix_may_send = selinux_socket_unix_may_send,
5949 : :
5950 : : .socket_create = selinux_socket_create,
5951 : : .socket_post_create = selinux_socket_post_create,
5952 : : .socket_bind = selinux_socket_bind,
5953 : : .socket_connect = selinux_socket_connect,
5954 : : .socket_listen = selinux_socket_listen,
5955 : : .socket_accept = selinux_socket_accept,
5956 : : .socket_sendmsg = selinux_socket_sendmsg,
5957 : : .socket_recvmsg = selinux_socket_recvmsg,
5958 : : .socket_getsockname = selinux_socket_getsockname,
5959 : : .socket_getpeername = selinux_socket_getpeername,
5960 : : .socket_getsockopt = selinux_socket_getsockopt,
5961 : : .socket_setsockopt = selinux_socket_setsockopt,
5962 : : .socket_shutdown = selinux_socket_shutdown,
5963 : : .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
5964 : : .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5965 : : .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
5966 : : .sk_alloc_security = selinux_sk_alloc_security,
5967 : : .sk_free_security = selinux_sk_free_security,
5968 : : .sk_clone_security = selinux_sk_clone_security,
5969 : : .sk_getsecid = selinux_sk_getsecid,
5970 : : .sock_graft = selinux_sock_graft,
5971 : : .inet_conn_request = selinux_inet_conn_request,
5972 : : .inet_csk_clone = selinux_inet_csk_clone,
5973 : : .inet_conn_established = selinux_inet_conn_established,
5974 : : .secmark_relabel_packet = selinux_secmark_relabel_packet,
5975 : : .secmark_refcount_inc = selinux_secmark_refcount_inc,
5976 : : .secmark_refcount_dec = selinux_secmark_refcount_dec,
5977 : : .req_classify_flow = selinux_req_classify_flow,
5978 : : .tun_dev_alloc_security = selinux_tun_dev_alloc_security,
5979 : : .tun_dev_free_security = selinux_tun_dev_free_security,
5980 : : .tun_dev_create = selinux_tun_dev_create,
5981 : : .tun_dev_attach_queue = selinux_tun_dev_attach_queue,
5982 : : .tun_dev_attach = selinux_tun_dev_attach,
5983 : : .tun_dev_open = selinux_tun_dev_open,
5984 : : .skb_owned_by = selinux_skb_owned_by,
5985 : :
5986 : : #ifdef CONFIG_SECURITY_NETWORK_XFRM
5987 : : .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5988 : : .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5989 : : .xfrm_policy_free_security = selinux_xfrm_policy_free,
5990 : : .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
5991 : : .xfrm_state_alloc = selinux_xfrm_state_alloc,
5992 : : .xfrm_state_alloc_acquire = selinux_xfrm_state_alloc_acquire,
5993 : : .xfrm_state_free_security = selinux_xfrm_state_free,
5994 : : .xfrm_state_delete_security = selinux_xfrm_state_delete,
5995 : : .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
5996 : : .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
5997 : : .xfrm_decode_session = selinux_xfrm_decode_session,
5998 : : #endif
5999 : :
6000 : : #ifdef CONFIG_KEYS
6001 : : .key_alloc = selinux_key_alloc,
6002 : : .key_free = selinux_key_free,
6003 : : .key_permission = selinux_key_permission,
6004 : : .key_getsecurity = selinux_key_getsecurity,
6005 : : #endif
6006 : :
6007 : : #ifdef CONFIG_AUDIT
6008 : : .audit_rule_init = selinux_audit_rule_init,
6009 : : .audit_rule_known = selinux_audit_rule_known,
6010 : : .audit_rule_match = selinux_audit_rule_match,
6011 : : .audit_rule_free = selinux_audit_rule_free,
6012 : : #endif
6013 : : };
6014 : :
6015 : 0 : static __init int selinux_init(void)
6016 : : {
6017 [ # # ]: 0 : if (!security_module_enable(&selinux_ops)) {
6018 : 0 : selinux_enabled = 0;
6019 : 0 : return 0;
6020 : : }
6021 : :
6022 [ # # ]: 0 : if (!selinux_enabled) {
6023 : 0 : printk(KERN_INFO "SELinux: Disabled at boot.\n");
6024 : 0 : return 0;
6025 : : }
6026 : :
6027 : 0 : printk(KERN_INFO "SELinux: Initializing.\n");
6028 : :
6029 : : /* Set the security state for the initial task. */
6030 : 0 : cred_init_security();
6031 : :
6032 : 0 : default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6033 : :
6034 : 0 : sel_inode_cache = kmem_cache_create("selinux_inode_security",
6035 : : sizeof(struct inode_security_struct),
6036 : : 0, SLAB_PANIC, NULL);
6037 : 0 : avc_init();
6038 : :
6039 [ # # ]: 0 : if (register_security(&selinux_ops))
6040 : 0 : panic("SELinux: Unable to register with kernel.\n");
6041 : :
6042 [ # # ]: 0 : if (selinux_enforcing)
6043 : 0 : printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
6044 : : else
6045 : 0 : printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
6046 : :
6047 : : return 0;
6048 : : }
6049 : :
6050 : 0 : static void delayed_superblock_init(struct super_block *sb, void *unused)
6051 : : {
6052 : 0 : superblock_doinit(sb, NULL);
6053 : 0 : }
6054 : :
6055 : 0 : void selinux_complete_init(void)
6056 : : {
6057 : 0 : printk(KERN_DEBUG "SELinux: Completing initialization.\n");
6058 : :
6059 : : /* Set up any superblocks initialized prior to the policy load. */
6060 : 0 : printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
6061 : 0 : iterate_supers(delayed_superblock_init, NULL);
6062 : 0 : }
6063 : :
6064 : : /* SELinux requires early initialization in order to label
6065 : : all processes and objects when they are created. */
6066 : : security_initcall(selinux_init);
6067 : :
6068 : : #if defined(CONFIG_NETFILTER)
6069 : :
6070 : : static struct nf_hook_ops selinux_ipv4_ops[] = {
6071 : : {
6072 : : .hook = selinux_ipv4_postroute,
6073 : : .owner = THIS_MODULE,
6074 : : .pf = NFPROTO_IPV4,
6075 : : .hooknum = NF_INET_POST_ROUTING,
6076 : : .priority = NF_IP_PRI_SELINUX_LAST,
6077 : : },
6078 : : {
6079 : : .hook = selinux_ipv4_forward,
6080 : : .owner = THIS_MODULE,
6081 : : .pf = NFPROTO_IPV4,
6082 : : .hooknum = NF_INET_FORWARD,
6083 : : .priority = NF_IP_PRI_SELINUX_FIRST,
6084 : : },
6085 : : {
6086 : : .hook = selinux_ipv4_output,
6087 : : .owner = THIS_MODULE,
6088 : : .pf = NFPROTO_IPV4,
6089 : : .hooknum = NF_INET_LOCAL_OUT,
6090 : : .priority = NF_IP_PRI_SELINUX_FIRST,
6091 : : }
6092 : : };
6093 : :
6094 : : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6095 : :
6096 : : static struct nf_hook_ops selinux_ipv6_ops[] = {
6097 : : {
6098 : : .hook = selinux_ipv6_postroute,
6099 : : .owner = THIS_MODULE,
6100 : : .pf = NFPROTO_IPV6,
6101 : : .hooknum = NF_INET_POST_ROUTING,
6102 : : .priority = NF_IP6_PRI_SELINUX_LAST,
6103 : : },
6104 : : {
6105 : : .hook = selinux_ipv6_forward,
6106 : : .owner = THIS_MODULE,
6107 : : .pf = NFPROTO_IPV6,
6108 : : .hooknum = NF_INET_FORWARD,
6109 : : .priority = NF_IP6_PRI_SELINUX_FIRST,
6110 : : }
6111 : : };
6112 : :
6113 : : #endif /* IPV6 */
6114 : :
6115 : 0 : static int __init selinux_nf_ip_init(void)
6116 : : {
6117 : : int err = 0;
6118 : :
6119 [ # # ]: 0 : if (!selinux_enabled)
6120 : : goto out;
6121 : :
6122 : 0 : printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
6123 : :
6124 : 0 : err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
6125 [ # # ]: 0 : if (err)
6126 : 0 : panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
6127 : :
6128 : : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6129 : 0 : err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
6130 [ # # ]: 0 : if (err)
6131 : 0 : panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
6132 : : #endif /* IPV6 */
6133 : :
6134 : : out:
6135 : 0 : return err;
6136 : : }
6137 : :
6138 : : __initcall(selinux_nf_ip_init);
6139 : :
6140 : : #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6141 : : static void selinux_nf_ip_exit(void)
6142 : : {
6143 : : printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
6144 : :
6145 : : nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
6146 : : #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6147 : : nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
6148 : : #endif /* IPV6 */
6149 : : }
6150 : : #endif
6151 : :
6152 : : #else /* CONFIG_NETFILTER */
6153 : :
6154 : : #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6155 : : #define selinux_nf_ip_exit()
6156 : : #endif
6157 : :
6158 : : #endif /* CONFIG_NETFILTER */
6159 : :
6160 : : #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6161 : : static int selinux_disabled;
6162 : :
6163 : : int selinux_disable(void)
6164 : : {
6165 : : if (ss_initialized) {
6166 : : /* Not permitted after initial policy load. */
6167 : : return -EINVAL;
6168 : : }
6169 : :
6170 : : if (selinux_disabled) {
6171 : : /* Only do this once. */
6172 : : return -EINVAL;
6173 : : }
6174 : :
6175 : : printk(KERN_INFO "SELinux: Disabled at runtime.\n");
6176 : :
6177 : : selinux_disabled = 1;
6178 : : selinux_enabled = 0;
6179 : :
6180 : : reset_security_ops();
6181 : :
6182 : : /* Try to destroy the avc node cache */
6183 : : avc_disable();
6184 : :
6185 : : /* Unregister netfilter hooks. */
6186 : : selinux_nf_ip_exit();
6187 : :
6188 : : /* Unregister selinuxfs. */
6189 : : exit_sel_fs();
6190 : :
6191 : : return 0;
6192 : : }
6193 : : #endif
|