avtab.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * Implementation of the access vector table type.
  3. *
  4. * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
  5. */
  6. /* Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
  7. *
  8. * Added conditional policy language extensions
  9. *
  10. * Copyright (C) 2003 Tresys Technology, LLC
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, version 2.
  14. *
  15. * Updated: Yuichi Nakamura <ynakam@hitachisoft.jp>
  16. * Tuned number of hash slots for avtab to reduce memory usage
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/slab.h>
  20. #include <linux/errno.h>
  21. #include "avtab.h"
  22. #include "policydb.h"
  23. static struct kmem_cache *avtab_node_cachep;
  24. static struct kmem_cache *avtab_xperms_cachep;
  25. static inline int avtab_hash(struct avtab_key *keyp, u16 mask)
  26. {
  27. return ((keyp->target_class + (keyp->target_type << 2) +
  28. (keyp->source_type << 9)) & mask);
  29. }
  30. static struct avtab_node*
  31. avtab_insert_node(struct avtab *h, int hvalue,
  32. struct avtab_node *prev, struct avtab_node *cur,
  33. struct avtab_key *key, struct avtab_datum *datum)
  34. {
  35. struct avtab_node *newnode;
  36. struct avtab_extended_perms *xperms;
  37. newnode = kmem_cache_zalloc(avtab_node_cachep, GFP_KERNEL);
  38. if (newnode == NULL)
  39. return NULL;
  40. newnode->key = *key;
  41. if (key->specified & AVTAB_XPERMS) {
  42. xperms = kmem_cache_zalloc(avtab_xperms_cachep, GFP_KERNEL);
  43. if (xperms == NULL) {
  44. kmem_cache_free(avtab_node_cachep, newnode);
  45. return NULL;
  46. }
  47. *xperms = *(datum->u.xperms);
  48. newnode->datum.u.xperms = xperms;
  49. } else {
  50. newnode->datum.u.data = datum->u.data;
  51. }
  52. if (prev) {
  53. newnode->next = prev->next;
  54. prev->next = newnode;
  55. } else {
  56. newnode->next = h->htable[hvalue];
  57. h->htable[hvalue] = newnode;
  58. }
  59. h->nel++;
  60. return newnode;
  61. }
  62. static int avtab_insert(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum)
  63. {
  64. int hvalue;
  65. struct avtab_node *prev, *cur, *newnode;
  66. u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
  67. if (!h || !h->htable)
  68. return -EINVAL;
  69. hvalue = avtab_hash(key, h->mask);
  70. for (prev = NULL, cur = h->htable[hvalue];
  71. cur;
  72. prev = cur, cur = cur->next) {
  73. if (key->source_type == cur->key.source_type &&
  74. key->target_type == cur->key.target_type &&
  75. key->target_class == cur->key.target_class &&
  76. (specified & cur->key.specified)) {
  77. /* extended perms may not be unique */
  78. if (specified & AVTAB_XPERMS)
  79. break;
  80. return -EEXIST;
  81. }
  82. if (key->source_type < cur->key.source_type)
  83. break;
  84. if (key->source_type == cur->key.source_type &&
  85. key->target_type < cur->key.target_type)
  86. break;
  87. if (key->source_type == cur->key.source_type &&
  88. key->target_type == cur->key.target_type &&
  89. key->target_class < cur->key.target_class)
  90. break;
  91. }
  92. newnode = avtab_insert_node(h, hvalue, prev, cur, key, datum);
  93. if (!newnode)
  94. return -ENOMEM;
  95. return 0;
  96. }
  97. /* Unlike avtab_insert(), this function allow multiple insertions of the same
  98. * key/specified mask into the table, as needed by the conditional avtab.
  99. * It also returns a pointer to the node inserted.
  100. */
  101. struct avtab_node *
  102. avtab_insert_nonunique(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum)
  103. {
  104. int hvalue;
  105. struct avtab_node *prev, *cur;
  106. u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
  107. if (!h || !h->htable)
  108. return NULL;
  109. hvalue = avtab_hash(key, h->mask);
  110. for (prev = NULL, cur = h->htable[hvalue];
  111. cur;
  112. prev = cur, cur = cur->next) {
  113. if (key->source_type == cur->key.source_type &&
  114. key->target_type == cur->key.target_type &&
  115. key->target_class == cur->key.target_class &&
  116. (specified & cur->key.specified))
  117. break;
  118. if (key->source_type < cur->key.source_type)
  119. break;
  120. if (key->source_type == cur->key.source_type &&
  121. key->target_type < cur->key.target_type)
  122. break;
  123. if (key->source_type == cur->key.source_type &&
  124. key->target_type == cur->key.target_type &&
  125. key->target_class < cur->key.target_class)
  126. break;
  127. }
  128. return avtab_insert_node(h, hvalue, prev, cur, key, datum);
  129. }
  130. struct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *key)
  131. {
  132. int hvalue;
  133. struct avtab_node *cur;
  134. u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
  135. if (!h || !h->htable)
  136. return NULL;
  137. hvalue = avtab_hash(key, h->mask);
  138. for (cur = h->htable[hvalue]; cur; cur = cur->next) {
  139. if (key->source_type == cur->key.source_type &&
  140. key->target_type == cur->key.target_type &&
  141. key->target_class == cur->key.target_class &&
  142. (specified & cur->key.specified))
  143. return &cur->datum;
  144. if (key->source_type < cur->key.source_type)
  145. break;
  146. if (key->source_type == cur->key.source_type &&
  147. key->target_type < cur->key.target_type)
  148. break;
  149. if (key->source_type == cur->key.source_type &&
  150. key->target_type == cur->key.target_type &&
  151. key->target_class < cur->key.target_class)
  152. break;
  153. }
  154. return NULL;
  155. }
  156. /* This search function returns a node pointer, and can be used in
  157. * conjunction with avtab_search_next_node()
  158. */
  159. struct avtab_node*
  160. avtab_search_node(struct avtab *h, struct avtab_key *key)
  161. {
  162. int hvalue;
  163. struct avtab_node *cur;
  164. u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
  165. if (!h || !h->htable)
  166. return NULL;
  167. hvalue = avtab_hash(key, h->mask);
  168. for (cur = h->htable[hvalue]; cur; cur = cur->next) {
  169. if (key->source_type == cur->key.source_type &&
  170. key->target_type == cur->key.target_type &&
  171. key->target_class == cur->key.target_class &&
  172. (specified & cur->key.specified))
  173. return cur;
  174. if (key->source_type < cur->key.source_type)
  175. break;
  176. if (key->source_type == cur->key.source_type &&
  177. key->target_type < cur->key.target_type)
  178. break;
  179. if (key->source_type == cur->key.source_type &&
  180. key->target_type == cur->key.target_type &&
  181. key->target_class < cur->key.target_class)
  182. break;
  183. }
  184. return NULL;
  185. }
  186. struct avtab_node*
  187. avtab_search_node_next(struct avtab_node *node, int specified)
  188. {
  189. struct avtab_node *cur;
  190. if (!node)
  191. return NULL;
  192. specified &= ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
  193. for (cur = node->next; cur; cur = cur->next) {
  194. if (node->key.source_type == cur->key.source_type &&
  195. node->key.target_type == cur->key.target_type &&
  196. node->key.target_class == cur->key.target_class &&
  197. (specified & cur->key.specified))
  198. return cur;
  199. if (node->key.source_type < cur->key.source_type)
  200. break;
  201. if (node->key.source_type == cur->key.source_type &&
  202. node->key.target_type < cur->key.target_type)
  203. break;
  204. if (node->key.source_type == cur->key.source_type &&
  205. node->key.target_type == cur->key.target_type &&
  206. node->key.target_class < cur->key.target_class)
  207. break;
  208. }
  209. return NULL;
  210. }
  211. void avtab_destroy(struct avtab *h)
  212. {
  213. int i;
  214. struct avtab_node *cur, *temp;
  215. if (!h || !h->htable)
  216. return;
  217. for (i = 0; i < h->nslot; i++) {
  218. cur = h->htable[i];
  219. while (cur) {
  220. temp = cur;
  221. cur = cur->next;
  222. if (temp->key.specified & AVTAB_XPERMS)
  223. kmem_cache_free(avtab_xperms_cachep,
  224. temp->datum.u.xperms);
  225. kmem_cache_free(avtab_node_cachep, temp);
  226. }
  227. h->htable[i] = NULL;
  228. }
  229. kfree(h->htable);
  230. h->htable = NULL;
  231. h->nslot = 0;
  232. h->mask = 0;
  233. }
  234. int avtab_init(struct avtab *h)
  235. {
  236. h->htable = NULL;
  237. h->nel = 0;
  238. return 0;
  239. }
  240. int avtab_alloc(struct avtab *h, u32 nrules)
  241. {
  242. u16 mask = 0;
  243. u32 shift = 0;
  244. u32 work = nrules;
  245. u32 nslot = 0;
  246. if (nrules == 0)
  247. goto avtab_alloc_out;
  248. while (work) {
  249. work = work >> 1;
  250. shift++;
  251. }
  252. if (shift > 2)
  253. shift = shift - 2;
  254. nslot = 1 << shift;
  255. if (nslot > MAX_AVTAB_HASH_BUCKETS)
  256. nslot = MAX_AVTAB_HASH_BUCKETS;
  257. mask = nslot - 1;
  258. h->htable = kcalloc(nslot, sizeof(*(h->htable)), GFP_KERNEL);
  259. if (!h->htable)
  260. return -ENOMEM;
  261. avtab_alloc_out:
  262. h->nel = 0;
  263. h->nslot = nslot;
  264. h->mask = mask;
  265. printk(KERN_DEBUG "SELinux: %d avtab hash slots, %d rules.\n",
  266. h->nslot, nrules);
  267. return 0;
  268. }
  269. void avtab_hash_eval(struct avtab *h, char *tag)
  270. {
  271. int i, chain_len, slots_used, max_chain_len;
  272. unsigned long long chain2_len_sum;
  273. struct avtab_node *cur;
  274. slots_used = 0;
  275. max_chain_len = 0;
  276. chain2_len_sum = 0;
  277. for (i = 0; i < h->nslot; i++) {
  278. cur = h->htable[i];
  279. if (cur) {
  280. slots_used++;
  281. chain_len = 0;
  282. while (cur) {
  283. chain_len++;
  284. cur = cur->next;
  285. }
  286. if (chain_len > max_chain_len)
  287. max_chain_len = chain_len;
  288. chain2_len_sum += chain_len * chain_len;
  289. }
  290. }
  291. printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
  292. "longest chain length %d sum of chain length^2 %llu\n",
  293. tag, h->nel, slots_used, h->nslot, max_chain_len,
  294. chain2_len_sum);
  295. }
  296. /*
  297. * extended permissions compatibility. Make ToT Android kernels compatible
  298. * with Android M releases
  299. */
  300. #define AVTAB_OPTYPE_ALLOWED 0x1000
  301. #define AVTAB_OPTYPE_AUDITALLOW 0x2000
  302. #define AVTAB_OPTYPE_DONTAUDIT 0x4000
  303. #define AVTAB_OPTYPE (AVTAB_OPTYPE_ALLOWED | \
  304. AVTAB_OPTYPE_AUDITALLOW | \
  305. AVTAB_OPTYPE_DONTAUDIT)
  306. #define AVTAB_XPERMS_OPTYPE 4
  307. #define avtab_xperms_to_optype(x) (x << AVTAB_XPERMS_OPTYPE)
  308. #define avtab_optype_to_xperms(x) (x >> AVTAB_XPERMS_OPTYPE)
  309. static unsigned int avtab_android_m_compat;
  310. static void avtab_android_m_compat_set(void)
  311. {
  312. if (!avtab_android_m_compat) {
  313. pr_info("SELinux: Android master kernel running Android"
  314. " M policy in compatibility mode.\n");
  315. avtab_android_m_compat = 1;
  316. }
  317. }
  318. static uint16_t spec_order[] = {
  319. AVTAB_ALLOWED,
  320. AVTAB_AUDITDENY,
  321. AVTAB_AUDITALLOW,
  322. AVTAB_TRANSITION,
  323. AVTAB_CHANGE,
  324. AVTAB_MEMBER,
  325. AVTAB_XPERMS_ALLOWED,
  326. AVTAB_XPERMS_AUDITALLOW,
  327. AVTAB_XPERMS_DONTAUDIT
  328. };
  329. int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
  330. int (*insertf)(struct avtab *a, struct avtab_key *k,
  331. struct avtab_datum *d, void *p),
  332. void *p)
  333. {
  334. __le16 buf16[4];
  335. u16 enabled;
  336. u32 items, items2, val, vers = pol->policyvers;
  337. struct avtab_key key;
  338. struct avtab_datum datum;
  339. struct avtab_extended_perms xperms;
  340. __le32 buf32[ARRAY_SIZE(xperms.perms.p)];
  341. unsigned int android_m_compat_optype = 0;
  342. int i, rc;
  343. unsigned set;
  344. memset(&key, 0, sizeof(struct avtab_key));
  345. memset(&datum, 0, sizeof(struct avtab_datum));
  346. if (vers < POLICYDB_VERSION_AVTAB) {
  347. rc = next_entry(buf32, fp, sizeof(u32));
  348. if (rc) {
  349. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  350. return rc;
  351. }
  352. items2 = le32_to_cpu(buf32[0]);
  353. if (items2 > ARRAY_SIZE(buf32)) {
  354. printk(KERN_ERR "SELinux: avtab: entry overflow\n");
  355. return -EINVAL;
  356. }
  357. rc = next_entry(buf32, fp, sizeof(u32)*items2);
  358. if (rc) {
  359. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  360. return rc;
  361. }
  362. items = 0;
  363. val = le32_to_cpu(buf32[items++]);
  364. key.source_type = (u16)val;
  365. if (key.source_type != val) {
  366. printk(KERN_ERR "SELinux: avtab: truncated source type\n");
  367. return -EINVAL;
  368. }
  369. val = le32_to_cpu(buf32[items++]);
  370. key.target_type = (u16)val;
  371. if (key.target_type != val) {
  372. printk(KERN_ERR "SELinux: avtab: truncated target type\n");
  373. return -EINVAL;
  374. }
  375. val = le32_to_cpu(buf32[items++]);
  376. key.target_class = (u16)val;
  377. if (key.target_class != val) {
  378. printk(KERN_ERR "SELinux: avtab: truncated target class\n");
  379. return -EINVAL;
  380. }
  381. val = le32_to_cpu(buf32[items++]);
  382. enabled = (val & AVTAB_ENABLED_OLD) ? AVTAB_ENABLED : 0;
  383. if (!(val & (AVTAB_AV | AVTAB_TYPE))) {
  384. printk(KERN_ERR "SELinux: avtab: null entry\n");
  385. return -EINVAL;
  386. }
  387. if ((val & AVTAB_AV) &&
  388. (val & AVTAB_TYPE)) {
  389. printk(KERN_ERR "SELinux: avtab: entry has both access vectors and types\n");
  390. return -EINVAL;
  391. }
  392. if (val & AVTAB_XPERMS) {
  393. printk(KERN_ERR "SELinux: avtab: entry has extended permissions\n");
  394. return -EINVAL;
  395. }
  396. for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
  397. if (val & spec_order[i]) {
  398. key.specified = spec_order[i] | enabled;
  399. datum.u.data = le32_to_cpu(buf32[items++]);
  400. rc = insertf(a, &key, &datum, p);
  401. if (rc)
  402. return rc;
  403. }
  404. }
  405. if (items != items2) {
  406. printk(KERN_ERR "SELinux: avtab: entry only had %d items, expected %d\n", items2, items);
  407. return -EINVAL;
  408. }
  409. return 0;
  410. }
  411. rc = next_entry(buf16, fp, sizeof(u16)*4);
  412. if (rc) {
  413. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  414. return rc;
  415. }
  416. items = 0;
  417. key.source_type = le16_to_cpu(buf16[items++]);
  418. key.target_type = le16_to_cpu(buf16[items++]);
  419. key.target_class = le16_to_cpu(buf16[items++]);
  420. key.specified = le16_to_cpu(buf16[items++]);
  421. if ((key.specified & AVTAB_OPTYPE) &&
  422. (vers == POLICYDB_VERSION_XPERMS_IOCTL)) {
  423. key.specified = avtab_optype_to_xperms(key.specified);
  424. android_m_compat_optype = 1;
  425. avtab_android_m_compat_set();
  426. }
  427. if (!policydb_type_isvalid(pol, key.source_type) ||
  428. !policydb_type_isvalid(pol, key.target_type) ||
  429. !policydb_class_isvalid(pol, key.target_class)) {
  430. printk(KERN_ERR "SELinux: avtab: invalid type or class\n");
  431. return -EINVAL;
  432. }
  433. set = 0;
  434. for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
  435. if (key.specified & spec_order[i])
  436. set++;
  437. }
  438. if (!set || set > 1) {
  439. printk(KERN_ERR "SELinux: avtab: more than one specifier\n");
  440. return -EINVAL;
  441. }
  442. if ((vers < POLICYDB_VERSION_XPERMS_IOCTL) &&
  443. (key.specified & AVTAB_XPERMS)) {
  444. printk(KERN_ERR "SELinux: avtab: policy version %u does not "
  445. "support extended permissions rules and one "
  446. "was specified\n", vers);
  447. return -EINVAL;
  448. } else if (key.specified & AVTAB_XPERMS) {
  449. memset(&xperms, 0, sizeof(struct avtab_extended_perms));
  450. rc = next_entry(&xperms.specified, fp, sizeof(u8));
  451. if (rc) {
  452. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  453. return rc;
  454. }
  455. if (avtab_android_m_compat ||
  456. ((xperms.specified != AVTAB_XPERMS_IOCTLFUNCTION) &&
  457. (xperms.specified != AVTAB_XPERMS_IOCTLDRIVER) &&
  458. (vers == POLICYDB_VERSION_XPERMS_IOCTL))) {
  459. xperms.driver = xperms.specified;
  460. if (android_m_compat_optype)
  461. xperms.specified = AVTAB_XPERMS_IOCTLDRIVER;
  462. else
  463. xperms.specified = AVTAB_XPERMS_IOCTLFUNCTION;
  464. avtab_android_m_compat_set();
  465. } else {
  466. rc = next_entry(&xperms.driver, fp, sizeof(u8));
  467. if (rc) {
  468. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  469. return rc;
  470. }
  471. }
  472. rc = next_entry(buf32, fp, sizeof(u32)*ARRAY_SIZE(xperms.perms.p));
  473. if (rc) {
  474. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  475. return rc;
  476. }
  477. for (i = 0; i < ARRAY_SIZE(xperms.perms.p); i++)
  478. xperms.perms.p[i] = le32_to_cpu(buf32[i]);
  479. datum.u.xperms = &xperms;
  480. } else {
  481. rc = next_entry(buf32, fp, sizeof(u32));
  482. if (rc) {
  483. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  484. return rc;
  485. }
  486. datum.u.data = le32_to_cpu(*buf32);
  487. }
  488. if ((key.specified & AVTAB_TYPE) &&
  489. !policydb_type_isvalid(pol, datum.u.data)) {
  490. printk(KERN_ERR "SELinux: avtab: invalid type\n");
  491. return -EINVAL;
  492. }
  493. return insertf(a, &key, &datum, p);
  494. }
  495. static int avtab_insertf(struct avtab *a, struct avtab_key *k,
  496. struct avtab_datum *d, void *p)
  497. {
  498. return avtab_insert(a, k, d);
  499. }
  500. int avtab_read(struct avtab *a, void *fp, struct policydb *pol)
  501. {
  502. int rc;
  503. __le32 buf[1];
  504. u32 nel, i;
  505. rc = next_entry(buf, fp, sizeof(u32));
  506. if (rc < 0) {
  507. printk(KERN_ERR "SELinux: avtab: truncated table\n");
  508. goto bad;
  509. }
  510. nel = le32_to_cpu(buf[0]);
  511. if (!nel) {
  512. printk(KERN_ERR "SELinux: avtab: table is empty\n");
  513. rc = -EINVAL;
  514. goto bad;
  515. }
  516. rc = avtab_alloc(a, nel);
  517. if (rc)
  518. goto bad;
  519. for (i = 0; i < nel; i++) {
  520. rc = avtab_read_item(a, fp, pol, avtab_insertf, NULL);
  521. if (rc) {
  522. if (rc == -ENOMEM)
  523. printk(KERN_ERR "SELinux: avtab: out of memory\n");
  524. else if (rc == -EEXIST)
  525. printk(KERN_ERR "SELinux: avtab: duplicate entry\n");
  526. goto bad;
  527. }
  528. }
  529. rc = 0;
  530. out:
  531. return rc;
  532. bad:
  533. avtab_destroy(a);
  534. goto out;
  535. }
  536. int avtab_write_item(struct policydb *p, struct avtab_node *cur, void *fp)
  537. {
  538. __le16 buf16[4];
  539. __le32 buf32[ARRAY_SIZE(cur->datum.u.xperms->perms.p)];
  540. int rc;
  541. unsigned int i;
  542. buf16[0] = cpu_to_le16(cur->key.source_type);
  543. buf16[1] = cpu_to_le16(cur->key.target_type);
  544. buf16[2] = cpu_to_le16(cur->key.target_class);
  545. if (avtab_android_m_compat && (cur->key.specified & AVTAB_XPERMS) &&
  546. (cur->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER))
  547. buf16[3] = cpu_to_le16(avtab_xperms_to_optype(cur->key.specified));
  548. else
  549. buf16[3] = cpu_to_le16(cur->key.specified);
  550. rc = put_entry(buf16, sizeof(u16), 4, fp);
  551. if (rc)
  552. return rc;
  553. if (cur->key.specified & AVTAB_XPERMS) {
  554. if (avtab_android_m_compat == 0) {
  555. rc = put_entry(&cur->datum.u.xperms->specified,
  556. sizeof(u8), 1, fp);
  557. if (rc)
  558. return rc;
  559. }
  560. rc = put_entry(&cur->datum.u.xperms->driver, sizeof(u8), 1, fp);
  561. if (rc)
  562. return rc;
  563. for (i = 0; i < ARRAY_SIZE(cur->datum.u.xperms->perms.p); i++)
  564. buf32[i] = cpu_to_le32(cur->datum.u.xperms->perms.p[i]);
  565. rc = put_entry(buf32, sizeof(u32),
  566. ARRAY_SIZE(cur->datum.u.xperms->perms.p), fp);
  567. } else {
  568. buf32[0] = cpu_to_le32(cur->datum.u.data);
  569. rc = put_entry(buf32, sizeof(u32), 1, fp);
  570. }
  571. if (rc)
  572. return rc;
  573. return 0;
  574. }
  575. int avtab_write(struct policydb *p, struct avtab *a, void *fp)
  576. {
  577. unsigned int i;
  578. int rc = 0;
  579. struct avtab_node *cur;
  580. __le32 buf[1];
  581. buf[0] = cpu_to_le32(a->nel);
  582. rc = put_entry(buf, sizeof(u32), 1, fp);
  583. if (rc)
  584. return rc;
  585. for (i = 0; i < a->nslot; i++) {
  586. for (cur = a->htable[i]; cur; cur = cur->next) {
  587. rc = avtab_write_item(p, cur, fp);
  588. if (rc)
  589. return rc;
  590. }
  591. }
  592. return rc;
  593. }
  594. void avtab_cache_init(void)
  595. {
  596. avtab_node_cachep = kmem_cache_create("avtab_node",
  597. sizeof(struct avtab_node),
  598. 0, SLAB_PANIC, NULL);
  599. avtab_xperms_cachep = kmem_cache_create("avtab_extended_perms",
  600. sizeof(struct avtab_extended_perms),
  601. 0, SLAB_PANIC, NULL);
  602. }
  603. void avtab_cache_destroy(void)
  604. {
  605. kmem_cache_destroy(avtab_node_cachep);
  606. kmem_cache_destroy(avtab_xperms_cachep);
  607. }