domain.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor policy attachment and domain transitions
  5. *
  6. * Copyright (C) 2002-2008 Novell/SUSE
  7. * Copyright 2009-2010 Canonical Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/fdtable.h>
  16. #include <linux/file.h>
  17. #include <linux/mount.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/tracehook.h>
  20. #include <linux/personality.h>
  21. #include <linux/xattr.h>
  22. #include "include/audit.h"
  23. #include "include/apparmorfs.h"
  24. #include "include/cred.h"
  25. #include "include/domain.h"
  26. #include "include/file.h"
  27. #include "include/ipc.h"
  28. #include "include/match.h"
  29. #include "include/path.h"
  30. #include "include/policy.h"
  31. #include "include/policy_ns.h"
  32. /**
  33. * aa_free_domain_entries - free entries in a domain table
  34. * @domain: the domain table to free (MAYBE NULL)
  35. */
  36. void aa_free_domain_entries(struct aa_domain *domain)
  37. {
  38. int i;
  39. if (domain) {
  40. if (!domain->table)
  41. return;
  42. for (i = 0; i < domain->size; i++)
  43. kzfree(domain->table[i]);
  44. kzfree(domain->table);
  45. domain->table = NULL;
  46. }
  47. }
  48. /**
  49. * may_change_ptraced_domain - check if can change profile on ptraced task
  50. * @to_label: profile to change to (NOT NULL)
  51. * @info: message if there is an error
  52. *
  53. * Check if current is ptraced and if so if the tracing task is allowed
  54. * to trace the new domain
  55. *
  56. * Returns: %0 or error if change not allowed
  57. */
  58. static int may_change_ptraced_domain(struct aa_label *to_label,
  59. const char **info)
  60. {
  61. struct task_struct *tracer;
  62. struct aa_label *tracerl = NULL;
  63. int error = 0;
  64. rcu_read_lock();
  65. tracer = ptrace_parent(current);
  66. if (tracer)
  67. /* released below */
  68. tracerl = aa_get_task_label(tracer);
  69. /* not ptraced */
  70. if (!tracer || unconfined(tracerl))
  71. goto out;
  72. error = aa_may_ptrace(tracerl, to_label, PTRACE_MODE_ATTACH);
  73. out:
  74. rcu_read_unlock();
  75. aa_put_label(tracerl);
  76. if (error)
  77. *info = "ptrace prevents transition";
  78. return error;
  79. }
  80. /**** TODO: dedup to aa_label_match - needs perm and dfa, merging
  81. * specifically this is an exact copy of aa_label_match except
  82. * aa_compute_perms is replaced with aa_compute_fperms
  83. * and policy.dfa with file.dfa
  84. ****/
  85. /* match a profile and its associated ns component if needed
  86. * Assumes visibility test has already been done.
  87. * If a subns profile is not to be matched should be prescreened with
  88. * visibility test.
  89. */
  90. static inline unsigned int match_component(struct aa_profile *profile,
  91. struct aa_profile *tp,
  92. bool stack, unsigned int state)
  93. {
  94. const char *ns_name;
  95. if (stack)
  96. state = aa_dfa_match(profile->file.dfa, state, "&");
  97. if (profile->ns == tp->ns)
  98. return aa_dfa_match(profile->file.dfa, state, tp->base.hname);
  99. /* try matching with namespace name and then profile */
  100. ns_name = aa_ns_name(profile->ns, tp->ns, true);
  101. state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
  102. state = aa_dfa_match(profile->file.dfa, state, ns_name);
  103. state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
  104. return aa_dfa_match(profile->file.dfa, state, tp->base.hname);
  105. }
  106. /**
  107. * label_compound_match - find perms for full compound label
  108. * @profile: profile to find perms for
  109. * @label: label to check access permissions for
  110. * @stack: whether this is a stacking request
  111. * @start: state to start match in
  112. * @subns: whether to do permission checks on components in a subns
  113. * @request: permissions to request
  114. * @perms: perms struct to set
  115. *
  116. * Returns: 0 on success else ERROR
  117. *
  118. * For the label A//&B//&C this does the perm match for A//&B//&C
  119. * @perms should be preinitialized with allperms OR a previous permission
  120. * check to be stacked.
  121. */
  122. static int label_compound_match(struct aa_profile *profile,
  123. struct aa_label *label, bool stack,
  124. unsigned int state, bool subns, u32 request,
  125. struct aa_perms *perms)
  126. {
  127. struct aa_profile *tp;
  128. struct label_it i;
  129. struct path_cond cond = { };
  130. /* find first subcomponent that is visible */
  131. label_for_each(i, label, tp) {
  132. if (!aa_ns_visible(profile->ns, tp->ns, subns))
  133. continue;
  134. state = match_component(profile, tp, stack, state);
  135. if (!state)
  136. goto fail;
  137. goto next;
  138. }
  139. /* no component visible */
  140. *perms = allperms;
  141. return 0;
  142. next:
  143. label_for_each_cont(i, label, tp) {
  144. if (!aa_ns_visible(profile->ns, tp->ns, subns))
  145. continue;
  146. state = aa_dfa_match(profile->file.dfa, state, "//&");
  147. state = match_component(profile, tp, false, state);
  148. if (!state)
  149. goto fail;
  150. }
  151. *perms = aa_compute_fperms(profile->file.dfa, state, &cond);
  152. aa_apply_modes_to_perms(profile, perms);
  153. if ((perms->allow & request) != request)
  154. return -EACCES;
  155. return 0;
  156. fail:
  157. *perms = nullperms;
  158. return -EACCES;
  159. }
  160. /**
  161. * label_components_match - find perms for all subcomponents of a label
  162. * @profile: profile to find perms for
  163. * @label: label to check access permissions for
  164. * @stack: whether this is a stacking request
  165. * @start: state to start match in
  166. * @subns: whether to do permission checks on components in a subns
  167. * @request: permissions to request
  168. * @perms: an initialized perms struct to add accumulation to
  169. *
  170. * Returns: 0 on success else ERROR
  171. *
  172. * For the label A//&B//&C this does the perm match for each of A and B and C
  173. * @perms should be preinitialized with allperms OR a previous permission
  174. * check to be stacked.
  175. */
  176. static int label_components_match(struct aa_profile *profile,
  177. struct aa_label *label, bool stack,
  178. unsigned int start, bool subns, u32 request,
  179. struct aa_perms *perms)
  180. {
  181. struct aa_profile *tp;
  182. struct label_it i;
  183. struct aa_perms tmp;
  184. struct path_cond cond = { };
  185. unsigned int state = 0;
  186. /* find first subcomponent to test */
  187. label_for_each(i, label, tp) {
  188. if (!aa_ns_visible(profile->ns, tp->ns, subns))
  189. continue;
  190. state = match_component(profile, tp, stack, start);
  191. if (!state)
  192. goto fail;
  193. goto next;
  194. }
  195. /* no subcomponents visible - no change in perms */
  196. return 0;
  197. next:
  198. tmp = aa_compute_fperms(profile->file.dfa, state, &cond);
  199. aa_apply_modes_to_perms(profile, &tmp);
  200. aa_perms_accum(perms, &tmp);
  201. label_for_each_cont(i, label, tp) {
  202. if (!aa_ns_visible(profile->ns, tp->ns, subns))
  203. continue;
  204. state = match_component(profile, tp, stack, start);
  205. if (!state)
  206. goto fail;
  207. tmp = aa_compute_fperms(profile->file.dfa, state, &cond);
  208. aa_apply_modes_to_perms(profile, &tmp);
  209. aa_perms_accum(perms, &tmp);
  210. }
  211. if ((perms->allow & request) != request)
  212. return -EACCES;
  213. return 0;
  214. fail:
  215. *perms = nullperms;
  216. return -EACCES;
  217. }
  218. /**
  219. * label_match - do a multi-component label match
  220. * @profile: profile to match against (NOT NULL)
  221. * @label: label to match (NOT NULL)
  222. * @stack: whether this is a stacking request
  223. * @state: state to start in
  224. * @subns: whether to match subns components
  225. * @request: permission request
  226. * @perms: Returns computed perms (NOT NULL)
  227. *
  228. * Returns: the state the match finished in, may be the none matching state
  229. */
  230. static int label_match(struct aa_profile *profile, struct aa_label *label,
  231. bool stack, unsigned int state, bool subns, u32 request,
  232. struct aa_perms *perms)
  233. {
  234. int error;
  235. *perms = nullperms;
  236. error = label_compound_match(profile, label, stack, state, subns,
  237. request, perms);
  238. if (!error)
  239. return error;
  240. *perms = allperms;
  241. return label_components_match(profile, label, stack, state, subns,
  242. request, perms);
  243. }
  244. /******* end TODO: dedup *****/
  245. /**
  246. * change_profile_perms - find permissions for change_profile
  247. * @profile: the current profile (NOT NULL)
  248. * @target: label to transition to (NOT NULL)
  249. * @stack: whether this is a stacking request
  250. * @request: requested perms
  251. * @start: state to start matching in
  252. *
  253. *
  254. * Returns: permission set
  255. *
  256. * currently only matches full label A//&B//&C or individual components A, B, C
  257. * not arbitrary combinations. Eg. A//&B, C
  258. */
  259. static int change_profile_perms(struct aa_profile *profile,
  260. struct aa_label *target, bool stack,
  261. u32 request, unsigned int start,
  262. struct aa_perms *perms)
  263. {
  264. if (profile_unconfined(profile)) {
  265. perms->allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC;
  266. perms->audit = perms->quiet = perms->kill = 0;
  267. return 0;
  268. }
  269. /* TODO: add profile in ns screening */
  270. return label_match(profile, target, stack, start, true, request, perms);
  271. }
  272. /**
  273. * aa_xattrs_match - check whether a file matches the xattrs defined in profile
  274. * @bprm: binprm struct for the process to validate
  275. * @profile: profile to match against (NOT NULL)
  276. * @state: state to start match in
  277. *
  278. * Returns: number of extended attributes that matched, or < 0 on error
  279. */
  280. static int aa_xattrs_match(const struct linux_binprm *bprm,
  281. struct aa_profile *profile, unsigned int state)
  282. {
  283. int i;
  284. ssize_t size;
  285. struct dentry *d;
  286. char *value = NULL;
  287. int value_size = 0, ret = profile->xattr_count;
  288. if (!bprm || !profile->xattr_count)
  289. return 0;
  290. might_sleep();
  291. /* transition from exec match to xattr set */
  292. state = aa_dfa_null_transition(profile->xmatch, state);
  293. d = bprm->file->f_path.dentry;
  294. for (i = 0; i < profile->xattr_count; i++) {
  295. size = vfs_getxattr_alloc(d, profile->xattrs[i], &value,
  296. value_size, GFP_KERNEL);
  297. if (size >= 0) {
  298. u32 perm;
  299. /* Check the xattr value, not just presence */
  300. state = aa_dfa_match_len(profile->xmatch, state, value,
  301. size);
  302. perm = dfa_user_allow(profile->xmatch, state);
  303. if (!(perm & MAY_EXEC)) {
  304. ret = -EINVAL;
  305. goto out;
  306. }
  307. }
  308. /* transition to next element */
  309. state = aa_dfa_null_transition(profile->xmatch, state);
  310. if (size < 0) {
  311. /*
  312. * No xattr match, so verify if transition to
  313. * next element was valid. IFF so the xattr
  314. * was optional.
  315. */
  316. if (!state) {
  317. ret = -EINVAL;
  318. goto out;
  319. }
  320. /* don't count missing optional xattr as matched */
  321. ret--;
  322. }
  323. }
  324. out:
  325. kfree(value);
  326. return ret;
  327. }
  328. /**
  329. * find_attach - do attachment search for unconfined processes
  330. * @bprm - binprm structure of transitioning task
  331. * @ns: the current namespace (NOT NULL)
  332. * @head - profile list to walk (NOT NULL)
  333. * @name - to match against (NOT NULL)
  334. * @info - info message if there was an error (NOT NULL)
  335. *
  336. * Do a linear search on the profiles in the list. There is a matching
  337. * preference where an exact match is preferred over a name which uses
  338. * expressions to match, and matching expressions with the greatest
  339. * xmatch_len are preferred.
  340. *
  341. * Requires: @head not be shared or have appropriate locks held
  342. *
  343. * Returns: label or NULL if no match found
  344. */
  345. static struct aa_label *find_attach(const struct linux_binprm *bprm,
  346. struct aa_ns *ns, struct list_head *head,
  347. const char *name, const char **info)
  348. {
  349. int candidate_len = 0, candidate_xattrs = 0;
  350. bool conflict = false;
  351. struct aa_profile *profile, *candidate = NULL;
  352. AA_BUG(!name);
  353. AA_BUG(!head);
  354. rcu_read_lock();
  355. restart:
  356. list_for_each_entry_rcu(profile, head, base.list) {
  357. if (profile->label.flags & FLAG_NULL &&
  358. &profile->label == ns_unconfined(profile->ns))
  359. continue;
  360. /* Find the "best" matching profile. Profiles must
  361. * match the path and extended attributes (if any)
  362. * associated with the file. A more specific path
  363. * match will be preferred over a less specific one,
  364. * and a match with more matching extended attributes
  365. * will be preferred over one with fewer. If the best
  366. * match has both the same level of path specificity
  367. * and the same number of matching extended attributes
  368. * as another profile, signal a conflict and refuse to
  369. * match.
  370. */
  371. if (profile->xmatch) {
  372. unsigned int state, count;
  373. u32 perm;
  374. state = aa_dfa_leftmatch(profile->xmatch, DFA_START,
  375. name, &count);
  376. perm = dfa_user_allow(profile->xmatch, state);
  377. /* any accepting state means a valid match. */
  378. if (perm & MAY_EXEC) {
  379. int ret = 0;
  380. if (count < candidate_len)
  381. continue;
  382. if (bprm && profile->xattr_count) {
  383. long rev = READ_ONCE(ns->revision);
  384. if (!aa_get_profile_not0(profile))
  385. goto restart;
  386. rcu_read_unlock();
  387. ret = aa_xattrs_match(bprm, profile,
  388. state);
  389. rcu_read_lock();
  390. aa_put_profile(profile);
  391. if (rev !=
  392. READ_ONCE(ns->revision))
  393. /* policy changed */
  394. goto restart;
  395. /*
  396. * Fail matching if the xattrs don't
  397. * match
  398. */
  399. if (ret < 0)
  400. continue;
  401. }
  402. /*
  403. * TODO: allow for more flexible best match
  404. *
  405. * The new match isn't more specific
  406. * than the current best match
  407. */
  408. if (count == candidate_len &&
  409. ret <= candidate_xattrs) {
  410. /* Match is equivalent, so conflict */
  411. if (ret == candidate_xattrs)
  412. conflict = true;
  413. continue;
  414. }
  415. /* Either the same length with more matching
  416. * xattrs, or a longer match
  417. */
  418. candidate = profile;
  419. candidate_len = profile->xmatch_len;
  420. candidate_xattrs = ret;
  421. conflict = false;
  422. }
  423. } else if (!strcmp(profile->base.name, name)) {
  424. /*
  425. * old exact non-re match, without conditionals such
  426. * as xattrs. no more searching required
  427. */
  428. candidate = profile;
  429. goto out;
  430. }
  431. }
  432. if (!candidate || conflict) {
  433. if (conflict)
  434. *info = "conflicting profile attachments";
  435. rcu_read_unlock();
  436. return NULL;
  437. }
  438. out:
  439. candidate = aa_get_newest_profile(candidate);
  440. rcu_read_unlock();
  441. return &candidate->label;
  442. }
  443. static const char *next_name(int xtype, const char *name)
  444. {
  445. return NULL;
  446. }
  447. /**
  448. * x_table_lookup - lookup an x transition name via transition table
  449. * @profile: current profile (NOT NULL)
  450. * @xindex: index into x transition table
  451. * @name: returns: name tested to find label (NOT NULL)
  452. *
  453. * Returns: refcounted label, or NULL on failure (MAYBE NULL)
  454. */
  455. struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
  456. const char **name)
  457. {
  458. struct aa_label *label = NULL;
  459. u32 xtype = xindex & AA_X_TYPE_MASK;
  460. int index = xindex & AA_X_INDEX_MASK;
  461. AA_BUG(!name);
  462. /* index is guaranteed to be in range, validated at load time */
  463. /* TODO: move lookup parsing to unpack time so this is a straight
  464. * index into the resultant label
  465. */
  466. for (*name = profile->file.trans.table[index]; !label && *name;
  467. *name = next_name(xtype, *name)) {
  468. if (xindex & AA_X_CHILD) {
  469. struct aa_profile *new_profile;
  470. /* release by caller */
  471. new_profile = aa_find_child(profile, *name);
  472. if (new_profile)
  473. label = &new_profile->label;
  474. continue;
  475. }
  476. label = aa_label_parse(&profile->label, *name, GFP_ATOMIC,
  477. true, false);
  478. if (IS_ERR(label))
  479. label = NULL;
  480. }
  481. /* released by caller */
  482. return label;
  483. }
  484. /**
  485. * x_to_label - get target label for a given xindex
  486. * @profile: current profile (NOT NULL)
  487. * @bprm: binprm structure of transitioning task
  488. * @name: name to lookup (NOT NULL)
  489. * @xindex: index into x transition table
  490. * @lookupname: returns: name used in lookup if one was specified (NOT NULL)
  491. *
  492. * find label for a transition index
  493. *
  494. * Returns: refcounted label or NULL if not found available
  495. */
  496. static struct aa_label *x_to_label(struct aa_profile *profile,
  497. const struct linux_binprm *bprm,
  498. const char *name, u32 xindex,
  499. const char **lookupname,
  500. const char **info)
  501. {
  502. struct aa_label *new = NULL;
  503. struct aa_ns *ns = profile->ns;
  504. u32 xtype = xindex & AA_X_TYPE_MASK;
  505. const char *stack = NULL;
  506. switch (xtype) {
  507. case AA_X_NONE:
  508. /* fail exec unless ix || ux fallback - handled by caller */
  509. *lookupname = NULL;
  510. break;
  511. case AA_X_TABLE:
  512. /* TODO: fix when perm mapping done at unload */
  513. stack = profile->file.trans.table[xindex & AA_X_INDEX_MASK];
  514. if (*stack != '&') {
  515. /* released by caller */
  516. new = x_table_lookup(profile, xindex, lookupname);
  517. stack = NULL;
  518. break;
  519. }
  520. /* fall through to X_NAME */
  521. case AA_X_NAME:
  522. if (xindex & AA_X_CHILD)
  523. /* released by caller */
  524. new = find_attach(bprm, ns, &profile->base.profiles,
  525. name, info);
  526. else
  527. /* released by caller */
  528. new = find_attach(bprm, ns, &ns->base.profiles,
  529. name, info);
  530. *lookupname = name;
  531. break;
  532. }
  533. if (!new) {
  534. if (xindex & AA_X_INHERIT) {
  535. /* (p|c|n)ix - don't change profile but do
  536. * use the newest version
  537. */
  538. *info = "ix fallback";
  539. /* no profile && no error */
  540. new = aa_get_newest_label(&profile->label);
  541. } else if (xindex & AA_X_UNCONFINED) {
  542. new = aa_get_newest_label(ns_unconfined(profile->ns));
  543. *info = "ux fallback";
  544. }
  545. }
  546. if (new && stack) {
  547. /* base the stack on post domain transition */
  548. struct aa_label *base = new;
  549. new = aa_label_parse(base, stack, GFP_ATOMIC, true, false);
  550. if (IS_ERR(new))
  551. new = NULL;
  552. aa_put_label(base);
  553. }
  554. /* released by caller */
  555. return new;
  556. }
  557. static struct aa_label *profile_transition(struct aa_profile *profile,
  558. const struct linux_binprm *bprm,
  559. char *buffer, struct path_cond *cond,
  560. bool *secure_exec)
  561. {
  562. struct aa_label *new = NULL;
  563. struct aa_profile *component;
  564. struct label_it i;
  565. const char *info = NULL, *name = NULL, *target = NULL;
  566. unsigned int state = profile->file.start;
  567. struct aa_perms perms = {};
  568. bool nonewprivs = false;
  569. int error = 0;
  570. AA_BUG(!profile);
  571. AA_BUG(!bprm);
  572. AA_BUG(!buffer);
  573. error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
  574. &name, &info, profile->disconnected);
  575. if (error) {
  576. if (profile_unconfined(profile) ||
  577. (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
  578. AA_DEBUG("name lookup ix on error");
  579. error = 0;
  580. new = aa_get_newest_label(&profile->label);
  581. }
  582. name = bprm->filename;
  583. goto audit;
  584. }
  585. if (profile_unconfined(profile)) {
  586. new = find_attach(bprm, profile->ns,
  587. &profile->ns->base.profiles, name, &info);
  588. if (new) {
  589. AA_DEBUG("unconfined attached to new label");
  590. return new;
  591. }
  592. AA_DEBUG("unconfined exec no attachment");
  593. return aa_get_newest_label(&profile->label);
  594. }
  595. /* find exec permissions for name */
  596. state = aa_str_perms(profile->file.dfa, state, name, cond, &perms);
  597. if (perms.allow & MAY_EXEC) {
  598. /* exec permission determine how to transition */
  599. new = x_to_label(profile, bprm, name, perms.xindex, &target,
  600. &info);
  601. if (new && new->proxy == profile->label.proxy && info) {
  602. /* hack ix fallback - improve how this is detected */
  603. goto audit;
  604. } else if (!new) {
  605. error = -EACCES;
  606. info = "profile transition not found";
  607. /* remove MAY_EXEC to audit as failure */
  608. perms.allow &= ~MAY_EXEC;
  609. } else {
  610. /* verify that each component's xattr requirements are
  611. * met, and fail execution otherwise
  612. */
  613. label_for_each(i, new, component) {
  614. if (aa_xattrs_match(bprm, component, state) <
  615. 0) {
  616. error = -EACCES;
  617. info = "required xattrs not present";
  618. perms.allow &= ~MAY_EXEC;
  619. aa_put_label(new);
  620. new = NULL;
  621. goto audit;
  622. }
  623. }
  624. }
  625. } else if (COMPLAIN_MODE(profile)) {
  626. /* no exec permission - learning mode */
  627. struct aa_profile *new_profile = NULL;
  628. char *n = kstrdup(name, GFP_ATOMIC);
  629. if (n) {
  630. /* name is ptr into buffer */
  631. long pos = name - buffer;
  632. /* break per cpu buffer hold */
  633. put_buffers(buffer);
  634. new_profile = aa_new_null_profile(profile, false, n,
  635. GFP_KERNEL);
  636. get_buffers(buffer);
  637. name = buffer + pos;
  638. strcpy((char *)name, n);
  639. kfree(n);
  640. }
  641. if (!new_profile) {
  642. error = -ENOMEM;
  643. info = "could not create null profile";
  644. } else {
  645. error = -EACCES;
  646. new = &new_profile->label;
  647. }
  648. perms.xindex |= AA_X_UNSAFE;
  649. } else
  650. /* fail exec */
  651. error = -EACCES;
  652. if (!new)
  653. goto audit;
  654. if (!(perms.xindex & AA_X_UNSAFE)) {
  655. if (DEBUG_ON) {
  656. dbg_printk("apparmor: scrubbing environment variables"
  657. " for %s profile=", name);
  658. aa_label_printk(new, GFP_ATOMIC);
  659. dbg_printk("\n");
  660. }
  661. *secure_exec = true;
  662. }
  663. audit:
  664. aa_audit_file(profile, &perms, OP_EXEC, MAY_EXEC, name, target, new,
  665. cond->uid, info, error);
  666. if (!new || nonewprivs) {
  667. aa_put_label(new);
  668. return ERR_PTR(error);
  669. }
  670. return new;
  671. }
  672. static int profile_onexec(struct aa_profile *profile, struct aa_label *onexec,
  673. bool stack, const struct linux_binprm *bprm,
  674. char *buffer, struct path_cond *cond,
  675. bool *secure_exec)
  676. {
  677. unsigned int state = profile->file.start;
  678. struct aa_perms perms = {};
  679. const char *xname = NULL, *info = "change_profile onexec";
  680. int error = -EACCES;
  681. AA_BUG(!profile);
  682. AA_BUG(!onexec);
  683. AA_BUG(!bprm);
  684. AA_BUG(!buffer);
  685. if (profile_unconfined(profile)) {
  686. /* change_profile on exec already granted */
  687. /*
  688. * NOTE: Domain transitions from unconfined are allowed
  689. * even when no_new_privs is set because this aways results
  690. * in a further reduction of permissions.
  691. */
  692. return 0;
  693. }
  694. error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
  695. &xname, &info, profile->disconnected);
  696. if (error) {
  697. if (profile_unconfined(profile) ||
  698. (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
  699. AA_DEBUG("name lookup ix on error");
  700. error = 0;
  701. }
  702. xname = bprm->filename;
  703. goto audit;
  704. }
  705. /* find exec permissions for name */
  706. state = aa_str_perms(profile->file.dfa, state, xname, cond, &perms);
  707. if (!(perms.allow & AA_MAY_ONEXEC)) {
  708. info = "no change_onexec valid for executable";
  709. goto audit;
  710. }
  711. /* test if this exec can be paired with change_profile onexec.
  712. * onexec permission is linked to exec with a standard pairing
  713. * exec\0change_profile
  714. */
  715. state = aa_dfa_null_transition(profile->file.dfa, state);
  716. error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC,
  717. state, &perms);
  718. if (error) {
  719. perms.allow &= ~AA_MAY_ONEXEC;
  720. goto audit;
  721. }
  722. if (!(perms.xindex & AA_X_UNSAFE)) {
  723. if (DEBUG_ON) {
  724. dbg_printk("apparmor: scrubbing environment "
  725. "variables for %s label=", xname);
  726. aa_label_printk(onexec, GFP_ATOMIC);
  727. dbg_printk("\n");
  728. }
  729. *secure_exec = true;
  730. }
  731. audit:
  732. return aa_audit_file(profile, &perms, OP_EXEC, AA_MAY_ONEXEC, xname,
  733. NULL, onexec, cond->uid, info, error);
  734. }
  735. /* ensure none ns domain transitions are correctly applied with onexec */
  736. static struct aa_label *handle_onexec(struct aa_label *label,
  737. struct aa_label *onexec, bool stack,
  738. const struct linux_binprm *bprm,
  739. char *buffer, struct path_cond *cond,
  740. bool *unsafe)
  741. {
  742. struct aa_profile *profile;
  743. struct aa_label *new;
  744. int error;
  745. AA_BUG(!label);
  746. AA_BUG(!onexec);
  747. AA_BUG(!bprm);
  748. AA_BUG(!buffer);
  749. if (!stack) {
  750. error = fn_for_each_in_ns(label, profile,
  751. profile_onexec(profile, onexec, stack,
  752. bprm, buffer, cond, unsafe));
  753. if (error)
  754. return ERR_PTR(error);
  755. new = fn_label_build_in_ns(label, profile, GFP_ATOMIC,
  756. aa_get_newest_label(onexec),
  757. profile_transition(profile, bprm, buffer,
  758. cond, unsafe));
  759. } else {
  760. /* TODO: determine how much we want to loosen this */
  761. error = fn_for_each_in_ns(label, profile,
  762. profile_onexec(profile, onexec, stack, bprm,
  763. buffer, cond, unsafe));
  764. if (error)
  765. return ERR_PTR(error);
  766. new = fn_label_build_in_ns(label, profile, GFP_ATOMIC,
  767. aa_label_merge(&profile->label, onexec,
  768. GFP_ATOMIC),
  769. profile_transition(profile, bprm, buffer,
  770. cond, unsafe));
  771. }
  772. if (new)
  773. return new;
  774. /* TODO: get rid of GLOBAL_ROOT_UID */
  775. error = fn_for_each_in_ns(label, profile,
  776. aa_audit_file(profile, &nullperms, OP_CHANGE_ONEXEC,
  777. AA_MAY_ONEXEC, bprm->filename, NULL,
  778. onexec, GLOBAL_ROOT_UID,
  779. "failed to build target label", -ENOMEM));
  780. return ERR_PTR(error);
  781. }
  782. /**
  783. * apparmor_bprm_set_creds - set the new creds on the bprm struct
  784. * @bprm: binprm for the exec (NOT NULL)
  785. *
  786. * Returns: %0 or error on failure
  787. *
  788. * TODO: once the other paths are done see if we can't refactor into a fn
  789. */
  790. int apparmor_bprm_set_creds(struct linux_binprm *bprm)
  791. {
  792. struct aa_task_ctx *ctx;
  793. struct aa_label *label, *new = NULL;
  794. struct aa_profile *profile;
  795. char *buffer = NULL;
  796. const char *info = NULL;
  797. int error = 0;
  798. bool unsafe = false;
  799. struct path_cond cond = {
  800. file_inode(bprm->file)->i_uid,
  801. file_inode(bprm->file)->i_mode
  802. };
  803. if (bprm->called_set_creds)
  804. return 0;
  805. ctx = task_ctx(current);
  806. AA_BUG(!cred_label(bprm->cred));
  807. AA_BUG(!ctx);
  808. label = aa_get_newest_label(cred_label(bprm->cred));
  809. /*
  810. * Detect no new privs being set, and store the label it
  811. * occurred under. Ideally this would happen when nnp
  812. * is set but there isn't a good way to do that yet.
  813. *
  814. * Testing for unconfined must be done before the subset test
  815. */
  816. if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) && !unconfined(label) &&
  817. !ctx->nnp)
  818. ctx->nnp = aa_get_label(label);
  819. /* buffer freed below, name is pointer into buffer */
  820. get_buffers(buffer);
  821. /* Test for onexec first as onexec override other x transitions. */
  822. if (ctx->onexec)
  823. new = handle_onexec(label, ctx->onexec, ctx->token,
  824. bprm, buffer, &cond, &unsafe);
  825. else
  826. new = fn_label_build(label, profile, GFP_ATOMIC,
  827. profile_transition(profile, bprm, buffer,
  828. &cond, &unsafe));
  829. AA_BUG(!new);
  830. if (IS_ERR(new)) {
  831. error = PTR_ERR(new);
  832. goto done;
  833. } else if (!new) {
  834. error = -ENOMEM;
  835. goto done;
  836. }
  837. /* Policy has specified a domain transitions. If no_new_privs and
  838. * confined ensure the transition is to confinement that is subset
  839. * of the confinement when the task entered no new privs.
  840. *
  841. * NOTE: Domain transitions from unconfined and to stacked
  842. * subsets are allowed even when no_new_privs is set because this
  843. * aways results in a further reduction of permissions.
  844. */
  845. if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) &&
  846. !unconfined(label) && !aa_label_is_subset(new, ctx->nnp)) {
  847. error = -EPERM;
  848. info = "no new privs";
  849. goto audit;
  850. }
  851. if (bprm->unsafe & LSM_UNSAFE_SHARE) {
  852. /* FIXME: currently don't mediate shared state */
  853. ;
  854. }
  855. if (bprm->unsafe & (LSM_UNSAFE_PTRACE)) {
  856. /* TODO: test needs to be profile of label to new */
  857. error = may_change_ptraced_domain(new, &info);
  858. if (error)
  859. goto audit;
  860. }
  861. if (unsafe) {
  862. if (DEBUG_ON) {
  863. dbg_printk("scrubbing environment variables for %s "
  864. "label=", bprm->filename);
  865. aa_label_printk(new, GFP_ATOMIC);
  866. dbg_printk("\n");
  867. }
  868. bprm->secureexec = 1;
  869. }
  870. if (label->proxy != new->proxy) {
  871. /* when transitioning clear unsafe personality bits */
  872. if (DEBUG_ON) {
  873. dbg_printk("apparmor: clearing unsafe personality "
  874. "bits. %s label=", bprm->filename);
  875. aa_label_printk(new, GFP_ATOMIC);
  876. dbg_printk("\n");
  877. }
  878. bprm->per_clear |= PER_CLEAR_ON_SETID;
  879. }
  880. aa_put_label(cred_label(bprm->cred));
  881. /* transfer reference, released when cred is freed */
  882. cred_label(bprm->cred) = new;
  883. done:
  884. aa_put_label(label);
  885. put_buffers(buffer);
  886. return error;
  887. audit:
  888. error = fn_for_each(label, profile,
  889. aa_audit_file(profile, &nullperms, OP_EXEC, MAY_EXEC,
  890. bprm->filename, NULL, new,
  891. file_inode(bprm->file)->i_uid, info,
  892. error));
  893. aa_put_label(new);
  894. goto done;
  895. }
  896. /*
  897. * Functions for self directed profile change
  898. */
  899. /* helper fn for change_hat
  900. *
  901. * Returns: label for hat transition OR ERR_PTR. Does NOT return NULL
  902. */
  903. static struct aa_label *build_change_hat(struct aa_profile *profile,
  904. const char *name, bool sibling)
  905. {
  906. struct aa_profile *root, *hat = NULL;
  907. const char *info = NULL;
  908. int error = 0;
  909. if (sibling && PROFILE_IS_HAT(profile)) {
  910. root = aa_get_profile_rcu(&profile->parent);
  911. } else if (!sibling && !PROFILE_IS_HAT(profile)) {
  912. root = aa_get_profile(profile);
  913. } else {
  914. info = "conflicting target types";
  915. error = -EPERM;
  916. goto audit;
  917. }
  918. hat = aa_find_child(root, name);
  919. if (!hat) {
  920. error = -ENOENT;
  921. if (COMPLAIN_MODE(profile)) {
  922. hat = aa_new_null_profile(profile, true, name,
  923. GFP_KERNEL);
  924. if (!hat) {
  925. info = "failed null profile create";
  926. error = -ENOMEM;
  927. }
  928. }
  929. }
  930. aa_put_profile(root);
  931. audit:
  932. aa_audit_file(profile, &nullperms, OP_CHANGE_HAT, AA_MAY_CHANGEHAT,
  933. name, hat ? hat->base.hname : NULL,
  934. hat ? &hat->label : NULL, GLOBAL_ROOT_UID, info,
  935. error);
  936. if (!hat || (error && error != -ENOENT))
  937. return ERR_PTR(error);
  938. /* if hat && error - complain mode, already audited and we adjust for
  939. * complain mode allow by returning hat->label
  940. */
  941. return &hat->label;
  942. }
  943. /* helper fn for changing into a hat
  944. *
  945. * Returns: label for hat transition or ERR_PTR. Does not return NULL
  946. */
  947. static struct aa_label *change_hat(struct aa_label *label, const char *hats[],
  948. int count, int flags)
  949. {
  950. struct aa_profile *profile, *root, *hat = NULL;
  951. struct aa_label *new;
  952. struct label_it it;
  953. bool sibling = false;
  954. const char *name, *info = NULL;
  955. int i, error;
  956. AA_BUG(!label);
  957. AA_BUG(!hats);
  958. AA_BUG(count < 1);
  959. if (PROFILE_IS_HAT(labels_profile(label)))
  960. sibling = true;
  961. /*find first matching hat */
  962. for (i = 0; i < count && !hat; i++) {
  963. name = hats[i];
  964. label_for_each_in_ns(it, labels_ns(label), label, profile) {
  965. if (sibling && PROFILE_IS_HAT(profile)) {
  966. root = aa_get_profile_rcu(&profile->parent);
  967. } else if (!sibling && !PROFILE_IS_HAT(profile)) {
  968. root = aa_get_profile(profile);
  969. } else { /* conflicting change type */
  970. info = "conflicting targets types";
  971. error = -EPERM;
  972. goto fail;
  973. }
  974. hat = aa_find_child(root, name);
  975. aa_put_profile(root);
  976. if (!hat) {
  977. if (!COMPLAIN_MODE(profile))
  978. goto outer_continue;
  979. /* complain mode succeed as if hat */
  980. } else if (!PROFILE_IS_HAT(hat)) {
  981. info = "target not hat";
  982. error = -EPERM;
  983. aa_put_profile(hat);
  984. goto fail;
  985. }
  986. aa_put_profile(hat);
  987. }
  988. /* found a hat for all profiles in ns */
  989. goto build;
  990. outer_continue:
  991. ;
  992. }
  993. /* no hats that match, find appropriate error
  994. *
  995. * In complain mode audit of the failure is based off of the first
  996. * hat supplied. This is done due how userspace interacts with
  997. * change_hat.
  998. */
  999. name = NULL;
  1000. label_for_each_in_ns(it, labels_ns(label), label, profile) {
  1001. if (!list_empty(&profile->base.profiles)) {
  1002. info = "hat not found";
  1003. error = -ENOENT;
  1004. goto fail;
  1005. }
  1006. }
  1007. info = "no hats defined";
  1008. error = -ECHILD;
  1009. fail:
  1010. label_for_each_in_ns(it, labels_ns(label), label, profile) {
  1011. /*
  1012. * no target as it has failed to be found or built
  1013. *
  1014. * change_hat uses probing and should not log failures
  1015. * related to missing hats
  1016. */
  1017. /* TODO: get rid of GLOBAL_ROOT_UID */
  1018. if (count > 1 || COMPLAIN_MODE(profile)) {
  1019. aa_audit_file(profile, &nullperms, OP_CHANGE_HAT,
  1020. AA_MAY_CHANGEHAT, name, NULL, NULL,
  1021. GLOBAL_ROOT_UID, info, error);
  1022. }
  1023. }
  1024. return ERR_PTR(error);
  1025. build:
  1026. new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
  1027. build_change_hat(profile, name, sibling),
  1028. aa_get_label(&profile->label));
  1029. if (!new) {
  1030. info = "label build failed";
  1031. error = -ENOMEM;
  1032. goto fail;
  1033. } /* else if (IS_ERR) build_change_hat has logged error so return new */
  1034. return new;
  1035. }
  1036. /**
  1037. * aa_change_hat - change hat to/from subprofile
  1038. * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
  1039. * @count: number of hat names in @hats
  1040. * @token: magic value to validate the hat change
  1041. * @flags: flags affecting behavior of the change
  1042. *
  1043. * Returns %0 on success, error otherwise.
  1044. *
  1045. * Change to the first profile specified in @hats that exists, and store
  1046. * the @hat_magic in the current task context. If the count == 0 and the
  1047. * @token matches that stored in the current task context, return to the
  1048. * top level profile.
  1049. *
  1050. * change_hat only applies to profiles in the current ns, and each profile
  1051. * in the ns must make the same transition otherwise change_hat will fail.
  1052. */
  1053. int aa_change_hat(const char *hats[], int count, u64 token, int flags)
  1054. {
  1055. const struct cred *cred;
  1056. struct aa_task_ctx *ctx = task_ctx(current);
  1057. struct aa_label *label, *previous, *new = NULL, *target = NULL;
  1058. struct aa_profile *profile;
  1059. struct aa_perms perms = {};
  1060. const char *info = NULL;
  1061. int error = 0;
  1062. /* released below */
  1063. cred = get_current_cred();
  1064. label = aa_get_newest_cred_label(cred);
  1065. previous = aa_get_newest_label(ctx->previous);
  1066. /*
  1067. * Detect no new privs being set, and store the label it
  1068. * occurred under. Ideally this would happen when nnp
  1069. * is set but there isn't a good way to do that yet.
  1070. *
  1071. * Testing for unconfined must be done before the subset test
  1072. */
  1073. if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
  1074. ctx->nnp = aa_get_label(label);
  1075. if (unconfined(label)) {
  1076. info = "unconfined can not change_hat";
  1077. error = -EPERM;
  1078. goto fail;
  1079. }
  1080. if (count) {
  1081. new = change_hat(label, hats, count, flags);
  1082. AA_BUG(!new);
  1083. if (IS_ERR(new)) {
  1084. error = PTR_ERR(new);
  1085. new = NULL;
  1086. /* already audited */
  1087. goto out;
  1088. }
  1089. error = may_change_ptraced_domain(new, &info);
  1090. if (error)
  1091. goto fail;
  1092. /*
  1093. * no new privs prevents domain transitions that would
  1094. * reduce restrictions.
  1095. */
  1096. if (task_no_new_privs(current) && !unconfined(label) &&
  1097. !aa_label_is_subset(new, ctx->nnp)) {
  1098. /* not an apparmor denial per se, so don't log it */
  1099. AA_DEBUG("no_new_privs - change_hat denied");
  1100. error = -EPERM;
  1101. goto out;
  1102. }
  1103. if (flags & AA_CHANGE_TEST)
  1104. goto out;
  1105. target = new;
  1106. error = aa_set_current_hat(new, token);
  1107. if (error == -EACCES)
  1108. /* kill task in case of brute force attacks */
  1109. goto kill;
  1110. } else if (previous && !(flags & AA_CHANGE_TEST)) {
  1111. /*
  1112. * no new privs prevents domain transitions that would
  1113. * reduce restrictions.
  1114. */
  1115. if (task_no_new_privs(current) && !unconfined(label) &&
  1116. !aa_label_is_subset(previous, ctx->nnp)) {
  1117. /* not an apparmor denial per se, so don't log it */
  1118. AA_DEBUG("no_new_privs - change_hat denied");
  1119. error = -EPERM;
  1120. goto out;
  1121. }
  1122. /* Return to saved label. Kill task if restore fails
  1123. * to avoid brute force attacks
  1124. */
  1125. target = previous;
  1126. error = aa_restore_previous_label(token);
  1127. if (error) {
  1128. if (error == -EACCES)
  1129. goto kill;
  1130. goto fail;
  1131. }
  1132. } /* else ignore @flags && restores when there is no saved profile */
  1133. out:
  1134. aa_put_label(new);
  1135. aa_put_label(previous);
  1136. aa_put_label(label);
  1137. put_cred(cred);
  1138. return error;
  1139. kill:
  1140. info = "failed token match";
  1141. perms.kill = AA_MAY_CHANGEHAT;
  1142. fail:
  1143. fn_for_each_in_ns(label, profile,
  1144. aa_audit_file(profile, &perms, OP_CHANGE_HAT,
  1145. AA_MAY_CHANGEHAT, NULL, NULL, target,
  1146. GLOBAL_ROOT_UID, info, error));
  1147. goto out;
  1148. }
  1149. static int change_profile_perms_wrapper(const char *op, const char *name,
  1150. struct aa_profile *profile,
  1151. struct aa_label *target, bool stack,
  1152. u32 request, struct aa_perms *perms)
  1153. {
  1154. const char *info = NULL;
  1155. int error = 0;
  1156. if (!error)
  1157. error = change_profile_perms(profile, target, stack, request,
  1158. profile->file.start, perms);
  1159. if (error)
  1160. error = aa_audit_file(profile, perms, op, request, name,
  1161. NULL, target, GLOBAL_ROOT_UID, info,
  1162. error);
  1163. return error;
  1164. }
  1165. /**
  1166. * aa_change_profile - perform a one-way profile transition
  1167. * @fqname: name of profile may include namespace (NOT NULL)
  1168. * @onexec: whether this transition is to take place immediately or at exec
  1169. * @flags: flags affecting change behavior
  1170. *
  1171. * Change to new profile @name. Unlike with hats, there is no way
  1172. * to change back. If @name isn't specified the current profile name is
  1173. * used.
  1174. * If @onexec then the transition is delayed until
  1175. * the next exec.
  1176. *
  1177. * Returns %0 on success, error otherwise.
  1178. */
  1179. int aa_change_profile(const char *fqname, int flags)
  1180. {
  1181. struct aa_label *label, *new = NULL, *target = NULL;
  1182. struct aa_profile *profile;
  1183. struct aa_perms perms = {};
  1184. const char *info = NULL;
  1185. const char *auditname = fqname; /* retain leading & if stack */
  1186. bool stack = flags & AA_CHANGE_STACK;
  1187. struct aa_task_ctx *ctx = task_ctx(current);
  1188. int error = 0;
  1189. char *op;
  1190. u32 request;
  1191. label = aa_get_current_label();
  1192. /*
  1193. * Detect no new privs being set, and store the label it
  1194. * occurred under. Ideally this would happen when nnp
  1195. * is set but there isn't a good way to do that yet.
  1196. *
  1197. * Testing for unconfined must be done before the subset test
  1198. */
  1199. if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
  1200. ctx->nnp = aa_get_label(label);
  1201. if (!fqname || !*fqname) {
  1202. AA_DEBUG("no profile name");
  1203. return -EINVAL;
  1204. }
  1205. if (flags & AA_CHANGE_ONEXEC) {
  1206. request = AA_MAY_ONEXEC;
  1207. if (stack)
  1208. op = OP_STACK_ONEXEC;
  1209. else
  1210. op = OP_CHANGE_ONEXEC;
  1211. } else {
  1212. request = AA_MAY_CHANGE_PROFILE;
  1213. if (stack)
  1214. op = OP_STACK;
  1215. else
  1216. op = OP_CHANGE_PROFILE;
  1217. }
  1218. label = aa_get_current_label();
  1219. if (*fqname == '&') {
  1220. stack = true;
  1221. /* don't have label_parse() do stacking */
  1222. fqname++;
  1223. }
  1224. target = aa_label_parse(label, fqname, GFP_KERNEL, true, false);
  1225. if (IS_ERR(target)) {
  1226. struct aa_profile *tprofile;
  1227. info = "label not found";
  1228. error = PTR_ERR(target);
  1229. target = NULL;
  1230. /*
  1231. * TODO: fixme using labels_profile is not right - do profile
  1232. * per complain profile
  1233. */
  1234. if ((flags & AA_CHANGE_TEST) ||
  1235. !COMPLAIN_MODE(labels_profile(label)))
  1236. goto audit;
  1237. /* released below */
  1238. tprofile = aa_new_null_profile(labels_profile(label), false,
  1239. fqname, GFP_KERNEL);
  1240. if (!tprofile) {
  1241. info = "failed null profile create";
  1242. error = -ENOMEM;
  1243. goto audit;
  1244. }
  1245. target = &tprofile->label;
  1246. goto check;
  1247. }
  1248. /*
  1249. * self directed transitions only apply to current policy ns
  1250. * TODO: currently requiring perms for stacking and straight change
  1251. * stacking doesn't strictly need this. Determine how much
  1252. * we want to loosen this restriction for stacking
  1253. *
  1254. * if (!stack) {
  1255. */
  1256. error = fn_for_each_in_ns(label, profile,
  1257. change_profile_perms_wrapper(op, auditname,
  1258. profile, target, stack,
  1259. request, &perms));
  1260. if (error)
  1261. /* auditing done in change_profile_perms_wrapper */
  1262. goto out;
  1263. /* } */
  1264. check:
  1265. /* check if tracing task is allowed to trace target domain */
  1266. error = may_change_ptraced_domain(target, &info);
  1267. if (error && !fn_for_each_in_ns(label, profile,
  1268. COMPLAIN_MODE(profile)))
  1269. goto audit;
  1270. /* TODO: add permission check to allow this
  1271. * if ((flags & AA_CHANGE_ONEXEC) && !current_is_single_threaded()) {
  1272. * info = "not a single threaded task";
  1273. * error = -EACCES;
  1274. * goto audit;
  1275. * }
  1276. */
  1277. if (flags & AA_CHANGE_TEST)
  1278. goto out;
  1279. /* stacking is always a subset, so only check the nonstack case */
  1280. if (!stack) {
  1281. new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
  1282. aa_get_label(target),
  1283. aa_get_label(&profile->label));
  1284. /*
  1285. * no new privs prevents domain transitions that would
  1286. * reduce restrictions.
  1287. */
  1288. if (task_no_new_privs(current) && !unconfined(label) &&
  1289. !aa_label_is_subset(new, ctx->nnp)) {
  1290. /* not an apparmor denial per se, so don't log it */
  1291. AA_DEBUG("no_new_privs - change_hat denied");
  1292. error = -EPERM;
  1293. goto out;
  1294. }
  1295. }
  1296. if (!(flags & AA_CHANGE_ONEXEC)) {
  1297. /* only transition profiles in the current ns */
  1298. if (stack)
  1299. new = aa_label_merge(label, target, GFP_KERNEL);
  1300. if (IS_ERR_OR_NULL(new)) {
  1301. info = "failed to build target label";
  1302. if (!new)
  1303. error = -ENOMEM;
  1304. else
  1305. error = PTR_ERR(new);
  1306. new = NULL;
  1307. perms.allow = 0;
  1308. goto audit;
  1309. }
  1310. error = aa_replace_current_label(new);
  1311. } else {
  1312. if (new) {
  1313. aa_put_label(new);
  1314. new = NULL;
  1315. }
  1316. /* full transition will be built in exec path */
  1317. error = aa_set_current_onexec(target, stack);
  1318. }
  1319. audit:
  1320. error = fn_for_each_in_ns(label, profile,
  1321. aa_audit_file(profile, &perms, op, request, auditname,
  1322. NULL, new ? new : target,
  1323. GLOBAL_ROOT_UID, info, error));
  1324. out:
  1325. aa_put_label(new);
  1326. aa_put_label(target);
  1327. aa_put_label(label);
  1328. return error;
  1329. }