lib.c 11 KB

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