1b886d83cSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 26380bd8dSJohn Johansen /* 36380bd8dSJohn Johansen * AppArmor security module 46380bd8dSJohn Johansen * 56380bd8dSJohn Johansen * This file contains AppArmor file mediation function definitions. 66380bd8dSJohn Johansen * 76380bd8dSJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 86380bd8dSJohn Johansen * Copyright 2009-2010 Canonical Ltd. 96380bd8dSJohn Johansen */ 106380bd8dSJohn Johansen 116380bd8dSJohn Johansen #ifndef __AA_FILE_H 126380bd8dSJohn Johansen #define __AA_FILE_H 136380bd8dSJohn Johansen 14190a9518SJohn Johansen #include <linux/spinlock.h> 15190a9518SJohn Johansen 166380bd8dSJohn Johansen #include "domain.h" 176380bd8dSJohn Johansen #include "match.h" 18fc7e0b26SJohn Johansen #include "perms.h" 196380bd8dSJohn Johansen 2053bdc46fSJohn Johansen struct aa_policydb; 216380bd8dSJohn Johansen struct aa_profile; 2237721e1bSAlexey Dobriyan struct path; 236380bd8dSJohn Johansen 24e53cfe6cSJohn Johansen #define mask_mode_t(X) (X & (MAY_EXEC | MAY_WRITE | MAY_READ | MAY_APPEND)) 256380bd8dSJohn Johansen 266380bd8dSJohn Johansen #define AA_AUDIT_FILE_MASK (MAY_READ | MAY_WRITE | MAY_EXEC | MAY_APPEND |\ 276380bd8dSJohn Johansen AA_MAY_CREATE | AA_MAY_DELETE | \ 28e53cfe6cSJohn Johansen AA_MAY_GETATTR | AA_MAY_SETATTR | \ 296380bd8dSJohn Johansen AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \ 306380bd8dSJohn Johansen AA_EXEC_MMAP | AA_MAY_LINK) 316380bd8dSJohn Johansen 3233bf60caSCasey Schaufler static inline struct aa_file_ctx *file_ctx(struct file *file) 3333bf60caSCasey Schaufler { 3433bf60caSCasey Schaufler return file->f_security + apparmor_blob_sizes.lbs_file; 3533bf60caSCasey Schaufler } 362835a13bSJohn Johansen 37af7caa8fSJohn Johansen /* struct aa_file_ctx - the AppArmor context the file was opened in 38190a9518SJohn Johansen * @lock: lock to update the ctx 39190a9518SJohn Johansen * @label: label currently cached on the ctx 40af7caa8fSJohn Johansen * @perms: the permission the file was opened with 41af7caa8fSJohn Johansen */ 42af7caa8fSJohn Johansen struct aa_file_ctx { 43190a9518SJohn Johansen spinlock_t lock; 44190a9518SJohn Johansen struct aa_label __rcu *label; 45e53cfe6cSJohn Johansen u32 allow; 46af7caa8fSJohn Johansen }; 47af7caa8fSJohn Johansen 48af7caa8fSJohn Johansen /** 492835a13bSJohn Johansen * aa_alloc_file_ctx - allocate file_ctx 502835a13bSJohn Johansen * @label: initial label of task creating the file 51af7caa8fSJohn Johansen * @gfp: gfp flags for allocation 52af7caa8fSJohn Johansen * 53af7caa8fSJohn Johansen * Returns: file_ctx or NULL on failure 54af7caa8fSJohn Johansen */ 55190a9518SJohn Johansen static inline struct aa_file_ctx *aa_alloc_file_ctx(struct aa_label *label, 56190a9518SJohn Johansen gfp_t gfp) 57af7caa8fSJohn Johansen { 582835a13bSJohn Johansen struct aa_file_ctx *ctx; 592835a13bSJohn Johansen 602835a13bSJohn Johansen ctx = kzalloc(sizeof(struct aa_file_ctx), gfp); 61190a9518SJohn Johansen if (ctx) { 62190a9518SJohn Johansen spin_lock_init(&ctx->lock); 63190a9518SJohn Johansen rcu_assign_pointer(ctx->label, aa_get_label(label)); 64190a9518SJohn Johansen } 652835a13bSJohn Johansen return ctx; 66af7caa8fSJohn Johansen } 67af7caa8fSJohn Johansen 68af7caa8fSJohn Johansen /** 692835a13bSJohn Johansen * aa_free_file_ctx - free a file_ctx 70af7caa8fSJohn Johansen * @ctx: file_ctx to free (MAYBE_NULL) 71af7caa8fSJohn Johansen */ 722835a13bSJohn Johansen static inline void aa_free_file_ctx(struct aa_file_ctx *ctx) 73af7caa8fSJohn Johansen { 74190a9518SJohn Johansen if (ctx) { 75190a9518SJohn Johansen aa_put_label(rcu_access_pointer(ctx->label)); 76453431a5SWaiman Long kfree_sensitive(ctx); 77af7caa8fSJohn Johansen } 78190a9518SJohn Johansen } 79190a9518SJohn Johansen 80190a9518SJohn Johansen static inline struct aa_label *aa_get_file_label(struct aa_file_ctx *ctx) 81190a9518SJohn Johansen { 82190a9518SJohn Johansen return aa_get_label_rcu(&ctx->label); 83190a9518SJohn Johansen } 84af7caa8fSJohn Johansen 856380bd8dSJohn Johansen /* 866380bd8dSJohn Johansen * The xindex is broken into 3 parts 876380bd8dSJohn Johansen * - index - an index into either the exec name table or the variable table 886380bd8dSJohn Johansen * - exec type - which determines how the executable name and index are used 896380bd8dSJohn Johansen * - flags - which modify how the destination name is applied 906380bd8dSJohn Johansen */ 91*0bece4faSJohn Johansen #define AA_X_INDEX_MASK AA_INDEX_MASK 926380bd8dSJohn Johansen 93ae6d35edSJohn Johansen #define AA_X_TYPE_MASK 0x0c000000 94*0bece4faSJohn Johansen #define AA_X_NONE AA_INDEX_NONE 95ae6d35edSJohn Johansen #define AA_X_NAME 0x04000000 /* use executable name px */ 96ae6d35edSJohn Johansen #define AA_X_TABLE 0x08000000 /* use a specified name ->n# */ 976380bd8dSJohn Johansen 98ae6d35edSJohn Johansen #define AA_X_UNSAFE 0x10000000 99ae6d35edSJohn Johansen #define AA_X_CHILD 0x20000000 100ae6d35edSJohn Johansen #define AA_X_INHERIT 0x40000000 101ae6d35edSJohn Johansen #define AA_X_UNCONFINED 0x80000000 1026380bd8dSJohn Johansen 1036380bd8dSJohn Johansen /* need to make conditional which ones are being set */ 1046380bd8dSJohn Johansen struct path_cond { 1052db81452SEric W. Biederman kuid_t uid; 1066380bd8dSJohn Johansen umode_t mode; 1076380bd8dSJohn Johansen }; 1086380bd8dSJohn Johansen 1096380bd8dSJohn Johansen #define COMBINED_PERM_MASK(X) ((X).allow | (X).audit | (X).quiet | (X).kill) 1106380bd8dSJohn Johansen 1112d679f3cSJohn Johansen int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms, 112ef88a7acSJohn Johansen const char *op, u32 request, const char *name, 11398c3d182SJohn Johansen const char *target, struct aa_label *tlabel, kuid_t ouid, 11498c3d182SJohn Johansen const char *info, int error); 1156380bd8dSJohn Johansen 11653bdc46fSJohn Johansen struct aa_perms *aa_lookup_fperms(struct aa_policydb *file_rules, 11733fc95d8SJohn Johansen aa_state_t state, struct path_cond *cond); 11833fc95d8SJohn Johansen aa_state_t aa_str_perms(struct aa_policydb *file_rules, aa_state_t start, 1196380bd8dSJohn Johansen const char *name, struct path_cond *cond, 1202d679f3cSJohn Johansen struct aa_perms *perms); 1216380bd8dSJohn Johansen 122aebd873eSJohn Johansen int __aa_path_perm(const char *op, struct aa_profile *profile, 123aebd873eSJohn Johansen const char *name, u32 request, struct path_cond *cond, 124aebd873eSJohn Johansen int flags, struct aa_perms *perms); 125aebd873eSJohn Johansen int aa_path_perm(const char *op, struct aa_label *label, 12647f6e5ccSJohn Johansen const struct path *path, int flags, u32 request, 12747f6e5ccSJohn Johansen struct path_cond *cond); 1286380bd8dSJohn Johansen 1298014370fSJohn Johansen int aa_path_link(struct aa_label *label, struct dentry *old_dentry, 1303539aaf6SAl Viro const struct path *new_dir, struct dentry *new_dentry); 1316380bd8dSJohn Johansen 132190a9518SJohn Johansen int aa_file_perm(const char *op, struct aa_label *label, struct file *file, 133341c1fdaSJohn Johansen u32 request, bool in_atomic); 1346380bd8dSJohn Johansen 135192ca6b5SJohn Johansen void aa_inherit_files(const struct cred *cred, struct files_struct *files); 136192ca6b5SJohn Johansen 1376380bd8dSJohn Johansen 1386380bd8dSJohn Johansen /** 1396380bd8dSJohn Johansen * aa_map_file_perms - map file flags to AppArmor permissions 1406380bd8dSJohn Johansen * @file: open file to map flags to AppArmor permissions 1416380bd8dSJohn Johansen * 1426380bd8dSJohn Johansen * Returns: apparmor permission set for the file 1436380bd8dSJohn Johansen */ 1446380bd8dSJohn Johansen static inline u32 aa_map_file_to_perms(struct file *file) 1456380bd8dSJohn Johansen { 14653fe8b99SJohn Johansen int flags = file->f_flags; 14753fe8b99SJohn Johansen u32 perms = 0; 14853fe8b99SJohn Johansen 14953fe8b99SJohn Johansen if (file->f_mode & FMODE_WRITE) 15053fe8b99SJohn Johansen perms |= MAY_WRITE; 15153fe8b99SJohn Johansen if (file->f_mode & FMODE_READ) 15253fe8b99SJohn Johansen perms |= MAY_READ; 1536380bd8dSJohn Johansen 1546380bd8dSJohn Johansen if ((flags & O_APPEND) && (perms & MAY_WRITE)) 1556380bd8dSJohn Johansen perms = (perms & ~MAY_WRITE) | MAY_APPEND; 1566380bd8dSJohn Johansen /* trunc implies write permission */ 1576380bd8dSJohn Johansen if (flags & O_TRUNC) 1586380bd8dSJohn Johansen perms |= MAY_WRITE; 1596380bd8dSJohn Johansen if (flags & O_CREAT) 1606380bd8dSJohn Johansen perms |= AA_MAY_CREATE; 1616380bd8dSJohn Johansen 1626380bd8dSJohn Johansen return perms; 1636380bd8dSJohn Johansen } 1646380bd8dSJohn Johansen 1656380bd8dSJohn Johansen #endif /* __AA_FILE_H */ 166