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 <linux/path.h> 196380bd8dSJohn Johansen 206380bd8dSJohn Johansen #include "domain.h" 216380bd8dSJohn Johansen #include "match.h" 226380bd8dSJohn Johansen 236380bd8dSJohn Johansen struct aa_profile; 246380bd8dSJohn Johansen 256380bd8dSJohn Johansen /* 266380bd8dSJohn Johansen * We use MAY_EXEC, MAY_WRITE, MAY_READ, MAY_APPEND and the following flags 276380bd8dSJohn Johansen * for profile permissions 286380bd8dSJohn Johansen */ 296380bd8dSJohn Johansen #define AA_MAY_CREATE 0x0010 306380bd8dSJohn Johansen #define AA_MAY_DELETE 0x0020 316380bd8dSJohn Johansen #define AA_MAY_META_WRITE 0x0040 326380bd8dSJohn Johansen #define AA_MAY_META_READ 0x0080 336380bd8dSJohn Johansen 346380bd8dSJohn Johansen #define AA_MAY_CHMOD 0x0100 356380bd8dSJohn Johansen #define AA_MAY_CHOWN 0x0200 366380bd8dSJohn Johansen #define AA_MAY_LOCK 0x0400 376380bd8dSJohn Johansen #define AA_EXEC_MMAP 0x0800 386380bd8dSJohn Johansen 396380bd8dSJohn Johansen #define AA_MAY_LINK 0x1000 406380bd8dSJohn Johansen #define AA_LINK_SUBSET AA_MAY_LOCK /* overlaid */ 416380bd8dSJohn Johansen #define AA_MAY_ONEXEC 0x40000000 /* exec allows onexec */ 426380bd8dSJohn Johansen #define AA_MAY_CHANGE_PROFILE 0x80000000 436380bd8dSJohn Johansen #define AA_MAY_CHANGEHAT 0x80000000 /* ctrl auditing only */ 446380bd8dSJohn Johansen 456380bd8dSJohn Johansen #define AA_AUDIT_FILE_MASK (MAY_READ | MAY_WRITE | MAY_EXEC | MAY_APPEND |\ 466380bd8dSJohn Johansen AA_MAY_CREATE | AA_MAY_DELETE | \ 476380bd8dSJohn Johansen AA_MAY_META_READ | AA_MAY_META_WRITE | \ 486380bd8dSJohn Johansen AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \ 496380bd8dSJohn Johansen AA_EXEC_MMAP | AA_MAY_LINK) 506380bd8dSJohn Johansen 516380bd8dSJohn Johansen /* 526380bd8dSJohn Johansen * The xindex is broken into 3 parts 536380bd8dSJohn Johansen * - index - an index into either the exec name table or the variable table 546380bd8dSJohn Johansen * - exec type - which determines how the executable name and index are used 556380bd8dSJohn Johansen * - flags - which modify how the destination name is applied 566380bd8dSJohn Johansen */ 576380bd8dSJohn Johansen #define AA_X_INDEX_MASK 0x03ff 586380bd8dSJohn Johansen 596380bd8dSJohn Johansen #define AA_X_TYPE_MASK 0x0c00 606380bd8dSJohn Johansen #define AA_X_TYPE_SHIFT 10 616380bd8dSJohn Johansen #define AA_X_NONE 0x0000 626380bd8dSJohn Johansen #define AA_X_NAME 0x0400 /* use executable name px */ 636380bd8dSJohn Johansen #define AA_X_TABLE 0x0800 /* use a specified name ->n# */ 646380bd8dSJohn Johansen 656380bd8dSJohn Johansen #define AA_X_UNSAFE 0x1000 666380bd8dSJohn Johansen #define AA_X_CHILD 0x2000 /* make >AA_X_NONE apply to children */ 676380bd8dSJohn Johansen #define AA_X_INHERIT 0x4000 686380bd8dSJohn Johansen #define AA_X_UNCONFINED 0x8000 696380bd8dSJohn Johansen 706380bd8dSJohn Johansen /* AA_SECURE_X_NEEDED - is passed in the bprm->unsafe field */ 716380bd8dSJohn Johansen #define AA_SECURE_X_NEEDED 0x8000 726380bd8dSJohn Johansen 736380bd8dSJohn Johansen /* need to make conditional which ones are being set */ 746380bd8dSJohn Johansen struct path_cond { 756380bd8dSJohn Johansen uid_t uid; 766380bd8dSJohn Johansen umode_t mode; 776380bd8dSJohn Johansen }; 786380bd8dSJohn Johansen 796380bd8dSJohn Johansen /* struct file_perms - file permission 806380bd8dSJohn Johansen * @allow: mask of permissions that are allowed 816380bd8dSJohn Johansen * @audit: mask of permissions to force an audit message for 826380bd8dSJohn Johansen * @quiet: mask of permissions to quiet audit messages for 836380bd8dSJohn Johansen * @kill: mask of permissions that when matched will kill the task 846380bd8dSJohn Johansen * @xindex: exec transition index if @allow contains MAY_EXEC 856380bd8dSJohn Johansen * 866380bd8dSJohn Johansen * The @audit and @queit mask should be mutually exclusive. 876380bd8dSJohn Johansen */ 886380bd8dSJohn Johansen struct file_perms { 896380bd8dSJohn Johansen u32 allow; 906380bd8dSJohn Johansen u32 audit; 916380bd8dSJohn Johansen u32 quiet; 926380bd8dSJohn Johansen u32 kill; 936380bd8dSJohn Johansen u16 xindex; 946380bd8dSJohn Johansen }; 956380bd8dSJohn Johansen 966380bd8dSJohn Johansen extern struct file_perms nullperms; 976380bd8dSJohn Johansen 986380bd8dSJohn Johansen #define COMBINED_PERM_MASK(X) ((X).allow | (X).audit | (X).quiet | (X).kill) 996380bd8dSJohn Johansen 1006380bd8dSJohn Johansen /* FIXME: split perms from dfa and match this to description 1016380bd8dSJohn Johansen * also add delegation info. 1026380bd8dSJohn Johansen */ 1036380bd8dSJohn Johansen static inline u16 dfa_map_xindex(u16 mask) 1046380bd8dSJohn Johansen { 1056380bd8dSJohn Johansen u16 old_index = (mask >> 10) & 0xf; 1066380bd8dSJohn Johansen u16 index = 0; 1076380bd8dSJohn Johansen 1086380bd8dSJohn Johansen if (mask & 0x100) 1096380bd8dSJohn Johansen index |= AA_X_UNSAFE; 1106380bd8dSJohn Johansen if (mask & 0x200) 1116380bd8dSJohn Johansen index |= AA_X_INHERIT; 1126380bd8dSJohn Johansen if (mask & 0x80) 1136380bd8dSJohn Johansen index |= AA_X_UNCONFINED; 1146380bd8dSJohn Johansen 1156380bd8dSJohn Johansen if (old_index == 1) { 1166380bd8dSJohn Johansen index |= AA_X_UNCONFINED; 1176380bd8dSJohn Johansen } else if (old_index == 2) { 1186380bd8dSJohn Johansen index |= AA_X_NAME; 1196380bd8dSJohn Johansen } else if (old_index == 3) { 1206380bd8dSJohn Johansen index |= AA_X_NAME | AA_X_CHILD; 1216380bd8dSJohn Johansen } else { 1226380bd8dSJohn Johansen index |= AA_X_TABLE; 1236380bd8dSJohn Johansen index |= old_index - 4; 1246380bd8dSJohn Johansen } 1256380bd8dSJohn Johansen 1266380bd8dSJohn Johansen return index; 1276380bd8dSJohn Johansen } 1286380bd8dSJohn Johansen 1296380bd8dSJohn Johansen /* 1306380bd8dSJohn Johansen * map old dfa inline permissions to new format 1316380bd8dSJohn Johansen */ 1326380bd8dSJohn Johansen #define dfa_user_allow(dfa, state) (((ACCEPT_TABLE(dfa)[state]) & 0x7f) | \ 1336380bd8dSJohn Johansen ((ACCEPT_TABLE(dfa)[state]) & 0x80000000)) 1346380bd8dSJohn Johansen #define dfa_user_audit(dfa, state) ((ACCEPT_TABLE2(dfa)[state]) & 0x7f) 1356380bd8dSJohn Johansen #define dfa_user_quiet(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 7) & 0x7f) 1366380bd8dSJohn Johansen #define dfa_user_xindex(dfa, state) \ 1376380bd8dSJohn Johansen (dfa_map_xindex(ACCEPT_TABLE(dfa)[state] & 0x3fff)) 1386380bd8dSJohn Johansen 1396380bd8dSJohn Johansen #define dfa_other_allow(dfa, state) ((((ACCEPT_TABLE(dfa)[state]) >> 14) & \ 1406380bd8dSJohn Johansen 0x7f) | \ 1416380bd8dSJohn Johansen ((ACCEPT_TABLE(dfa)[state]) & 0x80000000)) 1426380bd8dSJohn Johansen #define dfa_other_audit(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 14) & 0x7f) 1436380bd8dSJohn Johansen #define dfa_other_quiet(dfa, state) \ 1446380bd8dSJohn Johansen ((((ACCEPT_TABLE2(dfa)[state]) >> 7) >> 14) & 0x7f) 1456380bd8dSJohn Johansen #define dfa_other_xindex(dfa, state) \ 1466380bd8dSJohn Johansen dfa_map_xindex((ACCEPT_TABLE(dfa)[state] >> 14) & 0x3fff) 1476380bd8dSJohn Johansen 1486380bd8dSJohn Johansen int aa_audit_file(struct aa_profile *profile, struct file_perms *perms, 1496380bd8dSJohn Johansen gfp_t gfp, int op, u32 request, const char *name, 1506380bd8dSJohn Johansen const char *target, uid_t ouid, const char *info, int error); 1516380bd8dSJohn Johansen 1526380bd8dSJohn Johansen /** 1536380bd8dSJohn Johansen * struct aa_file_rules - components used for file rule permissions 1546380bd8dSJohn Johansen * @dfa: dfa to match path names and conditionals against 1556380bd8dSJohn Johansen * @perms: permission table indexed by the matched state accept entry of @dfa 1566380bd8dSJohn Johansen * @trans: transition table for indexed by named x transitions 1576380bd8dSJohn Johansen * 1586380bd8dSJohn Johansen * File permission are determined by matching a path against @dfa and then 1596380bd8dSJohn Johansen * then using the value of the accept entry for the matching state as 1606380bd8dSJohn Johansen * an index into @perms. If a named exec transition is required it is 1616380bd8dSJohn Johansen * looked up in the transition table. 1626380bd8dSJohn Johansen */ 1636380bd8dSJohn Johansen struct aa_file_rules { 1646380bd8dSJohn Johansen unsigned int start; 1656380bd8dSJohn Johansen struct aa_dfa *dfa; 1666380bd8dSJohn Johansen /* struct perms perms; */ 1676380bd8dSJohn Johansen struct aa_domain trans; 1686380bd8dSJohn Johansen /* TODO: add delegate table */ 1696380bd8dSJohn Johansen }; 1706380bd8dSJohn Johansen 1716380bd8dSJohn Johansen unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start, 1726380bd8dSJohn Johansen const char *name, struct path_cond *cond, 1736380bd8dSJohn Johansen struct file_perms *perms); 1746380bd8dSJohn Johansen 1756380bd8dSJohn Johansen int aa_path_perm(int op, struct aa_profile *profile, struct path *path, 1766380bd8dSJohn Johansen int flags, u32 request, struct path_cond *cond); 1776380bd8dSJohn Johansen 1786380bd8dSJohn Johansen int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry, 1796380bd8dSJohn Johansen struct path *new_dir, struct dentry *new_dentry); 1806380bd8dSJohn Johansen 1816380bd8dSJohn Johansen int aa_file_perm(int op, struct aa_profile *profile, struct file *file, 1826380bd8dSJohn Johansen u32 request); 1836380bd8dSJohn Johansen 1846380bd8dSJohn Johansen static inline void aa_free_file_rules(struct aa_file_rules *rules) 1856380bd8dSJohn Johansen { 1866380bd8dSJohn Johansen aa_put_dfa(rules->dfa); 1876380bd8dSJohn Johansen aa_free_domain_entries(&rules->trans); 1886380bd8dSJohn Johansen } 1896380bd8dSJohn Johansen 1906380bd8dSJohn Johansen #define ACC_FMODE(x) (("\000\004\002\006"[(x)&O_ACCMODE]) | (((x) << 1) & 0x40)) 1916380bd8dSJohn Johansen 1926380bd8dSJohn Johansen /* from namei.c */ 1936380bd8dSJohn Johansen #define MAP_OPEN_FLAGS(x) ((((x) + 1) & O_ACCMODE) ? (x) + 1 : (x)) 1946380bd8dSJohn Johansen 1956380bd8dSJohn Johansen /** 1966380bd8dSJohn Johansen * aa_map_file_perms - map file flags to AppArmor permissions 1976380bd8dSJohn Johansen * @file: open file to map flags to AppArmor permissions 1986380bd8dSJohn Johansen * 1996380bd8dSJohn Johansen * Returns: apparmor permission set for the file 2006380bd8dSJohn Johansen */ 2016380bd8dSJohn Johansen static inline u32 aa_map_file_to_perms(struct file *file) 2026380bd8dSJohn Johansen { 2036380bd8dSJohn Johansen int flags = MAP_OPEN_FLAGS(file->f_flags); 2046380bd8dSJohn Johansen u32 perms = ACC_FMODE(file->f_mode); 2056380bd8dSJohn Johansen 2066380bd8dSJohn Johansen if ((flags & O_APPEND) && (perms & MAY_WRITE)) 2076380bd8dSJohn Johansen perms = (perms & ~MAY_WRITE) | MAY_APPEND; 2086380bd8dSJohn Johansen /* trunc implies write permission */ 2096380bd8dSJohn Johansen if (flags & O_TRUNC) 2106380bd8dSJohn Johansen perms |= MAY_WRITE; 2116380bd8dSJohn Johansen if (flags & O_CREAT) 2126380bd8dSJohn Johansen perms |= AA_MAY_CREATE; 2136380bd8dSJohn Johansen 2146380bd8dSJohn Johansen return perms; 2156380bd8dSJohn Johansen } 2166380bd8dSJohn Johansen 2176380bd8dSJohn Johansen #endif /* __AA_FILE_H */ 218