policy_unpack.c 21 KB

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