Skip to content

Commit aebd873

Browse files
committed
apparmor: refactor path name lookup and permission checks around labels
Signed-off-by: John Johansen <john.johansen@canonical.com>
1 parent 98c3d18 commit aebd873

3 files changed

Lines changed: 85 additions & 45 deletions

File tree

‎security/apparmor/file.c‎

Lines changed: 77 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,39 @@ int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms,
152152
return aa_audit(type, profile, &sa, file_audit_cb);
153153
}
154154

155+
/**
156+
* is_deleted - test if a file has been completely unlinked
157+
* @dentry: dentry of file to test for deletion (NOT NULL)
158+
*
159+
* Returns: %1 if deleted else %0
160+
*/
161+
static inline bool is_deleted(struct dentry *dentry)
162+
{
163+
if (d_unlinked(dentry) && d_backing_inode(dentry)->i_nlink == 0)
164+
return 1;
165+
return 0;
166+
}
167+
168+
static int path_name(const char *op, struct aa_label *label,
169+
const struct path *path, int flags, char *buffer,
170+
const char **name, struct path_cond *cond, u32 request)
171+
{
172+
struct aa_profile *profile;
173+
const char *info = NULL;
174+
int error;
175+
176+
error = aa_path_name(path, flags, buffer, name, &info,
177+
labels_profile(label)->disconnected);
178+
if (error) {
179+
fn_for_each_confined(label, profile,
180+
aa_audit_file(profile, &nullperms, op, request, *name,
181+
NULL, NULL, cond->uid, info, error));
182+
return error;
183+
}
184+
185+
return 0;
186+
}
187+
155188
/**
156189
* map_old_perms - map old file perms layout to the new layout
157190
* @old: permission set in old mapping
@@ -249,60 +282,69 @@ unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
249282
return state;
250283
}
251284

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-
*/
258-
static inline bool is_deleted(struct dentry *dentry)
285+
int __aa_path_perm(const char *op, struct aa_profile *profile, const char *name,
286+
u32 request, struct path_cond *cond, int flags,
287+
struct aa_perms *perms)
259288
{
260-
if (d_unlinked(dentry) && d_backing_inode(dentry)->i_nlink == 0)
261-
return 1;
262-
return 0;
289+
int e = 0;
290+
291+
if (profile_unconfined(profile))
292+
return 0;
293+
aa_str_perms(profile->file.dfa, profile->file.start, name, cond, perms);
294+
if (request & ~perms->allow)
295+
e = -EACCES;
296+
return aa_audit_file(profile, perms, op, request, name, NULL, NULL,
297+
cond->uid, NULL, e);
298+
}
299+
300+
301+
static int profile_path_perm(const char *op, struct aa_profile *profile,
302+
const struct path *path, char *buffer, u32 request,
303+
struct path_cond *cond, int flags,
304+
struct aa_perms *perms)
305+
{
306+
const char *name;
307+
int error;
308+
309+
if (profile_unconfined(profile))
310+
return 0;
311+
312+
error = path_name(op, &profile->label, path,
313+
flags | profile->path_flags, buffer, &name, cond,
314+
request);
315+
if (error)
316+
return error;
317+
return __aa_path_perm(op, profile, name, request, cond, flags,
318+
perms);
263319
}
264320

265321
/**
266322
* aa_path_perm - do permissions check & audit for @path
267323
* @op: operation being checked
268-
* @profile: profile being enforced (NOT NULL)
324+
* @label: profile being enforced (NOT NULL)
269325
* @path: path to check permissions of (NOT NULL)
270326
* @flags: any additional path flags beyond what the profile specifies
271327
* @request: requested permissions
272328
* @cond: conditional info for this request (NOT NULL)
273329
*
274330
* Returns: %0 else error if access denied or other error
275331
*/
276-
int aa_path_perm(const char *op, struct aa_profile *profile,
332+
int aa_path_perm(const char *op, struct aa_label *label,
277333
const struct path *path, int flags, u32 request,
278334
struct path_cond *cond)
279335
{
280-
char *buffer = NULL;
281336
struct aa_perms perms = {};
282-
const char *name, *info = NULL;
337+
struct aa_profile *profile;
338+
char *buffer = NULL;
283339
int error;
284340

285-
flags |= profile->path_flags | (S_ISDIR(cond->mode) ? PATH_IS_DIR : 0);
341+
flags |= PATH_DELEGATE_DELETED | (S_ISDIR(cond->mode) ? PATH_IS_DIR :
342+
0);
286343
get_buffers(buffer);
287-
error = aa_path_name(path, flags, buffer, &name, &info,
288-
profile->disconnected);
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;
295-
info = NULL;
296-
perms.allow = request;
297-
}
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-
}
304-
error = aa_audit_file(profile, &perms, op, request, name, NULL, NULL,
305-
cond->uid, info, error);
344+
error = fn_for_each_confined(label, profile,
345+
profile_path_perm(op, profile, path, buffer, request,
346+
cond, flags, &perms));
347+
306348
put_buffers(buffer);
307349

308350
return error;
@@ -482,7 +524,7 @@ int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
482524
/* TODO: label cross check */
483525

484526
if (file->f_path.mnt && path_mediated_fs(file->f_path.dentry))
485-
error = aa_path_perm(op, labels_profile(label), &file->f_path,
527+
error = aa_path_perm(op, label, &file->f_path,
486528
PATH_DELEGATE_DELETED, request, &cond);
487529

488530
done:

‎security/apparmor/include/file.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
190190
const char *name, struct path_cond *cond,
191191
struct aa_perms *perms);
192192

193-
int aa_path_perm(const char *op, struct aa_profile *profile,
193+
int __aa_path_perm(const char *op, struct aa_profile *profile,
194+
const char *name, u32 request, struct path_cond *cond,
195+
int flags, struct aa_perms *perms);
196+
int aa_path_perm(const char *op, struct aa_label *label,
194197
const struct path *path, int flags, u32 request,
195198
struct path_cond *cond);
196199

‎security/apparmor/lsm.c‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ static int common_perm(const char *op, const struct path *path, u32 mask,
196196

197197
label = __begin_current_label_crit_section();
198198
if (!unconfined(label))
199-
error = aa_path_perm(op, labels_profile(label), path, 0, mask,
200-
cond);
199+
error = aa_path_perm(op, label, path, 0, mask, cond);
201200
__end_current_label_crit_section(label);
202201

203202
return error;
@@ -359,15 +358,12 @@ static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_d
359358
d_backing_inode(old_dentry)->i_mode
360359
};
361360

362-
error = aa_path_perm(OP_RENAME_SRC, labels_profile(label),
363-
&old_path, 0,
361+
error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
364362
MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
365363
AA_MAY_SETATTR | AA_MAY_DELETE,
366364
&cond);
367365
if (!error)
368-
error = aa_path_perm(OP_RENAME_DEST,
369-
labels_profile(label),
370-
&new_path,
366+
error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
371367
0, MAY_WRITE | AA_MAY_SETATTR |
372368
AA_MAY_CREATE, &cond);
373369

@@ -416,8 +412,7 @@ static int apparmor_file_open(struct file *file, const struct cred *cred)
416412
struct inode *inode = file_inode(file);
417413
struct path_cond cond = { inode->i_uid, inode->i_mode };
418414

419-
error = aa_path_perm(OP_OPEN, labels_profile(label),
420-
&file->f_path, 0,
415+
error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
421416
aa_map_file_to_perms(file), &cond);
422417
/* todo cache full allowed permissions set and state */
423418
fctx->allow = aa_map_file_to_perms(file);

0 commit comments

Comments
 (0)