xref: /openbmc/linux/security/apparmor/lib.c (revision 0ca554b9)
1cdff2642SJohn Johansen /*
2cdff2642SJohn Johansen  * AppArmor security module
3cdff2642SJohn Johansen  *
4cdff2642SJohn Johansen  * This file contains basic common functions used in AppArmor
5cdff2642SJohn Johansen  *
6cdff2642SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
7cdff2642SJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
8cdff2642SJohn Johansen  *
9cdff2642SJohn Johansen  * This program is free software; you can redistribute it and/or
10cdff2642SJohn Johansen  * modify it under the terms of the GNU General Public License as
11cdff2642SJohn Johansen  * published by the Free Software Foundation, version 2 of the
12cdff2642SJohn Johansen  * License.
13cdff2642SJohn Johansen  */
14cdff2642SJohn Johansen 
15b7f080cfSAlexey Dobriyan #include <linux/mm.h>
16cdff2642SJohn Johansen #include <linux/slab.h>
17cdff2642SJohn Johansen #include <linux/string.h>
18cdff2642SJohn Johansen #include <linux/vmalloc.h>
19cdff2642SJohn Johansen 
20cdff2642SJohn Johansen #include "include/audit.h"
2132c3df63SJames Morris #include "include/apparmor.h"
22cdff2642SJohn Johansen 
23cdff2642SJohn Johansen 
24cdff2642SJohn Johansen /**
25cdff2642SJohn Johansen  * aa_split_fqname - split a fqname into a profile and namespace name
26cdff2642SJohn Johansen  * @fqname: a full qualified name in namespace profile format (NOT NULL)
27cdff2642SJohn Johansen  * @ns_name: pointer to portion of the string containing the ns name (NOT NULL)
28cdff2642SJohn Johansen  *
29cdff2642SJohn Johansen  * Returns: profile name or NULL if one is not specified
30cdff2642SJohn Johansen  *
31cdff2642SJohn Johansen  * Split a namespace name from a profile name (see policy.c for naming
32cdff2642SJohn Johansen  * description).  If a portion of the name is missing it returns NULL for
33cdff2642SJohn Johansen  * that portion.
34cdff2642SJohn Johansen  *
35cdff2642SJohn Johansen  * NOTE: may modify the @fqname string.  The pointers returned point
36cdff2642SJohn Johansen  *       into the @fqname string.
37cdff2642SJohn Johansen  */
38cdff2642SJohn Johansen char *aa_split_fqname(char *fqname, char **ns_name)
39cdff2642SJohn Johansen {
40cdff2642SJohn Johansen 	char *name = strim(fqname);
41cdff2642SJohn Johansen 
42cdff2642SJohn Johansen 	*ns_name = NULL;
43cdff2642SJohn Johansen 	if (name[0] == ':') {
44cdff2642SJohn Johansen 		char *split = strchr(&name[1], ':');
4504ccd53fSJohn Johansen 		*ns_name = skip_spaces(&name[1]);
46cdff2642SJohn Johansen 		if (split) {
47cdff2642SJohn Johansen 			/* overwrite ':' with \0 */
48cdff2642SJohn Johansen 			*split = 0;
49cdff2642SJohn Johansen 			name = skip_spaces(split + 1);
50cdff2642SJohn Johansen 		} else
51cdff2642SJohn Johansen 			/* a ns name without a following profile is allowed */
52cdff2642SJohn Johansen 			name = NULL;
53cdff2642SJohn Johansen 	}
54cdff2642SJohn Johansen 	if (name && *name == 0)
55cdff2642SJohn Johansen 		name = NULL;
56cdff2642SJohn Johansen 
57cdff2642SJohn Johansen 	return name;
58cdff2642SJohn Johansen }
59cdff2642SJohn Johansen 
60cdff2642SJohn Johansen /**
61cdff2642SJohn Johansen  * aa_info_message - log a none profile related status message
62cdff2642SJohn Johansen  * @str: message to log
63cdff2642SJohn Johansen  */
64cdff2642SJohn Johansen void aa_info_message(const char *str)
65cdff2642SJohn Johansen {
66cdff2642SJohn Johansen 	if (audit_enabled) {
67cdff2642SJohn Johansen 		struct common_audit_data sa;
683b3b0e4fSEric Paris 		struct apparmor_audit_data aad = {0,};
6950c205f5SEric Paris 		sa.type = LSM_AUDIT_DATA_NONE;
703b3b0e4fSEric Paris 		sa.aad = &aad;
713b3b0e4fSEric Paris 		aad.info = str;
72cdff2642SJohn Johansen 		aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
73cdff2642SJohn Johansen 	}
74cdff2642SJohn Johansen 	printk(KERN_INFO "AppArmor: %s\n", str);
75cdff2642SJohn Johansen }
76cdff2642SJohn Johansen 
77cdff2642SJohn Johansen /**
780ca554b9SJohn Johansen  * __aa_kvmalloc - do allocation preferring kmalloc but falling back to vmalloc
790ca554b9SJohn Johansen  * @size: how many bytes of memory are required
800ca554b9SJohn Johansen  * @flags: the type of memory to allocate (see kmalloc).
81cdff2642SJohn Johansen  *
82cdff2642SJohn Johansen  * Return: allocated buffer or NULL if failed
83cdff2642SJohn Johansen  *
84cdff2642SJohn Johansen  * It is possible that policy being loaded from the user is larger than
85cdff2642SJohn Johansen  * what can be allocated by kmalloc, in those cases fall back to vmalloc.
86cdff2642SJohn Johansen  */
870ca554b9SJohn Johansen void *__aa_kvmalloc(size_t size, gfp_t flags)
88cdff2642SJohn Johansen {
89cdff2642SJohn Johansen 	void *buffer = NULL;
90cdff2642SJohn Johansen 
91cdff2642SJohn Johansen 	if (size == 0)
92cdff2642SJohn Johansen 		return NULL;
93cdff2642SJohn Johansen 
94cdff2642SJohn Johansen 	/* do not attempt kmalloc if we need more than 16 pages at once */
95cdff2642SJohn Johansen 	if (size <= (16*PAGE_SIZE))
960ca554b9SJohn Johansen 		buffer = kmalloc(size, flags | GFP_NOIO | __GFP_NOWARN);
97cdff2642SJohn Johansen 	if (!buffer) {
98cdff2642SJohn Johansen 		/* see kvfree for why size must be at least work_struct size
99cdff2642SJohn Johansen 		 * when allocated via vmalloc
100cdff2642SJohn Johansen 		 */
101cdff2642SJohn Johansen 		if (size < sizeof(struct work_struct))
102cdff2642SJohn Johansen 			size = sizeof(struct work_struct);
1030ca554b9SJohn Johansen 		if (flags & __GFP_ZERO)
1040ca554b9SJohn Johansen 			buffer = vzalloc(size);
1050ca554b9SJohn Johansen 		else
106cdff2642SJohn Johansen 			buffer = vmalloc(size);
107cdff2642SJohn Johansen 	}
108cdff2642SJohn Johansen 	return buffer;
109cdff2642SJohn Johansen }
110cdff2642SJohn Johansen 
111cdff2642SJohn Johansen /**
112cdff2642SJohn Johansen  * do_vfree - workqueue routine for freeing vmalloced memory
113cdff2642SJohn Johansen  * @work: data to be freed
114cdff2642SJohn Johansen  *
115cdff2642SJohn Johansen  * The work_struct is overlaid to the data being freed, as at the point
116cdff2642SJohn Johansen  * the work is scheduled the data is no longer valid, be its freeing
117cdff2642SJohn Johansen  * needs to be delayed until safe.
118cdff2642SJohn Johansen  */
119cdff2642SJohn Johansen static void do_vfree(struct work_struct *work)
120cdff2642SJohn Johansen {
121cdff2642SJohn Johansen 	vfree(work);
122cdff2642SJohn Johansen }
123cdff2642SJohn Johansen 
124cdff2642SJohn Johansen /**
125cdff2642SJohn Johansen  * kvfree - free an allocation do by kvmalloc
126cdff2642SJohn Johansen  * @buffer: buffer to free (MAYBE_NULL)
127cdff2642SJohn Johansen  *
128cdff2642SJohn Johansen  * Free a buffer allocated by kvmalloc
129cdff2642SJohn Johansen  */
130cdff2642SJohn Johansen void kvfree(void *buffer)
131cdff2642SJohn Johansen {
132cdff2642SJohn Johansen 	if (is_vmalloc_addr(buffer)) {
133cdff2642SJohn Johansen 		/* Data is no longer valid so just use the allocated space
134cdff2642SJohn Johansen 		 * as the work_struct
135cdff2642SJohn Johansen 		 */
136cdff2642SJohn Johansen 		struct work_struct *work = (struct work_struct *) buffer;
137cdff2642SJohn Johansen 		INIT_WORK(work, do_vfree);
138cdff2642SJohn Johansen 		schedule_work(work);
139cdff2642SJohn Johansen 	} else
140cdff2642SJohn Johansen 		kfree(buffer);
141cdff2642SJohn Johansen }
142