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