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