* License.
*
*
- * AppArmor sets confinement on every task, via the the aa_cred_ctx and
- * the aa_cred_ctx.label, both of which are required and are not allowed
- * to be NULL. The aa_cred_ctx is not reference counted and is unique
- * to each cred (which is reference count). The label pointed to by
- * the cred_ctx is reference counted.
+ * AppArmor sets confinement on every task, via the cred_label() which
+ * is required and are not allowed to be NULL. The cred_label is
+ * reference counted.
*
* TODO
* If a task uses change_hat it currently does not return to the old
#include "include/context.h"
#include "include/policy.h"
-/**
- * aa_alloc_cred_ctx - allocate a new cred_ctx
- * @flags: gfp flags for allocation
- *
- * Returns: allocated buffer or NULL on failure
- */
-struct aa_cred_ctx *aa_alloc_cred_ctx(gfp_t flags)
-{
- return kzalloc(sizeof(struct aa_cred_ctx), flags);
-}
-
-/**
- * aa_free_cred_ctx - free a cred_ctx
- * @ctx: cred_ctx to free (MAYBE NULL)
- */
-void aa_free_cred_ctx(struct aa_cred_ctx *ctx)
-{
- if (ctx) {
- aa_put_label(ctx->label);
-
- kzfree(ctx);
- }
-}
-
-/**
- * aa_dup_cred_ctx - duplicate a task context, incrementing reference counts
- * @new: a blank task context (NOT NULL)
- * @old: the task context to copy (NOT NULL)
- */
-void aa_dup_cred_ctx(struct aa_cred_ctx *new, const struct aa_cred_ctx *old)
-{
- *new = *old;
- aa_get_label(new->label);
-}
/**
* aa_get_task_label - Get another task's label
*/
int aa_replace_current_label(struct aa_label *label)
{
- struct aa_cred_ctx *ctx = current_cred_ctx();
+ struct aa_label *old = aa_current_raw_label();
struct cred *new;
+
AA_BUG(!label);
- if (ctx->label == label)
+ if (old == label)
return 0;
if (current_cred() != current_real_cred())
if (!new)
return -ENOMEM;
- ctx = cred_ctx(new);
- if (unconfined(label) || (labels_ns(ctx->label) != labels_ns(label)))
- /* if switching to unconfined or a different label namespace
+ if (unconfined(label) || (labels_ns(old) != labels_ns(label)))
+ /*
+ * if switching to unconfined or a different label namespace
* clear out context state
*/
aa_clear_task_ctx_trans(current_task_ctx());
/*
- * be careful switching ctx->profile, when racing replacement it
- * is possible that ctx->profile->proxy->profile is the reference
- * keeping @profile valid, so make sure to get its reference before
- * dropping the reference on ctx->profile
+ * be careful switching cred label, when racing replacement it
+ * is possible that the cred labels's->proxy->label is the reference
+ * keeping @label valid, so make sure to get its reference before
+ * dropping the reference on the cred's label
*/
aa_get_label(label);
- aa_put_label(ctx->label);
- ctx->label = label;
+ aa_put_label(cred_label(new));
+ cred_label(new) = label;
commit_creds(new);
return 0;
{
struct aa_task_ctx *tctx = current_task_ctx();
struct aa_cred_ctx *ctx;
- struct cred *new = prepare_creds();
+ struct cred *new;
+ new = prepare_creds();
if (!new)
return -ENOMEM;
AA_BUG(!label);
- ctx = cred_ctx(new);
if (!tctx->previous) {
/* transfer refcount */
- tctx->previous = ctx->label;
+ tctx->previous = cred_label(new);
tctx->token = token;
} else if (tctx->token == token) {
- aa_put_label(ctx->label);
+ aa_put_label(cred_label(new));
} else {
/* previous_profile && ctx->token != token */
abort_creds(new);
return -EACCES;
}
- ctx->label = aa_get_newest_label(label);
+ cred_label(new) = aa_get_newest_label(label);
/* clear exec on switching context */
aa_put_label(tctx->onexec);
tctx->onexec = NULL;
int aa_restore_previous_label(u64 token)
{
struct aa_task_ctx *tctx = current_task_ctx();
- struct aa_cred_ctx *ctx;
struct cred *new;
if (tctx->token != token)
new = prepare_creds();
if (!new)
return -ENOMEM;
- ctx = cred_ctx(new);
- aa_put_label(ctx->label);
- ctx->label = aa_get_newest_label(tctx->previous);
- AA_BUG(!ctx->label);
+ aa_put_label(cred_label(new));
+ cred_label(new) = aa_get_newest_label(tctx->previous);
+ AA_BUG(!cred_label(new));
/* clear exec && prev information when restoring to previous context */
aa_clear_task_ctx_trans(tctx);
*/
int apparmor_bprm_set_creds(struct linux_binprm *bprm)
{
- struct aa_cred_ctx *ctx;
struct aa_task_ctx *tctx;
struct aa_label *label, *new = NULL;
struct aa_profile *profile;
if (bprm->called_set_creds)
return 0;
- ctx = cred_ctx(bprm->cred);
tctx = current_task_ctx();
- AA_BUG(!ctx);
+ AA_BUG(!cred_label(bprm->cred));
AA_BUG(!tctx);
- label = aa_get_newest_label(ctx->label);
+ label = aa_get_newest_label(cred_label(bprm->cred));
/* buffer freed below, name is pointer into buffer */
get_buffers(buffer);
}
bprm->per_clear |= PER_CLEAR_ON_SETID;
}
- aa_put_label(ctx->label);
- /* transfer reference, released when ctx is freed */
- ctx->label = new;
+ aa_put_label(cred_label(bprm->cred));
+ /* transfer reference, released when cred is freed */
+ cred_label(bprm->cred) = new;
done:
aa_put_label(label);
int aa_change_hat(const char *hats[], int count, u64 token, int flags)
{
const struct cred *cred;
- struct aa_cred_ctx *ctx;
struct aa_task_ctx *tctx;
struct aa_label *label, *previous, *new = NULL, *target = NULL;
struct aa_profile *profile;
/* released below */
cred = get_current_cred();
- ctx = cred_ctx(cred);
tctx = current_task_ctx();
label = aa_get_newest_cred_label(cred);
previous = aa_get_newest_label(tctx->previous);
#include "label.h"
#include "policy_ns.h"
-#define cred_ctx(X) ((X)->security)
-#define current_cred_ctx() cred_ctx(current_cred())
-
#define task_ctx(X) ((X)->security)
#define current_task_ctx() (task_ctx(current))
+#define cred_label(X) ((X)->security)
-/**
- * struct aa_cred_ctx - primary label for confined tasks
- * @label: the current label (NOT NULL)
- */
-struct aa_cred_ctx {
- struct aa_label *label;
-};
-
-/**
+/*
* struct aa_task_ctx - information for current task label change
* @onexec: profile to transition to on next exec (MAY BE NULL)
* @previous: profile the task may return to (MAY BE NULL)
u64 token;
};
-struct aa_cred_ctx *aa_alloc_cred_ctx(gfp_t flags);
-void aa_free_cred_ctx(struct aa_cred_ctx *ctx);
-void aa_dup_cred_ctx(struct aa_cred_ctx *new, const struct aa_cred_ctx *old);
-
struct aa_task_ctx *aa_alloc_task_ctx(gfp_t flags);
void aa_free_task_ctx(struct aa_task_ctx *ctx);
void aa_dup_task_ctx(struct aa_task_ctx *new, const struct aa_task_ctx *old);
*/
static inline struct aa_label *aa_cred_raw_label(const struct cred *cred)
{
- struct aa_cred_ctx *ctx = cred_ctx(cred);
+ struct aa_label *label = cred_label(cred);
- AA_BUG(!ctx || !ctx->label);
- return ctx->label;
+ AA_BUG(!label);
+ return label;
}
/**
*/
/*
- * free the associated aa_cred_ctx and put its labels
+ * put the associated labels
*/
static void apparmor_cred_free(struct cred *cred)
{
- aa_free_cred_ctx(cred_ctx(cred));
- cred_ctx(cred) = NULL;
+ aa_put_label(cred_label(cred));
+ cred_label(cred) = NULL;
}
/*
*/
static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
{
- /* freed by apparmor_cred_free */
- struct aa_cred_ctx *ctx = aa_alloc_cred_ctx(gfp);
-
- if (!ctx)
- return -ENOMEM;
-
- cred_ctx(cred) = ctx;
+ cred_label(cred) = NULL;
return 0;
}
/*
- * prepare new aa_cred_ctx for modification by prepare_cred block
+ * prepare new cred label for modification by prepare_cred block
*/
static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
gfp_t gfp)
{
- /* freed by apparmor_cred_free */
- struct aa_cred_ctx *ctx = aa_alloc_cred_ctx(gfp);
-
- if (!ctx)
- return -ENOMEM;
-
- aa_dup_cred_ctx(ctx, cred_ctx(old));
- cred_ctx(new) = ctx;
+ cred_label(new) = aa_get_newest_label(cred_label(old));
return 0;
}
*/
static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
{
- const struct aa_cred_ctx *old_ctx = cred_ctx(old);
- struct aa_cred_ctx *new_ctx = cred_ctx(new);
-
- aa_dup_cred_ctx(new_ctx, old_ctx);
+ cred_label(new) = aa_get_newest_label(cred_label(old));
}
static void apparmor_task_free(struct task_struct *task)
/* released below */
const struct cred *cred = get_task_cred(task);
struct aa_task_ctx *tctx = current_task_ctx();
- struct aa_cred_ctx *ctx = cred_ctx(cred);
struct aa_label *label = NULL;
if (strcmp(name, "current") == 0)
- label = aa_get_newest_label(ctx->label);
+ label = aa_get_newest_label(cred_label(cred));
else if (strcmp(name, "prev") == 0 && tctx->previous)
label = aa_get_newest_label(tctx->previous);
else if (strcmp(name, "exec") == 0 && tctx->onexec)
static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
{
struct aa_label *label = aa_current_raw_label();
- struct aa_cred_ctx *new_ctx = cred_ctx(bprm->cred);
+ struct aa_label *new_label = cred_label(bprm->cred);
/* bail out if unconfined or not changing profile */
- if ((new_ctx->label->proxy == label->proxy) ||
- (unconfined(new_ctx->label)))
+ if ((new_label->proxy == label->proxy) ||
+ (unconfined(new_label)))
return;
aa_inherit_files(bprm->cred, current->files);
current->pdeath_signal = 0;
/* reset soft limits and set hard limits for the new label */
- __aa_transition_rlimits(label, new_ctx->label);
+ __aa_transition_rlimits(label, new_label);
}
/**
static int __init set_init_ctx(void)
{
struct cred *cred = (struct cred *)current->real_cred;
- struct aa_cred_ctx *ctx;
struct aa_task_ctx *tctx;
- ctx = aa_alloc_cred_ctx(GFP_KERNEL);
- if (!ctx)
- goto fail_cred;
tctx = aa_alloc_task_ctx(GFP_KERNEL);
if (!tctx)
- goto fail_task;
+ return -ENOMEM;
- ctx->label = aa_get_label(ns_unconfined(root_ns));
- cred_ctx(cred) = ctx;
+ cred_label(cred) = aa_get_label(ns_unconfined(root_ns));
task_ctx(current) = tctx;
return 0;
-
-fail_task:
- aa_free_cred_ctx(ctx);
-fail_cred:
- return -ENOMEM;
}
static void destroy_buffers(void)