domain.c 38 KB

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