Path: blob/master/security/apparmor/include/file.h
10817 views
/*1* AppArmor security module2*3* This file contains AppArmor file mediation function 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_FILE_H15#define __AA_FILE_H1617#include "domain.h"18#include "match.h"1920struct aa_profile;21struct path;2223/*24* We use MAY_EXEC, MAY_WRITE, MAY_READ, MAY_APPEND and the following flags25* for profile permissions26*/27#define AA_MAY_CREATE 0x001028#define AA_MAY_DELETE 0x002029#define AA_MAY_META_WRITE 0x004030#define AA_MAY_META_READ 0x00803132#define AA_MAY_CHMOD 0x010033#define AA_MAY_CHOWN 0x020034#define AA_MAY_LOCK 0x040035#define AA_EXEC_MMAP 0x08003637#define AA_MAY_LINK 0x100038#define AA_LINK_SUBSET AA_MAY_LOCK /* overlaid */39#define AA_MAY_ONEXEC 0x40000000 /* exec allows onexec */40#define AA_MAY_CHANGE_PROFILE 0x8000000041#define AA_MAY_CHANGEHAT 0x80000000 /* ctrl auditing only */4243#define AA_AUDIT_FILE_MASK (MAY_READ | MAY_WRITE | MAY_EXEC | MAY_APPEND |\44AA_MAY_CREATE | AA_MAY_DELETE | \45AA_MAY_META_READ | AA_MAY_META_WRITE | \46AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \47AA_EXEC_MMAP | AA_MAY_LINK)4849/*50* The xindex is broken into 3 parts51* - index - an index into either the exec name table or the variable table52* - exec type - which determines how the executable name and index are used53* - flags - which modify how the destination name is applied54*/55#define AA_X_INDEX_MASK 0x03ff5657#define AA_X_TYPE_MASK 0x0c0058#define AA_X_TYPE_SHIFT 1059#define AA_X_NONE 0x000060#define AA_X_NAME 0x0400 /* use executable name px */61#define AA_X_TABLE 0x0800 /* use a specified name ->n# */6263#define AA_X_UNSAFE 0x100064#define AA_X_CHILD 0x2000 /* make >AA_X_NONE apply to children */65#define AA_X_INHERIT 0x400066#define AA_X_UNCONFINED 0x80006768/* AA_SECURE_X_NEEDED - is passed in the bprm->unsafe field */69#define AA_SECURE_X_NEEDED 0x80007071/* need to make conditional which ones are being set */72struct path_cond {73uid_t uid;74umode_t mode;75};7677/* struct file_perms - file permission78* @allow: mask of permissions that are allowed79* @audit: mask of permissions to force an audit message for80* @quiet: mask of permissions to quiet audit messages for81* @kill: mask of permissions that when matched will kill the task82* @xindex: exec transition index if @allow contains MAY_EXEC83*84* The @audit and @queit mask should be mutually exclusive.85*/86struct file_perms {87u32 allow;88u32 audit;89u32 quiet;90u32 kill;91u16 xindex;92};9394extern struct file_perms nullperms;9596#define COMBINED_PERM_MASK(X) ((X).allow | (X).audit | (X).quiet | (X).kill)9798/* FIXME: split perms from dfa and match this to description99* also add delegation info.100*/101static inline u16 dfa_map_xindex(u16 mask)102{103u16 old_index = (mask >> 10) & 0xf;104u16 index = 0;105106if (mask & 0x100)107index |= AA_X_UNSAFE;108if (mask & 0x200)109index |= AA_X_INHERIT;110if (mask & 0x80)111index |= AA_X_UNCONFINED;112113if (old_index == 1) {114index |= AA_X_UNCONFINED;115} else if (old_index == 2) {116index |= AA_X_NAME;117} else if (old_index == 3) {118index |= AA_X_NAME | AA_X_CHILD;119} else {120index |= AA_X_TABLE;121index |= old_index - 4;122}123124return index;125}126127/*128* map old dfa inline permissions to new format129*/130#define dfa_user_allow(dfa, state) (((ACCEPT_TABLE(dfa)[state]) & 0x7f) | \131((ACCEPT_TABLE(dfa)[state]) & 0x80000000))132#define dfa_user_audit(dfa, state) ((ACCEPT_TABLE2(dfa)[state]) & 0x7f)133#define dfa_user_quiet(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 7) & 0x7f)134#define dfa_user_xindex(dfa, state) \135(dfa_map_xindex(ACCEPT_TABLE(dfa)[state] & 0x3fff))136137#define dfa_other_allow(dfa, state) ((((ACCEPT_TABLE(dfa)[state]) >> 14) & \1380x7f) | \139((ACCEPT_TABLE(dfa)[state]) & 0x80000000))140#define dfa_other_audit(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 14) & 0x7f)141#define dfa_other_quiet(dfa, state) \142((((ACCEPT_TABLE2(dfa)[state]) >> 7) >> 14) & 0x7f)143#define dfa_other_xindex(dfa, state) \144dfa_map_xindex((ACCEPT_TABLE(dfa)[state] >> 14) & 0x3fff)145146int aa_audit_file(struct aa_profile *profile, struct file_perms *perms,147gfp_t gfp, int op, u32 request, const char *name,148const char *target, uid_t ouid, const char *info, int error);149150/**151* struct aa_file_rules - components used for file rule permissions152* @dfa: dfa to match path names and conditionals against153* @perms: permission table indexed by the matched state accept entry of @dfa154* @trans: transition table for indexed by named x transitions155*156* File permission are determined by matching a path against @dfa and then157* then using the value of the accept entry for the matching state as158* an index into @perms. If a named exec transition is required it is159* looked up in the transition table.160*/161struct aa_file_rules {162unsigned int start;163struct aa_dfa *dfa;164/* struct perms perms; */165struct aa_domain trans;166/* TODO: add delegate table */167};168169unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,170const char *name, struct path_cond *cond,171struct file_perms *perms);172173int aa_path_perm(int op, struct aa_profile *profile, struct path *path,174int flags, u32 request, struct path_cond *cond);175176int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry,177struct path *new_dir, struct dentry *new_dentry);178179int aa_file_perm(int op, struct aa_profile *profile, struct file *file,180u32 request);181182static inline void aa_free_file_rules(struct aa_file_rules *rules)183{184aa_put_dfa(rules->dfa);185aa_free_domain_entries(&rules->trans);186}187188#define ACC_FMODE(x) (("\000\004\002\006"[(x)&O_ACCMODE]) | (((x) << 1) & 0x40))189190/* from namei.c */191#define MAP_OPEN_FLAGS(x) ((((x) + 1) & O_ACCMODE) ? (x) + 1 : (x))192193/**194* aa_map_file_perms - map file flags to AppArmor permissions195* @file: open file to map flags to AppArmor permissions196*197* Returns: apparmor permission set for the file198*/199static inline u32 aa_map_file_to_perms(struct file *file)200{201int flags = MAP_OPEN_FLAGS(file->f_flags);202u32 perms = ACC_FMODE(file->f_mode);203204if ((flags & O_APPEND) && (perms & MAY_WRITE))205perms = (perms & ~MAY_WRITE) | MAY_APPEND;206/* trunc implies write permission */207if (flags & O_TRUNC)208perms |= MAY_WRITE;209if (flags & O_CREAT)210perms |= AA_MAY_CREATE;211212return perms;213}214215#endif /* __AA_FILE_H */216217218