John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * AppArmor security module |
| 3 | * |
| 4 | * This file contains AppArmor functions for unpacking policy loaded from |
| 5 | * userspace. |
| 6 | * |
| 7 | * Copyright (C) 1998-2008 Novell/SUSE |
| 8 | * Copyright 2009-2010 Canonical Ltd. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation, version 2 of the |
| 13 | * License. |
| 14 | * |
Randy Dunlap | d410fa4e | 2011-05-19 15:59:38 -0700 | [diff] [blame] | 15 | * AppArmor uses a serialized binary format for loading policy. To find |
Kees Cook | 26fccd9 | 2017-05-13 04:51:45 -0700 | [diff] [blame] | 16 | * policy format documentation see Documentation/admin-guide/LSM/apparmor.rst |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 17 | * All policy is validated before it is used. |
| 18 | */ |
| 19 | |
| 20 | #include <asm/unaligned.h> |
| 21 | #include <linux/ctype.h> |
| 22 | #include <linux/errno.h> |
| 23 | |
| 24 | #include "include/apparmor.h" |
| 25 | #include "include/audit.h" |
John Johansen | d8889d4 | 2017-10-11 01:04:48 -0700 | [diff] [blame] | 26 | #include "include/cred.h" |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 27 | #include "include/crypto.h" |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 28 | #include "include/match.h" |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 29 | #include "include/path.h" |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 30 | #include "include/policy.h" |
| 31 | #include "include/policy_unpack.h" |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 32 | |
John Johansen | 474d6b75 | 2017-01-16 00:42:39 -0800 | [diff] [blame] | 33 | #define K_ABI_MASK 0x3ff |
John Johansen | 5ebfb12 | 2017-01-16 00:42:38 -0800 | [diff] [blame] | 34 | #define FORCE_COMPLAIN_FLAG 0x800 |
John Johansen | 474d6b75 | 2017-01-16 00:42:39 -0800 | [diff] [blame] | 35 | #define VERSION_LT(X, Y) (((X) & K_ABI_MASK) < ((Y) & K_ABI_MASK)) |
| 36 | #define VERSION_GT(X, Y) (((X) & K_ABI_MASK) > ((Y) & K_ABI_MASK)) |
| 37 | |
| 38 | #define v5 5 /* base version */ |
| 39 | #define v6 6 /* per entry policydb mediation check */ |
| 40 | #define v7 7 /* full network masking */ |
John Johansen | 5ebfb12 | 2017-01-16 00:42:38 -0800 | [diff] [blame] | 41 | |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 42 | /* |
| 43 | * The AppArmor interface treats data as a type byte followed by the |
| 44 | * actual data. The interface has the notion of a a named entry |
| 45 | * which has a name (AA_NAME typecode followed by name string) followed by |
| 46 | * the entries typecode and data. Named types allow for optional |
| 47 | * elements and extensions to be added and tested for without breaking |
| 48 | * backwards compatibility. |
| 49 | */ |
| 50 | |
| 51 | enum aa_code { |
| 52 | AA_U8, |
| 53 | AA_U16, |
| 54 | AA_U32, |
| 55 | AA_U64, |
| 56 | AA_NAME, /* same as string except it is items name */ |
| 57 | AA_STRING, |
| 58 | AA_BLOB, |
| 59 | AA_STRUCT, |
| 60 | AA_STRUCTEND, |
| 61 | AA_LIST, |
| 62 | AA_LISTEND, |
| 63 | AA_ARRAY, |
| 64 | AA_ARRAYEND, |
| 65 | }; |
| 66 | |
| 67 | /* |
| 68 | * aa_ext is the read of the buffer containing the serialized profile. The |
| 69 | * data is copied into a kernel buffer in apparmorfs and then handed off to |
| 70 | * the unpack routines. |
| 71 | */ |
| 72 | struct aa_ext { |
| 73 | void *start; |
| 74 | void *end; |
| 75 | void *pos; /* pointer to current position in the buffer */ |
| 76 | u32 version; |
| 77 | }; |
| 78 | |
| 79 | /* audit callback for unpack fields */ |
| 80 | static void audit_cb(struct audit_buffer *ab, void *va) |
| 81 | { |
| 82 | struct common_audit_data *sa = va; |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 83 | |
| 84 | if (aad(sa)->iface.ns) { |
| 85 | audit_log_format(ab, " ns="); |
| 86 | audit_log_untrustedstring(ab, aad(sa)->iface.ns); |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 87 | } |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 88 | if (aad(sa)->name) { |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 89 | audit_log_format(ab, " name="); |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 90 | audit_log_untrustedstring(ab, aad(sa)->name); |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 91 | } |
| 92 | if (aad(sa)->iface.pos) |
| 93 | audit_log_format(ab, " offset=%ld", aad(sa)->iface.pos); |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * audit_iface - do audit message for policy unpacking/load/replace/remove |
| 98 | * @new: profile if it has been allocated (MAYBE NULL) |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 99 | * @ns_name: name of the ns the profile is to be loaded to (MAY BE NULL) |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 100 | * @name: name of the profile being manipulated (MAYBE NULL) |
| 101 | * @info: any extra info about the failure (MAYBE NULL) |
John Johansen | b1b4bc2 | 2012-03-10 11:25:30 -0800 | [diff] [blame] | 102 | * @e: buffer position info |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 103 | * @error: error code |
| 104 | * |
| 105 | * Returns: %0 or error |
| 106 | */ |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 107 | static int audit_iface(struct aa_profile *new, const char *ns_name, |
| 108 | const char *name, const char *info, struct aa_ext *e, |
| 109 | int error) |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 110 | { |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 111 | struct aa_profile *profile = labels_profile(aa_current_raw_label()); |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 112 | DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, NULL); |
John Johansen | b1b4bc2 | 2012-03-10 11:25:30 -0800 | [diff] [blame] | 113 | if (e) |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 114 | aad(&sa)->iface.pos = e->pos - e->start; |
| 115 | aad(&sa)->iface.ns = ns_name; |
| 116 | if (new) |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 117 | aad(&sa)->name = new->base.hname; |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 118 | else |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 119 | aad(&sa)->name = name; |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 120 | aad(&sa)->info = info; |
| 121 | aad(&sa)->error = error; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 122 | |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 123 | return aa_audit(AUDIT_APPARMOR_STATUS, profile, &sa, audit_cb); |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 124 | } |
| 125 | |
John Johansen | 5d5182ca | 2017-05-09 00:08:41 -0700 | [diff] [blame] | 126 | void __aa_loaddata_update(struct aa_loaddata *data, long revision) |
| 127 | { |
| 128 | AA_BUG(!data); |
| 129 | AA_BUG(!data->ns); |
| 130 | AA_BUG(!data->dents[AAFS_LOADDATA_REVISION]); |
| 131 | AA_BUG(!mutex_is_locked(&data->ns->lock)); |
| 132 | AA_BUG(data->revision > revision); |
| 133 | |
| 134 | data->revision = revision; |
| 135 | d_inode(data->dents[AAFS_LOADDATA_DIR])->i_mtime = |
| 136 | current_time(d_inode(data->dents[AAFS_LOADDATA_DIR])); |
| 137 | d_inode(data->dents[AAFS_LOADDATA_REVISION])->i_mtime = |
| 138 | current_time(d_inode(data->dents[AAFS_LOADDATA_REVISION])); |
| 139 | } |
| 140 | |
| 141 | bool aa_rawdata_eq(struct aa_loaddata *l, struct aa_loaddata *r) |
| 142 | { |
| 143 | if (l->size != r->size) |
| 144 | return false; |
| 145 | if (aa_g_hash_policy && memcmp(l->hash, r->hash, aa_hash_size()) != 0) |
| 146 | return false; |
| 147 | return memcmp(l->data, r->data, r->size) == 0; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * need to take the ns mutex lock which is NOT safe most places that |
| 152 | * put_loaddata is called, so we have to delay freeing it |
| 153 | */ |
| 154 | static void do_loaddata_free(struct work_struct *work) |
| 155 | { |
| 156 | struct aa_loaddata *d = container_of(work, struct aa_loaddata, work); |
| 157 | struct aa_ns *ns = aa_get_ns(d->ns); |
| 158 | |
| 159 | if (ns) { |
John Johansen | feb3c76 | 2017-11-20 23:24:09 -0800 | [diff] [blame] | 160 | mutex_lock_nested(&ns->lock, ns->level); |
John Johansen | 5d5182ca | 2017-05-09 00:08:41 -0700 | [diff] [blame] | 161 | __aa_fs_remove_rawdata(d); |
| 162 | mutex_unlock(&ns->lock); |
| 163 | aa_put_ns(ns); |
| 164 | } |
| 165 | |
| 166 | kzfree(d->hash); |
John Johansen | a6a5257 | 2018-02-03 20:08:28 +0100 | [diff] [blame] | 167 | kzfree(d->name); |
| 168 | kvfree(d->data); |
| 169 | kzfree(d); |
John Johansen | 5d5182ca | 2017-05-09 00:08:41 -0700 | [diff] [blame] | 170 | } |
| 171 | |
John Johansen | 5ac8c35 | 2017-01-16 00:42:55 -0800 | [diff] [blame] | 172 | void aa_loaddata_kref(struct kref *kref) |
| 173 | { |
| 174 | struct aa_loaddata *d = container_of(kref, struct aa_loaddata, count); |
| 175 | |
| 176 | if (d) { |
John Johansen | 5d5182ca | 2017-05-09 00:08:41 -0700 | [diff] [blame] | 177 | INIT_WORK(&d->work, do_loaddata_free); |
| 178 | schedule_work(&d->work); |
John Johansen | 5ac8c35 | 2017-01-16 00:42:55 -0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
John Johansen | 5d5182ca | 2017-05-09 00:08:41 -0700 | [diff] [blame] | 182 | struct aa_loaddata *aa_loaddata_alloc(size_t size) |
| 183 | { |
John Johansen | a6a5257 | 2018-02-03 20:08:28 +0100 | [diff] [blame] | 184 | struct aa_loaddata *d; |
John Johansen | 5d5182ca | 2017-05-09 00:08:41 -0700 | [diff] [blame] | 185 | |
John Johansen | a6a5257 | 2018-02-03 20:08:28 +0100 | [diff] [blame] | 186 | d = kzalloc(sizeof(*d), GFP_KERNEL); |
John Johansen | 5d5182ca | 2017-05-09 00:08:41 -0700 | [diff] [blame] | 187 | if (d == NULL) |
| 188 | return ERR_PTR(-ENOMEM); |
John Johansen | a6a5257 | 2018-02-03 20:08:28 +0100 | [diff] [blame] | 189 | d->data = kvzalloc(size, GFP_KERNEL); |
| 190 | if (!d->data) { |
| 191 | kfree(d); |
| 192 | return ERR_PTR(-ENOMEM); |
| 193 | } |
John Johansen | 5d5182ca | 2017-05-09 00:08:41 -0700 | [diff] [blame] | 194 | kref_init(&d->count); |
| 195 | INIT_LIST_HEAD(&d->list); |
| 196 | |
| 197 | return d; |
| 198 | } |
| 199 | |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 200 | /* test if read will be in packed data bounds */ |
| 201 | static bool inbounds(struct aa_ext *e, size_t size) |
| 202 | { |
| 203 | return (size <= e->end - e->pos); |
| 204 | } |
| 205 | |
Matthew Garrett | 8e51f90 | 2018-02-08 12:37:19 -0800 | [diff] [blame] | 206 | static void *kvmemdup(const void *src, size_t len) |
| 207 | { |
| 208 | void *p = kvmalloc(len, GFP_KERNEL); |
| 209 | |
| 210 | if (p) |
| 211 | memcpy(p, src, len); |
| 212 | return p; |
| 213 | } |
| 214 | |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 215 | /** |
| 216 | * aa_u16_chunck - test and do bounds checking for a u16 size based chunk |
| 217 | * @e: serialized data read head (NOT NULL) |
| 218 | * @chunk: start address for chunk of data (NOT NULL) |
| 219 | * |
| 220 | * Returns: the size of chunk found with the read head at the end of the chunk. |
| 221 | */ |
| 222 | static size_t unpack_u16_chunk(struct aa_ext *e, char **chunk) |
| 223 | { |
| 224 | size_t size = 0; |
| 225 | |
| 226 | if (!inbounds(e, sizeof(u16))) |
| 227 | return 0; |
John Johansen | 2c17cd3 | 2017-01-16 00:43:14 -0800 | [diff] [blame] | 228 | size = le16_to_cpu(get_unaligned((__le16 *) e->pos)); |
| 229 | e->pos += sizeof(__le16); |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 230 | if (!inbounds(e, size)) |
| 231 | return 0; |
| 232 | *chunk = e->pos; |
| 233 | e->pos += size; |
| 234 | return size; |
| 235 | } |
| 236 | |
| 237 | /* unpack control byte */ |
| 238 | static bool unpack_X(struct aa_ext *e, enum aa_code code) |
| 239 | { |
| 240 | if (!inbounds(e, 1)) |
| 241 | return 0; |
| 242 | if (*(u8 *) e->pos != code) |
| 243 | return 0; |
| 244 | e->pos++; |
| 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * unpack_nameX - check is the next element is of type X with a name of @name |
| 250 | * @e: serialized data extent information (NOT NULL) |
| 251 | * @code: type code |
| 252 | * @name: name to match to the serialized element. (MAYBE NULL) |
| 253 | * |
| 254 | * check that the next serialized data element is of type X and has a tag |
| 255 | * name @name. If @name is specified then there must be a matching |
| 256 | * name element in the stream. If @name is NULL any name element will be |
| 257 | * skipped and only the typecode will be tested. |
| 258 | * |
| 259 | * Returns 1 on success (both type code and name tests match) and the read |
| 260 | * head is advanced past the headers |
| 261 | * |
| 262 | * Returns: 0 if either match fails, the read head does not move |
| 263 | */ |
| 264 | static bool unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name) |
| 265 | { |
| 266 | /* |
| 267 | * May need to reset pos if name or type doesn't match |
| 268 | */ |
| 269 | void *pos = e->pos; |
| 270 | /* |
| 271 | * Check for presence of a tagname, and if present name size |
| 272 | * AA_NAME tag value is a u16. |
| 273 | */ |
| 274 | if (unpack_X(e, AA_NAME)) { |
| 275 | char *tag = NULL; |
| 276 | size_t size = unpack_u16_chunk(e, &tag); |
| 277 | /* if a name is specified it must match. otherwise skip tag */ |
| 278 | if (name && (!size || strcmp(name, tag))) |
| 279 | goto fail; |
| 280 | } else if (name) { |
| 281 | /* if a name is specified and there is no name tag fail */ |
| 282 | goto fail; |
| 283 | } |
| 284 | |
| 285 | /* now check if type code matches */ |
| 286 | if (unpack_X(e, code)) |
| 287 | return 1; |
| 288 | |
| 289 | fail: |
| 290 | e->pos = pos; |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static bool unpack_u32(struct aa_ext *e, u32 *data, const char *name) |
| 295 | { |
| 296 | if (unpack_nameX(e, AA_U32, name)) { |
| 297 | if (!inbounds(e, sizeof(u32))) |
| 298 | return 0; |
| 299 | if (data) |
John Johansen | 2c17cd3 | 2017-01-16 00:43:14 -0800 | [diff] [blame] | 300 | *data = le32_to_cpu(get_unaligned((__le32 *) e->pos)); |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 301 | e->pos += sizeof(u32); |
| 302 | return 1; |
| 303 | } |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static bool unpack_u64(struct aa_ext *e, u64 *data, const char *name) |
| 308 | { |
| 309 | if (unpack_nameX(e, AA_U64, name)) { |
| 310 | if (!inbounds(e, sizeof(u64))) |
| 311 | return 0; |
| 312 | if (data) |
John Johansen | 2c17cd3 | 2017-01-16 00:43:14 -0800 | [diff] [blame] | 313 | *data = le64_to_cpu(get_unaligned((__le64 *) e->pos)); |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 314 | e->pos += sizeof(u64); |
| 315 | return 1; |
| 316 | } |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static size_t unpack_array(struct aa_ext *e, const char *name) |
| 321 | { |
| 322 | if (unpack_nameX(e, AA_ARRAY, name)) { |
| 323 | int size; |
| 324 | if (!inbounds(e, sizeof(u16))) |
| 325 | return 0; |
John Johansen | 2c17cd3 | 2017-01-16 00:43:14 -0800 | [diff] [blame] | 326 | size = (int)le16_to_cpu(get_unaligned((__le16 *) e->pos)); |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 327 | e->pos += sizeof(u16); |
| 328 | return size; |
| 329 | } |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static size_t unpack_blob(struct aa_ext *e, char **blob, const char *name) |
| 334 | { |
| 335 | if (unpack_nameX(e, AA_BLOB, name)) { |
| 336 | u32 size; |
| 337 | if (!inbounds(e, sizeof(u32))) |
| 338 | return 0; |
John Johansen | 2c17cd3 | 2017-01-16 00:43:14 -0800 | [diff] [blame] | 339 | size = le32_to_cpu(get_unaligned((__le32 *) e->pos)); |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 340 | e->pos += sizeof(u32); |
| 341 | if (inbounds(e, (size_t) size)) { |
| 342 | *blob = e->pos; |
| 343 | e->pos += size; |
| 344 | return size; |
| 345 | } |
| 346 | } |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | static int unpack_str(struct aa_ext *e, const char **string, const char *name) |
| 351 | { |
| 352 | char *src_str; |
| 353 | size_t size = 0; |
| 354 | void *pos = e->pos; |
| 355 | *string = NULL; |
| 356 | if (unpack_nameX(e, AA_STRING, name)) { |
| 357 | size = unpack_u16_chunk(e, &src_str); |
| 358 | if (size) { |
| 359 | /* strings are null terminated, length is size - 1 */ |
| 360 | if (src_str[size - 1] != 0) |
| 361 | goto fail; |
| 362 | *string = src_str; |
| 363 | } |
| 364 | } |
| 365 | return size; |
| 366 | |
| 367 | fail: |
| 368 | e->pos = pos; |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static int unpack_strdup(struct aa_ext *e, char **string, const char *name) |
| 373 | { |
| 374 | const char *tmp; |
| 375 | void *pos = e->pos; |
| 376 | int res = unpack_str(e, &tmp, name); |
| 377 | *string = NULL; |
| 378 | |
| 379 | if (!res) |
| 380 | return 0; |
| 381 | |
| 382 | *string = kmemdup(tmp, res, GFP_KERNEL); |
| 383 | if (!*string) { |
| 384 | e->pos = pos; |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | return res; |
| 389 | } |
| 390 | |
John Johansen | 180a6f5 | 2013-02-18 16:09:34 -0800 | [diff] [blame] | 391 | #define DFA_VALID_PERM_MASK 0xffffffff |
| 392 | #define DFA_VALID_PERM2_MASK 0xffffffff |
| 393 | |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 394 | /** |
| 395 | * verify_accept - verify the accept tables of a dfa |
| 396 | * @dfa: dfa to verify accept tables of (NOT NULL) |
| 397 | * @flags: flags governing dfa |
| 398 | * |
| 399 | * Returns: 1 if valid accept tables else 0 if error |
| 400 | */ |
| 401 | static bool verify_accept(struct aa_dfa *dfa, int flags) |
| 402 | { |
| 403 | int i; |
| 404 | |
| 405 | /* verify accept permissions */ |
| 406 | for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) { |
| 407 | int mode = ACCEPT_TABLE(dfa)[i]; |
| 408 | |
| 409 | if (mode & ~DFA_VALID_PERM_MASK) |
| 410 | return 0; |
| 411 | |
| 412 | if (ACCEPT_TABLE2(dfa)[i] & ~DFA_VALID_PERM2_MASK) |
| 413 | return 0; |
| 414 | } |
| 415 | return 1; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * unpack_dfa - unpack a file rule dfa |
| 420 | * @e: serialized data extent information (NOT NULL) |
| 421 | * |
| 422 | * returns dfa or ERR_PTR or NULL if no dfa |
| 423 | */ |
| 424 | static struct aa_dfa *unpack_dfa(struct aa_ext *e) |
| 425 | { |
| 426 | char *blob = NULL; |
| 427 | size_t size; |
| 428 | struct aa_dfa *dfa = NULL; |
| 429 | |
| 430 | size = unpack_blob(e, &blob, "aadfa"); |
| 431 | if (size) { |
| 432 | /* |
| 433 | * The dfa is aligned with in the blob to 8 bytes |
| 434 | * from the beginning of the stream. |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 435 | * alignment adjust needed by dfa unpack |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 436 | */ |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 437 | size_t sz = blob - (char *) e->start - |
| 438 | ((e->pos - e->start) & 7); |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 439 | size_t pad = ALIGN(sz, 8) - sz; |
| 440 | int flags = TO_ACCEPT1_FLAG(YYTD_DATA32) | |
John Johansen | abbf873 | 2017-01-16 00:42:37 -0800 | [diff] [blame] | 441 | TO_ACCEPT2_FLAG(YYTD_DATA32) | DFA_FLAG_VERIFY_STATES; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 442 | dfa = aa_dfa_unpack(blob + pad, size - pad, flags); |
| 443 | |
| 444 | if (IS_ERR(dfa)) |
| 445 | return dfa; |
| 446 | |
| 447 | if (!verify_accept(dfa, flags)) |
| 448 | goto fail; |
| 449 | } |
| 450 | |
| 451 | return dfa; |
| 452 | |
| 453 | fail: |
| 454 | aa_put_dfa(dfa); |
| 455 | return ERR_PTR(-EPROTO); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * unpack_trans_table - unpack a profile transition table |
| 460 | * @e: serialized data extent information (NOT NULL) |
| 461 | * @profile: profile to add the accept table to (NOT NULL) |
| 462 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 463 | * Returns: 1 if table successfully unpacked |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 464 | */ |
| 465 | static bool unpack_trans_table(struct aa_ext *e, struct aa_profile *profile) |
| 466 | { |
Geert Uytterhoeven | 19fe43a | 2017-07-06 10:56:21 +0200 | [diff] [blame] | 467 | void *saved_pos = e->pos; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 468 | |
| 469 | /* exec table is optional */ |
| 470 | if (unpack_nameX(e, AA_STRUCT, "xtable")) { |
| 471 | int i, size; |
| 472 | |
| 473 | size = unpack_array(e, NULL); |
| 474 | /* currently 4 exec bits and entries 0-3 are reserved iupcx */ |
| 475 | if (size > 16 - 4) |
| 476 | goto fail; |
| 477 | profile->file.trans.table = kzalloc(sizeof(char *) * size, |
| 478 | GFP_KERNEL); |
| 479 | if (!profile->file.trans.table) |
| 480 | goto fail; |
| 481 | |
| 482 | profile->file.trans.size = size; |
| 483 | for (i = 0; i < size; i++) { |
| 484 | char *str; |
John Johansen | 5379a33 | 2017-06-09 17:29:12 -0700 | [diff] [blame] | 485 | int c, j, pos, size2 = unpack_strdup(e, &str, NULL); |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 486 | /* unpack_strdup verifies that the last character is |
| 487 | * null termination byte. |
| 488 | */ |
James Morris | 7ee9585 | 2011-08-29 11:43:02 +1000 | [diff] [blame] | 489 | if (!size2) |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 490 | goto fail; |
| 491 | profile->file.trans.table[i] = str; |
| 492 | /* verify that name doesn't start with space */ |
| 493 | if (isspace(*str)) |
| 494 | goto fail; |
| 495 | |
| 496 | /* count internal # of internal \0 */ |
John Johansen | 5379a33 | 2017-06-09 17:29:12 -0700 | [diff] [blame] | 497 | for (c = j = 0; j < size2 - 1; j++) { |
| 498 | if (!str[j]) { |
| 499 | pos = j; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 500 | c++; |
John Johansen | 5379a33 | 2017-06-09 17:29:12 -0700 | [diff] [blame] | 501 | } |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 502 | } |
| 503 | if (*str == ':') { |
John Johansen | 5379a33 | 2017-06-09 17:29:12 -0700 | [diff] [blame] | 504 | /* first character after : must be valid */ |
| 505 | if (!str[1]) |
| 506 | goto fail; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 507 | /* beginning with : requires an embedded \0, |
| 508 | * verify that exactly 1 internal \0 exists |
| 509 | * trailing \0 already verified by unpack_strdup |
John Johansen | 5379a33 | 2017-06-09 17:29:12 -0700 | [diff] [blame] | 510 | * |
| 511 | * convert \0 back to : for label_parse |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 512 | */ |
John Johansen | 5379a33 | 2017-06-09 17:29:12 -0700 | [diff] [blame] | 513 | if (c == 1) |
| 514 | str[pos] = ':'; |
| 515 | else if (c > 1) |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 516 | goto fail; |
| 517 | } else if (c) |
| 518 | /* fail - all other cases with embedded \0 */ |
| 519 | goto fail; |
| 520 | } |
| 521 | if (!unpack_nameX(e, AA_ARRAYEND, NULL)) |
| 522 | goto fail; |
| 523 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) |
| 524 | goto fail; |
| 525 | } |
| 526 | return 1; |
| 527 | |
| 528 | fail: |
| 529 | aa_free_domain_entries(&profile->file.trans); |
Geert Uytterhoeven | 19fe43a | 2017-07-06 10:56:21 +0200 | [diff] [blame] | 530 | e->pos = saved_pos; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 531 | return 0; |
| 532 | } |
| 533 | |
Matthew Garrett | 8e51f90 | 2018-02-08 12:37:19 -0800 | [diff] [blame] | 534 | static bool unpack_xattrs(struct aa_ext *e, struct aa_profile *profile) |
| 535 | { |
| 536 | void *pos = e->pos; |
| 537 | |
| 538 | if (unpack_nameX(e, AA_STRUCT, "xattrs")) { |
| 539 | int i, size; |
| 540 | |
| 541 | size = unpack_array(e, NULL); |
| 542 | profile->xattr_count = size; |
John Johansen | 73f488c | 2017-12-12 15:28:05 -0800 | [diff] [blame^] | 543 | profile->xattrs = kcalloc(size, sizeof(char *), GFP_KERNEL); |
Matthew Garrett | 8e51f90 | 2018-02-08 12:37:19 -0800 | [diff] [blame] | 544 | if (!profile->xattrs) |
| 545 | goto fail; |
| 546 | for (i = 0; i < size; i++) { |
| 547 | if (!unpack_strdup(e, &profile->xattrs[i], NULL)) |
| 548 | goto fail; |
| 549 | } |
| 550 | if (!unpack_nameX(e, AA_ARRAYEND, NULL)) |
| 551 | goto fail; |
| 552 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) |
| 553 | goto fail; |
| 554 | } |
| 555 | |
Matthew Garrett | 8e51f90 | 2018-02-08 12:37:19 -0800 | [diff] [blame] | 556 | return 1; |
| 557 | |
| 558 | fail: |
| 559 | e->pos = pos; |
| 560 | return 0; |
| 561 | } |
| 562 | |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 563 | static bool unpack_rlimits(struct aa_ext *e, struct aa_profile *profile) |
| 564 | { |
| 565 | void *pos = e->pos; |
| 566 | |
| 567 | /* rlimits are optional */ |
| 568 | if (unpack_nameX(e, AA_STRUCT, "rlimits")) { |
| 569 | int i, size; |
| 570 | u32 tmp = 0; |
| 571 | if (!unpack_u32(e, &tmp, NULL)) |
| 572 | goto fail; |
| 573 | profile->rlimits.mask = tmp; |
| 574 | |
| 575 | size = unpack_array(e, NULL); |
| 576 | if (size > RLIM_NLIMITS) |
| 577 | goto fail; |
| 578 | for (i = 0; i < size; i++) { |
James Morris | 7ee9585 | 2011-08-29 11:43:02 +1000 | [diff] [blame] | 579 | u64 tmp2 = 0; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 580 | int a = aa_map_resource(i); |
James Morris | 7ee9585 | 2011-08-29 11:43:02 +1000 | [diff] [blame] | 581 | if (!unpack_u64(e, &tmp2, NULL)) |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 582 | goto fail; |
James Morris | 7ee9585 | 2011-08-29 11:43:02 +1000 | [diff] [blame] | 583 | profile->rlimits.limits[a].rlim_max = tmp2; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 584 | } |
| 585 | if (!unpack_nameX(e, AA_ARRAYEND, NULL)) |
| 586 | goto fail; |
| 587 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) |
| 588 | goto fail; |
| 589 | } |
| 590 | return 1; |
| 591 | |
| 592 | fail: |
| 593 | e->pos = pos; |
| 594 | return 0; |
| 595 | } |
| 596 | |
William Hua | e025be0 | 2017-01-15 16:49:28 -0800 | [diff] [blame] | 597 | static u32 strhash(const void *data, u32 len, u32 seed) |
| 598 | { |
| 599 | const char * const *key = data; |
| 600 | |
| 601 | return jhash(*key, strlen(*key), seed); |
| 602 | } |
| 603 | |
| 604 | static int datacmp(struct rhashtable_compare_arg *arg, const void *obj) |
| 605 | { |
| 606 | const struct aa_data *data = obj; |
| 607 | const char * const *key = arg->key; |
| 608 | |
| 609 | return strcmp(data->key, *key); |
| 610 | } |
| 611 | |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 612 | /** |
| 613 | * unpack_profile - unpack a serialized profile |
| 614 | * @e: serialized data extent information (NOT NULL) |
| 615 | * |
| 616 | * NOTE: unpack profile sets audit struct if there is a failure |
| 617 | */ |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 618 | static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name) |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 619 | { |
| 620 | struct aa_profile *profile = NULL; |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 621 | const char *tmpname, *tmpns = NULL, *name = NULL; |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 622 | const char *info = "failed to unpack profile"; |
Linus Torvalds | 80c094a | 2017-10-26 19:35:35 +0200 | [diff] [blame] | 623 | size_t ns_len; |
William Hua | e025be0 | 2017-01-15 16:49:28 -0800 | [diff] [blame] | 624 | struct rhashtable_params params = { 0 }; |
| 625 | char *key = NULL; |
| 626 | struct aa_data *data; |
John Johansen | ad5ff3d | 2012-02-16 07:07:53 -0800 | [diff] [blame] | 627 | int i, error = -EPROTO; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 628 | kernel_cap_t tmpcap; |
| 629 | u32 tmp; |
| 630 | |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 631 | *ns_name = NULL; |
| 632 | |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 633 | /* check that we have the right struct being passed */ |
| 634 | if (!unpack_nameX(e, AA_STRUCT, "profile")) |
| 635 | goto fail; |
| 636 | if (!unpack_str(e, &name, NULL)) |
| 637 | goto fail; |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 638 | if (*name == '\0') |
| 639 | goto fail; |
| 640 | |
| 641 | tmpname = aa_splitn_fqname(name, strlen(name), &tmpns, &ns_len); |
| 642 | if (tmpns) { |
| 643 | *ns_name = kstrndup(tmpns, ns_len, GFP_KERNEL); |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 644 | if (!*ns_name) { |
| 645 | info = "out of memory"; |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 646 | goto fail; |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 647 | } |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 648 | name = tmpname; |
| 649 | } |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 650 | |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 651 | profile = aa_alloc_profile(name, NULL, GFP_KERNEL); |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 652 | if (!profile) |
| 653 | return ERR_PTR(-ENOMEM); |
| 654 | |
| 655 | /* profile renaming is optional */ |
| 656 | (void) unpack_str(e, &profile->rename, "rename"); |
| 657 | |
John Johansen | 556d0be | 2013-07-10 21:17:43 -0700 | [diff] [blame] | 658 | /* attachment string is optional */ |
| 659 | (void) unpack_str(e, &profile->attach, "attach"); |
| 660 | |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 661 | /* xmatch is optional and may be NULL */ |
| 662 | profile->xmatch = unpack_dfa(e); |
| 663 | if (IS_ERR(profile->xmatch)) { |
| 664 | error = PTR_ERR(profile->xmatch); |
| 665 | profile->xmatch = NULL; |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 666 | info = "bad xmatch"; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 667 | goto fail; |
| 668 | } |
| 669 | /* xmatch_len is not optional if xmatch is set */ |
| 670 | if (profile->xmatch) { |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 671 | if (!unpack_u32(e, &tmp, NULL)) { |
| 672 | info = "missing xmatch len"; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 673 | goto fail; |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 674 | } |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 675 | profile->xmatch_len = tmp; |
| 676 | } |
| 677 | |
John Johansen | 72c8a76 | 2017-05-22 03:06:52 -0700 | [diff] [blame] | 678 | /* disconnected attachment string is optional */ |
| 679 | (void) unpack_str(e, &profile->disconnected, "disconnected"); |
| 680 | |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 681 | /* per profile debug flags (complain, audit) */ |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 682 | if (!unpack_nameX(e, AA_STRUCT, "flags")) { |
| 683 | info = "profile missing flags"; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 684 | goto fail; |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 685 | } |
| 686 | info = "failed to unpack profile flags"; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 687 | if (!unpack_u32(e, &tmp, NULL)) |
| 688 | goto fail; |
John Johansen | 0381650 | 2013-07-10 21:12:43 -0700 | [diff] [blame] | 689 | if (tmp & PACKED_FLAG_HAT) |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 690 | profile->label.flags |= FLAG_HAT; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 691 | if (!unpack_u32(e, &tmp, NULL)) |
| 692 | goto fail; |
John Johansen | 5ebfb12 | 2017-01-16 00:42:38 -0800 | [diff] [blame] | 693 | if (tmp == PACKED_MODE_COMPLAIN || (e->version & FORCE_COMPLAIN_FLAG)) |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 694 | profile->mode = APPARMOR_COMPLAIN; |
John Johansen | 0381650 | 2013-07-10 21:12:43 -0700 | [diff] [blame] | 695 | else if (tmp == PACKED_MODE_KILL) |
| 696 | profile->mode = APPARMOR_KILL; |
| 697 | else if (tmp == PACKED_MODE_UNCONFINED) |
| 698 | profile->mode = APPARMOR_UNCONFINED; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 699 | if (!unpack_u32(e, &tmp, NULL)) |
| 700 | goto fail; |
| 701 | if (tmp) |
| 702 | profile->audit = AUDIT_ALL; |
| 703 | |
| 704 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) |
| 705 | goto fail; |
| 706 | |
| 707 | /* path_flags is optional */ |
| 708 | if (unpack_u32(e, &profile->path_flags, "path_flags")) |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 709 | profile->path_flags |= profile->label.flags & |
| 710 | PATH_MEDIATE_DELETED; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 711 | else |
| 712 | /* set a default value if path_flags field is not present */ |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 713 | profile->path_flags = PATH_MEDIATE_DELETED; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 714 | |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 715 | info = "failed to unpack profile capabilities"; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 716 | if (!unpack_u32(e, &(profile->caps.allow.cap[0]), NULL)) |
| 717 | goto fail; |
| 718 | if (!unpack_u32(e, &(profile->caps.audit.cap[0]), NULL)) |
| 719 | goto fail; |
| 720 | if (!unpack_u32(e, &(profile->caps.quiet.cap[0]), NULL)) |
| 721 | goto fail; |
| 722 | if (!unpack_u32(e, &tmpcap.cap[0], NULL)) |
| 723 | goto fail; |
| 724 | |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 725 | info = "failed to unpack upper profile capabilities"; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 726 | if (unpack_nameX(e, AA_STRUCT, "caps64")) { |
| 727 | /* optional upper half of 64 bit caps */ |
| 728 | if (!unpack_u32(e, &(profile->caps.allow.cap[1]), NULL)) |
| 729 | goto fail; |
| 730 | if (!unpack_u32(e, &(profile->caps.audit.cap[1]), NULL)) |
| 731 | goto fail; |
| 732 | if (!unpack_u32(e, &(profile->caps.quiet.cap[1]), NULL)) |
| 733 | goto fail; |
| 734 | if (!unpack_u32(e, &(tmpcap.cap[1]), NULL)) |
| 735 | goto fail; |
| 736 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) |
| 737 | goto fail; |
| 738 | } |
| 739 | |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 740 | info = "failed to unpack extended profile capabilities"; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 741 | if (unpack_nameX(e, AA_STRUCT, "capsx")) { |
| 742 | /* optional extended caps mediation mask */ |
| 743 | if (!unpack_u32(e, &(profile->caps.extended.cap[0]), NULL)) |
| 744 | goto fail; |
| 745 | if (!unpack_u32(e, &(profile->caps.extended.cap[1]), NULL)) |
| 746 | goto fail; |
John Johansen | cdbd288 | 2012-02-16 07:06:41 -0800 | [diff] [blame] | 747 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) |
| 748 | goto fail; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Matthew Garrett | 8e51f90 | 2018-02-08 12:37:19 -0800 | [diff] [blame] | 751 | if (!unpack_xattrs(e, profile)) { |
| 752 | info = "failed to unpack profile xattrs"; |
| 753 | goto fail; |
| 754 | } |
| 755 | |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 756 | if (!unpack_rlimits(e, profile)) { |
| 757 | info = "failed to unpack profile rlimits"; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 758 | goto fail; |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 759 | } |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 760 | |
John Johansen | ad5ff3d | 2012-02-16 07:07:53 -0800 | [diff] [blame] | 761 | if (unpack_nameX(e, AA_STRUCT, "policydb")) { |
| 762 | /* generic policy dfa - optional and may be NULL */ |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 763 | info = "failed to unpack policydb"; |
John Johansen | ad5ff3d | 2012-02-16 07:07:53 -0800 | [diff] [blame] | 764 | profile->policy.dfa = unpack_dfa(e); |
| 765 | if (IS_ERR(profile->policy.dfa)) { |
| 766 | error = PTR_ERR(profile->policy.dfa); |
| 767 | profile->policy.dfa = NULL; |
| 768 | goto fail; |
John Johansen | 5f20fdf | 2016-06-15 10:00:55 +0300 | [diff] [blame] | 769 | } else if (!profile->policy.dfa) { |
| 770 | error = -EPROTO; |
| 771 | goto fail; |
John Johansen | ad5ff3d | 2012-02-16 07:07:53 -0800 | [diff] [blame] | 772 | } |
| 773 | if (!unpack_u32(e, &profile->policy.start[0], "start")) |
| 774 | /* default start state */ |
| 775 | profile->policy.start[0] = DFA_START; |
| 776 | /* setup class index */ |
| 777 | for (i = AA_CLASS_FILE; i <= AA_CLASS_LAST; i++) { |
| 778 | profile->policy.start[i] = |
| 779 | aa_dfa_next(profile->policy.dfa, |
| 780 | profile->policy.start[0], |
| 781 | i); |
| 782 | } |
| 783 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) |
| 784 | goto fail; |
John Johansen | 11c236b | 2017-01-16 00:42:42 -0800 | [diff] [blame] | 785 | } else |
| 786 | profile->policy.dfa = aa_get_dfa(nulldfa); |
John Johansen | ad5ff3d | 2012-02-16 07:07:53 -0800 | [diff] [blame] | 787 | |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 788 | /* get file rules */ |
| 789 | profile->file.dfa = unpack_dfa(e); |
| 790 | if (IS_ERR(profile->file.dfa)) { |
| 791 | error = PTR_ERR(profile->file.dfa); |
| 792 | profile->file.dfa = NULL; |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 793 | info = "failed to unpack profile file rules"; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 794 | goto fail; |
John Johansen | 6604d4c | 2017-01-16 00:42:41 -0800 | [diff] [blame] | 795 | } else if (profile->file.dfa) { |
| 796 | if (!unpack_u32(e, &profile->file.start, "dfa_start")) |
| 797 | /* default start state */ |
| 798 | profile->file.start = DFA_START; |
| 799 | } else if (profile->policy.dfa && |
| 800 | profile->policy.start[AA_CLASS_FILE]) { |
| 801 | profile->file.dfa = aa_get_dfa(profile->policy.dfa); |
| 802 | profile->file.start = profile->policy.start[AA_CLASS_FILE]; |
John Johansen | 11c236b | 2017-01-16 00:42:42 -0800 | [diff] [blame] | 803 | } else |
| 804 | profile->file.dfa = aa_get_dfa(nulldfa); |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 805 | |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 806 | if (!unpack_trans_table(e, profile)) { |
| 807 | info = "failed to unpack profile transition table"; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 808 | goto fail; |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 809 | } |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 810 | |
William Hua | e025be0 | 2017-01-15 16:49:28 -0800 | [diff] [blame] | 811 | if (unpack_nameX(e, AA_STRUCT, "data")) { |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 812 | info = "out of memory"; |
William Hua | e025be0 | 2017-01-15 16:49:28 -0800 | [diff] [blame] | 813 | profile->data = kzalloc(sizeof(*profile->data), GFP_KERNEL); |
| 814 | if (!profile->data) |
| 815 | goto fail; |
| 816 | |
| 817 | params.nelem_hint = 3; |
| 818 | params.key_len = sizeof(void *); |
| 819 | params.key_offset = offsetof(struct aa_data, key); |
| 820 | params.head_offset = offsetof(struct aa_data, head); |
| 821 | params.hashfn = strhash; |
| 822 | params.obj_cmpfn = datacmp; |
| 823 | |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 824 | if (rhashtable_init(profile->data, ¶ms)) { |
| 825 | info = "failed to init key, value hash table"; |
William Hua | e025be0 | 2017-01-15 16:49:28 -0800 | [diff] [blame] | 826 | goto fail; |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 827 | } |
William Hua | e025be0 | 2017-01-15 16:49:28 -0800 | [diff] [blame] | 828 | |
| 829 | while (unpack_strdup(e, &key, NULL)) { |
| 830 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 831 | if (!data) { |
| 832 | kzfree(key); |
| 833 | goto fail; |
| 834 | } |
| 835 | |
| 836 | data->key = key; |
| 837 | data->size = unpack_blob(e, &data->data, NULL); |
| 838 | data->data = kvmemdup(data->data, data->size); |
| 839 | if (data->size && !data->data) { |
| 840 | kzfree(data->key); |
| 841 | kzfree(data); |
| 842 | goto fail; |
| 843 | } |
| 844 | |
| 845 | rhashtable_insert_fast(profile->data, &data->head, |
| 846 | profile->data->p); |
| 847 | } |
| 848 | |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 849 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) { |
| 850 | info = "failed to unpack end of key, value data table"; |
William Hua | e025be0 | 2017-01-15 16:49:28 -0800 | [diff] [blame] | 851 | goto fail; |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 852 | } |
William Hua | e025be0 | 2017-01-15 16:49:28 -0800 | [diff] [blame] | 853 | } |
| 854 | |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 855 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) { |
| 856 | info = "failed to unpack end of profile"; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 857 | goto fail; |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 858 | } |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 859 | |
| 860 | return profile; |
| 861 | |
| 862 | fail: |
| 863 | if (profile) |
| 864 | name = NULL; |
| 865 | else if (!name) |
| 866 | name = "unknown"; |
John Johansen | 2410aa9 | 2017-07-18 23:37:18 -0700 | [diff] [blame] | 867 | audit_iface(profile, NULL, name, info, e, error); |
John Johansen | 8651e1d6 | 2013-07-10 21:11:43 -0700 | [diff] [blame] | 868 | aa_free_profile(profile); |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 869 | |
| 870 | return ERR_PTR(error); |
| 871 | } |
| 872 | |
| 873 | /** |
| 874 | * verify_head - unpack serialized stream header |
| 875 | * @e: serialized data read head (NOT NULL) |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 876 | * @required: whether the header is required or optional |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 877 | * @ns: Returns - namespace if one is specified else NULL (NOT NULL) |
| 878 | * |
| 879 | * Returns: error or 0 if header is good |
| 880 | */ |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 881 | static int verify_header(struct aa_ext *e, int required, const char **ns) |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 882 | { |
| 883 | int error = -EPROTONOSUPPORT; |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 884 | const char *name = NULL; |
| 885 | *ns = NULL; |
| 886 | |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 887 | /* get the interface version */ |
| 888 | if (!unpack_u32(e, &e->version, "version")) { |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 889 | if (required) { |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 890 | audit_iface(NULL, NULL, NULL, "invalid profile format", |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 891 | e, error); |
| 892 | return error; |
| 893 | } |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 894 | } |
| 895 | |
John Johansen | 474d6b75 | 2017-01-16 00:42:39 -0800 | [diff] [blame] | 896 | /* Check that the interface version is currently supported. |
| 897 | * if not specified use previous version |
| 898 | * Mask off everything that is not kernel abi version |
| 899 | */ |
Christos Gkekas | 86aea56 | 2017-07-08 20:50:21 +0100 | [diff] [blame] | 900 | if (VERSION_LT(e->version, v5) || VERSION_GT(e->version, v7)) { |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 901 | audit_iface(NULL, NULL, NULL, "unsupported interface version", |
John Johansen | 474d6b75 | 2017-01-16 00:42:39 -0800 | [diff] [blame] | 902 | e, error); |
| 903 | return error; |
| 904 | } |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 905 | |
| 906 | /* read the namespace if present */ |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 907 | if (unpack_str(e, &name, "namespace")) { |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 908 | if (*name == '\0') { |
| 909 | audit_iface(NULL, NULL, NULL, "invalid namespace name", |
| 910 | e, error); |
| 911 | return error; |
| 912 | } |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 913 | if (*ns && strcmp(*ns, name)) |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 914 | audit_iface(NULL, NULL, NULL, "invalid ns change", e, |
| 915 | error); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 916 | else if (!*ns) |
| 917 | *ns = name; |
| 918 | } |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 919 | |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | static bool verify_xindex(int xindex, int table_size) |
| 924 | { |
| 925 | int index, xtype; |
| 926 | xtype = xindex & AA_X_TYPE_MASK; |
| 927 | index = xindex & AA_X_INDEX_MASK; |
John Johansen | 23ca7b6 | 2016-03-17 12:02:54 -0700 | [diff] [blame] | 928 | if (xtype == AA_X_TABLE && index >= table_size) |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 929 | return 0; |
| 930 | return 1; |
| 931 | } |
| 932 | |
| 933 | /* verify dfa xindexes are in range of transition tables */ |
| 934 | static bool verify_dfa_xindex(struct aa_dfa *dfa, int table_size) |
| 935 | { |
| 936 | int i; |
| 937 | for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) { |
| 938 | if (!verify_xindex(dfa_user_xindex(dfa, i), table_size)) |
| 939 | return 0; |
| 940 | if (!verify_xindex(dfa_other_xindex(dfa, i), table_size)) |
| 941 | return 0; |
| 942 | } |
| 943 | return 1; |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * verify_profile - Do post unpack analysis to verify profile consistency |
| 948 | * @profile: profile to verify (NOT NULL) |
| 949 | * |
| 950 | * Returns: 0 if passes verification else error |
| 951 | */ |
| 952 | static int verify_profile(struct aa_profile *profile) |
| 953 | { |
John Johansen | abbf873 | 2017-01-16 00:42:37 -0800 | [diff] [blame] | 954 | if (profile->file.dfa && |
| 955 | !verify_dfa_xindex(profile->file.dfa, |
| 956 | profile->file.trans.size)) { |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 957 | audit_iface(profile, NULL, NULL, "Invalid named transition", |
John Johansen | abbf873 | 2017-01-16 00:42:37 -0800 | [diff] [blame] | 958 | NULL, -EPROTO); |
| 959 | return -EPROTO; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | return 0; |
| 963 | } |
| 964 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 965 | void aa_load_ent_free(struct aa_load_ent *ent) |
| 966 | { |
| 967 | if (ent) { |
| 968 | aa_put_profile(ent->rename); |
| 969 | aa_put_profile(ent->old); |
| 970 | aa_put_profile(ent->new); |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 971 | kfree(ent->ns_name); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 972 | kzfree(ent); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | struct aa_load_ent *aa_load_ent_alloc(void) |
| 977 | { |
| 978 | struct aa_load_ent *ent = kzalloc(sizeof(*ent), GFP_KERNEL); |
| 979 | if (ent) |
| 980 | INIT_LIST_HEAD(&ent->list); |
| 981 | return ent; |
| 982 | } |
| 983 | |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 984 | /** |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 985 | * aa_unpack - unpack packed binary profile(s) data loaded from user space |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 986 | * @udata: user data copied to kmem (NOT NULL) |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 987 | * @lh: list to place unpacked profiles in a aa_repl_ws |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 988 | * @ns: Returns namespace profile is in if specified else NULL (NOT NULL) |
| 989 | * |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 990 | * Unpack user data and return refcounted allocated profile(s) stored in |
| 991 | * @lh in order of discovery, with the list chain stored in base.list |
| 992 | * or error |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 993 | * |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 994 | * Returns: profile(s) on @lh else error pointer if fails to unpack |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 995 | */ |
John Johansen | 5ac8c35 | 2017-01-16 00:42:55 -0800 | [diff] [blame] | 996 | int aa_unpack(struct aa_loaddata *udata, struct list_head *lh, |
| 997 | const char **ns) |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 998 | { |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 999 | struct aa_load_ent *tmp, *ent; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 1000 | struct aa_profile *profile = NULL; |
| 1001 | int error; |
| 1002 | struct aa_ext e = { |
John Johansen | 5ac8c35 | 2017-01-16 00:42:55 -0800 | [diff] [blame] | 1003 | .start = udata->data, |
| 1004 | .end = udata->data + udata->size, |
| 1005 | .pos = udata->data, |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 1006 | }; |
| 1007 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1008 | *ns = NULL; |
| 1009 | while (e.pos < e.end) { |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 1010 | char *ns_name = NULL; |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 1011 | void *start; |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1012 | error = verify_header(&e, e.pos == e.start, ns); |
| 1013 | if (error) |
| 1014 | goto fail; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 1015 | |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 1016 | start = e.pos; |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 1017 | profile = unpack_profile(&e, &ns_name); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1018 | if (IS_ERR(profile)) { |
| 1019 | error = PTR_ERR(profile); |
| 1020 | goto fail; |
| 1021 | } |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 1022 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1023 | error = verify_profile(profile); |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 1024 | if (error) |
| 1025 | goto fail_profile; |
| 1026 | |
John Johansen | 31f75bf | 2017-01-16 00:43:07 -0800 | [diff] [blame] | 1027 | if (aa_g_hash_policy) |
| 1028 | error = aa_calc_profile_hash(profile, e.version, start, |
John Johansen | 6059f71 | 2014-10-24 09:16:14 -0700 | [diff] [blame] | 1029 | e.pos - start); |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 1030 | if (error) |
| 1031 | goto fail_profile; |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1032 | |
| 1033 | ent = aa_load_ent_alloc(); |
| 1034 | if (!ent) { |
| 1035 | error = -ENOMEM; |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 1036 | goto fail_profile; |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | ent->new = profile; |
John Johansen | 04dc715 | 2017-01-16 00:42:56 -0800 | [diff] [blame] | 1040 | ent->ns_name = ns_name; |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1041 | list_add_tail(&ent->list, lh); |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 1042 | } |
John Johansen | 5ac8c35 | 2017-01-16 00:42:55 -0800 | [diff] [blame] | 1043 | udata->abi = e.version & K_ABI_MASK; |
John Johansen | 31f75bf | 2017-01-16 00:43:07 -0800 | [diff] [blame] | 1044 | if (aa_g_hash_policy) { |
| 1045 | udata->hash = aa_calc_hash(udata->data, udata->size); |
| 1046 | if (IS_ERR(udata->hash)) { |
| 1047 | error = PTR_ERR(udata->hash); |
| 1048 | udata->hash = NULL; |
| 1049 | goto fail; |
| 1050 | } |
John Johansen | 5ac8c35 | 2017-01-16 00:42:55 -0800 | [diff] [blame] | 1051 | } |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1052 | return 0; |
| 1053 | |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 1054 | fail_profile: |
| 1055 | aa_put_profile(profile); |
| 1056 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1057 | fail: |
| 1058 | list_for_each_entry_safe(ent, tmp, lh, list) { |
| 1059 | list_del_init(&ent->list); |
| 1060 | aa_load_ent_free(ent); |
| 1061 | } |
| 1062 | |
| 1063 | return error; |
John Johansen | 736ec752 | 2010-07-29 14:48:02 -0700 | [diff] [blame] | 1064 | } |