policy_unpack.c 17 KB

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