policy_unpack.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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 <linux/unaligned.h>
  16. #include <kunit/visibility.h>
  17. #include <linux/ctype.h>
  18. #include <linux/errno.h>
  19. #include <linux/zstd.h>
  20. #include "include/apparmor.h"
  21. #include "include/audit.h"
  22. #include "include/cred.h"
  23. #include "include/crypto.h"
  24. #include "include/file.h"
  25. #include "include/match.h"
  26. #include "include/path.h"
  27. #include "include/policy.h"
  28. #include "include/policy_unpack.h"
  29. #include "include/policy_compat.h"
  30. /* audit callback for unpack fields */
  31. static void audit_cb(struct audit_buffer *ab, void *va)
  32. {
  33. struct common_audit_data *sa = va;
  34. struct apparmor_audit_data *ad = aad(sa);
  35. if (ad->iface.ns) {
  36. audit_log_format(ab, " ns=");
  37. audit_log_untrustedstring(ab, ad->iface.ns);
  38. }
  39. if (ad->name) {
  40. audit_log_format(ab, " name=");
  41. audit_log_untrustedstring(ab, ad->name);
  42. }
  43. if (ad->iface.pos)
  44. audit_log_format(ab, " offset=%ld", ad->iface.pos);
  45. }
  46. /**
  47. * audit_iface - do audit message for policy unpacking/load/replace/remove
  48. * @new: profile if it has been allocated (MAYBE NULL)
  49. * @ns_name: name of the ns the profile is to be loaded to (MAY BE NULL)
  50. * @name: name of the profile being manipulated (MAYBE NULL)
  51. * @info: any extra info about the failure (MAYBE NULL)
  52. * @e: buffer position info
  53. * @error: error code
  54. *
  55. * Returns: %0 or error
  56. */
  57. static int audit_iface(struct aa_profile *new, const char *ns_name,
  58. const char *name, const char *info, struct aa_ext *e,
  59. int error)
  60. {
  61. struct aa_profile *profile = labels_profile(aa_current_raw_label());
  62. DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE, NULL);
  63. if (e)
  64. ad.iface.pos = e->pos - e->start;
  65. ad.iface.ns = ns_name;
  66. if (new)
  67. ad.name = new->base.hname;
  68. else
  69. ad.name = name;
  70. ad.info = info;
  71. ad.error = error;
  72. return aa_audit(AUDIT_APPARMOR_STATUS, profile, &ad, audit_cb);
  73. }
  74. void __aa_loaddata_update(struct aa_loaddata *data, long revision)
  75. {
  76. AA_BUG(!data);
  77. AA_BUG(!data->ns);
  78. AA_BUG(!mutex_is_locked(&data->ns->lock));
  79. AA_BUG(data->revision > revision);
  80. data->revision = revision;
  81. if ((data->dents[AAFS_LOADDATA_REVISION])) {
  82. struct inode *inode;
  83. inode = d_inode(data->dents[AAFS_LOADDATA_DIR]);
  84. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  85. inode = d_inode(data->dents[AAFS_LOADDATA_REVISION]);
  86. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  87. }
  88. }
  89. bool aa_rawdata_eq(struct aa_loaddata *l, struct aa_loaddata *r)
  90. {
  91. if (l->size != r->size)
  92. return false;
  93. if (l->compressed_size != r->compressed_size)
  94. return false;
  95. if (aa_g_hash_policy && memcmp(l->hash, r->hash, aa_hash_size()) != 0)
  96. return false;
  97. return memcmp(l->data, r->data, r->compressed_size ?: r->size) == 0;
  98. }
  99. /*
  100. * need to take the ns mutex lock which is NOT safe most places that
  101. * put_loaddata is called, so we have to delay freeing it
  102. */
  103. static void do_loaddata_free(struct work_struct *work)
  104. {
  105. struct aa_loaddata *d = container_of(work, struct aa_loaddata, work);
  106. struct aa_ns *ns = aa_get_ns(d->ns);
  107. if (ns) {
  108. mutex_lock_nested(&ns->lock, ns->level);
  109. __aa_fs_remove_rawdata(d);
  110. mutex_unlock(&ns->lock);
  111. aa_put_ns(ns);
  112. }
  113. kfree_sensitive(d->hash);
  114. kfree_sensitive(d->name);
  115. kvfree(d->data);
  116. kfree_sensitive(d);
  117. }
  118. void aa_loaddata_kref(struct kref *kref)
  119. {
  120. struct aa_loaddata *d = container_of(kref, struct aa_loaddata, count);
  121. if (d) {
  122. INIT_WORK(&d->work, do_loaddata_free);
  123. schedule_work(&d->work);
  124. }
  125. }
  126. struct aa_loaddata *aa_loaddata_alloc(size_t size)
  127. {
  128. struct aa_loaddata *d;
  129. d = kzalloc(sizeof(*d), GFP_KERNEL);
  130. if (d == NULL)
  131. return ERR_PTR(-ENOMEM);
  132. d->data = kvzalloc(size, GFP_KERNEL);
  133. if (!d->data) {
  134. kfree(d);
  135. return ERR_PTR(-ENOMEM);
  136. }
  137. kref_init(&d->count);
  138. INIT_LIST_HEAD(&d->list);
  139. return d;
  140. }
  141. /* test if read will be in packed data bounds */
  142. VISIBLE_IF_KUNIT bool aa_inbounds(struct aa_ext *e, size_t size)
  143. {
  144. return (size <= e->end - e->pos);
  145. }
  146. EXPORT_SYMBOL_IF_KUNIT(aa_inbounds);
  147. /**
  148. * aa_unpack_u16_chunk - test and do bounds checking for a u16 size based chunk
  149. * @e: serialized data read head (NOT NULL)
  150. * @chunk: start address for chunk of data (NOT NULL)
  151. *
  152. * Returns: the size of chunk found with the read head at the end of the chunk.
  153. */
  154. VISIBLE_IF_KUNIT size_t aa_unpack_u16_chunk(struct aa_ext *e, char **chunk)
  155. {
  156. size_t size = 0;
  157. void *pos = e->pos;
  158. if (!aa_inbounds(e, sizeof(u16)))
  159. goto fail;
  160. size = le16_to_cpu(get_unaligned((__le16 *) e->pos));
  161. e->pos += sizeof(__le16);
  162. if (!aa_inbounds(e, size))
  163. goto fail;
  164. *chunk = e->pos;
  165. e->pos += size;
  166. return size;
  167. fail:
  168. e->pos = pos;
  169. return 0;
  170. }
  171. EXPORT_SYMBOL_IF_KUNIT(aa_unpack_u16_chunk);
  172. /* unpack control byte */
  173. VISIBLE_IF_KUNIT bool aa_unpack_X(struct aa_ext *e, enum aa_code code)
  174. {
  175. if (!aa_inbounds(e, 1))
  176. return false;
  177. if (*(u8 *) e->pos != code)
  178. return false;
  179. e->pos++;
  180. return true;
  181. }
  182. EXPORT_SYMBOL_IF_KUNIT(aa_unpack_X);
  183. /**
  184. * aa_unpack_nameX - check is the next element is of type X with a name of @name
  185. * @e: serialized data extent information (NOT NULL)
  186. * @code: type code
  187. * @name: name to match to the serialized element. (MAYBE NULL)
  188. *
  189. * check that the next serialized data element is of type X and has a tag
  190. * name @name. If @name is specified then there must be a matching
  191. * name element in the stream. If @name is NULL any name element will be
  192. * skipped and only the typecode will be tested.
  193. *
  194. * Returns true on success (both type code and name tests match) and the read
  195. * head is advanced past the headers
  196. *
  197. * Returns: false if either match fails, the read head does not move
  198. */
  199. VISIBLE_IF_KUNIT bool aa_unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name)
  200. {
  201. /*
  202. * May need to reset pos if name or type doesn't match
  203. */
  204. void *pos = e->pos;
  205. /*
  206. * Check for presence of a tagname, and if present name size
  207. * AA_NAME tag value is a u16.
  208. */
  209. if (aa_unpack_X(e, AA_NAME)) {
  210. char *tag = NULL;
  211. size_t size = aa_unpack_u16_chunk(e, &tag);
  212. /* if a name is specified it must match. otherwise skip tag */
  213. if (name && (!size || tag[size-1] != '\0' || strcmp(name, tag)))
  214. goto fail;
  215. } else if (name) {
  216. /* if a name is specified and there is no name tag fail */
  217. goto fail;
  218. }
  219. /* now check if type code matches */
  220. if (aa_unpack_X(e, code))
  221. return true;
  222. fail:
  223. e->pos = pos;
  224. return false;
  225. }
  226. EXPORT_SYMBOL_IF_KUNIT(aa_unpack_nameX);
  227. static bool unpack_u8(struct aa_ext *e, u8 *data, const char *name)
  228. {
  229. void *pos = e->pos;
  230. if (aa_unpack_nameX(e, AA_U8, name)) {
  231. if (!aa_inbounds(e, sizeof(u8)))
  232. goto fail;
  233. if (data)
  234. *data = *((u8 *)e->pos);
  235. e->pos += sizeof(u8);
  236. return true;
  237. }
  238. fail:
  239. e->pos = pos;
  240. return false;
  241. }
  242. VISIBLE_IF_KUNIT bool aa_unpack_u32(struct aa_ext *e, u32 *data, const char *name)
  243. {
  244. void *pos = e->pos;
  245. if (aa_unpack_nameX(e, AA_U32, name)) {
  246. if (!aa_inbounds(e, sizeof(u32)))
  247. goto fail;
  248. if (data)
  249. *data = le32_to_cpu(get_unaligned((__le32 *) e->pos));
  250. e->pos += sizeof(u32);
  251. return true;
  252. }
  253. fail:
  254. e->pos = pos;
  255. return false;
  256. }
  257. EXPORT_SYMBOL_IF_KUNIT(aa_unpack_u32);
  258. VISIBLE_IF_KUNIT bool aa_unpack_u64(struct aa_ext *e, u64 *data, const char *name)
  259. {
  260. void *pos = e->pos;
  261. if (aa_unpack_nameX(e, AA_U64, name)) {
  262. if (!aa_inbounds(e, sizeof(u64)))
  263. goto fail;
  264. if (data)
  265. *data = le64_to_cpu(get_unaligned((__le64 *) e->pos));
  266. e->pos += sizeof(u64);
  267. return true;
  268. }
  269. fail:
  270. e->pos = pos;
  271. return false;
  272. }
  273. EXPORT_SYMBOL_IF_KUNIT(aa_unpack_u64);
  274. static bool aa_unpack_cap_low(struct aa_ext *e, kernel_cap_t *data, const char *name)
  275. {
  276. u32 val;
  277. if (!aa_unpack_u32(e, &val, name))
  278. return false;
  279. data->val = val;
  280. return true;
  281. }
  282. static bool aa_unpack_cap_high(struct aa_ext *e, kernel_cap_t *data, const char *name)
  283. {
  284. u32 val;
  285. if (!aa_unpack_u32(e, &val, name))
  286. return false;
  287. data->val = (u32)data->val | ((u64)val << 32);
  288. return true;
  289. }
  290. VISIBLE_IF_KUNIT bool aa_unpack_array(struct aa_ext *e, const char *name, u16 *size)
  291. {
  292. void *pos = e->pos;
  293. if (aa_unpack_nameX(e, AA_ARRAY, name)) {
  294. if (!aa_inbounds(e, sizeof(u16)))
  295. goto fail;
  296. *size = le16_to_cpu(get_unaligned((__le16 *) e->pos));
  297. e->pos += sizeof(u16);
  298. return true;
  299. }
  300. fail:
  301. e->pos = pos;
  302. return false;
  303. }
  304. EXPORT_SYMBOL_IF_KUNIT(aa_unpack_array);
  305. VISIBLE_IF_KUNIT size_t aa_unpack_blob(struct aa_ext *e, char **blob, const char *name)
  306. {
  307. void *pos = e->pos;
  308. if (aa_unpack_nameX(e, AA_BLOB, name)) {
  309. u32 size;
  310. if (!aa_inbounds(e, sizeof(u32)))
  311. goto fail;
  312. size = le32_to_cpu(get_unaligned((__le32 *) e->pos));
  313. e->pos += sizeof(u32);
  314. if (aa_inbounds(e, (size_t) size)) {
  315. *blob = e->pos;
  316. e->pos += size;
  317. return size;
  318. }
  319. }
  320. fail:
  321. e->pos = pos;
  322. return 0;
  323. }
  324. EXPORT_SYMBOL_IF_KUNIT(aa_unpack_blob);
  325. VISIBLE_IF_KUNIT int aa_unpack_str(struct aa_ext *e, const char **string, const char *name)
  326. {
  327. char *src_str;
  328. size_t size = 0;
  329. void *pos = e->pos;
  330. *string = NULL;
  331. if (aa_unpack_nameX(e, AA_STRING, name)) {
  332. size = aa_unpack_u16_chunk(e, &src_str);
  333. if (size) {
  334. /* strings are null terminated, length is size - 1 */
  335. if (src_str[size - 1] != 0)
  336. goto fail;
  337. *string = src_str;
  338. return size;
  339. }
  340. }
  341. fail:
  342. e->pos = pos;
  343. return 0;
  344. }
  345. EXPORT_SYMBOL_IF_KUNIT(aa_unpack_str);
  346. VISIBLE_IF_KUNIT int aa_unpack_strdup(struct aa_ext *e, char **string, const char *name)
  347. {
  348. const char *tmp;
  349. void *pos = e->pos;
  350. int res = aa_unpack_str(e, &tmp, name);
  351. *string = NULL;
  352. if (!res)
  353. return 0;
  354. *string = kmemdup(tmp, res, GFP_KERNEL);
  355. if (!*string) {
  356. e->pos = pos;
  357. return 0;
  358. }
  359. return res;
  360. }
  361. EXPORT_SYMBOL_IF_KUNIT(aa_unpack_strdup);
  362. /**
  363. * unpack_dfa - unpack a file rule dfa
  364. * @e: serialized data extent information (NOT NULL)
  365. * @flags: dfa flags to check
  366. *
  367. * returns dfa or ERR_PTR or NULL if no dfa
  368. */
  369. static struct aa_dfa *unpack_dfa(struct aa_ext *e, int flags)
  370. {
  371. char *blob = NULL;
  372. size_t size;
  373. struct aa_dfa *dfa = NULL;
  374. size = aa_unpack_blob(e, &blob, "aadfa");
  375. if (size) {
  376. /*
  377. * The dfa is aligned with in the blob to 8 bytes
  378. * from the beginning of the stream.
  379. * alignment adjust needed by dfa unpack
  380. */
  381. size_t sz = blob - (char *) e->start -
  382. ((e->pos - e->start) & 7);
  383. size_t pad = ALIGN(sz, 8) - sz;
  384. if (aa_g_paranoid_load)
  385. flags |= DFA_FLAG_VERIFY_STATES;
  386. dfa = aa_dfa_unpack(blob + pad, size - pad, flags);
  387. if (IS_ERR(dfa))
  388. return dfa;
  389. }
  390. return dfa;
  391. }
  392. /**
  393. * unpack_trans_table - unpack a profile transition table
  394. * @e: serialized data extent information (NOT NULL)
  395. * @strs: str table to unpack to (NOT NULL)
  396. *
  397. * Returns: true if table successfully unpacked or not present
  398. */
  399. static bool unpack_trans_table(struct aa_ext *e, struct aa_str_table *strs)
  400. {
  401. void *saved_pos = e->pos;
  402. char **table = NULL;
  403. /* exec table is optional */
  404. if (aa_unpack_nameX(e, AA_STRUCT, "xtable")) {
  405. u16 size;
  406. int i;
  407. if (!aa_unpack_array(e, NULL, &size))
  408. /*
  409. * Note: index into trans table array is a max
  410. * of 2^24, but unpack array can only unpack
  411. * an array of 2^16 in size atm so no need
  412. * for size check here
  413. */
  414. goto fail;
  415. table = kcalloc(size, sizeof(char *), GFP_KERNEL);
  416. if (!table)
  417. goto fail;
  418. strs->table = table;
  419. strs->size = size;
  420. for (i = 0; i < size; i++) {
  421. char *str;
  422. int c, j, pos, size2 = aa_unpack_strdup(e, &str, NULL);
  423. /* aa_unpack_strdup verifies that the last character is
  424. * null termination byte.
  425. */
  426. if (!size2)
  427. goto fail;
  428. table[i] = str;
  429. /* verify that name doesn't start with space */
  430. if (isspace(*str))
  431. goto fail;
  432. /* count internal # of internal \0 */
  433. for (c = j = 0; j < size2 - 1; j++) {
  434. if (!str[j]) {
  435. pos = j;
  436. c++;
  437. }
  438. }
  439. if (*str == ':') {
  440. /* first character after : must be valid */
  441. if (!str[1])
  442. goto fail;
  443. /* beginning with : requires an embedded \0,
  444. * verify that exactly 1 internal \0 exists
  445. * trailing \0 already verified by aa_unpack_strdup
  446. *
  447. * convert \0 back to : for label_parse
  448. */
  449. if (c == 1)
  450. str[pos] = ':';
  451. else if (c > 1)
  452. goto fail;
  453. } else if (c)
  454. /* fail - all other cases with embedded \0 */
  455. goto fail;
  456. }
  457. if (!aa_unpack_nameX(e, AA_ARRAYEND, NULL))
  458. goto fail;
  459. if (!aa_unpack_nameX(e, AA_STRUCTEND, NULL))
  460. goto fail;
  461. }
  462. return true;
  463. fail:
  464. aa_free_str_table(strs);
  465. e->pos = saved_pos;
  466. return false;
  467. }
  468. static bool unpack_xattrs(struct aa_ext *e, struct aa_profile *profile)
  469. {
  470. void *pos = e->pos;
  471. if (aa_unpack_nameX(e, AA_STRUCT, "xattrs")) {
  472. u16 size;
  473. int i;
  474. if (!aa_unpack_array(e, NULL, &size))
  475. goto fail;
  476. profile->attach.xattr_count = size;
  477. profile->attach.xattrs = kcalloc(size, sizeof(char *), GFP_KERNEL);
  478. if (!profile->attach.xattrs)
  479. goto fail;
  480. for (i = 0; i < size; i++) {
  481. if (!aa_unpack_strdup(e, &profile->attach.xattrs[i], NULL))
  482. goto fail;
  483. }
  484. if (!aa_unpack_nameX(e, AA_ARRAYEND, NULL))
  485. goto fail;
  486. if (!aa_unpack_nameX(e, AA_STRUCTEND, NULL))
  487. goto fail;
  488. }
  489. return true;
  490. fail:
  491. e->pos = pos;
  492. return false;
  493. }
  494. static bool unpack_secmark(struct aa_ext *e, struct aa_ruleset *rules)
  495. {
  496. void *pos = e->pos;
  497. u16 size;
  498. int i;
  499. if (aa_unpack_nameX(e, AA_STRUCT, "secmark")) {
  500. if (!aa_unpack_array(e, NULL, &size))
  501. goto fail;
  502. rules->secmark = kcalloc(size, sizeof(struct aa_secmark),
  503. GFP_KERNEL);
  504. if (!rules->secmark)
  505. goto fail;
  506. rules->secmark_count = size;
  507. for (i = 0; i < size; i++) {
  508. if (!unpack_u8(e, &rules->secmark[i].audit, NULL))
  509. goto fail;
  510. if (!unpack_u8(e, &rules->secmark[i].deny, NULL))
  511. goto fail;
  512. if (!aa_unpack_strdup(e, &rules->secmark[i].label, NULL))
  513. goto fail;
  514. }
  515. if (!aa_unpack_nameX(e, AA_ARRAYEND, NULL))
  516. goto fail;
  517. if (!aa_unpack_nameX(e, AA_STRUCTEND, NULL))
  518. goto fail;
  519. }
  520. return true;
  521. fail:
  522. if (rules->secmark) {
  523. for (i = 0; i < size; i++)
  524. kfree(rules->secmark[i].label);
  525. kfree(rules->secmark);
  526. rules->secmark_count = 0;
  527. rules->secmark = NULL;
  528. }
  529. e->pos = pos;
  530. return false;
  531. }
  532. static bool unpack_rlimits(struct aa_ext *e, struct aa_ruleset *rules)
  533. {
  534. void *pos = e->pos;
  535. /* rlimits are optional */
  536. if (aa_unpack_nameX(e, AA_STRUCT, "rlimits")) {
  537. u16 size;
  538. int i;
  539. u32 tmp = 0;
  540. if (!aa_unpack_u32(e, &tmp, NULL))
  541. goto fail;
  542. rules->rlimits.mask = tmp;
  543. if (!aa_unpack_array(e, NULL, &size) ||
  544. size > RLIM_NLIMITS)
  545. goto fail;
  546. for (i = 0; i < size; i++) {
  547. u64 tmp2 = 0;
  548. int a = aa_map_resource(i);
  549. if (!aa_unpack_u64(e, &tmp2, NULL))
  550. goto fail;
  551. rules->rlimits.limits[a].rlim_max = tmp2;
  552. }
  553. if (!aa_unpack_nameX(e, AA_ARRAYEND, NULL))
  554. goto fail;
  555. if (!aa_unpack_nameX(e, AA_STRUCTEND, NULL))
  556. goto fail;
  557. }
  558. return true;
  559. fail:
  560. e->pos = pos;
  561. return false;
  562. }
  563. static bool unpack_perm(struct aa_ext *e, u32 version, struct aa_perms *perm)
  564. {
  565. if (version != 1)
  566. return false;
  567. return aa_unpack_u32(e, &perm->allow, NULL) &&
  568. aa_unpack_u32(e, &perm->allow, NULL) &&
  569. aa_unpack_u32(e, &perm->deny, NULL) &&
  570. aa_unpack_u32(e, &perm->subtree, NULL) &&
  571. aa_unpack_u32(e, &perm->cond, NULL) &&
  572. aa_unpack_u32(e, &perm->kill, NULL) &&
  573. aa_unpack_u32(e, &perm->complain, NULL) &&
  574. aa_unpack_u32(e, &perm->prompt, NULL) &&
  575. aa_unpack_u32(e, &perm->audit, NULL) &&
  576. aa_unpack_u32(e, &perm->quiet, NULL) &&
  577. aa_unpack_u32(e, &perm->hide, NULL) &&
  578. aa_unpack_u32(e, &perm->xindex, NULL) &&
  579. aa_unpack_u32(e, &perm->tag, NULL) &&
  580. aa_unpack_u32(e, &perm->label, NULL);
  581. }
  582. static ssize_t unpack_perms_table(struct aa_ext *e, struct aa_perms **perms)
  583. {
  584. void *pos = e->pos;
  585. u16 size = 0;
  586. AA_BUG(!perms);
  587. /*
  588. * policy perms are optional, in which case perms are embedded
  589. * in the dfa accept table
  590. */
  591. if (aa_unpack_nameX(e, AA_STRUCT, "perms")) {
  592. int i;
  593. u32 version;
  594. if (!aa_unpack_u32(e, &version, "version"))
  595. goto fail_reset;
  596. if (!aa_unpack_array(e, NULL, &size))
  597. goto fail_reset;
  598. *perms = kcalloc(size, sizeof(struct aa_perms), GFP_KERNEL);
  599. if (!*perms)
  600. goto fail_reset;
  601. for (i = 0; i < size; i++) {
  602. if (!unpack_perm(e, version, &(*perms)[i]))
  603. goto fail;
  604. }
  605. if (!aa_unpack_nameX(e, AA_ARRAYEND, NULL))
  606. goto fail;
  607. if (!aa_unpack_nameX(e, AA_STRUCTEND, NULL))
  608. goto fail;
  609. } else
  610. *perms = NULL;
  611. return size;
  612. fail:
  613. kfree(*perms);
  614. fail_reset:
  615. e->pos = pos;
  616. return -EPROTO;
  617. }
  618. static int unpack_pdb(struct aa_ext *e, struct aa_policydb **policy,
  619. bool required_dfa, bool required_trans,
  620. const char **info)
  621. {
  622. struct aa_policydb *pdb;
  623. void *pos = e->pos;
  624. int i, flags, error = -EPROTO;
  625. ssize_t size;
  626. pdb = aa_alloc_pdb(GFP_KERNEL);
  627. if (!pdb)
  628. return -ENOMEM;
  629. size = unpack_perms_table(e, &pdb->perms);
  630. if (size < 0) {
  631. error = size;
  632. pdb->perms = NULL;
  633. *info = "failed to unpack - perms";
  634. goto fail;
  635. }
  636. pdb->size = size;
  637. if (pdb->perms) {
  638. /* perms table present accept is index */
  639. flags = TO_ACCEPT1_FLAG(YYTD_DATA32);
  640. } else {
  641. /* packed perms in accept1 and accept2 */
  642. flags = TO_ACCEPT1_FLAG(YYTD_DATA32) |
  643. TO_ACCEPT2_FLAG(YYTD_DATA32);
  644. }
  645. pdb->dfa = unpack_dfa(e, flags);
  646. if (IS_ERR(pdb->dfa)) {
  647. error = PTR_ERR(pdb->dfa);
  648. pdb->dfa = NULL;
  649. *info = "failed to unpack - dfa";
  650. goto fail;
  651. } else if (!pdb->dfa) {
  652. if (required_dfa) {
  653. *info = "missing required dfa";
  654. goto fail;
  655. }
  656. } else {
  657. /*
  658. * only unpack the following if a dfa is present
  659. *
  660. * sadly start was given different names for file and policydb
  661. * but since it is optional we can try both
  662. */
  663. if (!aa_unpack_u32(e, &pdb->start[0], "start"))
  664. /* default start state */
  665. pdb->start[0] = DFA_START;
  666. if (!aa_unpack_u32(e, &pdb->start[AA_CLASS_FILE], "dfa_start")) {
  667. /* default start state for xmatch and file dfa */
  668. pdb->start[AA_CLASS_FILE] = DFA_START;
  669. } /* setup class index */
  670. for (i = AA_CLASS_FILE + 1; i <= AA_CLASS_LAST; i++) {
  671. pdb->start[i] = aa_dfa_next(pdb->dfa, pdb->start[0],
  672. i);
  673. }
  674. }
  675. /*
  676. * Unfortunately due to a bug in earlier userspaces, a
  677. * transition table may be present even when the dfa is
  678. * not. For compatibility reasons unpack and discard.
  679. */
  680. if (!unpack_trans_table(e, &pdb->trans) && required_trans) {
  681. *info = "failed to unpack profile transition table";
  682. goto fail;
  683. }
  684. if (!pdb->dfa && pdb->trans.table)
  685. aa_free_str_table(&pdb->trans);
  686. /* TODO: move compat mapping here, requires dfa merging first */
  687. /* TODO: move verify here, it has to be done after compat mappings */
  688. *policy = pdb;
  689. return 0;
  690. fail:
  691. aa_put_pdb(pdb);
  692. e->pos = pos;
  693. return error;
  694. }
  695. static u32 strhash(const void *data, u32 len, u32 seed)
  696. {
  697. const char * const *key = data;
  698. return jhash(*key, strlen(*key), seed);
  699. }
  700. static int datacmp(struct rhashtable_compare_arg *arg, const void *obj)
  701. {
  702. const struct aa_data *data = obj;
  703. const char * const *key = arg->key;
  704. return strcmp(data->key, *key);
  705. }
  706. /**
  707. * unpack_profile - unpack a serialized profile
  708. * @e: serialized data extent information (NOT NULL)
  709. * @ns_name: pointer of newly allocated copy of %NULL in case of error
  710. *
  711. * NOTE: unpack profile sets audit struct if there is a failure
  712. */
  713. static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name)
  714. {
  715. struct aa_ruleset *rules;
  716. struct aa_profile *profile = NULL;
  717. const char *tmpname, *tmpns = NULL, *name = NULL;
  718. const char *info = "failed to unpack profile";
  719. size_t ns_len;
  720. struct rhashtable_params params = { 0 };
  721. char *key = NULL, *disconnected = NULL;
  722. struct aa_data *data;
  723. int error = -EPROTO;
  724. kernel_cap_t tmpcap;
  725. u32 tmp;
  726. *ns_name = NULL;
  727. /* check that we have the right struct being passed */
  728. if (!aa_unpack_nameX(e, AA_STRUCT, "profile"))
  729. goto fail;
  730. if (!aa_unpack_str(e, &name, NULL))
  731. goto fail;
  732. if (*name == '\0')
  733. goto fail;
  734. tmpname = aa_splitn_fqname(name, strlen(name), &tmpns, &ns_len);
  735. if (tmpns) {
  736. if (!tmpname) {
  737. info = "empty profile name";
  738. goto fail;
  739. }
  740. *ns_name = kstrndup(tmpns, ns_len, GFP_KERNEL);
  741. if (!*ns_name) {
  742. info = "out of memory";
  743. error = -ENOMEM;
  744. goto fail;
  745. }
  746. name = tmpname;
  747. }
  748. profile = aa_alloc_profile(name, NULL, GFP_KERNEL);
  749. if (!profile) {
  750. info = "out of memory";
  751. error = -ENOMEM;
  752. goto fail;
  753. }
  754. rules = list_first_entry(&profile->rules, typeof(*rules), list);
  755. /* profile renaming is optional */
  756. (void) aa_unpack_str(e, &profile->rename, "rename");
  757. /* attachment string is optional */
  758. (void) aa_unpack_str(e, &profile->attach.xmatch_str, "attach");
  759. /* xmatch is optional and may be NULL */
  760. error = unpack_pdb(e, &profile->attach.xmatch, false, false, &info);
  761. if (error) {
  762. info = "bad xmatch";
  763. goto fail;
  764. }
  765. /* neither xmatch_len not xmatch_perms are optional if xmatch is set */
  766. if (profile->attach.xmatch->dfa) {
  767. if (!aa_unpack_u32(e, &tmp, NULL)) {
  768. info = "missing xmatch len";
  769. goto fail;
  770. }
  771. profile->attach.xmatch_len = tmp;
  772. profile->attach.xmatch->start[AA_CLASS_XMATCH] = DFA_START;
  773. if (!profile->attach.xmatch->perms) {
  774. error = aa_compat_map_xmatch(profile->attach.xmatch);
  775. if (error) {
  776. info = "failed to convert xmatch permission table";
  777. goto fail;
  778. }
  779. }
  780. }
  781. /* disconnected attachment string is optional */
  782. (void) aa_unpack_strdup(e, &disconnected, "disconnected");
  783. profile->disconnected = disconnected;
  784. /* per profile debug flags (complain, audit) */
  785. if (!aa_unpack_nameX(e, AA_STRUCT, "flags")) {
  786. info = "profile missing flags";
  787. goto fail;
  788. }
  789. info = "failed to unpack profile flags";
  790. if (!aa_unpack_u32(e, &tmp, NULL))
  791. goto fail;
  792. if (tmp & PACKED_FLAG_HAT)
  793. profile->label.flags |= FLAG_HAT;
  794. if (tmp & PACKED_FLAG_DEBUG1)
  795. profile->label.flags |= FLAG_DEBUG1;
  796. if (tmp & PACKED_FLAG_DEBUG2)
  797. profile->label.flags |= FLAG_DEBUG2;
  798. if (!aa_unpack_u32(e, &tmp, NULL))
  799. goto fail;
  800. if (tmp == PACKED_MODE_COMPLAIN || (e->version & FORCE_COMPLAIN_FLAG)) {
  801. profile->mode = APPARMOR_COMPLAIN;
  802. } else if (tmp == PACKED_MODE_ENFORCE) {
  803. profile->mode = APPARMOR_ENFORCE;
  804. } else if (tmp == PACKED_MODE_KILL) {
  805. profile->mode = APPARMOR_KILL;
  806. } else if (tmp == PACKED_MODE_UNCONFINED) {
  807. profile->mode = APPARMOR_UNCONFINED;
  808. profile->label.flags |= FLAG_UNCONFINED;
  809. } else if (tmp == PACKED_MODE_USER) {
  810. profile->mode = APPARMOR_USER;
  811. } else {
  812. goto fail;
  813. }
  814. if (!aa_unpack_u32(e, &tmp, NULL))
  815. goto fail;
  816. if (tmp)
  817. profile->audit = AUDIT_ALL;
  818. if (!aa_unpack_nameX(e, AA_STRUCTEND, NULL))
  819. goto fail;
  820. /* path_flags is optional */
  821. if (aa_unpack_u32(e, &profile->path_flags, "path_flags"))
  822. profile->path_flags |= profile->label.flags &
  823. PATH_MEDIATE_DELETED;
  824. else
  825. /* set a default value if path_flags field is not present */
  826. profile->path_flags = PATH_MEDIATE_DELETED;
  827. info = "failed to unpack profile capabilities";
  828. if (!aa_unpack_cap_low(e, &rules->caps.allow, NULL))
  829. goto fail;
  830. if (!aa_unpack_cap_low(e, &rules->caps.audit, NULL))
  831. goto fail;
  832. if (!aa_unpack_cap_low(e, &rules->caps.quiet, NULL))
  833. goto fail;
  834. if (!aa_unpack_cap_low(e, &tmpcap, NULL))
  835. goto fail;
  836. info = "failed to unpack upper profile capabilities";
  837. if (aa_unpack_nameX(e, AA_STRUCT, "caps64")) {
  838. /* optional upper half of 64 bit caps */
  839. if (!aa_unpack_cap_high(e, &rules->caps.allow, NULL))
  840. goto fail;
  841. if (!aa_unpack_cap_high(e, &rules->caps.audit, NULL))
  842. goto fail;
  843. if (!aa_unpack_cap_high(e, &rules->caps.quiet, NULL))
  844. goto fail;
  845. if (!aa_unpack_cap_high(e, &tmpcap, NULL))
  846. goto fail;
  847. if (!aa_unpack_nameX(e, AA_STRUCTEND, NULL))
  848. goto fail;
  849. }
  850. info = "failed to unpack extended profile capabilities";
  851. if (aa_unpack_nameX(e, AA_STRUCT, "capsx")) {
  852. /* optional extended caps mediation mask */
  853. if (!aa_unpack_cap_low(e, &rules->caps.extended, NULL))
  854. goto fail;
  855. if (!aa_unpack_cap_high(e, &rules->caps.extended, NULL))
  856. goto fail;
  857. if (!aa_unpack_nameX(e, AA_STRUCTEND, NULL))
  858. goto fail;
  859. }
  860. if (!unpack_xattrs(e, profile)) {
  861. info = "failed to unpack profile xattrs";
  862. goto fail;
  863. }
  864. if (!unpack_rlimits(e, rules)) {
  865. info = "failed to unpack profile rlimits";
  866. goto fail;
  867. }
  868. if (!unpack_secmark(e, rules)) {
  869. info = "failed to unpack profile secmark rules";
  870. goto fail;
  871. }
  872. if (aa_unpack_nameX(e, AA_STRUCT, "policydb")) {
  873. /* generic policy dfa - optional and may be NULL */
  874. info = "failed to unpack policydb";
  875. error = unpack_pdb(e, &rules->policy, true, false,
  876. &info);
  877. if (error)
  878. goto fail;
  879. /* Fixup: drop when we get rid of start array */
  880. if (aa_dfa_next(rules->policy->dfa, rules->policy->start[0],
  881. AA_CLASS_FILE))
  882. rules->policy->start[AA_CLASS_FILE] =
  883. aa_dfa_next(rules->policy->dfa,
  884. rules->policy->start[0],
  885. AA_CLASS_FILE);
  886. if (!aa_unpack_nameX(e, AA_STRUCTEND, NULL))
  887. goto fail;
  888. if (!rules->policy->perms) {
  889. error = aa_compat_map_policy(rules->policy,
  890. e->version);
  891. if (error) {
  892. info = "failed to remap policydb permission table";
  893. goto fail;
  894. }
  895. }
  896. } else {
  897. rules->policy = aa_get_pdb(nullpdb);
  898. }
  899. /* get file rules */
  900. error = unpack_pdb(e, &rules->file, false, true, &info);
  901. if (error) {
  902. goto fail;
  903. } else if (rules->file->dfa) {
  904. if (!rules->file->perms) {
  905. error = aa_compat_map_file(rules->file);
  906. if (error) {
  907. info = "failed to remap file permission table";
  908. goto fail;
  909. }
  910. }
  911. } else if (rules->policy->dfa &&
  912. rules->policy->start[AA_CLASS_FILE]) {
  913. aa_put_pdb(rules->file);
  914. rules->file = aa_get_pdb(rules->policy);
  915. } else {
  916. aa_put_pdb(rules->file);
  917. rules->file = aa_get_pdb(nullpdb);
  918. }
  919. error = -EPROTO;
  920. if (aa_unpack_nameX(e, AA_STRUCT, "data")) {
  921. info = "out of memory";
  922. profile->data = kzalloc(sizeof(*profile->data), GFP_KERNEL);
  923. if (!profile->data) {
  924. error = -ENOMEM;
  925. goto fail;
  926. }
  927. params.nelem_hint = 3;
  928. params.key_len = sizeof(void *);
  929. params.key_offset = offsetof(struct aa_data, key);
  930. params.head_offset = offsetof(struct aa_data, head);
  931. params.hashfn = strhash;
  932. params.obj_cmpfn = datacmp;
  933. if (rhashtable_init(profile->data, &params)) {
  934. info = "failed to init key, value hash table";
  935. goto fail;
  936. }
  937. while (aa_unpack_strdup(e, &key, NULL)) {
  938. data = kzalloc(sizeof(*data), GFP_KERNEL);
  939. if (!data) {
  940. kfree_sensitive(key);
  941. error = -ENOMEM;
  942. goto fail;
  943. }
  944. data->key = key;
  945. data->size = aa_unpack_blob(e, &data->data, NULL);
  946. data->data = kvmemdup(data->data, data->size, GFP_KERNEL);
  947. if (data->size && !data->data) {
  948. kfree_sensitive(data->key);
  949. kfree_sensitive(data);
  950. error = -ENOMEM;
  951. goto fail;
  952. }
  953. if (rhashtable_insert_fast(profile->data, &data->head,
  954. profile->data->p)) {
  955. kvfree_sensitive(data->data, data->size);
  956. kfree_sensitive(data->key);
  957. kfree_sensitive(data);
  958. info = "failed to insert data to table";
  959. goto fail;
  960. }
  961. }
  962. if (!aa_unpack_nameX(e, AA_STRUCTEND, NULL)) {
  963. info = "failed to unpack end of key, value data table";
  964. goto fail;
  965. }
  966. }
  967. if (!aa_unpack_nameX(e, AA_STRUCTEND, NULL)) {
  968. info = "failed to unpack end of profile";
  969. goto fail;
  970. }
  971. return profile;
  972. fail:
  973. if (error == 0)
  974. /* default error covers most cases */
  975. error = -EPROTO;
  976. if (*ns_name) {
  977. kfree(*ns_name);
  978. *ns_name = NULL;
  979. }
  980. if (profile)
  981. name = NULL;
  982. else if (!name)
  983. name = "unknown";
  984. audit_iface(profile, NULL, name, info, e, error);
  985. aa_free_profile(profile);
  986. return ERR_PTR(error);
  987. }
  988. /**
  989. * verify_header - unpack serialized stream header
  990. * @e: serialized data read head (NOT NULL)
  991. * @required: whether the header is required or optional
  992. * @ns: Returns - namespace if one is specified else NULL (NOT NULL)
  993. *
  994. * Returns: error or 0 if header is good
  995. */
  996. static int verify_header(struct aa_ext *e, int required, const char **ns)
  997. {
  998. int error = -EPROTONOSUPPORT;
  999. const char *name = NULL;
  1000. *ns = NULL;
  1001. /* get the interface version */
  1002. if (!aa_unpack_u32(e, &e->version, "version")) {
  1003. if (required) {
  1004. audit_iface(NULL, NULL, NULL, "invalid profile format",
  1005. e, error);
  1006. return error;
  1007. }
  1008. }
  1009. /* Check that the interface version is currently supported.
  1010. * if not specified use previous version
  1011. * Mask off everything that is not kernel abi version
  1012. */
  1013. if (VERSION_LT(e->version, v5) || VERSION_GT(e->version, v9)) {
  1014. audit_iface(NULL, NULL, NULL, "unsupported interface version",
  1015. e, error);
  1016. return error;
  1017. }
  1018. /* read the namespace if present */
  1019. if (aa_unpack_str(e, &name, "namespace")) {
  1020. if (*name == '\0') {
  1021. audit_iface(NULL, NULL, NULL, "invalid namespace name",
  1022. e, error);
  1023. return error;
  1024. }
  1025. if (*ns && strcmp(*ns, name)) {
  1026. audit_iface(NULL, NULL, NULL, "invalid ns change", e,
  1027. error);
  1028. } else if (!*ns) {
  1029. *ns = kstrdup(name, GFP_KERNEL);
  1030. if (!*ns)
  1031. return -ENOMEM;
  1032. }
  1033. }
  1034. return 0;
  1035. }
  1036. /**
  1037. * verify_dfa_accept_index - verify accept indexes are in range of perms table
  1038. * @dfa: the dfa to check accept indexes are in range
  1039. * @table_size: the permission table size the indexes should be within
  1040. */
  1041. static bool verify_dfa_accept_index(struct aa_dfa *dfa, int table_size)
  1042. {
  1043. int i;
  1044. for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
  1045. if (ACCEPT_TABLE(dfa)[i] >= table_size)
  1046. return false;
  1047. }
  1048. return true;
  1049. }
  1050. static bool verify_perm(struct aa_perms *perm)
  1051. {
  1052. /* TODO: allow option to just force the perms into a valid state */
  1053. if (perm->allow & perm->deny)
  1054. return false;
  1055. if (perm->subtree & ~perm->allow)
  1056. return false;
  1057. if (perm->cond & (perm->allow | perm->deny))
  1058. return false;
  1059. if (perm->kill & perm->allow)
  1060. return false;
  1061. if (perm->complain & (perm->allow | perm->deny))
  1062. return false;
  1063. if (perm->prompt & (perm->allow | perm->deny))
  1064. return false;
  1065. if (perm->complain & perm->prompt)
  1066. return false;
  1067. if (perm->hide & perm->allow)
  1068. return false;
  1069. return true;
  1070. }
  1071. static bool verify_perms(struct aa_policydb *pdb)
  1072. {
  1073. int i;
  1074. for (i = 0; i < pdb->size; i++) {
  1075. if (!verify_perm(&pdb->perms[i]))
  1076. return false;
  1077. /* verify indexes into str table */
  1078. if ((pdb->perms[i].xindex & AA_X_TYPE_MASK) == AA_X_TABLE &&
  1079. (pdb->perms[i].xindex & AA_X_INDEX_MASK) >= pdb->trans.size)
  1080. return false;
  1081. if (pdb->perms[i].tag && pdb->perms[i].tag >= pdb->trans.size)
  1082. return false;
  1083. if (pdb->perms[i].label &&
  1084. pdb->perms[i].label >= pdb->trans.size)
  1085. return false;
  1086. }
  1087. return true;
  1088. }
  1089. /**
  1090. * verify_profile - Do post unpack analysis to verify profile consistency
  1091. * @profile: profile to verify (NOT NULL)
  1092. *
  1093. * Returns: 0 if passes verification else error
  1094. *
  1095. * This verification is post any unpack mapping or changes
  1096. */
  1097. static int verify_profile(struct aa_profile *profile)
  1098. {
  1099. struct aa_ruleset *rules = list_first_entry(&profile->rules,
  1100. typeof(*rules), list);
  1101. if (!rules)
  1102. return 0;
  1103. if (rules->file->dfa && !verify_dfa_accept_index(rules->file->dfa,
  1104. rules->file->size)) {
  1105. audit_iface(profile, NULL, NULL,
  1106. "Unpack: file Invalid named transition", NULL,
  1107. -EPROTO);
  1108. return -EPROTO;
  1109. }
  1110. if (rules->policy->dfa &&
  1111. !verify_dfa_accept_index(rules->policy->dfa, rules->policy->size)) {
  1112. audit_iface(profile, NULL, NULL,
  1113. "Unpack: policy Invalid named transition", NULL,
  1114. -EPROTO);
  1115. return -EPROTO;
  1116. }
  1117. if (!verify_perms(rules->file)) {
  1118. audit_iface(profile, NULL, NULL,
  1119. "Unpack: Invalid perm index", NULL, -EPROTO);
  1120. return -EPROTO;
  1121. }
  1122. if (!verify_perms(rules->policy)) {
  1123. audit_iface(profile, NULL, NULL,
  1124. "Unpack: Invalid perm index", NULL, -EPROTO);
  1125. return -EPROTO;
  1126. }
  1127. if (!verify_perms(profile->attach.xmatch)) {
  1128. audit_iface(profile, NULL, NULL,
  1129. "Unpack: Invalid perm index", NULL, -EPROTO);
  1130. return -EPROTO;
  1131. }
  1132. return 0;
  1133. }
  1134. void aa_load_ent_free(struct aa_load_ent *ent)
  1135. {
  1136. if (ent) {
  1137. aa_put_profile(ent->rename);
  1138. aa_put_profile(ent->old);
  1139. aa_put_profile(ent->new);
  1140. kfree(ent->ns_name);
  1141. kfree_sensitive(ent);
  1142. }
  1143. }
  1144. struct aa_load_ent *aa_load_ent_alloc(void)
  1145. {
  1146. struct aa_load_ent *ent = kzalloc(sizeof(*ent), GFP_KERNEL);
  1147. if (ent)
  1148. INIT_LIST_HEAD(&ent->list);
  1149. return ent;
  1150. }
  1151. static int compress_zstd(const char *src, size_t slen, char **dst, size_t *dlen)
  1152. {
  1153. #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
  1154. const zstd_parameters params =
  1155. zstd_get_params(aa_g_rawdata_compression_level, slen);
  1156. const size_t wksp_len = zstd_cctx_workspace_bound(&params.cParams);
  1157. void *wksp = NULL;
  1158. zstd_cctx *ctx = NULL;
  1159. size_t out_len = zstd_compress_bound(slen);
  1160. void *out = NULL;
  1161. int ret = 0;
  1162. out = kvzalloc(out_len, GFP_KERNEL);
  1163. if (!out) {
  1164. ret = -ENOMEM;
  1165. goto cleanup;
  1166. }
  1167. wksp = kvzalloc(wksp_len, GFP_KERNEL);
  1168. if (!wksp) {
  1169. ret = -ENOMEM;
  1170. goto cleanup;
  1171. }
  1172. ctx = zstd_init_cctx(wksp, wksp_len);
  1173. if (!ctx) {
  1174. ret = -EINVAL;
  1175. goto cleanup;
  1176. }
  1177. out_len = zstd_compress_cctx(ctx, out, out_len, src, slen, &params);
  1178. if (zstd_is_error(out_len) || out_len >= slen) {
  1179. ret = -EINVAL;
  1180. goto cleanup;
  1181. }
  1182. if (is_vmalloc_addr(out)) {
  1183. *dst = kvzalloc(out_len, GFP_KERNEL);
  1184. if (*dst) {
  1185. memcpy(*dst, out, out_len);
  1186. kvfree(out);
  1187. out = NULL;
  1188. }
  1189. } else {
  1190. /*
  1191. * If the staging buffer was kmalloc'd, then using krealloc is
  1192. * probably going to be faster. The destination buffer will
  1193. * always be smaller, so it's just shrunk, avoiding a memcpy
  1194. */
  1195. *dst = krealloc(out, out_len, GFP_KERNEL);
  1196. }
  1197. if (!*dst) {
  1198. ret = -ENOMEM;
  1199. goto cleanup;
  1200. }
  1201. *dlen = out_len;
  1202. cleanup:
  1203. if (ret) {
  1204. kvfree(out);
  1205. *dst = NULL;
  1206. }
  1207. kvfree(wksp);
  1208. return ret;
  1209. #else
  1210. *dlen = slen;
  1211. return 0;
  1212. #endif
  1213. }
  1214. static int compress_loaddata(struct aa_loaddata *data)
  1215. {
  1216. AA_BUG(data->compressed_size > 0);
  1217. /*
  1218. * Shortcut the no compression case, else we increase the amount of
  1219. * storage required by a small amount
  1220. */
  1221. if (aa_g_rawdata_compression_level != 0) {
  1222. void *udata = data->data;
  1223. int error = compress_zstd(udata, data->size, &data->data,
  1224. &data->compressed_size);
  1225. if (error) {
  1226. data->compressed_size = data->size;
  1227. return error;
  1228. }
  1229. if (udata != data->data)
  1230. kvfree(udata);
  1231. } else
  1232. data->compressed_size = data->size;
  1233. return 0;
  1234. }
  1235. /**
  1236. * aa_unpack - unpack packed binary profile(s) data loaded from user space
  1237. * @udata: user data copied to kmem (NOT NULL)
  1238. * @lh: list to place unpacked profiles in a aa_repl_ws
  1239. * @ns: Returns namespace profile is in if specified else NULL (NOT NULL)
  1240. *
  1241. * Unpack user data and return refcounted allocated profile(s) stored in
  1242. * @lh in order of discovery, with the list chain stored in base.list
  1243. * or error
  1244. *
  1245. * Returns: profile(s) on @lh else error pointer if fails to unpack
  1246. */
  1247. int aa_unpack(struct aa_loaddata *udata, struct list_head *lh,
  1248. const char **ns)
  1249. {
  1250. struct aa_load_ent *tmp, *ent;
  1251. struct aa_profile *profile = NULL;
  1252. char *ns_name = NULL;
  1253. int error;
  1254. struct aa_ext e = {
  1255. .start = udata->data,
  1256. .end = udata->data + udata->size,
  1257. .pos = udata->data,
  1258. };
  1259. *ns = NULL;
  1260. while (e.pos < e.end) {
  1261. void *start;
  1262. error = verify_header(&e, e.pos == e.start, ns);
  1263. if (error)
  1264. goto fail;
  1265. start = e.pos;
  1266. profile = unpack_profile(&e, &ns_name);
  1267. if (IS_ERR(profile)) {
  1268. error = PTR_ERR(profile);
  1269. goto fail;
  1270. }
  1271. error = verify_profile(profile);
  1272. if (error)
  1273. goto fail_profile;
  1274. if (aa_g_hash_policy)
  1275. error = aa_calc_profile_hash(profile, e.version, start,
  1276. e.pos - start);
  1277. if (error)
  1278. goto fail_profile;
  1279. ent = aa_load_ent_alloc();
  1280. if (!ent) {
  1281. error = -ENOMEM;
  1282. goto fail_profile;
  1283. }
  1284. ent->new = profile;
  1285. ent->ns_name = ns_name;
  1286. ns_name = NULL;
  1287. list_add_tail(&ent->list, lh);
  1288. }
  1289. udata->abi = e.version & K_ABI_MASK;
  1290. if (aa_g_hash_policy) {
  1291. udata->hash = aa_calc_hash(udata->data, udata->size);
  1292. if (IS_ERR(udata->hash)) {
  1293. error = PTR_ERR(udata->hash);
  1294. udata->hash = NULL;
  1295. goto fail;
  1296. }
  1297. }
  1298. if (aa_g_export_binary) {
  1299. error = compress_loaddata(udata);
  1300. if (error)
  1301. goto fail;
  1302. }
  1303. return 0;
  1304. fail_profile:
  1305. kfree(ns_name);
  1306. aa_put_profile(profile);
  1307. fail:
  1308. list_for_each_entry_safe(ent, tmp, lh, list) {
  1309. list_del_init(&ent->list);
  1310. aa_load_ent_free(ent);
  1311. }
  1312. return error;
  1313. }