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