domain.c 39 KB

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