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