policy_unpack.c 19 KB

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