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