apparmorfs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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
  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. if (*pos != 0)
  42. /* only writes from pos 0, that is complete writes */
  43. return ERR_PTR(-ESPIPE);
  44. /*
  45. * Don't allow profile load/replace/remove from profiles that don't
  46. * have CAP_MAC_ADMIN
  47. */
  48. if (!aa_may_manage_policy(op))
  49. return ERR_PTR(-EACCES);
  50. /* freed by caller to simple_write_to_buffer */
  51. data = kvmalloc(alloc_size);
  52. if (data == NULL)
  53. return ERR_PTR(-ENOMEM);
  54. if (copy_from_user(data, userbuf, copy_size)) {
  55. kvfree(data);
  56. return ERR_PTR(-EFAULT);
  57. }
  58. return data;
  59. }
  60. /* .load file hook fn to load policy */
  61. static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
  62. loff_t *pos)
  63. {
  64. char *data;
  65. ssize_t error;
  66. data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
  67. error = PTR_ERR(data);
  68. if (!IS_ERR(data)) {
  69. error = aa_replace_profiles(data, size, PROF_ADD);
  70. kvfree(data);
  71. }
  72. return error;
  73. }
  74. static const struct file_operations aa_fs_profile_load = {
  75. .write = profile_load
  76. };
  77. /* .replace file hook fn to load and/or replace policy */
  78. static ssize_t profile_replace(struct file *f, const char __user *buf,
  79. size_t size, loff_t *pos)
  80. {
  81. char *data;
  82. ssize_t error;
  83. data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
  84. error = PTR_ERR(data);
  85. if (!IS_ERR(data)) {
  86. error = aa_replace_profiles(data, size, PROF_REPLACE);
  87. kvfree(data);
  88. }
  89. return error;
  90. }
  91. static const struct file_operations aa_fs_profile_replace = {
  92. .write = profile_replace
  93. };
  94. /* .remove file hook fn to remove loaded policy */
  95. static ssize_t profile_remove(struct file *f, const char __user *buf,
  96. size_t size, loff_t *pos)
  97. {
  98. char *data;
  99. ssize_t error;
  100. /*
  101. * aa_remove_profile needs a null terminated string so 1 extra
  102. * byte is allocated and the copied data is null terminated.
  103. */
  104. data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
  105. error = PTR_ERR(data);
  106. if (!IS_ERR(data)) {
  107. data[size] = 0;
  108. error = aa_remove_profiles(data, size);
  109. kvfree(data);
  110. }
  111. return error;
  112. }
  113. static const struct file_operations aa_fs_profile_remove = {
  114. .write = profile_remove
  115. };
  116. /** Base file system setup **/
  117. static struct dentry *aa_fs_dentry __initdata;
  118. static void __init aafs_remove(const char *name)
  119. {
  120. struct dentry *dentry;
  121. dentry = lookup_one_len(name, aa_fs_dentry, strlen(name));
  122. if (!IS_ERR(dentry)) {
  123. securityfs_remove(dentry);
  124. dput(dentry);
  125. }
  126. }
  127. /**
  128. * aafs_create - create an entry in the apparmor filesystem
  129. * @name: name of the entry (NOT NULL)
  130. * @mask: file permission mask of the file
  131. * @fops: file operations for the file (NOT NULL)
  132. *
  133. * Used aafs_remove to remove entries created with this fn.
  134. */
  135. static int __init aafs_create(const char *name, int mask,
  136. const struct file_operations *fops)
  137. {
  138. struct dentry *dentry;
  139. dentry = securityfs_create_file(name, S_IFREG | mask, aa_fs_dentry,
  140. NULL, fops);
  141. return IS_ERR(dentry) ? PTR_ERR(dentry) : 0;
  142. }
  143. /**
  144. * aa_destroy_aafs - cleanup and free aafs
  145. *
  146. * releases dentries allocated by aa_create_aafs
  147. */
  148. void __init aa_destroy_aafs(void)
  149. {
  150. if (aa_fs_dentry) {
  151. aafs_remove(".remove");
  152. aafs_remove(".replace");
  153. aafs_remove(".load");
  154. securityfs_remove(aa_fs_dentry);
  155. aa_fs_dentry = NULL;
  156. }
  157. }
  158. /**
  159. * aa_create_aafs - create the apparmor security filesystem
  160. *
  161. * dentries created here are released by aa_destroy_aafs
  162. *
  163. * Returns: error on failure
  164. */
  165. int __init aa_create_aafs(void)
  166. {
  167. int error;
  168. if (!apparmor_initialized)
  169. return 0;
  170. if (aa_fs_dentry) {
  171. AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
  172. return -EEXIST;
  173. }
  174. aa_fs_dentry = securityfs_create_dir("apparmor", NULL);
  175. if (IS_ERR(aa_fs_dentry)) {
  176. error = PTR_ERR(aa_fs_dentry);
  177. aa_fs_dentry = NULL;
  178. goto error;
  179. }
  180. error = aafs_create(".load", 0640, &aa_fs_profile_load);
  181. if (error)
  182. goto error;
  183. error = aafs_create(".replace", 0640, &aa_fs_profile_replace);
  184. if (error)
  185. goto error;
  186. error = aafs_create(".remove", 0640, &aa_fs_profile_remove);
  187. if (error)
  188. goto error;
  189. /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
  190. /* Report that AppArmor fs is enabled */
  191. aa_info_message("AppArmor Filesystem Enabled");
  192. return 0;
  193. error:
  194. aa_destroy_aafs();
  195. AA_ERROR("Error creating AppArmor securityfs\n");
  196. return error;
  197. }
  198. fs_initcall(aa_create_aafs);