domain.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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. */
  462. struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
  463. const char **name)
  464. {
  465. struct aa_ruleset *rules = list_first_entry(&profile->rules,
  466. typeof(*rules), list);
  467. struct aa_label *label = NULL;
  468. u32 xtype = xindex & AA_X_TYPE_MASK;
  469. int index = xindex & AA_X_INDEX_MASK;
  470. AA_BUG(!name);
  471. /* index is guaranteed to be in range, validated at load time */
  472. /* TODO: move lookup parsing to unpack time so this is a straight
  473. * index into the resultant label
  474. */
  475. for (*name = rules->file->trans.table[index]; !label && *name;
  476. *name = next_name(xtype, *name)) {
  477. if (xindex & AA_X_CHILD) {
  478. struct aa_profile *new_profile;
  479. /* release by caller */
  480. new_profile = aa_find_child(profile, *name);
  481. if (new_profile)
  482. label = &new_profile->label;
  483. continue;
  484. }
  485. label = aa_label_parse(&profile->label, *name, GFP_KERNEL,
  486. true, false);
  487. if (IS_ERR(label))
  488. label = NULL;
  489. }
  490. /* released by caller */
  491. return label;
  492. }
  493. /**
  494. * x_to_label - get target label for a given xindex
  495. * @profile: current profile (NOT NULL)
  496. * @bprm: binprm structure of transitioning task
  497. * @name: name to lookup (NOT NULL)
  498. * @xindex: index into x transition table
  499. * @lookupname: returns: name used in lookup if one was specified (NOT NULL)
  500. * @info: info message if there was an error (NOT NULL)
  501. *
  502. * find label for a transition index
  503. *
  504. * Returns: refcounted label or NULL if not found available
  505. */
  506. static struct aa_label *x_to_label(struct aa_profile *profile,
  507. const struct linux_binprm *bprm,
  508. const char *name, u32 xindex,
  509. const char **lookupname,
  510. const char **info)
  511. {
  512. struct aa_ruleset *rules = list_first_entry(&profile->rules,
  513. typeof(*rules), list);
  514. struct aa_label *new = NULL;
  515. struct aa_ns *ns = profile->ns;
  516. u32 xtype = xindex & AA_X_TYPE_MASK;
  517. const char *stack = NULL;
  518. switch (xtype) {
  519. case AA_X_NONE:
  520. /* fail exec unless ix || ux fallback - handled by caller */
  521. *lookupname = NULL;
  522. break;
  523. case AA_X_TABLE:
  524. /* TODO: fix when perm mapping done at unload */
  525. stack = rules->file->trans.table[xindex & AA_X_INDEX_MASK];
  526. if (*stack != '&') {
  527. /* released by caller */
  528. new = x_table_lookup(profile, xindex, lookupname);
  529. stack = NULL;
  530. break;
  531. }
  532. fallthrough; /* to X_NAME */
  533. case AA_X_NAME:
  534. if (xindex & AA_X_CHILD)
  535. /* released by caller */
  536. new = find_attach(bprm, ns, &profile->base.profiles,
  537. name, info);
  538. else
  539. /* released by caller */
  540. new = find_attach(bprm, ns, &ns->base.profiles,
  541. name, info);
  542. *lookupname = name;
  543. break;
  544. }
  545. if (!new) {
  546. if (xindex & AA_X_INHERIT) {
  547. /* (p|c|n)ix - don't change profile but do
  548. * use the newest version
  549. */
  550. *info = "ix fallback";
  551. /* no profile && no error */
  552. new = aa_get_newest_label(&profile->label);
  553. } else if (xindex & AA_X_UNCONFINED) {
  554. new = aa_get_newest_label(ns_unconfined(profile->ns));
  555. *info = "ux fallback";
  556. }
  557. }
  558. if (new && stack) {
  559. /* base the stack on post domain transition */
  560. struct aa_label *base = new;
  561. new = aa_label_parse(base, stack, GFP_KERNEL, true, false);
  562. if (IS_ERR(new))
  563. new = NULL;
  564. aa_put_label(base);
  565. }
  566. /* released by caller */
  567. return new;
  568. }
  569. static struct aa_label *profile_transition(const struct cred *subj_cred,
  570. struct aa_profile *profile,
  571. const struct linux_binprm *bprm,
  572. char *buffer, struct path_cond *cond,
  573. bool *secure_exec)
  574. {
  575. struct aa_ruleset *rules = list_first_entry(&profile->rules,
  576. typeof(*rules), list);
  577. struct aa_label *new = NULL;
  578. const char *info = NULL, *name = NULL, *target = NULL;
  579. aa_state_t state = rules->file->start[AA_CLASS_FILE];
  580. struct aa_perms perms = {};
  581. bool nonewprivs = false;
  582. int error = 0;
  583. AA_BUG(!profile);
  584. AA_BUG(!bprm);
  585. AA_BUG(!buffer);
  586. error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
  587. &name, &info, profile->disconnected);
  588. if (error) {
  589. if (profile_unconfined(profile) ||
  590. (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
  591. AA_DEBUG("name lookup ix on error");
  592. error = 0;
  593. new = aa_get_newest_label(&profile->label);
  594. }
  595. name = bprm->filename;
  596. goto audit;
  597. }
  598. if (profile_unconfined(profile)) {
  599. new = find_attach(bprm, profile->ns,
  600. &profile->ns->base.profiles, name, &info);
  601. if (new) {
  602. AA_DEBUG("unconfined attached to new label");
  603. return new;
  604. }
  605. AA_DEBUG("unconfined exec no attachment");
  606. return aa_get_newest_label(&profile->label);
  607. }
  608. /* find exec permissions for name */
  609. state = aa_str_perms(rules->file, state, name, cond, &perms);
  610. if (perms.allow & MAY_EXEC) {
  611. /* exec permission determine how to transition */
  612. new = x_to_label(profile, bprm, name, perms.xindex, &target,
  613. &info);
  614. if (new && new->proxy == profile->label.proxy && info) {
  615. /* hack ix fallback - improve how this is detected */
  616. goto audit;
  617. } else if (!new) {
  618. error = -EACCES;
  619. info = "profile transition not found";
  620. /* remove MAY_EXEC to audit as failure */
  621. perms.allow &= ~MAY_EXEC;
  622. }
  623. } else if (COMPLAIN_MODE(profile)) {
  624. /* no exec permission - learning mode */
  625. struct aa_profile *new_profile = NULL;
  626. new_profile = aa_new_learning_profile(profile, false, name,
  627. GFP_KERNEL);
  628. if (!new_profile) {
  629. error = -ENOMEM;
  630. info = "could not create null profile";
  631. } else {
  632. error = -EACCES;
  633. new = &new_profile->label;
  634. }
  635. perms.xindex |= AA_X_UNSAFE;
  636. } else
  637. /* fail exec */
  638. error = -EACCES;
  639. if (!new)
  640. goto audit;
  641. if (!(perms.xindex & AA_X_UNSAFE)) {
  642. if (DEBUG_ON) {
  643. dbg_printk("apparmor: scrubbing environment variables"
  644. " for %s profile=", name);
  645. aa_label_printk(new, GFP_KERNEL);
  646. dbg_printk("\n");
  647. }
  648. *secure_exec = true;
  649. }
  650. audit:
  651. aa_audit_file(subj_cred, profile, &perms, OP_EXEC, MAY_EXEC, name,
  652. target, new,
  653. cond->uid, info, error);
  654. if (!new || nonewprivs) {
  655. aa_put_label(new);
  656. return ERR_PTR(error);
  657. }
  658. return new;
  659. }
  660. static int profile_onexec(const struct cred *subj_cred,
  661. struct aa_profile *profile, struct aa_label *onexec,
  662. bool stack, const struct linux_binprm *bprm,
  663. char *buffer, struct path_cond *cond,
  664. bool *secure_exec)
  665. {
  666. struct aa_ruleset *rules = list_first_entry(&profile->rules,
  667. typeof(*rules), list);
  668. aa_state_t state = rules->file->start[AA_CLASS_FILE];
  669. struct aa_perms perms = {};
  670. const char *xname = NULL, *info = "change_profile onexec";
  671. int error = -EACCES;
  672. AA_BUG(!profile);
  673. AA_BUG(!onexec);
  674. AA_BUG(!bprm);
  675. AA_BUG(!buffer);
  676. if (profile_unconfined(profile)) {
  677. /* change_profile on exec already granted */
  678. /*
  679. * NOTE: Domain transitions from unconfined are allowed
  680. * even when no_new_privs is set because this aways results
  681. * in a further reduction of permissions.
  682. */
  683. return 0;
  684. }
  685. error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
  686. &xname, &info, profile->disconnected);
  687. if (error) {
  688. if (profile_unconfined(profile) ||
  689. (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
  690. AA_DEBUG("name lookup ix on error");
  691. error = 0;
  692. }
  693. xname = bprm->filename;
  694. goto audit;
  695. }
  696. /* find exec permissions for name */
  697. state = aa_str_perms(rules->file, state, xname, cond, &perms);
  698. if (!(perms.allow & AA_MAY_ONEXEC)) {
  699. info = "no change_onexec valid for executable";
  700. goto audit;
  701. }
  702. /* test if this exec can be paired with change_profile onexec.
  703. * onexec permission is linked to exec with a standard pairing
  704. * exec\0change_profile
  705. */
  706. state = aa_dfa_null_transition(rules->file->dfa, state);
  707. error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC,
  708. state, &perms);
  709. if (error) {
  710. perms.allow &= ~AA_MAY_ONEXEC;
  711. goto audit;
  712. }
  713. if (!(perms.xindex & AA_X_UNSAFE)) {
  714. if (DEBUG_ON) {
  715. dbg_printk("apparmor: scrubbing environment "
  716. "variables for %s label=", xname);
  717. aa_label_printk(onexec, GFP_KERNEL);
  718. dbg_printk("\n");
  719. }
  720. *secure_exec = true;
  721. }
  722. audit:
  723. return aa_audit_file(subj_cred, profile, &perms, OP_EXEC,
  724. AA_MAY_ONEXEC, xname,
  725. NULL, onexec, cond->uid, info, error);
  726. }
  727. /* ensure none ns domain transitions are correctly applied with onexec */
  728. static struct aa_label *handle_onexec(const struct cred *subj_cred,
  729. struct aa_label *label,
  730. struct aa_label *onexec, bool stack,
  731. const struct linux_binprm *bprm,
  732. char *buffer, struct path_cond *cond,
  733. bool *unsafe)
  734. {
  735. struct aa_profile *profile;
  736. struct aa_label *new;
  737. int error;
  738. AA_BUG(!label);
  739. AA_BUG(!onexec);
  740. AA_BUG(!bprm);
  741. AA_BUG(!buffer);
  742. if (!stack) {
  743. error = fn_for_each_in_ns(label, profile,
  744. profile_onexec(subj_cred, profile, onexec, stack,
  745. bprm, buffer, cond, unsafe));
  746. if (error)
  747. return ERR_PTR(error);
  748. new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
  749. aa_get_newest_label(onexec),
  750. profile_transition(subj_cred, profile, bprm,
  751. buffer,
  752. cond, unsafe));
  753. } else {
  754. /* TODO: determine how much we want to loosen this */
  755. error = fn_for_each_in_ns(label, profile,
  756. profile_onexec(subj_cred, profile, onexec, stack, bprm,
  757. buffer, cond, unsafe));
  758. if (error)
  759. return ERR_PTR(error);
  760. new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
  761. aa_label_merge(&profile->label, onexec,
  762. GFP_KERNEL),
  763. profile_transition(subj_cred, profile, bprm,
  764. buffer,
  765. cond, unsafe));
  766. }
  767. if (new)
  768. return new;
  769. /* TODO: get rid of GLOBAL_ROOT_UID */
  770. error = fn_for_each_in_ns(label, profile,
  771. aa_audit_file(subj_cred, profile, &nullperms,
  772. OP_CHANGE_ONEXEC,
  773. AA_MAY_ONEXEC, bprm->filename, NULL,
  774. onexec, GLOBAL_ROOT_UID,
  775. "failed to build target label", -ENOMEM));
  776. return ERR_PTR(error);
  777. }
  778. /**
  779. * apparmor_bprm_creds_for_exec - Update the new creds on the bprm struct
  780. * @bprm: binprm for the exec (NOT NULL)
  781. *
  782. * Returns: %0 or error on failure
  783. *
  784. * TODO: once the other paths are done see if we can't refactor into a fn
  785. */
  786. int apparmor_bprm_creds_for_exec(struct linux_binprm *bprm)
  787. {
  788. struct aa_task_ctx *ctx;
  789. struct aa_label *label, *new = NULL;
  790. const struct cred *subj_cred;
  791. struct aa_profile *profile;
  792. char *buffer = NULL;
  793. const char *info = NULL;
  794. int error = 0;
  795. bool unsafe = false;
  796. vfsuid_t vfsuid = i_uid_into_vfsuid(file_mnt_idmap(bprm->file),
  797. file_inode(bprm->file));
  798. struct path_cond cond = {
  799. vfsuid_into_kuid(vfsuid),
  800. file_inode(bprm->file)->i_mode
  801. };
  802. subj_cred = current_cred();
  803. ctx = task_ctx(current);
  804. AA_BUG(!cred_label(bprm->cred));
  805. AA_BUG(!ctx);
  806. label = aa_get_newest_label(cred_label(bprm->cred));
  807. /*
  808. * Detect no new privs being set, and store the label it
  809. * occurred under. Ideally this would happen when nnp
  810. * is set but there isn't a good way to do that yet.
  811. *
  812. * Testing for unconfined must be done before the subset test
  813. */
  814. if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) && !unconfined(label) &&
  815. !ctx->nnp)
  816. ctx->nnp = aa_get_label(label);
  817. /* buffer freed below, name is pointer into buffer */
  818. buffer = aa_get_buffer(false);
  819. if (!buffer) {
  820. error = -ENOMEM;
  821. goto done;
  822. }
  823. /* Test for onexec first as onexec override other x transitions. */
  824. if (ctx->onexec)
  825. new = handle_onexec(subj_cred, label, ctx->onexec, ctx->token,
  826. bprm, buffer, &cond, &unsafe);
  827. else
  828. new = fn_label_build(label, profile, GFP_KERNEL,
  829. profile_transition(subj_cred, profile, bprm,
  830. buffer,
  831. &cond, &unsafe));
  832. AA_BUG(!new);
  833. if (IS_ERR(new)) {
  834. error = PTR_ERR(new);
  835. goto done;
  836. } else if (!new) {
  837. error = -ENOMEM;
  838. goto done;
  839. }
  840. /* Policy has specified a domain transitions. If no_new_privs and
  841. * confined ensure the transition is to confinement that is subset
  842. * of the confinement when the task entered no new privs.
  843. *
  844. * NOTE: Domain transitions from unconfined and to stacked
  845. * subsets are allowed even when no_new_privs is set because this
  846. * aways results in a further reduction of permissions.
  847. */
  848. if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) &&
  849. !unconfined(label) &&
  850. !aa_label_is_unconfined_subset(new, ctx->nnp)) {
  851. error = -EPERM;
  852. info = "no new privs";
  853. goto audit;
  854. }
  855. if (bprm->unsafe & LSM_UNSAFE_SHARE) {
  856. /* FIXME: currently don't mediate shared state */
  857. ;
  858. }
  859. if (bprm->unsafe & (LSM_UNSAFE_PTRACE)) {
  860. /* TODO: test needs to be profile of label to new */
  861. error = may_change_ptraced_domain(bprm->cred, new, &info);
  862. if (error)
  863. goto audit;
  864. }
  865. if (unsafe) {
  866. if (DEBUG_ON) {
  867. dbg_printk("scrubbing environment variables for %s "
  868. "label=", bprm->filename);
  869. aa_label_printk(new, GFP_KERNEL);
  870. dbg_printk("\n");
  871. }
  872. bprm->secureexec = 1;
  873. }
  874. if (label->proxy != new->proxy) {
  875. /* when transitioning clear unsafe personality bits */
  876. if (DEBUG_ON) {
  877. dbg_printk("apparmor: clearing unsafe personality "
  878. "bits. %s label=", bprm->filename);
  879. aa_label_printk(new, GFP_KERNEL);
  880. dbg_printk("\n");
  881. }
  882. bprm->per_clear |= PER_CLEAR_ON_SETID;
  883. }
  884. aa_put_label(cred_label(bprm->cred));
  885. /* transfer reference, released when cred is freed */
  886. set_cred_label(bprm->cred, new);
  887. done:
  888. aa_put_label(label);
  889. aa_put_buffer(buffer);
  890. return error;
  891. audit:
  892. error = fn_for_each(label, profile,
  893. aa_audit_file(current_cred(), profile, &nullperms,
  894. OP_EXEC, MAY_EXEC,
  895. bprm->filename, NULL, new,
  896. vfsuid_into_kuid(vfsuid), info, error));
  897. aa_put_label(new);
  898. goto done;
  899. }
  900. /*
  901. * Functions for self directed profile change
  902. */
  903. /* helper fn for change_hat
  904. *
  905. * Returns: label for hat transition OR ERR_PTR. Does NOT return NULL
  906. */
  907. static struct aa_label *build_change_hat(const struct cred *subj_cred,
  908. struct aa_profile *profile,
  909. const char *name, bool sibling)
  910. {
  911. struct aa_profile *root, *hat = NULL;
  912. const char *info = NULL;
  913. int error = 0;
  914. if (sibling && PROFILE_IS_HAT(profile)) {
  915. root = aa_get_profile_rcu(&profile->parent);
  916. } else if (!sibling && !PROFILE_IS_HAT(profile)) {
  917. root = aa_get_profile(profile);
  918. } else {
  919. info = "conflicting target types";
  920. error = -EPERM;
  921. goto audit;
  922. }
  923. hat = aa_find_child(root, name);
  924. if (!hat) {
  925. error = -ENOENT;
  926. if (COMPLAIN_MODE(profile)) {
  927. hat = aa_new_learning_profile(profile, true, name,
  928. GFP_KERNEL);
  929. if (!hat) {
  930. info = "failed null profile create";
  931. error = -ENOMEM;
  932. }
  933. }
  934. }
  935. aa_put_profile(root);
  936. audit:
  937. aa_audit_file(subj_cred, profile, &nullperms, OP_CHANGE_HAT,
  938. AA_MAY_CHANGEHAT,
  939. name, hat ? hat->base.hname : NULL,
  940. hat ? &hat->label : NULL, GLOBAL_ROOT_UID, info,
  941. error);
  942. if (!hat || (error && error != -ENOENT))
  943. return ERR_PTR(error);
  944. /* if hat && error - complain mode, already audited and we adjust for
  945. * complain mode allow by returning hat->label
  946. */
  947. return &hat->label;
  948. }
  949. /* helper fn for changing into a hat
  950. *
  951. * Returns: label for hat transition or ERR_PTR. Does not return NULL
  952. */
  953. static struct aa_label *change_hat(const struct cred *subj_cred,
  954. struct aa_label *label, const char *hats[],
  955. int count, int flags)
  956. {
  957. struct aa_profile *profile, *root, *hat = NULL;
  958. struct aa_label *new;
  959. struct label_it it;
  960. bool sibling = false;
  961. const char *name, *info = NULL;
  962. int i, error;
  963. AA_BUG(!label);
  964. AA_BUG(!hats);
  965. AA_BUG(count < 1);
  966. if (PROFILE_IS_HAT(labels_profile(label)))
  967. sibling = true;
  968. /*find first matching hat */
  969. for (i = 0; i < count && !hat; i++) {
  970. name = hats[i];
  971. label_for_each_in_ns(it, labels_ns(label), label, profile) {
  972. if (sibling && PROFILE_IS_HAT(profile)) {
  973. root = aa_get_profile_rcu(&profile->parent);
  974. } else if (!sibling && !PROFILE_IS_HAT(profile)) {
  975. root = aa_get_profile(profile);
  976. } else { /* conflicting change type */
  977. info = "conflicting targets types";
  978. error = -EPERM;
  979. goto fail;
  980. }
  981. hat = aa_find_child(root, name);
  982. aa_put_profile(root);
  983. if (!hat) {
  984. if (!COMPLAIN_MODE(profile))
  985. goto outer_continue;
  986. /* complain mode succeed as if hat */
  987. } else if (!PROFILE_IS_HAT(hat)) {
  988. info = "target not hat";
  989. error = -EPERM;
  990. aa_put_profile(hat);
  991. goto fail;
  992. }
  993. aa_put_profile(hat);
  994. }
  995. /* found a hat for all profiles in ns */
  996. goto build;
  997. outer_continue:
  998. ;
  999. }
  1000. /* no hats that match, find appropriate error
  1001. *
  1002. * In complain mode audit of the failure is based off of the first
  1003. * hat supplied. This is done due how userspace interacts with
  1004. * change_hat.
  1005. */
  1006. name = NULL;
  1007. label_for_each_in_ns(it, labels_ns(label), label, profile) {
  1008. if (!list_empty(&profile->base.profiles)) {
  1009. info = "hat not found";
  1010. error = -ENOENT;
  1011. goto fail;
  1012. }
  1013. }
  1014. info = "no hats defined";
  1015. error = -ECHILD;
  1016. fail:
  1017. label_for_each_in_ns(it, labels_ns(label), label, profile) {
  1018. /*
  1019. * no target as it has failed to be found or built
  1020. *
  1021. * change_hat uses probing and should not log failures
  1022. * related to missing hats
  1023. */
  1024. /* TODO: get rid of GLOBAL_ROOT_UID */
  1025. if (count > 1 || COMPLAIN_MODE(profile)) {
  1026. aa_audit_file(subj_cred, profile, &nullperms,
  1027. OP_CHANGE_HAT,
  1028. AA_MAY_CHANGEHAT, name, NULL, NULL,
  1029. GLOBAL_ROOT_UID, info, error);
  1030. }
  1031. }
  1032. return ERR_PTR(error);
  1033. build:
  1034. new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
  1035. build_change_hat(subj_cred, profile, name,
  1036. sibling),
  1037. aa_get_label(&profile->label));
  1038. if (!new) {
  1039. info = "label build failed";
  1040. error = -ENOMEM;
  1041. goto fail;
  1042. } /* else if (IS_ERR) build_change_hat has logged error so return new */
  1043. return new;
  1044. }
  1045. /**
  1046. * aa_change_hat - change hat to/from subprofile
  1047. * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
  1048. * @count: number of hat names in @hats
  1049. * @token: magic value to validate the hat change
  1050. * @flags: flags affecting behavior of the change
  1051. *
  1052. * Returns %0 on success, error otherwise.
  1053. *
  1054. * Change to the first profile specified in @hats that exists, and store
  1055. * the @hat_magic in the current task context. If the count == 0 and the
  1056. * @token matches that stored in the current task context, return to the
  1057. * top level profile.
  1058. *
  1059. * change_hat only applies to profiles in the current ns, and each profile
  1060. * in the ns must make the same transition otherwise change_hat will fail.
  1061. */
  1062. int aa_change_hat(const char *hats[], int count, u64 token, int flags)
  1063. {
  1064. const struct cred *subj_cred;
  1065. struct aa_task_ctx *ctx = task_ctx(current);
  1066. struct aa_label *label, *previous, *new = NULL, *target = NULL;
  1067. struct aa_profile *profile;
  1068. struct aa_perms perms = {};
  1069. const char *info = NULL;
  1070. int error = 0;
  1071. /* released below */
  1072. subj_cred = get_current_cred();
  1073. label = aa_get_newest_cred_label(subj_cred);
  1074. previous = aa_get_newest_label(ctx->previous);
  1075. /*
  1076. * Detect no new privs being set, and store the label it
  1077. * occurred under. Ideally this would happen when nnp
  1078. * is set but there isn't a good way to do that yet.
  1079. *
  1080. * Testing for unconfined must be done before the subset test
  1081. */
  1082. if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
  1083. ctx->nnp = aa_get_label(label);
  1084. if (unconfined(label)) {
  1085. info = "unconfined can not change_hat";
  1086. error = -EPERM;
  1087. goto fail;
  1088. }
  1089. if (count) {
  1090. new = change_hat(subj_cred, label, hats, count, flags);
  1091. AA_BUG(!new);
  1092. if (IS_ERR(new)) {
  1093. error = PTR_ERR(new);
  1094. new = NULL;
  1095. /* already audited */
  1096. goto out;
  1097. }
  1098. /* target cred is the same as current except new label */
  1099. error = may_change_ptraced_domain(subj_cred, new, &info);
  1100. if (error)
  1101. goto fail;
  1102. /*
  1103. * no new privs prevents domain transitions that would
  1104. * reduce restrictions.
  1105. */
  1106. if (task_no_new_privs(current) && !unconfined(label) &&
  1107. !aa_label_is_unconfined_subset(new, ctx->nnp)) {
  1108. /* not an apparmor denial per se, so don't log it */
  1109. AA_DEBUG("no_new_privs - change_hat denied");
  1110. error = -EPERM;
  1111. goto out;
  1112. }
  1113. if (flags & AA_CHANGE_TEST)
  1114. goto out;
  1115. target = new;
  1116. error = aa_set_current_hat(new, token);
  1117. if (error == -EACCES)
  1118. /* kill task in case of brute force attacks */
  1119. goto kill;
  1120. } else if (previous && !(flags & AA_CHANGE_TEST)) {
  1121. /*
  1122. * no new privs prevents domain transitions that would
  1123. * reduce restrictions.
  1124. */
  1125. if (task_no_new_privs(current) && !unconfined(label) &&
  1126. !aa_label_is_unconfined_subset(previous, ctx->nnp)) {
  1127. /* not an apparmor denial per se, so don't log it */
  1128. AA_DEBUG("no_new_privs - change_hat denied");
  1129. error = -EPERM;
  1130. goto out;
  1131. }
  1132. /* Return to saved label. Kill task if restore fails
  1133. * to avoid brute force attacks
  1134. */
  1135. target = previous;
  1136. error = aa_restore_previous_label(token);
  1137. if (error) {
  1138. if (error == -EACCES)
  1139. goto kill;
  1140. goto fail;
  1141. }
  1142. } /* else ignore @flags && restores when there is no saved profile */
  1143. out:
  1144. aa_put_label(new);
  1145. aa_put_label(previous);
  1146. aa_put_label(label);
  1147. put_cred(subj_cred);
  1148. return error;
  1149. kill:
  1150. info = "failed token match";
  1151. perms.kill = AA_MAY_CHANGEHAT;
  1152. fail:
  1153. fn_for_each_in_ns(label, profile,
  1154. aa_audit_file(subj_cred, profile, &perms, OP_CHANGE_HAT,
  1155. AA_MAY_CHANGEHAT, NULL, NULL, target,
  1156. GLOBAL_ROOT_UID, info, error));
  1157. goto out;
  1158. }
  1159. static int change_profile_perms_wrapper(const char *op, const char *name,
  1160. const struct cred *subj_cred,
  1161. struct aa_profile *profile,
  1162. struct aa_label *target, bool stack,
  1163. u32 request, struct aa_perms *perms)
  1164. {
  1165. struct aa_ruleset *rules = list_first_entry(&profile->rules,
  1166. typeof(*rules), list);
  1167. const char *info = NULL;
  1168. int error = 0;
  1169. if (!error)
  1170. error = change_profile_perms(profile, target, stack, request,
  1171. rules->file->start[AA_CLASS_FILE],
  1172. perms);
  1173. if (error)
  1174. error = aa_audit_file(subj_cred, profile, perms, op, request,
  1175. name,
  1176. NULL, target, GLOBAL_ROOT_UID, info,
  1177. error);
  1178. return error;
  1179. }
  1180. static const char *stack_msg = "change_profile unprivileged unconfined converted to stacking";
  1181. /**
  1182. * aa_change_profile - perform a one-way profile transition
  1183. * @fqname: name of profile may include namespace (NOT NULL)
  1184. * @flags: flags affecting change behavior
  1185. *
  1186. * Change to new profile @name. Unlike with hats, there is no way
  1187. * to change back. If @name isn't specified the current profile name is
  1188. * used.
  1189. * If @onexec then the transition is delayed until
  1190. * the next exec.
  1191. *
  1192. * Returns %0 on success, error otherwise.
  1193. */
  1194. int aa_change_profile(const char *fqname, int flags)
  1195. {
  1196. struct aa_label *label, *new = NULL, *target = NULL;
  1197. struct aa_profile *profile;
  1198. struct aa_perms perms = {};
  1199. const char *info = NULL;
  1200. const char *auditname = fqname; /* retain leading & if stack */
  1201. bool stack = flags & AA_CHANGE_STACK;
  1202. struct aa_task_ctx *ctx = task_ctx(current);
  1203. const struct cred *subj_cred = get_current_cred();
  1204. int error = 0;
  1205. char *op;
  1206. u32 request;
  1207. label = aa_get_current_label();
  1208. /*
  1209. * Detect no new privs being set, and store the label it
  1210. * occurred under. Ideally this would happen when nnp
  1211. * is set but there isn't a good way to do that yet.
  1212. *
  1213. * Testing for unconfined must be done before the subset test
  1214. */
  1215. if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
  1216. ctx->nnp = aa_get_label(label);
  1217. if (!fqname || !*fqname) {
  1218. aa_put_label(label);
  1219. AA_DEBUG("no profile name");
  1220. return -EINVAL;
  1221. }
  1222. if (flags & AA_CHANGE_ONEXEC) {
  1223. request = AA_MAY_ONEXEC;
  1224. if (stack)
  1225. op = OP_STACK_ONEXEC;
  1226. else
  1227. op = OP_CHANGE_ONEXEC;
  1228. } else {
  1229. request = AA_MAY_CHANGE_PROFILE;
  1230. if (stack)
  1231. op = OP_STACK;
  1232. else
  1233. op = OP_CHANGE_PROFILE;
  1234. }
  1235. /* This should move to a per profile test. Requires pushing build
  1236. * into callback
  1237. */
  1238. if (!stack && unconfined(label) &&
  1239. label == &labels_ns(label)->unconfined->label &&
  1240. aa_unprivileged_unconfined_restricted &&
  1241. /* TODO: refactor so this check is a fn */
  1242. cap_capable(current_cred(), &init_user_ns, CAP_MAC_OVERRIDE,
  1243. CAP_OPT_NOAUDIT)) {
  1244. /* regardless of the request in this case apparmor
  1245. * stacks against unconfined so admin set policy can't be
  1246. * by-passed
  1247. */
  1248. stack = true;
  1249. perms.audit = request;
  1250. (void) fn_for_each_in_ns(label, profile,
  1251. aa_audit_file(subj_cred, profile, &perms, op,
  1252. request, auditname, NULL, target,
  1253. GLOBAL_ROOT_UID, stack_msg, 0));
  1254. perms.audit = 0;
  1255. }
  1256. if (*fqname == '&') {
  1257. stack = true;
  1258. /* don't have label_parse() do stacking */
  1259. fqname++;
  1260. }
  1261. target = aa_label_parse(label, fqname, GFP_KERNEL, true, false);
  1262. if (IS_ERR(target)) {
  1263. struct aa_profile *tprofile;
  1264. info = "label not found";
  1265. error = PTR_ERR(target);
  1266. target = NULL;
  1267. /*
  1268. * TODO: fixme using labels_profile is not right - do profile
  1269. * per complain profile
  1270. */
  1271. if ((flags & AA_CHANGE_TEST) ||
  1272. !COMPLAIN_MODE(labels_profile(label)))
  1273. goto audit;
  1274. /* released below */
  1275. tprofile = aa_new_learning_profile(labels_profile(label), false,
  1276. fqname, GFP_KERNEL);
  1277. if (!tprofile) {
  1278. info = "failed null profile create";
  1279. error = -ENOMEM;
  1280. goto audit;
  1281. }
  1282. target = &tprofile->label;
  1283. goto check;
  1284. }
  1285. /*
  1286. * self directed transitions only apply to current policy ns
  1287. * TODO: currently requiring perms for stacking and straight change
  1288. * stacking doesn't strictly need this. Determine how much
  1289. * we want to loosen this restriction for stacking
  1290. *
  1291. * if (!stack) {
  1292. */
  1293. error = fn_for_each_in_ns(label, profile,
  1294. change_profile_perms_wrapper(op, auditname,
  1295. subj_cred,
  1296. profile, target, stack,
  1297. request, &perms));
  1298. if (error)
  1299. /* auditing done in change_profile_perms_wrapper */
  1300. goto out;
  1301. /* } */
  1302. check:
  1303. /* check if tracing task is allowed to trace target domain */
  1304. error = may_change_ptraced_domain(subj_cred, target, &info);
  1305. if (error && !fn_for_each_in_ns(label, profile,
  1306. COMPLAIN_MODE(profile)))
  1307. goto audit;
  1308. /* TODO: add permission check to allow this
  1309. * if ((flags & AA_CHANGE_ONEXEC) && !current_is_single_threaded()) {
  1310. * info = "not a single threaded task";
  1311. * error = -EACCES;
  1312. * goto audit;
  1313. * }
  1314. */
  1315. if (flags & AA_CHANGE_TEST)
  1316. goto out;
  1317. /* stacking is always a subset, so only check the nonstack case */
  1318. if (!stack) {
  1319. new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
  1320. aa_get_label(target),
  1321. aa_get_label(&profile->label));
  1322. /*
  1323. * no new privs prevents domain transitions that would
  1324. * reduce restrictions.
  1325. */
  1326. if (task_no_new_privs(current) && !unconfined(label) &&
  1327. !aa_label_is_unconfined_subset(new, ctx->nnp)) {
  1328. /* not an apparmor denial per se, so don't log it */
  1329. AA_DEBUG("no_new_privs - change_hat denied");
  1330. error = -EPERM;
  1331. goto out;
  1332. }
  1333. }
  1334. if (!(flags & AA_CHANGE_ONEXEC)) {
  1335. /* only transition profiles in the current ns */
  1336. if (stack)
  1337. new = aa_label_merge(label, target, GFP_KERNEL);
  1338. if (IS_ERR_OR_NULL(new)) {
  1339. info = "failed to build target label";
  1340. if (!new)
  1341. error = -ENOMEM;
  1342. else
  1343. error = PTR_ERR(new);
  1344. new = NULL;
  1345. perms.allow = 0;
  1346. goto audit;
  1347. }
  1348. error = aa_replace_current_label(new);
  1349. } else {
  1350. if (new) {
  1351. aa_put_label(new);
  1352. new = NULL;
  1353. }
  1354. /* full transition will be built in exec path */
  1355. aa_set_current_onexec(target, stack);
  1356. }
  1357. audit:
  1358. error = fn_for_each_in_ns(label, profile,
  1359. aa_audit_file(subj_cred,
  1360. profile, &perms, op, request, auditname,
  1361. NULL, new ? new : target,
  1362. GLOBAL_ROOT_UID, info, error));
  1363. out:
  1364. aa_put_label(new);
  1365. aa_put_label(target);
  1366. aa_put_label(label);
  1367. put_cred(subj_cred);
  1368. return error;
  1369. }