policy_unpack.c 25 KB

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