regulator: Fix warning with CONFIG_BUG disabled
[zen-stable.git] / security / apparmor / lib.c
blob506d2baf614797624fc4b9450c0d12c9f56e8ae4
1 /*
2 * AppArmor security module
4 * This file contains basic common functions used in AppArmor
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
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.
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/vmalloc.h>
19 #include "include/audit.h"
22 /**
23 * aa_split_fqname - split a fqname into a profile and namespace name
24 * @fqname: a full qualified name in namespace profile format (NOT NULL)
25 * @ns_name: pointer to portion of the string containing the ns name (NOT NULL)
27 * Returns: profile name or NULL if one is not specified
29 * Split a namespace name from a profile name (see policy.c for naming
30 * description). If a portion of the name is missing it returns NULL for
31 * that portion.
33 * NOTE: may modify the @fqname string. The pointers returned point
34 * into the @fqname string.
36 char *aa_split_fqname(char *fqname, char **ns_name)
38 char *name = strim(fqname);
40 *ns_name = NULL;
41 if (name[0] == ':') {
42 char *split = strchr(&name[1], ':');
43 *ns_name = skip_spaces(&name[1]);
44 if (split) {
45 /* overwrite ':' with \0 */
46 *split = 0;
47 name = skip_spaces(split + 1);
48 } else
49 /* a ns name without a following profile is allowed */
50 name = NULL;
52 if (name && *name == 0)
53 name = NULL;
55 return name;
58 /**
59 * aa_info_message - log a none profile related status message
60 * @str: message to log
62 void aa_info_message(const char *str)
64 if (audit_enabled) {
65 struct common_audit_data sa;
66 COMMON_AUDIT_DATA_INIT(&sa, NONE);
67 sa.aad.info = str;
68 aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
70 printk(KERN_INFO "AppArmor: %s\n", str);
73 /**
74 * kvmalloc - do allocation preferring kmalloc but falling back to vmalloc
75 * @size: size of allocation
77 * Return: allocated buffer or NULL if failed
79 * It is possible that policy being loaded from the user is larger than
80 * what can be allocated by kmalloc, in those cases fall back to vmalloc.
82 void *kvmalloc(size_t size)
84 void *buffer = NULL;
86 if (size == 0)
87 return NULL;
89 /* do not attempt kmalloc if we need more than 16 pages at once */
90 if (size <= (16*PAGE_SIZE))
91 buffer = kmalloc(size, GFP_NOIO | __GFP_NOWARN);
92 if (!buffer) {
93 /* see kvfree for why size must be at least work_struct size
94 * when allocated via vmalloc
96 if (size < sizeof(struct work_struct))
97 size = sizeof(struct work_struct);
98 buffer = vmalloc(size);
100 return buffer;
104 * do_vfree - workqueue routine for freeing vmalloced memory
105 * @work: data to be freed
107 * The work_struct is overlaid to the data being freed, as at the point
108 * the work is scheduled the data is no longer valid, be its freeing
109 * needs to be delayed until safe.
111 static void do_vfree(struct work_struct *work)
113 vfree(work);
117 * kvfree - free an allocation do by kvmalloc
118 * @buffer: buffer to free (MAYBE_NULL)
120 * Free a buffer allocated by kvmalloc
122 void kvfree(void *buffer)
124 if (is_vmalloc_addr(buffer)) {
125 /* Data is no longer valid so just use the allocated space
126 * as the work_struct
128 struct work_struct *work = (struct work_struct *) buffer;
129 INIT_WORK(work, do_vfree);
130 schedule_work(work);
131 } else
132 kfree(buffer);