label.c 51 KB

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