16380bd8dSJohn Johansen /* 26380bd8dSJohn Johansen * AppArmor security module 36380bd8dSJohn Johansen * 46380bd8dSJohn Johansen * This file contains AppArmor file mediation function definitions. 56380bd8dSJohn Johansen * 66380bd8dSJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 76380bd8dSJohn Johansen * Copyright 2009-2010 Canonical Ltd. 86380bd8dSJohn Johansen * 96380bd8dSJohn Johansen * This program is free software; you can redistribute it and/or 106380bd8dSJohn Johansen * modify it under the terms of the GNU General Public License as 116380bd8dSJohn Johansen * published by the Free Software Foundation, version 2 of the 126380bd8dSJohn Johansen * License. 136380bd8dSJohn Johansen */ 146380bd8dSJohn Johansen 156380bd8dSJohn Johansen #ifndef __AA_FILE_H 166380bd8dSJohn Johansen #define __AA_FILE_H 176380bd8dSJohn Johansen 186380bd8dSJohn Johansen #include "domain.h" 196380bd8dSJohn Johansen #include "match.h" 20fc7e0b26SJohn Johansen #include "perms.h" 216380bd8dSJohn Johansen 226380bd8dSJohn Johansen struct aa_profile; 2337721e1bSAlexey Dobriyan struct path; 246380bd8dSJohn Johansen 25e53cfe6cSJohn Johansen #define mask_mode_t(X) (X & (MAY_EXEC | MAY_WRITE | MAY_READ | MAY_APPEND)) 266380bd8dSJohn Johansen 276380bd8dSJohn Johansen #define AA_AUDIT_FILE_MASK (MAY_READ | MAY_WRITE | MAY_EXEC | MAY_APPEND |\ 286380bd8dSJohn Johansen AA_MAY_CREATE | AA_MAY_DELETE | \ 29e53cfe6cSJohn Johansen AA_MAY_GETATTR | AA_MAY_SETATTR | \ 306380bd8dSJohn Johansen AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \ 316380bd8dSJohn Johansen AA_EXEC_MMAP | AA_MAY_LINK) 326380bd8dSJohn Johansen 33af7caa8fSJohn Johansen /* struct aa_file_ctx - the AppArmor context the file was opened in 34af7caa8fSJohn Johansen * @perms: the permission the file was opened with 35af7caa8fSJohn Johansen * 36af7caa8fSJohn Johansen * The file_ctx could currently be directly stored in file->f_security 37af7caa8fSJohn Johansen * as the profile reference is now stored in the f_cred. However the 38af7caa8fSJohn Johansen * ctx struct will expand in the future so we keep the struct. 39af7caa8fSJohn Johansen */ 40af7caa8fSJohn Johansen struct aa_file_ctx { 41e53cfe6cSJohn Johansen u32 allow; 42af7caa8fSJohn Johansen }; 43af7caa8fSJohn Johansen 44af7caa8fSJohn Johansen /** 45af7caa8fSJohn Johansen * aa_alloc_file_context - allocate file_ctx 46af7caa8fSJohn Johansen * @gfp: gfp flags for allocation 47af7caa8fSJohn Johansen * 48af7caa8fSJohn Johansen * Returns: file_ctx or NULL on failure 49af7caa8fSJohn Johansen */ 50af7caa8fSJohn Johansen static inline struct aa_file_ctx *aa_alloc_file_context(gfp_t gfp) 51af7caa8fSJohn Johansen { 52af7caa8fSJohn Johansen return kzalloc(sizeof(struct aa_file_ctx), gfp); 53af7caa8fSJohn Johansen } 54af7caa8fSJohn Johansen 55af7caa8fSJohn Johansen /** 56af7caa8fSJohn Johansen * aa_free_file_context - free a file_ctx 57af7caa8fSJohn Johansen * @ctx: file_ctx to free (MAYBE_NULL) 58af7caa8fSJohn Johansen */ 59af7caa8fSJohn Johansen static inline void aa_free_file_context(struct aa_file_ctx *ctx) 60af7caa8fSJohn Johansen { 61af7caa8fSJohn Johansen if (ctx) 62af7caa8fSJohn Johansen kzfree(ctx); 63af7caa8fSJohn Johansen } 64af7caa8fSJohn Johansen 656380bd8dSJohn Johansen /* 666380bd8dSJohn Johansen * The xindex is broken into 3 parts 676380bd8dSJohn Johansen * - index - an index into either the exec name table or the variable table 686380bd8dSJohn Johansen * - exec type - which determines how the executable name and index are used 696380bd8dSJohn Johansen * - flags - which modify how the destination name is applied 706380bd8dSJohn Johansen */ 716380bd8dSJohn Johansen #define AA_X_INDEX_MASK 0x03ff 726380bd8dSJohn Johansen 736380bd8dSJohn Johansen #define AA_X_TYPE_MASK 0x0c00 746380bd8dSJohn Johansen #define AA_X_TYPE_SHIFT 10 756380bd8dSJohn Johansen #define AA_X_NONE 0x0000 766380bd8dSJohn Johansen #define AA_X_NAME 0x0400 /* use executable name px */ 776380bd8dSJohn Johansen #define AA_X_TABLE 0x0800 /* use a specified name ->n# */ 786380bd8dSJohn Johansen 796380bd8dSJohn Johansen #define AA_X_UNSAFE 0x1000 806380bd8dSJohn Johansen #define AA_X_CHILD 0x2000 /* make >AA_X_NONE apply to children */ 816380bd8dSJohn Johansen #define AA_X_INHERIT 0x4000 826380bd8dSJohn Johansen #define AA_X_UNCONFINED 0x8000 836380bd8dSJohn Johansen 846380bd8dSJohn Johansen /* AA_SECURE_X_NEEDED - is passed in the bprm->unsafe field */ 856380bd8dSJohn Johansen #define AA_SECURE_X_NEEDED 0x8000 866380bd8dSJohn Johansen 876380bd8dSJohn Johansen /* need to make conditional which ones are being set */ 886380bd8dSJohn Johansen struct path_cond { 892db81452SEric W. Biederman kuid_t uid; 906380bd8dSJohn Johansen umode_t mode; 916380bd8dSJohn Johansen }; 926380bd8dSJohn Johansen 936380bd8dSJohn Johansen #define COMBINED_PERM_MASK(X) ((X).allow | (X).audit | (X).quiet | (X).kill) 946380bd8dSJohn Johansen 956380bd8dSJohn Johansen /* FIXME: split perms from dfa and match this to description 966380bd8dSJohn Johansen * also add delegation info. 976380bd8dSJohn Johansen */ 986380bd8dSJohn Johansen static inline u16 dfa_map_xindex(u16 mask) 996380bd8dSJohn Johansen { 1006380bd8dSJohn Johansen u16 old_index = (mask >> 10) & 0xf; 1016380bd8dSJohn Johansen u16 index = 0; 1026380bd8dSJohn Johansen 1036380bd8dSJohn Johansen if (mask & 0x100) 1046380bd8dSJohn Johansen index |= AA_X_UNSAFE; 1056380bd8dSJohn Johansen if (mask & 0x200) 1066380bd8dSJohn Johansen index |= AA_X_INHERIT; 1076380bd8dSJohn Johansen if (mask & 0x80) 1086380bd8dSJohn Johansen index |= AA_X_UNCONFINED; 1096380bd8dSJohn Johansen 1106380bd8dSJohn Johansen if (old_index == 1) { 1116380bd8dSJohn Johansen index |= AA_X_UNCONFINED; 1126380bd8dSJohn Johansen } else if (old_index == 2) { 1136380bd8dSJohn Johansen index |= AA_X_NAME; 1146380bd8dSJohn Johansen } else if (old_index == 3) { 1156380bd8dSJohn Johansen index |= AA_X_NAME | AA_X_CHILD; 1168b964eaeSJohn Johansen } else if (old_index) { 1176380bd8dSJohn Johansen index |= AA_X_TABLE; 1186380bd8dSJohn Johansen index |= old_index - 4; 1196380bd8dSJohn Johansen } 1206380bd8dSJohn Johansen 1216380bd8dSJohn Johansen return index; 1226380bd8dSJohn Johansen } 1236380bd8dSJohn Johansen 1246380bd8dSJohn Johansen /* 1256380bd8dSJohn Johansen * map old dfa inline permissions to new format 1266380bd8dSJohn Johansen */ 1276380bd8dSJohn Johansen #define dfa_user_allow(dfa, state) (((ACCEPT_TABLE(dfa)[state]) & 0x7f) | \ 1286380bd8dSJohn Johansen ((ACCEPT_TABLE(dfa)[state]) & 0x80000000)) 1296380bd8dSJohn Johansen #define dfa_user_audit(dfa, state) ((ACCEPT_TABLE2(dfa)[state]) & 0x7f) 1306380bd8dSJohn Johansen #define dfa_user_quiet(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 7) & 0x7f) 1316380bd8dSJohn Johansen #define dfa_user_xindex(dfa, state) \ 1326380bd8dSJohn Johansen (dfa_map_xindex(ACCEPT_TABLE(dfa)[state] & 0x3fff)) 1336380bd8dSJohn Johansen 1346380bd8dSJohn Johansen #define dfa_other_allow(dfa, state) ((((ACCEPT_TABLE(dfa)[state]) >> 14) & \ 1356380bd8dSJohn Johansen 0x7f) | \ 1366380bd8dSJohn Johansen ((ACCEPT_TABLE(dfa)[state]) & 0x80000000)) 1376380bd8dSJohn Johansen #define dfa_other_audit(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 14) & 0x7f) 1386380bd8dSJohn Johansen #define dfa_other_quiet(dfa, state) \ 1396380bd8dSJohn Johansen ((((ACCEPT_TABLE2(dfa)[state]) >> 7) >> 14) & 0x7f) 1406380bd8dSJohn Johansen #define dfa_other_xindex(dfa, state) \ 1416380bd8dSJohn Johansen dfa_map_xindex((ACCEPT_TABLE(dfa)[state] >> 14) & 0x3fff) 1426380bd8dSJohn Johansen 1432d679f3cSJohn Johansen int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms, 144ef88a7acSJohn Johansen const char *op, u32 request, const char *name, 1452db81452SEric W. Biederman const char *target, kuid_t ouid, const char *info, int error); 1466380bd8dSJohn Johansen 1476380bd8dSJohn Johansen /** 1486380bd8dSJohn Johansen * struct aa_file_rules - components used for file rule permissions 1496380bd8dSJohn Johansen * @dfa: dfa to match path names and conditionals against 1506380bd8dSJohn Johansen * @perms: permission table indexed by the matched state accept entry of @dfa 1516380bd8dSJohn Johansen * @trans: transition table for indexed by named x transitions 1526380bd8dSJohn Johansen * 1536380bd8dSJohn Johansen * File permission are determined by matching a path against @dfa and then 1546380bd8dSJohn Johansen * then using the value of the accept entry for the matching state as 1556380bd8dSJohn Johansen * an index into @perms. If a named exec transition is required it is 1566380bd8dSJohn Johansen * looked up in the transition table. 1576380bd8dSJohn Johansen */ 1586380bd8dSJohn Johansen struct aa_file_rules { 1596380bd8dSJohn Johansen unsigned int start; 1606380bd8dSJohn Johansen struct aa_dfa *dfa; 1616380bd8dSJohn Johansen /* struct perms perms; */ 1626380bd8dSJohn Johansen struct aa_domain trans; 1636380bd8dSJohn Johansen /* TODO: add delegate table */ 1646380bd8dSJohn Johansen }; 1656380bd8dSJohn Johansen 1662d679f3cSJohn Johansen struct aa_perms aa_compute_fperms(struct aa_dfa *dfa, unsigned int state, 1672d679f3cSJohn Johansen struct path_cond *cond); 1686380bd8dSJohn Johansen unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start, 1696380bd8dSJohn Johansen const char *name, struct path_cond *cond, 1702d679f3cSJohn Johansen struct aa_perms *perms); 1716380bd8dSJohn Johansen 17247f6e5ccSJohn Johansen int aa_path_perm(const char *op, struct aa_profile *profile, 17347f6e5ccSJohn Johansen const struct path *path, int flags, u32 request, 17447f6e5ccSJohn Johansen struct path_cond *cond); 1756380bd8dSJohn Johansen 1766380bd8dSJohn Johansen int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry, 1773539aaf6SAl Viro const struct path *new_dir, struct dentry *new_dentry); 1786380bd8dSJohn Johansen 17947f6e5ccSJohn Johansen int aa_file_perm(const char *op, struct aa_profile *profile, struct file *file, 1806380bd8dSJohn Johansen u32 request); 1816380bd8dSJohn Johansen 1826380bd8dSJohn Johansen static inline void aa_free_file_rules(struct aa_file_rules *rules) 1836380bd8dSJohn Johansen { 1846380bd8dSJohn Johansen aa_put_dfa(rules->dfa); 1856380bd8dSJohn Johansen aa_free_domain_entries(&rules->trans); 1866380bd8dSJohn Johansen } 1876380bd8dSJohn Johansen 1886380bd8dSJohn Johansen /** 1896380bd8dSJohn Johansen * aa_map_file_perms - map file flags to AppArmor permissions 1906380bd8dSJohn Johansen * @file: open file to map flags to AppArmor permissions 1916380bd8dSJohn Johansen * 1926380bd8dSJohn Johansen * Returns: apparmor permission set for the file 1936380bd8dSJohn Johansen */ 1946380bd8dSJohn Johansen static inline u32 aa_map_file_to_perms(struct file *file) 1956380bd8dSJohn Johansen { 19653fe8b99SJohn Johansen int flags = file->f_flags; 19753fe8b99SJohn Johansen u32 perms = 0; 19853fe8b99SJohn Johansen 19953fe8b99SJohn Johansen if (file->f_mode & FMODE_WRITE) 20053fe8b99SJohn Johansen perms |= MAY_WRITE; 20153fe8b99SJohn Johansen if (file->f_mode & FMODE_READ) 20253fe8b99SJohn Johansen perms |= MAY_READ; 2036380bd8dSJohn Johansen 2046380bd8dSJohn Johansen if ((flags & O_APPEND) && (perms & MAY_WRITE)) 2056380bd8dSJohn Johansen perms = (perms & ~MAY_WRITE) | MAY_APPEND; 2066380bd8dSJohn Johansen /* trunc implies write permission */ 2076380bd8dSJohn Johansen if (flags & O_TRUNC) 2086380bd8dSJohn Johansen perms |= MAY_WRITE; 2096380bd8dSJohn Johansen if (flags & O_CREAT) 2106380bd8dSJohn Johansen perms |= AA_MAY_CREATE; 2116380bd8dSJohn Johansen 2126380bd8dSJohn Johansen return perms; 2136380bd8dSJohn Johansen } 2146380bd8dSJohn Johansen 2156380bd8dSJohn Johansen #endif /* __AA_FILE_H */ 216