policy_unpack.c 25 KB

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