domain.c 34 KB

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