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