Linux Audio

Check our new training course

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