lib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. *ns_name = NULL;
  84. *ns_len = 0;
  85. if (!name)
  86. return NULL;
  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. * @str_size: size of the @str buffer
  175. * @chrs: NUL-terminated character buffer of permission characters
  176. * @mask: permission mask to convert
  177. */
  178. void aa_perm_mask_to_str(char *str, size_t str_size, const char *chrs, u32 mask)
  179. {
  180. unsigned int i, perm = 1;
  181. size_t num_chrs = strlen(chrs);
  182. for (i = 0; i < num_chrs; perm <<= 1, i++) {
  183. if (mask & perm) {
  184. /* Ensure that one byte is left for NUL-termination */
  185. if (WARN_ON_ONCE(str_size <= 1))
  186. break;
  187. *str++ = chrs[i];
  188. str_size--;
  189. }
  190. }
  191. *str = '\0';
  192. }
  193. void aa_audit_perm_names(struct audit_buffer *ab, const char * const *names,
  194. u32 mask)
  195. {
  196. const char *fmt = "%s";
  197. unsigned int i, perm = 1;
  198. bool prev = false;
  199. for (i = 0; i < 32; perm <<= 1, i++) {
  200. if (mask & perm) {
  201. audit_log_format(ab, fmt, names[i]);
  202. if (!prev) {
  203. prev = true;
  204. fmt = " %s";
  205. }
  206. }
  207. }
  208. }
  209. void aa_audit_perm_mask(struct audit_buffer *ab, u32 mask, const char *chrs,
  210. u32 chrsmask, const char * const *names, u32 namesmask)
  211. {
  212. char str[33];
  213. audit_log_format(ab, "\"");
  214. if ((mask & chrsmask) && chrs) {
  215. aa_perm_mask_to_str(str, sizeof(str), chrs, mask & chrsmask);
  216. mask &= ~chrsmask;
  217. audit_log_format(ab, "%s", str);
  218. if (mask & namesmask)
  219. audit_log_format(ab, " ");
  220. }
  221. if ((mask & namesmask) && names)
  222. aa_audit_perm_names(ab, names, mask & namesmask);
  223. audit_log_format(ab, "\"");
  224. }
  225. /**
  226. * aa_audit_perms_cb - generic callback fn for auditing perms
  227. * @ab: audit buffer (NOT NULL)
  228. * @va: audit struct to audit values of (NOT NULL)
  229. */
  230. static void aa_audit_perms_cb(struct audit_buffer *ab, void *va)
  231. {
  232. struct common_audit_data *sa = va;
  233. if (aad(sa)->request) {
  234. audit_log_format(ab, " requested_mask=");
  235. aa_audit_perm_mask(ab, aad(sa)->request, aa_file_perm_chrs,
  236. PERMS_CHRS_MASK, aa_file_perm_names,
  237. PERMS_NAMES_MASK);
  238. }
  239. if (aad(sa)->denied) {
  240. audit_log_format(ab, "denied_mask=");
  241. aa_audit_perm_mask(ab, aad(sa)->denied, aa_file_perm_chrs,
  242. PERMS_CHRS_MASK, aa_file_perm_names,
  243. PERMS_NAMES_MASK);
  244. }
  245. audit_log_format(ab, " peer=");
  246. aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
  247. FLAGS_NONE, GFP_ATOMIC);
  248. }
  249. /**
  250. * aa_apply_modes_to_perms - apply namespace and profile flags to perms
  251. * @profile: that perms where computed from
  252. * @perms: perms to apply mode modifiers to
  253. *
  254. * TODO: split into profile and ns based flags for when accumulating perms
  255. */
  256. void aa_apply_modes_to_perms(struct aa_profile *profile, struct aa_perms *perms)
  257. {
  258. switch (AUDIT_MODE(profile)) {
  259. case AUDIT_ALL:
  260. perms->audit = ALL_PERMS_MASK;
  261. /* fall through */
  262. case AUDIT_NOQUIET:
  263. perms->quiet = 0;
  264. break;
  265. case AUDIT_QUIET:
  266. perms->audit = 0;
  267. /* fall through */
  268. case AUDIT_QUIET_DENIED:
  269. perms->quiet = ALL_PERMS_MASK;
  270. break;
  271. }
  272. if (KILL_MODE(profile))
  273. perms->kill = ALL_PERMS_MASK;
  274. else if (COMPLAIN_MODE(profile))
  275. perms->complain = ALL_PERMS_MASK;
  276. /*
  277. * TODO:
  278. * else if (PROMPT_MODE(profile))
  279. * perms->prompt = ALL_PERMS_MASK;
  280. */
  281. }
  282. static u32 map_other(u32 x)
  283. {
  284. return ((x & 0x3) << 8) | /* SETATTR/GETATTR */
  285. ((x & 0x1c) << 18) | /* ACCEPT/BIND/LISTEN */
  286. ((x & 0x60) << 19); /* SETOPT/GETOPT */
  287. }
  288. void aa_compute_perms(struct aa_dfa *dfa, unsigned int state,
  289. struct aa_perms *perms)
  290. {
  291. *perms = (struct aa_perms) {
  292. .allow = dfa_user_allow(dfa, state),
  293. .audit = dfa_user_audit(dfa, state),
  294. .quiet = dfa_user_quiet(dfa, state),
  295. };
  296. /* for v5 perm mapping in the policydb, the other set is used
  297. * to extend the general perm set
  298. */
  299. perms->allow |= map_other(dfa_other_allow(dfa, state));
  300. perms->audit |= map_other(dfa_other_audit(dfa, state));
  301. perms->quiet |= map_other(dfa_other_quiet(dfa, state));
  302. // perms->xindex = dfa_user_xindex(dfa, state);
  303. }
  304. /**
  305. * aa_perms_accum_raw - accumulate perms with out masking off overlapping perms
  306. * @accum - perms struct to accumulate into
  307. * @addend - perms struct to add to @accum
  308. */
  309. void aa_perms_accum_raw(struct aa_perms *accum, struct aa_perms *addend)
  310. {
  311. accum->deny |= addend->deny;
  312. accum->allow &= addend->allow & ~addend->deny;
  313. accum->audit |= addend->audit & addend->allow;
  314. accum->quiet &= addend->quiet & ~addend->allow;
  315. accum->kill |= addend->kill & ~addend->allow;
  316. accum->stop |= addend->stop & ~addend->allow;
  317. accum->complain |= addend->complain & ~addend->allow & ~addend->deny;
  318. accum->cond |= addend->cond & ~addend->allow & ~addend->deny;
  319. accum->hide &= addend->hide & ~addend->allow;
  320. accum->prompt |= addend->prompt & ~addend->allow & ~addend->deny;
  321. }
  322. /**
  323. * aa_perms_accum - accumulate perms, masking off overlapping perms
  324. * @accum - perms struct to accumulate into
  325. * @addend - perms struct to add to @accum
  326. */
  327. void aa_perms_accum(struct aa_perms *accum, struct aa_perms *addend)
  328. {
  329. accum->deny |= addend->deny;
  330. accum->allow &= addend->allow & ~accum->deny;
  331. accum->audit |= addend->audit & accum->allow;
  332. accum->quiet &= addend->quiet & ~accum->allow;
  333. accum->kill |= addend->kill & ~accum->allow;
  334. accum->stop |= addend->stop & ~accum->allow;
  335. accum->complain |= addend->complain & ~accum->allow & ~accum->deny;
  336. accum->cond |= addend->cond & ~accum->allow & ~accum->deny;
  337. accum->hide &= addend->hide & ~accum->allow;
  338. accum->prompt |= addend->prompt & ~accum->allow & ~accum->deny;
  339. }
  340. void aa_profile_match_label(struct aa_profile *profile, struct aa_label *label,
  341. int type, u32 request, struct aa_perms *perms)
  342. {
  343. /* TODO: doesn't yet handle extended types */
  344. unsigned int state;
  345. state = aa_dfa_next(profile->policy.dfa,
  346. profile->policy.start[AA_CLASS_LABEL],
  347. type);
  348. aa_label_match(profile, label, state, false, request, perms);
  349. }
  350. /* currently unused */
  351. int aa_profile_label_perm(struct aa_profile *profile, struct aa_profile *target,
  352. u32 request, int type, u32 *deny,
  353. struct common_audit_data *sa)
  354. {
  355. struct aa_perms perms;
  356. aad(sa)->label = &profile->label;
  357. aad(sa)->peer = &target->label;
  358. aad(sa)->request = request;
  359. aa_profile_match_label(profile, &target->label, type, request, &perms);
  360. aa_apply_modes_to_perms(profile, &perms);
  361. *deny |= request & perms.deny;
  362. return aa_check_perms(profile, &perms, request, sa, aa_audit_perms_cb);
  363. }
  364. /**
  365. * aa_check_perms - do audit mode selection based on perms set
  366. * @profile: profile being checked
  367. * @perms: perms computed for the request
  368. * @request: requested perms
  369. * @deny: Returns: explicit deny set
  370. * @sa: initialized audit structure (MAY BE NULL if not auditing)
  371. * @cb: callback fn for type specific fields (MAY BE NULL)
  372. *
  373. * Returns: 0 if permission else error code
  374. *
  375. * Note: profile audit modes need to be set before calling by setting the
  376. * perm masks appropriately.
  377. *
  378. * If not auditing then complain mode is not enabled and the
  379. * error code will indicate whether there was an explicit deny
  380. * with a positive value.
  381. */
  382. int aa_check_perms(struct aa_profile *profile, struct aa_perms *perms,
  383. u32 request, struct common_audit_data *sa,
  384. void (*cb)(struct audit_buffer *, void *))
  385. {
  386. int type, error;
  387. u32 denied = request & (~perms->allow | perms->deny);
  388. if (likely(!denied)) {
  389. /* mask off perms that are not being force audited */
  390. request &= perms->audit;
  391. if (!request || !sa)
  392. return 0;
  393. type = AUDIT_APPARMOR_AUDIT;
  394. error = 0;
  395. } else {
  396. error = -EACCES;
  397. if (denied & perms->kill)
  398. type = AUDIT_APPARMOR_KILL;
  399. else if (denied == (denied & perms->complain))
  400. type = AUDIT_APPARMOR_ALLOWED;
  401. else
  402. type = AUDIT_APPARMOR_DENIED;
  403. if (denied == (denied & perms->hide))
  404. error = -ENOENT;
  405. denied &= ~perms->quiet;
  406. if (!sa || !denied)
  407. return error;
  408. }
  409. if (sa) {
  410. aad(sa)->label = &profile->label;
  411. aad(sa)->request = request;
  412. aad(sa)->denied = denied;
  413. aad(sa)->error = error;
  414. aa_audit_msg(type, sa, cb);
  415. }
  416. if (type == AUDIT_APPARMOR_ALLOWED)
  417. error = 0;
  418. return error;
  419. }
  420. /**
  421. * aa_policy_init - initialize a policy structure
  422. * @policy: policy to initialize (NOT NULL)
  423. * @prefix: prefix name if any is required. (MAYBE NULL)
  424. * @name: name of the policy, init will make a copy of it (NOT NULL)
  425. * @gfp: allocation mode
  426. *
  427. * Note: this fn creates a copy of strings passed in
  428. *
  429. * Returns: true if policy init successful
  430. */
  431. bool aa_policy_init(struct aa_policy *policy, const char *prefix,
  432. const char *name, gfp_t gfp)
  433. {
  434. char *hname;
  435. /* freed by policy_free */
  436. if (prefix) {
  437. hname = aa_str_alloc(strlen(prefix) + strlen(name) + 3, gfp);
  438. if (hname)
  439. sprintf(hname, "%s//%s", prefix, name);
  440. } else {
  441. hname = aa_str_alloc(strlen(name) + 1, gfp);
  442. if (hname)
  443. strcpy(hname, name);
  444. }
  445. if (!hname)
  446. return false;
  447. policy->hname = hname;
  448. /* base.name is a substring of fqname */
  449. policy->name = basename(policy->hname);
  450. INIT_LIST_HEAD(&policy->list);
  451. INIT_LIST_HEAD(&policy->profiles);
  452. return true;
  453. }
  454. /**
  455. * aa_policy_destroy - free the elements referenced by @policy
  456. * @policy: policy that is to have its elements freed (NOT NULL)
  457. */
  458. void aa_policy_destroy(struct aa_policy *policy)
  459. {
  460. AA_BUG(on_list_rcu(&policy->profiles));
  461. AA_BUG(on_list_rcu(&policy->list));
  462. /* don't free name as its a subset of hname */
  463. aa_put_str(policy->hname);
  464. }