label.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor label definitions
  5. *
  6. * Copyright 2017 Canonical Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation, version 2 of the
  11. * License.
  12. */
  13. #include <linux/audit.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/sort.h>
  16. #include "include/apparmor.h"
  17. #include "include/cred.h"
  18. #include "include/label.h"
  19. #include "include/policy.h"
  20. #include "include/secid.h"
  21. /*
  22. * the aa_label represents the set of profiles confining an object
  23. *
  24. * Labels maintain a reference count to the set of pointers they reference
  25. * Labels are ref counted by
  26. * tasks and object via the security field/security context off the field
  27. * code - will take a ref count on a label if it needs the label
  28. * beyond what is possible with an rcu_read_lock.
  29. * profiles - each profile is a label
  30. * secids - a pinned secid will keep a refcount of the label it is
  31. * referencing
  32. * objects - inode, files, sockets, ...
  33. *
  34. * Labels are not ref counted by the label set, so they maybe removed and
  35. * freed when no longer in use.
  36. *
  37. */
  38. #define PROXY_POISON 97
  39. #define LABEL_POISON 100
  40. static void free_proxy(struct aa_proxy *proxy)
  41. {
  42. if (proxy) {
  43. /* p->label will not updated any more as p is dead */
  44. aa_put_label(rcu_dereference_protected(proxy->label, true));
  45. memset(proxy, 0, sizeof(*proxy));
  46. RCU_INIT_POINTER(proxy->label, (struct aa_label *)PROXY_POISON);
  47. kfree(proxy);
  48. }
  49. }
  50. void aa_proxy_kref(struct kref *kref)
  51. {
  52. struct aa_proxy *proxy = container_of(kref, struct aa_proxy, count);
  53. free_proxy(proxy);
  54. }
  55. struct aa_proxy *aa_alloc_proxy(struct aa_label *label, gfp_t gfp)
  56. {
  57. struct aa_proxy *new;
  58. new = kzalloc(sizeof(struct aa_proxy), gfp);
  59. if (new) {
  60. kref_init(&new->count);
  61. rcu_assign_pointer(new->label, aa_get_label(label));
  62. }
  63. return new;
  64. }
  65. /* requires profile list write lock held */
  66. void __aa_proxy_redirect(struct aa_label *orig, struct aa_label *new)
  67. {
  68. struct aa_label *tmp;
  69. AA_BUG(!orig);
  70. AA_BUG(!new);
  71. lockdep_assert_held_exclusive(&labels_set(orig)->lock);
  72. tmp = rcu_dereference_protected(orig->proxy->label,
  73. &labels_ns(orig)->lock);
  74. rcu_assign_pointer(orig->proxy->label, aa_get_label(new));
  75. orig->flags |= FLAG_STALE;
  76. aa_put_label(tmp);
  77. }
  78. static void __proxy_share(struct aa_label *old, struct aa_label *new)
  79. {
  80. struct aa_proxy *proxy = new->proxy;
  81. new->proxy = aa_get_proxy(old->proxy);
  82. __aa_proxy_redirect(old, new);
  83. aa_put_proxy(proxy);
  84. }
  85. /**
  86. * ns_cmp - compare ns for label set ordering
  87. * @a: ns to compare (NOT NULL)
  88. * @b: ns to compare (NOT NULL)
  89. *
  90. * Returns: <0 if a < b
  91. * ==0 if a == b
  92. * >0 if a > b
  93. */
  94. static int ns_cmp(struct aa_ns *a, struct aa_ns *b)
  95. {
  96. int res;
  97. AA_BUG(!a);
  98. AA_BUG(!b);
  99. AA_BUG(!a->base.hname);
  100. AA_BUG(!b->base.hname);
  101. if (a == b)
  102. return 0;
  103. res = a->level - b->level;
  104. if (res)
  105. return res;
  106. return strcmp(a->base.hname, b->base.hname);
  107. }
  108. /**
  109. * profile_cmp - profile comparison for set ordering
  110. * @a: profile to compare (NOT NULL)
  111. * @b: profile to compare (NOT NULL)
  112. *
  113. * Returns: <0 if a < b
  114. * ==0 if a == b
  115. * >0 if a > b
  116. */
  117. static int profile_cmp(struct aa_profile *a, struct aa_profile *b)
  118. {
  119. int res;
  120. AA_BUG(!a);
  121. AA_BUG(!b);
  122. AA_BUG(!a->ns);
  123. AA_BUG(!b->ns);
  124. AA_BUG(!a->base.hname);
  125. AA_BUG(!b->base.hname);
  126. if (a == b || a->base.hname == b->base.hname)
  127. return 0;
  128. res = ns_cmp(a->ns, b->ns);
  129. if (res)
  130. return res;
  131. return strcmp(a->base.hname, b->base.hname);
  132. }
  133. /**
  134. * vec_cmp - label comparison for set ordering
  135. * @a: label to compare (NOT NULL)
  136. * @vec: vector of profiles to compare (NOT NULL)
  137. * @n: length of @vec
  138. *
  139. * Returns: <0 if a < vec
  140. * ==0 if a == vec
  141. * >0 if a > vec
  142. */
  143. static int vec_cmp(struct aa_profile **a, int an, struct aa_profile **b, int bn)
  144. {
  145. int i;
  146. AA_BUG(!a);
  147. AA_BUG(!*a);
  148. AA_BUG(!b);
  149. AA_BUG(!*b);
  150. AA_BUG(an <= 0);
  151. AA_BUG(bn <= 0);
  152. for (i = 0; i < an && i < bn; i++) {
  153. int res = profile_cmp(a[i], b[i]);
  154. if (res != 0)
  155. return res;
  156. }
  157. return an - bn;
  158. }
  159. static bool vec_is_stale(struct aa_profile **vec, int n)
  160. {
  161. int i;
  162. AA_BUG(!vec);
  163. for (i = 0; i < n; i++) {
  164. if (profile_is_stale(vec[i]))
  165. return true;
  166. }
  167. return false;
  168. }
  169. static bool vec_unconfined(struct aa_profile **vec, int n)
  170. {
  171. int i;
  172. AA_BUG(!vec);
  173. for (i = 0; i < n; i++) {
  174. if (!profile_unconfined(vec[i]))
  175. return false;
  176. }
  177. return true;
  178. }
  179. static int sort_cmp(const void *a, const void *b)
  180. {
  181. return profile_cmp(*(struct aa_profile **)a, *(struct aa_profile **)b);
  182. }
  183. /*
  184. * assumes vec is sorted
  185. * Assumes @vec has null terminator at vec[n], and will null terminate
  186. * vec[n - dups]
  187. */
  188. static inline int unique(struct aa_profile **vec, int n)
  189. {
  190. int i, pos, dups = 0;
  191. AA_BUG(n < 1);
  192. AA_BUG(!vec);
  193. pos = 0;
  194. for (i = 1; i < n; i++) {
  195. int res = profile_cmp(vec[pos], vec[i]);
  196. AA_BUG(res > 0, "vec not sorted");
  197. if (res == 0) {
  198. /* drop duplicate */
  199. aa_put_profile(vec[i]);
  200. dups++;
  201. continue;
  202. }
  203. pos++;
  204. if (dups)
  205. vec[pos] = vec[i];
  206. }
  207. AA_BUG(dups < 0);
  208. return dups;
  209. }
  210. /**
  211. * aa_vec_unique - canonical sort and unique a list of profiles
  212. * @n: number of refcounted profiles in the list (@n > 0)
  213. * @vec: list of profiles to sort and merge
  214. *
  215. * Returns: the number of duplicates eliminated == references put
  216. *
  217. * If @flags & VEC_FLAG_TERMINATE @vec has null terminator at vec[n], and will
  218. * null terminate vec[n - dups]
  219. */
  220. int aa_vec_unique(struct aa_profile **vec, int n, int flags)
  221. {
  222. int i, dups = 0;
  223. AA_BUG(n < 1);
  224. AA_BUG(!vec);
  225. /* vecs are usually small and inorder, have a fallback for larger */
  226. if (n > 8) {
  227. sort(vec, n, sizeof(struct aa_profile *), sort_cmp, NULL);
  228. dups = unique(vec, n);
  229. goto out;
  230. }
  231. /* insertion sort + unique in one */
  232. for (i = 1; i < n; i++) {
  233. struct aa_profile *tmp = vec[i];
  234. int pos, j;
  235. for (pos = i - 1 - dups; pos >= 0; pos--) {
  236. int res = profile_cmp(vec[pos], tmp);
  237. if (res == 0) {
  238. /* drop duplicate entry */
  239. aa_put_profile(tmp);
  240. dups++;
  241. goto continue_outer;
  242. } else if (res < 0)
  243. break;
  244. }
  245. /* pos is at entry < tmp, or index -1. Set to insert pos */
  246. pos++;
  247. for (j = i - dups; j > pos; j--)
  248. vec[j] = vec[j - 1];
  249. vec[pos] = tmp;
  250. continue_outer:
  251. ;
  252. }
  253. AA_BUG(dups < 0);
  254. out:
  255. if (flags & VEC_FLAG_TERMINATE)
  256. vec[n - dups] = NULL;
  257. return dups;
  258. }
  259. static void label_destroy(struct aa_label *label)
  260. {
  261. struct aa_label *tmp;
  262. AA_BUG(!label);
  263. if (!label_isprofile(label)) {
  264. struct aa_profile *profile;
  265. struct label_it i;
  266. aa_put_str(label->hname);
  267. label_for_each(i, label, profile) {
  268. aa_put_profile(profile);
  269. label->vec[i.i] = (struct aa_profile *)
  270. (LABEL_POISON + (long) i.i);
  271. }
  272. }
  273. if (rcu_dereference_protected(label->proxy->label, true) == label)
  274. rcu_assign_pointer(label->proxy->label, NULL);
  275. aa_free_secid(label->secid);
  276. tmp = rcu_dereference_protected(label->proxy->label, true);
  277. if (tmp == label)
  278. rcu_assign_pointer(label->proxy->label, NULL);
  279. aa_put_proxy(label->proxy);
  280. label->proxy = (struct aa_proxy *) PROXY_POISON + 1;
  281. }
  282. void aa_label_free(struct aa_label *label)
  283. {
  284. if (!label)
  285. return;
  286. label_destroy(label);
  287. kfree(label);
  288. }
  289. static void label_free_switch(struct aa_label *label)
  290. {
  291. if (label->flags & FLAG_NS_COUNT)
  292. aa_free_ns(labels_ns(label));
  293. else if (label_isprofile(label))
  294. aa_free_profile(labels_profile(label));
  295. else
  296. aa_label_free(label);
  297. }
  298. static void label_free_rcu(struct rcu_head *head)
  299. {
  300. struct aa_label *label = container_of(head, struct aa_label, rcu);
  301. if (label->flags & FLAG_IN_TREE)
  302. (void) aa_label_remove(label);
  303. label_free_switch(label);
  304. }
  305. void aa_label_kref(struct kref *kref)
  306. {
  307. struct aa_label *label = container_of(kref, struct aa_label, count);
  308. struct aa_ns *ns = labels_ns(label);
  309. if (!ns) {
  310. /* never live, no rcu callback needed, just using the fn */
  311. label_free_switch(label);
  312. return;
  313. }
  314. /* TODO: update labels_profile macro so it works here */
  315. AA_BUG(label_isprofile(label) &&
  316. on_list_rcu(&label->vec[0]->base.profiles));
  317. AA_BUG(label_isprofile(label) &&
  318. on_list_rcu(&label->vec[0]->base.list));
  319. /* TODO: if compound label and not stale add to reclaim cache */
  320. call_rcu(&label->rcu, label_free_rcu);
  321. }
  322. static void label_free_or_put_new(struct aa_label *label, struct aa_label *new)
  323. {
  324. if (label != new)
  325. /* need to free directly to break circular ref with proxy */
  326. aa_label_free(new);
  327. else
  328. aa_put_label(new);
  329. }
  330. bool aa_label_init(struct aa_label *label, int size, gfp_t gfp)
  331. {
  332. AA_BUG(!label);
  333. AA_BUG(size < 1);
  334. if (aa_alloc_secid(label, gfp) < 0)
  335. return false;
  336. label->size = size; /* doesn't include null */
  337. label->vec[size] = NULL; /* null terminate */
  338. kref_init(&label->count);
  339. RB_CLEAR_NODE(&label->node);
  340. return true;
  341. }
  342. /**
  343. * aa_label_alloc - allocate a label with a profile vector of @size length
  344. * @size: size of profile vector in the label
  345. * @proxy: proxy to use OR null if to allocate a new one
  346. * @gfp: memory allocation type
  347. *
  348. * Returns: new label
  349. * else NULL if failed
  350. */
  351. struct aa_label *aa_label_alloc(int size, struct aa_proxy *proxy, gfp_t gfp)
  352. {
  353. struct aa_label *new;
  354. AA_BUG(size < 1);
  355. /* + 1 for null terminator entry on vec */
  356. new = kzalloc(sizeof(*new) + sizeof(struct aa_profile *) * (size + 1),
  357. gfp);
  358. AA_DEBUG("%s (%p)\n", __func__, new);
  359. if (!new)
  360. goto fail;
  361. if (!aa_label_init(new, size, gfp))
  362. goto fail;
  363. if (!proxy) {
  364. proxy = aa_alloc_proxy(new, gfp);
  365. if (!proxy)
  366. goto fail;
  367. } else
  368. aa_get_proxy(proxy);
  369. /* just set new's proxy, don't redirect proxy here if it was passed in*/
  370. new->proxy = proxy;
  371. return new;
  372. fail:
  373. kfree(new);
  374. return NULL;
  375. }
  376. /**
  377. * label_cmp - label comparison for set ordering
  378. * @a: label to compare (NOT NULL)
  379. * @b: label to compare (NOT NULL)
  380. *
  381. * Returns: <0 if a < b
  382. * ==0 if a == b
  383. * >0 if a > b
  384. */
  385. static int label_cmp(struct aa_label *a, struct aa_label *b)
  386. {
  387. AA_BUG(!b);
  388. if (a == b)
  389. return 0;
  390. return vec_cmp(a->vec, a->size, b->vec, b->size);
  391. }
  392. /* helper fn for label_for_each_confined */
  393. int aa_label_next_confined(struct aa_label *label, int i)
  394. {
  395. AA_BUG(!label);
  396. AA_BUG(i < 0);
  397. for (; i < label->size; i++) {
  398. if (!profile_unconfined(label->vec[i]))
  399. return i;
  400. }
  401. return i;
  402. }
  403. /**
  404. * aa_label_next_not_in_set - return the next profile of @sub not in @set
  405. * @I: label iterator
  406. * @set: label to test against
  407. * @sub: label to if is subset of @set
  408. *
  409. * Returns: profile in @sub that is not in @set, with iterator set pos after
  410. * else NULL if @sub is a subset of @set
  411. */
  412. struct aa_profile *__aa_label_next_not_in_set(struct label_it *I,
  413. struct aa_label *set,
  414. struct aa_label *sub)
  415. {
  416. AA_BUG(!set);
  417. AA_BUG(!I);
  418. AA_BUG(I->i < 0);
  419. AA_BUG(I->i > set->size);
  420. AA_BUG(!sub);
  421. AA_BUG(I->j < 0);
  422. AA_BUG(I->j > sub->size);
  423. while (I->j < sub->size && I->i < set->size) {
  424. int res = profile_cmp(sub->vec[I->j], set->vec[I->i]);
  425. if (res == 0) {
  426. (I->j)++;
  427. (I->i)++;
  428. } else if (res > 0)
  429. (I->i)++;
  430. else
  431. return sub->vec[(I->j)++];
  432. }
  433. if (I->j < sub->size)
  434. return sub->vec[(I->j)++];
  435. return NULL;
  436. }
  437. /**
  438. * aa_label_is_subset - test if @sub is a subset of @set
  439. * @set: label to test against
  440. * @sub: label to test if is subset of @set
  441. *
  442. * Returns: true if @sub is subset of @set
  443. * else false
  444. */
  445. bool aa_label_is_subset(struct aa_label *set, struct aa_label *sub)
  446. {
  447. struct label_it i = { };
  448. AA_BUG(!set);
  449. AA_BUG(!sub);
  450. if (sub == set)
  451. return true;
  452. return __aa_label_next_not_in_set(&i, set, sub) == NULL;
  453. }
  454. /**
  455. * __label_remove - remove @label from the label set
  456. * @l: label to remove
  457. * @new: label to redirect to
  458. *
  459. * Requires: labels_set(@label)->lock write_lock
  460. * Returns: true if the label was in the tree and removed
  461. */
  462. static bool __label_remove(struct aa_label *label, struct aa_label *new)
  463. {
  464. struct aa_labelset *ls = labels_set(label);
  465. AA_BUG(!ls);
  466. AA_BUG(!label);
  467. lockdep_assert_held_exclusive(&ls->lock);
  468. if (new)
  469. __aa_proxy_redirect(label, new);
  470. if (!label_is_stale(label))
  471. __label_make_stale(label);
  472. if (label->flags & FLAG_IN_TREE) {
  473. rb_erase(&label->node, &ls->root);
  474. label->flags &= ~FLAG_IN_TREE;
  475. return true;
  476. }
  477. return false;
  478. }
  479. /**
  480. * __label_replace - replace @old with @new in label set
  481. * @old: label to remove from label set
  482. * @new: label to replace @old with
  483. *
  484. * Requires: labels_set(@old)->lock write_lock
  485. * valid ref count be held on @new
  486. * Returns: true if @old was in set and replaced by @new
  487. *
  488. * Note: current implementation requires label set be order in such a way
  489. * that @new directly replaces @old position in the set (ie.
  490. * using pointer comparison of the label address would not work)
  491. */
  492. static bool __label_replace(struct aa_label *old, struct aa_label *new)
  493. {
  494. struct aa_labelset *ls = labels_set(old);
  495. AA_BUG(!ls);
  496. AA_BUG(!old);
  497. AA_BUG(!new);
  498. lockdep_assert_held_exclusive(&ls->lock);
  499. AA_BUG(new->flags & FLAG_IN_TREE);
  500. if (!label_is_stale(old))
  501. __label_make_stale(old);
  502. if (old->flags & FLAG_IN_TREE) {
  503. rb_replace_node(&old->node, &new->node, &ls->root);
  504. old->flags &= ~FLAG_IN_TREE;
  505. new->flags |= FLAG_IN_TREE;
  506. return true;
  507. }
  508. return false;
  509. }
  510. /**
  511. * __label_insert - attempt to insert @l into a label set
  512. * @ls: set of labels to insert @l into (NOT NULL)
  513. * @label: new label to insert (NOT NULL)
  514. * @replace: whether insertion should replace existing entry that is not stale
  515. *
  516. * Requires: @ls->lock
  517. * caller to hold a valid ref on l
  518. * if @replace is true l has a preallocated proxy associated
  519. * Returns: @l if successful in inserting @l - with additional refcount
  520. * else ref counted equivalent label that is already in the set,
  521. * the else condition only happens if @replace is false
  522. */
  523. static struct aa_label *__label_insert(struct aa_labelset *ls,
  524. struct aa_label *label, bool replace)
  525. {
  526. struct rb_node **new, *parent = NULL;
  527. AA_BUG(!ls);
  528. AA_BUG(!label);
  529. AA_BUG(labels_set(label) != ls);
  530. lockdep_assert_held_exclusive(&ls->lock);
  531. AA_BUG(label->flags & FLAG_IN_TREE);
  532. /* Figure out where to put new node */
  533. new = &ls->root.rb_node;
  534. while (*new) {
  535. struct aa_label *this = rb_entry(*new, struct aa_label, node);
  536. int result = label_cmp(label, this);
  537. parent = *new;
  538. if (result == 0) {
  539. /* !__aa_get_label means queued for destruction,
  540. * so replace in place, however the label has
  541. * died before the replacement so do not share
  542. * the proxy
  543. */
  544. if (!replace && !label_is_stale(this)) {
  545. if (__aa_get_label(this))
  546. return this;
  547. } else
  548. __proxy_share(this, label);
  549. AA_BUG(!__label_replace(this, label));
  550. return aa_get_label(label);
  551. } else if (result < 0)
  552. new = &((*new)->rb_left);
  553. else /* (result > 0) */
  554. new = &((*new)->rb_right);
  555. }
  556. /* Add new node and rebalance tree. */
  557. rb_link_node(&label->node, parent, new);
  558. rb_insert_color(&label->node, &ls->root);
  559. label->flags |= FLAG_IN_TREE;
  560. return aa_get_label(label);
  561. }
  562. /**
  563. * __vec_find - find label that matches @vec in label set
  564. * @vec: vec of profiles to find matching label for (NOT NULL)
  565. * @n: length of @vec
  566. *
  567. * Requires: @vec_labelset(vec) lock held
  568. * caller to hold a valid ref on l
  569. *
  570. * Returns: ref counted @label if matching label is in tree
  571. * ref counted label that is equiv to @l in tree
  572. * else NULL if @vec equiv is not in tree
  573. */
  574. static struct aa_label *__vec_find(struct aa_profile **vec, int n)
  575. {
  576. struct rb_node *node;
  577. AA_BUG(!vec);
  578. AA_BUG(!*vec);
  579. AA_BUG(n <= 0);
  580. node = vec_labelset(vec, n)->root.rb_node;
  581. while (node) {
  582. struct aa_label *this = rb_entry(node, struct aa_label, node);
  583. int result = vec_cmp(this->vec, this->size, vec, n);
  584. if (result > 0)
  585. node = node->rb_left;
  586. else if (result < 0)
  587. node = node->rb_right;
  588. else
  589. return __aa_get_label(this);
  590. }
  591. return NULL;
  592. }
  593. /**
  594. * __label_find - find label @label in label set
  595. * @label: label to find (NOT NULL)
  596. *
  597. * Requires: labels_set(@label)->lock held
  598. * caller to hold a valid ref on l
  599. *
  600. * Returns: ref counted @label if @label is in tree OR
  601. * ref counted label that is equiv to @label in tree
  602. * else NULL if @label or equiv is not in tree
  603. */
  604. static struct aa_label *__label_find(struct aa_label *label)
  605. {
  606. AA_BUG(!label);
  607. return __vec_find(label->vec, label->size);
  608. }
  609. /**
  610. * aa_label_remove - remove a label from the labelset
  611. * @label: label to remove
  612. *
  613. * Returns: true if @label was removed from the tree
  614. * else @label was not in tree so it could not be removed
  615. */
  616. bool aa_label_remove(struct aa_label *label)
  617. {
  618. struct aa_labelset *ls = labels_set(label);
  619. unsigned long flags;
  620. bool res;
  621. AA_BUG(!ls);
  622. write_lock_irqsave(&ls->lock, flags);
  623. res = __label_remove(label, ns_unconfined(labels_ns(label)));
  624. write_unlock_irqrestore(&ls->lock, flags);
  625. return res;
  626. }
  627. /**
  628. * aa_label_replace - replace a label @old with a new version @new
  629. * @old: label to replace
  630. * @new: label replacing @old
  631. *
  632. * Returns: true if @old was in tree and replaced
  633. * else @old was not in tree, and @new was not inserted
  634. */
  635. bool aa_label_replace(struct aa_label *old, struct aa_label *new)
  636. {
  637. unsigned long flags;
  638. bool res;
  639. if (name_is_shared(old, new) && labels_ns(old) == labels_ns(new)) {
  640. write_lock_irqsave(&labels_set(old)->lock, flags);
  641. if (old->proxy != new->proxy)
  642. __proxy_share(old, new);
  643. else
  644. __aa_proxy_redirect(old, new);
  645. res = __label_replace(old, new);
  646. write_unlock_irqrestore(&labels_set(old)->lock, flags);
  647. } else {
  648. struct aa_label *l;
  649. struct aa_labelset *ls = labels_set(old);
  650. write_lock_irqsave(&ls->lock, flags);
  651. res = __label_remove(old, new);
  652. if (labels_ns(old) != labels_ns(new)) {
  653. write_unlock_irqrestore(&ls->lock, flags);
  654. ls = labels_set(new);
  655. write_lock_irqsave(&ls->lock, flags);
  656. }
  657. l = __label_insert(ls, new, true);
  658. res = (l == new);
  659. write_unlock_irqrestore(&ls->lock, flags);
  660. aa_put_label(l);
  661. }
  662. return res;
  663. }
  664. /**
  665. * vec_find - find label @l in label set
  666. * @vec: array of profiles to find equiv label for (NOT NULL)
  667. * @n: length of @vec
  668. *
  669. * Returns: refcounted label if @vec equiv is in tree
  670. * else NULL if @vec equiv is not in tree
  671. */
  672. static struct aa_label *vec_find(struct aa_profile **vec, int n)
  673. {
  674. struct aa_labelset *ls;
  675. struct aa_label *label;
  676. unsigned long flags;
  677. AA_BUG(!vec);
  678. AA_BUG(!*vec);
  679. AA_BUG(n <= 0);
  680. ls = vec_labelset(vec, n);
  681. read_lock_irqsave(&ls->lock, flags);
  682. label = __vec_find(vec, n);
  683. read_unlock_irqrestore(&ls->lock, flags);
  684. return label;
  685. }
  686. /* requires sort and merge done first */
  687. static struct aa_label *vec_create_and_insert_label(struct aa_profile **vec,
  688. int len, gfp_t gfp)
  689. {
  690. struct aa_label *label = NULL;
  691. struct aa_labelset *ls;
  692. unsigned long flags;
  693. struct aa_label *new;
  694. int i;
  695. AA_BUG(!vec);
  696. if (len == 1)
  697. return aa_get_label(&vec[0]->label);
  698. ls = labels_set(&vec[len - 1]->label);
  699. /* TODO: enable when read side is lockless
  700. * check if label exists before taking locks
  701. */
  702. new = aa_label_alloc(len, NULL, gfp);
  703. if (!new)
  704. return NULL;
  705. for (i = 0; i < len; i++)
  706. new->vec[i] = aa_get_profile(vec[i]);
  707. write_lock_irqsave(&ls->lock, flags);
  708. label = __label_insert(ls, new, false);
  709. write_unlock_irqrestore(&ls->lock, flags);
  710. label_free_or_put_new(label, new);
  711. return label;
  712. }
  713. struct aa_label *aa_vec_find_or_create_label(struct aa_profile **vec, int len,
  714. gfp_t gfp)
  715. {
  716. struct aa_label *label = vec_find(vec, len);
  717. if (label)
  718. return label;
  719. return vec_create_and_insert_label(vec, len, gfp);
  720. }
  721. /**
  722. * aa_label_find - find label @label in label set
  723. * @label: label to find (NOT NULL)
  724. *
  725. * Requires: caller to hold a valid ref on l
  726. *
  727. * Returns: refcounted @label if @label is in tree
  728. * refcounted label that is equiv to @label in tree
  729. * else NULL if @label or equiv is not in tree
  730. */
  731. struct aa_label *aa_label_find(struct aa_label *label)
  732. {
  733. AA_BUG(!label);
  734. return vec_find(label->vec, label->size);
  735. }
  736. /**
  737. * aa_label_insert - insert label @label into @ls or return existing label
  738. * @ls - labelset to insert @label into
  739. * @label - label to insert
  740. *
  741. * Requires: caller to hold a valid ref on @label
  742. *
  743. * Returns: ref counted @label if successful in inserting @label
  744. * else ref counted equivalent label that is already in the set
  745. */
  746. struct aa_label *aa_label_insert(struct aa_labelset *ls, struct aa_label *label)
  747. {
  748. struct aa_label *l;
  749. unsigned long flags;
  750. AA_BUG(!ls);
  751. AA_BUG(!label);
  752. /* check if label exists before taking lock */
  753. if (!label_is_stale(label)) {
  754. read_lock_irqsave(&ls->lock, flags);
  755. l = __label_find(label);
  756. read_unlock_irqrestore(&ls->lock, flags);
  757. if (l)
  758. return l;
  759. }
  760. write_lock_irqsave(&ls->lock, flags);
  761. l = __label_insert(ls, label, false);
  762. write_unlock_irqrestore(&ls->lock, flags);
  763. return l;
  764. }
  765. /**
  766. * aa_label_next_in_merge - find the next profile when merging @a and @b
  767. * @I: label iterator
  768. * @a: label to merge
  769. * @b: label to merge
  770. *
  771. * Returns: next profile
  772. * else null if no more profiles
  773. */
  774. struct aa_profile *aa_label_next_in_merge(struct label_it *I,
  775. struct aa_label *a,
  776. struct aa_label *b)
  777. {
  778. AA_BUG(!a);
  779. AA_BUG(!b);
  780. AA_BUG(!I);
  781. AA_BUG(I->i < 0);
  782. AA_BUG(I->i > a->size);
  783. AA_BUG(I->j < 0);
  784. AA_BUG(I->j > b->size);
  785. if (I->i < a->size) {
  786. if (I->j < b->size) {
  787. int res = profile_cmp(a->vec[I->i], b->vec[I->j]);
  788. if (res > 0)
  789. return b->vec[(I->j)++];
  790. if (res == 0)
  791. (I->j)++;
  792. }
  793. return a->vec[(I->i)++];
  794. }
  795. if (I->j < b->size)
  796. return b->vec[(I->j)++];
  797. return NULL;
  798. }
  799. /**
  800. * label_merge_cmp - cmp of @a merging with @b against @z for set ordering
  801. * @a: label to merge then compare (NOT NULL)
  802. * @b: label to merge then compare (NOT NULL)
  803. * @z: label to compare merge against (NOT NULL)
  804. *
  805. * Assumes: using the most recent versions of @a, @b, and @z
  806. *
  807. * Returns: <0 if a < b
  808. * ==0 if a == b
  809. * >0 if a > b
  810. */
  811. static int label_merge_cmp(struct aa_label *a, struct aa_label *b,
  812. struct aa_label *z)
  813. {
  814. struct aa_profile *p = NULL;
  815. struct label_it i = { };
  816. int k;
  817. AA_BUG(!a);
  818. AA_BUG(!b);
  819. AA_BUG(!z);
  820. for (k = 0;
  821. k < z->size && (p = aa_label_next_in_merge(&i, a, b));
  822. k++) {
  823. int res = profile_cmp(p, z->vec[k]);
  824. if (res != 0)
  825. return res;
  826. }
  827. if (p)
  828. return 1;
  829. else if (k < z->size)
  830. return -1;
  831. return 0;
  832. }
  833. /**
  834. * label_merge_insert - create a new label by merging @a and @b
  835. * @new: preallocated label to merge into (NOT NULL)
  836. * @a: label to merge with @b (NOT NULL)
  837. * @b: label to merge with @a (NOT NULL)
  838. *
  839. * Requires: preallocated proxy
  840. *
  841. * Returns: ref counted label either @new if merge is unique
  842. * @a if @b is a subset of @a
  843. * @b if @a is a subset of @b
  844. *
  845. * NOTE: will not use @new if the merge results in @new == @a or @b
  846. *
  847. * Must be used within labelset write lock to avoid racing with
  848. * setting labels stale.
  849. */
  850. static struct aa_label *label_merge_insert(struct aa_label *new,
  851. struct aa_label *a,
  852. struct aa_label *b)
  853. {
  854. struct aa_label *label;
  855. struct aa_labelset *ls;
  856. struct aa_profile *next;
  857. struct label_it i;
  858. unsigned long flags;
  859. int k = 0, invcount = 0;
  860. bool stale = false;
  861. AA_BUG(!a);
  862. AA_BUG(a->size < 0);
  863. AA_BUG(!b);
  864. AA_BUG(b->size < 0);
  865. AA_BUG(!new);
  866. AA_BUG(new->size < a->size + b->size);
  867. label_for_each_in_merge(i, a, b, next) {
  868. AA_BUG(!next);
  869. if (profile_is_stale(next)) {
  870. new->vec[k] = aa_get_newest_profile(next);
  871. AA_BUG(!new->vec[k]->label.proxy);
  872. AA_BUG(!new->vec[k]->label.proxy->label);
  873. if (next->label.proxy != new->vec[k]->label.proxy)
  874. invcount++;
  875. k++;
  876. stale = true;
  877. } else
  878. new->vec[k++] = aa_get_profile(next);
  879. }
  880. /* set to actual size which is <= allocated len */
  881. new->size = k;
  882. new->vec[k] = NULL;
  883. if (invcount) {
  884. new->size -= aa_vec_unique(&new->vec[0], new->size,
  885. VEC_FLAG_TERMINATE);
  886. /* TODO: deal with reference labels */
  887. if (new->size == 1) {
  888. label = aa_get_label(&new->vec[0]->label);
  889. return label;
  890. }
  891. } else if (!stale) {
  892. /*
  893. * merge could be same as a || b, note: it is not possible
  894. * for new->size == a->size == b->size unless a == b
  895. */
  896. if (k == a->size)
  897. return aa_get_label(a);
  898. else if (k == b->size)
  899. return aa_get_label(b);
  900. }
  901. if (vec_unconfined(new->vec, new->size))
  902. new->flags |= FLAG_UNCONFINED;
  903. ls = labels_set(new);
  904. write_lock_irqsave(&ls->lock, flags);
  905. label = __label_insert(labels_set(new), new, false);
  906. write_unlock_irqrestore(&ls->lock, flags);
  907. return label;
  908. }
  909. /**
  910. * labelset_of_merge - find which labelset a merged label should be inserted
  911. * @a: label to merge and insert
  912. * @b: label to merge and insert
  913. *
  914. * Returns: labelset that the merged label should be inserted into
  915. */
  916. static struct aa_labelset *labelset_of_merge(struct aa_label *a,
  917. struct aa_label *b)
  918. {
  919. struct aa_ns *nsa = labels_ns(a);
  920. struct aa_ns *nsb = labels_ns(b);
  921. if (ns_cmp(nsa, nsb) <= 0)
  922. return &nsa->labels;
  923. return &nsb->labels;
  924. }
  925. /**
  926. * __label_find_merge - find label that is equiv to merge of @a and @b
  927. * @ls: set of labels to search (NOT NULL)
  928. * @a: label to merge with @b (NOT NULL)
  929. * @b: label to merge with @a (NOT NULL)
  930. *
  931. * Requires: ls->lock read_lock held
  932. *
  933. * Returns: ref counted label that is equiv to merge of @a and @b
  934. * else NULL if merge of @a and @b is not in set
  935. */
  936. static struct aa_label *__label_find_merge(struct aa_labelset *ls,
  937. struct aa_label *a,
  938. struct aa_label *b)
  939. {
  940. struct rb_node *node;
  941. AA_BUG(!ls);
  942. AA_BUG(!a);
  943. AA_BUG(!b);
  944. if (a == b)
  945. return __label_find(a);
  946. node = ls->root.rb_node;
  947. while (node) {
  948. struct aa_label *this = container_of(node, struct aa_label,
  949. node);
  950. int result = label_merge_cmp(a, b, this);
  951. if (result < 0)
  952. node = node->rb_left;
  953. else if (result > 0)
  954. node = node->rb_right;
  955. else
  956. return __aa_get_label(this);
  957. }
  958. return NULL;
  959. }
  960. /**
  961. * aa_label_find_merge - find label that is equiv to merge of @a and @b
  962. * @a: label to merge with @b (NOT NULL)
  963. * @b: label to merge with @a (NOT NULL)
  964. *
  965. * Requires: labels be fully constructed with a valid ns
  966. *
  967. * Returns: ref counted label that is equiv to merge of @a and @b
  968. * else NULL if merge of @a and @b is not in set
  969. */
  970. struct aa_label *aa_label_find_merge(struct aa_label *a, struct aa_label *b)
  971. {
  972. struct aa_labelset *ls;
  973. struct aa_label *label, *ar = NULL, *br = NULL;
  974. unsigned long flags;
  975. AA_BUG(!a);
  976. AA_BUG(!b);
  977. if (label_is_stale(a))
  978. a = ar = aa_get_newest_label(a);
  979. if (label_is_stale(b))
  980. b = br = aa_get_newest_label(b);
  981. ls = labelset_of_merge(a, b);
  982. read_lock_irqsave(&ls->lock, flags);
  983. label = __label_find_merge(ls, a, b);
  984. read_unlock_irqrestore(&ls->lock, flags);
  985. aa_put_label(ar);
  986. aa_put_label(br);
  987. return label;
  988. }
  989. /**
  990. * aa_label_merge - attempt to insert new merged label of @a and @b
  991. * @ls: set of labels to insert label into (NOT NULL)
  992. * @a: label to merge with @b (NOT NULL)
  993. * @b: label to merge with @a (NOT NULL)
  994. * @gfp: memory allocation type
  995. *
  996. * Requires: caller to hold valid refs on @a and @b
  997. * labels be fully constructed with a valid ns
  998. *
  999. * Returns: ref counted new label if successful in inserting merge of a & b
  1000. * else ref counted equivalent label that is already in the set.
  1001. * else NULL if could not create label (-ENOMEM)
  1002. */
  1003. struct aa_label *aa_label_merge(struct aa_label *a, struct aa_label *b,
  1004. gfp_t gfp)
  1005. {
  1006. struct aa_label *label = NULL;
  1007. AA_BUG(!a);
  1008. AA_BUG(!b);
  1009. if (a == b)
  1010. return aa_get_newest_label(a);
  1011. /* TODO: enable when read side is lockless
  1012. * check if label exists before taking locks
  1013. if (!label_is_stale(a) && !label_is_stale(b))
  1014. label = aa_label_find_merge(a, b);
  1015. */
  1016. if (!label) {
  1017. struct aa_label *new;
  1018. a = aa_get_newest_label(a);
  1019. b = aa_get_newest_label(b);
  1020. /* could use label_merge_len(a, b), but requires double
  1021. * comparison for small savings
  1022. */
  1023. new = aa_label_alloc(a->size + b->size, NULL, gfp);
  1024. if (!new)
  1025. goto out;
  1026. label = label_merge_insert(new, a, b);
  1027. label_free_or_put_new(label, new);
  1028. out:
  1029. aa_put_label(a);
  1030. aa_put_label(b);
  1031. }
  1032. return label;
  1033. }
  1034. static inline bool label_is_visible(struct aa_profile *profile,
  1035. struct aa_label *label)
  1036. {
  1037. return aa_ns_visible(profile->ns, labels_ns(label), true);
  1038. }
  1039. /* match a profile and its associated ns component if needed
  1040. * Assumes visibility test has already been done.
  1041. * If a subns profile is not to be matched should be prescreened with
  1042. * visibility test.
  1043. */
  1044. static inline unsigned int match_component(struct aa_profile *profile,
  1045. struct aa_profile *tp,
  1046. unsigned int state)
  1047. {
  1048. const char *ns_name;
  1049. if (profile->ns == tp->ns)
  1050. return aa_dfa_match(profile->policy.dfa, state, tp->base.hname);
  1051. /* try matching with namespace name and then profile */
  1052. ns_name = aa_ns_name(profile->ns, tp->ns, true);
  1053. state = aa_dfa_match_len(profile->policy.dfa, state, ":", 1);
  1054. state = aa_dfa_match(profile->policy.dfa, state, ns_name);
  1055. state = aa_dfa_match_len(profile->policy.dfa, state, ":", 1);
  1056. return aa_dfa_match(profile->policy.dfa, state, tp->base.hname);
  1057. }
  1058. /**
  1059. * label_compound_match - find perms for full compound label
  1060. * @profile: profile to find perms for
  1061. * @label: label to check access permissions for
  1062. * @start: state to start match in
  1063. * @subns: whether to do permission checks on components in a subns
  1064. * @request: permissions to request
  1065. * @perms: perms struct to set
  1066. *
  1067. * Returns: 0 on success else ERROR
  1068. *
  1069. * For the label A//&B//&C this does the perm match for A//&B//&C
  1070. * @perms should be preinitialized with allperms OR a previous permission
  1071. * check to be stacked.
  1072. */
  1073. static int label_compound_match(struct aa_profile *profile,
  1074. struct aa_label *label,
  1075. unsigned int state, bool subns, u32 request,
  1076. struct aa_perms *perms)
  1077. {
  1078. struct aa_profile *tp;
  1079. struct label_it i;
  1080. /* find first subcomponent that is visible */
  1081. label_for_each(i, label, tp) {
  1082. if (!aa_ns_visible(profile->ns, tp->ns, subns))
  1083. continue;
  1084. state = match_component(profile, tp, state);
  1085. if (!state)
  1086. goto fail;
  1087. goto next;
  1088. }
  1089. /* no component visible */
  1090. *perms = allperms;
  1091. return 0;
  1092. next:
  1093. label_for_each_cont(i, label, tp) {
  1094. if (!aa_ns_visible(profile->ns, tp->ns, subns))
  1095. continue;
  1096. state = aa_dfa_match(profile->policy.dfa, state, "//&");
  1097. state = match_component(profile, tp, state);
  1098. if (!state)
  1099. goto fail;
  1100. }
  1101. aa_compute_perms(profile->policy.dfa, state, perms);
  1102. aa_apply_modes_to_perms(profile, perms);
  1103. if ((perms->allow & request) != request)
  1104. return -EACCES;
  1105. return 0;
  1106. fail:
  1107. *perms = nullperms;
  1108. return state;
  1109. }
  1110. /**
  1111. * label_components_match - find perms for all subcomponents of a label
  1112. * @profile: profile to find perms for
  1113. * @label: label to check access permissions for
  1114. * @start: state to start match in
  1115. * @subns: whether to do permission checks on components in a subns
  1116. * @request: permissions to request
  1117. * @perms: an initialized perms struct to add accumulation to
  1118. *
  1119. * Returns: 0 on success else ERROR
  1120. *
  1121. * For the label A//&B//&C this does the perm match for each of A and B and C
  1122. * @perms should be preinitialized with allperms OR a previous permission
  1123. * check to be stacked.
  1124. */
  1125. static int label_components_match(struct aa_profile *profile,
  1126. struct aa_label *label, unsigned int start,
  1127. bool subns, u32 request,
  1128. struct aa_perms *perms)
  1129. {
  1130. struct aa_profile *tp;
  1131. struct label_it i;
  1132. struct aa_perms tmp;
  1133. unsigned int state = 0;
  1134. /* find first subcomponent to test */
  1135. label_for_each(i, label, tp) {
  1136. if (!aa_ns_visible(profile->ns, tp->ns, subns))
  1137. continue;
  1138. state = match_component(profile, tp, start);
  1139. if (!state)
  1140. goto fail;
  1141. goto next;
  1142. }
  1143. /* no subcomponents visible - no change in perms */
  1144. return 0;
  1145. next:
  1146. aa_compute_perms(profile->policy.dfa, state, &tmp);
  1147. aa_apply_modes_to_perms(profile, &tmp);
  1148. aa_perms_accum(perms, &tmp);
  1149. label_for_each_cont(i, label, tp) {
  1150. if (!aa_ns_visible(profile->ns, tp->ns, subns))
  1151. continue;
  1152. state = match_component(profile, tp, start);
  1153. if (!state)
  1154. goto fail;
  1155. aa_compute_perms(profile->policy.dfa, state, &tmp);
  1156. aa_apply_modes_to_perms(profile, &tmp);
  1157. aa_perms_accum(perms, &tmp);
  1158. }
  1159. if ((perms->allow & request) != request)
  1160. return -EACCES;
  1161. return 0;
  1162. fail:
  1163. *perms = nullperms;
  1164. return -EACCES;
  1165. }
  1166. /**
  1167. * aa_label_match - do a multi-component label match
  1168. * @profile: profile to match against (NOT NULL)
  1169. * @label: label to match (NOT NULL)
  1170. * @state: state to start in
  1171. * @subns: whether to match subns components
  1172. * @request: permission request
  1173. * @perms: Returns computed perms (NOT NULL)
  1174. *
  1175. * Returns: the state the match finished in, may be the none matching state
  1176. */
  1177. int aa_label_match(struct aa_profile *profile, struct aa_label *label,
  1178. unsigned int state, bool subns, u32 request,
  1179. struct aa_perms *perms)
  1180. {
  1181. int error = label_compound_match(profile, label, state, subns, request,
  1182. perms);
  1183. if (!error)
  1184. return error;
  1185. *perms = allperms;
  1186. return label_components_match(profile, label, state, subns, request,
  1187. perms);
  1188. }
  1189. /**
  1190. * aa_update_label_name - update a label to have a stored name
  1191. * @ns: ns being viewed from (NOT NULL)
  1192. * @label: label to update (NOT NULL)
  1193. * @gfp: type of memory allocation
  1194. *
  1195. * Requires: labels_set(label) not locked in caller
  1196. *
  1197. * note: only updates the label name if it does not have a name already
  1198. * and if it is in the labelset
  1199. */
  1200. bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp)
  1201. {
  1202. struct aa_labelset *ls;
  1203. unsigned long flags;
  1204. char __counted *name;
  1205. bool res = false;
  1206. AA_BUG(!ns);
  1207. AA_BUG(!label);
  1208. if (label->hname || labels_ns(label) != ns)
  1209. return res;
  1210. if (aa_label_acntsxprint(&name, ns, label, FLAGS_NONE, gfp) == -1)
  1211. return res;
  1212. ls = labels_set(label);
  1213. write_lock_irqsave(&ls->lock, flags);
  1214. if (!label->hname && label->flags & FLAG_IN_TREE) {
  1215. label->hname = name;
  1216. res = true;
  1217. } else
  1218. aa_put_str(name);
  1219. write_unlock_irqrestore(&ls->lock, flags);
  1220. return res;
  1221. }
  1222. /*
  1223. * cached label name is present and visible
  1224. * @label->hname only exists if label is namespace hierachical
  1225. */
  1226. static inline bool use_label_hname(struct aa_ns *ns, struct aa_label *label,
  1227. int flags)
  1228. {
  1229. if (label->hname && (!ns || labels_ns(label) == ns) &&
  1230. !(flags & ~FLAG_SHOW_MODE))
  1231. return true;
  1232. return false;
  1233. }
  1234. /* helper macro for snprint routines */
  1235. #define update_for_len(total, len, size, str) \
  1236. do { \
  1237. size_t ulen = len; \
  1238. \
  1239. AA_BUG(len < 0); \
  1240. total += ulen; \
  1241. ulen = min(ulen, size); \
  1242. size -= ulen; \
  1243. str += ulen; \
  1244. } while (0)
  1245. /**
  1246. * aa_profile_snxprint - print a profile name to a buffer
  1247. * @str: buffer to write to. (MAY BE NULL if @size == 0)
  1248. * @size: size of buffer
  1249. * @view: namespace profile is being viewed from
  1250. * @profile: profile to view (NOT NULL)
  1251. * @flags: whether to include the mode string
  1252. * @prev_ns: last ns printed when used in compound print
  1253. *
  1254. * Returns: size of name written or would be written if larger than
  1255. * available buffer
  1256. *
  1257. * Note: will not print anything if the profile is not visible
  1258. */
  1259. static int aa_profile_snxprint(char *str, size_t size, struct aa_ns *view,
  1260. struct aa_profile *profile, int flags,
  1261. struct aa_ns **prev_ns)
  1262. {
  1263. const char *ns_name = NULL;
  1264. AA_BUG(!str && size != 0);
  1265. AA_BUG(!profile);
  1266. if (!view)
  1267. view = profiles_ns(profile);
  1268. if (view != profile->ns &&
  1269. (!prev_ns || (*prev_ns != profile->ns))) {
  1270. if (prev_ns)
  1271. *prev_ns = profile->ns;
  1272. ns_name = aa_ns_name(view, profile->ns,
  1273. flags & FLAG_VIEW_SUBNS);
  1274. if (ns_name == aa_hidden_ns_name) {
  1275. if (flags & FLAG_HIDDEN_UNCONFINED)
  1276. return snprintf(str, size, "%s", "unconfined");
  1277. return snprintf(str, size, "%s", ns_name);
  1278. }
  1279. }
  1280. if ((flags & FLAG_SHOW_MODE) && profile != profile->ns->unconfined) {
  1281. const char *modestr = aa_profile_mode_names[profile->mode];
  1282. if (ns_name)
  1283. return snprintf(str, size, ":%s:%s (%s)", ns_name,
  1284. profile->base.hname, modestr);
  1285. return snprintf(str, size, "%s (%s)", profile->base.hname,
  1286. modestr);
  1287. }
  1288. if (ns_name)
  1289. return snprintf(str, size, ":%s:%s", ns_name,
  1290. profile->base.hname);
  1291. return snprintf(str, size, "%s", profile->base.hname);
  1292. }
  1293. static const char *label_modename(struct aa_ns *ns, struct aa_label *label,
  1294. int flags)
  1295. {
  1296. struct aa_profile *profile;
  1297. struct label_it i;
  1298. int mode = -1, count = 0;
  1299. label_for_each(i, label, profile) {
  1300. if (aa_ns_visible(ns, profile->ns, flags & FLAG_VIEW_SUBNS)) {
  1301. if (profile->mode == APPARMOR_UNCONFINED)
  1302. /* special case unconfined so stacks with
  1303. * unconfined don't report as mixed. ie.
  1304. * profile_foo//&:ns1:unconfined (mixed)
  1305. */
  1306. continue;
  1307. count++;
  1308. if (mode == -1)
  1309. mode = profile->mode;
  1310. else if (mode != profile->mode)
  1311. return "mixed";
  1312. }
  1313. }
  1314. if (count == 0)
  1315. return "-";
  1316. if (mode == -1)
  1317. /* everything was unconfined */
  1318. mode = APPARMOR_UNCONFINED;
  1319. return aa_profile_mode_names[mode];
  1320. }
  1321. /* if any visible label is not unconfined the display_mode returns true */
  1322. static inline bool display_mode(struct aa_ns *ns, struct aa_label *label,
  1323. int flags)
  1324. {
  1325. if ((flags & FLAG_SHOW_MODE)) {
  1326. struct aa_profile *profile;
  1327. struct label_it i;
  1328. label_for_each(i, label, profile) {
  1329. if (aa_ns_visible(ns, profile->ns,
  1330. flags & FLAG_VIEW_SUBNS) &&
  1331. profile != profile->ns->unconfined)
  1332. return true;
  1333. }
  1334. /* only ns->unconfined in set of profiles in ns */
  1335. return false;
  1336. }
  1337. return false;
  1338. }
  1339. /**
  1340. * aa_label_snxprint - print a label name to a string buffer
  1341. * @str: buffer to write to. (MAY BE NULL if @size == 0)
  1342. * @size: size of buffer
  1343. * @ns: namespace profile is being viewed from
  1344. * @label: label to view (NOT NULL)
  1345. * @flags: whether to include the mode string
  1346. *
  1347. * Returns: size of name written or would be written if larger than
  1348. * available buffer
  1349. *
  1350. * Note: labels do not have to be strictly hierarchical to the ns as
  1351. * objects may be shared across different namespaces and thus
  1352. * pickup labeling from each ns. If a particular part of the
  1353. * label is not visible it will just be excluded. And if none
  1354. * of the label is visible "---" will be used.
  1355. */
  1356. int aa_label_snxprint(char *str, size_t size, struct aa_ns *ns,
  1357. struct aa_label *label, int flags)
  1358. {
  1359. struct aa_profile *profile;
  1360. struct aa_ns *prev_ns = NULL;
  1361. struct label_it i;
  1362. int count = 0, total = 0;
  1363. ssize_t len;
  1364. AA_BUG(!str && size != 0);
  1365. AA_BUG(!label);
  1366. if (flags & FLAG_ABS_ROOT) {
  1367. ns = root_ns;
  1368. len = snprintf(str, size, "=");
  1369. update_for_len(total, len, size, str);
  1370. } else if (!ns) {
  1371. ns = labels_ns(label);
  1372. }
  1373. label_for_each(i, label, profile) {
  1374. if (aa_ns_visible(ns, profile->ns, flags & FLAG_VIEW_SUBNS)) {
  1375. if (count > 0) {
  1376. len = snprintf(str, size, "//&");
  1377. update_for_len(total, len, size, str);
  1378. }
  1379. len = aa_profile_snxprint(str, size, ns, profile,
  1380. flags & FLAG_VIEW_SUBNS,
  1381. &prev_ns);
  1382. update_for_len(total, len, size, str);
  1383. count++;
  1384. }
  1385. }
  1386. if (count == 0) {
  1387. if (flags & FLAG_HIDDEN_UNCONFINED)
  1388. return snprintf(str, size, "%s", "unconfined");
  1389. return snprintf(str, size, "%s", aa_hidden_ns_name);
  1390. }
  1391. /* count == 1 && ... is for backwards compat where the mode
  1392. * is not displayed for 'unconfined' in the current ns
  1393. */
  1394. if (display_mode(ns, label, flags)) {
  1395. len = snprintf(str, size, " (%s)",
  1396. label_modename(ns, label, flags));
  1397. update_for_len(total, len, size, str);
  1398. }
  1399. return total;
  1400. }
  1401. #undef update_for_len
  1402. /**
  1403. * aa_label_asxprint - allocate a string buffer and print label into it
  1404. * @strp: Returns - the allocated buffer with the label name. (NOT NULL)
  1405. * @ns: namespace profile is being viewed from
  1406. * @label: label to view (NOT NULL)
  1407. * @flags: flags controlling what label info is printed
  1408. * @gfp: kernel memory allocation type
  1409. *
  1410. * Returns: size of name written or would be written if larger than
  1411. * available buffer
  1412. */
  1413. int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label,
  1414. int flags, gfp_t gfp)
  1415. {
  1416. int size;
  1417. AA_BUG(!strp);
  1418. AA_BUG(!label);
  1419. size = aa_label_snxprint(NULL, 0, ns, label, flags);
  1420. if (size < 0)
  1421. return size;
  1422. *strp = kmalloc(size + 1, gfp);
  1423. if (!*strp)
  1424. return -ENOMEM;
  1425. return aa_label_snxprint(*strp, size + 1, ns, label, flags);
  1426. }
  1427. /**
  1428. * aa_label_acntsxprint - allocate a __counted string buffer and print label
  1429. * @strp: buffer to write to. (MAY BE NULL if @size == 0)
  1430. * @ns: namespace profile is being viewed from
  1431. * @label: label to view (NOT NULL)
  1432. * @flags: flags controlling what label info is printed
  1433. * @gfp: kernel memory allocation type
  1434. *
  1435. * Returns: size of name written or would be written if larger than
  1436. * available buffer
  1437. */
  1438. int aa_label_acntsxprint(char __counted **strp, struct aa_ns *ns,
  1439. struct aa_label *label, int flags, gfp_t gfp)
  1440. {
  1441. int size;
  1442. AA_BUG(!strp);
  1443. AA_BUG(!label);
  1444. size = aa_label_snxprint(NULL, 0, ns, label, flags);
  1445. if (size < 0)
  1446. return size;
  1447. *strp = aa_str_alloc(size + 1, gfp);
  1448. if (!*strp)
  1449. return -ENOMEM;
  1450. return aa_label_snxprint(*strp, size + 1, ns, label, flags);
  1451. }
  1452. void aa_label_xaudit(struct audit_buffer *ab, struct aa_ns *ns,
  1453. struct aa_label *label, int flags, gfp_t gfp)
  1454. {
  1455. const char *str;
  1456. char *name = NULL;
  1457. int len;
  1458. AA_BUG(!ab);
  1459. AA_BUG(!label);
  1460. if (!use_label_hname(ns, label, flags) ||
  1461. display_mode(ns, label, flags)) {
  1462. len = aa_label_asxprint(&name, ns, label, flags, gfp);
  1463. if (len == -1) {
  1464. AA_DEBUG("label print error");
  1465. return;
  1466. }
  1467. str = name;
  1468. } else {
  1469. str = (char *) label->hname;
  1470. len = strlen(str);
  1471. }
  1472. if (audit_string_contains_control(str, len))
  1473. audit_log_n_hex(ab, str, len);
  1474. else
  1475. audit_log_n_string(ab, str, len);
  1476. kfree(name);
  1477. }
  1478. void aa_label_seq_xprint(struct seq_file *f, struct aa_ns *ns,
  1479. struct aa_label *label, int flags, gfp_t gfp)
  1480. {
  1481. AA_BUG(!f);
  1482. AA_BUG(!label);
  1483. if (!use_label_hname(ns, label, flags)) {
  1484. char *str;
  1485. int len;
  1486. len = aa_label_asxprint(&str, ns, label, flags, gfp);
  1487. if (len == -1) {
  1488. AA_DEBUG("label print error");
  1489. return;
  1490. }
  1491. seq_printf(f, "%s", str);
  1492. kfree(str);
  1493. } else if (display_mode(ns, label, flags))
  1494. seq_printf(f, "%s (%s)", label->hname,
  1495. label_modename(ns, label, flags));
  1496. else
  1497. seq_printf(f, "%s", label->hname);
  1498. }
  1499. void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags,
  1500. gfp_t gfp)
  1501. {
  1502. AA_BUG(!label);
  1503. if (!use_label_hname(ns, label, flags)) {
  1504. char *str;
  1505. int len;
  1506. len = aa_label_asxprint(&str, ns, label, flags, gfp);
  1507. if (len == -1) {
  1508. AA_DEBUG("label print error");
  1509. return;
  1510. }
  1511. pr_info("%s", str);
  1512. kfree(str);
  1513. } else if (display_mode(ns, label, flags))
  1514. pr_info("%s (%s)", label->hname,
  1515. label_modename(ns, label, flags));
  1516. else
  1517. pr_info("%s", label->hname);
  1518. }
  1519. void aa_label_audit(struct audit_buffer *ab, struct aa_label *label, gfp_t gfp)
  1520. {
  1521. struct aa_ns *ns = aa_get_current_ns();
  1522. aa_label_xaudit(ab, ns, label, FLAG_VIEW_SUBNS, gfp);
  1523. aa_put_ns(ns);
  1524. }
  1525. void aa_label_seq_print(struct seq_file *f, struct aa_label *label, gfp_t gfp)
  1526. {
  1527. struct aa_ns *ns = aa_get_current_ns();
  1528. aa_label_seq_xprint(f, ns, label, FLAG_VIEW_SUBNS, gfp);
  1529. aa_put_ns(ns);
  1530. }
  1531. void aa_label_printk(struct aa_label *label, gfp_t gfp)
  1532. {
  1533. struct aa_ns *ns = aa_get_current_ns();
  1534. aa_label_xprintk(ns, label, FLAG_VIEW_SUBNS, gfp);
  1535. aa_put_ns(ns);
  1536. }
  1537. static int label_count_strn_entries(const char *str, size_t n)
  1538. {
  1539. const char *end = str + n;
  1540. const char *split;
  1541. int count = 1;
  1542. AA_BUG(!str);
  1543. for (split = aa_label_strn_split(str, end - str);
  1544. split;
  1545. split = aa_label_strn_split(str, end - str)) {
  1546. count++;
  1547. str = split + 3;
  1548. }
  1549. return count;
  1550. }
  1551. /*
  1552. * ensure stacks with components like
  1553. * :ns:A//&B
  1554. * have :ns: applied to both 'A' and 'B' by making the lookup relative
  1555. * to the base if the lookup specifies an ns, else making the stacked lookup
  1556. * relative to the last embedded ns in the string.
  1557. */
  1558. static struct aa_profile *fqlookupn_profile(struct aa_label *base,
  1559. struct aa_label *currentbase,
  1560. const char *str, size_t n)
  1561. {
  1562. const char *first = skipn_spaces(str, n);
  1563. if (first && *first == ':')
  1564. return aa_fqlookupn_profile(base, str, n);
  1565. return aa_fqlookupn_profile(currentbase, str, n);
  1566. }
  1567. /**
  1568. * aa_label_strn_parse - parse, validate and convert a text string to a label
  1569. * @base: base label to use for lookups (NOT NULL)
  1570. * @str: null terminated text string (NOT NULL)
  1571. * @n: length of str to parse, will stop at \0 if encountered before n
  1572. * @gfp: allocation type
  1573. * @create: true if should create compound labels if they don't exist
  1574. * @force_stack: true if should stack even if no leading &
  1575. *
  1576. * Returns: the matching refcounted label if present
  1577. * else ERRPTR
  1578. */
  1579. struct aa_label *aa_label_strn_parse(struct aa_label *base, const char *str,
  1580. size_t n, gfp_t gfp, bool create,
  1581. bool force_stack)
  1582. {
  1583. DEFINE_VEC(profile, vec);
  1584. struct aa_label *label, *currbase = base;
  1585. int i, len, stack = 0, error;
  1586. const char *end = str + n;
  1587. const char *split;
  1588. AA_BUG(!base);
  1589. AA_BUG(!str);
  1590. str = skipn_spaces(str, n);
  1591. if (str == NULL || (*str == '=' && base != &root_ns->unconfined->label))
  1592. return ERR_PTR(-EINVAL);
  1593. len = label_count_strn_entries(str, end - str);
  1594. if (*str == '&' || force_stack) {
  1595. /* stack on top of base */
  1596. stack = base->size;
  1597. len += stack;
  1598. if (*str == '&')
  1599. str++;
  1600. }
  1601. error = vec_setup(profile, vec, len, gfp);
  1602. if (error)
  1603. return ERR_PTR(error);
  1604. for (i = 0; i < stack; i++)
  1605. vec[i] = aa_get_profile(base->vec[i]);
  1606. for (split = aa_label_strn_split(str, end - str), i = stack;
  1607. split && i < len; i++) {
  1608. vec[i] = fqlookupn_profile(base, currbase, str, split - str);
  1609. if (!vec[i])
  1610. goto fail;
  1611. /*
  1612. * if component specified a new ns it becomes the new base
  1613. * so that subsequent lookups are relative to it
  1614. */
  1615. if (vec[i]->ns != labels_ns(currbase))
  1616. currbase = &vec[i]->label;
  1617. str = split + 3;
  1618. split = aa_label_strn_split(str, end - str);
  1619. }
  1620. /* last element doesn't have a split */
  1621. if (i < len) {
  1622. vec[i] = fqlookupn_profile(base, currbase, str, end - str);
  1623. if (!vec[i])
  1624. goto fail;
  1625. }
  1626. if (len == 1)
  1627. /* no need to free vec as len < LOCAL_VEC_ENTRIES */
  1628. return &vec[0]->label;
  1629. len -= aa_vec_unique(vec, len, VEC_FLAG_TERMINATE);
  1630. /* TODO: deal with reference labels */
  1631. if (len == 1) {
  1632. label = aa_get_label(&vec[0]->label);
  1633. goto out;
  1634. }
  1635. if (create)
  1636. label = aa_vec_find_or_create_label(vec, len, gfp);
  1637. else
  1638. label = vec_find(vec, len);
  1639. if (!label)
  1640. goto fail;
  1641. out:
  1642. /* use adjusted len from after vec_unique, not original */
  1643. vec_cleanup(profile, vec, len);
  1644. return label;
  1645. fail:
  1646. label = ERR_PTR(-ENOENT);
  1647. goto out;
  1648. }
  1649. struct aa_label *aa_label_parse(struct aa_label *base, const char *str,
  1650. gfp_t gfp, bool create, bool force_stack)
  1651. {
  1652. return aa_label_strn_parse(base, str, strlen(str), gfp, create,
  1653. force_stack);
  1654. }
  1655. /**
  1656. * aa_labelset_destroy - remove all labels from the label set
  1657. * @ls: label set to cleanup (NOT NULL)
  1658. *
  1659. * Labels that are removed from the set may still exist beyond the set
  1660. * being destroyed depending on their reference counting
  1661. */
  1662. void aa_labelset_destroy(struct aa_labelset *ls)
  1663. {
  1664. struct rb_node *node;
  1665. unsigned long flags;
  1666. AA_BUG(!ls);
  1667. write_lock_irqsave(&ls->lock, flags);
  1668. for (node = rb_first(&ls->root); node; node = rb_first(&ls->root)) {
  1669. struct aa_label *this = rb_entry(node, struct aa_label, node);
  1670. if (labels_ns(this) != root_ns)
  1671. __label_remove(this,
  1672. ns_unconfined(labels_ns(this)->parent));
  1673. else
  1674. __label_remove(this, NULL);
  1675. }
  1676. write_unlock_irqrestore(&ls->lock, flags);
  1677. }
  1678. /*
  1679. * @ls: labelset to init (NOT NULL)
  1680. */
  1681. void aa_labelset_init(struct aa_labelset *ls)
  1682. {
  1683. AA_BUG(!ls);
  1684. rwlock_init(&ls->lock);
  1685. ls->root = RB_ROOT;
  1686. }
  1687. static struct aa_label *labelset_next_stale(struct aa_labelset *ls)
  1688. {
  1689. struct aa_label *label;
  1690. struct rb_node *node;
  1691. unsigned long flags;
  1692. AA_BUG(!ls);
  1693. read_lock_irqsave(&ls->lock, flags);
  1694. __labelset_for_each(ls, node) {
  1695. label = rb_entry(node, struct aa_label, node);
  1696. if ((label_is_stale(label) ||
  1697. vec_is_stale(label->vec, label->size)) &&
  1698. __aa_get_label(label))
  1699. goto out;
  1700. }
  1701. label = NULL;
  1702. out:
  1703. read_unlock_irqrestore(&ls->lock, flags);
  1704. return label;
  1705. }
  1706. /**
  1707. * __label_update - insert updated version of @label into labelset
  1708. * @label - the label to update/replace
  1709. *
  1710. * Returns: new label that is up to date
  1711. * else NULL on failure
  1712. *
  1713. * Requires: @ns lock be held
  1714. *
  1715. * Note: worst case is the stale @label does not get updated and has
  1716. * to be updated at a later time.
  1717. */
  1718. static struct aa_label *__label_update(struct aa_label *label)
  1719. {
  1720. struct aa_label *new, *tmp;
  1721. struct aa_labelset *ls;
  1722. unsigned long flags;
  1723. int i, invcount = 0;
  1724. AA_BUG(!label);
  1725. AA_BUG(!mutex_is_locked(&labels_ns(label)->lock));
  1726. new = aa_label_alloc(label->size, label->proxy, GFP_KERNEL);
  1727. if (!new)
  1728. return NULL;
  1729. /*
  1730. * while holding the ns_lock will stop profile replacement, removal,
  1731. * and label updates, label merging and removal can be occurring
  1732. */
  1733. ls = labels_set(label);
  1734. write_lock_irqsave(&ls->lock, flags);
  1735. for (i = 0; i < label->size; i++) {
  1736. AA_BUG(!label->vec[i]);
  1737. new->vec[i] = aa_get_newest_profile(label->vec[i]);
  1738. AA_BUG(!new->vec[i]);
  1739. AA_BUG(!new->vec[i]->label.proxy);
  1740. AA_BUG(!new->vec[i]->label.proxy->label);
  1741. if (new->vec[i]->label.proxy != label->vec[i]->label.proxy)
  1742. invcount++;
  1743. }
  1744. /* updated stale label by being removed/renamed from labelset */
  1745. if (invcount) {
  1746. new->size -= aa_vec_unique(&new->vec[0], new->size,
  1747. VEC_FLAG_TERMINATE);
  1748. /* TODO: deal with reference labels */
  1749. if (new->size == 1) {
  1750. tmp = aa_get_label(&new->vec[0]->label);
  1751. AA_BUG(tmp == label);
  1752. goto remove;
  1753. }
  1754. if (labels_set(label) != labels_set(new)) {
  1755. write_unlock_irqrestore(&ls->lock, flags);
  1756. tmp = aa_label_insert(labels_set(new), new);
  1757. write_lock_irqsave(&ls->lock, flags);
  1758. goto remove;
  1759. }
  1760. } else
  1761. AA_BUG(labels_ns(label) != labels_ns(new));
  1762. tmp = __label_insert(labels_set(label), new, true);
  1763. remove:
  1764. /* ensure label is removed, and redirected correctly */
  1765. __label_remove(label, tmp);
  1766. write_unlock_irqrestore(&ls->lock, flags);
  1767. label_free_or_put_new(tmp, new);
  1768. return tmp;
  1769. }
  1770. /**
  1771. * __labelset_update - update labels in @ns
  1772. * @ns: namespace to update labels in (NOT NULL)
  1773. *
  1774. * Requires: @ns lock be held
  1775. *
  1776. * Walk the labelset ensuring that all labels are up to date and valid
  1777. * Any label that has a stale component is marked stale and replaced and
  1778. * by an updated version.
  1779. *
  1780. * If failures happen due to memory pressures then stale labels will
  1781. * be left in place until the next pass.
  1782. */
  1783. static void __labelset_update(struct aa_ns *ns)
  1784. {
  1785. struct aa_label *label;
  1786. AA_BUG(!ns);
  1787. AA_BUG(!mutex_is_locked(&ns->lock));
  1788. do {
  1789. label = labelset_next_stale(&ns->labels);
  1790. if (label) {
  1791. struct aa_label *l = __label_update(label);
  1792. aa_put_label(l);
  1793. aa_put_label(label);
  1794. }
  1795. } while (label);
  1796. }
  1797. /**
  1798. * __aa_labelset_udate_subtree - update all labels with a stale component
  1799. * @ns: ns to start update at (NOT NULL)
  1800. *
  1801. * Requires: @ns lock be held
  1802. *
  1803. * Invalidates labels based on @p in @ns and any children namespaces.
  1804. */
  1805. void __aa_labelset_update_subtree(struct aa_ns *ns)
  1806. {
  1807. struct aa_ns *child;
  1808. AA_BUG(!ns);
  1809. AA_BUG(!mutex_is_locked(&ns->lock));
  1810. __labelset_update(ns);
  1811. list_for_each_entry(child, &ns->sub_ns, base.list) {
  1812. mutex_lock_nested(&child->lock, child->level);
  1813. __aa_labelset_update_subtree(child);
  1814. mutex_unlock(&child->lock);
  1815. }
  1816. }