avtab.c 17 KB

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