apparmorfs.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor /sys/kernel/security/apparmor interface functions
  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/security.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/module.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/namei.h>
  20. #include "include/apparmor.h"
  21. #include "include/apparmorfs.h"
  22. #include "include/audit.h"
  23. #include "include/context.h"
  24. #include "include/policy.h"
  25. /**
  26. * aa_simple_write_to_buffer - common routine for getting policy from user
  27. * @op: operation doing the user buffer copy
  28. * @userbuf: user buffer to copy data from (NOT NULL)
  29. * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
  30. * @copy_size: size of data to copy from user buffer
  31. * @pos: position write is at in the file (NOT NULL)
  32. *
  33. * Returns: kernel buffer containing copy of user buffer data or an
  34. * ERR_PTR on failure.
  35. */
  36. static char *aa_simple_write_to_buffer(int op, const char __user *userbuf,
  37. size_t alloc_size, size_t copy_size,
  38. loff_t *pos)
  39. {
  40. char *data;
  41. BUG_ON(copy_size > alloc_size);
  42. if (*pos != 0)
  43. /* only writes from pos 0, that is complete writes */
  44. return ERR_PTR(-ESPIPE);
  45. /*
  46. * Don't allow profile load/replace/remove from profiles that don't
  47. * have CAP_MAC_ADMIN
  48. */
  49. if (!aa_may_manage_policy(op))
  50. return ERR_PTR(-EACCES);
  51. /* freed by caller to simple_write_to_buffer */
  52. data = kvmalloc(alloc_size);
  53. if (data == NULL)
  54. return ERR_PTR(-ENOMEM);
  55. if (copy_from_user(data, userbuf, copy_size)) {
  56. kvfree(data);
  57. return ERR_PTR(-EFAULT);
  58. }
  59. return data;
  60. }
  61. /* .load file hook fn to load policy */
  62. static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
  63. loff_t *pos)
  64. {
  65. char *data;
  66. ssize_t error;
  67. data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
  68. error = PTR_ERR(data);
  69. if (!IS_ERR(data)) {
  70. error = aa_replace_profiles(data, size, PROF_ADD);
  71. kvfree(data);
  72. }
  73. return error;
  74. }
  75. static const struct file_operations aa_fs_profile_load = {
  76. .write = profile_load,
  77. .llseek = default_llseek,
  78. };
  79. /* .replace file hook fn to load and/or replace policy */
  80. static ssize_t profile_replace(struct file *f, const char __user *buf,
  81. size_t size, loff_t *pos)
  82. {
  83. char *data;
  84. ssize_t error;
  85. data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
  86. error = PTR_ERR(data);
  87. if (!IS_ERR(data)) {
  88. error = aa_replace_profiles(data, size, PROF_REPLACE);
  89. kvfree(data);
  90. }
  91. return error;
  92. }
  93. static const struct file_operations aa_fs_profile_replace = {
  94. .write = profile_replace,
  95. .llseek = default_llseek,
  96. };
  97. /* .remove file hook fn to remove loaded policy */
  98. static ssize_t profile_remove(struct file *f, const char __user *buf,
  99. size_t size, loff_t *pos)
  100. {
  101. char *data;
  102. ssize_t error;
  103. /*
  104. * aa_remove_profile needs a null terminated string so 1 extra
  105. * byte is allocated and the copied data is null terminated.
  106. */
  107. data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
  108. error = PTR_ERR(data);
  109. if (!IS_ERR(data)) {
  110. data[size] = 0;
  111. error = aa_remove_profiles(data, size);
  112. kvfree(data);
  113. }
  114. return error;
  115. }
  116. static const struct file_operations aa_fs_profile_remove = {
  117. .write = profile_remove,
  118. .llseek = default_llseek,
  119. };
  120. /** Base file system setup **/
  121. static struct aa_fs_entry aa_fs_entry_apparmor[] = {
  122. AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
  123. AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
  124. AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
  125. { }
  126. };
  127. static struct aa_fs_entry aa_fs_entry =
  128. AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
  129. /**
  130. * aafs_create_file - create a file entry in the apparmor securityfs
  131. * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
  132. * @parent: the parent dentry in the securityfs
  133. *
  134. * Use aafs_remove_file to remove entries created with this fn.
  135. */
  136. static int __init aafs_create_file(struct aa_fs_entry *fs_file,
  137. struct dentry *parent)
  138. {
  139. int error = 0;
  140. fs_file->dentry = securityfs_create_file(fs_file->name,
  141. S_IFREG | fs_file->mode,
  142. parent, fs_file,
  143. fs_file->file_ops);
  144. if (IS_ERR(fs_file->dentry)) {
  145. error = PTR_ERR(fs_file->dentry);
  146. fs_file->dentry = NULL;
  147. }
  148. return error;
  149. }
  150. /**
  151. * aafs_create_dir - recursively create a directory entry in the securityfs
  152. * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
  153. * @parent: the parent dentry in the securityfs
  154. *
  155. * Use aafs_remove_dir to remove entries created with this fn.
  156. */
  157. static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
  158. struct dentry *parent)
  159. {
  160. int error;
  161. struct aa_fs_entry *fs_file;
  162. fs_dir->dentry = securityfs_create_dir(fs_dir->name, parent);
  163. if (IS_ERR(fs_dir->dentry)) {
  164. error = PTR_ERR(fs_dir->dentry);
  165. fs_dir->dentry = NULL;
  166. goto failed;
  167. }
  168. for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) {
  169. if (fs_file->v_type == AA_FS_TYPE_DIR)
  170. error = aafs_create_dir(fs_file, fs_dir->dentry);
  171. else
  172. error = aafs_create_file(fs_file, fs_dir->dentry);
  173. if (error)
  174. goto failed;
  175. }
  176. return 0;
  177. failed:
  178. return error;
  179. }
  180. /**
  181. * aafs_remove_file - drop a single file entry in the apparmor securityfs
  182. * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
  183. */
  184. static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
  185. {
  186. if (!fs_file->dentry)
  187. return;
  188. securityfs_remove(fs_file->dentry);
  189. fs_file->dentry = NULL;
  190. }
  191. /**
  192. * aafs_remove_dir - recursively drop a directory entry from the securityfs
  193. * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
  194. */
  195. static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
  196. {
  197. struct aa_fs_entry *fs_file;
  198. for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) {
  199. if (fs_file->v_type == AA_FS_TYPE_DIR)
  200. aafs_remove_dir(fs_file);
  201. else
  202. aafs_remove_file(fs_file);
  203. }
  204. aafs_remove_file(fs_dir);
  205. }
  206. /**
  207. * aa_destroy_aafs - cleanup and free aafs
  208. *
  209. * releases dentries allocated by aa_create_aafs
  210. */
  211. void __init aa_destroy_aafs(void)
  212. {
  213. aafs_remove_dir(&aa_fs_entry);
  214. }
  215. /**
  216. * aa_create_aafs - create the apparmor security filesystem
  217. *
  218. * dentries created here are released by aa_destroy_aafs
  219. *
  220. * Returns: error on failure
  221. */
  222. static int __init aa_create_aafs(void)
  223. {
  224. int error;
  225. if (!apparmor_initialized)
  226. return 0;
  227. if (aa_fs_entry.dentry) {
  228. AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
  229. return -EEXIST;
  230. }
  231. /* Populate fs tree. */
  232. error = aafs_create_dir(&aa_fs_entry, NULL);
  233. if (error)
  234. goto error;
  235. /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
  236. /* Report that AppArmor fs is enabled */
  237. aa_info_message("AppArmor Filesystem Enabled");
  238. return 0;
  239. error:
  240. aa_destroy_aafs();
  241. AA_ERROR("Error creating AppArmor securityfs\n");
  242. return error;
  243. }
  244. fs_initcall(aa_create_aafs);