policy_unpack.c 19 KB

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