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
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/vmalloc.h>
20 #include "include/audit.h"
21 #include "include/apparmor.h"
25 * aa_split_fqname - split a fqname into a profile and namespace name
26 * @fqname: a full qualified name in namespace profile format (NOT NULL)
27 * @ns_name: pointer to portion of the string containing the ns name (NOT NULL)
29 * Returns: profile name or NULL if one is not specified
31 * Split a namespace name from a profile name (see policy.c for naming
32 * description). If a portion of the name is missing it returns NULL for
35 * NOTE: may modify the @fqname string. The pointers returned point
36 * into the @fqname string.
38 char *aa_split_fqname(char *fqname
, char **ns_name
)
40 char *name
= strim(fqname
);
44 char *split
= strchr(&name
[1], ':');
45 *ns_name
= skip_spaces(&name
[1]);
47 /* overwrite ':' with \0 */
49 name
= skip_spaces(split
+ 1);
51 /* a ns name without a following profile is allowed */
54 if (name
&& *name
== 0)
61 * aa_info_message - log a none profile related status message
62 * @str: message to log
64 void aa_info_message(const char *str
)
67 struct common_audit_data sa
;
68 struct apparmor_audit_data aad
= {0,};
69 sa
.type
= LSM_AUDIT_DATA_NONE
;
72 aa_audit_msg(AUDIT_APPARMOR_STATUS
, &sa
, NULL
);
74 printk(KERN_INFO
"AppArmor: %s\n", str
);
78 * kvmalloc - do allocation preferring kmalloc but falling back to vmalloc
79 * @size: size of allocation
81 * Return: allocated buffer or NULL if failed
83 * It is possible that policy being loaded from the user is larger than
84 * what can be allocated by kmalloc, in those cases fall back to vmalloc.
86 void *kvmalloc(size_t size
)
93 /* do not attempt kmalloc if we need more than 16 pages at once */
94 if (size
<= (16*PAGE_SIZE
))
95 buffer
= kmalloc(size
, GFP_NOIO
| __GFP_NOWARN
);
97 /* see kvfree for why size must be at least work_struct size
98 * when allocated via vmalloc
100 if (size
< sizeof(struct work_struct
))
101 size
= sizeof(struct work_struct
);
102 buffer
= vmalloc(size
);
108 * do_vfree - workqueue routine for freeing vmalloced memory
109 * @work: data to be freed
111 * The work_struct is overlaid to the data being freed, as at the point
112 * the work is scheduled the data is no longer valid, be its freeing
113 * needs to be delayed until safe.
115 static void do_vfree(struct work_struct
*work
)
121 * kvfree - free an allocation do by kvmalloc
122 * @buffer: buffer to free (MAYBE_NULL)
124 * Free a buffer allocated by kvmalloc
126 void kvfree(void *buffer
)
128 if (is_vmalloc_addr(buffer
)) {
129 /* Data is no longer valid so just use the allocated space
132 struct work_struct
*work
= (struct work_struct
*) buffer
;
133 INIT_WORK(work
, do_vfree
);