apparmorfs.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 dentry *aa_fs_dentry __initdata;
  122. static void __init aafs_remove(const char *name)
  123. {
  124. struct dentry *dentry;
  125. dentry = lookup_one_len(name, aa_fs_dentry, strlen(name));
  126. if (!IS_ERR(dentry)) {
  127. securityfs_remove(dentry);
  128. dput(dentry);
  129. }
  130. }
  131. /**
  132. * aafs_create - create an entry in the apparmor filesystem
  133. * @name: name of the entry (NOT NULL)
  134. * @mask: file permission mask of the file
  135. * @fops: file operations for the file (NOT NULL)
  136. *
  137. * Used aafs_remove to remove entries created with this fn.
  138. */
  139. static int __init aafs_create(const char *name, int mask,
  140. const struct file_operations *fops)
  141. {
  142. struct dentry *dentry;
  143. dentry = securityfs_create_file(name, S_IFREG | mask, aa_fs_dentry,
  144. NULL, fops);
  145. return IS_ERR(dentry) ? PTR_ERR(dentry) : 0;
  146. }
  147. /**
  148. * aa_destroy_aafs - cleanup and free aafs
  149. *
  150. * releases dentries allocated by aa_create_aafs
  151. */
  152. void __init aa_destroy_aafs(void)
  153. {
  154. if (aa_fs_dentry) {
  155. aafs_remove(".remove");
  156. aafs_remove(".replace");
  157. aafs_remove(".load");
  158. securityfs_remove(aa_fs_dentry);
  159. aa_fs_dentry = NULL;
  160. }
  161. }
  162. /**
  163. * aa_create_aafs - create the apparmor security filesystem
  164. *
  165. * dentries created here are released by aa_destroy_aafs
  166. *
  167. * Returns: error on failure
  168. */
  169. int __init aa_create_aafs(void)
  170. {
  171. int error;
  172. if (!apparmor_initialized)
  173. return 0;
  174. if (aa_fs_dentry) {
  175. AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
  176. return -EEXIST;
  177. }
  178. aa_fs_dentry = securityfs_create_dir("apparmor", NULL);
  179. if (IS_ERR(aa_fs_dentry)) {
  180. error = PTR_ERR(aa_fs_dentry);
  181. aa_fs_dentry = NULL;
  182. goto error;
  183. }
  184. error = aafs_create(".load", 0640, &aa_fs_profile_load);
  185. if (error)
  186. goto error;
  187. error = aafs_create(".replace", 0640, &aa_fs_profile_replace);
  188. if (error)
  189. goto error;
  190. error = aafs_create(".remove", 0640, &aa_fs_profile_remove);
  191. if (error)
  192. goto error;
  193. /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
  194. /* Report that AppArmor fs is enabled */
  195. aa_info_message("AppArmor Filesystem Enabled");
  196. return 0;
  197. error:
  198. aa_destroy_aafs();
  199. AA_ERROR("Error creating AppArmor securityfs\n");
  200. return error;
  201. }
  202. fs_initcall(aa_create_aafs);