apparmorfs.c 8.4 KB

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