label.c 51 KB

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