1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * AppArmor security module
4 *
5 * This file contains AppArmor LSM hooks.
6 *
7 * Copyright (C) 1998-2008 Novell/SUSE
8 * Copyright 2009-2010 Canonical Ltd.
9 */
10
11 #include <linux/lsm_hooks.h>
12 #include <linux/moduleparam.h>
13 #include <linux/mm.h>
14 #include <linux/mman.h>
15 #include <linux/mount.h>
16 #include <linux/namei.h>
17 #include <linux/ptrace.h>
18 #include <linux/ctype.h>
19 #include <linux/sysctl.h>
20 #include <linux/sysfs.h>
21 #include <linux/audit.h>
22 #include <linux/user_namespace.h>
23 #include <linux/netfilter_ipv4.h>
24 #include <linux/netfilter_ipv6.h>
25 #include <linux/zstd.h>
26 #include <net/sock.h>
27 #include <uapi/linux/mount.h>
28 #include <uapi/linux/lsm.h>
29
30 #include "include/af_unix.h"
31 #include "include/apparmor.h"
32 #include "include/apparmorfs.h"
33 #include "include/audit.h"
34 #include "include/capability.h"
35 #include "include/cred.h"
36 #include "include/crypto.h"
37 #include "include/file.h"
38 #include "include/ipc.h"
39 #include "include/net.h"
40 #include "include/path.h"
41 #include "include/label.h"
42 #include "include/policy.h"
43 #include "include/policy_ns.h"
44 #include "include/procattr.h"
45 #include "include/mount.h"
46 #include "include/secid.h"
47
48 /* Flag indicating whether initialization completed */
49 int apparmor_initialized;
50
51 union aa_buffer {
52 struct list_head list;
53 DECLARE_FLEX_ARRAY(char, buffer);
54 };
55
56 struct aa_local_cache {
57 unsigned int hold;
58 unsigned int count;
59 struct list_head head;
60 };
61
62 #define RESERVE_COUNT 2
63 static int reserve_count = RESERVE_COUNT;
64 static int buffer_count;
65
66 static LIST_HEAD(aa_global_buffers);
67 static DEFINE_SPINLOCK(aa_buffers_lock);
68 static DEFINE_PER_CPU(struct aa_local_cache, aa_local_buffers);
69
70 /*
71 * LSM hook functions
72 */
73
74 /*
75 * put the associated labels
76 */
apparmor_cred_free(struct cred * cred)77 static void apparmor_cred_free(struct cred *cred)
78 {
79 aa_put_label(cred_label(cred));
80 set_cred_label(cred, NULL);
81 }
82
83 /*
84 * allocate the apparmor part of blank credentials
85 */
apparmor_cred_alloc_blank(struct cred * cred,gfp_t gfp)86 static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
87 {
88 set_cred_label(cred, NULL);
89 return 0;
90 }
91
92 /*
93 * prepare new cred label for modification by prepare_cred block
94 */
apparmor_cred_prepare(struct cred * new,const struct cred * old,gfp_t gfp)95 static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
96 gfp_t gfp)
97 {
98 set_cred_label(new, aa_get_newest_label(cred_label(old)));
99 return 0;
100 }
101
102 /*
103 * transfer the apparmor data to a blank set of creds
104 */
apparmor_cred_transfer(struct cred * new,const struct cred * old)105 static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
106 {
107 set_cred_label(new, aa_get_newest_label(cred_label(old)));
108 }
109
apparmor_task_free(struct task_struct * task)110 static void apparmor_task_free(struct task_struct *task)
111 {
112
113 aa_free_task_ctx(task_ctx(task));
114 }
115
apparmor_task_alloc(struct task_struct * task,u64 clone_flags)116 static int apparmor_task_alloc(struct task_struct *task,
117 u64 clone_flags)
118 {
119 struct aa_task_ctx *new = task_ctx(task);
120
121 aa_dup_task_ctx(new, task_ctx(current));
122
123 return 0;
124 }
125
apparmor_ptrace_access_check(struct task_struct * child,unsigned int mode)126 static int apparmor_ptrace_access_check(struct task_struct *child,
127 unsigned int mode)
128 {
129 struct aa_label *tracer, *tracee;
130 const struct cred *cred;
131 int error;
132 bool needput;
133
134 cred = get_task_cred(child);
135 tracee = cred_label(cred); /* ref count on cred */
136 tracer = __begin_current_label_crit_section(&needput);
137 error = aa_may_ptrace(current_cred(), tracer, cred, tracee,
138 (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
139 : AA_PTRACE_TRACE);
140 __end_current_label_crit_section(tracer, needput);
141 put_cred(cred);
142
143 return error;
144 }
145
apparmor_ptrace_traceme(struct task_struct * parent)146 static int apparmor_ptrace_traceme(struct task_struct *parent)
147 {
148 struct aa_label *tracer, *tracee;
149 const struct cred *cred;
150 int error;
151 bool needput;
152
153 tracee = __begin_current_label_crit_section(&needput);
154 cred = get_task_cred(parent);
155 tracer = cred_label(cred); /* ref count on cred */
156 error = aa_may_ptrace(cred, tracer, current_cred(), tracee,
157 AA_PTRACE_TRACE);
158 put_cred(cred);
159 __end_current_label_crit_section(tracee, needput);
160
161 return error;
162 }
163
164 /* Derived from security/commoncap.c:cap_capget */
apparmor_capget(const struct task_struct * target,kernel_cap_t * effective,kernel_cap_t * inheritable,kernel_cap_t * permitted)165 static int apparmor_capget(const struct task_struct *target, kernel_cap_t *effective,
166 kernel_cap_t *inheritable, kernel_cap_t *permitted)
167 {
168 struct aa_label *label;
169 const struct cred *cred;
170
171 rcu_read_lock();
172 cred = __task_cred(target);
173 label = aa_get_newest_cred_label(cred);
174
175 /*
176 * cap_capget is stacked ahead of this and will
177 * initialize effective and permitted.
178 */
179 if (!unconfined(label)) {
180 struct aa_profile *profile;
181 struct label_it i;
182
183 label_for_each_confined(i, label, profile) {
184 kernel_cap_t allowed;
185
186 allowed = aa_profile_capget(profile);
187 *effective = cap_intersect(*effective, allowed);
188 *permitted = cap_intersect(*permitted, allowed);
189 }
190 }
191 rcu_read_unlock();
192 aa_put_label(label);
193
194 return 0;
195 }
196
apparmor_capable(const struct cred * cred,struct user_namespace * ns,int cap,unsigned int opts)197 static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
198 int cap, unsigned int opts)
199 {
200 struct aa_label *label;
201 int error = 0;
202
203 label = aa_get_newest_cred_label(cred);
204 if (!unconfined(label))
205 error = aa_capable(cred, label, cap, opts);
206 aa_put_label(label);
207
208 return error;
209 }
210
211 /**
212 * common_perm - basic common permission check wrapper fn for paths
213 * @op: operation being checked
214 * @path: path to check permission of (NOT NULL)
215 * @mask: requested permissions mask
216 * @cond: conditional info for the permission request (NOT NULL)
217 *
218 * Returns: %0 else error code if error or permission denied
219 */
common_perm(const char * op,const struct path * path,u32 mask,struct path_cond * cond)220 static int common_perm(const char *op, const struct path *path, u32 mask,
221 struct path_cond *cond)
222 {
223 struct aa_label *label;
224 int error = 0;
225 bool needput;
226
227 label = __begin_current_label_crit_section(&needput);
228 if (!unconfined(label))
229 error = aa_path_perm(op, current_cred(), label, path, 0, mask,
230 cond);
231 __end_current_label_crit_section(label, needput);
232
233 return error;
234 }
235
236 /**
237 * common_perm_cond - common permission wrapper around inode cond
238 * @op: operation being checked
239 * @path: location to check (NOT NULL)
240 * @mask: requested permissions mask
241 *
242 * Returns: %0 else error code if error or permission denied
243 */
common_perm_cond(const char * op,const struct path * path,u32 mask)244 static int common_perm_cond(const char *op, const struct path *path, u32 mask)
245 {
246 vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_idmap(path->mnt),
247 d_backing_inode(path->dentry));
248 struct path_cond cond = {
249 vfsuid_into_kuid(vfsuid),
250 d_backing_inode(path->dentry)->i_mode
251 };
252
253 if (!path_mediated_fs(path->dentry))
254 return 0;
255
256 return common_perm(op, path, mask, &cond);
257 }
258
259 /**
260 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
261 * @op: operation being checked
262 * @dir: directory of the dentry (NOT NULL)
263 * @dentry: dentry to check (NOT NULL)
264 * @mask: requested permissions mask
265 * @cond: conditional info for the permission request (NOT NULL)
266 *
267 * Returns: %0 else error code if error or permission denied
268 */
common_perm_dir_dentry(const char * op,const struct path * dir,struct dentry * dentry,u32 mask,struct path_cond * cond)269 static int common_perm_dir_dentry(const char *op, const struct path *dir,
270 struct dentry *dentry, u32 mask,
271 struct path_cond *cond)
272 {
273 struct path path = { .mnt = dir->mnt, .dentry = dentry };
274
275 return common_perm(op, &path, mask, cond);
276 }
277
278 /**
279 * common_perm_rm - common permission wrapper for operations doing rm
280 * @op: operation being checked
281 * @dir: directory that the dentry is in (NOT NULL)
282 * @dentry: dentry being rm'd (NOT NULL)
283 * @mask: requested permission mask
284 *
285 * Returns: %0 else error code if error or permission denied
286 */
common_perm_rm(const char * op,const struct path * dir,struct dentry * dentry,u32 mask)287 static int common_perm_rm(const char *op, const struct path *dir,
288 struct dentry *dentry, u32 mask)
289 {
290 struct inode *inode = d_backing_inode(dentry);
291 struct path_cond cond = { };
292 vfsuid_t vfsuid;
293
294 if (!inode || !path_mediated_fs(dentry))
295 return 0;
296
297 vfsuid = i_uid_into_vfsuid(mnt_idmap(dir->mnt), inode);
298 cond.uid = vfsuid_into_kuid(vfsuid);
299 cond.mode = inode->i_mode;
300
301 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
302 }
303
304 /**
305 * common_perm_create - common permission wrapper for operations doing create
306 * @op: operation being checked
307 * @dir: directory that dentry will be created in (NOT NULL)
308 * @dentry: dentry to create (NOT NULL)
309 * @mask: request permission mask
310 * @mode: created file mode
311 *
312 * Returns: %0 else error code if error or permission denied
313 */
common_perm_create(const char * op,const struct path * dir,struct dentry * dentry,u32 mask,umode_t mode)314 static int common_perm_create(const char *op, const struct path *dir,
315 struct dentry *dentry, u32 mask, umode_t mode)
316 {
317 struct path_cond cond = { current_fsuid(), mode };
318
319 if (!path_mediated_fs(dir->dentry))
320 return 0;
321
322 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
323 }
324
apparmor_path_unlink(const struct path * dir,struct dentry * dentry)325 static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
326 {
327 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
328 }
329
apparmor_path_mkdir(const struct path * dir,struct dentry * dentry,umode_t mode)330 static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
331 umode_t mode)
332 {
333 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
334 S_IFDIR);
335 }
336
apparmor_path_rmdir(const struct path * dir,struct dentry * dentry)337 static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
338 {
339 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
340 }
341
apparmor_path_mknod(const struct path * dir,struct dentry * dentry,umode_t mode,unsigned int dev)342 static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
343 umode_t mode, unsigned int dev)
344 {
345 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
346 }
347
apparmor_path_truncate(const struct path * path)348 static int apparmor_path_truncate(const struct path *path)
349 {
350 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
351 }
352
apparmor_file_truncate(struct file * file)353 static int apparmor_file_truncate(struct file *file)
354 {
355 return apparmor_path_truncate(&file->f_path);
356 }
357
apparmor_path_symlink(const struct path * dir,struct dentry * dentry,const char * old_name)358 static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
359 const char *old_name)
360 {
361 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
362 S_IFLNK);
363 }
364
apparmor_path_link(struct dentry * old_dentry,const struct path * new_dir,struct dentry * new_dentry)365 static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
366 struct dentry *new_dentry)
367 {
368 struct aa_label *label;
369 int error = 0;
370
371 if (!path_mediated_fs(old_dentry))
372 return 0;
373
374 label = begin_current_label_crit_section();
375 if (!unconfined(label))
376 error = aa_path_link(current_cred(), label, old_dentry, new_dir,
377 new_dentry);
378 end_current_label_crit_section(label);
379
380 return error;
381 }
382
apparmor_path_rename(const struct path * old_dir,struct dentry * old_dentry,const struct path * new_dir,struct dentry * new_dentry,const unsigned int flags)383 static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
384 const struct path *new_dir, struct dentry *new_dentry,
385 const unsigned int flags)
386 {
387 struct aa_label *label;
388 int error = 0;
389
390 if (!path_mediated_fs(old_dentry))
391 return 0;
392 if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry))
393 return 0;
394
395 label = begin_current_label_crit_section();
396 if (!unconfined(label)) {
397 struct mnt_idmap *idmap = mnt_idmap(old_dir->mnt);
398 vfsuid_t vfsuid;
399 struct path old_path = { .mnt = old_dir->mnt,
400 .dentry = old_dentry };
401 struct path new_path = { .mnt = new_dir->mnt,
402 .dentry = new_dentry };
403 struct path_cond cond = {
404 .mode = d_backing_inode(old_dentry)->i_mode
405 };
406 vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
407 cond.uid = vfsuid_into_kuid(vfsuid);
408
409 if (flags & RENAME_EXCHANGE) {
410 struct path_cond cond_exchange = {
411 .mode = d_backing_inode(new_dentry)->i_mode,
412 };
413 vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(new_dentry));
414 cond_exchange.uid = vfsuid_into_kuid(vfsuid);
415
416 error = aa_path_perm(OP_RENAME_SRC, current_cred(),
417 label, &new_path, 0,
418 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
419 AA_MAY_SETATTR | AA_MAY_DELETE,
420 &cond_exchange);
421 if (!error)
422 error = aa_path_perm(OP_RENAME_DEST, current_cred(),
423 label, &old_path,
424 0, MAY_WRITE | AA_MAY_SETATTR |
425 AA_MAY_CREATE, &cond_exchange);
426 }
427
428 if (!error)
429 error = aa_path_perm(OP_RENAME_SRC, current_cred(),
430 label, &old_path, 0,
431 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
432 AA_MAY_SETATTR | AA_MAY_DELETE,
433 &cond);
434 if (!error)
435 error = aa_path_perm(OP_RENAME_DEST, current_cred(),
436 label, &new_path,
437 0, MAY_WRITE | AA_MAY_SETATTR |
438 AA_MAY_CREATE, &cond);
439
440 }
441 end_current_label_crit_section(label);
442
443 return error;
444 }
445
apparmor_path_chmod(const struct path * path,umode_t mode)446 static int apparmor_path_chmod(const struct path *path, umode_t mode)
447 {
448 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
449 }
450
apparmor_path_chown(const struct path * path,kuid_t uid,kgid_t gid)451 static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
452 {
453 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
454 }
455
apparmor_inode_getattr(const struct path * path)456 static int apparmor_inode_getattr(const struct path *path)
457 {
458 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
459 }
460
apparmor_file_open(struct file * file)461 static int apparmor_file_open(struct file *file)
462 {
463 struct aa_file_ctx *fctx = file_ctx(file);
464 struct aa_label *label;
465 int error = 0;
466 bool needput;
467
468 if (!path_mediated_fs(file->f_path.dentry))
469 return 0;
470
471 /* If in exec, permission is handled by bprm hooks.
472 * Cache permissions granted by the previous exec check, with
473 * implicit read and executable mmap which are required to
474 * actually execute the image.
475 *
476 * Illogically, FMODE_EXEC is in f_flags, not f_mode.
477 */
478 if (file->f_flags & __FMODE_EXEC) {
479 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
480 return 0;
481 }
482
483 label = aa_get_newest_cred_label_condref(file->f_cred, &needput);
484 if (!unconfined(label)) {
485 struct mnt_idmap *idmap = file_mnt_idmap(file);
486 struct inode *inode = file_inode(file);
487 vfsuid_t vfsuid;
488 struct path_cond cond = {
489 .mode = inode->i_mode,
490 };
491 vfsuid = i_uid_into_vfsuid(idmap, inode);
492 cond.uid = vfsuid_into_kuid(vfsuid);
493
494 error = aa_path_perm(OP_OPEN, file->f_cred,
495 label, &file->f_path, 0,
496 aa_map_file_to_perms(file), &cond);
497 /* todo cache full allowed permissions set and state */
498 fctx->allow = aa_map_file_to_perms(file);
499 }
500 aa_put_label_condref(label, needput);
501
502 return error;
503 }
504
apparmor_file_alloc_security(struct file * file)505 static int apparmor_file_alloc_security(struct file *file)
506 {
507 struct aa_file_ctx *ctx = file_ctx(file);
508 struct aa_label *label = begin_current_label_crit_section();
509
510 spin_lock_init(&ctx->lock);
511 rcu_assign_pointer(ctx->label, aa_get_label(label));
512 end_current_label_crit_section(label);
513 return 0;
514 }
515
apparmor_file_free_security(struct file * file)516 static void apparmor_file_free_security(struct file *file)
517 {
518 struct aa_file_ctx *ctx = file_ctx(file);
519
520 if (ctx)
521 aa_put_label(rcu_access_pointer(ctx->label));
522 }
523
common_file_perm(const char * op,struct file * file,u32 mask)524 static int common_file_perm(const char *op, struct file *file, u32 mask)
525 {
526 struct aa_label *label;
527 int error = 0;
528
529 label = begin_current_label_crit_section();
530 error = aa_file_perm(op, current_cred(), label, file, mask, false);
531 end_current_label_crit_section(label);
532
533 return error;
534 }
535
apparmor_file_receive(struct file * file)536 static int apparmor_file_receive(struct file *file)
537 {
538 return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file));
539 }
540
apparmor_file_permission(struct file * file,int mask)541 static int apparmor_file_permission(struct file *file, int mask)
542 {
543 return common_file_perm(OP_FPERM, file, mask);
544 }
545
apparmor_file_lock(struct file * file,unsigned int cmd)546 static int apparmor_file_lock(struct file *file, unsigned int cmd)
547 {
548 u32 mask = AA_MAY_LOCK;
549
550 if (cmd == F_WRLCK)
551 mask |= MAY_WRITE;
552
553 return common_file_perm(OP_FLOCK, file, mask);
554 }
555
common_mmap(const char * op,struct file * file,unsigned long prot,unsigned long flags)556 static int common_mmap(const char *op, struct file *file, unsigned long prot,
557 unsigned long flags)
558 {
559 int mask = 0;
560
561 if (!file || !file_ctx(file))
562 return 0;
563
564 if (prot & PROT_READ)
565 mask |= MAY_READ;
566 /*
567 * Private mappings don't require write perms since they don't
568 * write back to the files
569 */
570 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
571 mask |= MAY_WRITE;
572 if (prot & PROT_EXEC)
573 mask |= AA_EXEC_MMAP;
574
575 return common_file_perm(op, file, mask);
576 }
577
apparmor_mmap_file(struct file * file,unsigned long reqprot,unsigned long prot,unsigned long flags)578 static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
579 unsigned long prot, unsigned long flags)
580 {
581 return common_mmap(OP_FMMAP, file, prot, flags);
582 }
583
apparmor_file_mprotect(struct vm_area_struct * vma,unsigned long reqprot,unsigned long prot)584 static int apparmor_file_mprotect(struct vm_area_struct *vma,
585 unsigned long reqprot, unsigned long prot)
586 {
587 return common_mmap(OP_FMPROT, vma->vm_file, prot,
588 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
589 }
590
591 #ifdef CONFIG_IO_URING
audit_uring_mask(u32 mask)592 static const char *audit_uring_mask(u32 mask)
593 {
594 if (mask & AA_MAY_CREATE_SQPOLL)
595 return "sqpoll";
596 if (mask & AA_MAY_OVERRIDE_CRED)
597 return "override_creds";
598 return "";
599 }
600
audit_uring_cb(struct audit_buffer * ab,void * va)601 static void audit_uring_cb(struct audit_buffer *ab, void *va)
602 {
603 struct apparmor_audit_data *ad = aad_of_va(va);
604
605 if (ad->request & AA_URING_PERM_MASK) {
606 audit_log_format(ab, " requested=\"%s\"",
607 audit_uring_mask(ad->request));
608 if (ad->denied & AA_URING_PERM_MASK) {
609 audit_log_format(ab, " denied=\"%s\"",
610 audit_uring_mask(ad->denied));
611 }
612 }
613 if (ad->uring.target) {
614 audit_log_format(ab, " tcontext=");
615 aa_label_xaudit(ab, labels_ns(ad->subj_label),
616 ad->uring.target,
617 FLAGS_NONE, GFP_ATOMIC);
618 }
619 }
620
profile_uring(struct aa_profile * profile,u32 request,struct aa_label * new,int cap,struct apparmor_audit_data * ad)621 static int profile_uring(struct aa_profile *profile, u32 request,
622 struct aa_label *new, int cap,
623 struct apparmor_audit_data *ad)
624 {
625 unsigned int state;
626 struct aa_ruleset *rules;
627 int error = 0;
628
629 AA_BUG(!profile);
630
631 rules = profile->label.rules[0];
632 state = RULE_MEDIATES(rules, AA_CLASS_IO_URING);
633 if (state) {
634 struct aa_perms perms = { };
635
636 if (new) {
637 aa_label_match(profile, rules, new, state,
638 false, request, &perms);
639 } else {
640 perms = *aa_lookup_perms(rules->policy, state);
641 }
642 aa_apply_modes_to_perms(profile, &perms);
643 error = aa_check_perms(profile, &perms, request, ad,
644 audit_uring_cb);
645 }
646
647 return error;
648 }
649
650 /**
651 * apparmor_uring_override_creds - check the requested cred override
652 * @new: the target creds
653 *
654 * Check to see if the current task is allowed to override it's credentials
655 * to service an io_uring operation.
656 */
apparmor_uring_override_creds(const struct cred * new)657 static int apparmor_uring_override_creds(const struct cred *new)
658 {
659 struct aa_profile *profile;
660 struct aa_label *label;
661 int error;
662 bool needput;
663 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING,
664 OP_URING_OVERRIDE);
665
666 ad.uring.target = cred_label(new);
667 label = __begin_current_label_crit_section(&needput);
668 error = fn_for_each(label, profile,
669 profile_uring(profile, AA_MAY_OVERRIDE_CRED,
670 cred_label(new), CAP_SYS_ADMIN, &ad));
671 __end_current_label_crit_section(label, needput);
672
673 return error;
674 }
675
676 /**
677 * apparmor_uring_sqpoll - check if a io_uring polling thread can be created
678 *
679 * Check to see if the current task is allowed to create a new io_uring
680 * kernel polling thread.
681 */
apparmor_uring_sqpoll(void)682 static int apparmor_uring_sqpoll(void)
683 {
684 struct aa_profile *profile;
685 struct aa_label *label;
686 int error;
687 bool needput;
688 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING,
689 OP_URING_SQPOLL);
690
691 label = __begin_current_label_crit_section(&needput);
692 error = fn_for_each(label, profile,
693 profile_uring(profile, AA_MAY_CREATE_SQPOLL,
694 NULL, CAP_SYS_ADMIN, &ad));
695 __end_current_label_crit_section(label, needput);
696
697 return error;
698 }
699 #endif /* CONFIG_IO_URING */
700
apparmor_sb_mount(const char * dev_name,const struct path * path,const char * type,unsigned long flags,void * data)701 static int apparmor_sb_mount(const char *dev_name, const struct path *path,
702 const char *type, unsigned long flags, void *data)
703 {
704 struct aa_label *label;
705 int error = 0;
706 bool needput;
707
708 /* Discard magic */
709 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
710 flags &= ~MS_MGC_MSK;
711
712 flags &= ~AA_MS_IGNORE_MASK;
713
714 label = __begin_current_label_crit_section(&needput);
715 if (!unconfined(label)) {
716 if (flags & MS_REMOUNT)
717 error = aa_remount(current_cred(), label, path, flags,
718 data);
719 else if (flags & MS_BIND)
720 error = aa_bind_mount(current_cred(), label, path,
721 dev_name, flags);
722 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
723 MS_UNBINDABLE))
724 error = aa_mount_change_type(current_cred(), label,
725 path, flags);
726 else if (flags & MS_MOVE)
727 error = aa_move_mount_old(current_cred(), label, path,
728 dev_name);
729 else
730 error = aa_new_mount(current_cred(), label, dev_name,
731 path, type, flags, data);
732 }
733 __end_current_label_crit_section(label, needput);
734
735 return error;
736 }
737
apparmor_move_mount(const struct path * from_path,const struct path * to_path)738 static int apparmor_move_mount(const struct path *from_path,
739 const struct path *to_path)
740 {
741 struct aa_label *label;
742 int error = 0;
743 bool needput;
744
745 label = __begin_current_label_crit_section(&needput);
746 if (!unconfined(label))
747 error = aa_move_mount(current_cred(), label, from_path,
748 to_path);
749 __end_current_label_crit_section(label, needput);
750
751 return error;
752 }
753
apparmor_sb_umount(struct vfsmount * mnt,int flags)754 static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
755 {
756 struct aa_label *label;
757 int error = 0;
758 bool needput;
759
760 label = __begin_current_label_crit_section(&needput);
761 if (!unconfined(label))
762 error = aa_umount(current_cred(), label, mnt, flags);
763 __end_current_label_crit_section(label, needput);
764
765 return error;
766 }
767
apparmor_sb_pivotroot(const struct path * old_path,const struct path * new_path)768 static int apparmor_sb_pivotroot(const struct path *old_path,
769 const struct path *new_path)
770 {
771 struct aa_label *label;
772 int error = 0;
773
774 label = aa_get_current_label();
775 if (!unconfined(label))
776 error = aa_pivotroot(current_cred(), label, old_path, new_path);
777 aa_put_label(label);
778
779 return error;
780 }
781
apparmor_getselfattr(unsigned int attr,struct lsm_ctx __user * lx,u32 * size,u32 flags)782 static int apparmor_getselfattr(unsigned int attr, struct lsm_ctx __user *lx,
783 u32 *size, u32 flags)
784 {
785 int error = -ENOENT;
786 struct aa_task_ctx *ctx = task_ctx(current);
787 struct aa_label *label = NULL;
788 char *value = NULL;
789
790 switch (attr) {
791 case LSM_ATTR_CURRENT:
792 label = aa_get_newest_label(cred_label(current_cred()));
793 break;
794 case LSM_ATTR_PREV:
795 if (ctx->previous)
796 label = aa_get_newest_label(ctx->previous);
797 break;
798 case LSM_ATTR_EXEC:
799 if (ctx->onexec)
800 label = aa_get_newest_label(ctx->onexec);
801 break;
802 default:
803 error = -EOPNOTSUPP;
804 break;
805 }
806
807 if (label) {
808 error = aa_getprocattr(label, &value, false);
809 if (error > 0)
810 error = lsm_fill_user_ctx(lx, size, value, error,
811 LSM_ID_APPARMOR, 0);
812 kfree(value);
813 }
814
815 aa_put_label(label);
816
817 if (error < 0)
818 return error;
819 return 1;
820 }
821
apparmor_getprocattr(struct task_struct * task,const char * name,char ** value)822 static int apparmor_getprocattr(struct task_struct *task, const char *name,
823 char **value)
824 {
825 int error = -ENOENT;
826 struct aa_label *label = NULL;
827
828 rcu_read_lock();
829 if (strcmp(name, "current") == 0)
830 label = aa_get_newest_cred_label(__task_cred(task));
831 else if (strcmp(name, "prev") == 0 && task_ctx(task)->previous)
832 label = aa_get_newest_label(task_ctx(task)->previous);
833 else if (strcmp(name, "exec") == 0 && task_ctx(task)->onexec)
834 label = aa_get_newest_label(task_ctx(task)->onexec);
835 else
836 error = -EINVAL;
837 rcu_read_unlock();
838
839 if (label)
840 error = aa_getprocattr(label, value, true);
841
842 aa_put_label(label);
843
844 return error;
845 }
846
do_setattr(u64 attr,void * value,size_t size)847 static int do_setattr(u64 attr, void *value, size_t size)
848 {
849 char *command, *largs = NULL, *args = value;
850 size_t arg_size;
851 int error;
852 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE,
853 OP_SETPROCATTR);
854
855 if (size == 0)
856 return -EINVAL;
857
858 /* AppArmor requires that the buffer must be null terminated atm */
859 if (args[size - 1] != '\0') {
860 largs = args = kmemdup_nul(value, size, GFP_KERNEL);
861 if (!args)
862 return -ENOMEM;
863 }
864
865 error = -EINVAL;
866 args = strim(args);
867 command = strsep(&args, " ");
868 if (!args)
869 goto out;
870 args = skip_spaces(args);
871 if (!*args)
872 goto out;
873
874 arg_size = size - (args - (largs ? largs : (char *) value));
875 if (attr == LSM_ATTR_CURRENT) {
876 if (strcmp(command, "changehat") == 0) {
877 error = aa_setprocattr_changehat(args, arg_size,
878 AA_CHANGE_NOFLAGS);
879 } else if (strcmp(command, "permhat") == 0) {
880 error = aa_setprocattr_changehat(args, arg_size,
881 AA_CHANGE_TEST);
882 } else if (strcmp(command, "changeprofile") == 0) {
883 error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
884 } else if (strcmp(command, "permprofile") == 0) {
885 error = aa_change_profile(args, AA_CHANGE_TEST);
886 } else if (strcmp(command, "stack") == 0) {
887 error = aa_change_profile(args, AA_CHANGE_STACK);
888 } else
889 goto fail;
890 } else if (attr == LSM_ATTR_EXEC) {
891 if (strcmp(command, "exec") == 0)
892 error = aa_change_profile(args, AA_CHANGE_ONEXEC);
893 else if (strcmp(command, "stack") == 0)
894 error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
895 AA_CHANGE_STACK));
896 else
897 goto fail;
898 } else
899 /* only support the "current" and "exec" process attributes */
900 goto fail;
901
902 if (!error)
903 error = size;
904 out:
905 kfree(largs);
906 return error;
907
908 fail:
909 ad.subj_label = begin_current_label_crit_section();
910 if (attr == LSM_ATTR_CURRENT)
911 ad.info = "current";
912 else if (attr == LSM_ATTR_EXEC)
913 ad.info = "exec";
914 else
915 ad.info = "invalid";
916 ad.error = error = -EINVAL;
917 aa_audit_msg(AUDIT_APPARMOR_DENIED, &ad, NULL);
918 end_current_label_crit_section(ad.subj_label);
919 goto out;
920 }
921
apparmor_setselfattr(unsigned int attr,struct lsm_ctx * ctx,u32 size,u32 flags)922 static int apparmor_setselfattr(unsigned int attr, struct lsm_ctx *ctx,
923 u32 size, u32 flags)
924 {
925 int rc;
926
927 if (attr != LSM_ATTR_CURRENT && attr != LSM_ATTR_EXEC)
928 return -EOPNOTSUPP;
929
930 rc = do_setattr(attr, ctx->ctx, ctx->ctx_len);
931 if (rc > 0)
932 return 0;
933 return rc;
934 }
935
apparmor_setprocattr(const char * name,void * value,size_t size)936 static int apparmor_setprocattr(const char *name, void *value,
937 size_t size)
938 {
939 int attr = lsm_name_to_attr(name);
940
941 if (attr)
942 return do_setattr(attr, value, size);
943 return -EINVAL;
944 }
945
946 /**
947 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
948 * @bprm: binprm for the exec (NOT NULL)
949 */
apparmor_bprm_committing_creds(const struct linux_binprm * bprm)950 static void apparmor_bprm_committing_creds(const struct linux_binprm *bprm)
951 {
952 struct aa_label *label = aa_current_raw_label();
953 struct aa_label *new_label = cred_label(bprm->cred);
954
955 /* bail out if unconfined or not changing profile */
956 if ((new_label->proxy == label->proxy) ||
957 (unconfined(new_label)))
958 return;
959
960 aa_inherit_files(bprm->cred, current->files);
961
962 current->pdeath_signal = 0;
963
964 /* reset soft limits and set hard limits for the new label */
965 __aa_transition_rlimits(label, new_label);
966 }
967
968 /**
969 * apparmor_bprm_committed_creds() - do cleanup after new creds committed
970 * @bprm: binprm for the exec (NOT NULL)
971 */
apparmor_bprm_committed_creds(const struct linux_binprm * bprm)972 static void apparmor_bprm_committed_creds(const struct linux_binprm *bprm)
973 {
974 /* clear out temporary/transitional state from the context */
975 aa_clear_task_ctx_trans(task_ctx(current));
976
977 return;
978 }
979
apparmor_current_getlsmprop_subj(struct lsm_prop * prop)980 static void apparmor_current_getlsmprop_subj(struct lsm_prop *prop)
981 {
982 struct aa_label *label;
983 bool needput;
984
985 label = __begin_current_label_crit_section(&needput);
986 prop->apparmor.label = label;
987 __end_current_label_crit_section(label, needput);
988 }
989
apparmor_task_getlsmprop_obj(struct task_struct * p,struct lsm_prop * prop)990 static void apparmor_task_getlsmprop_obj(struct task_struct *p,
991 struct lsm_prop *prop)
992 {
993 struct aa_label *label = aa_get_task_label(p);
994
995 prop->apparmor.label = label;
996 aa_put_label(label);
997 }
998
apparmor_task_setrlimit(struct task_struct * task,unsigned int resource,struct rlimit * new_rlim)999 static int apparmor_task_setrlimit(struct task_struct *task,
1000 unsigned int resource, struct rlimit *new_rlim)
1001 {
1002 struct aa_label *label;
1003 int error = 0;
1004 bool needput;
1005
1006 label = __begin_current_label_crit_section(&needput);
1007
1008 if (!unconfined(label))
1009 error = aa_task_setrlimit(current_cred(), label, task,
1010 resource, new_rlim);
1011 __end_current_label_crit_section(label, needput);
1012
1013 return error;
1014 }
1015
apparmor_task_kill(struct task_struct * target,struct kernel_siginfo * info,int sig,const struct cred * cred)1016 static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info,
1017 int sig, const struct cred *cred)
1018 {
1019 const struct cred *tc;
1020 struct aa_label *cl, *tl;
1021 int error;
1022 bool needput;
1023
1024 tc = get_task_cred(target);
1025 tl = aa_get_newest_cred_label(tc);
1026 if (cred) {
1027 /*
1028 * Dealing with USB IO specific behavior
1029 */
1030 cl = aa_get_newest_cred_label(cred);
1031 error = aa_may_signal(cred, cl, tc, tl, sig);
1032 aa_put_label(cl);
1033 } else {
1034 cl = __begin_current_label_crit_section(&needput);
1035 error = aa_may_signal(current_cred(), cl, tc, tl, sig);
1036 __end_current_label_crit_section(cl, needput);
1037 }
1038 aa_put_label(tl);
1039 put_cred(tc);
1040
1041 return error;
1042 }
1043
apparmor_userns_create(const struct cred * cred)1044 static int apparmor_userns_create(const struct cred *cred)
1045 {
1046 struct aa_label *label;
1047 struct aa_profile *profile;
1048 int error = 0;
1049 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_TASK, AA_CLASS_NS,
1050 OP_USERNS_CREATE);
1051
1052 ad.subj_cred = current_cred();
1053
1054 label = begin_current_label_crit_section();
1055 if (!unconfined(label)) {
1056 error = fn_for_each(label, profile,
1057 aa_profile_ns_perm(profile, &ad,
1058 AA_USERNS_CREATE));
1059 }
1060 end_current_label_crit_section(label);
1061
1062 return error;
1063 }
1064
apparmor_sk_alloc_security(struct sock * sk,int family,gfp_t gfp)1065 static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t gfp)
1066 {
1067 struct aa_sk_ctx *ctx = aa_sock(sk);
1068 struct aa_label *label;
1069 bool needput;
1070
1071 label = __begin_current_label_crit_section(&needput);
1072 //spin_lock_init(&ctx->lock);
1073 rcu_assign_pointer(ctx->label, aa_get_label(label));
1074 rcu_assign_pointer(ctx->peer, NULL);
1075 rcu_assign_pointer(ctx->peer_lastupdate, NULL);
1076 __end_current_label_crit_section(label, needput);
1077 return 0;
1078 }
1079
apparmor_sk_free_security(struct sock * sk)1080 static void apparmor_sk_free_security(struct sock *sk)
1081 {
1082 struct aa_sk_ctx *ctx = aa_sock(sk);
1083
1084 /* dead these won't be updated any more */
1085 aa_put_label(rcu_dereference_protected(ctx->label, true));
1086 aa_put_label(rcu_dereference_protected(ctx->peer, true));
1087 aa_put_label(rcu_dereference_protected(ctx->peer_lastupdate, true));
1088 }
1089
1090 /**
1091 * apparmor_sk_clone_security - clone the sk_security field
1092 * @sk: sock to have security cloned
1093 * @newsk: sock getting clone
1094 */
apparmor_sk_clone_security(const struct sock * sk,struct sock * newsk)1095 static void apparmor_sk_clone_security(const struct sock *sk,
1096 struct sock *newsk)
1097 {
1098 struct aa_sk_ctx *ctx = aa_sock(sk);
1099 struct aa_sk_ctx *new = aa_sock(newsk);
1100
1101 /* not actually in use yet */
1102 if (rcu_access_pointer(ctx->label) != rcu_access_pointer(new->label)) {
1103 aa_put_label(rcu_dereference_protected(new->label, true));
1104 rcu_assign_pointer(new->label, aa_get_label_rcu(&ctx->label));
1105 }
1106
1107 if (rcu_access_pointer(ctx->peer) != rcu_access_pointer(new->peer)) {
1108 aa_put_label(rcu_dereference_protected(new->peer, true));
1109 rcu_assign_pointer(new->peer, aa_get_label_rcu(&ctx->peer));
1110 }
1111
1112 if (rcu_access_pointer(ctx->peer_lastupdate) != rcu_access_pointer(new->peer_lastupdate)) {
1113 aa_put_label(rcu_dereference_protected(new->peer_lastupdate, true));
1114 rcu_assign_pointer(new->peer_lastupdate,
1115 aa_get_label_rcu(&ctx->peer_lastupdate));
1116 }
1117 }
1118
unix_connect_perm(const struct cred * cred,struct aa_label * label,struct sock * sk,struct sock * peer_sk)1119 static int unix_connect_perm(const struct cred *cred, struct aa_label *label,
1120 struct sock *sk, struct sock *peer_sk)
1121 {
1122 struct aa_sk_ctx *peer_ctx = aa_sock(peer_sk);
1123 int error;
1124
1125 error = aa_unix_peer_perm(cred, label, OP_CONNECT,
1126 (AA_MAY_CONNECT | AA_MAY_SEND | AA_MAY_RECEIVE),
1127 sk, peer_sk,
1128 rcu_dereference_protected(peer_ctx->label,
1129 lockdep_is_held(&unix_sk(peer_sk)->lock)));
1130 if (!is_unix_fs(peer_sk)) {
1131 last_error(error,
1132 aa_unix_peer_perm(cred,
1133 rcu_dereference_protected(peer_ctx->label,
1134 lockdep_is_held(&unix_sk(peer_sk)->lock)),
1135 OP_CONNECT,
1136 (AA_MAY_ACCEPT | AA_MAY_SEND | AA_MAY_RECEIVE),
1137 peer_sk, sk, label));
1138 }
1139
1140 return error;
1141 }
1142
1143 /* lockdep check in unix_connect_perm - push sks here to check */
unix_connect_peers(struct aa_sk_ctx * sk_ctx,struct aa_sk_ctx * peer_ctx)1144 static void unix_connect_peers(struct aa_sk_ctx *sk_ctx,
1145 struct aa_sk_ctx *peer_ctx)
1146 {
1147 /* Cross reference the peer labels for SO_PEERSEC */
1148 struct aa_label *label = rcu_dereference_protected(sk_ctx->label, true);
1149
1150 aa_get_label(label);
1151 aa_put_label(rcu_dereference_protected(peer_ctx->peer,
1152 true));
1153 rcu_assign_pointer(peer_ctx->peer, label); /* transfer cnt */
1154
1155 label = aa_get_label(rcu_dereference_protected(peer_ctx->label,
1156 true));
1157 //spin_unlock(&peer_ctx->lock);
1158
1159 //spin_lock(&sk_ctx->lock);
1160 aa_put_label(rcu_dereference_protected(sk_ctx->peer,
1161 true));
1162 aa_put_label(rcu_dereference_protected(sk_ctx->peer_lastupdate,
1163 true));
1164
1165 rcu_assign_pointer(sk_ctx->peer, aa_get_label(label));
1166 rcu_assign_pointer(sk_ctx->peer_lastupdate, label); /* transfer cnt */
1167 //spin_unlock(&sk_ctx->lock);
1168 }
1169
1170 /**
1171 * apparmor_unix_stream_connect - check perms before making unix domain conn
1172 * @sk: sk attempting to connect
1173 * @peer_sk: sk that is accepting the connection
1174 * @newsk: new sk created for this connection
1175 * peer is locked when this hook is called
1176 *
1177 * Return:
1178 * 0 if connection is permitted
1179 * error code on denial or failure
1180 */
apparmor_unix_stream_connect(struct sock * sk,struct sock * peer_sk,struct sock * newsk)1181 static int apparmor_unix_stream_connect(struct sock *sk, struct sock *peer_sk,
1182 struct sock *newsk)
1183 {
1184 struct aa_sk_ctx *sk_ctx = aa_sock(sk);
1185 struct aa_sk_ctx *peer_ctx = aa_sock(peer_sk);
1186 struct aa_sk_ctx *new_ctx = aa_sock(newsk);
1187 struct aa_label *label;
1188 int error;
1189 bool needput;
1190
1191 label = __begin_current_label_crit_section(&needput);
1192 error = unix_connect_perm(current_cred(), label, sk, peer_sk);
1193 __end_current_label_crit_section(label, needput);
1194
1195 if (error)
1196 return error;
1197
1198 /* newsk doesn't go through post_create, but does go through
1199 * security_sk_alloc()
1200 */
1201 rcu_assign_pointer(new_ctx->label,
1202 aa_get_label(rcu_dereference_protected(peer_ctx->label,
1203 true)));
1204
1205 /* Cross reference the peer labels for SO_PEERSEC */
1206 unix_connect_peers(sk_ctx, new_ctx);
1207
1208 return 0;
1209 }
1210
1211 /**
1212 * apparmor_unix_may_send - check perms before conn or sending unix dgrams
1213 * @sock: socket sending the message
1214 * @peer: socket message is being send to
1215 *
1216 * Performs bidirectional permission checks for Unix domain socket communication:
1217 * 1. Verifies sender has AA_MAY_SEND to target socket
1218 * 2. Verifies receiver has AA_MAY_RECEIVE from source socket
1219 *
1220 * sock and peer are locked when this hook is called
1221 * called by: dgram_connect peer setup but path not copied to newsk
1222 *
1223 * Return:
1224 * 0 if transmission is permitted
1225 * error code on denial or failure
1226 */
apparmor_unix_may_send(struct socket * sock,struct socket * peer)1227 static int apparmor_unix_may_send(struct socket *sock, struct socket *peer)
1228 {
1229 struct aa_sk_ctx *peer_ctx = aa_sock(peer->sk);
1230 struct aa_label *label;
1231 int error;
1232 bool needput;
1233
1234 label = __begin_current_label_crit_section(&needput);
1235 error = xcheck(aa_unix_peer_perm(current_cred(),
1236 label, OP_SENDMSG, AA_MAY_SEND,
1237 sock->sk, peer->sk,
1238 rcu_dereference_protected(peer_ctx->label,
1239 true)),
1240 aa_unix_peer_perm(peer->file ? peer->file->f_cred : NULL,
1241 rcu_dereference_protected(peer_ctx->label,
1242 true),
1243 OP_SENDMSG, AA_MAY_RECEIVE, peer->sk,
1244 sock->sk, label));
1245 __end_current_label_crit_section(label, needput);
1246
1247 return error;
1248 }
1249
apparmor_socket_create(int family,int type,int protocol,int kern)1250 static int apparmor_socket_create(int family, int type, int protocol, int kern)
1251 {
1252 struct aa_label *label;
1253 int error = 0;
1254
1255 AA_BUG(in_interrupt());
1256
1257 if (kern)
1258 return 0;
1259
1260 label = begin_current_label_crit_section();
1261 if (!unconfined(label)) {
1262 if (family == PF_UNIX)
1263 error = aa_unix_create_perm(label, family, type,
1264 protocol);
1265 else
1266 error = aa_af_perm(current_cred(), label, OP_CREATE,
1267 AA_MAY_CREATE, family, type,
1268 protocol);
1269 }
1270 end_current_label_crit_section(label);
1271
1272 return error;
1273 }
1274
1275 /**
1276 * apparmor_socket_post_create - setup the per-socket security struct
1277 * @sock: socket that is being setup
1278 * @family: family of socket being created
1279 * @type: type of the socket
1280 * @protocol: protocol of the socket
1281 * @kern: socket is a special kernel socket
1282 *
1283 * Note:
1284 * - kernel sockets labeled kernel_t used to use unconfined
1285 * - socket may not have sk here if created with sock_create_lite or
1286 * sock_alloc. These should be accept cases which will be handled in
1287 * sock_graft.
1288 */
apparmor_socket_post_create(struct socket * sock,int family,int type,int protocol,int kern)1289 static int apparmor_socket_post_create(struct socket *sock, int family,
1290 int type, int protocol, int kern)
1291 {
1292 struct aa_label *label;
1293
1294 if (kern) {
1295 label = aa_get_label(kernel_t);
1296 } else
1297 label = aa_get_current_label();
1298
1299 if (sock->sk) {
1300 struct aa_sk_ctx *ctx = aa_sock(sock->sk);
1301
1302 /* still not live */
1303 aa_put_label(rcu_dereference_protected(ctx->label, true));
1304 rcu_assign_pointer(ctx->label, aa_get_label(label));
1305 }
1306 aa_put_label(label);
1307
1308 return 0;
1309 }
1310
apparmor_socket_socketpair(struct socket * socka,struct socket * sockb)1311 static int apparmor_socket_socketpair(struct socket *socka,
1312 struct socket *sockb)
1313 {
1314 struct aa_sk_ctx *a_ctx = aa_sock(socka->sk);
1315 struct aa_sk_ctx *b_ctx = aa_sock(sockb->sk);
1316 struct aa_label *label;
1317
1318 /* socks not live yet - initial values set in sk_alloc */
1319 label = begin_current_label_crit_section();
1320 if (rcu_access_pointer(a_ctx->label) != label) {
1321 AA_BUG("a_ctx != label");
1322 aa_put_label(rcu_dereference_protected(a_ctx->label, true));
1323 rcu_assign_pointer(a_ctx->label, aa_get_label(label));
1324 }
1325 if (rcu_access_pointer(b_ctx->label) != label) {
1326 AA_BUG("b_ctx != label");
1327 aa_put_label(rcu_dereference_protected(b_ctx->label, true));
1328 rcu_assign_pointer(b_ctx->label, aa_get_label(label));
1329 }
1330
1331 if (socka->sk->sk_family == PF_UNIX) {
1332 /* unix socket pairs by-pass unix_stream_connect */
1333 unix_connect_peers(a_ctx, b_ctx);
1334 }
1335 end_current_label_crit_section(label);
1336
1337 return 0;
1338 }
1339
1340 /**
1341 * apparmor_socket_bind - check perms before bind addr to socket
1342 * @sock: socket to bind the address to (must be non-NULL)
1343 * @address: address that is being bound (must be non-NULL)
1344 * @addrlen: length of @address
1345 *
1346 * Performs security checks before allowing a socket to bind to an address.
1347 * Handles Unix domain sockets specially through aa_unix_bind_perm().
1348 * For other socket families, uses generic permission check via aa_sk_perm().
1349 *
1350 * Return:
1351 * 0 if binding is permitted
1352 * error code on denial or invalid parameters
1353 */
apparmor_socket_bind(struct socket * sock,struct sockaddr * address,int addrlen)1354 static int apparmor_socket_bind(struct socket *sock,
1355 struct sockaddr *address, int addrlen)
1356 {
1357 AA_BUG(!sock);
1358 AA_BUG(!sock->sk);
1359 AA_BUG(!address);
1360 AA_BUG(in_interrupt());
1361
1362 if (sock->sk->sk_family == PF_UNIX)
1363 return aa_unix_bind_perm(sock, address, addrlen);
1364 return aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk);
1365 }
1366
apparmor_socket_connect(struct socket * sock,struct sockaddr * address,int addrlen)1367 static int apparmor_socket_connect(struct socket *sock,
1368 struct sockaddr *address, int addrlen)
1369 {
1370 AA_BUG(!sock);
1371 AA_BUG(!sock->sk);
1372 AA_BUG(!address);
1373 AA_BUG(in_interrupt());
1374
1375 /* PF_UNIX goes through unix_stream_connect && unix_may_send */
1376 if (sock->sk->sk_family == PF_UNIX)
1377 return 0;
1378 return aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk);
1379 }
1380
apparmor_socket_listen(struct socket * sock,int backlog)1381 static int apparmor_socket_listen(struct socket *sock, int backlog)
1382 {
1383 AA_BUG(!sock);
1384 AA_BUG(!sock->sk);
1385 AA_BUG(in_interrupt());
1386
1387 if (sock->sk->sk_family == PF_UNIX)
1388 return aa_unix_listen_perm(sock, backlog);
1389 return aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk);
1390 }
1391
1392 /*
1393 * Note: while @newsock is created and has some information, the accept
1394 * has not been done.
1395 */
apparmor_socket_accept(struct socket * sock,struct socket * newsock)1396 static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
1397 {
1398 AA_BUG(!sock);
1399 AA_BUG(!sock->sk);
1400 AA_BUG(!newsock);
1401 AA_BUG(in_interrupt());
1402
1403 if (sock->sk->sk_family == PF_UNIX)
1404 return aa_unix_accept_perm(sock, newsock);
1405 return aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk);
1406 }
1407
aa_sock_msg_perm(const char * op,u32 request,struct socket * sock,struct msghdr * msg,int size)1408 static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
1409 struct msghdr *msg, int size)
1410 {
1411 AA_BUG(!sock);
1412 AA_BUG(!sock->sk);
1413 AA_BUG(!msg);
1414 AA_BUG(in_interrupt());
1415
1416 /* PF_UNIX goes through unix_may_send */
1417 if (sock->sk->sk_family == PF_UNIX)
1418 return 0;
1419 return aa_sk_perm(op, request, sock->sk);
1420 }
1421
apparmor_socket_sendmsg(struct socket * sock,struct msghdr * msg,int size)1422 static int apparmor_socket_sendmsg(struct socket *sock,
1423 struct msghdr *msg, int size)
1424 {
1425 return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
1426 }
1427
apparmor_socket_recvmsg(struct socket * sock,struct msghdr * msg,int size,int flags)1428 static int apparmor_socket_recvmsg(struct socket *sock,
1429 struct msghdr *msg, int size, int flags)
1430 {
1431 return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
1432 }
1433
1434 /* revaliation, get/set attr, shutdown */
aa_sock_perm(const char * op,u32 request,struct socket * sock)1435 static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
1436 {
1437 AA_BUG(!sock);
1438 AA_BUG(!sock->sk);
1439 AA_BUG(in_interrupt());
1440
1441 if (sock->sk->sk_family == PF_UNIX)
1442 return aa_unix_sock_perm(op, request, sock);
1443 return aa_sk_perm(op, request, sock->sk);
1444 }
1445
apparmor_socket_getsockname(struct socket * sock)1446 static int apparmor_socket_getsockname(struct socket *sock)
1447 {
1448 return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
1449 }
1450
apparmor_socket_getpeername(struct socket * sock)1451 static int apparmor_socket_getpeername(struct socket *sock)
1452 {
1453 return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
1454 }
1455
1456 /* revaliation, get/set attr, opt */
aa_sock_opt_perm(const char * op,u32 request,struct socket * sock,int level,int optname)1457 static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
1458 int level, int optname)
1459 {
1460 AA_BUG(!sock);
1461 AA_BUG(!sock->sk);
1462 AA_BUG(in_interrupt());
1463
1464 if (sock->sk->sk_family == PF_UNIX)
1465 return aa_unix_opt_perm(op, request, sock, level, optname);
1466 return aa_sk_perm(op, request, sock->sk);
1467 }
1468
apparmor_socket_getsockopt(struct socket * sock,int level,int optname)1469 static int apparmor_socket_getsockopt(struct socket *sock, int level,
1470 int optname)
1471 {
1472 return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
1473 level, optname);
1474 }
1475
apparmor_socket_setsockopt(struct socket * sock,int level,int optname)1476 static int apparmor_socket_setsockopt(struct socket *sock, int level,
1477 int optname)
1478 {
1479 return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
1480 level, optname);
1481 }
1482
apparmor_socket_shutdown(struct socket * sock,int how)1483 static int apparmor_socket_shutdown(struct socket *sock, int how)
1484 {
1485 return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
1486 }
1487
1488 #ifdef CONFIG_NETWORK_SECMARK
1489 /**
1490 * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk
1491 * @sk: sk to associate @skb with
1492 * @skb: skb to check for perms
1493 *
1494 * Note: can not sleep may be called with locks held
1495 *
1496 * dont want protocol specific in __skb_recv_datagram()
1497 * to deny an incoming connection socket_sock_rcv_skb()
1498 */
apparmor_socket_sock_rcv_skb(struct sock * sk,struct sk_buff * skb)1499 static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
1500 {
1501 struct aa_sk_ctx *ctx = aa_sock(sk);
1502 int error;
1503
1504 if (!skb->secmark)
1505 return 0;
1506
1507 /*
1508 * If reach here before socket_post_create hook is called, in which
1509 * case label is null, drop the packet.
1510 */
1511 if (!rcu_access_pointer(ctx->label))
1512 return -EACCES;
1513
1514 rcu_read_lock();
1515 error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_RECVMSG,
1516 AA_MAY_RECEIVE, skb->secmark, sk);
1517 rcu_read_unlock();
1518
1519 return error;
1520 }
1521 #endif
1522
1523
sk_peer_get_label(struct sock * sk)1524 static struct aa_label *sk_peer_get_label(struct sock *sk)
1525 {
1526 struct aa_sk_ctx *ctx = aa_sock(sk);
1527
1528 if (rcu_access_pointer(ctx->peer))
1529 return aa_get_label_rcu(&ctx->peer);
1530
1531 return ERR_PTR(-ENOPROTOOPT);
1532 }
1533
1534 /**
1535 * apparmor_socket_getpeersec_stream - get security context of peer
1536 * @sock: socket that we are trying to get the peer context of
1537 * @optval: output - buffer to copy peer name to
1538 * @optlen: output - size of copied name in @optval
1539 * @len: size of @optval buffer
1540 * Returns: 0 on success, -errno of failure
1541 *
1542 * Note: for tcp only valid if using ipsec or cipso on lan
1543 */
apparmor_socket_getpeersec_stream(struct socket * sock,sockptr_t optval,sockptr_t optlen,unsigned int len)1544 static int apparmor_socket_getpeersec_stream(struct socket *sock,
1545 sockptr_t optval, sockptr_t optlen,
1546 unsigned int len)
1547 {
1548 char *name = NULL;
1549 int slen, error = 0;
1550 struct aa_label *label;
1551 struct aa_label *peer;
1552
1553 peer = sk_peer_get_label(sock->sk);
1554 if (IS_ERR(peer)) {
1555 error = PTR_ERR(peer);
1556 goto done;
1557 }
1558 label = begin_current_label_crit_section();
1559 slen = aa_label_asxprint(&name, labels_ns(label), peer,
1560 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
1561 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
1562 /* don't include terminating \0 in slen, it breaks some apps */
1563 if (slen < 0) {
1564 error = -ENOMEM;
1565 goto done_put;
1566 }
1567 if (slen > len) {
1568 error = -ERANGE;
1569 goto done_len;
1570 }
1571
1572 if (copy_to_sockptr(optval, name, slen))
1573 error = -EFAULT;
1574 done_len:
1575 if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
1576 error = -EFAULT;
1577
1578 done_put:
1579 end_current_label_crit_section(label);
1580 aa_put_label(peer);
1581 done:
1582 kfree(name);
1583 return error;
1584 }
1585
1586 /**
1587 * apparmor_socket_getpeersec_dgram - get security label of packet
1588 * @sock: the peer socket
1589 * @skb: packet data
1590 * @secid: pointer to where to put the secid of the packet
1591 *
1592 * Sets the netlabel socket state on sk from parent
1593 */
apparmor_socket_getpeersec_dgram(struct socket * sock,struct sk_buff * skb,u32 * secid)1594 static int apparmor_socket_getpeersec_dgram(struct socket *sock,
1595 struct sk_buff *skb, u32 *secid)
1596
1597 {
1598 /* TODO: requires secid support */
1599 return -ENOPROTOOPT;
1600 }
1601
1602 /**
1603 * apparmor_sock_graft - Initialize newly created socket
1604 * @sk: child sock
1605 * @parent: parent socket
1606 *
1607 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
1608 * just set sk security information off of current creating process label
1609 * Labeling of sk for accept case - probably should be sock based
1610 * instead of task, because of the case where an implicitly labeled
1611 * socket is shared by different tasks.
1612 */
apparmor_sock_graft(struct sock * sk,struct socket * parent)1613 static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
1614 {
1615 struct aa_sk_ctx *ctx = aa_sock(sk);
1616
1617 /* setup - not live */
1618 if (!rcu_access_pointer(ctx->label))
1619 rcu_assign_pointer(ctx->label, aa_get_current_label());
1620 }
1621
1622 #ifdef CONFIG_NETWORK_SECMARK
apparmor_inet_conn_request(const struct sock * sk,struct sk_buff * skb,struct request_sock * req)1623 static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
1624 struct request_sock *req)
1625 {
1626 struct aa_sk_ctx *ctx = aa_sock(sk);
1627 int error;
1628
1629 if (!skb->secmark)
1630 return 0;
1631
1632 rcu_read_lock();
1633 error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_CONNECT,
1634 AA_MAY_CONNECT, skb->secmark, sk);
1635 rcu_read_unlock();
1636
1637 return error;
1638 }
1639 #endif
1640
1641 /*
1642 * The cred blob is a pointer to, not an instance of, an aa_label.
1643 */
1644 struct lsm_blob_sizes apparmor_blob_sizes __ro_after_init = {
1645 .lbs_cred = sizeof(struct aa_label *),
1646 .lbs_file = sizeof(struct aa_file_ctx),
1647 .lbs_task = sizeof(struct aa_task_ctx),
1648 .lbs_sock = sizeof(struct aa_sk_ctx),
1649 };
1650
1651 static const struct lsm_id apparmor_lsmid = {
1652 .name = "apparmor",
1653 .id = LSM_ID_APPARMOR,
1654 };
1655
1656 static struct security_hook_list apparmor_hooks[] __ro_after_init = {
1657 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
1658 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
1659 LSM_HOOK_INIT(capget, apparmor_capget),
1660 LSM_HOOK_INIT(capable, apparmor_capable),
1661
1662 LSM_HOOK_INIT(move_mount, apparmor_move_mount),
1663 LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
1664 LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
1665 LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
1666
1667 LSM_HOOK_INIT(path_link, apparmor_path_link),
1668 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
1669 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
1670 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
1671 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
1672 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
1673 LSM_HOOK_INIT(path_rename, apparmor_path_rename),
1674 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
1675 LSM_HOOK_INIT(path_chown, apparmor_path_chown),
1676 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
1677 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
1678
1679 LSM_HOOK_INIT(file_open, apparmor_file_open),
1680 LSM_HOOK_INIT(file_receive, apparmor_file_receive),
1681 LSM_HOOK_INIT(file_permission, apparmor_file_permission),
1682 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
1683 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
1684 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
1685 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
1686 LSM_HOOK_INIT(file_lock, apparmor_file_lock),
1687 LSM_HOOK_INIT(file_truncate, apparmor_file_truncate),
1688
1689 LSM_HOOK_INIT(getselfattr, apparmor_getselfattr),
1690 LSM_HOOK_INIT(setselfattr, apparmor_setselfattr),
1691 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
1692 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
1693
1694 LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
1695 LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
1696 LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
1697
1698 LSM_HOOK_INIT(unix_stream_connect, apparmor_unix_stream_connect),
1699 LSM_HOOK_INIT(unix_may_send, apparmor_unix_may_send),
1700
1701 LSM_HOOK_INIT(socket_create, apparmor_socket_create),
1702 LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
1703 LSM_HOOK_INIT(socket_socketpair, apparmor_socket_socketpair),
1704 LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
1705 LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
1706 LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
1707 LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
1708 LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
1709 LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
1710 LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
1711 LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
1712 LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
1713 LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
1714 LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
1715 #ifdef CONFIG_NETWORK_SECMARK
1716 LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
1717 #endif
1718 LSM_HOOK_INIT(socket_getpeersec_stream,
1719 apparmor_socket_getpeersec_stream),
1720 LSM_HOOK_INIT(socket_getpeersec_dgram,
1721 apparmor_socket_getpeersec_dgram),
1722 LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
1723 #ifdef CONFIG_NETWORK_SECMARK
1724 LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request),
1725 #endif
1726
1727 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
1728 LSM_HOOK_INIT(cred_free, apparmor_cred_free),
1729 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
1730 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
1731
1732 LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec),
1733 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
1734 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
1735
1736 LSM_HOOK_INIT(task_free, apparmor_task_free),
1737 LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
1738 LSM_HOOK_INIT(current_getlsmprop_subj,
1739 apparmor_current_getlsmprop_subj),
1740 LSM_HOOK_INIT(task_getlsmprop_obj, apparmor_task_getlsmprop_obj),
1741 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
1742 LSM_HOOK_INIT(task_kill, apparmor_task_kill),
1743 LSM_HOOK_INIT(userns_create, apparmor_userns_create),
1744
1745 #ifdef CONFIG_AUDIT
1746 LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
1747 LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
1748 LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
1749 LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
1750 #endif
1751
1752 LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
1753 LSM_HOOK_INIT(lsmprop_to_secctx, apparmor_lsmprop_to_secctx),
1754 LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
1755 LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
1756
1757 #ifdef CONFIG_IO_URING
1758 LSM_HOOK_INIT(uring_override_creds, apparmor_uring_override_creds),
1759 LSM_HOOK_INIT(uring_sqpoll, apparmor_uring_sqpoll),
1760 #endif
1761 };
1762
1763 /*
1764 * AppArmor sysfs module parameters
1765 */
1766
1767 static int param_set_aabool(const char *val, const struct kernel_param *kp);
1768 static int param_get_aabool(char *buffer, const struct kernel_param *kp);
1769 #define param_check_aabool param_check_bool
1770 static const struct kernel_param_ops param_ops_aabool = {
1771 .flags = KERNEL_PARAM_OPS_FL_NOARG,
1772 .set = param_set_aabool,
1773 .get = param_get_aabool
1774 };
1775
1776 static int param_set_aauint(const char *val, const struct kernel_param *kp);
1777 static int param_get_aauint(char *buffer, const struct kernel_param *kp);
1778 #define param_check_aauint param_check_uint
1779 static const struct kernel_param_ops param_ops_aauint = {
1780 .set = param_set_aauint,
1781 .get = param_get_aauint
1782 };
1783
1784 static int param_set_aacompressionlevel(const char *val,
1785 const struct kernel_param *kp);
1786 static int param_get_aacompressionlevel(char *buffer,
1787 const struct kernel_param *kp);
1788 #define param_check_aacompressionlevel param_check_int
1789 static const struct kernel_param_ops param_ops_aacompressionlevel = {
1790 .set = param_set_aacompressionlevel,
1791 .get = param_get_aacompressionlevel
1792 };
1793
1794 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
1795 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
1796 #define param_check_aalockpolicy param_check_bool
1797 static const struct kernel_param_ops param_ops_aalockpolicy = {
1798 .flags = KERNEL_PARAM_OPS_FL_NOARG,
1799 .set = param_set_aalockpolicy,
1800 .get = param_get_aalockpolicy
1801 };
1802
1803 static int param_set_debug(const char *val, const struct kernel_param *kp);
1804 static int param_get_debug(char *buffer, const struct kernel_param *kp);
1805
1806 static int param_set_audit(const char *val, const struct kernel_param *kp);
1807 static int param_get_audit(char *buffer, const struct kernel_param *kp);
1808
1809 static int param_set_mode(const char *val, const struct kernel_param *kp);
1810 static int param_get_mode(char *buffer, const struct kernel_param *kp);
1811
1812 /* Flag values, also controllable via /sys/module/apparmor/parameters
1813 * We define special types as we want to do additional mediation.
1814 */
1815
1816 /* AppArmor global enforcement switch - complain, enforce, kill */
1817 enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
1818 module_param_call(mode, param_set_mode, param_get_mode,
1819 &aa_g_profile_mode, S_IRUSR | S_IWUSR);
1820
1821 /* whether policy verification hashing is enabled */
1822 bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
1823 #ifdef CONFIG_SECURITY_APPARMOR_HASH
1824 module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
1825 #endif
1826
1827 /* whether policy exactly as loaded is retained for debug and checkpointing */
1828 bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY);
1829 #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
1830 module_param_named(export_binary, aa_g_export_binary, aabool, 0600);
1831 #endif
1832
1833 /* policy loaddata compression level */
1834 int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL;
1835 module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level,
1836 aacompressionlevel, 0400);
1837
1838 /* Debug mode */
1839 int aa_g_debug;
1840 module_param_call(debug, param_set_debug, param_get_debug,
1841 &aa_g_debug, 0600);
1842
1843 /* Audit mode */
1844 enum audit_mode aa_g_audit;
1845 module_param_call(audit, param_set_audit, param_get_audit,
1846 &aa_g_audit, S_IRUSR | S_IWUSR);
1847
1848 /* Determines if audit header is included in audited messages. This
1849 * provides more context if the audit daemon is not running
1850 */
1851 bool aa_g_audit_header = true;
1852 module_param_named(audit_header, aa_g_audit_header, aabool,
1853 S_IRUSR | S_IWUSR);
1854
1855 /* lock out loading/removal of policy
1856 * TODO: add in at boot loading of policy, which is the only way to
1857 * load policy, if lock_policy is set
1858 */
1859 bool aa_g_lock_policy;
1860 module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
1861 S_IRUSR | S_IWUSR);
1862
1863 /* Syscall logging mode */
1864 bool aa_g_logsyscall;
1865 module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
1866
1867 /* Maximum pathname length before accesses will start getting rejected */
1868 unsigned int aa_g_path_max = 2 * PATH_MAX;
1869 module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
1870
1871 /* Determines how paranoid loading of policy is and how much verification
1872 * on the loaded policy is done.
1873 * DEPRECATED: read only as strict checking of load is always done now
1874 * that none root users (user namespaces) can load policy.
1875 */
1876 bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD);
1877 module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
1878
1879 static int param_get_aaintbool(char *buffer, const struct kernel_param *kp);
1880 static int param_set_aaintbool(const char *val, const struct kernel_param *kp);
1881 #define param_check_aaintbool param_check_int
1882 static const struct kernel_param_ops param_ops_aaintbool = {
1883 .set = param_set_aaintbool,
1884 .get = param_get_aaintbool
1885 };
1886 /* Boot time disable flag */
1887 static int apparmor_enabled __ro_after_init = 1;
1888 module_param_named(enabled, apparmor_enabled, aaintbool, 0444);
1889
apparmor_enabled_setup(char * str)1890 static int __init apparmor_enabled_setup(char *str)
1891 {
1892 unsigned long enabled;
1893 int error = kstrtoul(str, 0, &enabled);
1894 if (!error)
1895 apparmor_enabled = enabled ? 1 : 0;
1896 return 1;
1897 }
1898
1899 __setup("apparmor=", apparmor_enabled_setup);
1900
1901 /* set global flag turning off the ability to load policy */
param_set_aalockpolicy(const char * val,const struct kernel_param * kp)1902 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
1903 {
1904 if (!apparmor_enabled)
1905 return -EINVAL;
1906 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1907 return -EPERM;
1908 return param_set_bool(val, kp);
1909 }
1910
param_get_aalockpolicy(char * buffer,const struct kernel_param * kp)1911 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
1912 {
1913 if (!apparmor_enabled)
1914 return -EINVAL;
1915 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1916 return -EPERM;
1917 return param_get_bool(buffer, kp);
1918 }
1919
param_set_aabool(const char * val,const struct kernel_param * kp)1920 static int param_set_aabool(const char *val, const struct kernel_param *kp)
1921 {
1922 if (!apparmor_enabled)
1923 return -EINVAL;
1924 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1925 return -EPERM;
1926 return param_set_bool(val, kp);
1927 }
1928
param_get_aabool(char * buffer,const struct kernel_param * kp)1929 static int param_get_aabool(char *buffer, const struct kernel_param *kp)
1930 {
1931 if (!apparmor_enabled)
1932 return -EINVAL;
1933 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1934 return -EPERM;
1935 return param_get_bool(buffer, kp);
1936 }
1937
param_set_aauint(const char * val,const struct kernel_param * kp)1938 static int param_set_aauint(const char *val, const struct kernel_param *kp)
1939 {
1940 int error;
1941
1942 if (!apparmor_enabled)
1943 return -EINVAL;
1944 /* file is ro but enforce 2nd line check */
1945 if (apparmor_initialized)
1946 return -EPERM;
1947
1948 error = param_set_uint(val, kp);
1949 aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer));
1950 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
1951
1952 return error;
1953 }
1954
param_get_aauint(char * buffer,const struct kernel_param * kp)1955 static int param_get_aauint(char *buffer, const struct kernel_param *kp)
1956 {
1957 if (!apparmor_enabled)
1958 return -EINVAL;
1959 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1960 return -EPERM;
1961 return param_get_uint(buffer, kp);
1962 }
1963
1964 /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */
param_set_aaintbool(const char * val,const struct kernel_param * kp)1965 static int param_set_aaintbool(const char *val, const struct kernel_param *kp)
1966 {
1967 struct kernel_param kp_local;
1968 bool value;
1969 int error;
1970
1971 if (apparmor_initialized)
1972 return -EPERM;
1973
1974 /* Create local copy, with arg pointing to bool type. */
1975 value = !!*((int *)kp->arg);
1976 memcpy(&kp_local, kp, sizeof(kp_local));
1977 kp_local.arg = &value;
1978
1979 error = param_set_bool(val, &kp_local);
1980 if (!error)
1981 *((int *)kp->arg) = *((bool *)kp_local.arg);
1982 return error;
1983 }
1984
1985 /*
1986 * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to
1987 * 1/0, this converts the "int that is actually bool" back to bool for
1988 * display in the /sys filesystem, while keeping it "int" for the LSM
1989 * infrastructure.
1990 */
param_get_aaintbool(char * buffer,const struct kernel_param * kp)1991 static int param_get_aaintbool(char *buffer, const struct kernel_param *kp)
1992 {
1993 struct kernel_param kp_local;
1994 bool value;
1995
1996 /* Create local copy, with arg pointing to bool type. */
1997 value = !!*((int *)kp->arg);
1998 memcpy(&kp_local, kp, sizeof(kp_local));
1999 kp_local.arg = &value;
2000
2001 return param_get_bool(buffer, &kp_local);
2002 }
2003
param_set_aacompressionlevel(const char * val,const struct kernel_param * kp)2004 static int param_set_aacompressionlevel(const char *val,
2005 const struct kernel_param *kp)
2006 {
2007 int error;
2008
2009 if (!apparmor_enabled)
2010 return -EINVAL;
2011 if (apparmor_initialized)
2012 return -EPERM;
2013
2014 error = param_set_int(val, kp);
2015
2016 aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level,
2017 AA_MIN_CLEVEL, AA_MAX_CLEVEL);
2018 pr_info("AppArmor: policy rawdata compression level set to %d\n",
2019 aa_g_rawdata_compression_level);
2020
2021 return error;
2022 }
2023
param_get_aacompressionlevel(char * buffer,const struct kernel_param * kp)2024 static int param_get_aacompressionlevel(char *buffer,
2025 const struct kernel_param *kp)
2026 {
2027 if (!apparmor_enabled)
2028 return -EINVAL;
2029 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
2030 return -EPERM;
2031 return param_get_int(buffer, kp);
2032 }
2033
param_get_debug(char * buffer,const struct kernel_param * kp)2034 static int param_get_debug(char *buffer, const struct kernel_param *kp)
2035 {
2036 if (!apparmor_enabled)
2037 return -EINVAL;
2038 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
2039 return -EPERM;
2040 return aa_print_debug_params(buffer);
2041 }
2042
param_set_debug(const char * val,const struct kernel_param * kp)2043 static int param_set_debug(const char *val, const struct kernel_param *kp)
2044 {
2045 int i;
2046
2047 if (!apparmor_enabled)
2048 return -EINVAL;
2049 if (!val)
2050 return -EINVAL;
2051 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
2052 return -EPERM;
2053
2054 i = aa_parse_debug_params(val);
2055 if (i == DEBUG_PARSE_ERROR)
2056 return -EINVAL;
2057
2058 aa_g_debug = i;
2059 return 0;
2060 }
2061
param_get_audit(char * buffer,const struct kernel_param * kp)2062 static int param_get_audit(char *buffer, const struct kernel_param *kp)
2063 {
2064 if (!apparmor_enabled)
2065 return -EINVAL;
2066 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
2067 return -EPERM;
2068 return sysfs_emit(buffer, "%s\n", audit_mode_names[aa_g_audit]);
2069 }
2070
param_set_audit(const char * val,const struct kernel_param * kp)2071 static int param_set_audit(const char *val, const struct kernel_param *kp)
2072 {
2073 int i;
2074
2075 if (!apparmor_enabled)
2076 return -EINVAL;
2077 if (!val)
2078 return -EINVAL;
2079 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
2080 return -EPERM;
2081
2082 i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
2083 if (i < 0)
2084 return -EINVAL;
2085
2086 aa_g_audit = i;
2087 return 0;
2088 }
2089
param_get_mode(char * buffer,const struct kernel_param * kp)2090 static int param_get_mode(char *buffer, const struct kernel_param *kp)
2091 {
2092 if (!apparmor_enabled)
2093 return -EINVAL;
2094 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
2095 return -EPERM;
2096 return sysfs_emit(buffer, "%s\n", aa_profile_mode_names[aa_g_profile_mode]);
2097 }
2098
param_set_mode(const char * val,const struct kernel_param * kp)2099 static int param_set_mode(const char *val, const struct kernel_param *kp)
2100 {
2101 int i;
2102
2103 if (!apparmor_enabled)
2104 return -EINVAL;
2105 if (!val)
2106 return -EINVAL;
2107 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
2108 return -EPERM;
2109
2110 i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
2111 val);
2112 if (i < 0)
2113 return -EINVAL;
2114
2115 aa_g_profile_mode = i;
2116 return 0;
2117 }
2118
2119 /* arbitrary cap on how long to hold buffer because contention was
2120 * encountered before trying to put it back into the global pool
2121 */
2122 #define MAX_HOLD_COUNT 64
2123
2124 /* the hold count is a heuristic for lock contention, and can be
2125 * incremented async to actual buffer alloc/free. Because buffers
2126 * may be put back onto a percpu cache different than the ->hold was
2127 * added to the counts can be out of sync. Guard against underflow
2128 * and overflow
2129 */
cache_hold_inc(unsigned int * hold)2130 static void cache_hold_inc(unsigned int *hold)
2131 {
2132 if (*hold > MAX_HOLD_COUNT)
2133 (*hold)++;
2134 }
2135
aa_get_buffer(bool in_atomic)2136 char *aa_get_buffer(bool in_atomic)
2137 {
2138 union aa_buffer *aa_buf;
2139 struct aa_local_cache *cache;
2140 bool try_again = true;
2141 gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
2142
2143 /* use per cpu cached buffers first */
2144 cache = get_cpu_ptr(&aa_local_buffers);
2145 if (!list_empty(&cache->head)) {
2146 aa_buf = list_first_entry(&cache->head, union aa_buffer, list);
2147 list_del(&aa_buf->list);
2148 if (cache->hold)
2149 cache->hold--;
2150 cache->count--;
2151 put_cpu_ptr(&aa_local_buffers);
2152 return &aa_buf->buffer[0];
2153 }
2154 /* exit percpu as spinlocks may sleep on realtime kernels */
2155 put_cpu_ptr(&aa_local_buffers);
2156
2157 if (!spin_trylock(&aa_buffers_lock)) {
2158 /* had contention on lock so increase hold count. Doesn't
2159 * really matter if recorded before or after the spin lock
2160 * as there is no way to guarantee the buffer will be put
2161 * back on the same percpu cache. Instead rely on holds
2162 * roughly averaging out over time.
2163 */
2164 cache = get_cpu_ptr(&aa_local_buffers);
2165 cache_hold_inc(&cache->hold);
2166 put_cpu_ptr(&aa_local_buffers);
2167 spin_lock(&aa_buffers_lock);
2168 }
2169 retry:
2170 if (buffer_count > reserve_count ||
2171 (in_atomic && !list_empty(&aa_global_buffers))) {
2172 aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
2173 list);
2174 list_del(&aa_buf->list);
2175 buffer_count--;
2176 spin_unlock(&aa_buffers_lock);
2177 return aa_buf->buffer;
2178 }
2179 if (in_atomic) {
2180 /*
2181 * out of reserve buffers and in atomic context so increase
2182 * how many buffers to keep in reserve
2183 */
2184 reserve_count++;
2185 flags = GFP_ATOMIC;
2186 }
2187 spin_unlock(&aa_buffers_lock);
2188
2189 if (!in_atomic)
2190 might_sleep();
2191 aa_buf = kmalloc(aa_g_path_max, flags);
2192 if (!aa_buf) {
2193 if (try_again) {
2194 try_again = false;
2195 spin_lock(&aa_buffers_lock);
2196 goto retry;
2197 }
2198 pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n");
2199 return NULL;
2200 }
2201 return aa_buf->buffer;
2202 }
2203
aa_put_buffer(char * buf)2204 void aa_put_buffer(char *buf)
2205 {
2206 union aa_buffer *aa_buf;
2207 struct aa_local_cache *cache;
2208
2209 if (!buf)
2210 return;
2211 aa_buf = container_of(buf, union aa_buffer, buffer[0]);
2212
2213 cache = get_cpu_ptr(&aa_local_buffers);
2214 if (!cache->hold) {
2215 put_cpu_ptr(&aa_local_buffers);
2216
2217 if (spin_trylock(&aa_buffers_lock)) {
2218 /* put back on global list */
2219 list_add(&aa_buf->list, &aa_global_buffers);
2220 buffer_count++;
2221 spin_unlock(&aa_buffers_lock);
2222 return;
2223 }
2224 /* contention on global list, fallback to percpu */
2225 cache = get_cpu_ptr(&aa_local_buffers);
2226 cache_hold_inc(&cache->hold);
2227 }
2228
2229 /* cache in percpu list */
2230 list_add(&aa_buf->list, &cache->head);
2231 cache->count++;
2232 put_cpu_ptr(&aa_local_buffers);
2233 }
2234
2235 /*
2236 * AppArmor init functions
2237 */
2238
2239 /**
2240 * set_init_ctx - set a task context and profile on the first task.
2241 *
2242 * TODO: allow setting an alternate profile than unconfined
2243 */
set_init_ctx(void)2244 static int __init set_init_ctx(void)
2245 {
2246 struct cred *cred = (__force struct cred *)current->real_cred;
2247
2248 set_cred_label(cred, aa_get_label(ns_unconfined(root_ns)));
2249
2250 return 0;
2251 }
2252
destroy_buffers(void)2253 static void destroy_buffers(void)
2254 {
2255 union aa_buffer *aa_buf;
2256
2257 spin_lock(&aa_buffers_lock);
2258 while (!list_empty(&aa_global_buffers)) {
2259 aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
2260 list);
2261 list_del(&aa_buf->list);
2262 spin_unlock(&aa_buffers_lock);
2263 kfree(aa_buf);
2264 spin_lock(&aa_buffers_lock);
2265 }
2266 spin_unlock(&aa_buffers_lock);
2267 }
2268
alloc_buffers(void)2269 static int __init alloc_buffers(void)
2270 {
2271 union aa_buffer *aa_buf;
2272 int i, num;
2273
2274 /*
2275 * per cpu set of cached allocated buffers used to help reduce
2276 * lock contention
2277 */
2278 for_each_possible_cpu(i) {
2279 per_cpu(aa_local_buffers, i).hold = 0;
2280 per_cpu(aa_local_buffers, i).count = 0;
2281 INIT_LIST_HEAD(&per_cpu(aa_local_buffers, i).head);
2282 }
2283 /*
2284 * A function may require two buffers at once. Usually the buffers are
2285 * used for a short period of time and are shared. On UP kernel buffers
2286 * two should be enough, with more CPUs it is possible that more
2287 * buffers will be used simultaneously. The preallocated pool may grow.
2288 * This preallocation has also the side-effect that AppArmor will be
2289 * disabled early at boot if aa_g_path_max is extremely high.
2290 */
2291 if (num_online_cpus() > 1)
2292 num = 4 + RESERVE_COUNT;
2293 else
2294 num = 2 + RESERVE_COUNT;
2295
2296 for (i = 0; i < num; i++) {
2297
2298 aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL |
2299 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
2300 if (!aa_buf) {
2301 destroy_buffers();
2302 return -ENOMEM;
2303 }
2304 aa_put_buffer(aa_buf->buffer);
2305 }
2306 return 0;
2307 }
2308
2309 #ifdef CONFIG_SYSCTL
apparmor_dointvec(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)2310 static int apparmor_dointvec(const struct ctl_table *table, int write,
2311 void *buffer, size_t *lenp, loff_t *ppos)
2312 {
2313 if (!aa_current_policy_admin_capable(NULL))
2314 return -EPERM;
2315 if (!apparmor_enabled)
2316 return -EINVAL;
2317
2318 return proc_dointvec(table, write, buffer, lenp, ppos);
2319 }
2320
2321 static const struct ctl_table apparmor_sysctl_table[] = {
2322 #ifdef CONFIG_USER_NS
2323 {
2324 .procname = "unprivileged_userns_apparmor_policy",
2325 .data = &unprivileged_userns_apparmor_policy,
2326 .maxlen = sizeof(int),
2327 .mode = 0600,
2328 .proc_handler = apparmor_dointvec,
2329 },
2330 #endif /* CONFIG_USER_NS */
2331 {
2332 .procname = "apparmor_display_secid_mode",
2333 .data = &apparmor_display_secid_mode,
2334 .maxlen = sizeof(int),
2335 .mode = 0600,
2336 .proc_handler = apparmor_dointvec,
2337 },
2338 {
2339 .procname = "apparmor_restrict_unprivileged_unconfined",
2340 .data = &aa_unprivileged_unconfined_restricted,
2341 .maxlen = sizeof(int),
2342 .mode = 0600,
2343 .proc_handler = apparmor_dointvec,
2344 },
2345 };
2346
apparmor_init_sysctl(void)2347 static int __init apparmor_init_sysctl(void)
2348 {
2349 return register_sysctl("kernel", apparmor_sysctl_table) ? 0 : -ENOMEM;
2350 }
2351 #else
apparmor_init_sysctl(void)2352 static inline int apparmor_init_sysctl(void)
2353 {
2354 return 0;
2355 }
2356 #endif /* CONFIG_SYSCTL */
2357
2358 #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
apparmor_ip_postroute(void * priv,struct sk_buff * skb,const struct nf_hook_state * state)2359 static unsigned int apparmor_ip_postroute(void *priv,
2360 struct sk_buff *skb,
2361 const struct nf_hook_state *state)
2362 {
2363 struct aa_sk_ctx *ctx;
2364 struct sock *sk;
2365 int error;
2366
2367 if (!skb->secmark)
2368 return NF_ACCEPT;
2369
2370 sk = skb_to_full_sk(skb);
2371 if (sk == NULL)
2372 return NF_ACCEPT;
2373
2374 ctx = aa_sock(sk);
2375 rcu_read_lock();
2376 error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_SENDMSG,
2377 AA_MAY_SEND, skb->secmark, sk);
2378 rcu_read_unlock();
2379 if (!error)
2380 return NF_ACCEPT;
2381
2382 return NF_DROP_ERR(-ECONNREFUSED);
2383
2384 }
2385
2386 static const struct nf_hook_ops apparmor_nf_ops[] = {
2387 {
2388 .hook = apparmor_ip_postroute,
2389 .pf = NFPROTO_IPV4,
2390 .hooknum = NF_INET_POST_ROUTING,
2391 .priority = NF_IP_PRI_SELINUX_FIRST,
2392 },
2393 #if IS_ENABLED(CONFIG_IPV6)
2394 {
2395 .hook = apparmor_ip_postroute,
2396 .pf = NFPROTO_IPV6,
2397 .hooknum = NF_INET_POST_ROUTING,
2398 .priority = NF_IP6_PRI_SELINUX_FIRST,
2399 },
2400 #endif
2401 };
2402
apparmor_nf_register(struct net * net)2403 static int __net_init apparmor_nf_register(struct net *net)
2404 {
2405 return nf_register_net_hooks(net, apparmor_nf_ops,
2406 ARRAY_SIZE(apparmor_nf_ops));
2407 }
2408
apparmor_nf_unregister(struct net * net)2409 static void __net_exit apparmor_nf_unregister(struct net *net)
2410 {
2411 nf_unregister_net_hooks(net, apparmor_nf_ops,
2412 ARRAY_SIZE(apparmor_nf_ops));
2413 }
2414
2415 static struct pernet_operations apparmor_net_ops = {
2416 .init = apparmor_nf_register,
2417 .exit = apparmor_nf_unregister,
2418 };
2419
apparmor_nf_ip_init(void)2420 static int __init apparmor_nf_ip_init(void)
2421 {
2422 int err;
2423
2424 if (!apparmor_enabled)
2425 return 0;
2426
2427 err = register_pernet_subsys(&apparmor_net_ops);
2428 if (err)
2429 panic("Apparmor: register_pernet_subsys: error %d\n", err);
2430
2431 return 0;
2432 }
2433 #endif
2434
2435 static char nulldfa_src[] __aligned(8) = {
2436 #include "nulldfa.in"
2437 };
2438 static struct aa_dfa *nulldfa;
2439
2440 static char stacksplitdfa_src[] __aligned(8) = {
2441 #include "stacksplitdfa.in"
2442 };
2443 struct aa_dfa *stacksplitdfa;
2444 struct aa_policydb *nullpdb;
2445
aa_setup_dfa_engine(void)2446 static int __init aa_setup_dfa_engine(void)
2447 {
2448 int error = -ENOMEM;
2449
2450 nullpdb = aa_alloc_pdb(GFP_KERNEL);
2451 if (!nullpdb)
2452 return -ENOMEM;
2453
2454 nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src),
2455 TO_ACCEPT1_FLAG(YYTD_DATA32) |
2456 TO_ACCEPT2_FLAG(YYTD_DATA32));
2457 if (IS_ERR(nulldfa)) {
2458 error = PTR_ERR(nulldfa);
2459 nulldfa = NULL;
2460 goto fail;
2461 }
2462 nullpdb->dfa = aa_get_dfa(nulldfa);
2463 nullpdb->perms = kzalloc_objs(struct aa_perms, 2);
2464 if (!nullpdb->perms)
2465 goto fail;
2466 nullpdb->size = 2;
2467
2468 stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src,
2469 sizeof(stacksplitdfa_src),
2470 TO_ACCEPT1_FLAG(YYTD_DATA32) |
2471 TO_ACCEPT2_FLAG(YYTD_DATA32));
2472 if (IS_ERR(stacksplitdfa)) {
2473 error = PTR_ERR(stacksplitdfa);
2474 goto fail;
2475 }
2476
2477 return 0;
2478
2479 fail:
2480 aa_put_pdb(nullpdb);
2481 aa_put_dfa(nulldfa);
2482 nullpdb = NULL;
2483 nulldfa = NULL;
2484 stacksplitdfa = NULL;
2485
2486 return error;
2487 }
2488
aa_teardown_dfa_engine(void)2489 static void __init aa_teardown_dfa_engine(void)
2490 {
2491 aa_put_dfa(stacksplitdfa);
2492 aa_put_dfa(nulldfa);
2493 aa_put_pdb(nullpdb);
2494 nullpdb = NULL;
2495 stacksplitdfa = NULL;
2496 nulldfa = NULL;
2497 }
2498
apparmor_init(void)2499 static int __init apparmor_init(void)
2500 {
2501 int error;
2502
2503 error = aa_setup_dfa_engine();
2504 if (error) {
2505 AA_ERROR("Unable to setup dfa engine\n");
2506 goto alloc_out;
2507 }
2508
2509 error = aa_alloc_root_ns();
2510 if (error) {
2511 AA_ERROR("Unable to allocate default profile namespace\n");
2512 goto alloc_out;
2513 }
2514
2515 error = apparmor_init_sysctl();
2516 if (error) {
2517 AA_ERROR("Unable to register sysctls\n");
2518 goto alloc_out;
2519
2520 }
2521
2522 error = alloc_buffers();
2523 if (error) {
2524 AA_ERROR("Unable to allocate work buffers\n");
2525 goto alloc_out;
2526 }
2527
2528 error = set_init_ctx();
2529 if (error) {
2530 AA_ERROR("Failed to set context on init task\n");
2531 aa_free_root_ns();
2532 goto buffers_out;
2533 }
2534 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
2535 &apparmor_lsmid);
2536
2537 /* Inform the audit system that secctx is used */
2538 audit_cfg_lsm(&apparmor_lsmid, AUDIT_CFG_LSM_SECCTX_SUBJECT);
2539
2540 /* Report that AppArmor successfully initialized */
2541 apparmor_initialized = 1;
2542 if (aa_g_profile_mode == APPARMOR_COMPLAIN)
2543 aa_info_message("AppArmor initialized: complain mode enabled");
2544 else if (aa_g_profile_mode == APPARMOR_KILL)
2545 aa_info_message("AppArmor initialized: kill mode enabled");
2546 else
2547 aa_info_message("AppArmor initialized");
2548
2549 return error;
2550
2551 buffers_out:
2552 destroy_buffers();
2553 alloc_out:
2554 aa_destroy_aafs();
2555 aa_teardown_dfa_engine();
2556
2557 apparmor_enabled = false;
2558 return error;
2559 }
2560
2561 DEFINE_LSM(apparmor) = {
2562 .id = &apparmor_lsmid,
2563 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
2564 .enabled = &apparmor_enabled,
2565 .blobs = &apparmor_blob_sizes,
2566 .init = apparmor_init,
2567 .initcall_fs = aa_create_aafs,
2568 #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
2569 .initcall_device = apparmor_nf_ip_init,
2570 #endif
2571 #ifdef CONFIG_SECURITY_APPARMOR_HASH
2572 .initcall_late = init_profile_hash,
2573 #endif
2574 };
2575