lib.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/policy.h"
  23. /**
  24. * aa_split_fqname - split a fqname into a profile and namespace name
  25. * @fqname: a full qualified name in namespace profile format (NOT NULL)
  26. * @ns_name: pointer to portion of the string containing the ns name (NOT NULL)
  27. *
  28. * Returns: profile name or NULL if one is not specified
  29. *
  30. * Split a namespace name from a profile name (see policy.c for naming
  31. * description). If a portion of the name is missing it returns NULL for
  32. * that portion.
  33. *
  34. * NOTE: may modify the @fqname string. The pointers returned point
  35. * into the @fqname string.
  36. */
  37. char *aa_split_fqname(char *fqname, char **ns_name)
  38. {
  39. char *name = strim(fqname);
  40. *ns_name = NULL;
  41. if (name[0] == ':') {
  42. char *split = strchr(&name[1], ':');
  43. *ns_name = skip_spaces(&name[1]);
  44. if (split) {
  45. /* overwrite ':' with \0 */
  46. *split++ = 0;
  47. if (strncmp(split, "//", 2) == 0)
  48. split += 2;
  49. name = skip_spaces(split);
  50. } else
  51. /* a ns name without a following profile is allowed */
  52. name = NULL;
  53. }
  54. if (name && *name == 0)
  55. name = NULL;
  56. return name;
  57. }
  58. /**
  59. * skipn_spaces - Removes leading whitespace from @str.
  60. * @str: The string to be stripped.
  61. *
  62. * Returns a pointer to the first non-whitespace character in @str.
  63. * if all whitespace will return NULL
  64. */
  65. static const char *skipn_spaces(const char *str, size_t n)
  66. {
  67. for (; n && isspace(*str); --n)
  68. ++str;
  69. if (n)
  70. return (char *)str;
  71. return NULL;
  72. }
  73. const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
  74. size_t *ns_len)
  75. {
  76. const char *end = fqname + n;
  77. const char *name = skipn_spaces(fqname, n);
  78. if (!name)
  79. return NULL;
  80. *ns_name = NULL;
  81. *ns_len = 0;
  82. if (name[0] == ':') {
  83. char *split = strnchr(&name[1], end - &name[1], ':');
  84. *ns_name = skipn_spaces(&name[1], end - &name[1]);
  85. if (!*ns_name)
  86. return NULL;
  87. if (split) {
  88. *ns_len = split - *ns_name;
  89. if (*ns_len == 0)
  90. *ns_name = NULL;
  91. split++;
  92. if (end - split > 1 && strncmp(split, "//", 2) == 0)
  93. split += 2;
  94. name = skipn_spaces(split, end - split);
  95. } else {
  96. /* a ns name without a following profile is allowed */
  97. name = NULL;
  98. *ns_len = end - *ns_name;
  99. }
  100. }
  101. if (name && *name == 0)
  102. name = NULL;
  103. return name;
  104. }
  105. /**
  106. * aa_info_message - log a none profile related status message
  107. * @str: message to log
  108. */
  109. void aa_info_message(const char *str)
  110. {
  111. if (audit_enabled) {
  112. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, NULL);
  113. aad(&sa)->info = str;
  114. aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
  115. }
  116. printk(KERN_INFO "AppArmor: %s\n", str);
  117. }
  118. /**
  119. * __aa_kvmalloc - do allocation preferring kmalloc but falling back to vmalloc
  120. * @size: how many bytes of memory are required
  121. * @flags: the type of memory to allocate (see kmalloc).
  122. *
  123. * Return: allocated buffer or NULL if failed
  124. *
  125. * It is possible that policy being loaded from the user is larger than
  126. * what can be allocated by kmalloc, in those cases fall back to vmalloc.
  127. */
  128. void *__aa_kvmalloc(size_t size, gfp_t flags)
  129. {
  130. void *buffer = NULL;
  131. if (size == 0)
  132. return NULL;
  133. /* do not attempt kmalloc if we need more than 16 pages at once */
  134. if (size <= (16*PAGE_SIZE))
  135. buffer = kmalloc(size, flags | GFP_KERNEL | __GFP_NORETRY |
  136. __GFP_NOWARN);
  137. if (!buffer) {
  138. if (flags & __GFP_ZERO)
  139. buffer = vzalloc(size);
  140. else
  141. buffer = vmalloc(size);
  142. }
  143. return buffer;
  144. }
  145. /**
  146. * aa_policy_init - initialize a policy structure
  147. * @policy: policy to initialize (NOT NULL)
  148. * @prefix: prefix name if any is required. (MAYBE NULL)
  149. * @name: name of the policy, init will make a copy of it (NOT NULL)
  150. *
  151. * Note: this fn creates a copy of strings passed in
  152. *
  153. * Returns: true if policy init successful
  154. */
  155. bool aa_policy_init(struct aa_policy *policy, const char *prefix,
  156. const char *name, gfp_t gfp)
  157. {
  158. /* freed by policy_free */
  159. if (prefix) {
  160. policy->hname = kmalloc(strlen(prefix) + strlen(name) + 3,
  161. gfp);
  162. if (policy->hname)
  163. sprintf((char *)policy->hname, "%s//%s", prefix, name);
  164. } else
  165. policy->hname = kstrdup(name, gfp);
  166. if (!policy->hname)
  167. return 0;
  168. /* base.name is a substring of fqname */
  169. policy->name = basename(policy->hname);
  170. INIT_LIST_HEAD(&policy->list);
  171. INIT_LIST_HEAD(&policy->profiles);
  172. return 1;
  173. }
  174. /**
  175. * aa_policy_destroy - free the elements referenced by @policy
  176. * @policy: policy that is to have its elements freed (NOT NULL)
  177. */
  178. void aa_policy_destroy(struct aa_policy *policy)
  179. {
  180. AA_BUG(on_list_rcu(&policy->profiles));
  181. AA_BUG(on_list_rcu(&policy->list));
  182. /* don't free name as its a subset of hname */
  183. kzfree(policy->hname);
  184. }