1
2
3
4
5
6
7
8
9
10
11
12
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
24
25
26
27
28
29
30static void audit_file_mask(struct audit_buffer *ab, u32 mask)
31{
32 char str[10];
33
34 char *m = str;
35
36 if (mask & AA_EXEC_MMAP)
37 *m++ = 'm';
38 if (mask & (MAY_READ | AA_MAY_META_READ))
39 *m++ = 'r';
40 if (mask & (MAY_WRITE | AA_MAY_META_WRITE | AA_MAY_CHMOD |
41 AA_MAY_CHOWN))
42 *m++ = 'w';
43 else if (mask & MAY_APPEND)
44 *m++ = 'a';
45 if (mask & AA_MAY_CREATE)
46 *m++ = 'c';
47 if (mask & AA_MAY_DELETE)
48 *m++ = 'd';
49 if (mask & AA_MAY_LINK)
50 *m++ = 'l';
51 if (mask & AA_MAY_LOCK)
52 *m++ = 'k';
53 if (mask & MAY_EXEC)
54 *m++ = 'x';
55 *m = '\0';
56
57 audit_log_string(ab, str);
58}
59
60
61
62
63
64
65static void file_audit_cb(struct audit_buffer *ab, void *va)
66{
67 struct common_audit_data *sa = va;
68 uid_t fsuid = current_fsuid();
69
70 if (sa->aad->fs.request & AA_AUDIT_FILE_MASK) {
71 audit_log_format(ab, " requested_mask=");
72 audit_file_mask(ab, sa->aad->fs.request);
73 }
74 if (sa->aad->fs.denied & AA_AUDIT_FILE_MASK) {
75 audit_log_format(ab, " denied_mask=");
76 audit_file_mask(ab, sa->aad->fs.denied);
77 }
78 if (sa->aad->fs.request & AA_AUDIT_FILE_MASK) {
79 audit_log_format(ab, " fsuid=%d", fsuid);
80 audit_log_format(ab, " ouid=%d", sa->aad->fs.ouid);
81 }
82
83 if (sa->aad->fs.target) {
84 audit_log_format(ab, " target=");
85 audit_log_untrustedstring(ab, sa->aad->fs.target);
86 }
87}
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104int aa_audit_file(struct aa_profile *profile, struct file_perms *perms,
105 gfp_t gfp, int op, u32 request, const char *name,
106 const char *target, uid_t ouid, const char *info, int error)
107{
108 int type = AUDIT_APPARMOR_AUTO;
109 struct common_audit_data sa;
110 struct apparmor_audit_data aad = {0,};
111 COMMON_AUDIT_DATA_INIT(&sa, NONE);
112 sa.aad = &aad;
113 aad.op = op,
114 aad.fs.request = request;
115 aad.name = name;
116 aad.fs.target = target;
117 aad.fs.ouid = ouid;
118 aad.info = info;
119 aad.error = error;
120
121 if (likely(!sa.aad->error)) {
122 u32 mask = perms->audit;
123
124 if (unlikely(AUDIT_MODE(profile) == AUDIT_ALL))
125 mask = 0xffff;
126
127
128 sa.aad->fs.request &= mask;
129
130 if (likely(!sa.aad->fs.request))
131 return 0;
132 type = AUDIT_APPARMOR_AUDIT;
133 } else {
134
135 sa.aad->fs.request = sa.aad->fs.request & ~perms->allow;
136
137 if (sa.aad->fs.request & perms->kill)
138 type = AUDIT_APPARMOR_KILL;
139
140
141 if ((sa.aad->fs.request & perms->quiet) &&
142 AUDIT_MODE(profile) != AUDIT_NOQUIET &&
143 AUDIT_MODE(profile) != AUDIT_ALL)
144 sa.aad->fs.request &= ~perms->quiet;
145
146 if (!sa.aad->fs.request)
147 return COMPLAIN_MODE(profile) ? 0 : sa.aad->error;
148 }
149
150 sa.aad->fs.denied = sa.aad->fs.request & ~perms->allow;
151 return aa_audit(type, profile, gfp, &sa, file_audit_cb);
152}
153
154
155
156
157
158
159
160static u32 map_old_perms(u32 old)
161{
162 u32 new = old & 0xf;
163 if (old & MAY_READ)
164 new |= AA_MAY_META_READ;
165 if (old & MAY_WRITE)
166 new |= AA_MAY_META_WRITE | AA_MAY_CREATE | AA_MAY_DELETE |
167 AA_MAY_CHMOD | AA_MAY_CHOWN;
168 if (old & 0x10)
169 new |= AA_MAY_LINK;
170
171
172
173 if (old & 0x20)
174 new |= AA_MAY_LOCK | AA_LINK_SUBSET;
175 if (old & 0x40)
176 new |= AA_EXEC_MMAP;
177
178 return new;
179}
180
181
182
183
184
185
186
187
188
189
190
191
192static struct file_perms compute_perms(struct aa_dfa *dfa, unsigned int state,
193 struct path_cond *cond)
194{
195 struct file_perms perms;
196
197
198
199
200
201
202 perms.kill = 0;
203
204 if (current_fsuid() == cond->uid) {
205 perms.allow = map_old_perms(dfa_user_allow(dfa, state));
206 perms.audit = map_old_perms(dfa_user_audit(dfa, state));
207 perms.quiet = map_old_perms(dfa_user_quiet(dfa, state));
208 perms.xindex = dfa_user_xindex(dfa, state);
209 } else {
210 perms.allow = map_old_perms(dfa_other_allow(dfa, state));
211 perms.audit = map_old_perms(dfa_other_audit(dfa, state));
212 perms.quiet = map_old_perms(dfa_other_quiet(dfa, state));
213 perms.xindex = dfa_other_xindex(dfa, state);
214 }
215 perms.allow |= AA_MAY_META_READ;
216
217
218 if (ACCEPT_TABLE(dfa)[state] & 0x80000000)
219 perms.allow |= AA_MAY_CHANGE_PROFILE;
220 if (ACCEPT_TABLE(dfa)[state] & 0x40000000)
221 perms.allow |= AA_MAY_ONEXEC;
222
223 return perms;
224}
225
226
227
228
229
230
231
232
233
234
235
236unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
237 const char *name, struct path_cond *cond,
238 struct file_perms *perms)
239{
240 unsigned int state;
241 if (!dfa) {
242 *perms = nullperms;
243 return DFA_NOMATCH;
244 }
245
246 state = aa_dfa_match(dfa, start, name);
247 *perms = compute_perms(dfa, state, cond);
248
249 return state;
250}
251
252
253
254
255
256
257
258static inline bool is_deleted(struct dentry *dentry)
259{
260 if (d_unlinked(dentry) && dentry->d_inode->i_nlink == 0)
261 return 1;
262 return 0;
263}
264
265
266
267
268
269
270
271
272
273
274
275
276int aa_path_perm(int op, struct aa_profile *profile, struct path *path,
277 int flags, u32 request, struct path_cond *cond)
278{
279 char *buffer = NULL;
280 struct file_perms perms = {};
281 const char *name, *info = NULL;
282 int error;
283
284 flags |= profile->path_flags | (S_ISDIR(cond->mode) ? PATH_IS_DIR : 0);
285 error = aa_path_name(path, flags, &buffer, &name, &info);
286 if (error) {
287 if (error == -ENOENT && is_deleted(path->dentry)) {
288
289
290
291 error = 0;
292 info = NULL;
293 perms.allow = request;
294 }
295 } else {
296 aa_str_perms(profile->file.dfa, profile->file.start, name, cond,
297 &perms);
298 if (request & ~perms.allow)
299 error = -EACCES;
300 }
301 error = aa_audit_file(profile, &perms, GFP_KERNEL, op, request, name,
302 NULL, cond->uid, info, error);
303 kfree(buffer);
304
305 return error;
306}
307
308
309
310
311
312
313
314
315
316
317
318
319static inline bool xindex_is_subset(u32 link, u32 target)
320{
321 if (((link & ~AA_X_UNSAFE) != (target & ~AA_X_UNSAFE)) ||
322 ((link & AA_X_UNSAFE) && !(target & AA_X_UNSAFE)))
323 return 0;
324
325 return 1;
326}
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry,
347 struct path *new_dir, struct dentry *new_dentry)
348{
349 struct path link = { new_dir->mnt, new_dentry };
350 struct path target = { new_dir->mnt, old_dentry };
351 struct path_cond cond = {
352 old_dentry->d_inode->i_uid,
353 old_dentry->d_inode->i_mode
354 };
355 char *buffer = NULL, *buffer2 = NULL;
356 const char *lname, *tname = NULL, *info = NULL;
357 struct file_perms lperms, perms;
358 u32 request = AA_MAY_LINK;
359 unsigned int state;
360 int error;
361
362 lperms = nullperms;
363
364
365 error = aa_path_name(&link, profile->path_flags, &buffer, &lname,
366 &info);
367 if (error)
368 goto audit;
369
370
371 error = aa_path_name(&target, profile->path_flags, &buffer2, &tname,
372 &info);
373 if (error)
374 goto audit;
375
376 error = -EACCES;
377
378 state = aa_str_perms(profile->file.dfa, profile->file.start, lname,
379 &cond, &lperms);
380
381 if (!(lperms.allow & AA_MAY_LINK))
382 goto audit;
383
384
385 state = aa_dfa_null_transition(profile->file.dfa, state);
386 aa_str_perms(profile->file.dfa, state, tname, &cond, &perms);
387
388
389
390
391 lperms.audit = perms.audit;
392 lperms.quiet = perms.quiet;
393 lperms.kill = perms.kill;
394
395 if (!(perms.allow & AA_MAY_LINK)) {
396 info = "target restricted";
397 goto audit;
398 }
399
400
401 if (!(perms.allow & AA_LINK_SUBSET))
402 goto done_tests;
403
404
405
406
407 aa_str_perms(profile->file.dfa, profile->file.start, tname, &cond,
408 &perms);
409
410
411 request = lperms.allow & ~AA_MAY_LINK;
412 lperms.allow &= perms.allow | AA_MAY_LINK;
413
414 request |= AA_AUDIT_FILE_MASK & (lperms.allow & ~perms.allow);
415 if (request & ~lperms.allow) {
416 goto audit;
417 } else if ((lperms.allow & MAY_EXEC) &&
418 !xindex_is_subset(lperms.xindex, perms.xindex)) {
419 lperms.allow &= ~MAY_EXEC;
420 request |= MAY_EXEC;
421 info = "link not subset of target";
422 goto audit;
423 }
424
425done_tests:
426 error = 0;
427
428audit:
429 error = aa_audit_file(profile, &lperms, GFP_KERNEL, OP_LINK, request,
430 lname, tname, cond.uid, info, error);
431 kfree(buffer);
432 kfree(buffer2);
433
434 return error;
435}
436
437
438
439
440
441
442
443
444
445
446int aa_file_perm(int op, struct aa_profile *profile, struct file *file,
447 u32 request)
448{
449 struct path_cond cond = {
450 .uid = file->f_path.dentry->d_inode->i_uid,
451 .mode = file->f_path.dentry->d_inode->i_mode
452 };
453
454 return aa_path_perm(op, profile, &file->f_path, PATH_DELEGATE_DELETED,
455 request, &cond);
456}
457