xref: /linux/security/apparmor/mount.c (revision a3ed5d43f786bdce14cd9eeeb7ca7ab695c82948)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AppArmor security module
4  *
5  * This file contains AppArmor mediation of files
6  *
7  * Copyright (C) 1998-2008 Novell/SUSE
8  * Copyright 2009-2017 Canonical Ltd.
9  */
10 
11 #include <linux/fs.h>
12 #include <linux/mount.h>
13 #include <linux/namei.h>
14 #include <uapi/linux/mount.h>
15 
16 #include "include/apparmor.h"
17 #include "include/audit.h"
18 #include "include/cred.h"
19 #include "include/domain.h"
20 #include "include/file.h"
21 #include "include/match.h"
22 #include "include/mount.h"
23 #include "include/path.h"
24 #include "include/policy.h"
25 
26 
27 static void audit_mnt_flags(struct audit_buffer *ab, unsigned long flags)
28 {
29 	if (flags & MS_RDONLY)
30 		audit_log_format(ab, "ro");
31 	else
32 		audit_log_format(ab, "rw");
33 	if (flags & MS_NOSUID)
34 		audit_log_format(ab, ", nosuid");
35 	if (flags & MS_NODEV)
36 		audit_log_format(ab, ", nodev");
37 	if (flags & MS_NOEXEC)
38 		audit_log_format(ab, ", noexec");
39 	if (flags & MS_SYNCHRONOUS)
40 		audit_log_format(ab, ", sync");
41 	if (flags & MS_REMOUNT)
42 		audit_log_format(ab, ", remount");
43 	if (flags & MS_MANDLOCK)
44 		audit_log_format(ab, ", mand");
45 	if (flags & MS_DIRSYNC)
46 		audit_log_format(ab, ", dirsync");
47 	if (flags & MS_NOSYMFOLLOW)
48 		audit_log_format(ab, ", nosymfollow");
49 	if (flags & MS_NOATIME)
50 		audit_log_format(ab, ", noatime");
51 	if (flags & MS_NODIRATIME)
52 		audit_log_format(ab, ", nodiratime");
53 	if (flags & MS_BIND)
54 		audit_log_format(ab, flags & MS_REC ? ", rbind" : ", bind");
55 	if (flags & MS_MOVE)
56 		audit_log_format(ab, ", move");
57 	if (flags & MS_SILENT)
58 		audit_log_format(ab, ", silent");
59 	if (flags & MS_POSIXACL)
60 		audit_log_format(ab, ", acl");
61 	if (flags & MS_UNBINDABLE)
62 		audit_log_format(ab, flags & MS_REC ? ", runbindable" :
63 				 ", unbindable");
64 	if (flags & MS_PRIVATE)
65 		audit_log_format(ab, flags & MS_REC ? ", rprivate" :
66 				 ", private");
67 	if (flags & MS_SLAVE)
68 		audit_log_format(ab, flags & MS_REC ? ", rslave" :
69 				 ", slave");
70 	if (flags & MS_SHARED)
71 		audit_log_format(ab, flags & MS_REC ? ", rshared" :
72 				 ", shared");
73 	if (flags & MS_RELATIME)
74 		audit_log_format(ab, ", relatime");
75 	if (flags & MS_I_VERSION)
76 		audit_log_format(ab, ", iversion");
77 	if (flags & MS_STRICTATIME)
78 		audit_log_format(ab, ", strictatime");
79 	if (flags & MS_NOUSER)
80 		audit_log_format(ab, ", nouser");
81 }
82 
83 /**
84  * audit_cb - call back for mount specific audit fields
85  * @ab: audit_buffer  (NOT NULL)
86  * @va: audit struct to audit values of  (NOT NULL)
87  */
88 static void audit_cb(struct audit_buffer *ab, void *va)
89 {
90 	struct common_audit_data *sa = va;
91 	struct apparmor_audit_data *ad = aad(sa);
92 
93 	if (ad->mnt.type) {
94 		audit_log_format(ab, " fstype=");
95 		audit_log_untrustedstring(ab, ad->mnt.type);
96 	}
97 	if (ad->mnt.src_name) {
98 		audit_log_format(ab, " srcname=");
99 		audit_log_untrustedstring(ab, ad->mnt.src_name);
100 	}
101 	if (ad->mnt.trans) {
102 		audit_log_format(ab, " trans=");
103 		audit_log_untrustedstring(ab, ad->mnt.trans);
104 	}
105 	if (ad->mnt.flags) {
106 		audit_log_format(ab, " flags=\"");
107 		audit_mnt_flags(ab, ad->mnt.flags);
108 		audit_log_format(ab, "\"");
109 	}
110 	if (ad->mnt.data) {
111 		audit_log_format(ab, " options=");
112 		audit_log_untrustedstring(ab, ad->mnt.data);
113 	}
114 }
115 
116 /**
117  * audit_mount - handle the auditing of mount operations
118  * @subj_cred: cred of the subject
119  * @profile: the profile being enforced  (NOT NULL)
120  * @op: operation being mediated (NOT NULL)
121  * @name: name of object being mediated (MAYBE NULL)
122  * @src_name: src_name of object being mediated (MAYBE_NULL)
123  * @type: type of filesystem (MAYBE_NULL)
124  * @trans: name of trans (MAYBE NULL)
125  * @flags: filesystem independent mount flags
126  * @data: filesystem mount flags
127  * @request: permissions requested
128  * @perms: the permissions computed for the request (NOT NULL)
129  * @info: extra information message (MAYBE NULL)
130  * @error: 0 if operation allowed else failure error code
131  *
132  * Returns: %0 or error on failure
133  */
134 static int audit_mount(const struct cred *subj_cred,
135 		       struct aa_profile *profile, const char *op,
136 		       const char *name, const char *src_name,
137 		       const char *type, const char *trans,
138 		       unsigned long flags, const void *data, u32 request,
139 		       const struct aa_perms *perms, const char *info,
140 		       int error)
141 {
142 	int audit_type = AUDIT_APPARMOR_AUTO;
143 	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_MOUNT, op);
144 
145 	if (likely(!error)) {
146 		u32 mask = perms->audit;
147 
148 		if (unlikely(AUDIT_MODE(profile) == AUDIT_ALL))
149 			mask = 0xffff;
150 
151 		/* mask off perms that are not being force audited */
152 		request &= mask;
153 
154 		if (likely(!request))
155 			return 0;
156 		audit_type = AUDIT_APPARMOR_AUDIT;
157 	} else {
158 		/* only report permissions that were denied */
159 		request = request & ~perms->allow;
160 
161 		if (request & perms->kill)
162 			audit_type = AUDIT_APPARMOR_KILL;
163 
164 		/* quiet known rejects, assumes quiet and kill do not overlap */
165 		if ((request & perms->quiet) &&
166 		    AUDIT_MODE(profile) != AUDIT_NOQUIET &&
167 		    AUDIT_MODE(profile) != AUDIT_ALL)
168 			request &= ~perms->quiet;
169 
170 		if (!request)
171 			return error;
172 	}
173 
174 	ad.subj_cred = subj_cred;
175 	ad.name = name;
176 	ad.mnt.src_name = src_name;
177 	ad.mnt.type = type;
178 	ad.mnt.trans = trans;
179 	ad.mnt.flags = flags;
180 	if (data && (perms->audit & AA_AUDIT_DATA))
181 		ad.mnt.data = data;
182 	ad.info = info;
183 	ad.error = error;
184 
185 	return aa_audit(audit_type, profile, &ad, audit_cb);
186 }
187 
188 /**
189  * match_mnt_flags - Do an ordered match on mount flags
190  * @dfa: dfa to match against
191  * @state: state to start in
192  * @flags: mount flags to match against
193  *
194  * Mount flags are encoded as an ordered match. This is done instead of
195  * checking against a simple bitmask, to allow for logical operations
196  * on the flags.
197  *
198  * Returns: next state after flags match
199  */
200 static aa_state_t match_mnt_flags(struct aa_dfa *dfa, aa_state_t state,
201 				    unsigned long flags)
202 {
203 	unsigned int i;
204 
205 	for (i = 0; i <= 31 ; ++i) {
206 		if ((1 << i) & flags)
207 			state = aa_dfa_next(dfa, state, i + 1);
208 	}
209 
210 	return state;
211 }
212 
213 static const char * const mnt_info_table[] = {
214 	"match succeeded",
215 	"failed mntpnt match",
216 	"failed srcname match",
217 	"failed type match",
218 	"failed flags match",
219 	"failed data match",
220 	"failed perms check"
221 };
222 
223 /*
224  * Returns 0 on success else element that match failed in, this is the
225  * index into the mnt_info_table above
226  */
227 static int do_match_mnt(struct aa_policydb *policy, aa_state_t start,
228 			const char *mntpnt, const char *devname,
229 			const char *type, unsigned long flags,
230 			void *data, bool binary, struct aa_perms *perms)
231 {
232 	aa_state_t state;
233 
234 	AA_BUG(!policy);
235 	AA_BUG(!policy->dfa);
236 	AA_BUG(!policy->perms);
237 	AA_BUG(!perms);
238 
239 	state = aa_dfa_match(policy->dfa, start, mntpnt);
240 	state = aa_dfa_null_transition(policy->dfa, state);
241 	if (!state)
242 		return 1;
243 
244 	if (devname)
245 		state = aa_dfa_match(policy->dfa, state, devname);
246 	state = aa_dfa_null_transition(policy->dfa, state);
247 	if (!state)
248 		return 2;
249 
250 	if (type)
251 		state = aa_dfa_match(policy->dfa, state, type);
252 	state = aa_dfa_null_transition(policy->dfa, state);
253 	if (!state)
254 		return 3;
255 
256 	state = match_mnt_flags(policy->dfa, state, flags);
257 	if (!state)
258 		return 4;
259 	*perms = *aa_lookup_perms(policy, state);
260 	if (perms->allow & AA_MAY_MOUNT)
261 		return 0;
262 
263 	/* only match data if not binary and the DFA flags data is expected */
264 	if (data && !binary && (perms->allow & AA_MNT_CONT_MATCH)) {
265 		state = aa_dfa_null_transition(policy->dfa, state);
266 		if (!state)
267 			return 4;
268 
269 		state = aa_dfa_match(policy->dfa, state, data);
270 		if (!state)
271 			return 5;
272 		*perms = *aa_lookup_perms(policy, state);
273 		if (perms->allow & AA_MAY_MOUNT)
274 			return 0;
275 	}
276 
277 	/* failed at perms check, don't confuse with flags match */
278 	return 6;
279 }
280 
281 
282 static int path_flags(struct aa_profile *profile, const struct path *path)
283 {
284 	AA_BUG(!profile);
285 	AA_BUG(!path);
286 
287 	return profile->path_flags |
288 		(S_ISDIR(path->dentry->d_inode->i_mode) ? PATH_IS_DIR : 0);
289 }
290 
291 /**
292  * match_mnt_path_str - handle path matching for mount
293  * @subj_cred: cred of confined subject
294  * @profile: the confining profile
295  * @mntpath: for the mntpnt (NOT NULL)
296  * @buffer: buffer to be used to lookup mntpath
297  * @devname: string for the devname/src_name (MAY BE NULL OR ERRPTR)
298  * @type: string for the dev type (MAYBE NULL)
299  * @flags: mount flags to match
300  * @data: fs mount data (MAYBE NULL)
301  * @binary: whether @data is binary
302  * @devinfo: error str if (IS_ERR(@devname))
303  *
304  * Returns: 0 on success else error
305  */
306 static int match_mnt_path_str(const struct cred *subj_cred,
307 			      struct aa_profile *profile,
308 			      const struct path *mntpath, char *buffer,
309 			      const char *devname, const char *type,
310 			      unsigned long flags, void *data, bool binary,
311 			      const char *devinfo)
312 {
313 	struct aa_perms perms = { };
314 	const char *mntpnt = NULL, *info = NULL;
315 	struct aa_ruleset *rules = profile->label.rules[0];
316 	int pos, error;
317 
318 	AA_BUG(!profile);
319 	AA_BUG(!mntpath);
320 	AA_BUG(!buffer);
321 
322 	if (!RULE_MEDIATES(rules, AA_CLASS_MOUNT))
323 		return 0;
324 
325 	error = aa_path_name(mntpath, path_flags(profile, mntpath), buffer,
326 			     &mntpnt, &info, profile->disconnected);
327 	if (error)
328 		goto audit;
329 	if (IS_ERR(devname)) {
330 		error = PTR_ERR(devname);
331 		devname = NULL;
332 		info = devinfo;
333 		goto audit;
334 	}
335 
336 	error = -EACCES;
337 	pos = do_match_mnt(rules->policy,
338 			   rules->policy->start[AA_CLASS_MOUNT],
339 			   mntpnt, devname, type, flags, data, binary, &perms);
340 	if (pos) {
341 		info = mnt_info_table[pos];
342 		goto audit;
343 	}
344 	error = 0;
345 
346 audit:
347 	return audit_mount(subj_cred, profile, OP_MOUNT, mntpnt, devname,
348 			   type, NULL,
349 			   flags, data, AA_MAY_MOUNT, &perms, info, error);
350 }
351 
352 /**
353  * match_mnt - handle path matching for mount
354  * @subj_cred: cred of the subject
355  * @profile: the confining profile
356  * @path: for the mntpnt (NOT NULL)
357  * @buffer: buffer to be used to lookup mntpath
358  * @devpath: path devname/src_name (MAYBE NULL)
359  * @devbuffer: buffer to be used to lookup devname/src_name
360  * @type: string for the dev type (MAYBE NULL)
361  * @flags: mount flags to match
362  * @data: fs mount data (MAYBE NULL)
363  * @binary: whether @data is binary
364  *
365  * Returns: 0 on success else error
366  */
367 static int match_mnt(const struct cred *subj_cred,
368 		     struct aa_profile *profile, const struct path *path,
369 		     char *buffer, const struct path *devpath, char *devbuffer,
370 		     const char *type, unsigned long flags, void *data,
371 		     bool binary)
372 {
373 	const char *devname = NULL, *info = NULL;
374 	struct aa_ruleset *rules = profile->label.rules[0];
375 	int error = -EACCES;
376 
377 	AA_BUG(!profile);
378 	AA_BUG(devpath && !devbuffer);
379 
380 	if (!RULE_MEDIATES(rules, AA_CLASS_MOUNT))
381 		return 0;
382 
383 	if (devpath) {
384 		error = aa_path_name(devpath, path_flags(profile, devpath),
385 				     devbuffer, &devname, &info,
386 				     profile->disconnected);
387 		if (error)
388 			devname = ERR_PTR(error);
389 	}
390 
391 	return match_mnt_path_str(subj_cred, profile, path, buffer, devname,
392 				  type, flags, data, binary, info);
393 }
394 
395 int aa_remount(const struct cred *subj_cred,
396 	       struct aa_label *label, const struct path *path,
397 	       unsigned long flags, void *data)
398 {
399 	struct aa_profile *profile;
400 	char *buffer = NULL;
401 	bool binary;
402 	int error;
403 
404 	AA_BUG(!label);
405 	AA_BUG(!path);
406 
407 	binary = path->dentry->d_sb->s_type->fs_flags & FS_BINARY_MOUNTDATA;
408 
409 	buffer = aa_get_buffer(false);
410 	if (!buffer)
411 		return -ENOMEM;
412 	error = fn_for_each_confined(label, profile,
413 			match_mnt(subj_cred, profile, path, buffer, NULL,
414 				  NULL, NULL,
415 				  flags, data, binary));
416 	aa_put_buffer(buffer);
417 
418 	return error;
419 }
420 
421 int aa_bind_mount(const struct cred *subj_cred,
422 		  struct aa_label *label, const struct path *path,
423 		  const char *dev_name, unsigned long flags)
424 {
425 	struct aa_profile *profile;
426 	char *buffer = NULL, *old_buffer = NULL;
427 	struct path old_path;
428 	int error;
429 
430 	AA_BUG(!label);
431 	AA_BUG(!path);
432 
433 	if (!dev_name || !*dev_name)
434 		return -EINVAL;
435 
436 	flags &= MS_REC | MS_BIND;
437 
438 	error = kern_path(dev_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
439 	if (error)
440 		return error;
441 
442 	buffer = aa_get_buffer(false);
443 	old_buffer = aa_get_buffer(false);
444 	error = -ENOMEM;
445 	if (!buffer || !old_buffer)
446 		goto out;
447 
448 	error = fn_for_each_confined(label, profile,
449 			match_mnt(subj_cred, profile, path, buffer, &old_path,
450 				  old_buffer, NULL, flags, NULL, false));
451 out:
452 	aa_put_buffer(buffer);
453 	aa_put_buffer(old_buffer);
454 	path_put(&old_path);
455 
456 	return error;
457 }
458 
459 int aa_mount_change_type(const struct cred *subj_cred,
460 			 struct aa_label *label, const struct path *path,
461 			 unsigned long flags)
462 {
463 	struct aa_profile *profile;
464 	char *buffer = NULL;
465 	int error;
466 
467 	AA_BUG(!label);
468 	AA_BUG(!path);
469 
470 	/* These are the flags allowed by do_change_type() */
471 	flags &= (MS_REC | MS_SILENT | MS_SHARED | MS_PRIVATE | MS_SLAVE |
472 		  MS_UNBINDABLE);
473 
474 	buffer = aa_get_buffer(false);
475 	if (!buffer)
476 		return -ENOMEM;
477 	error = fn_for_each_confined(label, profile,
478 			match_mnt(subj_cred, profile, path, buffer, NULL,
479 				  NULL, NULL,
480 				  flags, NULL, false));
481 	aa_put_buffer(buffer);
482 
483 	return error;
484 }
485 
486 int aa_move_mount(const struct cred *subj_cred,
487 		  struct aa_label *label, const struct path *from_path,
488 		  const struct path *to_path)
489 {
490 	struct aa_profile *profile;
491 	char *to_buffer = NULL, *from_buffer = NULL;
492 	int error;
493 
494 	AA_BUG(!label);
495 	AA_BUG(!from_path);
496 	AA_BUG(!to_path);
497 
498 	to_buffer = aa_get_buffer(false);
499 	from_buffer = aa_get_buffer(false);
500 	error = -ENOMEM;
501 	if (!to_buffer || !from_buffer)
502 		goto out;
503 
504 	if (!our_mnt(from_path->mnt))
505 		/* moving a mount detached from the namespace */
506 		from_path = NULL;
507 	error = fn_for_each_confined(label, profile,
508 			match_mnt(subj_cred, profile, to_path, to_buffer,
509 				  from_path, from_buffer,
510 				  NULL, MS_MOVE, NULL, false));
511 out:
512 	aa_put_buffer(to_buffer);
513 	aa_put_buffer(from_buffer);
514 
515 	return error;
516 }
517 
518 int aa_move_mount_old(const struct cred *subj_cred, struct aa_label *label,
519 		      const struct path *path, const char *orig_name)
520 {
521 	struct path old_path;
522 	int error;
523 
524 	if (!orig_name || !*orig_name)
525 		return -EINVAL;
526 	error = kern_path(orig_name, LOOKUP_FOLLOW, &old_path);
527 	if (error)
528 		return error;
529 
530 	error = aa_move_mount(subj_cred, label, &old_path, path);
531 	path_put(&old_path);
532 
533 	return error;
534 }
535 
536 int aa_new_mount(const struct cred *subj_cred, struct aa_label *label,
537 		 const char *dev_name, const struct path *path,
538 		 const char *type, unsigned long flags, void *data)
539 {
540 	struct aa_profile *profile;
541 	char *buffer = NULL, *dev_buffer = NULL;
542 	bool binary = true;
543 	int error;
544 	int requires_dev = 0;
545 	struct path tmp_path, *dev_path = NULL;
546 
547 	AA_BUG(!label);
548 	AA_BUG(!path);
549 
550 	if (type) {
551 		struct file_system_type *fstype;
552 
553 		fstype = get_fs_type(type);
554 		if (!fstype)
555 			return -ENODEV;
556 		binary = fstype->fs_flags & FS_BINARY_MOUNTDATA;
557 		requires_dev = fstype->fs_flags & FS_REQUIRES_DEV;
558 		put_filesystem(fstype);
559 
560 		if (requires_dev) {
561 			if (!dev_name || !*dev_name)
562 				return -ENOENT;
563 
564 			error = kern_path(dev_name, LOOKUP_FOLLOW, &tmp_path);
565 			if (error)
566 				return error;
567 			dev_path = &tmp_path;
568 		}
569 	}
570 
571 	buffer = aa_get_buffer(false);
572 	if (!buffer) {
573 		error = -ENOMEM;
574 		goto out;
575 	}
576 	if (dev_path) {
577 		dev_buffer = aa_get_buffer(false);
578 		if (!dev_buffer) {
579 			error = -ENOMEM;
580 			goto out;
581 		}
582 		error = fn_for_each_confined(label, profile,
583 				match_mnt(subj_cred, profile, path, buffer,
584 					  dev_path, dev_buffer,
585 				  type, flags, data, binary));
586 	} else {
587 		error = fn_for_each_confined(label, profile,
588 				match_mnt_path_str(subj_cred, profile, path,
589 					buffer, dev_name,
590 					type, flags, data, binary, NULL));
591 	}
592 
593 out:
594 	aa_put_buffer(buffer);
595 	aa_put_buffer(dev_buffer);
596 	if (dev_path)
597 		path_put(dev_path);
598 
599 	return error;
600 }
601 
602 static int profile_umount(const struct cred *subj_cred,
603 			  struct aa_profile *profile, const struct path *path,
604 			  char *buffer)
605 {
606 	struct aa_ruleset *rules = profile->label.rules[0];
607 	struct aa_perms perms = { };
608 	const char *name = NULL, *info = NULL;
609 	aa_state_t state;
610 	int error;
611 
612 	AA_BUG(!profile);
613 	AA_BUG(!path);
614 
615 	if (!RULE_MEDIATES(rules, AA_CLASS_MOUNT))
616 		return 0;
617 
618 	error = aa_path_name(path, path_flags(profile, path), buffer, &name,
619 			     &info, profile->disconnected);
620 	if (error)
621 		goto audit;
622 
623 	state = aa_dfa_match(rules->policy->dfa,
624 			     rules->policy->start[AA_CLASS_MOUNT],
625 			     name);
626 	perms = *aa_lookup_perms(rules->policy, state);
627 	if (AA_MAY_UMOUNT & ~perms.allow)
628 		error = -EACCES;
629 
630 audit:
631 	return audit_mount(subj_cred, profile, OP_UMOUNT, name, NULL, NULL,
632 			   NULL, 0, NULL,
633 			   AA_MAY_UMOUNT, &perms, info, error);
634 }
635 
636 int aa_umount(const struct cred *subj_cred, struct aa_label *label,
637 	      struct vfsmount *mnt, int flags)
638 {
639 	struct aa_profile *profile;
640 	char *buffer = NULL;
641 	int error;
642 	struct path path = { .mnt = mnt, .dentry = mnt->mnt_root };
643 
644 	AA_BUG(!label);
645 	AA_BUG(!mnt);
646 
647 	buffer = aa_get_buffer(false);
648 	if (!buffer)
649 		return -ENOMEM;
650 
651 	error = fn_for_each_confined(label, profile,
652 			profile_umount(subj_cred, profile, &path, buffer));
653 	aa_put_buffer(buffer);
654 
655 	return error;
656 }
657 
658 /* helper fn for transition on pivotroot
659  *
660  * Returns: label for transition or ERR_PTR. Does not return NULL
661  */
662 static struct aa_label *build_pivotroot(const struct cred *subj_cred,
663 					struct aa_profile *profile,
664 					const struct path *new_path,
665 					char *new_buffer,
666 					const struct path *old_path,
667 					char *old_buffer)
668 {
669 	struct aa_ruleset *rules = profile->label.rules[0];
670 	const char *old_name, *new_name = NULL, *info = NULL;
671 	const char *trans_name = NULL;
672 	struct aa_perms perms = { };
673 	aa_state_t state;
674 	int error;
675 
676 	AA_BUG(!profile);
677 	AA_BUG(!new_path);
678 	AA_BUG(!old_path);
679 
680 	if (profile_unconfined(profile) ||
681 	    !RULE_MEDIATES(rules, AA_CLASS_MOUNT))
682 		return aa_get_newest_label(&profile->label);
683 
684 	error = aa_path_name(old_path, path_flags(profile, old_path),
685 			     old_buffer, &old_name, &info,
686 			     profile->disconnected);
687 	if (error)
688 		goto audit;
689 	error = aa_path_name(new_path, path_flags(profile, new_path),
690 			     new_buffer, &new_name, &info,
691 			     profile->disconnected);
692 	if (error)
693 		goto audit;
694 
695 	error = -EACCES;
696 	state = aa_dfa_match(rules->policy->dfa,
697 			     rules->policy->start[AA_CLASS_MOUNT],
698 			     new_name);
699 	state = aa_dfa_null_transition(rules->policy->dfa, state);
700 	state = aa_dfa_match(rules->policy->dfa, state, old_name);
701 	perms = *aa_lookup_perms(rules->policy, state);
702 
703 	if (AA_MAY_PIVOTROOT & perms.allow)
704 		error = 0;
705 
706 audit:
707 	error = audit_mount(subj_cred, profile, OP_PIVOTROOT, new_name,
708 			    old_name,
709 			    NULL, trans_name, 0, NULL, AA_MAY_PIVOTROOT,
710 			    &perms, info, error);
711 	if (error)
712 		return ERR_PTR(error);
713 
714 	return aa_get_newest_label(&profile->label);
715 }
716 
717 int aa_pivotroot(const struct cred *subj_cred, struct aa_label *label,
718 		 const struct path *old_path,
719 		 const struct path *new_path)
720 {
721 	struct aa_profile *profile;
722 	struct aa_label *target = NULL;
723 	char *old_buffer = NULL, *new_buffer = NULL, *info = NULL;
724 	int error;
725 
726 	AA_BUG(!label);
727 	AA_BUG(!old_path);
728 	AA_BUG(!new_path);
729 
730 	old_buffer = aa_get_buffer(false);
731 	new_buffer = aa_get_buffer(false);
732 	error = -ENOMEM;
733 	if (!old_buffer || !new_buffer)
734 		goto out;
735 	target = fn_label_build(label, profile, GFP_KERNEL,
736 			build_pivotroot(subj_cred, profile, new_path,
737 					new_buffer,
738 					old_path, old_buffer));
739 	AA_BUG(!target);
740 	if (!IS_ERR(target)) {
741 		error = aa_replace_current_label(target);
742 		if (error)
743 			goto fail;
744 		aa_put_label(target);
745 	} else
746 		/* already audited error */
747 		error = PTR_ERR(target);
748 out:
749 	aa_put_buffer(old_buffer);
750 	aa_put_buffer(new_buffer);
751 
752 	return error;
753 
754 fail:
755 	/* TODO: add back in auditing of new_name and old_name */
756 	error = fn_for_each(label, profile,
757 			audit_mount(subj_cred, profile, OP_PIVOTROOT,
758 				    NULL /*new_name */,
759 				    NULL /* old_name */,
760 				    NULL, NULL,
761 				    0, target->hname, AA_MAY_PIVOTROOT, &nullperms, info,
762 				    error));
763 	aa_put_label(target);
764 	goto out;
765 }
766