apparmorfs.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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_STRING:
  131. seq_printf(seq, "%s\n", fs_file->v.string);
  132. break;
  133. case AA_FS_TYPE_U64:
  134. seq_printf(seq, "%#08lx\n", fs_file->v.u64);
  135. break;
  136. default:
  137. /* Ignore unpritable entry types. */
  138. break;
  139. }
  140. return 0;
  141. }
  142. static int aa_fs_seq_open(struct inode *inode, struct file *file)
  143. {
  144. return single_open(file, aa_fs_seq_show, inode->i_private);
  145. }
  146. const struct file_operations aa_fs_seq_file_ops = {
  147. .owner = THIS_MODULE,
  148. .open = aa_fs_seq_open,
  149. .read = seq_read,
  150. .llseek = seq_lseek,
  151. .release = single_release,
  152. };
  153. /** Base file system setup **/
  154. static struct aa_fs_entry aa_fs_entry_file[] = {
  155. AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
  156. "link lock"),
  157. { }
  158. };
  159. static struct aa_fs_entry aa_fs_entry_domain[] = {
  160. AA_FS_FILE_BOOLEAN("change_hat", 1),
  161. AA_FS_FILE_BOOLEAN("change_hatv", 1),
  162. AA_FS_FILE_BOOLEAN("change_onexec", 1),
  163. AA_FS_FILE_BOOLEAN("change_profile", 1),
  164. { }
  165. };
  166. static struct aa_fs_entry aa_fs_entry_features[] = {
  167. AA_FS_DIR("domain", aa_fs_entry_domain),
  168. AA_FS_DIR("file", aa_fs_entry_file),
  169. AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),
  170. { }
  171. };
  172. static struct aa_fs_entry aa_fs_entry_apparmor[] = {
  173. AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
  174. AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
  175. AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
  176. AA_FS_DIR("features", aa_fs_entry_features),
  177. { }
  178. };
  179. static struct aa_fs_entry aa_fs_entry =
  180. AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
  181. /**
  182. * aafs_create_file - create a file entry in the apparmor securityfs
  183. * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
  184. * @parent: the parent dentry in the securityfs
  185. *
  186. * Use aafs_remove_file to remove entries created with this fn.
  187. */
  188. static int __init aafs_create_file(struct aa_fs_entry *fs_file,
  189. struct dentry *parent)
  190. {
  191. int error = 0;
  192. fs_file->dentry = securityfs_create_file(fs_file->name,
  193. S_IFREG | fs_file->mode,
  194. parent, fs_file,
  195. fs_file->file_ops);
  196. if (IS_ERR(fs_file->dentry)) {
  197. error = PTR_ERR(fs_file->dentry);
  198. fs_file->dentry = NULL;
  199. }
  200. return error;
  201. }
  202. /**
  203. * aafs_create_dir - recursively create a directory entry in the securityfs
  204. * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
  205. * @parent: the parent dentry in the securityfs
  206. *
  207. * Use aafs_remove_dir to remove entries created with this fn.
  208. */
  209. static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
  210. struct dentry *parent)
  211. {
  212. int error;
  213. struct aa_fs_entry *fs_file;
  214. fs_dir->dentry = securityfs_create_dir(fs_dir->name, parent);
  215. if (IS_ERR(fs_dir->dentry)) {
  216. error = PTR_ERR(fs_dir->dentry);
  217. fs_dir->dentry = NULL;
  218. goto failed;
  219. }
  220. for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) {
  221. if (fs_file->v_type == AA_FS_TYPE_DIR)
  222. error = aafs_create_dir(fs_file, fs_dir->dentry);
  223. else
  224. error = aafs_create_file(fs_file, fs_dir->dentry);
  225. if (error)
  226. goto failed;
  227. }
  228. return 0;
  229. failed:
  230. return error;
  231. }
  232. /**
  233. * aafs_remove_file - drop a single file entry in the apparmor securityfs
  234. * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
  235. */
  236. static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
  237. {
  238. if (!fs_file->dentry)
  239. return;
  240. securityfs_remove(fs_file->dentry);
  241. fs_file->dentry = NULL;
  242. }
  243. /**
  244. * aafs_remove_dir - recursively drop a directory entry from the securityfs
  245. * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
  246. */
  247. static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
  248. {
  249. struct aa_fs_entry *fs_file;
  250. for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) {
  251. if (fs_file->v_type == AA_FS_TYPE_DIR)
  252. aafs_remove_dir(fs_file);
  253. else
  254. aafs_remove_file(fs_file);
  255. }
  256. aafs_remove_file(fs_dir);
  257. }
  258. /**
  259. * aa_destroy_aafs - cleanup and free aafs
  260. *
  261. * releases dentries allocated by aa_create_aafs
  262. */
  263. void __init aa_destroy_aafs(void)
  264. {
  265. aafs_remove_dir(&aa_fs_entry);
  266. }
  267. /**
  268. * aa_create_aafs - create the apparmor security filesystem
  269. *
  270. * dentries created here are released by aa_destroy_aafs
  271. *
  272. * Returns: error on failure
  273. */
  274. static int __init aa_create_aafs(void)
  275. {
  276. int error;
  277. if (!apparmor_initialized)
  278. return 0;
  279. if (aa_fs_entry.dentry) {
  280. AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
  281. return -EEXIST;
  282. }
  283. /* Populate fs tree. */
  284. error = aafs_create_dir(&aa_fs_entry, NULL);
  285. if (error)
  286. goto error;
  287. /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
  288. /* Report that AppArmor fs is enabled */
  289. aa_info_message("AppArmor Filesystem Enabled");
  290. return 0;
  291. error:
  292. aa_destroy_aafs();
  293. AA_ERROR("Error creating AppArmor securityfs\n");
  294. return error;
  295. }
  296. fs_initcall(aa_create_aafs);