blob: 1ee656f66aa43b8c3380c2b4783e9d82aed755ee [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
15#include "include/apparmor.h"
16#include "include/audit.h"
17#include "include/file.h"
18#include "include/match.h"
19#include "include/path.h"
20#include "include/policy.h"
21
22struct file_perms nullperms;
23
John Johansene53cfe62017-05-26 15:07:22 -070024static u32 map_mask_to_chr_mask(u32 mask)
25{
26 u32 m = mask & PERMS_CHRS_MASK;
27
28 if (mask & AA_MAY_GETATTR)
29 m |= MAY_READ;
30 if (mask & (AA_MAY_SETATTR | AA_MAY_CHMOD | AA_MAY_CHOWN))
31 m |= MAY_WRITE;
32
33 return m;
34}
John Johansen6380bd82010-07-29 14:48:04 -070035
36/**
37 * audit_file_mask - convert mask to permission string
38 * @buffer: buffer to write string to (NOT NULL)
39 * @mask: permission mask to convert
40 */
41static void audit_file_mask(struct audit_buffer *ab, u32 mask)
42{
43 char str[10];
44
John Johansene53cfe62017-05-26 15:07:22 -070045 aa_perm_mask_to_str(str, aa_file_perm_chrs, map_mask_to_chr_mask(mask));
John Johansen6380bd82010-07-29 14:48:04 -070046 audit_log_string(ab, str);
47}
48
49/**
50 * file_audit_cb - call back for file specific audit fields
51 * @ab: audit_buffer (NOT NULL)
52 * @va: audit struct to audit values of (NOT NULL)
53 */
54static void file_audit_cb(struct audit_buffer *ab, void *va)
55{
56 struct common_audit_data *sa = va;
Eric W. Biederman2db81452012-02-07 16:33:13 -080057 kuid_t fsuid = current_fsuid();
John Johansen6380bd82010-07-29 14:48:04 -070058
John Johansenaa9aeea2017-05-29 12:16:04 -070059 if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
John Johansen6380bd82010-07-29 14:48:04 -070060 audit_log_format(ab, " requested_mask=");
John Johansenaa9aeea2017-05-29 12:16:04 -070061 audit_file_mask(ab, aad(sa)->request);
John Johansen6380bd82010-07-29 14:48:04 -070062 }
John Johansenaa9aeea2017-05-29 12:16:04 -070063 if (aad(sa)->denied & AA_AUDIT_FILE_MASK) {
John Johansen6380bd82010-07-29 14:48:04 -070064 audit_log_format(ab, " denied_mask=");
John Johansenaa9aeea2017-05-29 12:16:04 -070065 audit_file_mask(ab, aad(sa)->denied);
John Johansen6380bd82010-07-29 14:48:04 -070066 }
John Johansenaa9aeea2017-05-29 12:16:04 -070067 if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
Eric W. Biederman2db81452012-02-07 16:33:13 -080068 audit_log_format(ab, " fsuid=%d",
69 from_kuid(&init_user_ns, fsuid));
70 audit_log_format(ab, " ouid=%d",
John Johansenef88a7a2017-01-16 00:43:02 -080071 from_kuid(&init_user_ns, aad(sa)->fs.ouid));
John Johansen6380bd82010-07-29 14:48:04 -070072 }
73
John Johansenef88a7a2017-01-16 00:43:02 -080074 if (aad(sa)->fs.target) {
John Johansen6380bd82010-07-29 14:48:04 -070075 audit_log_format(ab, " target=");
John Johansenef88a7a2017-01-16 00:43:02 -080076 audit_log_untrustedstring(ab, aad(sa)->fs.target);
John Johansen6380bd82010-07-29 14:48:04 -070077 }
78}
79
80/**
81 * aa_audit_file - handle the auditing of file operations
82 * @profile: the profile being enforced (NOT NULL)
83 * @perms: the permissions computed for the request (NOT NULL)
84 * @gfp: allocation flags
85 * @op: operation being mediated
86 * @request: permissions requested
87 * @name: name of object being mediated (MAYBE NULL)
88 * @target: name of target (MAYBE NULL)
89 * @ouid: object uid
90 * @info: extra information message (MAYBE NULL)
91 * @error: 0 if operation allowed else failure error code
92 *
93 * Returns: %0 or error on failure
94 */
95int aa_audit_file(struct aa_profile *profile, struct file_perms *perms,
John Johansenef88a7a2017-01-16 00:43:02 -080096 const char *op, u32 request, const char *name,
Eric W. Biederman2db81452012-02-07 16:33:13 -080097 const char *target, kuid_t ouid, const char *info, int error)
John Johansen6380bd82010-07-29 14:48:04 -070098{
99 int type = AUDIT_APPARMOR_AUTO;
John Johansenef88a7a2017-01-16 00:43:02 -0800100 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_TASK, op);
John Johansen6380bd82010-07-29 14:48:04 -0700101
John Johansenef88a7a2017-01-16 00:43:02 -0800102 sa.u.tsk = NULL;
John Johansenaa9aeea2017-05-29 12:16:04 -0700103 aad(&sa)->request = request;
John Johansenef88a7a2017-01-16 00:43:02 -0800104 aad(&sa)->name = name;
105 aad(&sa)->fs.target = target;
106 aad(&sa)->fs.ouid = ouid;
107 aad(&sa)->info = info;
108 aad(&sa)->error = error;
109 sa.u.tsk = NULL;
110
111 if (likely(!aad(&sa)->error)) {
John Johansen6380bd82010-07-29 14:48:04 -0700112 u32 mask = perms->audit;
113
114 if (unlikely(AUDIT_MODE(profile) == AUDIT_ALL))
115 mask = 0xffff;
116
117 /* mask off perms that are not being force audited */
John Johansenaa9aeea2017-05-29 12:16:04 -0700118 aad(&sa)->request &= mask;
John Johansen6380bd82010-07-29 14:48:04 -0700119
John Johansenaa9aeea2017-05-29 12:16:04 -0700120 if (likely(!aad(&sa)->request))
John Johansen6380bd82010-07-29 14:48:04 -0700121 return 0;
122 type = AUDIT_APPARMOR_AUDIT;
123 } else {
124 /* only report permissions that were denied */
John Johansenaa9aeea2017-05-29 12:16:04 -0700125 aad(&sa)->request = aad(&sa)->request & ~perms->allow;
126 AA_BUG(!aad(&sa)->request);
John Johansen6380bd82010-07-29 14:48:04 -0700127
John Johansenaa9aeea2017-05-29 12:16:04 -0700128 if (aad(&sa)->request & perms->kill)
John Johansen6380bd82010-07-29 14:48:04 -0700129 type = AUDIT_APPARMOR_KILL;
130
131 /* quiet known rejects, assumes quiet and kill do not overlap */
John Johansenaa9aeea2017-05-29 12:16:04 -0700132 if ((aad(&sa)->request & perms->quiet) &&
John Johansen6380bd82010-07-29 14:48:04 -0700133 AUDIT_MODE(profile) != AUDIT_NOQUIET &&
134 AUDIT_MODE(profile) != AUDIT_ALL)
John Johansenaa9aeea2017-05-29 12:16:04 -0700135 aad(&sa)->request &= ~perms->quiet;
John Johansen6380bd82010-07-29 14:48:04 -0700136
John Johansenaa9aeea2017-05-29 12:16:04 -0700137 if (!aad(&sa)->request)
John Johansenef88a7a2017-01-16 00:43:02 -0800138 return COMPLAIN_MODE(profile) ? 0 : aad(&sa)->error;
John Johansen6380bd82010-07-29 14:48:04 -0700139 }
140
John Johansenaa9aeea2017-05-29 12:16:04 -0700141 aad(&sa)->denied = aad(&sa)->request & ~perms->allow;
John Johansenef88a7a2017-01-16 00:43:02 -0800142 return aa_audit(type, profile, &sa, file_audit_cb);
John Johansen6380bd82010-07-29 14:48:04 -0700143}
144
145/**
146 * map_old_perms - map old file perms layout to the new layout
147 * @old: permission set in old mapping
148 *
149 * Returns: new permission mapping
150 */
151static u32 map_old_perms(u32 old)
152{
153 u32 new = old & 0xf;
154 if (old & MAY_READ)
John Johansene53cfe62017-05-26 15:07:22 -0700155 new |= AA_MAY_GETATTR | AA_MAY_OPEN;
John Johansen6380bd82010-07-29 14:48:04 -0700156 if (old & MAY_WRITE)
John Johansene53cfe62017-05-26 15:07:22 -0700157 new |= AA_MAY_SETATTR | AA_MAY_CREATE | AA_MAY_DELETE |
158 AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_OPEN;
John Johansen6380bd82010-07-29 14:48:04 -0700159 if (old & 0x10)
160 new |= AA_MAY_LINK;
161 /* the old mapping lock and link_subset flags where overlaid
162 * and use was determined by part of a pair that they were in
163 */
164 if (old & 0x20)
165 new |= AA_MAY_LOCK | AA_LINK_SUBSET;
166 if (old & 0x40) /* AA_EXEC_MMAP */
167 new |= AA_EXEC_MMAP;
168
John Johansen6380bd82010-07-29 14:48:04 -0700169 return new;
170}
171
172/**
173 * compute_perms - convert dfa compressed perms to internal perms
174 * @dfa: dfa to compute perms for (NOT NULL)
175 * @state: state in dfa
176 * @cond: conditions to consider (NOT NULL)
177 *
178 * TODO: convert from dfa + state to permission entry, do computation conversion
179 * at load time.
180 *
181 * Returns: computed permission set
182 */
183static struct file_perms compute_perms(struct aa_dfa *dfa, unsigned int state,
184 struct path_cond *cond)
185{
186 struct file_perms perms;
187
188 /* FIXME: change over to new dfa format
189 * currently file perms are encoded in the dfa, new format
190 * splits the permissions from the dfa. This mapping can be
191 * done at profile load
192 */
193 perms.kill = 0;
194
Eric W. Biederman2db81452012-02-07 16:33:13 -0800195 if (uid_eq(current_fsuid(), cond->uid)) {
John Johansen6380bd82010-07-29 14:48:04 -0700196 perms.allow = map_old_perms(dfa_user_allow(dfa, state));
197 perms.audit = map_old_perms(dfa_user_audit(dfa, state));
198 perms.quiet = map_old_perms(dfa_user_quiet(dfa, state));
199 perms.xindex = dfa_user_xindex(dfa, state);
200 } else {
201 perms.allow = map_old_perms(dfa_other_allow(dfa, state));
202 perms.audit = map_old_perms(dfa_other_audit(dfa, state));
203 perms.quiet = map_old_perms(dfa_other_quiet(dfa, state));
204 perms.xindex = dfa_other_xindex(dfa, state);
205 }
John Johansene53cfe62017-05-26 15:07:22 -0700206 perms.allow |= AA_MAY_GETATTR;
John Johansen6380bd82010-07-29 14:48:04 -0700207
208 /* change_profile wasn't determined by ownership in old mapping */
209 if (ACCEPT_TABLE(dfa)[state] & 0x80000000)
210 perms.allow |= AA_MAY_CHANGE_PROFILE;
John Johansen0421ea92012-03-27 04:14:33 -0700211 if (ACCEPT_TABLE(dfa)[state] & 0x40000000)
212 perms.allow |= AA_MAY_ONEXEC;
John Johansen6380bd82010-07-29 14:48:04 -0700213
214 return perms;
215}
216
217/**
218 * aa_str_perms - find permission that match @name
219 * @dfa: to match against (MAYBE NULL)
220 * @state: state to start matching in
221 * @name: string to match against dfa (NOT NULL)
222 * @cond: conditions to consider for permission set computation (NOT NULL)
223 * @perms: Returns - the permissions found when matching @name
224 *
225 * Returns: the final state in @dfa when beginning @start and walking @name
226 */
227unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
228 const char *name, struct path_cond *cond,
229 struct file_perms *perms)
230{
231 unsigned int state;
232 if (!dfa) {
233 *perms = nullperms;
234 return DFA_NOMATCH;
235 }
236
237 state = aa_dfa_match(dfa, start, name);
238 *perms = compute_perms(dfa, state, cond);
239
240 return state;
241}
242
243/**
244 * is_deleted - test if a file has been completely unlinked
245 * @dentry: dentry of file to test for deletion (NOT NULL)
246 *
247 * Returns: %1 if deleted else %0
248 */
249static inline bool is_deleted(struct dentry *dentry)
250{
David Howellsc6f493d2015-03-17 22:26:22 +0000251 if (d_unlinked(dentry) && d_backing_inode(dentry)->i_nlink == 0)
John Johansen6380bd82010-07-29 14:48:04 -0700252 return 1;
253 return 0;
254}
255
256/**
257 * aa_path_perm - do permissions check & audit for @path
258 * @op: operation being checked
259 * @profile: profile being enforced (NOT NULL)
260 * @path: path to check permissions of (NOT NULL)
261 * @flags: any additional path flags beyond what the profile specifies
262 * @request: requested permissions
263 * @cond: conditional info for this request (NOT NULL)
264 *
265 * Returns: %0 else error if access denied or other error
266 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800267int aa_path_perm(const char *op, struct aa_profile *profile,
268 const struct path *path, int flags, u32 request,
269 struct path_cond *cond)
John Johansen6380bd82010-07-29 14:48:04 -0700270{
271 char *buffer = NULL;
272 struct file_perms perms = {};
273 const char *name, *info = NULL;
274 int error;
275
276 flags |= profile->path_flags | (S_ISDIR(cond->mode) ? PATH_IS_DIR : 0);
John Johansen4227c332017-05-23 03:25:14 -0700277 get_buffers(buffer);
278 error = aa_path_name(path, flags, buffer, &name, &info,
John Johansen72c8a762017-05-22 03:06:52 -0700279 profile->disconnected);
John Johansen6380bd82010-07-29 14:48:04 -0700280 if (error) {
281 if (error == -ENOENT && is_deleted(path->dentry)) {
282 /* Access to open files that are deleted are
283 * give a pass (implicit delegation)
284 */
285 error = 0;
John Johansen57fa1e12012-02-16 06:20:33 -0800286 info = NULL;
John Johansen6380bd82010-07-29 14:48:04 -0700287 perms.allow = request;
John Johansen57fa1e12012-02-16 06:20:33 -0800288 }
John Johansen6380bd82010-07-29 14:48:04 -0700289 } else {
290 aa_str_perms(profile->file.dfa, profile->file.start, name, cond,
291 &perms);
292 if (request & ~perms.allow)
293 error = -EACCES;
294 }
John Johansenef88a7a2017-01-16 00:43:02 -0800295 error = aa_audit_file(profile, &perms, op, request, name, NULL,
296 cond->uid, info, error);
John Johansen4227c332017-05-23 03:25:14 -0700297 put_buffers(buffer);
John Johansen6380bd82010-07-29 14:48:04 -0700298
299 return error;
300}
301
302/**
303 * xindex_is_subset - helper for aa_path_link
304 * @link: link permission set
305 * @target: target permission set
306 *
307 * test target x permissions are equal OR a subset of link x permissions
308 * this is done as part of the subset test, where a hardlink must have
309 * a subset of permissions that the target has.
310 *
311 * Returns: %1 if subset else %0
312 */
313static inline bool xindex_is_subset(u32 link, u32 target)
314{
315 if (((link & ~AA_X_UNSAFE) != (target & ~AA_X_UNSAFE)) ||
316 ((link & AA_X_UNSAFE) && !(target & AA_X_UNSAFE)))
317 return 0;
318
319 return 1;
320}
321
322/**
323 * aa_path_link - Handle hard link permission check
324 * @profile: the profile being enforced (NOT NULL)
325 * @old_dentry: the target dentry (NOT NULL)
326 * @new_dir: directory the new link will be created in (NOT NULL)
327 * @new_dentry: the link being created (NOT NULL)
328 *
329 * Handle the permission test for a link & target pair. Permission
330 * is encoded as a pair where the link permission is determined
331 * first, and if allowed, the target is tested. The target test
332 * is done from the point of the link match (not start of DFA)
333 * making the target permission dependent on the link permission match.
334 *
335 * The subset test if required forces that permissions granted
336 * on link are a subset of the permission granted to target.
337 *
338 * Returns: %0 if allowed else error
339 */
340int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry,
Al Viro3539aaf2016-03-25 15:07:03 -0400341 const struct path *new_dir, struct dentry *new_dentry)
John Johansen6380bd82010-07-29 14:48:04 -0700342{
Kees Cook8486adf2016-12-16 17:04:13 -0800343 struct path link = { .mnt = new_dir->mnt, .dentry = new_dentry };
344 struct path target = { .mnt = new_dir->mnt, .dentry = old_dentry };
John Johansen6380bd82010-07-29 14:48:04 -0700345 struct path_cond cond = {
David Howellsc6f493d2015-03-17 22:26:22 +0000346 d_backing_inode(old_dentry)->i_uid,
347 d_backing_inode(old_dentry)->i_mode
John Johansen6380bd82010-07-29 14:48:04 -0700348 };
349 char *buffer = NULL, *buffer2 = NULL;
350 const char *lname, *tname = NULL, *info = NULL;
351 struct file_perms lperms, perms;
352 u32 request = AA_MAY_LINK;
353 unsigned int state;
354 int error;
355
John Johansen4227c332017-05-23 03:25:14 -0700356 get_buffers(buffer, buffer2);
John Johansen6380bd82010-07-29 14:48:04 -0700357 lperms = nullperms;
358
359 /* buffer freed below, lname is pointer in buffer */
John Johansen4227c332017-05-23 03:25:14 -0700360 error = aa_path_name(&link, profile->path_flags, buffer, &lname,
John Johansen72c8a762017-05-22 03:06:52 -0700361 &info, profile->disconnected);
John Johansen6380bd82010-07-29 14:48:04 -0700362 if (error)
363 goto audit;
364
365 /* buffer2 freed below, tname is pointer in buffer2 */
John Johansen4227c332017-05-23 03:25:14 -0700366 error = aa_path_name(&target, profile->path_flags, buffer2, &tname,
John Johansen72c8a762017-05-22 03:06:52 -0700367 &info, profile->disconnected);
John Johansen6380bd82010-07-29 14:48:04 -0700368 if (error)
369 goto audit;
370
371 error = -EACCES;
372 /* aa_str_perms - handles the case of the dfa being NULL */
373 state = aa_str_perms(profile->file.dfa, profile->file.start, lname,
374 &cond, &lperms);
375
376 if (!(lperms.allow & AA_MAY_LINK))
377 goto audit;
378
379 /* test to see if target can be paired with link */
380 state = aa_dfa_null_transition(profile->file.dfa, state);
381 aa_str_perms(profile->file.dfa, state, tname, &cond, &perms);
382
383 /* force audit/quiet masks for link are stored in the second entry
384 * in the link pair.
385 */
386 lperms.audit = perms.audit;
387 lperms.quiet = perms.quiet;
388 lperms.kill = perms.kill;
389
390 if (!(perms.allow & AA_MAY_LINK)) {
391 info = "target restricted";
392 goto audit;
393 }
394
395 /* done if link subset test is not required */
396 if (!(perms.allow & AA_LINK_SUBSET))
397 goto done_tests;
398
399 /* Do link perm subset test requiring allowed permission on link are a
400 * subset of the allowed permissions on target.
401 */
402 aa_str_perms(profile->file.dfa, profile->file.start, tname, &cond,
403 &perms);
404
405 /* AA_MAY_LINK is not considered in the subset test */
406 request = lperms.allow & ~AA_MAY_LINK;
407 lperms.allow &= perms.allow | AA_MAY_LINK;
408
409 request |= AA_AUDIT_FILE_MASK & (lperms.allow & ~perms.allow);
410 if (request & ~lperms.allow) {
411 goto audit;
412 } else if ((lperms.allow & MAY_EXEC) &&
413 !xindex_is_subset(lperms.xindex, perms.xindex)) {
414 lperms.allow &= ~MAY_EXEC;
415 request |= MAY_EXEC;
416 info = "link not subset of target";
417 goto audit;
418 }
419
420done_tests:
421 error = 0;
422
423audit:
John Johansenef88a7a2017-01-16 00:43:02 -0800424 error = aa_audit_file(profile, &lperms, OP_LINK, request,
John Johansen6380bd82010-07-29 14:48:04 -0700425 lname, tname, cond.uid, info, error);
John Johansen4227c332017-05-23 03:25:14 -0700426 put_buffers(buffer, buffer2);
John Johansen6380bd82010-07-29 14:48:04 -0700427
428 return error;
429}
430
431/**
432 * aa_file_perm - do permission revalidation check & audit for @file
433 * @op: operation being checked
434 * @profile: profile being enforced (NOT NULL)
435 * @file: file to revalidate access permissions on (NOT NULL)
436 * @request: requested permissions
437 *
438 * Returns: %0 if access allowed else error
439 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800440int aa_file_perm(const char *op, struct aa_profile *profile, struct file *file,
John Johansen6380bd82010-07-29 14:48:04 -0700441 u32 request)
442{
443 struct path_cond cond = {
Al Viro496ad9a2013-01-23 17:07:38 -0500444 .uid = file_inode(file)->i_uid,
445 .mode = file_inode(file)->i_mode
John Johansen6380bd82010-07-29 14:48:04 -0700446 };
447
448 return aa_path_perm(op, profile, &file->f_path, PATH_DELEGATE_DELETED,
449 request, &cond);
450}