GNU Linux-libre 4.14.259-gnu1
[releases.git] / security / apparmor / mount.c
1 /*
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>
18
19 #include "include/apparmor.h"
20 #include "include/audit.h"
21 #include "include/context.h"
22 #include "include/domain.h"
23 #include "include/file.h"
24 #include "include/match.h"
25 #include "include/mount.h"
26 #include "include/path.h"
27 #include "include/policy.h"
28
29
30 static void audit_mnt_flags(struct audit_buffer *ab, unsigned long flags)
31 {
32         if (flags & MS_RDONLY)
33                 audit_log_format(ab, "ro");
34         else
35                 audit_log_format(ab, "rw");
36         if (flags & MS_NOSUID)
37                 audit_log_format(ab, ", nosuid");
38         if (flags & MS_NODEV)
39                 audit_log_format(ab, ", nodev");
40         if (flags & MS_NOEXEC)
41                 audit_log_format(ab, ", noexec");
42         if (flags & MS_SYNCHRONOUS)
43                 audit_log_format(ab, ", sync");
44         if (flags & MS_REMOUNT)
45                 audit_log_format(ab, ", remount");
46         if (flags & MS_MANDLOCK)
47                 audit_log_format(ab, ", mand");
48         if (flags & MS_DIRSYNC)
49                 audit_log_format(ab, ", dirsync");
50         if (flags & MS_NOATIME)
51                 audit_log_format(ab, ", noatime");
52         if (flags & MS_NODIRATIME)
53                 audit_log_format(ab, ", nodiratime");
54         if (flags & MS_BIND)
55                 audit_log_format(ab, flags & MS_REC ? ", rbind" : ", bind");
56         if (flags & MS_MOVE)
57                 audit_log_format(ab, ", move");
58         if (flags & MS_SILENT)
59                 audit_log_format(ab, ", silent");
60         if (flags & MS_POSIXACL)
61                 audit_log_format(ab, ", acl");
62         if (flags & MS_UNBINDABLE)
63                 audit_log_format(ab, flags & MS_REC ? ", runbindable" :
64                                  ", unbindable");
65         if (flags & MS_PRIVATE)
66                 audit_log_format(ab, flags & MS_REC ? ", rprivate" :
67                                  ", private");
68         if (flags & MS_SLAVE)
69                 audit_log_format(ab, flags & MS_REC ? ", rslave" :
70                                  ", slave");
71         if (flags & MS_SHARED)
72                 audit_log_format(ab, flags & MS_REC ? ", rshared" :
73                                  ", shared");
74         if (flags & MS_RELATIME)
75                 audit_log_format(ab, ", relatime");
76         if (flags & MS_I_VERSION)
77                 audit_log_format(ab, ", iversion");
78         if (flags & MS_STRICTATIME)
79                 audit_log_format(ab, ", strictatime");
80         if (flags & MS_NOUSER)
81                 audit_log_format(ab, ", nouser");
82 }
83
84 /**
85  * audit_cb - call back for mount specific audit fields
86  * @ab: audit_buffer  (NOT NULL)
87  * @va: audit struct to audit values of  (NOT NULL)
88  */
89 static void audit_cb(struct audit_buffer *ab, void *va)
90 {
91         struct common_audit_data *sa = va;
92
93         if (aad(sa)->mnt.type) {
94                 audit_log_format(ab, " fstype=");
95                 audit_log_untrustedstring(ab, aad(sa)->mnt.type);
96         }
97         if (aad(sa)->mnt.src_name) {
98                 audit_log_format(ab, " srcname=");
99                 audit_log_untrustedstring(ab, aad(sa)->mnt.src_name);
100         }
101         if (aad(sa)->mnt.trans) {
102                 audit_log_format(ab, " trans=");
103                 audit_log_untrustedstring(ab, aad(sa)->mnt.trans);
104         }
105         if (aad(sa)->mnt.flags) {
106                 audit_log_format(ab, " flags=\"");
107                 audit_mnt_flags(ab, aad(sa)->mnt.flags);
108                 audit_log_format(ab, "\"");
109         }
110         if (aad(sa)->mnt.data) {
111                 audit_log_format(ab, " options=");
112                 audit_log_untrustedstring(ab, aad(sa)->mnt.data);
113         }
114 }
115
116 /**
117  * audit_mount - handle the auditing of mount operations
118  * @profile: the profile being enforced  (NOT NULL)
119  * @op: operation being mediated (NOT NULL)
120  * @name: name of object being mediated (MAYBE NULL)
121  * @src_name: src_name of object being mediated (MAYBE_NULL)
122  * @type: type of filesystem (MAYBE_NULL)
123  * @trans: name of trans (MAYBE NULL)
124  * @flags: filesystem idependent mount flags
125  * @data: filesystem mount flags
126  * @request: permissions requested
127  * @perms: the permissions computed for the request (NOT NULL)
128  * @info: extra information message (MAYBE NULL)
129  * @error: 0 if operation allowed else failure error code
130  *
131  * Returns: %0 or error on failure
132  */
133 static int audit_mount(struct aa_profile *profile, const char *op,
134                        const char *name, const char *src_name,
135                        const char *type, const char *trans,
136                        unsigned long flags, const void *data, u32 request,
137                        struct aa_perms *perms, const char *info, int error)
138 {
139         int audit_type = AUDIT_APPARMOR_AUTO;
140         DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, op);
141
142         if (likely(!error)) {
143                 u32 mask = perms->audit;
144
145                 if (unlikely(AUDIT_MODE(profile) == AUDIT_ALL))
146                         mask = 0xffff;
147
148                 /* mask off perms that are not being force audited */
149                 request &= mask;
150
151                 if (likely(!request))
152                         return 0;
153                 audit_type = AUDIT_APPARMOR_AUDIT;
154         } else {
155                 /* only report permissions that were denied */
156                 request = request & ~perms->allow;
157
158                 if (request & perms->kill)
159                         audit_type = AUDIT_APPARMOR_KILL;
160
161                 /* quiet known rejects, assumes quiet and kill do not overlap */
162                 if ((request & perms->quiet) &&
163                     AUDIT_MODE(profile) != AUDIT_NOQUIET &&
164                     AUDIT_MODE(profile) != AUDIT_ALL)
165                         request &= ~perms->quiet;
166
167                 if (!request)
168                         return error;
169         }
170
171         aad(&sa)->name = name;
172         aad(&sa)->mnt.src_name = src_name;
173         aad(&sa)->mnt.type = type;
174         aad(&sa)->mnt.trans = trans;
175         aad(&sa)->mnt.flags = flags;
176         if (data && (perms->audit & AA_AUDIT_DATA))
177                 aad(&sa)->mnt.data = data;
178         aad(&sa)->info = info;
179         aad(&sa)->error = error;
180
181         return aa_audit(audit_type, profile, &sa, audit_cb);
182 }
183
184 /**
185  * match_mnt_flags - Do an ordered match on mount flags
186  * @dfa: dfa to match against
187  * @state: state to start in
188  * @flags: mount flags to match against
189  *
190  * Mount flags are encoded as an ordered match. This is done instead of
191  * checking against a simple bitmask, to allow for logical operations
192  * on the flags.
193  *
194  * Returns: next state after flags match
195  */
196 static unsigned int match_mnt_flags(struct aa_dfa *dfa, unsigned int state,
197                                     unsigned long flags)
198 {
199         unsigned int i;
200
201         for (i = 0; i <= 31 ; ++i) {
202                 if ((1 << i) & flags)
203                         state = aa_dfa_next(dfa, state, i + 1);
204         }
205
206         return state;
207 }
208
209 /**
210  * compute_mnt_perms - compute mount permission associated with @state
211  * @dfa: dfa to match against (NOT NULL)
212  * @state: state match finished in
213  *
214  * Returns: mount permissions
215  */
216 static struct aa_perms compute_mnt_perms(struct aa_dfa *dfa,
217                                            unsigned int state)
218 {
219         struct aa_perms perms;
220
221         perms.kill = 0;
222         perms.allow = dfa_user_allow(dfa, state);
223         perms.audit = dfa_user_audit(dfa, state);
224         perms.quiet = dfa_user_quiet(dfa, state);
225         perms.xindex = dfa_user_xindex(dfa, state);
226
227         return perms;
228 }
229
230 static 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  */
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)
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
296 static 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  */
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,
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
333         if (!PROFILE_MEDIATES(profile, AA_CLASS_MOUNT))
334                 return 0;
335
336         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
357 audit:
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  */
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,
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
387         if (!PROFILE_MEDIATES(profile, AA_CLASS_MOUNT))
388                 return 0;
389
390         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
402 int 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
415         get_buffers(buffer);
416         error = fn_for_each_confined(label, profile,
417                         match_mnt(profile, path, buffer, NULL, NULL, NULL,
418                                   flags, data, binary));
419         put_buffers(buffer);
420
421         return error;
422 }
423
424 int aa_bind_mount(struct aa_label *label, const struct path *path,
425                   const char *dev_name, unsigned long flags)
426 {
427         struct aa_profile *profile;
428         char *buffer = NULL, *old_buffer = NULL;
429         struct path old_path;
430         int error;
431
432         AA_BUG(!label);
433         AA_BUG(!path);
434
435         if (!dev_name || !*dev_name)
436                 return -EINVAL;
437
438         flags &= MS_REC | MS_BIND;
439
440         error = kern_path(dev_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
441         if (error)
442                 return error;
443
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);
449         path_put(&old_path);
450
451         return error;
452 }
453
454 int aa_mount_change_type(struct aa_label *label, const struct path *path,
455                          unsigned long flags)
456 {
457         struct aa_profile *profile;
458         char *buffer = NULL;
459         int error;
460
461         AA_BUG(!label);
462         AA_BUG(!path);
463
464         /* These are the flags allowed by do_change_type() */
465         flags &= (MS_REC | MS_SILENT | MS_SHARED | MS_PRIVATE | MS_SLAVE |
466                   MS_UNBINDABLE);
467
468         get_buffers(buffer);
469         error = fn_for_each_confined(label, profile,
470                         match_mnt(profile, path, buffer, NULL, NULL, NULL,
471                                   flags, NULL, false));
472         put_buffers(buffer);
473
474         return error;
475 }
476
477 int aa_move_mount(struct aa_label *label, const struct path *path,
478                   const char *orig_name)
479 {
480         struct aa_profile *profile;
481         char *buffer = NULL, *old_buffer = NULL;
482         struct path old_path;
483         int error;
484
485         AA_BUG(!label);
486         AA_BUG(!path);
487
488         if (!orig_name || !*orig_name)
489                 return -EINVAL;
490
491         error = kern_path(orig_name, LOOKUP_FOLLOW, &old_path);
492         if (error)
493                 return error;
494
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);
500         path_put(&old_path);
501
502         return error;
503 }
504
505 int aa_new_mount(struct aa_label *label, const char *dev_name,
506                  const struct path *path, const char *type, unsigned long flags,
507                  void *data)
508 {
509         struct aa_profile *profile;
510         char *buffer = NULL, *dev_buffer = NULL;
511         bool binary = true;
512         int error;
513         int requires_dev = 0;
514         struct path tmp_path, *dev_path = NULL;
515
516         AA_BUG(!label);
517         AA_BUG(!path);
518
519         if (type) {
520                 struct file_system_type *fstype;
521
522                 fstype = get_fs_type(type);
523                 if (!fstype)
524                         return -ENODEV;
525                 binary = fstype->fs_flags & FS_BINARY_MOUNTDATA;
526                 requires_dev = fstype->fs_flags & FS_REQUIRES_DEV;
527                 put_filesystem(fstype);
528
529                 if (requires_dev) {
530                         if (!dev_name || !*dev_name)
531                                 return -ENOENT;
532
533                         error = kern_path(dev_name, LOOKUP_FOLLOW, &tmp_path);
534                         if (error)
535                                 return error;
536                         dev_path = &tmp_path;
537                 }
538         }
539
540         get_buffers(buffer, dev_buffer);
541         if (dev_path) {
542                 error = fn_for_each_confined(label, profile,
543                         match_mnt(profile, path, buffer, dev_path, dev_buffer,
544                                   type, flags, data, binary));
545         } else {
546                 error = fn_for_each_confined(label, profile,
547                         match_mnt_path_str(profile, path, buffer, dev_name,
548                                            type, flags, data, binary, NULL));
549         }
550         put_buffers(buffer, dev_buffer);
551         if (dev_path)
552                 path_put(dev_path);
553
554         return error;
555 }
556
557 static int profile_umount(struct aa_profile *profile, struct path *path,
558                           char *buffer)
559 {
560         struct aa_perms perms = { };
561         const char *name = NULL, *info = NULL;
562         unsigned int state;
563         int error;
564
565         AA_BUG(!profile);
566         AA_BUG(!path);
567
568         if (!PROFILE_MEDIATES(profile, AA_CLASS_MOUNT))
569                 return 0;
570
571         error = aa_path_name(path, path_flags(profile, path), buffer, &name,
572                              &info, profile->disconnected);
573         if (error)
574                 goto audit;
575
576         state = aa_dfa_match(profile->policy.dfa,
577                              profile->policy.start[AA_CLASS_MOUNT],
578                              name);
579         perms = compute_mnt_perms(profile->policy.dfa, state);
580         if (AA_MAY_UMOUNT & ~perms.allow)
581                 error = -EACCES;
582
583 audit:
584         return audit_mount(profile, OP_UMOUNT, name, NULL, NULL, NULL, 0, NULL,
585                            AA_MAY_UMOUNT, &perms, info, error);
586 }
587
588 int aa_umount(struct aa_label *label, struct vfsmount *mnt, int flags)
589 {
590         struct aa_profile *profile;
591         char *buffer = NULL;
592         int error;
593         struct path path = { .mnt = mnt, .dentry = mnt->mnt_root };
594
595         AA_BUG(!label);
596         AA_BUG(!mnt);
597
598         get_buffers(buffer);
599         error = fn_for_each_confined(label, profile,
600                         profile_umount(profile, &path, buffer));
601         put_buffers(buffer);
602
603         return error;
604 }
605
606 /* helper fn for transition on pivotroot
607  *
608  * Returns: label for transition or ERR_PTR. Does not return NULL
609  */
610 static struct aa_label *build_pivotroot(struct aa_profile *profile,
611                                         const struct path *new_path,
612                                         char *new_buffer,
613                                         const struct path *old_path,
614                                         char *old_buffer)
615 {
616         const char *old_name, *new_name = NULL, *info = NULL;
617         const char *trans_name = NULL;
618         struct aa_perms perms = { };
619         unsigned int state;
620         int error;
621
622         AA_BUG(!profile);
623         AA_BUG(!new_path);
624         AA_BUG(!old_path);
625
626         if (profile_unconfined(profile) ||
627             !PROFILE_MEDIATES(profile, AA_CLASS_MOUNT))
628                 return aa_get_newest_label(&profile->label);
629
630         error = aa_path_name(old_path, path_flags(profile, old_path),
631                              old_buffer, &old_name, &info,
632                              profile->disconnected);
633         if (error)
634                 goto audit;
635         error = aa_path_name(new_path, path_flags(profile, new_path),
636                              new_buffer, &new_name, &info,
637                              profile->disconnected);
638         if (error)
639                 goto audit;
640
641         error = -EACCES;
642         state = aa_dfa_match(profile->policy.dfa,
643                              profile->policy.start[AA_CLASS_MOUNT],
644                              new_name);
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);
648
649         if (AA_MAY_PIVOTROOT & perms.allow)
650                 error = 0;
651
652 audit:
653         error = audit_mount(profile, OP_PIVOTROOT, new_name, old_name,
654                             NULL, trans_name, 0, NULL, AA_MAY_PIVOTROOT,
655                             &perms, info, error);
656         if (error)
657                 return ERR_PTR(error);
658
659         return aa_get_newest_label(&profile->label);
660 }
661
662 int aa_pivotroot(struct aa_label *label, const struct path *old_path,
663                  const struct path *new_path)
664 {
665         struct aa_profile *profile;
666         struct aa_label *target = NULL;
667         char *old_buffer = NULL, *new_buffer = NULL, *info = NULL;
668         int error;
669
670         AA_BUG(!label);
671         AA_BUG(!old_path);
672         AA_BUG(!new_path);
673
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));
678         if (!target) {
679                 info = "label build failed";
680                 error = -ENOMEM;
681                 goto fail;
682         } else if (!IS_ERR(target)) {
683                 error = aa_replace_current_label(target);
684                 if (error) {
685                         /* TODO: audit target */
686                         aa_put_label(target);
687                         goto out;
688                 }
689         } else
690                 /* already audited error */
691                 error = PTR_ERR(target);
692 out:
693         put_buffers(old_buffer, new_buffer);
694
695         return error;
696
697 fail:
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 */,
701                                     NULL /* old_name */,
702                                     NULL, NULL,
703                                     0, NULL, AA_MAY_PIVOTROOT, &nullperms, info,
704                                     error));
705         goto out;
706 }