lib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains basic common functions used in AppArmor
  5. *
  6. * Copyright (C) 1998-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/ctype.h>
  15. #include <linux/mm.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/vmalloc.h>
  19. #include "include/audit.h"
  20. #include "include/apparmor.h"
  21. #include "include/lib.h"
  22. #include "include/perms.h"
  23. #include "include/policy.h"
  24. struct aa_perms nullperms;
  25. struct aa_perms allperms = { .allow = ALL_PERMS_MASK,
  26. .quiet = ALL_PERMS_MASK,
  27. .hide = ALL_PERMS_MASK };
  28. /**
  29. * aa_split_fqname - split a fqname into a profile and namespace name
  30. * @fqname: a full qualified name in namespace profile format (NOT NULL)
  31. * @ns_name: pointer to portion of the string containing the ns name (NOT NULL)
  32. *
  33. * Returns: profile name or NULL if one is not specified
  34. *
  35. * Split a namespace name from a profile name (see policy.c for naming
  36. * description). If a portion of the name is missing it returns NULL for
  37. * that portion.
  38. *
  39. * NOTE: may modify the @fqname string. The pointers returned point
  40. * into the @fqname string.
  41. */
  42. char *aa_split_fqname(char *fqname, char **ns_name)
  43. {
  44. char *name = strim(fqname);
  45. *ns_name = NULL;
  46. if (name[0] == ':') {
  47. char *split = strchr(&name[1], ':');
  48. *ns_name = skip_spaces(&name[1]);
  49. if (split) {
  50. /* overwrite ':' with \0 */
  51. *split++ = 0;
  52. if (strncmp(split, "//", 2) == 0)
  53. split += 2;
  54. name = skip_spaces(split);
  55. } else
  56. /* a ns name without a following profile is allowed */
  57. name = NULL;
  58. }
  59. if (name && *name == 0)
  60. name = NULL;
  61. return name;
  62. }
  63. /**
  64. * skipn_spaces - Removes leading whitespace from @str.
  65. * @str: The string to be stripped.
  66. *
  67. * Returns a pointer to the first non-whitespace character in @str.
  68. * if all whitespace will return NULL
  69. */
  70. const char *skipn_spaces(const char *str, size_t n)
  71. {
  72. for (; n && isspace(*str); --n)
  73. ++str;
  74. if (n)
  75. return (char *)str;
  76. return NULL;
  77. }
  78. const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
  79. size_t *ns_len)
  80. {
  81. const char *end = fqname + n;
  82. const char *name = skipn_spaces(fqname, n);
  83. if (!name)
  84. return NULL;
  85. *ns_name = NULL;
  86. *ns_len = 0;
  87. if (name[0] == ':') {
  88. char *split = strnchr(&name[1], end - &name[1], ':');
  89. *ns_name = skipn_spaces(&name[1], end - &name[1]);
  90. if (!*ns_name)
  91. return NULL;
  92. if (split) {
  93. *ns_len = split - *ns_name;
  94. if (*ns_len == 0)
  95. *ns_name = NULL;
  96. split++;
  97. if (end - split > 1 && strncmp(split, "//", 2) == 0)
  98. split += 2;
  99. name = skipn_spaces(split, end - split);
  100. } else {
  101. /* a ns name without a following profile is allowed */
  102. name = NULL;
  103. *ns_len = end - *ns_name;
  104. }
  105. }
  106. if (name && *name == 0)
  107. name = NULL;
  108. return name;
  109. }
  110. /**
  111. * aa_info_message - log a none profile related status message
  112. * @str: message to log
  113. */
  114. void aa_info_message(const char *str)
  115. {
  116. if (audit_enabled) {
  117. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, NULL);
  118. aad(&sa)->info = str;
  119. aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
  120. }
  121. printk(KERN_INFO "AppArmor: %s\n", str);
  122. }
  123. __counted char *aa_str_alloc(int size, gfp_t gfp)
  124. {
  125. struct counted_str *str;
  126. str = kmalloc(sizeof(struct counted_str) + size, gfp);
  127. if (!str)
  128. return NULL;
  129. kref_init(&str->count);
  130. return str->name;
  131. }
  132. void aa_str_kref(struct kref *kref)
  133. {
  134. kfree(container_of(kref, struct counted_str, count));
  135. }
  136. const char aa_file_perm_chrs[] = "xwracd km l ";
  137. const char *aa_file_perm_names[] = {
  138. "exec",
  139. "write",
  140. "read",
  141. "append",
  142. "create",
  143. "delete",
  144. "open",
  145. "rename",
  146. "setattr",
  147. "getattr",
  148. "setcred",
  149. "getcred",
  150. "chmod",
  151. "chown",
  152. "chgrp",
  153. "lock",
  154. "mmap",
  155. "mprot",
  156. "link",
  157. "snapshot",
  158. "unknown",
  159. "unknown",
  160. "unknown",
  161. "unknown",
  162. "unknown",
  163. "unknown",
  164. "unknown",
  165. "unknown",
  166. "stack",
  167. "change_onexec",
  168. "change_profile",
  169. "change_hat",
  170. };
  171. /**
  172. * aa_perm_mask_to_str - convert a perm mask to its short string
  173. * @str: character buffer to store string in (at least 10 characters)
  174. * @mask: permission mask to convert
  175. */
  176. void aa_perm_mask_to_str(char *str, const char *chrs, u32 mask)
  177. {
  178. unsigned int i, perm = 1;
  179. for (i = 0; i < 32; perm <<= 1, i++) {
  180. if (mask & perm)
  181. *str++ = chrs[i];
  182. }
  183. *str = '\0';
  184. }
  185. void aa_audit_perm_names(struct audit_buffer *ab, const char **names, u32 mask)
  186. {
  187. const char *fmt = "%s";
  188. unsigned int i, perm = 1;
  189. bool prev = false;
  190. for (i = 0; i < 32; perm <<= 1, i++) {
  191. if (mask & perm) {
  192. audit_log_format(ab, fmt, names[i]);
  193. if (!prev) {
  194. prev = true;
  195. fmt = " %s";
  196. }
  197. }
  198. }
  199. }
  200. void aa_audit_perm_mask(struct audit_buffer *ab, u32 mask, const char *chrs,
  201. u32 chrsmask, const char **names, u32 namesmask)
  202. {
  203. char str[33];
  204. audit_log_format(ab, "\"");
  205. if ((mask & chrsmask) && chrs) {
  206. aa_perm_mask_to_str(str, chrs, mask & chrsmask);
  207. mask &= ~chrsmask;
  208. audit_log_format(ab, "%s", str);
  209. if (mask & namesmask)
  210. audit_log_format(ab, " ");
  211. }
  212. if ((mask & namesmask) && names)
  213. aa_audit_perm_names(ab, names, mask & namesmask);
  214. audit_log_format(ab, "\"");
  215. }
  216. /**
  217. * aa_audit_perms_cb - generic callback fn for auditing perms
  218. * @ab: audit buffer (NOT NULL)
  219. * @va: audit struct to audit values of (NOT NULL)
  220. */
  221. static void aa_audit_perms_cb(struct audit_buffer *ab, void *va)
  222. {
  223. struct common_audit_data *sa = va;
  224. if (aad(sa)->request) {
  225. audit_log_format(ab, " requested_mask=");
  226. aa_audit_perm_mask(ab, aad(sa)->request, aa_file_perm_chrs,
  227. PERMS_CHRS_MASK, aa_file_perm_names,
  228. PERMS_NAMES_MASK);
  229. }
  230. if (aad(sa)->denied) {
  231. audit_log_format(ab, "denied_mask=");
  232. aa_audit_perm_mask(ab, aad(sa)->denied, aa_file_perm_chrs,
  233. PERMS_CHRS_MASK, aa_file_perm_names,
  234. PERMS_NAMES_MASK);
  235. }
  236. audit_log_format(ab, " peer=");
  237. aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
  238. FLAGS_NONE, GFP_ATOMIC);
  239. }
  240. /**
  241. * aa_apply_modes_to_perms - apply namespace and profile flags to perms
  242. * @profile: that perms where computed from
  243. * @perms: perms to apply mode modifiers to
  244. *
  245. * TODO: split into profile and ns based flags for when accumulating perms
  246. */
  247. void aa_apply_modes_to_perms(struct aa_profile *profile, struct aa_perms *perms)
  248. {
  249. switch (AUDIT_MODE(profile)) {
  250. case AUDIT_ALL:
  251. perms->audit = ALL_PERMS_MASK;
  252. /* fall through */
  253. case AUDIT_NOQUIET:
  254. perms->quiet = 0;
  255. break;
  256. case AUDIT_QUIET:
  257. perms->audit = 0;
  258. /* fall through */
  259. case AUDIT_QUIET_DENIED:
  260. perms->quiet = ALL_PERMS_MASK;
  261. break;
  262. }
  263. if (KILL_MODE(profile))
  264. perms->kill = ALL_PERMS_MASK;
  265. else if (COMPLAIN_MODE(profile))
  266. perms->complain = ALL_PERMS_MASK;
  267. /*
  268. * TODO:
  269. * else if (PROMPT_MODE(profile))
  270. * perms->prompt = ALL_PERMS_MASK;
  271. */
  272. }
  273. static u32 map_other(u32 x)
  274. {
  275. return ((x & 0x3) << 8) | /* SETATTR/GETATTR */
  276. ((x & 0x1c) << 18) | /* ACCEPT/BIND/LISTEN */
  277. ((x & 0x60) << 19); /* SETOPT/GETOPT */
  278. }
  279. void aa_compute_perms(struct aa_dfa *dfa, unsigned int state,
  280. struct aa_perms *perms)
  281. {
  282. perms->deny = 0;
  283. perms->kill = perms->stop = 0;
  284. perms->complain = perms->cond = 0;
  285. perms->hide = 0;
  286. perms->prompt = 0;
  287. perms->allow = dfa_user_allow(dfa, state);
  288. perms->audit = dfa_user_audit(dfa, state);
  289. perms->quiet = dfa_user_quiet(dfa, state);
  290. /* for v5 perm mapping in the policydb, the other set is used
  291. * to extend the general perm set
  292. */
  293. perms->allow |= map_other(dfa_other_allow(dfa, state));
  294. perms->audit |= map_other(dfa_other_audit(dfa, state));
  295. perms->quiet |= map_other(dfa_other_quiet(dfa, state));
  296. // perms->xindex = dfa_user_xindex(dfa, state);
  297. }
  298. /**
  299. * aa_perms_accum_raw - accumulate perms with out masking off overlapping perms
  300. * @accum - perms struct to accumulate into
  301. * @addend - perms struct to add to @accum
  302. */
  303. void aa_perms_accum_raw(struct aa_perms *accum, struct aa_perms *addend)
  304. {
  305. accum->deny |= addend->deny;
  306. accum->allow &= addend->allow & ~addend->deny;
  307. accum->audit |= addend->audit & addend->allow;
  308. accum->quiet &= addend->quiet & ~addend->allow;
  309. accum->kill |= addend->kill & ~addend->allow;
  310. accum->stop |= addend->stop & ~addend->allow;
  311. accum->complain |= addend->complain & ~addend->allow & ~addend->deny;
  312. accum->cond |= addend->cond & ~addend->allow & ~addend->deny;
  313. accum->hide &= addend->hide & ~addend->allow;
  314. accum->prompt |= addend->prompt & ~addend->allow & ~addend->deny;
  315. }
  316. /**
  317. * aa_perms_accum - accumulate perms, masking off overlapping perms
  318. * @accum - perms struct to accumulate into
  319. * @addend - perms struct to add to @accum
  320. */
  321. void aa_perms_accum(struct aa_perms *accum, struct aa_perms *addend)
  322. {
  323. accum->deny |= addend->deny;
  324. accum->allow &= addend->allow & ~accum->deny;
  325. accum->audit |= addend->audit & accum->allow;
  326. accum->quiet &= addend->quiet & ~accum->allow;
  327. accum->kill |= addend->kill & ~accum->allow;
  328. accum->stop |= addend->stop & ~accum->allow;
  329. accum->complain |= addend->complain & ~accum->allow & ~accum->deny;
  330. accum->cond |= addend->cond & ~accum->allow & ~accum->deny;
  331. accum->hide &= addend->hide & ~accum->allow;
  332. accum->prompt |= addend->prompt & ~accum->allow & ~accum->deny;
  333. }
  334. void aa_profile_match_label(struct aa_profile *profile, struct aa_label *label,
  335. int type, u32 request, struct aa_perms *perms)
  336. {
  337. /* TODO: doesn't yet handle extended types */
  338. unsigned int state;
  339. state = aa_dfa_next(profile->policy.dfa,
  340. profile->policy.start[AA_CLASS_LABEL],
  341. type);
  342. aa_label_match(profile, label, state, false, request, perms);
  343. }
  344. /* currently unused */
  345. int aa_profile_label_perm(struct aa_profile *profile, struct aa_profile *target,
  346. u32 request, int type, u32 *deny,
  347. struct common_audit_data *sa)
  348. {
  349. struct aa_perms perms;
  350. aad(sa)->label = &profile->label;
  351. aad(sa)->peer = &target->label;
  352. aad(sa)->request = request;
  353. aa_profile_match_label(profile, &target->label, type, request, &perms);
  354. aa_apply_modes_to_perms(profile, &perms);
  355. *deny |= request & perms.deny;
  356. return aa_check_perms(profile, &perms, request, sa, aa_audit_perms_cb);
  357. }
  358. /**
  359. * aa_check_perms - do audit mode selection based on perms set
  360. * @profile: profile being checked
  361. * @perms: perms computed for the request
  362. * @request: requested perms
  363. * @deny: Returns: explicit deny set
  364. * @sa: initialized audit structure (MAY BE NULL if not auditing)
  365. * @cb: callback fn for tpye specific fields (MAY BE NULL)
  366. *
  367. * Returns: 0 if permission else error code
  368. *
  369. * Note: profile audit modes need to be set before calling by setting the
  370. * perm masks appropriately.
  371. *
  372. * If not auditing then complain mode is not enabled and the
  373. * error code will indicate whether there was an explicit deny
  374. * with a positive value.
  375. */
  376. int aa_check_perms(struct aa_profile *profile, struct aa_perms *perms,
  377. u32 request, struct common_audit_data *sa,
  378. void (*cb)(struct audit_buffer *, void *))
  379. {
  380. int type, error;
  381. bool stop = false;
  382. u32 denied = request & (~perms->allow | perms->deny);
  383. if (likely(!denied)) {
  384. /* mask off perms that are not being force audited */
  385. request &= perms->audit;
  386. if (!request || !sa)
  387. return 0;
  388. type = AUDIT_APPARMOR_AUDIT;
  389. error = 0;
  390. } else {
  391. error = -EACCES;
  392. if (denied & perms->kill)
  393. type = AUDIT_APPARMOR_KILL;
  394. else if (denied == (denied & perms->complain))
  395. type = AUDIT_APPARMOR_ALLOWED;
  396. else
  397. type = AUDIT_APPARMOR_DENIED;
  398. if (denied & perms->stop)
  399. stop = true;
  400. if (denied == (denied & perms->hide))
  401. error = -ENOENT;
  402. denied &= ~perms->quiet;
  403. if (!sa || !denied)
  404. return error;
  405. }
  406. if (sa) {
  407. aad(sa)->label = &profile->label;
  408. aad(sa)->request = request;
  409. aad(sa)->denied = denied;
  410. aad(sa)->error = error;
  411. aa_audit_msg(type, sa, cb);
  412. }
  413. if (type == AUDIT_APPARMOR_ALLOWED)
  414. error = 0;
  415. return error;
  416. }
  417. /**
  418. * aa_policy_init - initialize a policy structure
  419. * @policy: policy to initialize (NOT NULL)
  420. * @prefix: prefix name if any is required. (MAYBE NULL)
  421. * @name: name of the policy, init will make a copy of it (NOT NULL)
  422. * @gfp: allocation mode
  423. *
  424. * Note: this fn creates a copy of strings passed in
  425. *
  426. * Returns: true if policy init successful
  427. */
  428. bool aa_policy_init(struct aa_policy *policy, const char *prefix,
  429. const char *name, gfp_t gfp)
  430. {
  431. char *hname;
  432. /* freed by policy_free */
  433. if (prefix) {
  434. hname = aa_str_alloc(strlen(prefix) + strlen(name) + 3, gfp);
  435. if (hname)
  436. sprintf(hname, "%s//%s", prefix, name);
  437. } else {
  438. hname = aa_str_alloc(strlen(name) + 1, gfp);
  439. if (hname)
  440. strcpy(hname, name);
  441. }
  442. if (!hname)
  443. return false;
  444. policy->hname = hname;
  445. /* base.name is a substring of fqname */
  446. policy->name = basename(policy->hname);
  447. INIT_LIST_HEAD(&policy->list);
  448. INIT_LIST_HEAD(&policy->profiles);
  449. return true;
  450. }
  451. /**
  452. * aa_policy_destroy - free the elements referenced by @policy
  453. * @policy: policy that is to have its elements freed (NOT NULL)
  454. */
  455. void aa_policy_destroy(struct aa_policy *policy)
  456. {
  457. AA_BUG(on_list_rcu(&policy->profiles));
  458. AA_BUG(on_list_rcu(&policy->list));
  459. /* don't free name as its a subset of hname */
  460. aa_put_str(policy->hname);
  461. }