label.c 51 KB

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