policy_unpack.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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 see Documentation/admin-guide/LSM/apparmor.rst
  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/cred.h"
  25. #include "include/crypto.h"
  26. #include "include/match.h"
  27. #include "include/path.h"
  28. #include "include/policy.h"
  29. #include "include/policy_unpack.h"
  30. #define K_ABI_MASK 0x3ff
  31. #define FORCE_COMPLAIN_FLAG 0x800
  32. #define VERSION_LT(X, Y) (((X) & K_ABI_MASK) < ((Y) & K_ABI_MASK))
  33. #define VERSION_GT(X, Y) (((X) & K_ABI_MASK) > ((Y) & K_ABI_MASK))
  34. #define v5 5 /* base version */
  35. #define v6 6 /* per entry policydb mediation check */
  36. #define v7 7
  37. #define v8 8 /* full network masking */
  38. /*
  39. * The AppArmor interface treats data as a type byte followed by the
  40. * actual data. The interface has the notion of a a named entry
  41. * which has a name (AA_NAME typecode followed by name string) followed by
  42. * the entries typecode and data. Named types allow for optional
  43. * elements and extensions to be added and tested for without breaking
  44. * backwards compatibility.
  45. */
  46. enum aa_code {
  47. AA_U8,
  48. AA_U16,
  49. AA_U32,
  50. AA_U64,
  51. AA_NAME, /* same as string except it is items name */
  52. AA_STRING,
  53. AA_BLOB,
  54. AA_STRUCT,
  55. AA_STRUCTEND,
  56. AA_LIST,
  57. AA_LISTEND,
  58. AA_ARRAY,
  59. AA_ARRAYEND,
  60. };
  61. /*
  62. * aa_ext is the read of the buffer containing the serialized profile. The
  63. * data is copied into a kernel buffer in apparmorfs and then handed off to
  64. * the unpack routines.
  65. */
  66. struct aa_ext {
  67. void *start;
  68. void *end;
  69. void *pos; /* pointer to current position in the buffer */
  70. u32 version;
  71. };
  72. /* audit callback for unpack fields */
  73. static void audit_cb(struct audit_buffer *ab, void *va)
  74. {
  75. struct common_audit_data *sa = va;
  76. if (aad(sa)->iface.ns) {
  77. audit_log_format(ab, " ns=");
  78. audit_log_untrustedstring(ab, aad(sa)->iface.ns);
  79. }
  80. if (aad(sa)->name) {
  81. audit_log_format(ab, " name=");
  82. audit_log_untrustedstring(ab, aad(sa)->name);
  83. }
  84. if (aad(sa)->iface.pos)
  85. audit_log_format(ab, " offset=%ld", aad(sa)->iface.pos);
  86. }
  87. /**
  88. * audit_iface - do audit message for policy unpacking/load/replace/remove
  89. * @new: profile if it has been allocated (MAYBE NULL)
  90. * @ns_name: name of the ns the profile is to be loaded to (MAY BE NULL)
  91. * @name: name of the profile being manipulated (MAYBE NULL)
  92. * @info: any extra info about the failure (MAYBE NULL)
  93. * @e: buffer position info
  94. * @error: error code
  95. *
  96. * Returns: %0 or error
  97. */
  98. static int audit_iface(struct aa_profile *new, const char *ns_name,
  99. const char *name, const char *info, struct aa_ext *e,
  100. int error)
  101. {
  102. struct aa_profile *profile = labels_profile(aa_current_raw_label());
  103. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, NULL);
  104. if (e)
  105. aad(&sa)->iface.pos = e->pos - e->start;
  106. aad(&sa)->iface.ns = ns_name;
  107. if (new)
  108. aad(&sa)->name = new->base.hname;
  109. else
  110. aad(&sa)->name = name;
  111. aad(&sa)->info = info;
  112. aad(&sa)->error = error;
  113. return aa_audit(AUDIT_APPARMOR_STATUS, profile, &sa, audit_cb);
  114. }
  115. void __aa_loaddata_update(struct aa_loaddata *data, long revision)
  116. {
  117. AA_BUG(!data);
  118. AA_BUG(!data->ns);
  119. AA_BUG(!data->dents[AAFS_LOADDATA_REVISION]);
  120. AA_BUG(!mutex_is_locked(&data->ns->lock));
  121. AA_BUG(data->revision > revision);
  122. data->revision = revision;
  123. d_inode(data->dents[AAFS_LOADDATA_DIR])->i_mtime =
  124. current_time(d_inode(data->dents[AAFS_LOADDATA_DIR]));
  125. d_inode(data->dents[AAFS_LOADDATA_REVISION])->i_mtime =
  126. current_time(d_inode(data->dents[AAFS_LOADDATA_REVISION]));
  127. }
  128. bool aa_rawdata_eq(struct aa_loaddata *l, struct aa_loaddata *r)
  129. {
  130. if (l->size != r->size)
  131. return false;
  132. if (aa_g_hash_policy && memcmp(l->hash, r->hash, aa_hash_size()) != 0)
  133. return false;
  134. return memcmp(l->data, r->data, r->size) == 0;
  135. }
  136. /*
  137. * need to take the ns mutex lock which is NOT safe most places that
  138. * put_loaddata is called, so we have to delay freeing it
  139. */
  140. static void do_loaddata_free(struct work_struct *work)
  141. {
  142. struct aa_loaddata *d = container_of(work, struct aa_loaddata, work);
  143. struct aa_ns *ns = aa_get_ns(d->ns);
  144. if (ns) {
  145. mutex_lock_nested(&ns->lock, ns->level);
  146. __aa_fs_remove_rawdata(d);
  147. mutex_unlock(&ns->lock);
  148. aa_put_ns(ns);
  149. }
  150. kzfree(d->hash);
  151. kzfree(d->name);
  152. kvfree(d->data);
  153. kzfree(d);
  154. }
  155. void aa_loaddata_kref(struct kref *kref)
  156. {
  157. struct aa_loaddata *d = container_of(kref, struct aa_loaddata, count);
  158. if (d) {
  159. INIT_WORK(&d->work, do_loaddata_free);
  160. schedule_work(&d->work);
  161. }
  162. }
  163. struct aa_loaddata *aa_loaddata_alloc(size_t size)
  164. {
  165. struct aa_loaddata *d;
  166. d = kzalloc(sizeof(*d), GFP_KERNEL);
  167. if (d == NULL)
  168. return ERR_PTR(-ENOMEM);
  169. d->data = kvzalloc(size, GFP_KERNEL);
  170. if (!d->data) {
  171. kfree(d);
  172. return ERR_PTR(-ENOMEM);
  173. }
  174. kref_init(&d->count);
  175. INIT_LIST_HEAD(&d->list);
  176. return d;
  177. }
  178. /* test if read will be in packed data bounds */
  179. static bool inbounds(struct aa_ext *e, size_t size)
  180. {
  181. return (size <= e->end - e->pos);
  182. }
  183. static void *kvmemdup(const void *src, size_t len)
  184. {
  185. void *p = kvmalloc(len, GFP_KERNEL);
  186. if (p)
  187. memcpy(p, src, len);
  188. return p;
  189. }
  190. /**
  191. * aa_u16_chunck - test and do bounds checking for a u16 size based chunk
  192. * @e: serialized data read head (NOT NULL)
  193. * @chunk: start address for chunk of data (NOT NULL)
  194. *
  195. * Returns: the size of chunk found with the read head at the end of the chunk.
  196. */
  197. static size_t unpack_u16_chunk(struct aa_ext *e, char **chunk)
  198. {
  199. size_t size = 0;
  200. if (!inbounds(e, sizeof(u16)))
  201. return 0;
  202. size = le16_to_cpu(get_unaligned((__le16 *) e->pos));
  203. e->pos += sizeof(__le16);
  204. if (!inbounds(e, size))
  205. return 0;
  206. *chunk = e->pos;
  207. e->pos += size;
  208. return size;
  209. }
  210. /* unpack control byte */
  211. static bool unpack_X(struct aa_ext *e, enum aa_code code)
  212. {
  213. if (!inbounds(e, 1))
  214. return 0;
  215. if (*(u8 *) e->pos != code)
  216. return 0;
  217. e->pos++;
  218. return 1;
  219. }
  220. /**
  221. * unpack_nameX - check is the next element is of type X with a name of @name
  222. * @e: serialized data extent information (NOT NULL)
  223. * @code: type code
  224. * @name: name to match to the serialized element. (MAYBE NULL)
  225. *
  226. * check that the next serialized data element is of type X and has a tag
  227. * name @name. If @name is specified then there must be a matching
  228. * name element in the stream. If @name is NULL any name element will be
  229. * skipped and only the typecode will be tested.
  230. *
  231. * Returns 1 on success (both type code and name tests match) and the read
  232. * head is advanced past the headers
  233. *
  234. * Returns: 0 if either match fails, the read head does not move
  235. */
  236. static bool unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name)
  237. {
  238. /*
  239. * May need to reset pos if name or type doesn't match
  240. */
  241. void *pos = e->pos;
  242. /*
  243. * Check for presence of a tagname, and if present name size
  244. * AA_NAME tag value is a u16.
  245. */
  246. if (unpack_X(e, AA_NAME)) {
  247. char *tag = NULL;
  248. size_t size = unpack_u16_chunk(e, &tag);
  249. /* if a name is specified it must match. otherwise skip tag */
  250. if (name && (!size || strcmp(name, tag)))
  251. goto fail;
  252. } else if (name) {
  253. /* if a name is specified and there is no name tag fail */
  254. goto fail;
  255. }
  256. /* now check if type code matches */
  257. if (unpack_X(e, code))
  258. return 1;
  259. fail:
  260. e->pos = pos;
  261. return 0;
  262. }
  263. static bool unpack_u8(struct aa_ext *e, u8 *data, const char *name)
  264. {
  265. if (unpack_nameX(e, AA_U8, name)) {
  266. if (!inbounds(e, sizeof(u8)))
  267. return 0;
  268. if (data)
  269. *data = get_unaligned((u8 *)e->pos);
  270. e->pos += sizeof(u8);
  271. return 1;
  272. }
  273. return 0;
  274. }
  275. static bool unpack_u32(struct aa_ext *e, u32 *data, const char *name)
  276. {
  277. if (unpack_nameX(e, AA_U32, name)) {
  278. if (!inbounds(e, sizeof(u32)))
  279. return 0;
  280. if (data)
  281. *data = le32_to_cpu(get_unaligned((__le32 *) e->pos));
  282. e->pos += sizeof(u32);
  283. return 1;
  284. }
  285. return 0;
  286. }
  287. static bool unpack_u64(struct aa_ext *e, u64 *data, const char *name)
  288. {
  289. if (unpack_nameX(e, AA_U64, name)) {
  290. if (!inbounds(e, sizeof(u64)))
  291. return 0;
  292. if (data)
  293. *data = le64_to_cpu(get_unaligned((__le64 *) e->pos));
  294. e->pos += sizeof(u64);
  295. return 1;
  296. }
  297. return 0;
  298. }
  299. static size_t unpack_array(struct aa_ext *e, const char *name)
  300. {
  301. if (unpack_nameX(e, AA_ARRAY, name)) {
  302. int size;
  303. if (!inbounds(e, sizeof(u16)))
  304. return 0;
  305. size = (int)le16_to_cpu(get_unaligned((__le16 *) e->pos));
  306. e->pos += sizeof(u16);
  307. return size;
  308. }
  309. return 0;
  310. }
  311. static size_t unpack_blob(struct aa_ext *e, char **blob, const char *name)
  312. {
  313. if (unpack_nameX(e, AA_BLOB, name)) {
  314. u32 size;
  315. if (!inbounds(e, sizeof(u32)))
  316. return 0;
  317. size = le32_to_cpu(get_unaligned((__le32 *) e->pos));
  318. e->pos += sizeof(u32);
  319. if (inbounds(e, (size_t) size)) {
  320. *blob = e->pos;
  321. e->pos += size;
  322. return size;
  323. }
  324. }
  325. return 0;
  326. }
  327. static int unpack_str(struct aa_ext *e, const char **string, const char *name)
  328. {
  329. char *src_str;
  330. size_t size = 0;
  331. void *pos = e->pos;
  332. *string = NULL;
  333. if (unpack_nameX(e, AA_STRING, name)) {
  334. size = unpack_u16_chunk(e, &src_str);
  335. if (size) {
  336. /* strings are null terminated, length is size - 1 */
  337. if (src_str[size - 1] != 0)
  338. goto fail;
  339. *string = src_str;
  340. }
  341. }
  342. return size;
  343. fail:
  344. e->pos = pos;
  345. return 0;
  346. }
  347. static int unpack_strdup(struct aa_ext *e, char **string, const char *name)
  348. {
  349. const char *tmp;
  350. void *pos = e->pos;
  351. int res = unpack_str(e, &tmp, name);
  352. *string = NULL;
  353. if (!res)
  354. return 0;
  355. *string = kmemdup(tmp, res, GFP_KERNEL);
  356. if (!*string) {
  357. e->pos = pos;
  358. return 0;
  359. }
  360. return res;
  361. }
  362. /**
  363. * unpack_dfa - unpack a file rule dfa
  364. * @e: serialized data extent information (NOT NULL)
  365. *
  366. * returns dfa or ERR_PTR or NULL if no dfa
  367. */
  368. static struct aa_dfa *unpack_dfa(struct aa_ext *e)
  369. {
  370. char *blob = NULL;
  371. size_t size;
  372. struct aa_dfa *dfa = NULL;
  373. size = unpack_blob(e, &blob, "aadfa");
  374. if (size) {
  375. /*
  376. * The dfa is aligned with in the blob to 8 bytes
  377. * from the beginning of the stream.
  378. * alignment adjust needed by dfa unpack
  379. */
  380. size_t sz = blob - (char *) e->start -
  381. ((e->pos - e->start) & 7);
  382. size_t pad = ALIGN(sz, 8) - sz;
  383. int flags = TO_ACCEPT1_FLAG(YYTD_DATA32) |
  384. TO_ACCEPT2_FLAG(YYTD_DATA32) | DFA_FLAG_VERIFY_STATES;
  385. dfa = aa_dfa_unpack(blob + pad, size - pad, flags);
  386. if (IS_ERR(dfa))
  387. return dfa;
  388. }
  389. return dfa;
  390. }
  391. /**
  392. * unpack_trans_table - unpack a profile transition table
  393. * @e: serialized data extent information (NOT NULL)
  394. * @profile: profile to add the accept table to (NOT NULL)
  395. *
  396. * Returns: 1 if table successfully unpacked
  397. */
  398. static bool unpack_trans_table(struct aa_ext *e, struct aa_profile *profile)
  399. {
  400. void *saved_pos = e->pos;
  401. /* exec table is optional */
  402. if (unpack_nameX(e, AA_STRUCT, "xtable")) {
  403. int i, size;
  404. size = unpack_array(e, NULL);
  405. /* currently 4 exec bits and entries 0-3 are reserved iupcx */
  406. if (size > 16 - 4)
  407. goto fail;
  408. profile->file.trans.table = kcalloc(size, sizeof(char *),
  409. GFP_KERNEL);
  410. if (!profile->file.trans.table)
  411. goto fail;
  412. profile->file.trans.size = size;
  413. for (i = 0; i < size; i++) {
  414. char *str;
  415. int c, j, pos, size2 = unpack_strdup(e, &str, NULL);
  416. /* unpack_strdup verifies that the last character is
  417. * null termination byte.
  418. */
  419. if (!size2)
  420. goto fail;
  421. profile->file.trans.table[i] = str;
  422. /* verify that name doesn't start with space */
  423. if (isspace(*str))
  424. goto fail;
  425. /* count internal # of internal \0 */
  426. for (c = j = 0; j < size2 - 1; j++) {
  427. if (!str[j]) {
  428. pos = j;
  429. c++;
  430. }
  431. }
  432. if (*str == ':') {
  433. /* first character after : must be valid */
  434. if (!str[1])
  435. goto fail;
  436. /* beginning with : requires an embedded \0,
  437. * verify that exactly 1 internal \0 exists
  438. * trailing \0 already verified by unpack_strdup
  439. *
  440. * convert \0 back to : for label_parse
  441. */
  442. if (c == 1)
  443. str[pos] = ':';
  444. else if (c > 1)
  445. goto fail;
  446. } else if (c)
  447. /* fail - all other cases with embedded \0 */
  448. goto fail;
  449. }
  450. if (!unpack_nameX(e, AA_ARRAYEND, NULL))
  451. goto fail;
  452. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  453. goto fail;
  454. }
  455. return 1;
  456. fail:
  457. aa_free_domain_entries(&profile->file.trans);
  458. e->pos = saved_pos;
  459. return 0;
  460. }
  461. static bool unpack_xattrs(struct aa_ext *e, struct aa_profile *profile)
  462. {
  463. void *pos = e->pos;
  464. if (unpack_nameX(e, AA_STRUCT, "xattrs")) {
  465. int i, size;
  466. size = unpack_array(e, NULL);
  467. profile->xattr_count = size;
  468. profile->xattrs = kcalloc(size, sizeof(char *), GFP_KERNEL);
  469. if (!profile->xattrs)
  470. goto fail;
  471. for (i = 0; i < size; i++) {
  472. if (!unpack_strdup(e, &profile->xattrs[i], NULL))
  473. goto fail;
  474. }
  475. if (!unpack_nameX(e, AA_ARRAYEND, NULL))
  476. goto fail;
  477. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  478. goto fail;
  479. }
  480. return 1;
  481. fail:
  482. e->pos = pos;
  483. return 0;
  484. }
  485. static bool unpack_secmark(struct aa_ext *e, struct aa_profile *profile)
  486. {
  487. void *pos = e->pos;
  488. int i, size;
  489. if (unpack_nameX(e, AA_STRUCT, "secmark")) {
  490. size = unpack_array(e, NULL);
  491. profile->secmark = kcalloc(size, sizeof(struct aa_secmark),
  492. GFP_KERNEL);
  493. if (!profile->secmark)
  494. goto fail;
  495. profile->secmark_count = size;
  496. for (i = 0; i < size; i++) {
  497. if (!unpack_u8(e, &profile->secmark[i].audit, NULL))
  498. goto fail;
  499. if (!unpack_u8(e, &profile->secmark[i].deny, NULL))
  500. goto fail;
  501. if (!unpack_strdup(e, &profile->secmark[i].label, NULL))
  502. goto fail;
  503. }
  504. if (!unpack_nameX(e, AA_ARRAYEND, NULL))
  505. goto fail;
  506. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  507. goto fail;
  508. }
  509. return 1;
  510. fail:
  511. if (profile->secmark) {
  512. for (i = 0; i < size; i++)
  513. kfree(profile->secmark[i].label);
  514. kfree(profile->secmark);
  515. profile->secmark_count = 0;
  516. }
  517. e->pos = pos;
  518. return 0;
  519. }
  520. static bool unpack_rlimits(struct aa_ext *e, struct aa_profile *profile)
  521. {
  522. void *pos = e->pos;
  523. /* rlimits are optional */
  524. if (unpack_nameX(e, AA_STRUCT, "rlimits")) {
  525. int i, size;
  526. u32 tmp = 0;
  527. if (!unpack_u32(e, &tmp, NULL))
  528. goto fail;
  529. profile->rlimits.mask = tmp;
  530. size = unpack_array(e, NULL);
  531. if (size > RLIM_NLIMITS)
  532. goto fail;
  533. for (i = 0; i < size; i++) {
  534. u64 tmp2 = 0;
  535. int a = aa_map_resource(i);
  536. if (!unpack_u64(e, &tmp2, NULL))
  537. goto fail;
  538. profile->rlimits.limits[a].rlim_max = tmp2;
  539. }
  540. if (!unpack_nameX(e, AA_ARRAYEND, NULL))
  541. goto fail;
  542. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  543. goto fail;
  544. }
  545. return 1;
  546. fail:
  547. e->pos = pos;
  548. return 0;
  549. }
  550. static u32 strhash(const void *data, u32 len, u32 seed)
  551. {
  552. const char * const *key = data;
  553. return jhash(*key, strlen(*key), seed);
  554. }
  555. static int datacmp(struct rhashtable_compare_arg *arg, const void *obj)
  556. {
  557. const struct aa_data *data = obj;
  558. const char * const *key = arg->key;
  559. return strcmp(data->key, *key);
  560. }
  561. /**
  562. * unpack_profile - unpack a serialized profile
  563. * @e: serialized data extent information (NOT NULL)
  564. *
  565. * NOTE: unpack profile sets audit struct if there is a failure
  566. */
  567. static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name)
  568. {
  569. struct aa_profile *profile = NULL;
  570. const char *tmpname, *tmpns = NULL, *name = NULL;
  571. const char *info = "failed to unpack profile";
  572. size_t ns_len;
  573. struct rhashtable_params params = { 0 };
  574. char *key = NULL;
  575. struct aa_data *data;
  576. int i, error = -EPROTO;
  577. kernel_cap_t tmpcap;
  578. u32 tmp;
  579. *ns_name = NULL;
  580. /* check that we have the right struct being passed */
  581. if (!unpack_nameX(e, AA_STRUCT, "profile"))
  582. goto fail;
  583. if (!unpack_str(e, &name, NULL))
  584. goto fail;
  585. if (*name == '\0')
  586. goto fail;
  587. tmpname = aa_splitn_fqname(name, strlen(name), &tmpns, &ns_len);
  588. if (tmpns) {
  589. *ns_name = kstrndup(tmpns, ns_len, GFP_KERNEL);
  590. if (!*ns_name) {
  591. info = "out of memory";
  592. goto fail;
  593. }
  594. name = tmpname;
  595. }
  596. profile = aa_alloc_profile(name, NULL, GFP_KERNEL);
  597. if (!profile)
  598. return ERR_PTR(-ENOMEM);
  599. /* profile renaming is optional */
  600. (void) unpack_str(e, &profile->rename, "rename");
  601. /* attachment string is optional */
  602. (void) unpack_str(e, &profile->attach, "attach");
  603. /* xmatch is optional and may be NULL */
  604. profile->xmatch = unpack_dfa(e);
  605. if (IS_ERR(profile->xmatch)) {
  606. error = PTR_ERR(profile->xmatch);
  607. profile->xmatch = NULL;
  608. info = "bad xmatch";
  609. goto fail;
  610. }
  611. /* xmatch_len is not optional if xmatch is set */
  612. if (profile->xmatch) {
  613. if (!unpack_u32(e, &tmp, NULL)) {
  614. info = "missing xmatch len";
  615. goto fail;
  616. }
  617. profile->xmatch_len = tmp;
  618. }
  619. /* disconnected attachment string is optional */
  620. (void) unpack_str(e, &profile->disconnected, "disconnected");
  621. /* per profile debug flags (complain, audit) */
  622. if (!unpack_nameX(e, AA_STRUCT, "flags")) {
  623. info = "profile missing flags";
  624. goto fail;
  625. }
  626. info = "failed to unpack profile flags";
  627. if (!unpack_u32(e, &tmp, NULL))
  628. goto fail;
  629. if (tmp & PACKED_FLAG_HAT)
  630. profile->label.flags |= FLAG_HAT;
  631. if (!unpack_u32(e, &tmp, NULL))
  632. goto fail;
  633. if (tmp == PACKED_MODE_COMPLAIN || (e->version & FORCE_COMPLAIN_FLAG))
  634. profile->mode = APPARMOR_COMPLAIN;
  635. else if (tmp == PACKED_MODE_KILL)
  636. profile->mode = APPARMOR_KILL;
  637. else if (tmp == PACKED_MODE_UNCONFINED)
  638. profile->mode = APPARMOR_UNCONFINED;
  639. if (!unpack_u32(e, &tmp, NULL))
  640. goto fail;
  641. if (tmp)
  642. profile->audit = AUDIT_ALL;
  643. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  644. goto fail;
  645. /* path_flags is optional */
  646. if (unpack_u32(e, &profile->path_flags, "path_flags"))
  647. profile->path_flags |= profile->label.flags &
  648. PATH_MEDIATE_DELETED;
  649. else
  650. /* set a default value if path_flags field is not present */
  651. profile->path_flags = PATH_MEDIATE_DELETED;
  652. info = "failed to unpack profile capabilities";
  653. if (!unpack_u32(e, &(profile->caps.allow.cap[0]), NULL))
  654. goto fail;
  655. if (!unpack_u32(e, &(profile->caps.audit.cap[0]), NULL))
  656. goto fail;
  657. if (!unpack_u32(e, &(profile->caps.quiet.cap[0]), NULL))
  658. goto fail;
  659. if (!unpack_u32(e, &tmpcap.cap[0], NULL))
  660. goto fail;
  661. info = "failed to unpack upper profile capabilities";
  662. if (unpack_nameX(e, AA_STRUCT, "caps64")) {
  663. /* optional upper half of 64 bit caps */
  664. if (!unpack_u32(e, &(profile->caps.allow.cap[1]), NULL))
  665. goto fail;
  666. if (!unpack_u32(e, &(profile->caps.audit.cap[1]), NULL))
  667. goto fail;
  668. if (!unpack_u32(e, &(profile->caps.quiet.cap[1]), NULL))
  669. goto fail;
  670. if (!unpack_u32(e, &(tmpcap.cap[1]), NULL))
  671. goto fail;
  672. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  673. goto fail;
  674. }
  675. info = "failed to unpack extended profile capabilities";
  676. if (unpack_nameX(e, AA_STRUCT, "capsx")) {
  677. /* optional extended caps mediation mask */
  678. if (!unpack_u32(e, &(profile->caps.extended.cap[0]), NULL))
  679. goto fail;
  680. if (!unpack_u32(e, &(profile->caps.extended.cap[1]), NULL))
  681. goto fail;
  682. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  683. goto fail;
  684. }
  685. if (!unpack_xattrs(e, profile)) {
  686. info = "failed to unpack profile xattrs";
  687. goto fail;
  688. }
  689. if (!unpack_rlimits(e, profile)) {
  690. info = "failed to unpack profile rlimits";
  691. goto fail;
  692. }
  693. if (!unpack_secmark(e, profile)) {
  694. info = "failed to unpack profile secmark rules";
  695. goto fail;
  696. }
  697. if (unpack_nameX(e, AA_STRUCT, "policydb")) {
  698. /* generic policy dfa - optional and may be NULL */
  699. info = "failed to unpack policydb";
  700. profile->policy.dfa = unpack_dfa(e);
  701. if (IS_ERR(profile->policy.dfa)) {
  702. error = PTR_ERR(profile->policy.dfa);
  703. profile->policy.dfa = NULL;
  704. goto fail;
  705. } else if (!profile->policy.dfa) {
  706. error = -EPROTO;
  707. goto fail;
  708. }
  709. if (!unpack_u32(e, &profile->policy.start[0], "start"))
  710. /* default start state */
  711. profile->policy.start[0] = DFA_START;
  712. /* setup class index */
  713. for (i = AA_CLASS_FILE; i <= AA_CLASS_LAST; i++) {
  714. profile->policy.start[i] =
  715. aa_dfa_next(profile->policy.dfa,
  716. profile->policy.start[0],
  717. i);
  718. }
  719. if (!unpack_nameX(e, AA_STRUCTEND, NULL))
  720. goto fail;
  721. } else
  722. profile->policy.dfa = aa_get_dfa(nulldfa);
  723. /* get file rules */
  724. profile->file.dfa = unpack_dfa(e);
  725. if (IS_ERR(profile->file.dfa)) {
  726. error = PTR_ERR(profile->file.dfa);
  727. profile->file.dfa = NULL;
  728. info = "failed to unpack profile file rules";
  729. goto fail;
  730. } else if (profile->file.dfa) {
  731. if (!unpack_u32(e, &profile->file.start, "dfa_start"))
  732. /* default start state */
  733. profile->file.start = DFA_START;
  734. } else if (profile->policy.dfa &&
  735. profile->policy.start[AA_CLASS_FILE]) {
  736. profile->file.dfa = aa_get_dfa(profile->policy.dfa);
  737. profile->file.start = profile->policy.start[AA_CLASS_FILE];
  738. } else
  739. profile->file.dfa = aa_get_dfa(nulldfa);
  740. if (!unpack_trans_table(e, profile)) {
  741. info = "failed to unpack profile transition table";
  742. goto fail;
  743. }
  744. if (unpack_nameX(e, AA_STRUCT, "data")) {
  745. info = "out of memory";
  746. profile->data = kzalloc(sizeof(*profile->data), GFP_KERNEL);
  747. if (!profile->data)
  748. goto fail;
  749. params.nelem_hint = 3;
  750. params.key_len = sizeof(void *);
  751. params.key_offset = offsetof(struct aa_data, key);
  752. params.head_offset = offsetof(struct aa_data, head);
  753. params.hashfn = strhash;
  754. params.obj_cmpfn = datacmp;
  755. if (rhashtable_init(profile->data, &params)) {
  756. info = "failed to init key, value hash table";
  757. goto fail;
  758. }
  759. while (unpack_strdup(e, &key, NULL)) {
  760. data = kzalloc(sizeof(*data), GFP_KERNEL);
  761. if (!data) {
  762. kzfree(key);
  763. goto fail;
  764. }
  765. data->key = key;
  766. data->size = unpack_blob(e, &data->data, NULL);
  767. data->data = kvmemdup(data->data, data->size);
  768. if (data->size && !data->data) {
  769. kzfree(data->key);
  770. kzfree(data);
  771. goto fail;
  772. }
  773. rhashtable_insert_fast(profile->data, &data->head,
  774. profile->data->p);
  775. }
  776. if (!unpack_nameX(e, AA_STRUCTEND, NULL)) {
  777. info = "failed to unpack end of key, value data table";
  778. goto fail;
  779. }
  780. }
  781. if (!unpack_nameX(e, AA_STRUCTEND, NULL)) {
  782. info = "failed to unpack end of profile";
  783. goto fail;
  784. }
  785. return profile;
  786. fail:
  787. if (profile)
  788. name = NULL;
  789. else if (!name)
  790. name = "unknown";
  791. audit_iface(profile, NULL, name, info, e, error);
  792. aa_free_profile(profile);
  793. return ERR_PTR(error);
  794. }
  795. /**
  796. * verify_head - unpack serialized stream header
  797. * @e: serialized data read head (NOT NULL)
  798. * @required: whether the header is required or optional
  799. * @ns: Returns - namespace if one is specified else NULL (NOT NULL)
  800. *
  801. * Returns: error or 0 if header is good
  802. */
  803. static int verify_header(struct aa_ext *e, int required, const char **ns)
  804. {
  805. int error = -EPROTONOSUPPORT;
  806. const char *name = NULL;
  807. *ns = NULL;
  808. /* get the interface version */
  809. if (!unpack_u32(e, &e->version, "version")) {
  810. if (required) {
  811. audit_iface(NULL, NULL, NULL, "invalid profile format",
  812. e, error);
  813. return error;
  814. }
  815. }
  816. /* Check that the interface version is currently supported.
  817. * if not specified use previous version
  818. * Mask off everything that is not kernel abi version
  819. */
  820. if (VERSION_LT(e->version, v5) || VERSION_GT(e->version, v7)) {
  821. audit_iface(NULL, NULL, NULL, "unsupported interface version",
  822. e, error);
  823. return error;
  824. }
  825. /* read the namespace if present */
  826. if (unpack_str(e, &name, "namespace")) {
  827. if (*name == '\0') {
  828. audit_iface(NULL, NULL, NULL, "invalid namespace name",
  829. e, error);
  830. return error;
  831. }
  832. if (*ns && strcmp(*ns, name))
  833. audit_iface(NULL, NULL, NULL, "invalid ns change", e,
  834. error);
  835. else if (!*ns)
  836. *ns = name;
  837. }
  838. return 0;
  839. }
  840. static bool verify_xindex(int xindex, int table_size)
  841. {
  842. int index, xtype;
  843. xtype = xindex & AA_X_TYPE_MASK;
  844. index = xindex & AA_X_INDEX_MASK;
  845. if (xtype == AA_X_TABLE && index >= table_size)
  846. return 0;
  847. return 1;
  848. }
  849. /* verify dfa xindexes are in range of transition tables */
  850. static bool verify_dfa_xindex(struct aa_dfa *dfa, int table_size)
  851. {
  852. int i;
  853. for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
  854. if (!verify_xindex(dfa_user_xindex(dfa, i), table_size))
  855. return 0;
  856. if (!verify_xindex(dfa_other_xindex(dfa, i), table_size))
  857. return 0;
  858. }
  859. return 1;
  860. }
  861. /**
  862. * verify_profile - Do post unpack analysis to verify profile consistency
  863. * @profile: profile to verify (NOT NULL)
  864. *
  865. * Returns: 0 if passes verification else error
  866. */
  867. static int verify_profile(struct aa_profile *profile)
  868. {
  869. if (profile->file.dfa &&
  870. !verify_dfa_xindex(profile->file.dfa,
  871. profile->file.trans.size)) {
  872. audit_iface(profile, NULL, NULL, "Invalid named transition",
  873. NULL, -EPROTO);
  874. return -EPROTO;
  875. }
  876. return 0;
  877. }
  878. void aa_load_ent_free(struct aa_load_ent *ent)
  879. {
  880. if (ent) {
  881. aa_put_profile(ent->rename);
  882. aa_put_profile(ent->old);
  883. aa_put_profile(ent->new);
  884. kfree(ent->ns_name);
  885. kzfree(ent);
  886. }
  887. }
  888. struct aa_load_ent *aa_load_ent_alloc(void)
  889. {
  890. struct aa_load_ent *ent = kzalloc(sizeof(*ent), GFP_KERNEL);
  891. if (ent)
  892. INIT_LIST_HEAD(&ent->list);
  893. return ent;
  894. }
  895. /**
  896. * aa_unpack - unpack packed binary profile(s) data loaded from user space
  897. * @udata: user data copied to kmem (NOT NULL)
  898. * @lh: list to place unpacked profiles in a aa_repl_ws
  899. * @ns: Returns namespace profile is in if specified else NULL (NOT NULL)
  900. *
  901. * Unpack user data and return refcounted allocated profile(s) stored in
  902. * @lh in order of discovery, with the list chain stored in base.list
  903. * or error
  904. *
  905. * Returns: profile(s) on @lh else error pointer if fails to unpack
  906. */
  907. int aa_unpack(struct aa_loaddata *udata, struct list_head *lh,
  908. const char **ns)
  909. {
  910. struct aa_load_ent *tmp, *ent;
  911. struct aa_profile *profile = NULL;
  912. int error;
  913. struct aa_ext e = {
  914. .start = udata->data,
  915. .end = udata->data + udata->size,
  916. .pos = udata->data,
  917. };
  918. *ns = NULL;
  919. while (e.pos < e.end) {
  920. char *ns_name = NULL;
  921. void *start;
  922. error = verify_header(&e, e.pos == e.start, ns);
  923. if (error)
  924. goto fail;
  925. start = e.pos;
  926. profile = unpack_profile(&e, &ns_name);
  927. if (IS_ERR(profile)) {
  928. error = PTR_ERR(profile);
  929. goto fail;
  930. }
  931. error = verify_profile(profile);
  932. if (error)
  933. goto fail_profile;
  934. if (aa_g_hash_policy)
  935. error = aa_calc_profile_hash(profile, e.version, start,
  936. e.pos - start);
  937. if (error)
  938. goto fail_profile;
  939. ent = aa_load_ent_alloc();
  940. if (!ent) {
  941. error = -ENOMEM;
  942. goto fail_profile;
  943. }
  944. ent->new = profile;
  945. ent->ns_name = ns_name;
  946. list_add_tail(&ent->list, lh);
  947. }
  948. udata->abi = e.version & K_ABI_MASK;
  949. if (aa_g_hash_policy) {
  950. udata->hash = aa_calc_hash(udata->data, udata->size);
  951. if (IS_ERR(udata->hash)) {
  952. error = PTR_ERR(udata->hash);
  953. udata->hash = NULL;
  954. goto fail;
  955. }
  956. }
  957. return 0;
  958. fail_profile:
  959. aa_put_profile(profile);
  960. fail:
  961. list_for_each_entry_safe(ent, tmp, lh, list) {
  962. list_del_init(&ent->list);
  963. aa_load_ent_free(ent);
  964. }
  965. return error;
  966. }