policy_unpack.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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 look in Documentation/security/apparmor.txt
  17. * All policy is validated before it is used.
  18. */
  19. #include <asm/unaligned.h>
  20. #include <linux/ctype.h>
  21. #include <linux/errno.h>
  22. #include "include/apparmor.h"
  23. #include "include/audit.h"
  24. #include "include/context.h"
  25. #include "include/crypto.h"
  26. #include "include/match.h"
  27. #include "include/policy.h"
  28. #include "include/policy_unpack.h"
  29. #define K_ABI_MASK 0x3ff
  30. #define FORCE_COMPLAIN_FLAG 0x800
  31. #define VERSION_LT(X, Y) (((X) & K_ABI_MASK) < ((Y) & K_ABI_MASK))
  32. #define VERSION_GT(X, Y) (((X) & K_ABI_MASK) > ((Y) & K_ABI_MASK))
  33. #define v5 5 /* base version */
  34. #define v6 6 /* per entry policydb mediation check */
  35. #define v7 7 /* full network masking */
  36. /*
  37. * The AppArmor interface treats data as a type byte followed by the
  38. * actual data. The interface has the notion of a a named entry
  39. * which has a name (AA_NAME typecode followed by name string) followed by
  40. * the entries typecode and data. Named types allow for optional
  41. * elements and extensions to be added and tested for without breaking
  42. * backwards compatibility.
  43. */
  44. enum aa_code {
  45. AA_U8,
  46. AA_U16,
  47. AA_U32,
  48. AA_U64,
  49. AA_NAME, /* same as string except it is items name */
  50. AA_STRING,
  51. AA_BLOB,
  52. AA_STRUCT,
  53. AA_STRUCTEND,
  54. AA_LIST,
  55. AA_LISTEND,
  56. AA_ARRAY,
  57. AA_ARRAYEND,
  58. };
  59. /*
  60. * aa_ext is the read of the buffer containing the serialized profile. The
  61. * data is copied into a kernel buffer in apparmorfs and then handed off to
  62. * the unpack routines.
  63. */
  64. struct aa_ext {
  65. void *start;
  66. void *end;
  67. void *pos; /* pointer to current position in the buffer */
  68. u32 version;
  69. };
  70. /* audit callback for unpack fields */
  71. static void audit_cb(struct audit_buffer *ab, void *va)
  72. {
  73. struct common_audit_data *sa = va;
  74. if (sa->aad->iface.target) {
  75. struct aa_profile *name = sa->aad->iface.target;
  76. audit_log_format(ab, " name=");
  77. audit_log_untrustedstring(ab, name->base.hname);
  78. }
  79. if (sa->aad->iface.pos)
  80. audit_log_format(ab, " offset=%ld", sa->aad->iface.pos);
  81. }
  82. /**
  83. * audit_iface - do audit message for policy unpacking/load/replace/remove
  84. * @new: profile if it has been allocated (MAYBE NULL)
  85. * @name: name of the profile being manipulated (MAYBE NULL)
  86. * @info: any extra info about the failure (MAYBE NULL)
  87. * @e: buffer position info
  88. * @error: error code
  89. *
  90. * Returns: %0 or error
  91. */
  92. static int audit_iface(struct aa_profile *new, const char *name,
  93. const char *info, struct aa_ext *e, int error)
  94. {
  95. struct aa_profile *profile = __aa_current_profile();
  96. struct common_audit_data sa;
  97. struct apparmor_audit_data aad = {0,};
  98. sa.type = LSM_AUDIT_DATA_NONE;
  99. sa.aad = &aad;
  100. if (e)
  101. aad.iface.pos = e->pos - e->start;
  102. aad.iface.target = new;
  103. aad.name = name;
  104. aad.info = info;
  105. aad.error = error;
  106. return aa_audit(AUDIT_APPARMOR_STATUS, profile, GFP_KERNEL, &sa,
  107. audit_cb);
  108. }
  109. void aa_loaddata_kref(struct kref *kref)
  110. {
  111. struct aa_loaddata *d = container_of(kref, struct aa_loaddata, count);
  112. if (d) {
  113. kzfree(d->hash);
  114. kvfree(d);
  115. }
  116. }
  117. /* test if read will be in packed data bounds */
  118. static bool inbounds(struct aa_ext *e, size_t size)
  119. {
  120. return (size <= e->end - e->pos);
  121. }
  122. /**
  123. * aa_u16_chunck - test and do bounds checking for a u16 size based chunk
  124. * @e: serialized data read head (NOT NULL)
  125. * @chunk: start address for chunk of data (NOT NULL)
  126. *
  127. * Returns: the size of chunk found with the read head at the end of the chunk.
  128. */
  129. static size_t unpack_u16_chunk(struct aa_ext *e, char **chunk)
  130. {
  131. size_t size = 0;
  132. if (!inbounds(e, sizeof(u16)))
  133. return 0;
  134. size = le16_to_cpu(get_unaligned((u16 *) e->pos));
  135. e->pos += sizeof(u16);
  136. if (!inbounds(e, size))
  137. return 0;
  138. *chunk = e->pos;
  139. e->pos += size;
  140. return size;
  141. }
  142. /* unpack control byte */
  143. static bool unpack_X(struct aa_ext *e, enum aa_code code)
  144. {
  145. if (!inbounds(e, 1))
  146. return 0;
  147. if (*(u8 *) e->pos != code)
  148. return 0;
  149. e->pos++;
  150. return 1;
  151. }
  152. /**
  153. * unpack_nameX - check is the next element is of type X with a name of @name
  154. * @e: serialized data extent information (NOT NULL)
  155. * @code: type code
  156. * @name: name to match to the serialized element. (MAYBE NULL)
  157. *
  158. * check that the next serialized data element is of type X and has a tag
  159. * name @name. If @name is specified then there must be a matching
  160. * name element in the stream. If @name is NULL any name element will be
  161. * skipped and only the typecode will be tested.
  162. *
  163. * Returns 1 on success (both type code and name tests match) and the read
  164. * head is advanced past the headers
  165. *
  166. * Returns: 0 if either match fails, the read head does not move
  167. */
  168. static bool unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name)
  169. {
  170. /*
  171. * May need to reset pos if name or type doesn't match
  172. */
  173. void *pos = e->pos;
  174. /*
  175. * Check for presence of a tagname, and if present name size
  176. * AA_NAME tag value is a u16.
  177. */
  178. if (unpack_X(e, AA_NAME)) {
  179. char *tag = NULL;
  180. size_t size = unpack_u16_chunk(e, &tag);
  181. /* if a name is specified it must match. otherwise skip tag */
  182. if (name && (!size || strcmp(name, tag)))
  183. goto fail;
  184. } else if (name) {
  185. /* if a name is specified and there is no name tag fail */
  186. goto fail;
  187. }
  188. /* now check if type code matches */
  189. if (unpack_X(e, code))
  190. return 1;
  191. fail:
  192. e->pos = pos;
  193. return 0;
  194. }
  195. static bool unpack_u32(struct aa_ext *e, u32 *data, const char *name)
  196. {
  197. if (unpack_nameX(e, AA_U32, name)) {
  198. if (!inbounds(e, sizeof(u32)))
  199. return 0;
  200. if (data)
  201. *data = le32_to_cpu(get_unaligned((u32 *) e->pos));
  202. e->pos += sizeof(u32);
  203. return 1;
  204. }
  205. return 0;
  206. }
  207. static bool unpack_u64(struct aa_ext *e, u64 *data, const char *name)
  208. {
  209. if (unpack_nameX(e, AA_U64, name)) {
  210. if (!inbounds(e, sizeof(u64)))
  211. return 0;
  212. if (data)
  213. *data = le64_to_cpu(get_unaligned((u64 *) e->pos));
  214. e->pos += sizeof(u64);
  215. return 1;
  216. }
  217. return 0;
  218. }
  219. static size_t unpack_array(struct aa_ext *e, const char *name)
  220. {
  221. if (unpack_nameX(e, AA_ARRAY, name)) {
  222. int size;
  223. if (!inbounds(e, sizeof(u16)))
  224. return 0;
  225. size = (int)le16_to_cpu(get_unaligned((u16 *) e->pos));
  226. e->pos += sizeof(u16);
  227. return size;
  228. }
  229. return 0;
  230. }
  231. static size_t unpack_blob(struct aa_ext *e, char **blob, const char *name)
  232. {
  233. if (unpack_nameX(e, AA_BLOB, name)) {
  234. u32 size;
  235. if (!inbounds(e, sizeof(u32)))
  236. return 0;
  237. size = le32_to_cpu(get_unaligned((u32 *) e->pos));
  238. e->pos += sizeof(u32);
  239. if (inbounds(e, (size_t) size)) {
  240. *blob = e->pos;
  241. e->pos += size;
  242. return size;
  243. }
  244. }
  245. return 0;
  246. }
  247. static int unpack_str(struct aa_ext *e, const char **string, const char *name)
  248. {
  249. char *src_str;
  250. size_t size = 0;
  251. void *pos = e->pos;
  252. *string = NULL;
  253. if (unpack_nameX(e, AA_STRING, name)) {
  254. size = unpack_u16_chunk(e, &src_str);
  255. if (size) {
  256. /* strings are null terminated, length is size - 1 */
  257. if (src_str[size - 1] != 0)
  258. goto fail;
  259. *string = src_str;
  260. }
  261. }
  262. return size;
  263. fail:
  264. e->pos = pos;
  265. return 0;
  266. }
  267. static int unpack_strdup(struct aa_ext *e, char **string, const char *name)
  268. {
  269. const char *tmp;
  270. void *pos = e->pos;
  271. int res = unpack_str(e, &tmp, name);
  272. *string = NULL;
  273. if (!res)
  274. return 0;
  275. *string = kmemdup(tmp, res, GFP_KERNEL);
  276. if (!*string) {
  277. e->pos = pos;
  278. return 0;
  279. }
  280. return res;
  281. }
  282. #define DFA_VALID_PERM_MASK 0xffffffff
  283. #define DFA_VALID_PERM2_MASK 0xffffffff
  284. /**
  285. * verify_accept - verify the accept tables of a dfa
  286. * @dfa: dfa to verify accept tables of (NOT NULL)
  287. * @flags: flags governing dfa
  288. *
  289. * Returns: 1 if valid accept tables else 0 if error
  290. */
  291. static bool verify_accept(struct aa_dfa *dfa, int flags)
  292. {
  293. int i;
  294. /* verify accept permissions */
  295. for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
  296. int mode = ACCEPT_TABLE(dfa)[i];
  297. if (mode & ~DFA_VALID_PERM_MASK)
  298. return 0;
  299. if (ACCEPT_TABLE2(dfa)[i] & ~DFA_VALID_PERM2_MASK)
  300. return 0;
  301. }
  302. return 1;
  303. }
  304. /**
  305. * unpack_dfa - unpack a file rule dfa
  306. * @e: serialized data extent information (NOT NULL)
  307. *
  308. * returns dfa or ERR_PTR or NULL if no dfa
  309. */
  310. static struct aa_dfa *unpack_dfa(struct aa_ext *e)
  311. {
  312. char *blob = NULL;
  313. size_t size;
  314. struct aa_dfa *dfa = NULL;
  315. size = unpack_blob(e, &blob, "aadfa");
  316. if (size) {
  317. /*
  318. * The dfa is aligned with in the blob to 8 bytes
  319. * from the beginning of the stream.
  320. * alignment adjust needed by dfa unpack
  321. */
  322. size_t sz = blob - (char *) e->start -
  323. ((e->pos - e->start) & 7);
  324. size_t pad = ALIGN(sz, 8) - sz;
  325. int flags = TO_ACCEPT1_FLAG(YYTD_DATA32) |
  326. TO_ACCEPT2_FLAG(YYTD_DATA32) | DFA_FLAG_VERIFY_STATES;
  327. dfa = aa_dfa_unpack(blob + pad, size - pad, flags);
  328. if (IS_ERR(dfa))
  329. return dfa;
  330. if (!verify_accept(dfa, flags))
  331. goto fail;
  332. }
  333. return dfa;
  334. fail:
  335. aa_put_dfa(dfa);
  336. return ERR_PTR(-EPROTO);
  337. }
  338. /**
  339. * unpack_trans_table - unpack a profile transition table
  340. * @e: serialized data extent information (NOT NULL)
  341. * @profile: profile to add the accept table to (NOT NULL)
  342. *
  343. * Returns: 1 if table successfully unpacked
  344. */
  345. static bool unpack_trans_table(struct aa_ext *e, struct aa_profile *profile)
  346. {
  347. void *pos = e->pos;
  348. /* exec table is optional */
  349. if (unpack_nameX(e, AA_STRUCT, "xtable")) {
  350. int i, size;
  351. size = unpack_array(e, NULL);
  352. /* currently 4 exec bits and entries 0-3 are reserved iupcx */
  353. if (size > 16 - 4)
  354. goto fail;
  355. profile->file.trans.table = kzalloc(sizeof(char *) * size,
  356. GFP_KERNEL);
  357. if (!profile->file.trans.table)
  358. goto fail;
  359. profile->file.trans.size = size;
  360. for (i = 0; i < size; i++) {
  361. char *str;
  362. int c, j, size2 = unpack_strdup(e, &str, NULL);
  363. /* unpack_strdup verifies that the last character is
  364. * null termination byte.
  365. */
  366. if (!size2)
  367. goto fail;
  368. profile->file.trans.table[i] = str;
  369. /* verify that name doesn't start with space */
  370. if (isspace(*str))
  371. goto fail;
  372. /* count internal # of internal \0 */
  373. for (c = j = 0; j < size2 - 2; j++) {
  374. if (!str[j])
  375. c++;
  376. }
  377. if (*str == ':') {
  378. /* beginning with : requires an embedded \0,
  379. * verify that exactly 1 internal \0 exists
  380. * trailing \0 already verified by unpack_strdup
  381. */
  382. if (c != 1)
  383. goto fail;
  384. /* first character after : must be valid */
  385. if (!str[1])
  386. goto fail;
  387. } else if (c)
  388. /* fail - all other cases with embedded \0 */
  389. goto fail;
  390. }
  391. if (!unpack_nameX(e, AA_ARRAYEND, NULL))
  392. goto fail;
  393. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  394. goto fail;
  395. }
  396. return 1;
  397. fail:
  398. aa_free_domain_entries(&profile->file.trans);
  399. e->pos = pos;
  400. return 0;
  401. }
  402. static bool unpack_rlimits(struct aa_ext *e, struct aa_profile *profile)
  403. {
  404. void *pos = e->pos;
  405. /* rlimits are optional */
  406. if (unpack_nameX(e, AA_STRUCT, "rlimits")) {
  407. int i, size;
  408. u32 tmp = 0;
  409. if (!unpack_u32(e, &tmp, NULL))
  410. goto fail;
  411. profile->rlimits.mask = tmp;
  412. size = unpack_array(e, NULL);
  413. if (size > RLIM_NLIMITS)
  414. goto fail;
  415. for (i = 0; i < size; i++) {
  416. u64 tmp2 = 0;
  417. int a = aa_map_resource(i);
  418. if (!unpack_u64(e, &tmp2, NULL))
  419. goto fail;
  420. profile->rlimits.limits[a].rlim_max = tmp2;
  421. }
  422. if (!unpack_nameX(e, AA_ARRAYEND, NULL))
  423. goto fail;
  424. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  425. goto fail;
  426. }
  427. return 1;
  428. fail:
  429. e->pos = pos;
  430. return 0;
  431. }
  432. /**
  433. * unpack_profile - unpack a serialized profile
  434. * @e: serialized data extent information (NOT NULL)
  435. *
  436. * NOTE: unpack profile sets audit struct if there is a failure
  437. */
  438. static struct aa_profile *unpack_profile(struct aa_ext *e)
  439. {
  440. struct aa_profile *profile = NULL;
  441. const char *name = NULL;
  442. int i, error = -EPROTO;
  443. kernel_cap_t tmpcap;
  444. u32 tmp;
  445. /* check that we have the right struct being passed */
  446. if (!unpack_nameX(e, AA_STRUCT, "profile"))
  447. goto fail;
  448. if (!unpack_str(e, &name, NULL))
  449. goto fail;
  450. profile = aa_alloc_profile(name, GFP_KERNEL);
  451. if (!profile)
  452. return ERR_PTR(-ENOMEM);
  453. /* profile renaming is optional */
  454. (void) unpack_str(e, &profile->rename, "rename");
  455. /* attachment string is optional */
  456. (void) unpack_str(e, &profile->attach, "attach");
  457. /* xmatch is optional and may be NULL */
  458. profile->xmatch = unpack_dfa(e);
  459. if (IS_ERR(profile->xmatch)) {
  460. error = PTR_ERR(profile->xmatch);
  461. profile->xmatch = NULL;
  462. goto fail;
  463. }
  464. /* xmatch_len is not optional if xmatch is set */
  465. if (profile->xmatch) {
  466. if (!unpack_u32(e, &tmp, NULL))
  467. goto fail;
  468. profile->xmatch_len = tmp;
  469. }
  470. /* per profile debug flags (complain, audit) */
  471. if (!unpack_nameX(e, AA_STRUCT, "flags"))
  472. goto fail;
  473. if (!unpack_u32(e, &tmp, NULL))
  474. goto fail;
  475. if (tmp & PACKED_FLAG_HAT)
  476. profile->flags |= PFLAG_HAT;
  477. if (!unpack_u32(e, &tmp, NULL))
  478. goto fail;
  479. if (tmp == PACKED_MODE_COMPLAIN || (e->version & FORCE_COMPLAIN_FLAG))
  480. profile->mode = APPARMOR_COMPLAIN;
  481. else if (tmp == PACKED_MODE_KILL)
  482. profile->mode = APPARMOR_KILL;
  483. else if (tmp == PACKED_MODE_UNCONFINED)
  484. profile->mode = APPARMOR_UNCONFINED;
  485. if (!unpack_u32(e, &tmp, NULL))
  486. goto fail;
  487. if (tmp)
  488. profile->audit = AUDIT_ALL;
  489. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  490. goto fail;
  491. /* path_flags is optional */
  492. if (unpack_u32(e, &profile->path_flags, "path_flags"))
  493. profile->path_flags |= profile->flags & PFLAG_MEDIATE_DELETED;
  494. else
  495. /* set a default value if path_flags field is not present */
  496. profile->path_flags = PFLAG_MEDIATE_DELETED;
  497. if (!unpack_u32(e, &(profile->caps.allow.cap[0]), NULL))
  498. goto fail;
  499. if (!unpack_u32(e, &(profile->caps.audit.cap[0]), NULL))
  500. goto fail;
  501. if (!unpack_u32(e, &(profile->caps.quiet.cap[0]), NULL))
  502. goto fail;
  503. if (!unpack_u32(e, &tmpcap.cap[0], NULL))
  504. goto fail;
  505. if (unpack_nameX(e, AA_STRUCT, "caps64")) {
  506. /* optional upper half of 64 bit caps */
  507. if (!unpack_u32(e, &(profile->caps.allow.cap[1]), NULL))
  508. goto fail;
  509. if (!unpack_u32(e, &(profile->caps.audit.cap[1]), NULL))
  510. goto fail;
  511. if (!unpack_u32(e, &(profile->caps.quiet.cap[1]), NULL))
  512. goto fail;
  513. if (!unpack_u32(e, &(tmpcap.cap[1]), NULL))
  514. goto fail;
  515. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  516. goto fail;
  517. }
  518. if (unpack_nameX(e, AA_STRUCT, "capsx")) {
  519. /* optional extended caps mediation mask */
  520. if (!unpack_u32(e, &(profile->caps.extended.cap[0]), NULL))
  521. goto fail;
  522. if (!unpack_u32(e, &(profile->caps.extended.cap[1]), NULL))
  523. goto fail;
  524. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  525. goto fail;
  526. }
  527. if (!unpack_rlimits(e, profile))
  528. goto fail;
  529. if (unpack_nameX(e, AA_STRUCT, "policydb")) {
  530. /* generic policy dfa - optional and may be NULL */
  531. profile->policy.dfa = unpack_dfa(e);
  532. if (IS_ERR(profile->policy.dfa)) {
  533. error = PTR_ERR(profile->policy.dfa);
  534. profile->policy.dfa = NULL;
  535. goto fail;
  536. } else if (!profile->policy.dfa) {
  537. error = -EPROTO;
  538. goto fail;
  539. }
  540. if (!unpack_u32(e, &profile->policy.start[0], "start"))
  541. /* default start state */
  542. profile->policy.start[0] = DFA_START;
  543. /* setup class index */
  544. for (i = AA_CLASS_FILE; i <= AA_CLASS_LAST; i++) {
  545. profile->policy.start[i] =
  546. aa_dfa_next(profile->policy.dfa,
  547. profile->policy.start[0],
  548. i);
  549. }
  550. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  551. goto fail;
  552. } else
  553. profile->policy.dfa = aa_get_dfa(nulldfa);
  554. /* get file rules */
  555. profile->file.dfa = unpack_dfa(e);
  556. if (IS_ERR(profile->file.dfa)) {
  557. error = PTR_ERR(profile->file.dfa);
  558. profile->file.dfa = NULL;
  559. goto fail;
  560. } else if (profile->file.dfa) {
  561. if (!unpack_u32(e, &profile->file.start, "dfa_start"))
  562. /* default start state */
  563. profile->file.start = DFA_START;
  564. } else if (profile->policy.dfa &&
  565. profile->policy.start[AA_CLASS_FILE]) {
  566. profile->file.dfa = aa_get_dfa(profile->policy.dfa);
  567. profile->file.start = profile->policy.start[AA_CLASS_FILE];
  568. } else
  569. profile->file.dfa = aa_get_dfa(nulldfa);
  570. if (!unpack_trans_table(e, profile))
  571. goto fail;
  572. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  573. goto fail;
  574. return profile;
  575. fail:
  576. if (profile)
  577. name = NULL;
  578. else if (!name)
  579. name = "unknown";
  580. audit_iface(profile, name, "failed to unpack profile", e, error);
  581. aa_free_profile(profile);
  582. return ERR_PTR(error);
  583. }
  584. /**
  585. * verify_head - unpack serialized stream header
  586. * @e: serialized data read head (NOT NULL)
  587. * @required: whether the header is required or optional
  588. * @ns: Returns - namespace if one is specified else NULL (NOT NULL)
  589. *
  590. * Returns: error or 0 if header is good
  591. */
  592. static int verify_header(struct aa_ext *e, int required, const char **ns)
  593. {
  594. int error = -EPROTONOSUPPORT;
  595. const char *name = NULL;
  596. *ns = NULL;
  597. /* get the interface version */
  598. if (!unpack_u32(e, &e->version, "version")) {
  599. if (required) {
  600. audit_iface(NULL, NULL, "invalid profile format",
  601. e, error);
  602. return error;
  603. }
  604. }
  605. /* Check that the interface version is currently supported.
  606. * if not specified use previous version
  607. * Mask off everything that is not kernel abi version
  608. */
  609. if (VERSION_LT(e->version, v5) && VERSION_GT(e->version, v7)) {
  610. audit_iface(NULL, NULL, "unsupported interface version",
  611. e, error);
  612. return error;
  613. }
  614. /* read the namespace if present */
  615. if (unpack_str(e, &name, "namespace")) {
  616. if (*ns && strcmp(*ns, name))
  617. audit_iface(NULL, NULL, "invalid ns change", e, error);
  618. else if (!*ns)
  619. *ns = name;
  620. }
  621. return 0;
  622. }
  623. static bool verify_xindex(int xindex, int table_size)
  624. {
  625. int index, xtype;
  626. xtype = xindex & AA_X_TYPE_MASK;
  627. index = xindex & AA_X_INDEX_MASK;
  628. if (xtype == AA_X_TABLE && index >= table_size)
  629. return 0;
  630. return 1;
  631. }
  632. /* verify dfa xindexes are in range of transition tables */
  633. static bool verify_dfa_xindex(struct aa_dfa *dfa, int table_size)
  634. {
  635. int i;
  636. for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
  637. if (!verify_xindex(dfa_user_xindex(dfa, i), table_size))
  638. return 0;
  639. if (!verify_xindex(dfa_other_xindex(dfa, i), table_size))
  640. return 0;
  641. }
  642. return 1;
  643. }
  644. /**
  645. * verify_profile - Do post unpack analysis to verify profile consistency
  646. * @profile: profile to verify (NOT NULL)
  647. *
  648. * Returns: 0 if passes verification else error
  649. */
  650. static int verify_profile(struct aa_profile *profile)
  651. {
  652. if (profile->file.dfa &&
  653. !verify_dfa_xindex(profile->file.dfa,
  654. profile->file.trans.size)) {
  655. audit_iface(profile, NULL, "Invalid named transition",
  656. NULL, -EPROTO);
  657. return -EPROTO;
  658. }
  659. return 0;
  660. }
  661. void aa_load_ent_free(struct aa_load_ent *ent)
  662. {
  663. if (ent) {
  664. aa_put_profile(ent->rename);
  665. aa_put_profile(ent->old);
  666. aa_put_profile(ent->new);
  667. kzfree(ent);
  668. }
  669. }
  670. struct aa_load_ent *aa_load_ent_alloc(void)
  671. {
  672. struct aa_load_ent *ent = kzalloc(sizeof(*ent), GFP_KERNEL);
  673. if (ent)
  674. INIT_LIST_HEAD(&ent->list);
  675. return ent;
  676. }
  677. /**
  678. * aa_unpack - unpack packed binary profile(s) data loaded from user space
  679. * @udata: user data copied to kmem (NOT NULL)
  680. * @lh: list to place unpacked profiles in a aa_repl_ws
  681. * @ns: Returns namespace profile is in if specified else NULL (NOT NULL)
  682. *
  683. * Unpack user data and return refcounted allocated profile(s) stored in
  684. * @lh in order of discovery, with the list chain stored in base.list
  685. * or error
  686. *
  687. * Returns: profile(s) on @lh else error pointer if fails to unpack
  688. */
  689. int aa_unpack(struct aa_loaddata *udata, struct list_head *lh,
  690. const char **ns)
  691. {
  692. struct aa_load_ent *tmp, *ent;
  693. struct aa_profile *profile = NULL;
  694. int error;
  695. struct aa_ext e = {
  696. .start = udata->data,
  697. .end = udata->data + udata->size,
  698. .pos = udata->data,
  699. };
  700. *ns = NULL;
  701. while (e.pos < e.end) {
  702. void *start;
  703. error = verify_header(&e, e.pos == e.start, ns);
  704. if (error)
  705. goto fail;
  706. start = e.pos;
  707. profile = unpack_profile(&e);
  708. if (IS_ERR(profile)) {
  709. error = PTR_ERR(profile);
  710. goto fail;
  711. }
  712. error = verify_profile(profile);
  713. if (error)
  714. goto fail_profile;
  715. error = aa_calc_profile_hash(profile, e.version, start,
  716. e.pos - start);
  717. if (error)
  718. goto fail_profile;
  719. ent = aa_load_ent_alloc();
  720. if (!ent) {
  721. error = -ENOMEM;
  722. goto fail_profile;
  723. }
  724. ent->new = profile;
  725. list_add_tail(&ent->list, lh);
  726. }
  727. udata->abi = e.version & K_ABI_MASK;
  728. udata->hash = aa_calc_hash(udata->data, udata->size);
  729. if (IS_ERR(udata->hash)) {
  730. error = PTR_ERR(udata->hash);
  731. udata->hash = NULL;
  732. goto fail;
  733. }
  734. return 0;
  735. fail_profile:
  736. aa_put_profile(profile);
  737. fail:
  738. list_for_each_entry_safe(ent, tmp, lh, list) {
  739. list_del_init(&ent->list);
  740. aa_load_ent_free(ent);
  741. }
  742. return error;
  743. }