blob: d6e1f214839851a282218f71cc745886e9f1ab77 [file] [log] [blame]
John Johansencdff2642010-07-29 14:47:57 -07001/*
2 * AppArmor security module
3 *
4 * This file contains basic common functions used in AppArmor
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
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000015#include <linux/mm.h>
John Johansencdff2642010-07-29 14:47:57 -070016#include <linux/slab.h>
17#include <linux/string.h>
18#include <linux/vmalloc.h>
19
20#include "include/audit.h"
James Morris32c3df62011-08-29 11:15:25 +100021#include "include/apparmor.h"
John Johansencdff2642010-07-29 14:47:57 -070022
23
24/**
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)
28 *
29 * Returns: profile name or NULL if one is not specified
30 *
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
33 * that portion.
34 *
35 * NOTE: may modify the @fqname string. The pointers returned point
36 * into the @fqname string.
37 */
38char *aa_split_fqname(char *fqname, char **ns_name)
39{
40 char *name = strim(fqname);
41
42 *ns_name = NULL;
43 if (name[0] == ':') {
44 char *split = strchr(&name[1], ':');
John Johansen04ccd532010-08-27 18:33:28 -070045 *ns_name = skip_spaces(&name[1]);
John Johansencdff2642010-07-29 14:47:57 -070046 if (split) {
47 /* overwrite ':' with \0 */
48 *split = 0;
49 name = skip_spaces(split + 1);
50 } else
51 /* a ns name without a following profile is allowed */
52 name = NULL;
John Johansencdff2642010-07-29 14:47:57 -070053 }
54 if (name && *name == 0)
55 name = NULL;
56
57 return name;
58}
59
60/**
61 * aa_info_message - log a none profile related status message
62 * @str: message to log
63 */
64void aa_info_message(const char *str)
65{
66 if (audit_enabled) {
67 struct common_audit_data sa;
Eric Paris3b3b0e42012-04-03 09:37:02 -070068 struct apparmor_audit_data aad = {0,};
Eric Paris50c205f2012-04-04 15:01:43 -040069 sa.type = LSM_AUDIT_DATA_NONE;
Eric Paris3b3b0e42012-04-03 09:37:02 -070070 sa.aad = &aad;
71 aad.info = str;
John Johansencdff2642010-07-29 14:47:57 -070072 aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
73 }
74 printk(KERN_INFO "AppArmor: %s\n", str);
75}
76
77/**
John Johansen0ca554b2013-02-18 16:04:34 -080078 * __aa_kvmalloc - do allocation preferring kmalloc but falling back to vmalloc
79 * @size: how many bytes of memory are required
80 * @flags: the type of memory to allocate (see kmalloc).
John Johansencdff2642010-07-29 14:47:57 -070081 *
82 * Return: allocated buffer or NULL if failed
83 *
84 * It is possible that policy being loaded from the user is larger than
85 * what can be allocated by kmalloc, in those cases fall back to vmalloc.
86 */
John Johansen0ca554b2013-02-18 16:04:34 -080087void *__aa_kvmalloc(size_t size, gfp_t flags)
John Johansencdff2642010-07-29 14:47:57 -070088{
89 void *buffer = NULL;
90
91 if (size == 0)
92 return NULL;
93
94 /* do not attempt kmalloc if we need more than 16 pages at once */
95 if (size <= (16*PAGE_SIZE))
John Johansen0ca554b2013-02-18 16:04:34 -080096 buffer = kmalloc(size, flags | GFP_NOIO | __GFP_NOWARN);
John Johansencdff2642010-07-29 14:47:57 -070097 if (!buffer) {
98 /* see kvfree for why size must be at least work_struct size
99 * when allocated via vmalloc
100 */
101 if (size < sizeof(struct work_struct))
102 size = sizeof(struct work_struct);
John Johansen0ca554b2013-02-18 16:04:34 -0800103 if (flags & __GFP_ZERO)
104 buffer = vzalloc(size);
105 else
106 buffer = vmalloc(size);
John Johansencdff2642010-07-29 14:47:57 -0700107 }
108 return buffer;
109}
110
111/**
112 * do_vfree - workqueue routine for freeing vmalloced memory
113 * @work: data to be freed
114 *
115 * The work_struct is overlaid to the data being freed, as at the point
116 * the work is scheduled the data is no longer valid, be its freeing
117 * needs to be delayed until safe.
118 */
119static void do_vfree(struct work_struct *work)
120{
121 vfree(work);
122}
123
124/**
125 * kvfree - free an allocation do by kvmalloc
126 * @buffer: buffer to free (MAYBE_NULL)
127 *
128 * Free a buffer allocated by kvmalloc
129 */
130void kvfree(void *buffer)
131{
132 if (is_vmalloc_addr(buffer)) {
133 /* Data is no longer valid so just use the allocated space
134 * as the work_struct
135 */
136 struct work_struct *work = (struct work_struct *) buffer;
137 INIT_WORK(work, do_vfree);
138 schedule_work(work);
139 } else
140 kfree(buffer);
141}