blob: a40bc1e276dcef897049b21995d7f82b16096c61 [file] [log] [blame]
John Johansen6380bd82010-07-29 14:48:04 -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-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
John Johansen192ca6b2017-06-09 11:58:42 -070015#include <linux/tty.h>
16#include <linux/fdtable.h>
17#include <linux/file.h>
18
John Johansen6380bd82010-07-29 14:48:04 -070019#include "include/apparmor.h"
20#include "include/audit.h"
John Johansen192ca6b2017-06-09 11:58:42 -070021#include "include/context.h"
John Johansen6380bd82010-07-29 14:48:04 -070022#include "include/file.h"
23#include "include/match.h"
24#include "include/path.h"
25#include "include/policy.h"
John Johansen190a9512017-06-09 14:59:51 -070026#include "include/label.h"
John Johansen6380bd82010-07-29 14:48:04 -070027
John Johansene53cfe62017-05-26 15:07:22 -070028static 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}
John Johansen6380bd82010-07-29 14:48:04 -070039
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
John Johansene53cfe62017-05-26 15:07:22 -070049 aa_perm_mask_to_str(str, aa_file_perm_chrs, map_mask_to_chr_mask(mask));
John Johansen6380bd82010-07-29 14:48:04 -070050 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;
Eric W. Biederman2db81452012-02-07 16:33:13 -080061 kuid_t fsuid = current_fsuid();
John Johansen6380bd82010-07-29 14:48:04 -070062
John Johansenaa9aeea2017-05-29 12:16:04 -070063 if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
John Johansen6380bd82010-07-29 14:48:04 -070064 audit_log_format(ab, " requested_mask=");
John Johansenaa9aeea2017-05-29 12:16:04 -070065 audit_file_mask(ab, aad(sa)->request);
John Johansen6380bd82010-07-29 14:48:04 -070066 }
John Johansenaa9aeea2017-05-29 12:16:04 -070067 if (aad(sa)->denied & AA_AUDIT_FILE_MASK) {
John Johansen6380bd82010-07-29 14:48:04 -070068 audit_log_format(ab, " denied_mask=");
John Johansenaa9aeea2017-05-29 12:16:04 -070069 audit_file_mask(ab, aad(sa)->denied);
John Johansen6380bd82010-07-29 14:48:04 -070070 }
John Johansenaa9aeea2017-05-29 12:16:04 -070071 if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
Eric W. Biederman2db81452012-02-07 16:33:13 -080072 audit_log_format(ab, " fsuid=%d",
73 from_kuid(&init_user_ns, fsuid));
74 audit_log_format(ab, " ouid=%d",
John Johansenef88a7a2017-01-16 00:43:02 -080075 from_kuid(&init_user_ns, aad(sa)->fs.ouid));
John Johansen6380bd82010-07-29 14:48:04 -070076 }
77
John Johansen98c3d182017-06-09 15:48:20 -070078 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) {
John Johansen6380bd82010-07-29 14:48:04 -070083 audit_log_format(ab, " target=");
John Johansenef88a7a2017-01-16 00:43:02 -080084 audit_log_untrustedstring(ab, aad(sa)->fs.target);
John Johansen6380bd82010-07-29 14:48:04 -070085 }
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)
John Johansen6380bd82010-07-29 14:48:04 -070092 * @op: operation being mediated
93 * @request: permissions requested
94 * @name: name of object being mediated (MAYBE NULL)
95 * @target: name of target (MAYBE NULL)
John Johansen98c3d182017-06-09 15:48:20 -070096 * @tlabel: target label (MAY BE NULL)
John Johansen6380bd82010-07-29 14:48:04 -070097 * @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 */
John Johansen2d679f32017-05-29 12:19:39 -0700103int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms,
John Johansenef88a7a2017-01-16 00:43:02 -0800104 const char *op, u32 request, const char *name,
John Johansen98c3d182017-06-09 15:48:20 -0700105 const char *target, struct aa_label *tlabel,
106 kuid_t ouid, const char *info, int error)
John Johansen6380bd82010-07-29 14:48:04 -0700107{
108 int type = AUDIT_APPARMOR_AUTO;
John Johansenef88a7a2017-01-16 00:43:02 -0800109 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_TASK, op);
John Johansen6380bd82010-07-29 14:48:04 -0700110
John Johansenef88a7a2017-01-16 00:43:02 -0800111 sa.u.tsk = NULL;
John Johansenaa9aeea2017-05-29 12:16:04 -0700112 aad(&sa)->request = request;
John Johansenef88a7a2017-01-16 00:43:02 -0800113 aad(&sa)->name = name;
114 aad(&sa)->fs.target = target;
John Johansen98c3d182017-06-09 15:48:20 -0700115 aad(&sa)->peer = tlabel;
John Johansenef88a7a2017-01-16 00:43:02 -0800116 aad(&sa)->fs.ouid = ouid;
117 aad(&sa)->info = info;
118 aad(&sa)->error = error;
119 sa.u.tsk = NULL;
120
121 if (likely(!aad(&sa)->error)) {
John Johansen6380bd82010-07-29 14:48:04 -0700122 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 */
John Johansenaa9aeea2017-05-29 12:16:04 -0700128 aad(&sa)->request &= mask;
John Johansen6380bd82010-07-29 14:48:04 -0700129
John Johansenaa9aeea2017-05-29 12:16:04 -0700130 if (likely(!aad(&sa)->request))
John Johansen6380bd82010-07-29 14:48:04 -0700131 return 0;
132 type = AUDIT_APPARMOR_AUDIT;
133 } else {
134 /* only report permissions that were denied */
John Johansenaa9aeea2017-05-29 12:16:04 -0700135 aad(&sa)->request = aad(&sa)->request & ~perms->allow;
136 AA_BUG(!aad(&sa)->request);
John Johansen6380bd82010-07-29 14:48:04 -0700137
John Johansenaa9aeea2017-05-29 12:16:04 -0700138 if (aad(&sa)->request & perms->kill)
John Johansen6380bd82010-07-29 14:48:04 -0700139 type = AUDIT_APPARMOR_KILL;
140
141 /* quiet known rejects, assumes quiet and kill do not overlap */
John Johansenaa9aeea2017-05-29 12:16:04 -0700142 if ((aad(&sa)->request & perms->quiet) &&
John Johansen6380bd82010-07-29 14:48:04 -0700143 AUDIT_MODE(profile) != AUDIT_NOQUIET &&
144 AUDIT_MODE(profile) != AUDIT_ALL)
John Johansenaa9aeea2017-05-29 12:16:04 -0700145 aad(&sa)->request &= ~perms->quiet;
John Johansen6380bd82010-07-29 14:48:04 -0700146
John Johansenaa9aeea2017-05-29 12:16:04 -0700147 if (!aad(&sa)->request)
John Johansen98c3d182017-06-09 15:48:20 -0700148 return aad(&sa)->error;
John Johansen6380bd82010-07-29 14:48:04 -0700149 }
150
John Johansenaa9aeea2017-05-29 12:16:04 -0700151 aad(&sa)->denied = aad(&sa)->request & ~perms->allow;
John Johansenef88a7a2017-01-16 00:43:02 -0800152 return aa_audit(type, profile, &sa, file_audit_cb);
John Johansen6380bd82010-07-29 14:48:04 -0700153}
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)
John Johansene53cfe62017-05-26 15:07:22 -0700165 new |= AA_MAY_GETATTR | AA_MAY_OPEN;
John Johansen6380bd82010-07-29 14:48:04 -0700166 if (old & MAY_WRITE)
John Johansene53cfe62017-05-26 15:07:22 -0700167 new |= AA_MAY_SETATTR | AA_MAY_CREATE | AA_MAY_DELETE |
168 AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_OPEN;
John Johansen6380bd82010-07-29 14:48:04 -0700169 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
John Johansen6380bd82010-07-29 14:48:04 -0700179 return new;
180}
181
182/**
John Johansen2d679f32017-05-29 12:19:39 -0700183 * aa_compute_fperms - convert dfa compressed perms to internal perms
John Johansen6380bd82010-07-29 14:48:04 -0700184 * @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 */
John Johansen2d679f32017-05-29 12:19:39 -0700193struct aa_perms aa_compute_fperms(struct aa_dfa *dfa, unsigned int state,
194 struct path_cond *cond)
John Johansen6380bd82010-07-29 14:48:04 -0700195{
John Johansen2d679f32017-05-29 12:19:39 -0700196 struct aa_perms perms;
John Johansen6380bd82010-07-29 14:48:04 -0700197
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 */
John Johansen2d679f32017-05-29 12:19:39 -0700203 perms.deny = 0;
204 perms.kill = perms.stop = 0;
205 perms.complain = perms.cond = 0;
206 perms.hide = 0;
207 perms.prompt = 0;
John Johansen6380bd82010-07-29 14:48:04 -0700208
Eric W. Biederman2db81452012-02-07 16:33:13 -0800209 if (uid_eq(current_fsuid(), cond->uid)) {
John Johansen6380bd82010-07-29 14:48:04 -0700210 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 }
John Johansene53cfe62017-05-26 15:07:22 -0700220 perms.allow |= AA_MAY_GETATTR;
John Johansen6380bd82010-07-29 14:48:04 -0700221
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;
John Johansen0421ea92012-03-27 04:14:33 -0700225 if (ACCEPT_TABLE(dfa)[state] & 0x40000000)
226 perms.allow |= AA_MAY_ONEXEC;
John Johansen6380bd82010-07-29 14:48:04 -0700227
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,
John Johansen2d679f32017-05-29 12:19:39 -0700243 struct aa_perms *perms)
John Johansen6380bd82010-07-29 14:48:04 -0700244{
245 unsigned int state;
John Johansen6380bd82010-07-29 14:48:04 -0700246 state = aa_dfa_match(dfa, start, name);
John Johansen2d679f32017-05-29 12:19:39 -0700247 *perms = aa_compute_fperms(dfa, state, cond);
John Johansen6380bd82010-07-29 14:48:04 -0700248
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{
David Howellsc6f493d2015-03-17 22:26:22 +0000260 if (d_unlinked(dentry) && d_backing_inode(dentry)->i_nlink == 0)
John Johansen6380bd82010-07-29 14:48:04 -0700261 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 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800276int aa_path_perm(const char *op, struct aa_profile *profile,
277 const struct path *path, int flags, u32 request,
278 struct path_cond *cond)
John Johansen6380bd82010-07-29 14:48:04 -0700279{
280 char *buffer = NULL;
John Johansen2d679f32017-05-29 12:19:39 -0700281 struct aa_perms perms = {};
John Johansen6380bd82010-07-29 14:48:04 -0700282 const char *name, *info = NULL;
283 int error;
284
285 flags |= profile->path_flags | (S_ISDIR(cond->mode) ? PATH_IS_DIR : 0);
John Johansen4227c332017-05-23 03:25:14 -0700286 get_buffers(buffer);
287 error = aa_path_name(path, flags, buffer, &name, &info,
John Johansen72c8a762017-05-22 03:06:52 -0700288 profile->disconnected);
John Johansen6380bd82010-07-29 14:48:04 -0700289 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;
John Johansen57fa1e12012-02-16 06:20:33 -0800295 info = NULL;
John Johansen6380bd82010-07-29 14:48:04 -0700296 perms.allow = request;
John Johansen57fa1e12012-02-16 06:20:33 -0800297 }
John Johansen6380bd82010-07-29 14:48:04 -0700298 } else {
299 aa_str_perms(profile->file.dfa, profile->file.start, name, cond,
300 &perms);
301 if (request & ~perms.allow)
302 error = -EACCES;
303 }
John Johansen98c3d182017-06-09 15:48:20 -0700304 error = aa_audit_file(profile, &perms, op, request, name, NULL, NULL,
John Johansenef88a7a2017-01-16 00:43:02 -0800305 cond->uid, info, error);
John Johansen4227c332017-05-23 03:25:14 -0700306 put_buffers(buffer);
John Johansen6380bd82010-07-29 14:48:04 -0700307
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,
Al Viro3539aaf2016-03-25 15:07:03 -0400350 const struct path *new_dir, struct dentry *new_dentry)
John Johansen6380bd82010-07-29 14:48:04 -0700351{
Kees Cook8486adf2016-12-16 17:04:13 -0800352 struct path link = { .mnt = new_dir->mnt, .dentry = new_dentry };
353 struct path target = { .mnt = new_dir->mnt, .dentry = old_dentry };
John Johansen6380bd82010-07-29 14:48:04 -0700354 struct path_cond cond = {
David Howellsc6f493d2015-03-17 22:26:22 +0000355 d_backing_inode(old_dentry)->i_uid,
356 d_backing_inode(old_dentry)->i_mode
John Johansen6380bd82010-07-29 14:48:04 -0700357 };
358 char *buffer = NULL, *buffer2 = NULL;
359 const char *lname, *tname = NULL, *info = NULL;
John Johansen2d679f32017-05-29 12:19:39 -0700360 struct aa_perms lperms, perms;
John Johansen6380bd82010-07-29 14:48:04 -0700361 u32 request = AA_MAY_LINK;
362 unsigned int state;
363 int error;
364
John Johansen4227c332017-05-23 03:25:14 -0700365 get_buffers(buffer, buffer2);
John Johansen6380bd82010-07-29 14:48:04 -0700366 lperms = nullperms;
367
368 /* buffer freed below, lname is pointer in buffer */
John Johansen4227c332017-05-23 03:25:14 -0700369 error = aa_path_name(&link, profile->path_flags, buffer, &lname,
John Johansen72c8a762017-05-22 03:06:52 -0700370 &info, profile->disconnected);
John Johansen6380bd82010-07-29 14:48:04 -0700371 if (error)
372 goto audit;
373
374 /* buffer2 freed below, tname is pointer in buffer2 */
John Johansen4227c332017-05-23 03:25:14 -0700375 error = aa_path_name(&target, profile->path_flags, buffer2, &tname,
John Johansen72c8a762017-05-22 03:06:52 -0700376 &info, profile->disconnected);
John Johansen6380bd82010-07-29 14:48:04 -0700377 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:
John Johansenef88a7a2017-01-16 00:43:02 -0800433 error = aa_audit_file(profile, &lperms, OP_LINK, request,
John Johansen98c3d182017-06-09 15:48:20 -0700434 lname, tname, NULL, cond.uid, info, error);
John Johansen4227c332017-05-23 03:25:14 -0700435 put_buffers(buffer, buffer2);
John Johansen6380bd82010-07-29 14:48:04 -0700436
437 return error;
438}
439
440/**
441 * aa_file_perm - do permission revalidation check & audit for @file
442 * @op: operation being checked
John Johansen190a9512017-06-09 14:59:51 -0700443 * @label: label being enforced (NOT NULL)
John Johansen6380bd82010-07-29 14:48:04 -0700444 * @file: file to revalidate access permissions on (NOT NULL)
445 * @request: requested permissions
446 *
447 * Returns: %0 if access allowed else error
448 */
John Johansen190a9512017-06-09 14:59:51 -0700449int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
John Johansen6380bd82010-07-29 14:48:04 -0700450 u32 request)
451{
452 struct path_cond cond = {
Al Viro496ad9a2013-01-23 17:07:38 -0500453 .uid = file_inode(file)->i_uid,
454 .mode = file_inode(file)->i_mode
John Johansen6380bd82010-07-29 14:48:04 -0700455 };
John Johansen190a9512017-06-09 14:59:51 -0700456 struct aa_file_ctx *fctx;
457 struct aa_label *flabel;
458 u32 denied;
459 int error = 0;
John Johansen6380bd82010-07-29 14:48:04 -0700460
John Johansen190a9512017-06-09 14:59:51 -0700461 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);
487
488done:
489 rcu_read_unlock();
490
491 return error;
John Johansen6380bd82010-07-29 14:48:04 -0700492}
John Johansen192ca6b2017-06-09 11:58:42 -0700493
John Johansen637f6882017-06-09 08:14:28 -0700494static void revalidate_tty(struct aa_label *label)
John Johansen192ca6b2017-06-09 11:58:42 -0700495{
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
John Johansen190a9512017-06-09 14:59:51 -0700512 if (aa_file_perm(OP_INHERIT, label, file, MAY_READ | MAY_WRITE))
John Johansen192ca6b2017-06-09 11:58:42 -0700513 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{
John Johansen637f6882017-06-09 08:14:28 -0700524 struct aa_label *label = (struct aa_label *)p;
John Johansen192ca6b2017-06-09 11:58:42 -0700525
John Johansen190a9512017-06-09 14:59:51 -0700526 if (aa_file_perm(OP_INHERIT, label, file, aa_map_file_to_perms(file)))
John Johansen192ca6b2017-06-09 11:58:42 -0700527 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{
John Johansen637f6882017-06-09 08:14:28 -0700535 struct aa_label *label = aa_get_newest_cred_label(cred);
John Johansen192ca6b2017-06-09 11:58:42 -0700536 struct file *devnull = NULL;
537 unsigned int n;
538
John Johansen637f6882017-06-09 08:14:28 -0700539 revalidate_tty(label);
John Johansen192ca6b2017-06-09 11:58:42 -0700540
541 /* Revalidate access to inherited open files. */
John Johansen637f6882017-06-09 08:14:28 -0700542 n = iterate_fd(files, 0, match_file, label);
John Johansen192ca6b2017-06-09 11:58:42 -0700543 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);
John Johansen637f6882017-06-09 08:14:28 -0700552 } while ((n = iterate_fd(files, n, match_file, label)) != 0);
John Johansen192ca6b2017-06-09 11:58:42 -0700553 if (devnull)
554 fput(devnull);
555out:
John Johansen637f6882017-06-09 08:14:28 -0700556 aa_put_label(label);
John Johansen192ca6b2017-06-09 11:58:42 -0700557}