apparmorfs.c 8.1 KB

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