This source file includes following definitions.
- aa_ns_visible
- aa_ns_name
- alloc_ns
- aa_free_ns
- aa_findn_ns
- aa_find_ns
- __aa_lookupn_ns
- aa_lookupn_ns
- __aa_create_ns
- __aa_find_or_create_ns
- aa_prepare_ns
- destroy_ns
- __aa_remove_ns
- __ns_list_release
- aa_alloc_root_ns
- aa_free_root_ns
1
2
3
4
5
6
7
8
9
10
11
12
13
14 #include <linux/list.h>
15 #include <linux/mutex.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18
19 #include "include/apparmor.h"
20 #include "include/cred.h"
21 #include "include/policy_ns.h"
22 #include "include/label.h"
23 #include "include/policy.h"
24
25
26 struct aa_ns *root_ns;
27 const char *aa_hidden_ns_name = "---";
28
29
30
31
32
33
34
35
36
37 bool aa_ns_visible(struct aa_ns *curr, struct aa_ns *view, bool subns)
38 {
39 if (curr == view)
40 return true;
41
42 if (!subns)
43 return false;
44
45 for ( ; view; view = view->parent) {
46 if (view->parent == curr)
47 return true;
48 }
49
50 return false;
51 }
52
53
54
55
56
57
58
59
60
61 const char *aa_ns_name(struct aa_ns *curr, struct aa_ns *view, bool subns)
62 {
63
64 if (curr == view)
65 return "";
66
67 if (aa_ns_visible(curr, view, subns)) {
68
69
70
71
72
73
74 return view->base.hname + strlen(curr->base.hname) + 2;
75 }
76
77 return aa_hidden_ns_name;
78 }
79
80
81
82
83
84
85
86
87 static struct aa_ns *alloc_ns(const char *prefix, const char *name)
88 {
89 struct aa_ns *ns;
90
91 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
92 AA_DEBUG("%s(%p)\n", __func__, ns);
93 if (!ns)
94 return NULL;
95 if (!aa_policy_init(&ns->base, prefix, name, GFP_KERNEL))
96 goto fail_ns;
97
98 INIT_LIST_HEAD(&ns->sub_ns);
99 INIT_LIST_HEAD(&ns->rawdata_list);
100 mutex_init(&ns->lock);
101 init_waitqueue_head(&ns->wait);
102
103
104 ns->unconfined = aa_alloc_profile("unconfined", NULL, GFP_KERNEL);
105 if (!ns->unconfined)
106 goto fail_unconfined;
107
108 ns->unconfined->label.flags |= FLAG_IX_ON_NAME_ERROR |
109 FLAG_IMMUTIBLE | FLAG_NS_COUNT | FLAG_UNCONFINED;
110 ns->unconfined->mode = APPARMOR_UNCONFINED;
111 ns->unconfined->file.dfa = aa_get_dfa(nulldfa);
112 ns->unconfined->policy.dfa = aa_get_dfa(nulldfa);
113
114
115 ns->unconfined->ns = ns;
116
117 atomic_set(&ns->uniq_null, 0);
118
119 aa_labelset_init(&ns->labels);
120
121 return ns;
122
123 fail_unconfined:
124 kzfree(ns->base.hname);
125 fail_ns:
126 kzfree(ns);
127 return NULL;
128 }
129
130
131
132
133
134
135
136
137 void aa_free_ns(struct aa_ns *ns)
138 {
139 if (!ns)
140 return;
141
142 aa_policy_destroy(&ns->base);
143 aa_labelset_destroy(&ns->labels);
144 aa_put_ns(ns->parent);
145
146 ns->unconfined->ns = NULL;
147 aa_free_profile(ns->unconfined);
148 kzfree(ns);
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162 struct aa_ns *aa_findn_ns(struct aa_ns *root, const char *name, size_t n)
163 {
164 struct aa_ns *ns = NULL;
165
166 rcu_read_lock();
167 ns = aa_get_ns(__aa_findn_ns(&root->sub_ns, name, n));
168 rcu_read_unlock();
169
170 return ns;
171 }
172
173
174
175
176
177
178
179
180
181
182
183 struct aa_ns *aa_find_ns(struct aa_ns *root, const char *name)
184 {
185 return aa_findn_ns(root, name, strlen(name));
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200 struct aa_ns *__aa_lookupn_ns(struct aa_ns *view, const char *hname, size_t n)
201 {
202 struct aa_ns *ns = view;
203 const char *split;
204
205 for (split = strnstr(hname, "//", n); split;
206 split = strnstr(hname, "//", n)) {
207 ns = __aa_findn_ns(&ns->sub_ns, hname, split - hname);
208 if (!ns)
209 return NULL;
210
211 n -= split + 2 - hname;
212 hname = split + 2;
213 }
214
215 if (n)
216 return __aa_findn_ns(&ns->sub_ns, hname, n);
217 return NULL;
218 }
219
220
221
222
223
224
225
226
227
228
229
230
231 struct aa_ns *aa_lookupn_ns(struct aa_ns *view, const char *name, size_t n)
232 {
233 struct aa_ns *ns = NULL;
234
235 rcu_read_lock();
236 ns = aa_get_ns(__aa_lookupn_ns(view, name, n));
237 rcu_read_unlock();
238
239 return ns;
240 }
241
242 static struct aa_ns *__aa_create_ns(struct aa_ns *parent, const char *name,
243 struct dentry *dir)
244 {
245 struct aa_ns *ns;
246 int error;
247
248 AA_BUG(!parent);
249 AA_BUG(!name);
250 AA_BUG(!mutex_is_locked(&parent->lock));
251
252 ns = alloc_ns(parent->base.hname, name);
253 if (!ns)
254 return ERR_PTR(-ENOMEM);
255 ns->level = parent->level + 1;
256 mutex_lock_nested(&ns->lock, ns->level);
257 error = __aafs_ns_mkdir(ns, ns_subns_dir(parent), name, dir);
258 if (error) {
259 AA_ERROR("Failed to create interface for ns %s\n",
260 ns->base.name);
261 mutex_unlock(&ns->lock);
262 aa_free_ns(ns);
263 return ERR_PTR(error);
264 }
265 ns->parent = aa_get_ns(parent);
266 list_add_rcu(&ns->base.list, &parent->sub_ns);
267
268 aa_get_ns(ns);
269 mutex_unlock(&ns->lock);
270
271 return ns;
272 }
273
274
275
276
277
278
279
280
281
282 struct aa_ns *__aa_find_or_create_ns(struct aa_ns *parent, const char *name,
283 struct dentry *dir)
284 {
285 struct aa_ns *ns;
286
287 AA_BUG(!mutex_is_locked(&parent->lock));
288
289
290
291 ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name));
292 if (!ns)
293 ns = __aa_create_ns(parent, name, dir);
294 else
295 ns = ERR_PTR(-EEXIST);
296
297
298 return ns;
299 }
300
301
302
303
304
305
306
307
308 struct aa_ns *aa_prepare_ns(struct aa_ns *parent, const char *name)
309 {
310 struct aa_ns *ns;
311
312 mutex_lock_nested(&parent->lock, parent->level);
313
314
315 ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name));
316 if (!ns)
317 ns = __aa_create_ns(parent, name, NULL);
318 mutex_unlock(&parent->lock);
319
320
321 return ns;
322 }
323
324 static void __ns_list_release(struct list_head *head);
325
326
327
328
329
330 static void destroy_ns(struct aa_ns *ns)
331 {
332 if (!ns)
333 return;
334
335 mutex_lock_nested(&ns->lock, ns->level);
336
337 __aa_profile_list_release(&ns->base.profiles);
338
339
340 __ns_list_release(&ns->sub_ns);
341
342 if (ns->parent) {
343 unsigned long flags;
344
345 write_lock_irqsave(&ns->labels.lock, flags);
346 __aa_proxy_redirect(ns_unconfined(ns),
347 ns_unconfined(ns->parent));
348 write_unlock_irqrestore(&ns->labels.lock, flags);
349 }
350 __aafs_ns_rmdir(ns);
351 mutex_unlock(&ns->lock);
352 }
353
354
355
356
357
358
359
360 void __aa_remove_ns(struct aa_ns *ns)
361 {
362
363 list_del_rcu(&ns->base.list);
364 destroy_ns(ns);
365 aa_put_ns(ns);
366 }
367
368
369
370
371
372
373
374 static void __ns_list_release(struct list_head *head)
375 {
376 struct aa_ns *ns, *tmp;
377
378 list_for_each_entry_safe(ns, tmp, head, base.list)
379 __aa_remove_ns(ns);
380
381 }
382
383
384
385
386
387
388
389 int __init aa_alloc_root_ns(void)
390 {
391
392 root_ns = alloc_ns(NULL, "root");
393 if (!root_ns)
394 return -ENOMEM;
395
396 return 0;
397 }
398
399
400
401
402 void __init aa_free_root_ns(void)
403 {
404 struct aa_ns *ns = root_ns;
405
406 root_ns = NULL;
407
408 destroy_ns(ns);
409 aa_put_ns(ns);
410 }