policy_unpack.c 25 KB

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