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