policy_unpack.c 29 KB

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