2 * AppArmor security module
4 * This file contains AppArmor mediation of files
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2017 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/mount.h>
17 #include <linux/namei.h>
18 #include <uapi/linux/mount.h>
20 #include "include/apparmor.h"
21 #include "include/audit.h"
22 #include "include/cred.h"
23 #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"
31 static void audit_mnt_flags(struct audit_buffer
*ab
, unsigned long flags
)
33 if (flags
& MS_RDONLY
)
34 audit_log_format(ab
, "ro");
36 audit_log_format(ab
, "rw");
37 if (flags
& MS_NOSUID
)
38 audit_log_format(ab
, ", nosuid");
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");
56 audit_log_format(ab
, flags
& MS_REC
? ", rbind" : ", bind");
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" :
66 if (flags
& MS_PRIVATE
)
67 audit_log_format(ab
, flags
& MS_REC
? ", rprivate" :
70 audit_log_format(ab
, flags
& MS_REC
? ", rslave" :
72 if (flags
& MS_SHARED
)
73 audit_log_format(ab
, flags
& MS_REC
? ", rshared" :
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");
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)
90 static void audit_cb(struct audit_buffer
*ab
, void *va
)
92 struct common_audit_data
*sa
= va
;
94 if (aad(sa
)->mnt
.type
) {
95 audit_log_format(ab
, " fstype=");
96 audit_log_untrustedstring(ab
, aad(sa
)->mnt
.type
);
98 if (aad(sa
)->mnt
.src_name
) {
99 audit_log_format(ab
, " srcname=");
100 audit_log_untrustedstring(ab
, aad(sa
)->mnt
.src_name
);
102 if (aad(sa
)->mnt
.trans
) {
103 audit_log_format(ab
, " trans=");
104 audit_log_untrustedstring(ab
, aad(sa
)->mnt
.trans
);
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
, "\"");
111 if (aad(sa
)->mnt
.data
) {
112 audit_log_format(ab
, " options=");
113 audit_log_untrustedstring(ab
, aad(sa
)->mnt
.data
);
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)
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
132 * Returns: %0 or error on failure
134 static 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
)
140 int audit_type
= AUDIT_APPARMOR_AUTO
;
141 DEFINE_AUDIT_DATA(sa
, LSM_AUDIT_DATA_NONE
, op
);
143 if (likely(!error
)) {
144 u32 mask
= perms
->audit
;
146 if (unlikely(AUDIT_MODE(profile
) == AUDIT_ALL
))
149 /* mask off perms that are not being force audited */
152 if (likely(!request
))
154 audit_type
= AUDIT_APPARMOR_AUDIT
;
156 /* only report permissions that were denied */
157 request
= request
& ~perms
->allow
;
159 if (request
& perms
->kill
)
160 audit_type
= AUDIT_APPARMOR_KILL
;
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
;
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
;
182 return aa_audit(audit_type
, profile
, &sa
, audit_cb
);
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
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
195 * Returns: next state after flags match
197 static unsigned int match_mnt_flags(struct aa_dfa
*dfa
, unsigned int state
,
202 for (i
= 0; i
<= 31 ; ++i
) {
203 if ((1 << i
) & flags
)
204 state
= aa_dfa_next(dfa
, state
, i
+ 1);
211 * compute_mnt_perms - compute mount permission associated with @state
212 * @dfa: dfa to match against (NOT NULL)
213 * @state: state match finished in
215 * Returns: mount permissions
217 static struct aa_perms
compute_mnt_perms(struct aa_dfa
*dfa
,
220 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
),
230 static const char * const mnt_info_table
[] = {
232 "failed mntpnt match",
233 "failed srcname match",
235 "failed flags match",
240 * Returns 0 on success else element that match failed in, this is the
241 * index into the mnt_info_table above
243 static 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
)
253 state
= aa_dfa_match(dfa
, start
, mntpnt
);
254 state
= aa_dfa_null_transition(dfa
, state
);
259 state
= aa_dfa_match(dfa
, state
, devname
);
260 state
= aa_dfa_null_transition(dfa
, state
);
265 state
= aa_dfa_match(dfa
, state
, type
);
266 state
= aa_dfa_null_transition(dfa
, state
);
270 state
= match_mnt_flags(dfa
, state
, flags
);
273 *perms
= compute_mnt_perms(dfa
, state
);
274 if (perms
->allow
& AA_MAY_MOUNT
)
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
);
283 state
= aa_dfa_match(dfa
, state
, data
);
286 *perms
= compute_mnt_perms(dfa
, state
);
287 if (perms
->allow
& AA_MAY_MOUNT
)
291 /* failed at end of flags match */
296 static int path_flags(struct aa_profile
*profile
, const struct path
*path
)
301 return profile
->path_flags
|
302 (S_ISDIR(path
->dentry
->d_inode
->i_mode
) ? PATH_IS_DIR
: 0);
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))
317 * Returns: 0 on success else error
319 static 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
,
325 struct aa_perms perms
= { };
326 const char *mntpnt
= NULL
, *info
= NULL
;
333 if (!PROFILE_MEDIATES(profile
, AA_CLASS_MOUNT
))
336 error
= aa_path_name(mntpath
, path_flags(profile
, mntpath
), buffer
,
337 &mntpnt
, &info
, profile
->disconnected
);
340 if (IS_ERR(devname
)) {
341 error
= PTR_ERR(devname
);
348 pos
= do_match_mnt(profile
->policy
.dfa
,
349 profile
->policy
.start
[AA_CLASS_MOUNT
],
350 mntpnt
, devname
, type
, flags
, data
, binary
, &perms
);
352 info
= mnt_info_table
[pos
];
358 return audit_mount(profile
, OP_MOUNT
, mntpnt
, devname
, type
, NULL
,
359 flags
, data
, AA_MAY_MOUNT
, &perms
, info
, error
);
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
374 * Returns: 0 on success else error
376 static 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
,
381 const char *devname
= NULL
, *info
= NULL
;
385 AA_BUG(devpath
&& !devbuffer
);
387 if (!PROFILE_MEDIATES(profile
, AA_CLASS_MOUNT
))
391 error
= aa_path_name(devpath
, path_flags(profile
, devpath
),
392 devbuffer
, &devname
, &info
,
393 profile
->disconnected
);
395 devname
= ERR_PTR(error
);
398 return match_mnt_path_str(profile
, path
, buffer
, devname
, type
, flags
,
402 int aa_remount(struct aa_label
*label
, const struct path
*path
,
403 unsigned long flags
, void *data
)
405 struct aa_profile
*profile
;
413 binary
= path
->dentry
->d_sb
->s_type
->fs_flags
& FS_BINARY_MOUNTDATA
;
416 error
= fn_for_each_confined(label
, profile
,
417 match_mnt(profile
, path
, buffer
, NULL
, NULL
, NULL
,
418 flags
, data
, binary
));
424 int aa_bind_mount(struct aa_label
*label
, const struct path
*path
,
425 const char *dev_name
, unsigned long flags
)
427 struct aa_profile
*profile
;
428 char *buffer
= NULL
, *old_buffer
= NULL
;
429 struct path old_path
;
435 if (!dev_name
|| !*dev_name
)
438 flags
&= MS_REC
| MS_BIND
;
440 error
= kern_path(dev_name
, LOOKUP_FOLLOW
|LOOKUP_AUTOMOUNT
, &old_path
);
444 get_buffers(buffer
, old_buffer
);
445 error
= fn_for_each_confined(label
, profile
,
446 match_mnt(profile
, path
, buffer
, &old_path
, old_buffer
,
447 NULL
, flags
, NULL
, false));
448 put_buffers(buffer
, old_buffer
);
454 int aa_mount_change_type(struct aa_label
*label
, const struct path
*path
,
457 struct aa_profile
*profile
;
464 /* These are the flags allowed by do_change_type() */
465 flags
&= (MS_REC
| MS_SILENT
| MS_SHARED
| MS_PRIVATE
| MS_SLAVE
|
469 error
= fn_for_each_confined(label
, profile
,
470 match_mnt(profile
, path
, buffer
, NULL
, NULL
, NULL
,
471 flags
, NULL
, false));
477 int aa_move_mount(struct aa_label
*label
, const struct path
*path
,
478 const char *orig_name
)
480 struct aa_profile
*profile
;
481 char *buffer
= NULL
, *old_buffer
= NULL
;
482 struct path old_path
;
488 if (!orig_name
|| !*orig_name
)
491 error
= kern_path(orig_name
, LOOKUP_FOLLOW
, &old_path
);
495 get_buffers(buffer
, old_buffer
);
496 error
= fn_for_each_confined(label
, profile
,
497 match_mnt(profile
, path
, buffer
, &old_path
, old_buffer
,
498 NULL
, MS_MOVE
, NULL
, false));
499 put_buffers(buffer
, old_buffer
);
505 int aa_new_mount(struct aa_label
*label
, const char *dev_name
,
506 const struct path
*path
, const char *type
, unsigned long flags
,
509 struct aa_profile
*profile
;
510 char *buffer
= NULL
, *dev_buffer
= NULL
;
513 int requires_dev
= 0;
514 struct path tmp_path
, *dev_path
= NULL
;
520 struct file_system_type
*fstype
;
522 fstype
= get_fs_type(type
);
525 binary
= fstype
->fs_flags
& FS_BINARY_MOUNTDATA
;
526 requires_dev
= fstype
->fs_flags
& FS_REQUIRES_DEV
;
527 put_filesystem(fstype
);
530 if (!dev_name
|| !*dev_name
)
533 error
= kern_path(dev_name
, LOOKUP_FOLLOW
, &tmp_path
);
536 dev_path
= &tmp_path
;
540 get_buffers(buffer
, dev_buffer
);
542 error
= fn_for_each_confined(label
, profile
,
543 match_mnt(profile
, path
, buffer
, dev_path
, dev_buffer
,
544 type
, flags
, data
, binary
));
546 error
= fn_for_each_confined(label
, profile
,
547 match_mnt_path_str(profile
, path
, buffer
, dev_name
,
548 type
, flags
, data
, binary
, NULL
));
550 put_buffers(buffer
, dev_buffer
);
557 static int profile_umount(struct aa_profile
*profile
, struct path
*path
,
560 struct aa_perms perms
= { };
561 const char *name
= NULL
, *info
= NULL
;
568 if (!PROFILE_MEDIATES(profile
, AA_CLASS_MOUNT
))
571 error
= aa_path_name(path
, path_flags(profile
, path
), buffer
, &name
,
572 &info
, profile
->disconnected
);
576 state
= aa_dfa_match(profile
->policy
.dfa
,
577 profile
->policy
.start
[AA_CLASS_MOUNT
],
579 perms
= compute_mnt_perms(profile
->policy
.dfa
, state
);
580 if (AA_MAY_UMOUNT
& ~perms
.allow
)
584 return audit_mount(profile
, OP_UMOUNT
, name
, NULL
, NULL
, NULL
, 0, NULL
,
585 AA_MAY_UMOUNT
, &perms
, info
, error
);
588 int aa_umount(struct aa_label
*label
, struct vfsmount
*mnt
, int flags
)
590 struct aa_profile
*profile
;
593 struct path path
= { .mnt
= mnt
, .dentry
= mnt
->mnt_root
};
599 error
= fn_for_each_confined(label
, profile
,
600 profile_umount(profile
, &path
, buffer
));
606 /* helper fn for transition on pivotroot
608 * Returns: label for transition or ERR_PTR. Does not return NULL
610 static struct aa_label
*build_pivotroot(struct aa_profile
*profile
,
611 const struct path
*new_path
,
613 const struct path
*old_path
,
616 const char *old_name
, *new_name
= NULL
, *info
= NULL
;
617 const char *trans_name
= NULL
;
618 struct aa_perms perms
= { };
626 if (profile_unconfined(profile
) ||
627 !PROFILE_MEDIATES(profile
, AA_CLASS_MOUNT
))
628 return aa_get_newest_label(&profile
->label
);
630 error
= aa_path_name(old_path
, path_flags(profile
, old_path
),
631 old_buffer
, &old_name
, &info
,
632 profile
->disconnected
);
635 error
= aa_path_name(new_path
, path_flags(profile
, new_path
),
636 new_buffer
, &new_name
, &info
,
637 profile
->disconnected
);
642 state
= aa_dfa_match(profile
->policy
.dfa
,
643 profile
->policy
.start
[AA_CLASS_MOUNT
],
645 state
= aa_dfa_null_transition(profile
->policy
.dfa
, state
);
646 state
= aa_dfa_match(profile
->policy
.dfa
, state
, old_name
);
647 perms
= compute_mnt_perms(profile
->policy
.dfa
, state
);
649 if (AA_MAY_PIVOTROOT
& perms
.allow
)
653 error
= audit_mount(profile
, OP_PIVOTROOT
, new_name
, old_name
,
654 NULL
, trans_name
, 0, NULL
, AA_MAY_PIVOTROOT
,
655 &perms
, info
, error
);
657 return ERR_PTR(error
);
659 return aa_get_newest_label(&profile
->label
);
662 int aa_pivotroot(struct aa_label
*label
, const struct path
*old_path
,
663 const struct path
*new_path
)
665 struct aa_profile
*profile
;
666 struct aa_label
*target
= NULL
;
667 char *old_buffer
= NULL
, *new_buffer
= NULL
, *info
= NULL
;
674 get_buffers(old_buffer
, new_buffer
);
675 target
= fn_label_build(label
, profile
, GFP_ATOMIC
,
676 build_pivotroot(profile
, new_path
, new_buffer
,
677 old_path
, old_buffer
));
679 info
= "label build failed";
682 } else if (!IS_ERR(target
)) {
683 error
= aa_replace_current_label(target
);
685 /* TODO: audit target */
686 aa_put_label(target
);
690 /* already audited error */
691 error
= PTR_ERR(target
);
693 put_buffers(old_buffer
, new_buffer
);
698 /* TODO: add back in auditing of new_name and old_name */
699 error
= fn_for_each(label
, profile
,
700 audit_mount(profile
, OP_PIVOTROOT
, NULL
/*new_name */,
703 0, NULL
, AA_MAY_PIVOTROOT
, &nullperms
, info
,