policy_unpack.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. struct apparmor_audit_data aad = {0,};
  91. COMMON_AUDIT_DATA_INIT(&sa, 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. /**
  268. * verify_accept - verify the accept tables of a dfa
  269. * @dfa: dfa to verify accept tables of (NOT NULL)
  270. * @flags: flags governing dfa
  271. *
  272. * Returns: 1 if valid accept tables else 0 if error
  273. */
  274. static bool verify_accept(struct aa_dfa *dfa, int flags)
  275. {
  276. int i;
  277. /* verify accept permissions */
  278. for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
  279. int mode = ACCEPT_TABLE(dfa)[i];
  280. if (mode & ~DFA_VALID_PERM_MASK)
  281. return 0;
  282. if (ACCEPT_TABLE2(dfa)[i] & ~DFA_VALID_PERM2_MASK)
  283. return 0;
  284. }
  285. return 1;
  286. }
  287. /**
  288. * unpack_dfa - unpack a file rule dfa
  289. * @e: serialized data extent information (NOT NULL)
  290. *
  291. * returns dfa or ERR_PTR or NULL if no dfa
  292. */
  293. static struct aa_dfa *unpack_dfa(struct aa_ext *e)
  294. {
  295. char *blob = NULL;
  296. size_t size;
  297. struct aa_dfa *dfa = NULL;
  298. size = unpack_blob(e, &blob, "aadfa");
  299. if (size) {
  300. /*
  301. * The dfa is aligned with in the blob to 8 bytes
  302. * from the beginning of the stream.
  303. */
  304. size_t sz = blob - (char *)e->start;
  305. size_t pad = ALIGN(sz, 8) - sz;
  306. int flags = TO_ACCEPT1_FLAG(YYTD_DATA32) |
  307. TO_ACCEPT2_FLAG(YYTD_DATA32);
  308. if (aa_g_paranoid_load)
  309. flags |= DFA_FLAG_VERIFY_STATES;
  310. dfa = aa_dfa_unpack(blob + pad, size - pad, flags);
  311. if (IS_ERR(dfa))
  312. return dfa;
  313. if (!verify_accept(dfa, flags))
  314. goto fail;
  315. }
  316. return dfa;
  317. fail:
  318. aa_put_dfa(dfa);
  319. return ERR_PTR(-EPROTO);
  320. }
  321. /**
  322. * unpack_trans_table - unpack a profile transition table
  323. * @e: serialized data extent information (NOT NULL)
  324. * @profile: profile to add the accept table to (NOT NULL)
  325. *
  326. * Returns: 1 if table successfully unpacked
  327. */
  328. static bool unpack_trans_table(struct aa_ext *e, struct aa_profile *profile)
  329. {
  330. void *pos = e->pos;
  331. /* exec table is optional */
  332. if (unpack_nameX(e, AA_STRUCT, "xtable")) {
  333. int i, size;
  334. size = unpack_array(e, NULL);
  335. /* currently 4 exec bits and entries 0-3 are reserved iupcx */
  336. if (size > 16 - 4)
  337. goto fail;
  338. profile->file.trans.table = kzalloc(sizeof(char *) * size,
  339. GFP_KERNEL);
  340. if (!profile->file.trans.table)
  341. goto fail;
  342. profile->file.trans.size = size;
  343. for (i = 0; i < size; i++) {
  344. char *str;
  345. int c, j, size2 = unpack_strdup(e, &str, NULL);
  346. /* unpack_strdup verifies that the last character is
  347. * null termination byte.
  348. */
  349. if (!size2)
  350. goto fail;
  351. profile->file.trans.table[i] = str;
  352. /* verify that name doesn't start with space */
  353. if (isspace(*str))
  354. goto fail;
  355. /* count internal # of internal \0 */
  356. for (c = j = 0; j < size2 - 2; j++) {
  357. if (!str[j])
  358. c++;
  359. }
  360. if (*str == ':') {
  361. /* beginning with : requires an embedded \0,
  362. * verify that exactly 1 internal \0 exists
  363. * trailing \0 already verified by unpack_strdup
  364. */
  365. if (c != 1)
  366. goto fail;
  367. /* first character after : must be valid */
  368. if (!str[1])
  369. goto fail;
  370. } else if (c)
  371. /* fail - all other cases with embedded \0 */
  372. goto fail;
  373. }
  374. if (!unpack_nameX(e, AA_ARRAYEND, NULL))
  375. goto fail;
  376. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  377. goto fail;
  378. }
  379. return 1;
  380. fail:
  381. aa_free_domain_entries(&profile->file.trans);
  382. e->pos = pos;
  383. return 0;
  384. }
  385. static bool unpack_rlimits(struct aa_ext *e, struct aa_profile *profile)
  386. {
  387. void *pos = e->pos;
  388. /* rlimits are optional */
  389. if (unpack_nameX(e, AA_STRUCT, "rlimits")) {
  390. int i, size;
  391. u32 tmp = 0;
  392. if (!unpack_u32(e, &tmp, NULL))
  393. goto fail;
  394. profile->rlimits.mask = tmp;
  395. size = unpack_array(e, NULL);
  396. if (size > RLIM_NLIMITS)
  397. goto fail;
  398. for (i = 0; i < size; i++) {
  399. u64 tmp2 = 0;
  400. int a = aa_map_resource(i);
  401. if (!unpack_u64(e, &tmp2, NULL))
  402. goto fail;
  403. profile->rlimits.limits[a].rlim_max = tmp2;
  404. }
  405. if (!unpack_nameX(e, AA_ARRAYEND, NULL))
  406. goto fail;
  407. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  408. goto fail;
  409. }
  410. return 1;
  411. fail:
  412. e->pos = pos;
  413. return 0;
  414. }
  415. /**
  416. * unpack_profile - unpack a serialized profile
  417. * @e: serialized data extent information (NOT NULL)
  418. *
  419. * NOTE: unpack profile sets audit struct if there is a failure
  420. */
  421. static struct aa_profile *unpack_profile(struct aa_ext *e)
  422. {
  423. struct aa_profile *profile = NULL;
  424. const char *name = NULL;
  425. int i, error = -EPROTO;
  426. kernel_cap_t tmpcap;
  427. u32 tmp;
  428. /* check that we have the right struct being passed */
  429. if (!unpack_nameX(e, AA_STRUCT, "profile"))
  430. goto fail;
  431. if (!unpack_str(e, &name, NULL))
  432. goto fail;
  433. profile = aa_alloc_profile(name);
  434. if (!profile)
  435. return ERR_PTR(-ENOMEM);
  436. /* profile renaming is optional */
  437. (void) unpack_str(e, &profile->rename, "rename");
  438. /* xmatch is optional and may be NULL */
  439. profile->xmatch = unpack_dfa(e);
  440. if (IS_ERR(profile->xmatch)) {
  441. error = PTR_ERR(profile->xmatch);
  442. profile->xmatch = NULL;
  443. goto fail;
  444. }
  445. /* xmatch_len is not optional if xmatch is set */
  446. if (profile->xmatch) {
  447. if (!unpack_u32(e, &tmp, NULL))
  448. goto fail;
  449. profile->xmatch_len = tmp;
  450. }
  451. /* per profile debug flags (complain, audit) */
  452. if (!unpack_nameX(e, AA_STRUCT, "flags"))
  453. goto fail;
  454. if (!unpack_u32(e, &tmp, NULL))
  455. goto fail;
  456. if (tmp)
  457. profile->flags |= PFLAG_HAT;
  458. if (!unpack_u32(e, &tmp, NULL))
  459. goto fail;
  460. if (tmp)
  461. profile->mode = APPARMOR_COMPLAIN;
  462. if (!unpack_u32(e, &tmp, NULL))
  463. goto fail;
  464. if (tmp)
  465. profile->audit = AUDIT_ALL;
  466. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  467. goto fail;
  468. /* path_flags is optional */
  469. if (unpack_u32(e, &profile->path_flags, "path_flags"))
  470. profile->path_flags |= profile->flags & PFLAG_MEDIATE_DELETED;
  471. else
  472. /* set a default value if path_flags field is not present */
  473. profile->path_flags = PFLAG_MEDIATE_DELETED;
  474. if (!unpack_u32(e, &(profile->caps.allow.cap[0]), NULL))
  475. goto fail;
  476. if (!unpack_u32(e, &(profile->caps.audit.cap[0]), NULL))
  477. goto fail;
  478. if (!unpack_u32(e, &(profile->caps.quiet.cap[0]), NULL))
  479. goto fail;
  480. if (!unpack_u32(e, &tmpcap.cap[0], NULL))
  481. goto fail;
  482. if (unpack_nameX(e, AA_STRUCT, "caps64")) {
  483. /* optional upper half of 64 bit caps */
  484. if (!unpack_u32(e, &(profile->caps.allow.cap[1]), NULL))
  485. goto fail;
  486. if (!unpack_u32(e, &(profile->caps.audit.cap[1]), NULL))
  487. goto fail;
  488. if (!unpack_u32(e, &(profile->caps.quiet.cap[1]), NULL))
  489. goto fail;
  490. if (!unpack_u32(e, &(tmpcap.cap[1]), NULL))
  491. goto fail;
  492. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  493. goto fail;
  494. }
  495. if (unpack_nameX(e, AA_STRUCT, "capsx")) {
  496. /* optional extended caps mediation mask */
  497. if (!unpack_u32(e, &(profile->caps.extended.cap[0]), NULL))
  498. goto fail;
  499. if (!unpack_u32(e, &(profile->caps.extended.cap[1]), NULL))
  500. goto fail;
  501. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  502. goto fail;
  503. }
  504. if (!unpack_rlimits(e, profile))
  505. goto fail;
  506. if (unpack_nameX(e, AA_STRUCT, "policydb")) {
  507. /* generic policy dfa - optional and may be NULL */
  508. profile->policy.dfa = unpack_dfa(e);
  509. if (IS_ERR(profile->policy.dfa)) {
  510. error = PTR_ERR(profile->policy.dfa);
  511. profile->policy.dfa = NULL;
  512. goto fail;
  513. }
  514. if (!unpack_u32(e, &profile->policy.start[0], "start"))
  515. /* default start state */
  516. profile->policy.start[0] = DFA_START;
  517. /* setup class index */
  518. for (i = AA_CLASS_FILE; i <= AA_CLASS_LAST; i++) {
  519. profile->policy.start[i] =
  520. aa_dfa_next(profile->policy.dfa,
  521. profile->policy.start[0],
  522. i);
  523. }
  524. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  525. goto fail;
  526. }
  527. /* get file rules */
  528. profile->file.dfa = unpack_dfa(e);
  529. if (IS_ERR(profile->file.dfa)) {
  530. error = PTR_ERR(profile->file.dfa);
  531. profile->file.dfa = NULL;
  532. goto fail;
  533. }
  534. if (!unpack_u32(e, &profile->file.start, "dfa_start"))
  535. /* default start state */
  536. profile->file.start = DFA_START;
  537. if (!unpack_trans_table(e, profile))
  538. goto fail;
  539. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  540. goto fail;
  541. return profile;
  542. fail:
  543. if (profile)
  544. name = NULL;
  545. else if (!name)
  546. name = "unknown";
  547. audit_iface(profile, name, "failed to unpack profile", e, error);
  548. aa_put_profile(profile);
  549. return ERR_PTR(error);
  550. }
  551. /**
  552. * verify_head - unpack serialized stream header
  553. * @e: serialized data read head (NOT NULL)
  554. * @ns: Returns - namespace if one is specified else NULL (NOT NULL)
  555. *
  556. * Returns: error or 0 if header is good
  557. */
  558. static int verify_header(struct aa_ext *e, const char **ns)
  559. {
  560. int error = -EPROTONOSUPPORT;
  561. /* get the interface version */
  562. if (!unpack_u32(e, &e->version, "version")) {
  563. audit_iface(NULL, NULL, "invalid profile format", e, error);
  564. return error;
  565. }
  566. /* check that the interface version is currently supported */
  567. if (e->version != 5) {
  568. audit_iface(NULL, NULL, "unsupported interface version", e,
  569. error);
  570. return error;
  571. }
  572. /* read the namespace if present */
  573. if (!unpack_str(e, ns, "namespace"))
  574. *ns = NULL;
  575. return 0;
  576. }
  577. static bool verify_xindex(int xindex, int table_size)
  578. {
  579. int index, xtype;
  580. xtype = xindex & AA_X_TYPE_MASK;
  581. index = xindex & AA_X_INDEX_MASK;
  582. if (xtype == AA_X_TABLE && index > table_size)
  583. return 0;
  584. return 1;
  585. }
  586. /* verify dfa xindexes are in range of transition tables */
  587. static bool verify_dfa_xindex(struct aa_dfa *dfa, int table_size)
  588. {
  589. int i;
  590. for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
  591. if (!verify_xindex(dfa_user_xindex(dfa, i), table_size))
  592. return 0;
  593. if (!verify_xindex(dfa_other_xindex(dfa, i), table_size))
  594. return 0;
  595. }
  596. return 1;
  597. }
  598. /**
  599. * verify_profile - Do post unpack analysis to verify profile consistency
  600. * @profile: profile to verify (NOT NULL)
  601. *
  602. * Returns: 0 if passes verification else error
  603. */
  604. static int verify_profile(struct aa_profile *profile)
  605. {
  606. if (aa_g_paranoid_load) {
  607. if (profile->file.dfa &&
  608. !verify_dfa_xindex(profile->file.dfa,
  609. profile->file.trans.size)) {
  610. audit_iface(profile, NULL, "Invalid named transition",
  611. NULL, -EPROTO);
  612. return -EPROTO;
  613. }
  614. }
  615. return 0;
  616. }
  617. /**
  618. * aa_unpack - unpack packed binary profile data loaded from user space
  619. * @udata: user data copied to kmem (NOT NULL)
  620. * @size: the size of the user data
  621. * @ns: Returns namespace profile is in if specified else NULL (NOT NULL)
  622. *
  623. * Unpack user data and return refcounted allocated profile or ERR_PTR
  624. *
  625. * Returns: profile else error pointer if fails to unpack
  626. */
  627. struct aa_profile *aa_unpack(void *udata, size_t size, const char **ns)
  628. {
  629. struct aa_profile *profile = NULL;
  630. int error;
  631. struct aa_ext e = {
  632. .start = udata,
  633. .end = udata + size,
  634. .pos = udata,
  635. };
  636. error = verify_header(&e, ns);
  637. if (error)
  638. return ERR_PTR(error);
  639. profile = unpack_profile(&e);
  640. if (IS_ERR(profile))
  641. return profile;
  642. error = verify_profile(profile);
  643. if (error) {
  644. aa_put_profile(profile);
  645. profile = ERR_PTR(error);
  646. }
  647. /* return refcount */
  648. return profile;
  649. }