Path: blob/master/security/apparmor/include/policy.h
10817 views
/*1* AppArmor security module2*3* This file contains AppArmor policy definitions.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_POLICY_H15#define __AA_POLICY_H1617#include <linux/capability.h>18#include <linux/cred.h>19#include <linux/kref.h>20#include <linux/sched.h>21#include <linux/slab.h>22#include <linux/socket.h>2324#include "apparmor.h"25#include "audit.h"26#include "capability.h"27#include "domain.h"28#include "file.h"29#include "resource.h"3031extern const char *profile_mode_names[];32#define APPARMOR_NAMES_MAX_INDEX 33334#define COMPLAIN_MODE(_profile) \35((aa_g_profile_mode == APPARMOR_COMPLAIN) || \36((_profile)->mode == APPARMOR_COMPLAIN))3738#define KILL_MODE(_profile) \39((aa_g_profile_mode == APPARMOR_KILL) || \40((_profile)->mode == APPARMOR_KILL))4142#define PROFILE_IS_HAT(_profile) ((_profile)->flags & PFLAG_HAT)4344/*45* FIXME: currently need a clean way to replace and remove profiles as a46* set. It should be done at the namespace level.47* Either, with a set of profiles loaded at the namespace level or via48* a mark and remove marked interface.49*/50enum profile_mode {51APPARMOR_ENFORCE, /* enforce access rules */52APPARMOR_COMPLAIN, /* allow and log access violations */53APPARMOR_KILL, /* kill task on access violation */54};5556enum profile_flags {57PFLAG_HAT = 1, /* profile is a hat */58PFLAG_UNCONFINED = 2, /* profile is an unconfined profile */59PFLAG_NULL = 4, /* profile is null learning profile */60PFLAG_IX_ON_NAME_ERROR = 8, /* fallback to ix on name lookup fail */61PFLAG_IMMUTABLE = 0x10, /* don't allow changes/replacement */62PFLAG_USER_DEFINED = 0x20, /* user based profile - lower privs */63PFLAG_NO_LIST_REF = 0x40, /* list doesn't keep profile ref */64PFLAG_OLD_NULL_TRANS = 0x100, /* use // as the null transition */6566/* These flags must correspond with PATH_flags */67PFLAG_MEDIATE_DELETED = 0x10000, /* mediate instead delegate deleted */68};6970struct aa_profile;7172/* struct aa_policy - common part of both namespaces and profiles73* @name: name of the object74* @hname - The hierarchical name75* @count: reference count of the obj76* @list: list policy object is on77* @profiles: head of the profiles list contained in the object78*/79struct aa_policy {80char *name;81char *hname;82struct kref count;83struct list_head list;84struct list_head profiles;85};8687/* struct aa_ns_acct - accounting of profiles in namespace88* @max_size: maximum space allowed for all profiles in namespace89* @max_count: maximum number of profiles that can be in this namespace90* @size: current size of profiles91* @count: current count of profiles (includes null profiles)92*/93struct aa_ns_acct {94int max_size;95int max_count;96int size;97int count;98};99100/* struct aa_namespace - namespace for a set of profiles101* @base: common policy102* @parent: parent of namespace103* @lock: lock for modifying the object104* @acct: accounting for the namespace105* @unconfined: special unconfined profile for the namespace106* @sub_ns: list of namespaces under the current namespace.107*108* An aa_namespace defines the set profiles that are searched to determine109* which profile to attach to a task. Profiles can not be shared between110* aa_namespaces and profile names within a namespace are guaranteed to be111* unique. When profiles in separate namespaces have the same name they112* are NOT considered to be equivalent.113*114* Namespaces are hierarchical and only namespaces and profiles below the115* current namespace are visible.116*117* Namespace names must be unique and can not contain the characters :/\0118*119* FIXME TODO: add vserver support of namespaces (can it all be done in120* userspace?)121*/122struct aa_namespace {123struct aa_policy base;124struct aa_namespace *parent;125rwlock_t lock;126struct aa_ns_acct acct;127struct aa_profile *unconfined;128struct list_head sub_ns;129};130131/* struct aa_profile - basic confinement data132* @base - base components of the profile (name, refcount, lists, lock ...)133* @parent: parent of profile134* @ns: namespace the profile is in135* @replacedby: is set to the profile that replaced this profile136* @rename: optional profile name that this profile renamed137* @xmatch: optional extended matching for unconfined executables names138* @xmatch_len: xmatch prefix len, used to determine xmatch priority139* @sid: the unique security id number of this profile140* @audit: the auditing mode of the profile141* @mode: the enforcement mode of the profile142* @flags: flags controlling profile behavior143* @path_flags: flags controlling path generation behavior144* @size: the memory consumed by this profiles rules145* @file: The set of rules governing basic file access and domain transitions146* @caps: capabilities for the profile147* @rlimits: rlimits for the profile148*149* The AppArmor profile contains the basic confinement data. Each profile150* has a name, and exists in a namespace. The @name and @exec_match are151* used to determine profile attachment against unconfined tasks. All other152* attachments are determined by profile X transition rules.153*154* The @replacedby field is write protected by the profile lock. Reads155* are assumed to be atomic, and are done without locking.156*157* Profiles have a hierarchy where hats and children profiles keep158* a reference to their parent.159*160* Profile names can not begin with a : and can not contain the \0161* character. If a profile name begins with / it will be considered when162* determining profile attachment on "unconfined" tasks.163*/164struct aa_profile {165struct aa_policy base;166struct aa_profile *parent;167168struct aa_namespace *ns;169struct aa_profile *replacedby;170const char *rename;171172struct aa_dfa *xmatch;173int xmatch_len;174u32 sid;175enum audit_mode audit;176enum profile_mode mode;177u32 flags;178u32 path_flags;179int size;180181struct aa_file_rules file;182struct aa_caps caps;183struct aa_rlimit rlimits;184};185186extern struct aa_namespace *root_ns;187extern enum profile_mode aa_g_profile_mode;188189void aa_add_profile(struct aa_policy *common, struct aa_profile *profile);190191bool aa_ns_visible(struct aa_namespace *curr, struct aa_namespace *view);192const char *aa_ns_name(struct aa_namespace *parent, struct aa_namespace *child);193int aa_alloc_root_ns(void);194void aa_free_root_ns(void);195void aa_free_namespace_kref(struct kref *kref);196197struct aa_namespace *aa_find_namespace(struct aa_namespace *root,198const char *name);199200static inline struct aa_policy *aa_get_common(struct aa_policy *c)201{202if (c)203kref_get(&c->count);204205return c;206}207208/**209* aa_get_namespace - increment references count on @ns210* @ns: namespace to increment reference count of (MAYBE NULL)211*212* Returns: pointer to @ns, if @ns is NULL returns NULL213* Requires: @ns must be held with valid refcount when called214*/215static inline struct aa_namespace *aa_get_namespace(struct aa_namespace *ns)216{217if (ns)218kref_get(&(ns->base.count));219220return ns;221}222223/**224* aa_put_namespace - decrement refcount on @ns225* @ns: namespace to put reference of226*227* Decrement reference count of @ns and if no longer in use free it228*/229static inline void aa_put_namespace(struct aa_namespace *ns)230{231if (ns)232kref_put(&ns->base.count, aa_free_namespace_kref);233}234235struct aa_profile *aa_alloc_profile(const char *name);236struct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat);237void aa_free_profile_kref(struct kref *kref);238struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name);239struct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *name);240struct aa_profile *aa_match_profile(struct aa_namespace *ns, const char *name);241242ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace);243ssize_t aa_remove_profiles(char *name, size_t size);244245#define PROF_ADD 1246#define PROF_REPLACE 0247248#define unconfined(X) ((X)->flags & PFLAG_UNCONFINED)249250/**251* aa_newest_version - find the newest version of @profile252* @profile: the profile to check for newer versions of (NOT NULL)253*254* Returns: newest version of @profile, if @profile is the newest version255* return @profile.256*257* NOTE: the profile returned is not refcounted, The refcount on @profile258* must be held until the caller decides what to do with the returned newest259* version.260*/261static inline struct aa_profile *aa_newest_version(struct aa_profile *profile)262{263while (profile->replacedby)264profile = profile->replacedby;265266return profile;267}268269/**270* aa_get_profile - increment refcount on profile @p271* @p: profile (MAYBE NULL)272*273* Returns: pointer to @p if @p is NULL will return NULL274* Requires: @p must be held with valid refcount when called275*/276static inline struct aa_profile *aa_get_profile(struct aa_profile *p)277{278if (p)279kref_get(&(p->base.count));280281return p;282}283284/**285* aa_put_profile - decrement refcount on profile @p286* @p: profile (MAYBE NULL)287*/288static inline void aa_put_profile(struct aa_profile *p)289{290if (p)291kref_put(&p->base.count, aa_free_profile_kref);292}293294static inline int AUDIT_MODE(struct aa_profile *profile)295{296if (aa_g_audit != AUDIT_NORMAL)297return aa_g_audit;298299return profile->audit;300}301302bool aa_may_manage_policy(int op);303304#endif /* __AA_POLICY_H */305306307