Skip to content

Commit de62de5

Browse files
committed
apparmor: move task related defines and fns to task.X files
Signed-off-by: John Johansen <john.johansen@canonical.com>
1 parent d065f2f commit de62de5

6 files changed

Lines changed: 105 additions & 98 deletions

File tree

‎security/apparmor/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
obj-$(CONFIG_SECURITY_APPARMOR) += apparmor.o
55

6-
apparmor-y := apparmorfs.o audit.o capability.o context.o ipc.o lib.o match.o \
6+
apparmor-y := apparmorfs.o audit.o capability.o task.o ipc.o lib.o match.o \
77
path.o domain.o policy.o policy_unpack.o procattr.o lsm.o \
88
resource.o secid.o file.o policy_ns.o label.o mount.o
99
apparmor-$(CONFIG_SECURITY_APPARMOR_HASH) += crypto.o

‎security/apparmor/domain.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ int apparmor_bprm_set_creds(struct linux_binprm *bprm)
794794
if (bprm->called_set_creds)
795795
return 0;
796796

797-
ctx = current_task_ctx();
797+
ctx = task_ctx(current);
798798
AA_BUG(!cred_label(bprm->cred));
799799
AA_BUG(!ctx);
800800

@@ -1067,7 +1067,7 @@ int aa_change_hat(const char *hats[], int count, u64 token, int flags)
10671067

10681068
/* released below */
10691069
cred = get_current_cred();
1070-
ctx = current_task_ctx();
1070+
ctx = task_ctx(current);
10711071
label = aa_get_newest_cred_label(cred);
10721072
previous = aa_get_newest_label(ctx->previous);
10731073

‎security/apparmor/include/context.h‎

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,10 @@
2121

2222
#include "label.h"
2323
#include "policy_ns.h"
24+
#include "task.h"
2425

25-
#define task_ctx(X) ((X)->security)
26-
#define current_task_ctx() (task_ctx(current))
2726
#define cred_label(X) ((X)->security)
2827

29-
/*
30-
* struct aa_task_ctx - information for current task label change
31-
* @onexec: profile to transition to on next exec (MAY BE NULL)
32-
* @previous: profile the task may return to (MAY BE NULL)
33-
* @token: magic value the task must know for returning to @previous_profile
34-
*/
35-
struct aa_task_ctx {
36-
struct aa_label *onexec;
37-
struct aa_label *previous;
38-
u64 token;
39-
};
40-
41-
struct aa_task_ctx *aa_alloc_task_ctx(gfp_t flags);
42-
void aa_free_task_ctx(struct aa_task_ctx *ctx);
43-
void aa_dup_task_ctx(struct aa_task_ctx *new, const struct aa_task_ctx *old);
44-
45-
int aa_replace_current_label(struct aa_label *label);
46-
int aa_set_current_onexec(struct aa_label *label, bool stack);
47-
int aa_set_current_hat(struct aa_label *label, u64 token);
48-
int aa_restore_previous_label(u64 cookie);
49-
struct aa_label *aa_get_task_label(struct task_struct *task);
50-
5128

5229
/**
5330
* aa_cred_raw_label - obtain cred's label
@@ -196,19 +173,4 @@ static inline struct aa_ns *aa_get_current_ns(void)
196173
return ns;
197174
}
198175

199-
/**
200-
* aa_clear_task_ctx_trans - clear transition tracking info from the ctx
201-
* @ctx: task context to clear (NOT NULL)
202-
*/
203-
static inline void aa_clear_task_ctx_trans(struct aa_task_ctx *ctx)
204-
{
205-
AA_BUG(!ctx);
206-
207-
aa_put_label(ctx->previous);
208-
aa_put_label(ctx->onexec);
209-
ctx->previous = NULL;
210-
ctx->onexec = NULL;
211-
ctx->token = 0;
212-
}
213-
214176
#endif /* __AA_CONTEXT_H */

‎security/apparmor/include/task.h‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* AppArmor security module
3+
*
4+
* This file contains AppArmor task related definitions and mediation
5+
*
6+
* Copyright 2017 Canonical Ltd.
7+
*
8+
* This program is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU General Public License as
10+
* published by the Free Software Foundation, version 2 of the
11+
* License.
12+
*/
13+
14+
#ifndef __AA_TASK_H
15+
#define __AA_TASK_H
16+
17+
#define task_ctx(X) ((X)->security)
18+
19+
/*
20+
* struct aa_task_ctx - information for current task label change
21+
* @onexec: profile to transition to on next exec (MAY BE NULL)
22+
* @previous: profile the task may return to (MAY BE NULL)
23+
* @token: magic value the task must know for returning to @previous_profile
24+
*/
25+
struct aa_task_ctx {
26+
struct aa_label *onexec;
27+
struct aa_label *previous;
28+
u64 token;
29+
};
30+
31+
int aa_replace_current_label(struct aa_label *label);
32+
int aa_set_current_onexec(struct aa_label *label, bool stack);
33+
int aa_set_current_hat(struct aa_label *label, u64 token);
34+
int aa_restore_previous_label(u64 cookie);
35+
struct aa_label *aa_get_task_label(struct task_struct *task);
36+
37+
/**
38+
* aa_alloc_task_ctx - allocate a new task_ctx
39+
* @flags: gfp flags for allocation
40+
*
41+
* Returns: allocated buffer or NULL on failure
42+
*/
43+
static inline struct aa_task_ctx *aa_alloc_task_ctx(gfp_t flags)
44+
{
45+
return kzalloc(sizeof(struct aa_task_ctx), flags);
46+
}
47+
48+
/**
49+
* aa_free_task_ctx - free a task_ctx
50+
* @ctx: task_ctx to free (MAYBE NULL)
51+
*/
52+
static inline void aa_free_task_ctx(struct aa_task_ctx *ctx)
53+
{
54+
if (ctx) {
55+
aa_put_label(ctx->previous);
56+
aa_put_label(ctx->onexec);
57+
58+
kzfree(ctx);
59+
}
60+
}
61+
62+
/**
63+
* aa_dup_task_ctx - duplicate a task context, incrementing reference counts
64+
* @new: a blank task context (NOT NULL)
65+
* @old: the task context to copy (NOT NULL)
66+
*/
67+
static inline void aa_dup_task_ctx(struct aa_task_ctx *new,
68+
const struct aa_task_ctx *old)
69+
{
70+
*new = *old;
71+
aa_get_label(new->previous);
72+
aa_get_label(new->onexec);
73+
}
74+
75+
/**
76+
* aa_clear_task_ctx_trans - clear transition tracking info from the ctx
77+
* @ctx: task context to clear (NOT NULL)
78+
*/
79+
static inline void aa_clear_task_ctx_trans(struct aa_task_ctx *ctx)
80+
{
81+
AA_BUG(!ctx);
82+
83+
aa_put_label(ctx->previous);
84+
aa_put_label(ctx->onexec);
85+
ctx->previous = NULL;
86+
ctx->onexec = NULL;
87+
ctx->token = 0;
88+
}
89+
90+
#endif /* __AA_TASK_H */

‎security/apparmor/lsm.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static int apparmor_task_alloc(struct task_struct *task,
101101
if (!new)
102102
return -ENOMEM;
103103

104-
aa_dup_task_ctx(new, current_task_ctx());
104+
aa_dup_task_ctx(new, task_ctx(current));
105105
task_ctx(task) = new;
106106

107107
return 0;
@@ -582,7 +582,7 @@ static int apparmor_getprocattr(struct task_struct *task, char *name,
582582
int error = -ENOENT;
583583
/* released below */
584584
const struct cred *cred = get_task_cred(task);
585-
struct aa_task_ctx *ctx = current_task_ctx();
585+
struct aa_task_ctx *ctx = task_ctx(current);
586586
struct aa_label *label = NULL;
587587

588588
if (strcmp(name, "current") == 0)
@@ -705,7 +705,7 @@ static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
705705
static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
706706
{
707707
/* clear out temporary/transitional state from the context */
708-
aa_clear_task_ctx_trans(current_task_ctx());
708+
aa_clear_task_ctx_trans(task_ctx(current));
709709

710710
return;
711711
}
Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
/*
22
* AppArmor security module
33
*
4-
* This file contains AppArmor functions used to manipulate object security
5-
* contexts.
4+
* This file contains AppArmor task related definitions and mediation
65
*
7-
* Copyright (C) 1998-2008 Novell/SUSE
8-
* Copyright 2009-2010 Canonical Ltd.
6+
* Copyright 2017 Canonical Ltd.
97
*
108
* This program is free software; you can redistribute it and/or
119
* modify it under the terms of the GNU General Public License as
1210
* published by the Free Software Foundation, version 2 of the
1311
* License.
1412
*
15-
*
16-
* AppArmor sets confinement on every task, via the cred_label() which
17-
* is required and are not allowed to be NULL. The cred_label is
18-
* reference counted.
19-
*
2013
* TODO
2114
* If a task uses change_hat it currently does not return to the old
2215
* cred or task context but instead creates a new one. Ideally the task
2316
* should return to the previous cred if it has not been modified.
24-
*
2517
*/
2618

2719
#include "include/context.h"
28-
#include "include/policy.h"
29-
20+
#include "include/task.h"
3021

3122
/**
3223
* aa_get_task_label - Get another task's label
@@ -45,43 +36,6 @@ struct aa_label *aa_get_task_label(struct task_struct *task)
4536
return p;
4637
}
4738

48-
/**
49-
* aa_alloc_task_ctx - allocate a new task_ctx
50-
* @flags: gfp flags for allocation
51-
*
52-
* Returns: allocated buffer or NULL on failure
53-
*/
54-
struct aa_task_ctx *aa_alloc_task_ctx(gfp_t flags)
55-
{
56-
return kzalloc(sizeof(struct aa_task_ctx), flags);
57-
}
58-
59-
/**
60-
* aa_free_task_ctx - free a task_ctx
61-
* @ctx: task_ctx to free (MAYBE NULL)
62-
*/
63-
void aa_free_task_ctx(struct aa_task_ctx *ctx)
64-
{
65-
if (ctx) {
66-
aa_put_label(ctx->previous);
67-
aa_put_label(ctx->onexec);
68-
69-
kzfree(ctx);
70-
}
71-
}
72-
73-
/**
74-
* aa_dup_task_ctx - duplicate a task context, incrementing reference counts
75-
* @new: a blank task context (NOT NULL)
76-
* @old: the task context to copy (NOT NULL)
77-
*/
78-
void aa_dup_task_ctx(struct aa_task_ctx *new, const struct aa_task_ctx *old)
79-
{
80-
*new = *old;
81-
aa_get_label(new->previous);
82-
aa_get_label(new->onexec);
83-
}
84-
8539
/**
8640
* aa_replace_current_label - replace the current tasks label
8741
* @label: new label (NOT NULL)
@@ -110,7 +64,7 @@ int aa_replace_current_label(struct aa_label *label)
11064
* if switching to unconfined or a different label namespace
11165
* clear out context state
11266
*/
113-
aa_clear_task_ctx_trans(current_task_ctx());
67+
aa_clear_task_ctx_trans(task_ctx(current));
11468

11569
/*
11670
* be careful switching cred label, when racing replacement it
@@ -126,6 +80,7 @@ int aa_replace_current_label(struct aa_label *label)
12680
return 0;
12781
}
12882

83+
12984
/**
13085
* aa_set_current_onexec - set the tasks change_profile to happen onexec
13186
* @label: system label to set at exec (MAYBE NULL to clear value)
@@ -134,7 +89,7 @@ int aa_replace_current_label(struct aa_label *label)
13489
*/
13590
int aa_set_current_onexec(struct aa_label *label, bool stack)
13691
{
137-
struct aa_task_ctx *ctx = current_task_ctx();
92+
struct aa_task_ctx *ctx = task_ctx(current);
13893

13994
aa_get_label(label);
14095
aa_put_label(ctx->onexec);
@@ -156,7 +111,7 @@ int aa_set_current_onexec(struct aa_label *label, bool stack)
156111
*/
157112
int aa_set_current_hat(struct aa_label *label, u64 token)
158113
{
159-
struct aa_task_ctx *ctx = current_task_ctx();
114+
struct aa_task_ctx *ctx = task_ctx(current);
160115
struct cred *new;
161116

162117
new = prepare_creds();
@@ -196,7 +151,7 @@ int aa_set_current_hat(struct aa_label *label, u64 token)
196151
*/
197152
int aa_restore_previous_label(u64 token)
198153
{
199-
struct aa_task_ctx *ctx = current_task_ctx();
154+
struct aa_task_ctx *ctx = task_ctx(current);
200155
struct cred *new;
201156

202157
if (ctx->token != token)

0 commit comments

Comments
 (0)