domain.c 41 KB

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