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