Skip to content

Commit 190a951

Browse files
committed
apparmor: move aa_file_perm() to use labels
Signed-off-by: John Johansen <john.johansen@canonical.com>
1 parent 290f458 commit 190a951

3 files changed

Lines changed: 64 additions & 37 deletions

File tree

‎security/apparmor/file.c‎

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "include/match.h"
2424
#include "include/path.h"
2525
#include "include/policy.h"
26+
#include "include/label.h"
2627

2728
static u32 map_mask_to_chr_mask(u32 mask)
2829
{
@@ -433,22 +434,55 @@ int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry,
433434
/**
434435
* aa_file_perm - do permission revalidation check & audit for @file
435436
* @op: operation being checked
436-
* @profile: profile being enforced (NOT NULL)
437+
* @label: label being enforced (NOT NULL)
437438
* @file: file to revalidate access permissions on (NOT NULL)
438439
* @request: requested permissions
439440
*
440441
* Returns: %0 if access allowed else error
441442
*/
442-
int aa_file_perm(const char *op, struct aa_profile *profile, struct file *file,
443+
int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
443444
u32 request)
444445
{
445446
struct path_cond cond = {
446447
.uid = file_inode(file)->i_uid,
447448
.mode = file_inode(file)->i_mode
448449
};
450+
struct aa_file_ctx *fctx;
451+
struct aa_label *flabel;
452+
u32 denied;
453+
int error = 0;
454+
455+
AA_BUG(!label);
456+
AA_BUG(!file);
457+
458+
fctx = file_ctx(file);
459+
460+
rcu_read_lock();
461+
flabel = rcu_dereference(fctx->label);
462+
AA_BUG(!flabel);
463+
464+
/* revalidate access, if task is unconfined, or the cached cred
465+
* doesn't match or if the request is for more permissions than
466+
* was granted.
467+
*
468+
* Note: the test for !unconfined(flabel) is to handle file
469+
* delegation from unconfined tasks
470+
*/
471+
denied = request & ~fctx->allow;
472+
if (unconfined(label) || unconfined(flabel) ||
473+
(!denied && aa_label_is_subset(flabel, label)))
474+
goto done;
475+
476+
/* TODO: label cross check */
477+
478+
if (file->f_path.mnt && path_mediated_fs(file->f_path.dentry))
479+
error = aa_path_perm(op, labels_profile(label), &file->f_path,
480+
PATH_DELEGATE_DELETED, request, &cond);
449481

450-
return aa_path_perm(op, profile, &file->f_path, PATH_DELEGATE_DELETED,
451-
request, &cond);
482+
done:
483+
rcu_read_unlock();
484+
485+
return error;
452486
}
453487

454488
static void revalidate_tty(struct aa_label *label)
@@ -469,8 +503,7 @@ static void revalidate_tty(struct aa_label *label)
469503
struct tty_file_private, list);
470504
file = file_priv->file;
471505

472-
if (aa_file_perm(OP_INHERIT, labels_profile(label), file,
473-
MAY_READ | MAY_WRITE))
506+
if (aa_file_perm(OP_INHERIT, label, file, MAY_READ | MAY_WRITE))
474507
drop_tty = 1;
475508
}
476509
spin_unlock(&tty->files_lock);
@@ -484,8 +517,7 @@ static int match_file(const void *p, struct file *file, unsigned int fd)
484517
{
485518
struct aa_label *label = (struct aa_label *)p;
486519

487-
if (aa_file_perm(OP_INHERIT, labels_profile(label), file,
488-
aa_map_file_to_perms(file)))
520+
if (aa_file_perm(OP_INHERIT, label, file, aa_map_file_to_perms(file)))
489521
return fd + 1;
490522
return 0;
491523
}

‎security/apparmor/include/file.h‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#ifndef __AA_FILE_H
1616
#define __AA_FILE_H
1717

18+
#include <linux/spinlock.h>
19+
1820
#include "domain.h"
1921
#include "match.h"
2022
#include "perms.h"
@@ -33,13 +35,13 @@ struct path;
3335
#define file_ctx(X) ((struct aa_file_ctx *)(X)->f_security)
3436

3537
/* struct aa_file_ctx - the AppArmor context the file was opened in
38+
* @lock: lock to update the ctx
39+
* @label: label currently cached on the ctx
3640
* @perms: the permission the file was opened with
37-
*
38-
* The file_ctx could currently be directly stored in file->f_security
39-
* as the profile reference is now stored in the f_cred. However the
40-
* ctx struct will expand in the future so we keep the struct.
4141
*/
4242
struct aa_file_ctx {
43+
spinlock_t lock;
44+
struct aa_label __rcu *label;
4345
u32 allow;
4446
};
4547

@@ -50,12 +52,16 @@ struct aa_file_ctx {
5052
*
5153
* Returns: file_ctx or NULL on failure
5254
*/
53-
static inline struct aa_file_ctx *aa_alloc_file_ctx(gfp_t gfp)
55+
static inline struct aa_file_ctx *aa_alloc_file_ctx(struct aa_label *label,
56+
gfp_t gfp)
5457
{
5558
struct aa_file_ctx *ctx;
5659

5760
ctx = kzalloc(sizeof(struct aa_file_ctx), gfp);
58-
61+
if (ctx) {
62+
spin_lock_init(&ctx->lock);
63+
rcu_assign_pointer(ctx->label, aa_get_label(label));
64+
}
5965
return ctx;
6066
}
6167

@@ -65,8 +71,15 @@ static inline struct aa_file_ctx *aa_alloc_file_ctx(gfp_t gfp)
6571
*/
6672
static inline void aa_free_file_ctx(struct aa_file_ctx *ctx)
6773
{
68-
if (ctx)
74+
if (ctx) {
75+
aa_put_label(rcu_access_pointer(ctx->label));
6976
kzfree(ctx);
77+
}
78+
}
79+
80+
static inline struct aa_label *aa_get_file_label(struct aa_file_ctx *ctx)
81+
{
82+
return aa_get_label_rcu(&ctx->label);
7083
}
7184

7285
/*
@@ -183,7 +196,7 @@ int aa_path_perm(const char *op, struct aa_profile *profile,
183196
int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry,
184197
const struct path *new_dir, struct dentry *new_dentry);
185198

186-
int aa_file_perm(const char *op, struct aa_profile *profile, struct file *file,
199+
int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
187200
u32 request);
188201

189202
void aa_inherit_files(const struct cred *cred, struct files_struct *files);

‎security/apparmor/lsm.c‎

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ static int apparmor_file_alloc_security(struct file *file)
433433

434434
/* freed by apparmor_file_free_security */
435435
struct aa_label *label = begin_current_label_crit_section();
436-
file->f_security = aa_alloc_file_ctx(GFP_KERNEL);
436+
file->f_security = aa_alloc_file_ctx(label, GFP_KERNEL);
437437
if (!file_ctx(file))
438438
error = -ENOMEM;
439439
end_current_label_crit_section(label);
@@ -448,33 +448,15 @@ static void apparmor_file_free_security(struct file *file)
448448

449449
static int common_file_perm(const char *op, struct file *file, u32 mask)
450450
{
451-
struct aa_file_ctx *fctx = file->f_security;
452-
struct aa_label *label, *flabel;
451+
struct aa_label *label;
453452
int error = 0;
454453

455454
/* don't reaudit files closed during inheritance */
456455
if (file->f_path.dentry == aa_null.dentry)
457456
return -EACCES;
458457

459-
flabel = aa_cred_raw_label(file->f_cred);
460-
AA_BUG(!flabel);
461-
462-
if (!file->f_path.mnt ||
463-
!path_mediated_fs(file->f_path.dentry))
464-
return 0;
465-
466458
label = __begin_current_label_crit_section();
467-
468-
/* revalidate access, if task is unconfined, or the cached cred
469-
* doesn't match or if the request is for more permissions than
470-
* was granted.
471-
*
472-
* Note: the test for !unconfined(fprofile) is to handle file
473-
* delegation from unconfined tasks
474-
*/
475-
if (!unconfined(label) && !unconfined(flabel) &&
476-
((flabel != label) || (mask & ~fctx->allow)))
477-
error = aa_file_perm(op, labels_profile(label), file, mask);
459+
error = aa_file_perm(op, label, file, mask);
478460
__end_current_label_crit_section(label);
479461

480462
return error;

0 commit comments

Comments
 (0)