]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - security/apparmor/file.c
apparmor: update aa_audit_file() to use labels
[mirror_ubuntu-bionic-kernel.git] / security / apparmor / file.c
CommitLineData
6380bd8d
JJ
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-2010 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
192ca6b5
JJ
15#include <linux/tty.h>
16#include <linux/fdtable.h>
17#include <linux/file.h>
18
6380bd8d
JJ
19#include "include/apparmor.h"
20#include "include/audit.h"
192ca6b5 21#include "include/context.h"
6380bd8d
JJ
22#include "include/file.h"
23#include "include/match.h"
24#include "include/path.h"
25#include "include/policy.h"
190a9518 26#include "include/label.h"
6380bd8d 27
e53cfe6c
JJ
28static u32 map_mask_to_chr_mask(u32 mask)
29{
30 u32 m = mask & PERMS_CHRS_MASK;
31
32 if (mask & AA_MAY_GETATTR)
33 m |= MAY_READ;
34 if (mask & (AA_MAY_SETATTR | AA_MAY_CHMOD | AA_MAY_CHOWN))
35 m |= MAY_WRITE;
36
37 return m;
38}
6380bd8d
JJ
39
40/**
41 * audit_file_mask - convert mask to permission string
42 * @buffer: buffer to write string to (NOT NULL)
43 * @mask: permission mask to convert
44 */
45static void audit_file_mask(struct audit_buffer *ab, u32 mask)
46{
47 char str[10];
48
e53cfe6c 49 aa_perm_mask_to_str(str, aa_file_perm_chrs, map_mask_to_chr_mask(mask));
6380bd8d
JJ
50 audit_log_string(ab, str);
51}
52
53/**
54 * file_audit_cb - call back for file specific audit fields
55 * @ab: audit_buffer (NOT NULL)
56 * @va: audit struct to audit values of (NOT NULL)
57 */
58static void file_audit_cb(struct audit_buffer *ab, void *va)
59{
60 struct common_audit_data *sa = va;
2db81452 61 kuid_t fsuid = current_fsuid();
6380bd8d 62
aa9aeea8 63 if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
6380bd8d 64 audit_log_format(ab, " requested_mask=");
aa9aeea8 65 audit_file_mask(ab, aad(sa)->request);
6380bd8d 66 }
aa9aeea8 67 if (aad(sa)->denied & AA_AUDIT_FILE_MASK) {
6380bd8d 68 audit_log_format(ab, " denied_mask=");
aa9aeea8 69 audit_file_mask(ab, aad(sa)->denied);
6380bd8d 70 }
aa9aeea8 71 if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
2db81452
EB
72 audit_log_format(ab, " fsuid=%d",
73 from_kuid(&init_user_ns, fsuid));
74 audit_log_format(ab, " ouid=%d",
ef88a7ac 75 from_kuid(&init_user_ns, aad(sa)->fs.ouid));
6380bd8d
JJ
76 }
77
98c3d182
JJ
78 if (aad(sa)->peer) {
79 audit_log_format(ab, " target=");
80 aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
81 FLAG_VIEW_SUBNS, GFP_ATOMIC);
82 } else if (aad(sa)->fs.target) {
6380bd8d 83 audit_log_format(ab, " target=");
ef88a7ac 84 audit_log_untrustedstring(ab, aad(sa)->fs.target);
6380bd8d
JJ
85 }
86}
87
88/**
89 * aa_audit_file - handle the auditing of file operations
90 * @profile: the profile being enforced (NOT NULL)
91 * @perms: the permissions computed for the request (NOT NULL)
6380bd8d
JJ
92 * @op: operation being mediated
93 * @request: permissions requested
94 * @name: name of object being mediated (MAYBE NULL)
95 * @target: name of target (MAYBE NULL)
98c3d182 96 * @tlabel: target label (MAY BE NULL)
6380bd8d
JJ
97 * @ouid: object uid
98 * @info: extra information message (MAYBE NULL)
99 * @error: 0 if operation allowed else failure error code
100 *
101 * Returns: %0 or error on failure
102 */
2d679f3c 103int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms,
ef88a7ac 104 const char *op, u32 request, const char *name,
98c3d182
JJ
105 const char *target, struct aa_label *tlabel,
106 kuid_t ouid, const char *info, int error)
6380bd8d
JJ
107{
108 int type = AUDIT_APPARMOR_AUTO;
ef88a7ac
JJ
109 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_TASK, op);
110
111 sa.u.tsk = NULL;
aa9aeea8 112 aad(&sa)->request = request;
ef88a7ac
JJ
113 aad(&sa)->name = name;
114 aad(&sa)->fs.target = target;
98c3d182 115 aad(&sa)->peer = tlabel;
ef88a7ac
JJ
116 aad(&sa)->fs.ouid = ouid;
117 aad(&sa)->info = info;
118 aad(&sa)->error = error;
b6b1b81b 119 sa.u.tsk = NULL;
ef88a7ac
JJ
120
121 if (likely(!aad(&sa)->error)) {
6380bd8d
JJ
122 u32 mask = perms->audit;
123
124 if (unlikely(AUDIT_MODE(profile) == AUDIT_ALL))
125 mask = 0xffff;
126
127 /* mask off perms that are not being force audited */
aa9aeea8 128 aad(&sa)->request &= mask;
6380bd8d 129
aa9aeea8 130 if (likely(!aad(&sa)->request))
6380bd8d
JJ
131 return 0;
132 type = AUDIT_APPARMOR_AUDIT;
133 } else {
134 /* only report permissions that were denied */
aa9aeea8
JJ
135 aad(&sa)->request = aad(&sa)->request & ~perms->allow;
136 AA_BUG(!aad(&sa)->request);
6380bd8d 137
aa9aeea8 138 if (aad(&sa)->request & perms->kill)
6380bd8d
JJ
139 type = AUDIT_APPARMOR_KILL;
140
141 /* quiet known rejects, assumes quiet and kill do not overlap */
aa9aeea8 142 if ((aad(&sa)->request & perms->quiet) &&
6380bd8d
JJ
143 AUDIT_MODE(profile) != AUDIT_NOQUIET &&
144 AUDIT_MODE(profile) != AUDIT_ALL)
aa9aeea8 145 aad(&sa)->request &= ~perms->quiet;
6380bd8d 146
aa9aeea8 147 if (!aad(&sa)->request)
98c3d182 148 return aad(&sa)->error;
6380bd8d
JJ
149 }
150
aa9aeea8 151 aad(&sa)->denied = aad(&sa)->request & ~perms->allow;
ef88a7ac 152 return aa_audit(type, profile, &sa, file_audit_cb);
6380bd8d
JJ
153}
154
155/**
156 * map_old_perms - map old file perms layout to the new layout
157 * @old: permission set in old mapping
158 *
159 * Returns: new permission mapping
160 */
161static u32 map_old_perms(u32 old)
162{
163 u32 new = old & 0xf;
164 if (old & MAY_READ)
e53cfe6c 165 new |= AA_MAY_GETATTR | AA_MAY_OPEN;
6380bd8d 166 if (old & MAY_WRITE)
e53cfe6c
JJ
167 new |= AA_MAY_SETATTR | AA_MAY_CREATE | AA_MAY_DELETE |
168 AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_OPEN;
6380bd8d
JJ
169 if (old & 0x10)
170 new |= AA_MAY_LINK;
171 /* the old mapping lock and link_subset flags where overlaid
172 * and use was determined by part of a pair that they were in
173 */
174 if (old & 0x20)
175 new |= AA_MAY_LOCK | AA_LINK_SUBSET;
176 if (old & 0x40) /* AA_EXEC_MMAP */
177 new |= AA_EXEC_MMAP;
178
6380bd8d
JJ
179 return new;
180}
181
182/**
2d679f3c 183 * aa_compute_fperms - convert dfa compressed perms to internal perms
6380bd8d
JJ
184 * @dfa: dfa to compute perms for (NOT NULL)
185 * @state: state in dfa
186 * @cond: conditions to consider (NOT NULL)
187 *
188 * TODO: convert from dfa + state to permission entry, do computation conversion
189 * at load time.
190 *
191 * Returns: computed permission set
192 */
2d679f3c
JJ
193struct aa_perms aa_compute_fperms(struct aa_dfa *dfa, unsigned int state,
194 struct path_cond *cond)
6380bd8d 195{
2d679f3c 196 struct aa_perms perms;
6380bd8d
JJ
197
198 /* FIXME: change over to new dfa format
199 * currently file perms are encoded in the dfa, new format
200 * splits the permissions from the dfa. This mapping can be
201 * done at profile load
202 */
2d679f3c
JJ
203 perms.deny = 0;
204 perms.kill = perms.stop = 0;
205 perms.complain = perms.cond = 0;
206 perms.hide = 0;
207 perms.prompt = 0;
6380bd8d 208
2db81452 209 if (uid_eq(current_fsuid(), cond->uid)) {
6380bd8d
JJ
210 perms.allow = map_old_perms(dfa_user_allow(dfa, state));
211 perms.audit = map_old_perms(dfa_user_audit(dfa, state));
212 perms.quiet = map_old_perms(dfa_user_quiet(dfa, state));
213 perms.xindex = dfa_user_xindex(dfa, state);
214 } else {
215 perms.allow = map_old_perms(dfa_other_allow(dfa, state));
216 perms.audit = map_old_perms(dfa_other_audit(dfa, state));
217 perms.quiet = map_old_perms(dfa_other_quiet(dfa, state));
218 perms.xindex = dfa_other_xindex(dfa, state);
219 }
e53cfe6c 220 perms.allow |= AA_MAY_GETATTR;
6380bd8d
JJ
221
222 /* change_profile wasn't determined by ownership in old mapping */
223 if (ACCEPT_TABLE(dfa)[state] & 0x80000000)
224 perms.allow |= AA_MAY_CHANGE_PROFILE;
0421ea91
JJ
225 if (ACCEPT_TABLE(dfa)[state] & 0x40000000)
226 perms.allow |= AA_MAY_ONEXEC;
6380bd8d
JJ
227
228 return perms;
229}
230
231/**
232 * aa_str_perms - find permission that match @name
233 * @dfa: to match against (MAYBE NULL)
234 * @state: state to start matching in
235 * @name: string to match against dfa (NOT NULL)
236 * @cond: conditions to consider for permission set computation (NOT NULL)
237 * @perms: Returns - the permissions found when matching @name
238 *
239 * Returns: the final state in @dfa when beginning @start and walking @name
240 */
241unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
242 const char *name, struct path_cond *cond,
2d679f3c 243 struct aa_perms *perms)
6380bd8d
JJ
244{
245 unsigned int state;
6380bd8d 246 state = aa_dfa_match(dfa, start, name);
2d679f3c 247 *perms = aa_compute_fperms(dfa, state, cond);
6380bd8d
JJ
248
249 return state;
250}
251
252/**
253 * is_deleted - test if a file has been completely unlinked
254 * @dentry: dentry of file to test for deletion (NOT NULL)
255 *
256 * Returns: %1 if deleted else %0
257 */
258static inline bool is_deleted(struct dentry *dentry)
259{
c6f493d6 260 if (d_unlinked(dentry) && d_backing_inode(dentry)->i_nlink == 0)
6380bd8d
JJ
261 return 1;
262 return 0;
263}
264
265/**
266 * aa_path_perm - do permissions check & audit for @path
267 * @op: operation being checked
268 * @profile: profile being enforced (NOT NULL)
269 * @path: path to check permissions of (NOT NULL)
270 * @flags: any additional path flags beyond what the profile specifies
271 * @request: requested permissions
272 * @cond: conditional info for this request (NOT NULL)
273 *
274 * Returns: %0 else error if access denied or other error
275 */
47f6e5cc
JJ
276int aa_path_perm(const char *op, struct aa_profile *profile,
277 const struct path *path, int flags, u32 request,
278 struct path_cond *cond)
6380bd8d
JJ
279{
280 char *buffer = NULL;
2d679f3c 281 struct aa_perms perms = {};
6380bd8d
JJ
282 const char *name, *info = NULL;
283 int error;
284
285 flags |= profile->path_flags | (S_ISDIR(cond->mode) ? PATH_IS_DIR : 0);
4227c333
JJ
286 get_buffers(buffer);
287 error = aa_path_name(path, flags, buffer, &name, &info,
72c8a768 288 profile->disconnected);
6380bd8d
JJ
289 if (error) {
290 if (error == -ENOENT && is_deleted(path->dentry)) {
291 /* Access to open files that are deleted are
292 * give a pass (implicit delegation)
293 */
294 error = 0;
57fa1e18 295 info = NULL;
6380bd8d 296 perms.allow = request;
57fa1e18 297 }
6380bd8d
JJ
298 } else {
299 aa_str_perms(profile->file.dfa, profile->file.start, name, cond,
300 &perms);
301 if (request & ~perms.allow)
302 error = -EACCES;
303 }
98c3d182 304 error = aa_audit_file(profile, &perms, op, request, name, NULL, NULL,
ef88a7ac 305 cond->uid, info, error);
4227c333 306 put_buffers(buffer);
6380bd8d
JJ
307
308 return error;
309}
310
311/**
312 * xindex_is_subset - helper for aa_path_link
313 * @link: link permission set
314 * @target: target permission set
315 *
316 * test target x permissions are equal OR a subset of link x permissions
317 * this is done as part of the subset test, where a hardlink must have
318 * a subset of permissions that the target has.
319 *
320 * Returns: %1 if subset else %0
321 */
322static inline bool xindex_is_subset(u32 link, u32 target)
323{
324 if (((link & ~AA_X_UNSAFE) != (target & ~AA_X_UNSAFE)) ||
325 ((link & AA_X_UNSAFE) && !(target & AA_X_UNSAFE)))
326 return 0;
327
328 return 1;
329}
330
331/**
332 * aa_path_link - Handle hard link permission check
333 * @profile: the profile being enforced (NOT NULL)
334 * @old_dentry: the target dentry (NOT NULL)
335 * @new_dir: directory the new link will be created in (NOT NULL)
336 * @new_dentry: the link being created (NOT NULL)
337 *
338 * Handle the permission test for a link & target pair. Permission
339 * is encoded as a pair where the link permission is determined
340 * first, and if allowed, the target is tested. The target test
341 * is done from the point of the link match (not start of DFA)
342 * making the target permission dependent on the link permission match.
343 *
344 * The subset test if required forces that permissions granted
345 * on link are a subset of the permission granted to target.
346 *
347 * Returns: %0 if allowed else error
348 */
349int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry,
3539aaf6 350 const struct path *new_dir, struct dentry *new_dentry)
6380bd8d 351{
8486adf0
KC
352 struct path link = { .mnt = new_dir->mnt, .dentry = new_dentry };
353 struct path target = { .mnt = new_dir->mnt, .dentry = old_dentry };
6380bd8d 354 struct path_cond cond = {
c6f493d6
DH
355 d_backing_inode(old_dentry)->i_uid,
356 d_backing_inode(old_dentry)->i_mode
6380bd8d
JJ
357 };
358 char *buffer = NULL, *buffer2 = NULL;
359 const char *lname, *tname = NULL, *info = NULL;
2d679f3c 360 struct aa_perms lperms, perms;
6380bd8d
JJ
361 u32 request = AA_MAY_LINK;
362 unsigned int state;
363 int error;
364
4227c333 365 get_buffers(buffer, buffer2);
6380bd8d
JJ
366 lperms = nullperms;
367
368 /* buffer freed below, lname is pointer in buffer */
4227c333 369 error = aa_path_name(&link, profile->path_flags, buffer, &lname,
72c8a768 370 &info, profile->disconnected);
6380bd8d
JJ
371 if (error)
372 goto audit;
373
374 /* buffer2 freed below, tname is pointer in buffer2 */
4227c333 375 error = aa_path_name(&target, profile->path_flags, buffer2, &tname,
72c8a768 376 &info, profile->disconnected);
6380bd8d
JJ
377 if (error)
378 goto audit;
379
380 error = -EACCES;
381 /* aa_str_perms - handles the case of the dfa being NULL */
382 state = aa_str_perms(profile->file.dfa, profile->file.start, lname,
383 &cond, &lperms);
384
385 if (!(lperms.allow & AA_MAY_LINK))
386 goto audit;
387
388 /* test to see if target can be paired with link */
389 state = aa_dfa_null_transition(profile->file.dfa, state);
390 aa_str_perms(profile->file.dfa, state, tname, &cond, &perms);
391
392 /* force audit/quiet masks for link are stored in the second entry
393 * in the link pair.
394 */
395 lperms.audit = perms.audit;
396 lperms.quiet = perms.quiet;
397 lperms.kill = perms.kill;
398
399 if (!(perms.allow & AA_MAY_LINK)) {
400 info = "target restricted";
401 goto audit;
402 }
403
404 /* done if link subset test is not required */
405 if (!(perms.allow & AA_LINK_SUBSET))
406 goto done_tests;
407
408 /* Do link perm subset test requiring allowed permission on link are a
409 * subset of the allowed permissions on target.
410 */
411 aa_str_perms(profile->file.dfa, profile->file.start, tname, &cond,
412 &perms);
413
414 /* AA_MAY_LINK is not considered in the subset test */
415 request = lperms.allow & ~AA_MAY_LINK;
416 lperms.allow &= perms.allow | AA_MAY_LINK;
417
418 request |= AA_AUDIT_FILE_MASK & (lperms.allow & ~perms.allow);
419 if (request & ~lperms.allow) {
420 goto audit;
421 } else if ((lperms.allow & MAY_EXEC) &&
422 !xindex_is_subset(lperms.xindex, perms.xindex)) {
423 lperms.allow &= ~MAY_EXEC;
424 request |= MAY_EXEC;
425 info = "link not subset of target";
426 goto audit;
427 }
428
429done_tests:
430 error = 0;
431
432audit:
ef88a7ac 433 error = aa_audit_file(profile, &lperms, OP_LINK, request,
98c3d182 434 lname, tname, NULL, cond.uid, info, error);
4227c333 435 put_buffers(buffer, buffer2);
6380bd8d
JJ
436
437 return error;
438}
439
440/**
441 * aa_file_perm - do permission revalidation check & audit for @file
442 * @op: operation being checked
190a9518 443 * @label: label being enforced (NOT NULL)
6380bd8d
JJ
444 * @file: file to revalidate access permissions on (NOT NULL)
445 * @request: requested permissions
446 *
447 * Returns: %0 if access allowed else error
448 */
190a9518 449int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
6380bd8d
JJ
450 u32 request)
451{
452 struct path_cond cond = {
496ad9aa
AV
453 .uid = file_inode(file)->i_uid,
454 .mode = file_inode(file)->i_mode
6380bd8d 455 };
190a9518
JJ
456 struct aa_file_ctx *fctx;
457 struct aa_label *flabel;
458 u32 denied;
459 int error = 0;
460
461 AA_BUG(!label);
462 AA_BUG(!file);
463
464 fctx = file_ctx(file);
465
466 rcu_read_lock();
467 flabel = rcu_dereference(fctx->label);
468 AA_BUG(!flabel);
469
470 /* revalidate access, if task is unconfined, or the cached cred
471 * doesn't match or if the request is for more permissions than
472 * was granted.
473 *
474 * Note: the test for !unconfined(flabel) is to handle file
475 * delegation from unconfined tasks
476 */
477 denied = request & ~fctx->allow;
478 if (unconfined(label) || unconfined(flabel) ||
479 (!denied && aa_label_is_subset(flabel, label)))
480 goto done;
481
482 /* TODO: label cross check */
483
484 if (file->f_path.mnt && path_mediated_fs(file->f_path.dentry))
485 error = aa_path_perm(op, labels_profile(label), &file->f_path,
486 PATH_DELEGATE_DELETED, request, &cond);
6380bd8d 487
190a9518
JJ
488done:
489 rcu_read_unlock();
490
491 return error;
6380bd8d 492}
192ca6b5 493
637f688d 494static void revalidate_tty(struct aa_label *label)
192ca6b5
JJ
495{
496 struct tty_struct *tty;
497 int drop_tty = 0;
498
499 tty = get_current_tty();
500 if (!tty)
501 return;
502
503 spin_lock(&tty->files_lock);
504 if (!list_empty(&tty->tty_files)) {
505 struct tty_file_private *file_priv;
506 struct file *file;
507 /* TODO: Revalidate access to controlling tty. */
508 file_priv = list_first_entry(&tty->tty_files,
509 struct tty_file_private, list);
510 file = file_priv->file;
511
190a9518 512 if (aa_file_perm(OP_INHERIT, label, file, MAY_READ | MAY_WRITE))
192ca6b5
JJ
513 drop_tty = 1;
514 }
515 spin_unlock(&tty->files_lock);
516 tty_kref_put(tty);
517
518 if (drop_tty)
519 no_tty();
520}
521
522static int match_file(const void *p, struct file *file, unsigned int fd)
523{
637f688d 524 struct aa_label *label = (struct aa_label *)p;
192ca6b5 525
190a9518 526 if (aa_file_perm(OP_INHERIT, label, file, aa_map_file_to_perms(file)))
192ca6b5
JJ
527 return fd + 1;
528 return 0;
529}
530
531
532/* based on selinux's flush_unauthorized_files */
533void aa_inherit_files(const struct cred *cred, struct files_struct *files)
534{
637f688d 535 struct aa_label *label = aa_get_newest_cred_label(cred);
192ca6b5
JJ
536 struct file *devnull = NULL;
537 unsigned int n;
538
637f688d 539 revalidate_tty(label);
192ca6b5
JJ
540
541 /* Revalidate access to inherited open files. */
637f688d 542 n = iterate_fd(files, 0, match_file, label);
192ca6b5
JJ
543 if (!n) /* none found? */
544 goto out;
545
546 devnull = dentry_open(&aa_null, O_RDWR, cred);
547 if (IS_ERR(devnull))
548 devnull = NULL;
549 /* replace all the matching ones with this */
550 do {
551 replace_fd(n - 1, devnull, 0);
637f688d 552 } while ((n = iterate_fd(files, n, match_file, label)) != 0);
192ca6b5
JJ
553 if (devnull)
554 fput(devnull);
555out:
637f688d 556 aa_put_label(label);
192ca6b5 557}