GNU Linux-libre 4.14.332-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         "failed perms check"
238 };
239
240 /*
241  * Returns 0 on success else element that match failed in, this is the
242  * index into the mnt_info_table above
243  */
244 static int do_match_mnt(struct aa_dfa *dfa, unsigned int start,
245                         const char *mntpnt, const char *devname,
246                         const char *type, unsigned long flags,
247                         void *data, bool binary, struct aa_perms *perms)
248 {
249         unsigned int state;
250
251         AA_BUG(!dfa);
252         AA_BUG(!perms);
253
254         state = aa_dfa_match(dfa, start, mntpnt);
255         state = aa_dfa_null_transition(dfa, state);
256         if (!state)
257                 return 1;
258
259         if (devname)
260                 state = aa_dfa_match(dfa, state, devname);
261         state = aa_dfa_null_transition(dfa, state);
262         if (!state)
263                 return 2;
264
265         if (type)
266                 state = aa_dfa_match(dfa, state, type);
267         state = aa_dfa_null_transition(dfa, state);
268         if (!state)
269                 return 3;
270
271         state = match_mnt_flags(dfa, state, flags);
272         if (!state)
273                 return 4;
274         *perms = compute_mnt_perms(dfa, state);
275         if (perms->allow & AA_MAY_MOUNT)
276                 return 0;
277
278         /* only match data if not binary and the DFA flags data is expected */
279         if (data && !binary && (perms->allow & AA_MNT_CONT_MATCH)) {
280                 state = aa_dfa_null_transition(dfa, state);
281                 if (!state)
282                         return 4;
283
284                 state = aa_dfa_match(dfa, state, data);
285                 if (!state)
286                         return 5;
287                 *perms = compute_mnt_perms(dfa, state);
288                 if (perms->allow & AA_MAY_MOUNT)
289                         return 0;
290         }
291
292         /* failed at perms check, don't confuse with flags match */
293         return 6;
294 }
295
296
297 static int path_flags(struct aa_profile *profile, const struct path *path)
298 {
299         AA_BUG(!profile);
300         AA_BUG(!path);
301
302         return profile->path_flags |
303                 (S_ISDIR(path->dentry->d_inode->i_mode) ? PATH_IS_DIR : 0);
304 }
305
306 /**
307  * match_mnt_path_str - handle path matching for mount
308  * @profile: the confining profile
309  * @mntpath: for the mntpnt (NOT NULL)
310  * @buffer: buffer to be used to lookup mntpath
311  * @devnme: string for the devname/src_name (MAY BE NULL OR ERRPTR)
312  * @type: string for the dev type (MAYBE NULL)
313  * @flags: mount flags to match
314  * @data: fs mount data (MAYBE NULL)
315  * @binary: whether @data is binary
316  * @devinfo: error str if (IS_ERR(@devname))
317  *
318  * Returns: 0 on success else error
319  */
320 static int match_mnt_path_str(struct aa_profile *profile,
321                               const struct path *mntpath, char *buffer,
322                               const char *devname, const char *type,
323                               unsigned long flags, void *data, bool binary,
324                               const char *devinfo)
325 {
326         struct aa_perms perms = { };
327         const char *mntpnt = NULL, *info = NULL;
328         int pos, error;
329
330         AA_BUG(!profile);
331         AA_BUG(!mntpath);
332         AA_BUG(!buffer);
333
334         if (!PROFILE_MEDIATES(profile, AA_CLASS_MOUNT))
335                 return 0;
336
337         error = aa_path_name(mntpath, path_flags(profile, mntpath), buffer,
338                              &mntpnt, &info, profile->disconnected);
339         if (error)
340                 goto audit;
341         if (IS_ERR(devname)) {
342                 error = PTR_ERR(devname);
343                 devname = NULL;
344                 info = devinfo;
345                 goto audit;
346         }
347
348         error = -EACCES;
349         pos = do_match_mnt(profile->policy.dfa,
350                            profile->policy.start[AA_CLASS_MOUNT],
351                            mntpnt, devname, type, flags, data, binary, &perms);
352         if (pos) {
353                 info = mnt_info_table[pos];
354                 goto audit;
355         }
356         error = 0;
357
358 audit:
359         return audit_mount(profile, OP_MOUNT, mntpnt, devname, type, NULL,
360                            flags, data, AA_MAY_MOUNT, &perms, info, error);
361 }
362
363 /**
364  * match_mnt - handle path matching for mount
365  * @profile: the confining profile
366  * @mntpath: for the mntpnt (NOT NULL)
367  * @buffer: buffer to be used to lookup mntpath
368  * @devpath: path devname/src_name (MAYBE NULL)
369  * @devbuffer: buffer to be used to lookup devname/src_name
370  * @type: string for the dev type (MAYBE NULL)
371  * @flags: mount flags to match
372  * @data: fs mount data (MAYBE NULL)
373  * @binary: whether @data is binary
374  *
375  * Returns: 0 on success else error
376  */
377 static int match_mnt(struct aa_profile *profile, const struct path *path,
378                      char *buffer, struct path *devpath, char *devbuffer,
379                      const char *type, unsigned long flags, void *data,
380                      bool binary)
381 {
382         const char *devname = NULL, *info = NULL;
383         int error = -EACCES;
384
385         AA_BUG(!profile);
386         AA_BUG(devpath && !devbuffer);
387
388         if (!PROFILE_MEDIATES(profile, AA_CLASS_MOUNT))
389                 return 0;
390
391         if (devpath) {
392                 error = aa_path_name(devpath, path_flags(profile, devpath),
393                                      devbuffer, &devname, &info,
394                                      profile->disconnected);
395                 if (error)
396                         devname = ERR_PTR(error);
397         }
398
399         return match_mnt_path_str(profile, path, buffer, devname, type, flags,
400                                   data, binary, info);
401 }
402
403 int aa_remount(struct aa_label *label, const struct path *path,
404                unsigned long flags, void *data)
405 {
406         struct aa_profile *profile;
407         char *buffer = NULL;
408         bool binary;
409         int error;
410
411         AA_BUG(!label);
412         AA_BUG(!path);
413
414         binary = path->dentry->d_sb->s_type->fs_flags & FS_BINARY_MOUNTDATA;
415
416         get_buffers(buffer);
417         error = fn_for_each_confined(label, profile,
418                         match_mnt(profile, path, buffer, NULL, NULL, NULL,
419                                   flags, data, binary));
420         put_buffers(buffer);
421
422         return error;
423 }
424
425 int aa_bind_mount(struct aa_label *label, const struct path *path,
426                   const char *dev_name, unsigned long flags)
427 {
428         struct aa_profile *profile;
429         char *buffer = NULL, *old_buffer = NULL;
430         struct path old_path;
431         int error;
432
433         AA_BUG(!label);
434         AA_BUG(!path);
435
436         if (!dev_name || !*dev_name)
437                 return -EINVAL;
438
439         flags &= MS_REC | MS_BIND;
440
441         error = kern_path(dev_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
442         if (error)
443                 return error;
444
445         get_buffers(buffer, old_buffer);
446         error = fn_for_each_confined(label, profile,
447                         match_mnt(profile, path, buffer, &old_path, old_buffer,
448                                   NULL, flags, NULL, false));
449         put_buffers(buffer, old_buffer);
450         path_put(&old_path);
451
452         return error;
453 }
454
455 int aa_mount_change_type(struct aa_label *label, const struct path *path,
456                          unsigned long flags)
457 {
458         struct aa_profile *profile;
459         char *buffer = NULL;
460         int error;
461
462         AA_BUG(!label);
463         AA_BUG(!path);
464
465         /* These are the flags allowed by do_change_type() */
466         flags &= (MS_REC | MS_SILENT | MS_SHARED | MS_PRIVATE | MS_SLAVE |
467                   MS_UNBINDABLE);
468
469         get_buffers(buffer);
470         error = fn_for_each_confined(label, profile,
471                         match_mnt(profile, path, buffer, NULL, NULL, NULL,
472                                   flags, NULL, false));
473         put_buffers(buffer);
474
475         return error;
476 }
477
478 int aa_move_mount(struct aa_label *label, const struct path *path,
479                   const char *orig_name)
480 {
481         struct aa_profile *profile;
482         char *buffer = NULL, *old_buffer = NULL;
483         struct path old_path;
484         int error;
485
486         AA_BUG(!label);
487         AA_BUG(!path);
488
489         if (!orig_name || !*orig_name)
490                 return -EINVAL;
491
492         error = kern_path(orig_name, LOOKUP_FOLLOW, &old_path);
493         if (error)
494                 return error;
495
496         get_buffers(buffer, old_buffer);
497         error = fn_for_each_confined(label, profile,
498                         match_mnt(profile, path, buffer, &old_path, old_buffer,
499                                   NULL, MS_MOVE, NULL, false));
500         put_buffers(buffer, old_buffer);
501         path_put(&old_path);
502
503         return error;
504 }
505
506 int aa_new_mount(struct aa_label *label, const char *dev_name,
507                  const struct path *path, const char *type, unsigned long flags,
508                  void *data)
509 {
510         struct aa_profile *profile;
511         char *buffer = NULL, *dev_buffer = NULL;
512         bool binary = true;
513         int error;
514         int requires_dev = 0;
515         struct path tmp_path, *dev_path = NULL;
516
517         AA_BUG(!label);
518         AA_BUG(!path);
519
520         if (type) {
521                 struct file_system_type *fstype;
522
523                 fstype = get_fs_type(type);
524                 if (!fstype)
525                         return -ENODEV;
526                 binary = fstype->fs_flags & FS_BINARY_MOUNTDATA;
527                 requires_dev = fstype->fs_flags & FS_REQUIRES_DEV;
528                 put_filesystem(fstype);
529
530                 if (requires_dev) {
531                         if (!dev_name || !*dev_name)
532                                 return -ENOENT;
533
534                         error = kern_path(dev_name, LOOKUP_FOLLOW, &tmp_path);
535                         if (error)
536                                 return error;
537                         dev_path = &tmp_path;
538                 }
539         }
540
541         get_buffers(buffer, dev_buffer);
542         if (dev_path) {
543                 error = fn_for_each_confined(label, profile,
544                         match_mnt(profile, path, buffer, dev_path, dev_buffer,
545                                   type, flags, data, binary));
546         } else {
547                 error = fn_for_each_confined(label, profile,
548                         match_mnt_path_str(profile, path, buffer, dev_name,
549                                            type, flags, data, binary, NULL));
550         }
551         put_buffers(buffer, dev_buffer);
552         if (dev_path)
553                 path_put(dev_path);
554
555         return error;
556 }
557
558 static int profile_umount(struct aa_profile *profile, struct path *path,
559                           char *buffer)
560 {
561         struct aa_perms perms = { };
562         const char *name = NULL, *info = NULL;
563         unsigned int state;
564         int error;
565
566         AA_BUG(!profile);
567         AA_BUG(!path);
568
569         if (!PROFILE_MEDIATES(profile, AA_CLASS_MOUNT))
570                 return 0;
571
572         error = aa_path_name(path, path_flags(profile, path), buffer, &name,
573                              &info, profile->disconnected);
574         if (error)
575                 goto audit;
576
577         state = aa_dfa_match(profile->policy.dfa,
578                              profile->policy.start[AA_CLASS_MOUNT],
579                              name);
580         perms = compute_mnt_perms(profile->policy.dfa, state);
581         if (AA_MAY_UMOUNT & ~perms.allow)
582                 error = -EACCES;
583
584 audit:
585         return audit_mount(profile, OP_UMOUNT, name, NULL, NULL, NULL, 0, NULL,
586                            AA_MAY_UMOUNT, &perms, info, error);
587 }
588
589 int aa_umount(struct aa_label *label, struct vfsmount *mnt, int flags)
590 {
591         struct aa_profile *profile;
592         char *buffer = NULL;
593         int error;
594         struct path path = { .mnt = mnt, .dentry = mnt->mnt_root };
595
596         AA_BUG(!label);
597         AA_BUG(!mnt);
598
599         get_buffers(buffer);
600         error = fn_for_each_confined(label, profile,
601                         profile_umount(profile, &path, buffer));
602         put_buffers(buffer);
603
604         return error;
605 }
606
607 /* helper fn for transition on pivotroot
608  *
609  * Returns: label for transition or ERR_PTR. Does not return NULL
610  */
611 static struct aa_label *build_pivotroot(struct aa_profile *profile,
612                                         const struct path *new_path,
613                                         char *new_buffer,
614                                         const struct path *old_path,
615                                         char *old_buffer)
616 {
617         const char *old_name, *new_name = NULL, *info = NULL;
618         const char *trans_name = NULL;
619         struct aa_perms perms = { };
620         unsigned int state;
621         int error;
622
623         AA_BUG(!profile);
624         AA_BUG(!new_path);
625         AA_BUG(!old_path);
626
627         if (profile_unconfined(profile) ||
628             !PROFILE_MEDIATES(profile, AA_CLASS_MOUNT))
629                 return aa_get_newest_label(&profile->label);
630
631         error = aa_path_name(old_path, path_flags(profile, old_path),
632                              old_buffer, &old_name, &info,
633                              profile->disconnected);
634         if (error)
635                 goto audit;
636         error = aa_path_name(new_path, path_flags(profile, new_path),
637                              new_buffer, &new_name, &info,
638                              profile->disconnected);
639         if (error)
640                 goto audit;
641
642         error = -EACCES;
643         state = aa_dfa_match(profile->policy.dfa,
644                              profile->policy.start[AA_CLASS_MOUNT],
645                              new_name);
646         state = aa_dfa_null_transition(profile->policy.dfa, state);
647         state = aa_dfa_match(profile->policy.dfa, state, old_name);
648         perms = compute_mnt_perms(profile->policy.dfa, state);
649
650         if (AA_MAY_PIVOTROOT & perms.allow)
651                 error = 0;
652
653 audit:
654         error = audit_mount(profile, OP_PIVOTROOT, new_name, old_name,
655                             NULL, trans_name, 0, NULL, AA_MAY_PIVOTROOT,
656                             &perms, info, error);
657         if (error)
658                 return ERR_PTR(error);
659
660         return aa_get_newest_label(&profile->label);
661 }
662
663 int aa_pivotroot(struct aa_label *label, const struct path *old_path,
664                  const struct path *new_path)
665 {
666         struct aa_profile *profile;
667         struct aa_label *target = NULL;
668         char *old_buffer = NULL, *new_buffer = NULL, *info = NULL;
669         int error;
670
671         AA_BUG(!label);
672         AA_BUG(!old_path);
673         AA_BUG(!new_path);
674
675         get_buffers(old_buffer, new_buffer);
676         target = fn_label_build(label, profile, GFP_ATOMIC,
677                         build_pivotroot(profile, new_path, new_buffer,
678                                         old_path, old_buffer));
679         if (!target) {
680                 info = "label build failed";
681                 error = -ENOMEM;
682                 goto fail;
683         } else if (!IS_ERR(target)) {
684                 error = aa_replace_current_label(target);
685                 if (error) {
686                         /* TODO: audit target */
687                         aa_put_label(target);
688                         goto out;
689                 }
690                 aa_put_label(target);
691         } else
692                 /* already audited error */
693                 error = PTR_ERR(target);
694 out:
695         put_buffers(old_buffer, new_buffer);
696
697         return error;
698
699 fail:
700         /* TODO: add back in auditing of new_name and old_name */
701         error = fn_for_each(label, profile,
702                         audit_mount(profile, OP_PIVOTROOT, NULL /*new_name */,
703                                     NULL /* old_name */,
704                                     NULL, NULL,
705                                     0, NULL, AA_MAY_PIVOTROOT, &nullperms, info,
706                                     error));
707         goto out;
708 }