policy_unpack.c 25 KB

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