1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * AppArmor security module 4 * 5 * This file contains AppArmor policy loading interface function definitions. 6 * 7 * Copyright (C) 1998-2008 Novell/SUSE 8 * Copyright 2009-2010 Canonical Ltd. 9 */ 10 11 #ifndef __POLICY_INTERFACE_H 12 #define __POLICY_INTERFACE_H 13 14 #include <linux/list.h> 15 #include <linux/kref.h> 16 #include <linux/dcache.h> 17 #include <linux/workqueue.h> 18 19 struct aa_load_ent { 20 struct list_head list; 21 struct aa_profile *new; 22 struct aa_profile *old; 23 struct aa_profile *rename; 24 const char *ns_name; 25 }; 26 27 void aa_load_ent_free(struct aa_load_ent *ent); 28 struct aa_load_ent *aa_load_ent_alloc(void); 29 30 #define PACKED_FLAG_HAT 1 31 #define PACKED_FLAG_DEBUG1 2 32 #define PACKED_FLAG_DEBUG2 4 33 34 #define PACKED_MODE_ENFORCE 0 35 #define PACKED_MODE_COMPLAIN 1 36 #define PACKED_MODE_KILL 2 37 #define PACKED_MODE_UNCONFINED 3 38 39 struct aa_ns; 40 41 enum { 42 AAFS_LOADDATA_ABI = 0, 43 AAFS_LOADDATA_REVISION, 44 AAFS_LOADDATA_HASH, 45 AAFS_LOADDATA_DATA, 46 AAFS_LOADDATA_COMPRESSED_SIZE, 47 AAFS_LOADDATA_DIR, /* must be last actual entry */ 48 AAFS_LOADDATA_NDENTS /* count of entries */ 49 }; 50 51 /* 52 * The AppArmor interface treats data as a type byte followed by the 53 * actual data. The interface has the notion of a named entry 54 * which has a name (AA_NAME typecode followed by name string) followed by 55 * the entries typecode and data. Named types allow for optional 56 * elements and extensions to be added and tested for without breaking 57 * backwards compatibility. 58 */ 59 60 enum aa_code { 61 AA_U8, 62 AA_U16, 63 AA_U32, 64 AA_U64, 65 AA_NAME, /* same as string except it is items name */ 66 AA_STRING, 67 AA_BLOB, 68 AA_STRUCT, 69 AA_STRUCTEND, 70 AA_LIST, 71 AA_LISTEND, 72 AA_ARRAY, 73 AA_ARRAYEND, 74 }; 75 76 /* 77 * aa_ext is the read of the buffer containing the serialized profile. The 78 * data is copied into a kernel buffer in apparmorfs and then handed off to 79 * the unpack routines. 80 */ 81 struct aa_ext { 82 void *start; 83 void *end; 84 void *pos; /* pointer to current position in the buffer */ 85 u32 version; 86 }; 87 88 /* 89 * struct aa_loaddata - buffer of policy raw_data set 90 * 91 * there is no loaddata ref for being on ns list, nor a ref from 92 * d_inode(@dentry) when grab a ref from these, @ns->lock must be held 93 * && __aa_get_loaddata() needs to be used, and the return value 94 * checked, if NULL the loaddata is already being reaped and should be 95 * considered dead. 96 */ 97 struct aa_loaddata { 98 struct kref count; 99 struct list_head list; 100 struct work_struct work; 101 struct dentry *dents[AAFS_LOADDATA_NDENTS]; 102 struct aa_ns *ns; 103 char *name; 104 size_t size; /* the original size of the payload */ 105 size_t compressed_size; /* the compressed size of the payload */ 106 long revision; /* the ns policy revision this caused */ 107 int abi; 108 unsigned char *hash; 109 110 /* Pointer to payload. If @compressed_size > 0, then this is the 111 * compressed version of the payload, else it is the uncompressed 112 * version (with the size indicated by @size). 113 */ 114 char *data; 115 }; 116 117 int aa_unpack(struct aa_loaddata *udata, struct list_head *lh, const char **ns); 118 119 /** 120 * __aa_get_loaddata - get a reference count to uncounted data reference 121 * @data: reference to get a count on 122 * 123 * Returns: pointer to reference OR NULL if race is lost and reference is 124 * being repeated. 125 * Requires: @data->ns->lock held, and the return code MUST be checked 126 * 127 * Use only from inode->i_private and @data->list found references 128 */ 129 static inline struct aa_loaddata * __aa_get_loaddata(struct aa_loaddata * data)130 __aa_get_loaddata(struct aa_loaddata *data) 131 { 132 if (data && kref_get_unless_zero(&(data->count))) 133 return data; 134 135 return NULL; 136 } 137 138 /** 139 * aa_get_loaddata - get a reference count from a counted data reference 140 * @data: reference to get a count on 141 * 142 * Returns: point to reference 143 * Requires: @data to have a valid reference count on it. It is a bug 144 * if the race to reap can be encountered when it is used. 145 */ 146 static inline struct aa_loaddata * aa_get_loaddata(struct aa_loaddata * data)147 aa_get_loaddata(struct aa_loaddata *data) 148 { 149 struct aa_loaddata *tmp = __aa_get_loaddata(data); 150 151 AA_BUG(data && !tmp); 152 153 return tmp; 154 } 155 156 void __aa_loaddata_update(struct aa_loaddata *data, long revision); 157 bool aa_rawdata_eq(struct aa_loaddata *l, struct aa_loaddata *r); 158 void aa_loaddata_kref(struct kref *kref); 159 struct aa_loaddata *aa_loaddata_alloc(size_t size); aa_put_loaddata(struct aa_loaddata * data)160 static inline void aa_put_loaddata(struct aa_loaddata *data) 161 { 162 if (data) 163 kref_put(&data->count, aa_loaddata_kref); 164 } 165 166 #if IS_ENABLED(CONFIG_KUNIT) 167 bool aa_inbounds(struct aa_ext *e, size_t size); 168 size_t aa_unpack_u16_chunk(struct aa_ext *e, char **chunk); 169 bool aa_unpack_X(struct aa_ext *e, enum aa_code code); 170 bool aa_unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name); 171 bool aa_unpack_u32(struct aa_ext *e, u32 *data, const char *name); 172 bool aa_unpack_u64(struct aa_ext *e, u64 *data, const char *name); 173 size_t aa_unpack_array(struct aa_ext *e, const char *name); 174 size_t aa_unpack_blob(struct aa_ext *e, char **blob, const char *name); 175 int aa_unpack_str(struct aa_ext *e, const char **string, const char *name); 176 int aa_unpack_strdup(struct aa_ext *e, char **string, const char *name); 177 #endif 178 179 #endif /* __POLICY_INTERFACE_H */ 180