policy_unpack.c 17 KB

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