]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - security/apparmor/lsm.c
UBUNTU: Ubuntu-raspi2-4.10.0-1000.2
[mirror_ubuntu-zesty-kernel.git] / security / apparmor / lsm.c
1 /*
2 * AppArmor security module
3 *
4 * This file contains AppArmor LSM hooks.
5 *
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
12 * License.
13 */
14
15 #include <linux/lsm_hooks.h>
16 #include <linux/moduleparam.h>
17 #include <linux/mm.h>
18 #include <linux/mman.h>
19 #include <linux/mount.h>
20 #include <linux/namei.h>
21 #include <linux/ptrace.h>
22 #include <linux/ctype.h>
23 #include <linux/sysctl.h>
24 #include <linux/audit.h>
25 #include <linux/user_namespace.h>
26 #include <linux/kmemleak.h>
27 #include <net/sock.h>
28
29 #include "include/af_unix.h"
30 #include "include/apparmor.h"
31 #include "include/apparmorfs.h"
32 #include "include/audit.h"
33 #include "include/capability.h"
34 #include "include/context.h"
35 #include "include/file.h"
36 #include "include/ipc.h"
37 #include "include/net.h"
38 #include "include/path.h"
39 #include "include/policy.h"
40 #include "include/procattr.h"
41 #include "include/mount.h"
42
43 /* Flag indicating whether initialization completed */
44 int apparmor_initialized;
45
46 DEFINE_PER_CPU(struct aa_buffers, aa_buffers);
47
48
49 /*
50 * LSM hook functions
51 */
52
53 /*
54 * free the associated aa_task_ctx and put its labels
55 */
56 static void apparmor_cred_free(struct cred *cred)
57 {
58 aa_free_task_context(cred_ctx(cred));
59 cred_ctx(cred) = NULL;
60 }
61
62 /*
63 * allocate the apparmor part of blank credentials
64 */
65 static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
66 {
67 /* freed by apparmor_cred_free */
68 struct aa_task_ctx *ctx = aa_alloc_task_context(gfp);
69 if (!ctx)
70 return -ENOMEM;
71
72 cred_ctx(cred) = ctx;
73 return 0;
74 }
75
76 /*
77 * prepare new aa_task_ctx for modification by prepare_cred block
78 */
79 static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
80 gfp_t gfp)
81 {
82 /* freed by apparmor_cred_free */
83 struct aa_task_ctx *ctx = aa_alloc_task_context(gfp);
84 if (!ctx)
85 return -ENOMEM;
86
87 aa_dup_task_context(ctx, cred_ctx(old));
88 cred_ctx(new) = ctx;
89 return 0;
90 }
91
92 /*
93 * transfer the apparmor data to a blank set of creds
94 */
95 static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
96 {
97 const struct aa_task_ctx *old_ctx = cred_ctx(old);
98 struct aa_task_ctx *new_ctx = cred_ctx(new);
99
100 aa_dup_task_context(new_ctx, old_ctx);
101 }
102
103 static int apparmor_ptrace_access_check(struct task_struct *child,
104 unsigned int mode)
105 {
106 struct aa_label *tracer, *tracee;
107 int error;
108
109 tracer = aa_begin_current_label(DO_UPDATE);
110 tracee = aa_get_task_label(child);
111 error = aa_may_ptrace(tracer, tracee,
112 mode == PTRACE_MODE_READ ? AA_PTRACE_READ : AA_PTRACE_TRACE);
113 aa_put_label(tracee);
114 aa_end_current_label(tracer);
115
116 return error;
117 }
118
119 static int apparmor_ptrace_traceme(struct task_struct *parent)
120 {
121 struct aa_label *tracer, *tracee;
122 int error;
123
124 tracee = aa_begin_current_label(DO_UPDATE);
125 tracer = aa_get_task_label(parent);
126 error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
127 aa_put_label(tracer);
128 aa_end_current_label(tracee);
129
130 return error;
131 }
132
133 /* Derived from security/commoncap.c:cap_capget */
134 static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective,
135 kernel_cap_t *inheritable, kernel_cap_t *permitted)
136 {
137 struct aa_label *label;
138 const struct cred *cred;
139
140 rcu_read_lock();
141 cred = __task_cred(target);
142 label = aa_get_newest_cred_label(cred);
143
144 /*
145 * cap_capget is stacked ahead of this and will
146 * initialize effective and permitted.
147 */
148 if (!unconfined(label)) {
149 struct aa_profile *profile;
150 struct label_it i;
151 label_for_each_confined(i, label, profile) {
152 if (COMPLAIN_MODE(profile))
153 continue;
154 *effective = cap_intersect(*effective,
155 profile->caps.allow);
156 *permitted = cap_intersect(*permitted,
157 profile->caps.allow);
158 }
159 }
160 rcu_read_unlock();
161 aa_put_label(label);
162
163 return 0;
164 }
165
166 static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
167 int cap, int audit)
168 {
169 struct aa_label *label;
170 int error = 0;
171
172 label = aa_get_newest_cred_label(cred);
173 if (!unconfined(label))
174 error = aa_capable(label, cap, audit);
175 aa_put_label(label);
176
177 return error;
178 }
179
180 /**
181 * common_perm - basic common permission check wrapper fn for paths
182 * @op: operation being checked
183 * @path: path to check permission of (NOT NULL)
184 * @mask: requested permissions mask
185 * @cond: conditional info for the permission request (NOT NULL)
186 *
187 * Returns: %0 else error code if error or permission denied
188 */
189 static int common_perm(const char *op, const struct path *path, u32 mask,
190 struct path_cond *cond)
191 {
192 struct aa_label *label;
193 int error = 0;
194
195 label = aa_begin_current_label(NO_UPDATE);
196 if (!unconfined(label))
197 error = aa_path_perm(op, label, path, 0, mask, cond);
198 aa_end_current_label(label);
199
200 return error;
201 }
202
203 static int common_perm_cond(const char *op, const struct path *path, u32 mask)
204 {
205 struct path_cond cond = { d_backing_inode(path->dentry)->i_uid,
206 d_backing_inode(path->dentry)->i_mode
207 };
208
209 if (!path_mediated_fs(path->dentry))
210 return 0;
211
212 return common_perm(op, path, mask, &cond);
213 }
214
215 static void apparmor_inode_free_security(struct inode *inode)
216 {
217 struct aa_label *ctx = inode_ctx(inode);
218
219 if (ctx) {
220 inode_ctx(inode) = NULL;
221 aa_put_label(ctx);
222 }
223 }
224
225 /**
226 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
227 * @op: operation being checked
228 * @dir: directory of the dentry (NOT NULL)
229 * @dentry: dentry to check (NOT NULL)
230 * @mask: requested permissions mask
231 * @cond: conditional info for the permission request (NOT NULL)
232 *
233 * Returns: %0 else error code if error or permission denied
234 */
235 static int common_perm_dir_dentry(const char *op, const struct path *dir,
236 struct dentry *dentry, u32 mask,
237 struct path_cond *cond)
238 {
239 struct path path = { dir->mnt, dentry };
240
241 return common_perm(op, &path, mask, cond);
242 }
243
244 /**
245 * common_perm_rm - common permission wrapper for operations doing rm
246 * @op: operation being checked
247 * @dir: directory that the dentry is in (NOT NULL)
248 * @dentry: dentry being rm'd (NOT NULL)
249 * @mask: requested permission mask
250 *
251 * Returns: %0 else error code if error or permission denied
252 */
253 static int common_perm_rm(const char *op, const struct path *dir,
254 struct dentry *dentry, u32 mask)
255 {
256 struct inode *inode = d_backing_inode(dentry);
257 struct path_cond cond = { };
258
259 if (!inode || !path_mediated_fs(dentry))
260 return 0;
261
262 cond.uid = inode->i_uid;
263 cond.mode = inode->i_mode;
264
265 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
266 }
267
268 /**
269 * common_perm_create - common permission wrapper for operations doing create
270 * @op: operation being checked
271 * @dir: directory that dentry will be created in (NOT NULL)
272 * @dentry: dentry to create (NOT NULL)
273 * @mask: request permission mask
274 * @mode: created file mode
275 *
276 * Returns: %0 else error code if error or permission denied
277 */
278 static int common_perm_create(const char *op, const struct path *dir,
279 struct dentry *dentry, u32 mask, umode_t mode)
280 {
281 struct path_cond cond = { current_fsuid(), mode };
282
283 if (!path_mediated_fs(dir->dentry))
284 return 0;
285
286 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
287 }
288
289 static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
290 {
291 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
292 }
293
294 static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
295 umode_t mode)
296 {
297 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
298 S_IFDIR);
299 }
300
301 static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
302 {
303 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
304 }
305
306 static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
307 umode_t mode, unsigned int dev)
308 {
309 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
310 }
311
312 static int apparmor_path_truncate(const struct path *path)
313 {
314 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
315 }
316
317 static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
318 const char *old_name)
319 {
320 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
321 S_IFLNK);
322 }
323
324 static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
325 struct dentry *new_dentry)
326 {
327 struct aa_label *label;
328 int error = 0;
329
330 if (!path_mediated_fs(old_dentry))
331 return 0;
332
333 label = aa_begin_current_label(DO_UPDATE);
334 if (!unconfined(label))
335 error = aa_path_link(label, old_dentry, new_dir, new_dentry);
336 aa_end_current_label(label);
337
338 return error;
339 }
340
341 static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
342 const struct path *new_dir, struct dentry *new_dentry)
343 {
344 struct aa_label *label;
345 int error = 0;
346
347 if (!path_mediated_fs(old_dentry))
348 return 0;
349
350 label = aa_begin_current_label(DO_UPDATE);
351 if (!unconfined(label)) {
352 struct path old_path = { old_dir->mnt, old_dentry };
353 struct path new_path = { new_dir->mnt, new_dentry };
354 struct path_cond cond = { d_backing_inode(old_dentry)->i_uid,
355 d_backing_inode(old_dentry)->i_mode
356 };
357
358 error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
359 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
360 AA_MAY_SETATTR | AA_MAY_DELETE,
361 &cond);
362 if (!error)
363 error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
364 0, MAY_WRITE | AA_MAY_SETATTR |
365 AA_MAY_CREATE, &cond);
366
367 }
368 aa_end_current_label(label);
369
370 return error;
371 }
372
373 static int apparmor_path_chmod(const struct path *path, umode_t mode)
374 {
375 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
376 }
377
378 static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
379 {
380 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
381 }
382
383 static int apparmor_inode_getattr(const struct path *path)
384 {
385 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
386 }
387
388 static int apparmor_file_open(struct file *file, const struct cred *cred)
389 {
390 struct aa_file_ctx *fctx = file_ctx(file);
391 struct aa_label *label;
392 int error = 0;
393
394 if (!path_mediated_fs(file->f_path.dentry))
395 return 0;
396
397 /* If in exec, permission is handled by bprm hooks.
398 * Cache permissions granted by the previous exec check, with
399 * implicit read and executable mmap which are required to
400 * actually execute the image.
401 */
402 if (current->in_execve) {
403 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
404 return 0;
405 }
406
407 label = aa_get_newest_cred_label(cred);
408 if (!unconfined(label)) {
409 struct inode *inode = file_inode(file);
410 struct path_cond cond = { inode->i_uid, inode->i_mode };
411
412 error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
413 aa_map_file_to_perms(file), &cond);
414 /* todo cache full allowed permissions set and state */
415 fctx->allow = aa_map_file_to_perms(file);
416 }
417 aa_put_label(label);
418
419 return error;
420 }
421
422 static int apparmor_file_alloc_security(struct file *file)
423 {
424 int error = 0;
425
426 /* freed by apparmor_file_free_security */
427 struct aa_label *label = aa_begin_current_label(DO_UPDATE);
428 file->f_security = aa_alloc_file_ctx(label, GFP_KERNEL);
429 if (!file_ctx(file))
430 error = -ENOMEM;
431 aa_end_current_label(label);
432
433 return error;
434 }
435
436 static void apparmor_file_free_security(struct file *file)
437 {
438 aa_free_file_ctx(file_ctx(file));
439 }
440
441 static int common_file_perm(const char *op, struct file *file, u32 mask)
442 {
443 struct aa_label *label;
444 int error = 0;
445
446 if (file->f_path.dentry == aa_null.dentry)
447 return -EACCES;
448
449 label = aa_begin_current_label(NO_UPDATE);
450 error = aa_file_perm(op, label, file, mask);
451 aa_end_current_label(label);
452
453 return error;
454 }
455
456 static int apparmor_file_receive(struct file *file)
457 {
458 return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file));
459 }
460
461 static int apparmor_file_permission(struct file *file, int mask)
462 {
463 return common_file_perm(OP_FPERM, file, mask);
464 }
465
466 static int apparmor_file_lock(struct file *file, unsigned int cmd)
467 {
468 u32 mask = AA_MAY_LOCK;
469
470 if (cmd == F_WRLCK)
471 mask |= MAY_WRITE;
472
473 return common_file_perm(OP_FLOCK, file, mask);
474 }
475
476 static int common_mmap(const char *op, struct file *file, unsigned long prot,
477 unsigned long flags)
478 {
479 int mask = 0;
480
481 if (!file || !file_ctx(file))
482 return 0;
483
484 if (prot & PROT_READ)
485 mask |= MAY_READ;
486 /*
487 * Private mappings don't require write perms since they don't
488 * write back to the files
489 */
490 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
491 mask |= MAY_WRITE;
492 if (prot & PROT_EXEC)
493 mask |= AA_EXEC_MMAP;
494
495 return common_file_perm(op, file, mask);
496 }
497
498 static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
499 unsigned long prot, unsigned long flags)
500 {
501 return common_mmap(OP_FMMAP, file, prot, flags);
502 }
503
504 static int apparmor_file_mprotect(struct vm_area_struct *vma,
505 unsigned long reqprot, unsigned long prot)
506 {
507 return common_mmap(OP_FMPROT, vma->vm_file, prot,
508 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
509 }
510
511 static int apparmor_sb_mount(const char *dev_name, const struct path *path,
512 const char *type, unsigned long flags, void *data)
513 {
514 struct aa_label *label;
515 int error = 0;
516
517 /* Discard magic */
518 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
519 flags &= ~MS_MGC_MSK;
520
521 flags &= ~AA_MS_IGNORE_MASK;
522
523 label = aa_begin_current_label(NO_UPDATE);
524 if (!unconfined(label)) {
525 if (flags & MS_REMOUNT)
526 error = aa_remount(label, path, flags, data);
527 else if (flags & MS_BIND)
528 error = aa_bind_mount(label, path, dev_name, flags);
529 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
530 MS_UNBINDABLE))
531 error = aa_mount_change_type(label, path, flags);
532 else if (flags & MS_MOVE)
533 error = aa_move_mount(label, path, dev_name);
534 else
535 error = aa_new_mount(label, dev_name, path, type,
536 flags, data);
537 }
538 aa_end_current_label(label);
539
540 return error;
541 }
542
543 static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
544 {
545 struct aa_label *label;
546 int error = 0;
547
548 label = aa_begin_current_label(NO_UPDATE);
549 if (!unconfined(label))
550 error = aa_umount(label, mnt, flags);
551 aa_end_current_label(label);
552
553 return error;
554 }
555
556 static int apparmor_sb_pivotroot(const struct path *old_path,
557 const struct path *new_path)
558 {
559 struct aa_label *label;
560 int error = 0;
561
562 label = aa_get_current_label();
563 if (!unconfined(label))
564 error = aa_pivotroot(label, old_path, new_path);
565 aa_put_label(label);
566
567 return error;
568 }
569
570 static int apparmor_getprocattr(struct task_struct *task, char *name,
571 char **value)
572 {
573 int error = -ENOENT;
574 /* released below */
575 const struct cred *cred = get_task_cred(task);
576 struct aa_task_ctx *ctx = cred_ctx(cred);
577 struct aa_label *label = NULL;
578
579 if (strcmp(name, "current") == 0)
580 label = aa_get_newest_label(ctx->label);
581 else if (strcmp(name, "prev") == 0 && ctx->previous)
582 label = aa_get_newest_label(ctx->previous);
583 else if (strcmp(name, "exec") == 0 && ctx->onexec)
584 label = aa_get_newest_label(ctx->onexec);
585 else
586 error = -EINVAL;
587
588 if (label)
589 error = aa_getprocattr(label, value);
590
591 aa_put_label(label);
592 put_cred(cred);
593
594 return error;
595 }
596
597 static int apparmor_setprocattr(struct task_struct *task, char *name,
598 void *value, size_t size)
599 {
600 char *command, *largs = NULL, *args = value;
601 size_t arg_size;
602 int error;
603 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR);
604
605 if (size == 0)
606 return -EINVAL;
607 /* task can only write its own attributes */
608 if (current != task)
609 return -EACCES;
610
611 /* AppArmor requires that the buffer must be null terminated atm */
612 if (args[size - 1] != '\0') {
613 /* null terminate */
614 largs = args = kmalloc(size + 1, GFP_KERNEL);
615 if (!args)
616 return -ENOMEM;
617 memcpy(args, value, size);
618 args[size] = '\0';
619 }
620
621 error = -EINVAL;
622 args = strim(args);
623 command = strsep(&args, " ");
624 if (!args)
625 goto out;
626 args = skip_spaces(args);
627 if (!*args)
628 goto out;
629
630 arg_size = size - (args - (largs ? largs : (char *) value));
631 if (strcmp(name, "current") == 0) {
632 if (strcmp(command, "changehat") == 0) {
633 error = aa_setprocattr_changehat(args, arg_size,
634 !AA_DO_TEST);
635 } else if (strcmp(command, "permhat") == 0) {
636 error = aa_setprocattr_changehat(args, arg_size,
637 AA_DO_TEST);
638 } else if (strcmp(command, "changeprofile") == 0) {
639 error = aa_change_profile(args, !AA_ONEXEC,
640 !AA_DO_TEST, false);
641 } else if (strcmp(command, "permprofile") == 0) {
642 error = aa_change_profile(args, !AA_ONEXEC, AA_DO_TEST,
643 false);
644 } else if (strcmp(command, "stack") == 0) {
645 error = aa_change_profile(args, !AA_ONEXEC, !AA_DO_TEST,
646 true);
647 } else
648 goto fail;
649 } else if (strcmp(name, "exec") == 0) {
650 if (strcmp(command, "exec") == 0)
651 error = aa_change_profile(args, AA_ONEXEC, !AA_DO_TEST,
652 false);
653 else if (strcmp(command, "stack") == 0)
654 error = aa_change_profile(args, AA_ONEXEC, !AA_DO_TEST,
655 true);
656 else
657 goto fail;
658 } else
659 /* only support the "current" and "exec" process attributes */
660 goto fail;
661
662 if (!error)
663 error = size;
664 out:
665 kfree(largs);
666 return error;
667
668 fail:
669 aad(&sa)->label = aa_begin_current_label(DO_UPDATE);
670 aad(&sa)->info = name;
671 aad(&sa)->error = error = -EINVAL;
672 aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
673 aa_end_current_label(aad(&sa)->label);
674 goto out;
675 }
676
677 /**
678 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
679 * @bprm: binprm for the exec (NOT NULL)
680 */
681 void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
682 {
683 struct aa_label *label = aa_current_raw_label();
684 struct aa_task_ctx *new_ctx = cred_ctx(bprm->cred);
685
686 /* bail out if unconfined or not changing profile */
687 if ((new_ctx->label->proxy == label->proxy) ||
688 (unconfined(new_ctx->label)))
689 return;
690
691 aa_inherit_files(bprm->cred, current->files);
692
693 current->pdeath_signal = 0;
694
695 /* reset soft limits and set hard limits for the new label */
696 __aa_transition_rlimits(label, new_ctx->label);
697 }
698
699 /**
700 * apparmor_bprm_commited_cred - do cleanup after new creds committed
701 * @bprm: binprm for the exec (NOT NULL)
702 */
703 void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
704 {
705 /* TODO: cleanup signals - ipc mediation */
706 return;
707 }
708
709 static int apparmor_task_setrlimit(struct task_struct *task,
710 unsigned int resource, struct rlimit *new_rlim)
711 {
712 struct aa_label *label = aa_begin_current_label(NO_UPDATE);
713 int error = 0;
714
715 if (!unconfined(label))
716 error = aa_task_setrlimit(label, task, resource, new_rlim);
717 aa_end_current_label(label);
718
719 return error;
720 }
721
722 /**
723 * apparmor_sk_alloc_security - allocate and attach the sk_security field
724 */
725 static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
726 {
727 struct aa_sk_ctx *ctx;
728
729 ctx = kzalloc(sizeof(*ctx), flags);
730 if (!ctx)
731 return -ENOMEM;
732
733 SK_CTX(sk) = ctx;
734 //??? set local too current???
735
736 return 0;
737 }
738
739 /**
740 * apparmor_sk_free_security - free the sk_security field
741 */
742 static void apparmor_sk_free_security(struct sock *sk)
743 {
744 struct aa_sk_ctx *ctx = SK_CTX(sk);
745
746 SK_CTX(sk) = NULL;
747 aa_put_label(ctx->label);
748 aa_put_label(ctx->peer);
749 path_put(&ctx->path);
750 kfree(ctx);
751 }
752
753 /**
754 * apparmor_clone_security - clone the sk_security field
755 */
756 static void apparmor_sk_clone_security(const struct sock *sk,
757 struct sock *newsk)
758 {
759 struct aa_sk_ctx *ctx = SK_CTX(sk);
760 struct aa_sk_ctx *new = SK_CTX(newsk);
761
762 new->label = aa_get_label(ctx->label);
763 new->peer = aa_get_label(ctx->peer);
764 new->path = ctx->path;
765 path_get(&new->path);
766 }
767
768 static struct path *UNIX_FS_CONN_PATH(struct sock *sk, struct sock *newsk)
769 {
770 if (sk->sk_family == PF_UNIX && UNIX_FS(sk))
771 return &unix_sk(sk)->path;
772 else if (newsk->sk_family == PF_UNIX && UNIX_FS(newsk))
773 return &unix_sk(newsk)->path;
774 return NULL;
775 }
776
777 /**
778 * apparmor_unix_stream_connect - check perms before making unix domain conn
779 *
780 * peer is locked when this hook is called
781 */
782 static int apparmor_unix_stream_connect(struct sock *sk, struct sock *peer_sk,
783 struct sock *newsk)
784 {
785 struct aa_sk_ctx *sk_ctx = SK_CTX(sk);
786 struct aa_sk_ctx *peer_ctx = SK_CTX(peer_sk);
787 struct aa_sk_ctx *new_ctx = SK_CTX(newsk);
788 struct aa_label *label;
789 struct path *path;
790 int error;
791
792 label = aa_begin_current_label(NO_UPDATE);
793 error = aa_unix_peer_perm(label, OP_CONNECT,
794 (AA_MAY_CONNECT | AA_MAY_SEND | AA_MAY_RECEIVE),
795 sk, peer_sk, NULL);
796 if (!UNIX_FS(peer_sk)) {
797 last_error(error,
798 aa_unix_peer_perm(peer_ctx->label, OP_CONNECT,
799 (AA_MAY_ACCEPT | AA_MAY_SEND | AA_MAY_RECEIVE),
800 peer_sk, sk, label));
801 }
802 aa_end_current_label(label);
803
804 if (error)
805 return error;
806
807 /* label newsk if it wasn't labeled in post_create. Normally this
808 * would be done in sock_graft, but because we are directly looking
809 * at the peer_sk to obtain peer_labeling for unix socks this
810 * does not work
811 */
812 if (!new_ctx->label)
813 new_ctx->label = aa_get_label(peer_ctx->label);
814
815 /* Cross reference the peer labels for SO_PEERSEC */
816 if (new_ctx->peer)
817 aa_put_label(new_ctx->peer);
818
819 if (sk_ctx->peer)
820 aa_put_label(sk_ctx->peer);
821
822 new_ctx->peer = aa_get_label(sk_ctx->label);
823 sk_ctx->peer = aa_get_label(peer_ctx->label);
824
825 path = UNIX_FS_CONN_PATH(sk, peer_sk);
826 if (path) {
827 new_ctx->path = *path;
828 sk_ctx->path = *path;
829 path_get(path);
830 path_get(path);
831 }
832 return 0;
833 }
834
835 /**
836 * apparmor_unix_may_send - check perms before conn or sending unix dgrams
837 *
838 * other is locked when this hook is called
839 *
840 * dgram connect calls may_send, peer setup but path not copied?????
841 */
842 static int apparmor_unix_may_send(struct socket *sock, struct socket *peer)
843 {
844 struct aa_sk_ctx *peer_ctx = SK_CTX(peer->sk);
845 struct aa_label *label = aa_begin_current_label(NO_UPDATE);
846 int error;
847
848 error = xcheck(aa_unix_peer_perm(label, OP_SENDMSG, AA_MAY_SEND,
849 sock->sk, peer->sk, NULL),
850 aa_unix_peer_perm(peer_ctx->label, OP_SENDMSG, AA_MAY_RECEIVE,
851 peer->sk, sock->sk, label));
852 aa_end_current_label(label);
853
854 return error;
855 }
856
857 /**
858 * apparmor_socket_create - check perms before creating a new socket
859 */
860 static int apparmor_socket_create(int family, int type, int protocol, int kern)
861 {
862 struct aa_label *label;
863 int error = 0;
864
865 label = aa_begin_current_label(DO_UPDATE);
866 if (!(kern || unconfined(label)))
867 error = aa_sock_create_perm(label, family, type, protocol);
868 aa_end_current_label(label);
869
870 return error;
871 }
872
873 /**
874 * apparmor_socket_post_create - setup the per-socket security struct
875 *
876 * Note:
877 * - kernel sockets currently labeled unconfined but we may want to
878 * move to a special kernel label
879 * - socket may not have sk here if created with sock_create_lite or
880 * sock_alloc. These should be accept cases which will be handled in
881 * sock_graft.
882 */
883 static int apparmor_socket_post_create(struct socket *sock, int family,
884 int type, int protocol, int kern)
885 {
886 struct aa_label *label;
887
888 if (kern) {
889 struct aa_ns *ns = aa_get_current_ns();
890 label = aa_get_label(ns_unconfined(ns));
891 aa_put_ns(ns);
892 } else
893 label = aa_get_current_label();
894
895 if (sock->sk) {
896 struct aa_sk_ctx *ctx = SK_CTX(sock->sk);
897 aa_put_label(ctx->label);
898 ctx->label = aa_get_label(label);
899 }
900 aa_put_label(label);
901
902 return 0;
903 }
904
905 /**
906 * apparmor_socket_bind - check perms before bind addr to socket
907 */
908 static int apparmor_socket_bind(struct socket *sock,
909 struct sockaddr *address, int addrlen)
910 {
911 return aa_sock_bind_perm(sock, address, addrlen);
912 }
913
914 /**
915 * apparmor_socket_connect - check perms before connecting @sock to @address
916 */
917 static int apparmor_socket_connect(struct socket *sock,
918 struct sockaddr *address, int addrlen)
919 {
920 return aa_sock_connect_perm(sock, address, addrlen);
921 }
922
923 /**
924 * apparmor_socket_list - check perms before allowing listen
925 */
926 static int apparmor_socket_listen(struct socket *sock, int backlog)
927 {
928 return aa_sock_listen_perm(sock, backlog);
929 }
930
931 /**
932 * apparmor_socket_accept - check perms before accepting a new connection.
933 *
934 * Note: while @newsock is created and has some information, the accept
935 * has not been done.
936 */
937 static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
938 {
939 return aa_sock_accept_perm(sock, newsock);
940 }
941
942 /**
943 * apparmor_socket_sendmsg - check perms before sending msg to another socket
944 */
945 static int apparmor_socket_sendmsg(struct socket *sock,
946 struct msghdr *msg, int size)
947 {
948 int error = aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
949 if (!error) {
950 /* TODO: setup delegation on scm rights
951 see smack for AF_INET, AF_INET6 */
952 ;
953 }
954
955 return error;
956 }
957
958 /**
959 * apparmor_socket_recvmsg - check perms before receiving a message
960 */
961 static int apparmor_socket_recvmsg(struct socket *sock,
962 struct msghdr *msg, int size, int flags)
963 {
964 return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
965 }
966
967 /**
968 * apparmor_socket_getsockname - check perms before getting the local address
969 */
970 static int apparmor_socket_getsockname(struct socket *sock)
971 {
972 return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
973 }
974
975 /**
976 * apparmor_socket_getpeername - check perms before getting remote address
977 */
978 static int apparmor_socket_getpeername(struct socket *sock)
979 {
980 return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
981 }
982
983 /**
984 * apparmor_getsockopt - check perms before getting socket options
985 */
986 static int apparmor_socket_getsockopt(struct socket *sock, int level,
987 int optname)
988 {
989 return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
990 level, optname);
991 }
992
993 /**
994 * apparmor_setsockopt - check perms before setting socket options
995 */
996 static int apparmor_socket_setsockopt(struct socket *sock, int level,
997 int optname)
998 {
999 return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
1000 level, optname);
1001 }
1002
1003 /**
1004 * apparmor_socket_shutdown - check perms before shutting down @sock conn
1005 */
1006 static int apparmor_socket_shutdown(struct socket *sock, int how)
1007 {
1008 return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
1009 }
1010
1011 /**
1012 * apparmor_socket_sock_recv_skb - check perms before associating skb to sk
1013 *
1014 * Note: can not sleep maybe called with locks held
1015
1016 dont want protocol specific in __skb_recv_datagram()
1017 to deny an incoming connection socket_sock_rcv_skb()
1018
1019 */
1020 static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
1021 {
1022 /* TODO: */
1023 return 0;
1024 }
1025
1026
1027 static struct aa_label *sk_peer_label(struct sock *sk)
1028 {
1029 struct sock *peer_sk;
1030 struct aa_sk_ctx *ctx = SK_CTX(sk);
1031
1032 if (ctx->peer)
1033 return ctx->peer;
1034
1035 if (sk->sk_family != PF_UNIX)
1036 return ERR_PTR(-ENOPROTOOPT);
1037
1038 /* check for sockpair peering which does not go through
1039 * security_unix_stream_connect
1040 */
1041 peer_sk = unix_peer(sk);
1042 if (peer_sk) {
1043 ctx = SK_CTX(peer_sk);
1044 if (ctx->label)
1045 return ctx->label;
1046 }
1047
1048 return ERR_PTR(-ENOPROTOOPT);
1049 }
1050
1051 /**
1052 * apparmor_socket_getpeersec_stream - get security context of peer
1053 *
1054 * Note: for tcp only valid if using ipsec or cipso on lan
1055 */
1056 static int apparmor_socket_getpeersec_stream(struct socket *sock,
1057 char __user *optval,
1058 int __user *optlen, unsigned len)
1059 {
1060 char *name;
1061 int slen, error = 0;
1062 struct aa_label *label = aa_begin_current_label(DO_UPDATE);
1063 struct aa_label *peer = sk_peer_label(sock->sk);
1064
1065 if (IS_ERR(peer))
1066 return PTR_ERR(peer);
1067
1068 slen = aa_label_asxprint(&name, labels_ns(label), peer,
1069 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
1070 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
1071 /* don't include terminating \0 in slen, it breaks some apps */
1072 if (slen < 0) {
1073 error = -ENOMEM;
1074 } else {
1075 if (slen > len) {
1076 error = -ERANGE;
1077 } else if (copy_to_user(optval, name, slen)) {
1078 error = -EFAULT;
1079 goto out;
1080 }
1081 if (put_user(slen, optlen))
1082 error = -EFAULT;
1083 out:
1084 kfree(name);
1085
1086 }
1087 aa_end_current_label(label);
1088
1089 return error;
1090 }
1091
1092 /**
1093 * apparmor_socket_getpeersec_dgram - get security label of packet
1094 * @sock: the peer socket
1095 * @skb: packet data
1096 * @secid: pointer to where to put the secid of the packet
1097 *
1098 * Sets the netlabel socket state on sk from parent
1099 */
1100 static int apparmor_socket_getpeersec_dgram(struct socket *sock,
1101 struct sk_buff *skb, u32 *secid)
1102
1103 {
1104 /* TODO: requires secid support, and netlabel */
1105 return -ENOPROTOOPT;
1106 }
1107
1108 /**
1109 * apparmor_sock_graft - Initialize newly created socket
1110 * @sk: child sock
1111 * @parent: parent socket
1112 *
1113 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
1114 * just set sk security information off of current creating process label
1115 * Labeling of sk for accept case - probably should be sock based
1116 * instead of task, because of the case where an implicitly labeled
1117 * socket is shared by different tasks.
1118 */
1119 static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
1120 {
1121 struct aa_sk_ctx *ctx = SK_CTX(sk);
1122 if (!ctx->label)
1123 ctx->label = aa_get_current_label();
1124 }
1125
1126 static int apparmor_task_kill(struct task_struct *target, struct siginfo *info,
1127 int sig, u32 secid)
1128 {
1129 struct aa_label *cl, *tl;
1130 int error;
1131
1132 if (secid)
1133 /* TODO: after secid to label mapping is done.
1134 * Dealing with USB IO specific behavior
1135 */
1136 return 0;
1137 cl = aa_begin_current_label(NO_UPDATE);
1138 tl = aa_get_task_label(target);
1139 error = aa_may_signal(cl, tl, sig);
1140 aa_put_label(tl);
1141 aa_end_current_label(cl);
1142
1143 return error;
1144 }
1145
1146 #ifndef LSM_HOOKS_NAME
1147 #define LSM_HOOKS_NAME(X) //.name = (X),
1148 #endif
1149 static struct security_hook_list apparmor_hooks[] = {
1150 LSM_HOOKS_NAME("apparmor")
1151
1152 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
1153 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
1154 LSM_HOOK_INIT(capget, apparmor_capget),
1155 LSM_HOOK_INIT(capable, apparmor_capable),
1156
1157 LSM_HOOK_INIT(inode_free_security, apparmor_inode_free_security),
1158
1159 LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
1160 LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
1161 LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
1162
1163 LSM_HOOK_INIT(path_link, apparmor_path_link),
1164 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
1165 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
1166 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
1167 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
1168 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
1169 LSM_HOOK_INIT(path_rename, apparmor_path_rename),
1170 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
1171 LSM_HOOK_INIT(path_chown, apparmor_path_chown),
1172 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
1173 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
1174
1175 LSM_HOOK_INIT(file_open, apparmor_file_open),
1176 LSM_HOOK_INIT(file_receive, apparmor_file_receive),
1177 LSM_HOOK_INIT(file_permission, apparmor_file_permission),
1178 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
1179 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
1180 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
1181 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
1182 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
1183 LSM_HOOK_INIT(file_lock, apparmor_file_lock),
1184
1185 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
1186 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
1187
1188 LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
1189 LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
1190 LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
1191
1192 LSM_HOOK_INIT(unix_stream_connect, apparmor_unix_stream_connect),
1193 LSM_HOOK_INIT(unix_may_send, apparmor_unix_may_send),
1194
1195 LSM_HOOK_INIT(socket_create, apparmor_socket_create),
1196 LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
1197 LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
1198 LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
1199 LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
1200 LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
1201 LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
1202 LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
1203 LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
1204 LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
1205 LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
1206 LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
1207 LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
1208 LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
1209 LSM_HOOK_INIT(socket_getpeersec_stream, apparmor_socket_getpeersec_stream),
1210 LSM_HOOK_INIT(socket_getpeersec_dgram, apparmor_socket_getpeersec_dgram),
1211 LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
1212
1213 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
1214 LSM_HOOK_INIT(cred_free, apparmor_cred_free),
1215 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
1216 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
1217
1218 LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
1219 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
1220 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
1221 LSM_HOOK_INIT(bprm_secureexec, apparmor_bprm_secureexec),
1222
1223 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
1224 LSM_HOOK_INIT(task_kill, apparmor_task_kill),
1225 };
1226
1227 /*
1228 * AppArmor sysfs module parameters
1229 */
1230
1231 static int param_set_aabool(const char *val, const struct kernel_param *kp);
1232 static int param_get_aabool(char *buffer, const struct kernel_param *kp);
1233 #define param_check_aabool param_check_bool
1234 static const struct kernel_param_ops param_ops_aabool = {
1235 .flags = KERNEL_PARAM_OPS_FL_NOARG,
1236 .set = param_set_aabool,
1237 .get = param_get_aabool
1238 };
1239
1240 static int param_set_aauint(const char *val, const struct kernel_param *kp);
1241 static int param_get_aauint(char *buffer, const struct kernel_param *kp);
1242 #define param_check_aauint param_check_uint
1243 static const struct kernel_param_ops param_ops_aauint = {
1244 .set = param_set_aauint,
1245 .get = param_get_aauint
1246 };
1247
1248 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
1249 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
1250 #define param_check_aalockpolicy param_check_bool
1251 static const struct kernel_param_ops param_ops_aalockpolicy = {
1252 .flags = KERNEL_PARAM_OPS_FL_NOARG,
1253 .set = param_set_aalockpolicy,
1254 .get = param_get_aalockpolicy
1255 };
1256
1257 static int param_set_audit(const char *val, struct kernel_param *kp);
1258 static int param_get_audit(char *buffer, struct kernel_param *kp);
1259
1260 static int param_set_mode(const char *val, struct kernel_param *kp);
1261 static int param_get_mode(char *buffer, struct kernel_param *kp);
1262
1263 /* Flag values, also controllable via /sys/module/apparmor/parameters
1264 * We define special types as we want to do additional mediation.
1265 */
1266
1267 /* AppArmor global enforcement switch - complain, enforce, kill */
1268 enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
1269 module_param_call(mode, param_set_mode, param_get_mode,
1270 &aa_g_profile_mode, S_IRUGO | S_IWUSR);
1271
1272 #ifdef CONFIG_SECURITY_APPARMOR_HASH
1273 /* whether policy verification hashing is enabled */
1274 bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
1275 module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
1276 #endif
1277
1278 /* Debug mode */
1279 bool aa_g_debug;
1280 module_param_named(debug, aa_g_debug, aabool, S_IRUGO | S_IWUSR);
1281
1282 /* Audit mode */
1283 enum audit_mode aa_g_audit;
1284 module_param_call(audit, param_set_audit, param_get_audit, &aa_g_audit,
1285 S_IRUGO | S_IWUSR);
1286
1287 /* Determines if audit header is included in audited messages. This
1288 * provides more context if the audit daemon is not running
1289 */
1290 bool aa_g_audit_header = 1;
1291 module_param_named(audit_header, aa_g_audit_header, aabool, S_IRUGO | S_IWUSR);
1292
1293 /* lock out loading/removal of policy
1294 * TODO: add in at boot loading of policy, which is the only way to
1295 * load policy, if lock_policy is set
1296 */
1297 bool aa_g_lock_policy;
1298 module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
1299 S_IRUGO | S_IWUSR);
1300
1301 /* Syscall logging mode */
1302 bool aa_g_logsyscall;
1303 module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUGO | S_IWUSR);
1304
1305 /* Maximum pathname length before accesses will start getting rejected */
1306 unsigned int aa_g_path_max = 2 * PATH_MAX;
1307 module_param_named(path_max, aa_g_path_max, aauint, S_IRUGO | S_IWUSR);
1308
1309 /* Determines how paranoid loading of policy is and how much verification
1310 * on the loaded policy is done.
1311 * DEPRECATED: read only as strict checking of load is always done now
1312 * that none root users (user namespaces) can load policy.
1313 */
1314 bool aa_g_paranoid_load = 1;
1315 module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
1316
1317 /* Boot time disable flag */
1318 static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE;
1319 module_param_named(enabled, apparmor_enabled, bool, S_IRUGO);
1320
1321 /* Boot time to set use of default or unconfined as initial profile */
1322 bool aa_g_unconfined_init = CONFIG_SECURITY_APPARMOR_UNCONFINED_INIT;
1323 module_param_named(unconfined, aa_g_unconfined_init, aabool, S_IRUGO);
1324
1325
1326 static int __init apparmor_enabled_setup(char *str)
1327 {
1328 unsigned long enabled;
1329 int error = kstrtoul(str, 0, &enabled);
1330 if (!error)
1331 apparmor_enabled = enabled ? 1 : 0;
1332 return 1;
1333 }
1334
1335 __setup("apparmor=", apparmor_enabled_setup);
1336
1337 /* set global flag turning off the ability to load policy */
1338 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
1339 {
1340 if (!apparmor_enabled)
1341 return -EINVAL;
1342 if (apparmor_initialized && !policy_admin_capable(NULL))
1343 return -EPERM;
1344 return param_set_bool(val, kp);
1345 }
1346
1347 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
1348 {
1349 if (!apparmor_enabled)
1350 return -EINVAL;
1351 if (apparmor_initialized && !policy_view_capable(NULL))
1352 return -EPERM;
1353 return param_get_bool(buffer, kp);
1354 }
1355
1356 static int param_set_aabool(const char *val, const struct kernel_param *kp)
1357 {
1358 if (!apparmor_enabled)
1359 return -EINVAL;
1360 if (apparmor_initialized && !policy_admin_capable(NULL))
1361 return -EPERM;
1362 return param_set_bool(val, kp);
1363 }
1364
1365 static int param_get_aabool(char *buffer, const struct kernel_param *kp)
1366 {
1367 if (!apparmor_enabled)
1368 return -EINVAL;
1369 if (apparmor_initialized && !policy_view_capable(NULL))
1370 return -EPERM;
1371 return param_get_bool(buffer, kp);
1372 }
1373
1374 static int param_set_aauint(const char *val, const struct kernel_param *kp)
1375 {
1376 if (!apparmor_enabled)
1377 return -EINVAL;
1378 if (apparmor_initialized && !policy_admin_capable(NULL))
1379 return -EPERM;
1380 return param_set_uint(val, kp);
1381 }
1382
1383 static int param_get_aauint(char *buffer, const struct kernel_param *kp)
1384 {
1385 if (!apparmor_enabled)
1386 return -EINVAL;
1387 if (apparmor_initialized && !policy_view_capable(NULL))
1388 return -EPERM;
1389 return param_get_uint(buffer, kp);
1390 }
1391
1392 static int param_get_audit(char *buffer, struct kernel_param *kp)
1393 {
1394 if (!apparmor_enabled)
1395 return -EINVAL;
1396 if (apparmor_initialized && !policy_view_capable(NULL))
1397 return -EPERM;
1398 return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
1399 }
1400
1401 static int param_set_audit(const char *val, struct kernel_param *kp)
1402 {
1403 int i;
1404 if (!apparmor_enabled)
1405 return -EINVAL;
1406 if (!val)
1407 return -EINVAL;
1408 if (apparmor_initialized && !policy_admin_capable(NULL))
1409 return -EPERM;
1410
1411 for (i = 0; i < AUDIT_MAX_INDEX; i++) {
1412 if (strcmp(val, audit_mode_names[i]) == 0) {
1413 aa_g_audit = i;
1414 return 0;
1415 }
1416 }
1417
1418 return -EINVAL;
1419 }
1420
1421 static int param_get_mode(char *buffer, struct kernel_param *kp)
1422 {
1423 if (!apparmor_enabled)
1424 return -EINVAL;
1425 if (apparmor_initialized && !policy_view_capable(NULL))
1426 return -EPERM;
1427
1428 return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
1429 }
1430
1431 static int param_set_mode(const char *val, struct kernel_param *kp)
1432 {
1433 int i;
1434 if (!apparmor_enabled)
1435 return -EINVAL;
1436 if (!val)
1437 return -EINVAL;
1438 if (apparmor_initialized && !policy_admin_capable(NULL))
1439 return -EPERM;
1440
1441 for (i = 0; i < APPARMOR_MODE_NAMES_MAX_INDEX; i++) {
1442 if (strcmp(val, aa_profile_mode_names[i]) == 0) {
1443 aa_g_profile_mode = i;
1444 return 0;
1445 }
1446 }
1447
1448 return -EINVAL;
1449 }
1450
1451 /*
1452 * AppArmor init functions
1453 */
1454
1455 /**
1456 * set_init_ctx - set a task context and profile on the first task.
1457 */
1458 static int __init set_init_ctx(void)
1459 {
1460 struct cred *cred = (struct cred *)current->real_cred;
1461 struct aa_task_ctx *ctx;
1462
1463 ctx = aa_alloc_task_context(GFP_KERNEL);
1464 if (!ctx)
1465 return -ENOMEM;
1466
1467 if (!aa_g_unconfined_init) {
1468 ctx->label = aa_setup_default_label();
1469 if (!ctx->label) {
1470 aa_free_task_context(ctx);
1471 return -ENOMEM;
1472 }
1473 /* fs setup of default is done in aa_create_aafs() */
1474 } else
1475 ctx->label = aa_get_label(ns_unconfined(root_ns));
1476 cred_ctx(cred) = ctx;
1477
1478 return 0;
1479 }
1480
1481 static void destroy_buffers(void)
1482 {
1483 u32 i, j;
1484
1485 for_each_possible_cpu(i) {
1486 for_each_cpu_buffer(j) {
1487 kfree(per_cpu(aa_buffers, i).buf[j]);
1488 per_cpu(aa_buffers, i).buf[j] = NULL;
1489 }
1490 }
1491 }
1492
1493 static int __init alloc_buffers(void)
1494 {
1495 u32 i, j;
1496
1497 for_each_possible_cpu(i) {
1498 for_each_cpu_buffer(j) {
1499 char *buffer;
1500 if (cpu_to_node(i) > num_online_nodes())
1501 /* fallback to kmalloc for offline nodes */
1502 buffer = kmalloc(aa_g_path_max, GFP_KERNEL);
1503 else
1504 buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL,
1505 cpu_to_node(i));
1506 if (!buffer) {
1507 destroy_buffers();
1508 return -ENOMEM;
1509 }
1510 per_cpu(aa_buffers, i).buf[j] = buffer;
1511 }
1512 }
1513
1514 return 0;
1515 }
1516
1517 #ifdef CONFIG_SYSCTL
1518 static int apparmor_dointvec(struct ctl_table *table, int write,
1519 void __user *buffer, size_t *lenp, loff_t *ppos)
1520 {
1521 if (!policy_admin_capable(NULL))
1522 return -EPERM;
1523 if (!apparmor_enabled)
1524 return -EINVAL;
1525
1526 return proc_dointvec(table, write, buffer, lenp, ppos);
1527 }
1528
1529 static struct ctl_path apparmor_sysctl_path[] = {
1530 { .procname = "kernel", },
1531 { }
1532 };
1533
1534 static struct ctl_table apparmor_sysctl_table[] = {
1535 {
1536 .procname = "unprivileged_userns_apparmor_policy",
1537 .data = &unprivileged_userns_apparmor_policy,
1538 .maxlen = sizeof(int),
1539 .mode = 0600,
1540 .proc_handler = apparmor_dointvec,
1541 },
1542 { }
1543 };
1544
1545 static int __init apparmor_init_sysctl(void)
1546 {
1547 return register_sysctl_paths(apparmor_sysctl_path,
1548 apparmor_sysctl_table) ? 0 : -ENOMEM;
1549 }
1550 #else
1551 static inline int apparmor_init_sysctl(void)
1552 {
1553 return 0;
1554 }
1555 #endif /* CONFIG_SYSCTL */
1556
1557 static int __init apparmor_init(void)
1558 {
1559 int error;
1560
1561 if (!apparmor_enabled || !security_module_enable("apparmor")) {
1562 aa_info_message("AppArmor disabled by boot time parameter");
1563 apparmor_enabled = 0;
1564 return 0;
1565 }
1566
1567 error = aa_setup_dfa_engine();
1568 if (error) {
1569 AA_ERROR("Unable to setup dfa engine\n");
1570 goto alloc_out;
1571 }
1572
1573 error = aa_alloc_root_ns();
1574 if (error) {
1575 AA_ERROR("Unable to allocate default profile namespace\n");
1576 goto alloc_out;
1577 }
1578
1579 error = apparmor_init_sysctl();
1580 if (error) {
1581 AA_ERROR("Unable to register sysctls\n");
1582 goto alloc_out;
1583
1584 }
1585
1586 error = alloc_buffers();
1587 if (error) {
1588 AA_ERROR("Unable to allocate work buffers\n");
1589 goto buffers_out;
1590 }
1591
1592 error = set_init_ctx();
1593 if (error) {
1594 AA_ERROR("Failed to set context on init task\n");
1595 aa_free_root_ns();
1596 goto buffers_out;
1597 }
1598 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks));
1599
1600 /* Report that AppArmor successfully initialized */
1601 apparmor_initialized = 1;
1602 if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1603 aa_info_message("AppArmor initialized: complain mode enabled");
1604 else if (aa_g_profile_mode == APPARMOR_KILL)
1605 aa_info_message("AppArmor initialized: kill mode enabled");
1606 else
1607 aa_info_message("AppArmor initialized");
1608
1609 return error;
1610
1611 buffers_out:
1612 destroy_buffers();
1613
1614 alloc_out:
1615 aa_destroy_aafs();
1616 aa_teardown_dfa_engine();
1617
1618 apparmor_enabled = 0;
1619 return error;
1620 }
1621
1622 security_initcall(apparmor_init);