lib.c 13 KB

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