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