Path: blob/master/security/apparmor/include/context.h
10817 views
/*1* AppArmor security module2*3* This file contains AppArmor contexts used to associate "labels" to objects.4*5* Copyright (C) 1998-2008 Novell/SUSE6* Copyright 2009-2010 Canonical Ltd.7*8* This program is free software; you can redistribute it and/or9* modify it under the terms of the GNU General Public License as10* published by the Free Software Foundation, version 2 of the11* License.12*/1314#ifndef __AA_CONTEXT_H15#define __AA_CONTEXT_H1617#include <linux/cred.h>18#include <linux/slab.h>19#include <linux/sched.h>2021#include "policy.h"2223/* struct aa_file_cxt - the AppArmor context the file was opened in24* @perms: the permission the file was opened with25*26* The file_cxt could currently be directly stored in file->f_security27* as the profile reference is now stored in the f_cred. However the28* cxt struct will expand in the future so we keep the struct.29*/30struct aa_file_cxt {31u16 allow;32};3334/**35* aa_alloc_file_context - allocate file_cxt36* @gfp: gfp flags for allocation37*38* Returns: file_cxt or NULL on failure39*/40static inline struct aa_file_cxt *aa_alloc_file_context(gfp_t gfp)41{42return kzalloc(sizeof(struct aa_file_cxt), gfp);43}4445/**46* aa_free_file_context - free a file_cxt47* @cxt: file_cxt to free (MAYBE_NULL)48*/49static inline void aa_free_file_context(struct aa_file_cxt *cxt)50{51if (cxt)52kzfree(cxt);53}5455/**56* struct aa_task_cxt - primary label for confined tasks57* @profile: the current profile (NOT NULL)58* @exec: profile to transition to on next exec (MAYBE NULL)59* @previous: profile the task may return to (MAYBE NULL)60* @token: magic value the task must know for returning to @previous_profile61*62* Contains the task's current profile (which could change due to63* change_hat). Plus the hat_magic needed during change_hat.64*65* TODO: make so a task can be confined by a stack of contexts66*/67struct aa_task_cxt {68struct aa_profile *profile;69struct aa_profile *onexec;70struct aa_profile *previous;71u64 token;72};7374struct aa_task_cxt *aa_alloc_task_context(gfp_t flags);75void aa_free_task_context(struct aa_task_cxt *cxt);76void aa_dup_task_context(struct aa_task_cxt *new,77const struct aa_task_cxt *old);78int aa_replace_current_profile(struct aa_profile *profile);79int aa_set_current_onexec(struct aa_profile *profile);80int aa_set_current_hat(struct aa_profile *profile, u64 token);81int aa_restore_previous_profile(u64 cookie);8283/**84* __aa_task_is_confined - determine if @task has any confinement85* @task: task to check confinement of (NOT NULL)86*87* If @task != current needs to be called in RCU safe critical section88*/89static inline bool __aa_task_is_confined(struct task_struct *task)90{91struct aa_task_cxt *cxt = __task_cred(task)->security;9293BUG_ON(!cxt || !cxt->profile);94if (unconfined(aa_newest_version(cxt->profile)))95return 0;9697return 1;98}99100/**101* aa_cred_profile - obtain cred's profiles102* @cred: cred to obtain profiles from (NOT NULL)103*104* Returns: confining profile105*106* does NOT increment reference count107*/108static inline struct aa_profile *aa_cred_profile(const struct cred *cred)109{110struct aa_task_cxt *cxt = cred->security;111BUG_ON(!cxt || !cxt->profile);112return aa_newest_version(cxt->profile);113}114115/**116* __aa_current_profile - find the current tasks confining profile117*118* Returns: up to date confining profile or the ns unconfined profile (NOT NULL)119*120* This fn will not update the tasks cred to the most up to date version121* of the profile so it is safe to call when inside of locks.122*/123static inline struct aa_profile *__aa_current_profile(void)124{125return aa_cred_profile(current_cred());126}127128/**129* aa_current_profile - find the current tasks confining profile and do updates130*131* Returns: up to date confining profile or the ns unconfined profile (NOT NULL)132*133* This fn will update the tasks cred structure if the profile has been134* replaced. Not safe to call inside locks135*/136static inline struct aa_profile *aa_current_profile(void)137{138const struct aa_task_cxt *cxt = current_cred()->security;139struct aa_profile *profile;140BUG_ON(!cxt || !cxt->profile);141142profile = aa_newest_version(cxt->profile);143/*144* Whether or not replacement succeeds, use newest profile so145* there is no need to update it after replacement.146*/147if (unlikely((cxt->profile != profile)))148aa_replace_current_profile(profile);149150return profile;151}152153#endif /* __AA_CONTEXT_H */154155156