inode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * inode.c - securityfs
  3. *
  4. * Copyright (C) 2005 Greg Kroah-Hartman <gregkh@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. * Based on fs/debugfs/inode.c which had the following copyright notice:
  11. * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
  12. * Copyright (C) 2004 IBM Inc.
  13. */
  14. /* #define DEBUG */
  15. #include <linux/module.h>
  16. #include <linux/fs.h>
  17. #include <linux/mount.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/init.h>
  20. #include <linux/namei.h>
  21. #include <linux/security.h>
  22. #include <linux/lsm_hooks.h>
  23. #include <linux/magic.h>
  24. static struct vfsmount *mount;
  25. static int mount_count;
  26. static void securityfs_evict_inode(struct inode *inode)
  27. {
  28. truncate_inode_pages_final(&inode->i_data);
  29. clear_inode(inode);
  30. if (S_ISLNK(inode->i_mode))
  31. kfree(inode->i_link);
  32. }
  33. static const struct super_operations securityfs_super_operations = {
  34. .statfs = simple_statfs,
  35. .evict_inode = securityfs_evict_inode,
  36. };
  37. static int fill_super(struct super_block *sb, void *data, int silent)
  38. {
  39. static const struct tree_descr files[] = {{""}};
  40. int error;
  41. error = simple_fill_super(sb, SECURITYFS_MAGIC, files);
  42. if (error)
  43. return error;
  44. sb->s_op = &securityfs_super_operations;
  45. return 0;
  46. }
  47. static struct dentry *get_sb(struct file_system_type *fs_type,
  48. int flags, const char *dev_name,
  49. void *data)
  50. {
  51. return mount_single(fs_type, flags, data, fill_super);
  52. }
  53. static struct file_system_type fs_type = {
  54. .owner = THIS_MODULE,
  55. .name = "securityfs",
  56. .mount = get_sb,
  57. .kill_sb = kill_litter_super,
  58. };
  59. /**
  60. * securityfs_create_dentry - create a dentry in the securityfs filesystem
  61. *
  62. * @name: a pointer to a string containing the name of the file to create.
  63. * @mode: the permission that the file should have
  64. * @parent: a pointer to the parent dentry for this file. This should be a
  65. * directory dentry if set. If this parameter is %NULL, then the
  66. * file will be created in the root of the securityfs filesystem.
  67. * @data: a pointer to something that the caller will want to get to later
  68. * on. The inode.i_private pointer will point to this value on
  69. * the open() call.
  70. * @fops: a pointer to a struct file_operations that should be used for
  71. * this file.
  72. * @iops: a point to a struct of inode_operations that should be used for
  73. * this file/dir
  74. *
  75. * This is the basic "create a file/dir/symlink" function for
  76. * securityfs. It allows for a wide range of flexibility in creating
  77. * a file, or a directory (if you want to create a directory, the
  78. * securityfs_create_dir() function is recommended to be used
  79. * instead).
  80. *
  81. * This function returns a pointer to a dentry if it succeeds. This
  82. * pointer must be passed to the securityfs_remove() function when the
  83. * file is to be removed (no automatic cleanup happens if your module
  84. * is unloaded, you are responsible here). If an error occurs, the
  85. * function will return the error value (via ERR_PTR).
  86. *
  87. * If securityfs is not enabled in the kernel, the value %-ENODEV is
  88. * returned.
  89. */
  90. static struct dentry *securityfs_create_dentry(const char *name, umode_t mode,
  91. struct dentry *parent, void *data,
  92. const struct file_operations *fops,
  93. const struct inode_operations *iops)
  94. {
  95. struct dentry *dentry;
  96. struct inode *dir, *inode;
  97. int error;
  98. if (!(mode & S_IFMT))
  99. mode = (mode & S_IALLUGO) | S_IFREG;
  100. pr_debug("securityfs: creating file '%s'\n",name);
  101. error = simple_pin_fs(&fs_type, &mount, &mount_count);
  102. if (error)
  103. return ERR_PTR(error);
  104. if (!parent)
  105. parent = mount->mnt_root;
  106. dir = d_inode(parent);
  107. inode_lock(dir);
  108. dentry = lookup_one_len(name, parent, strlen(name));
  109. if (IS_ERR(dentry))
  110. goto out;
  111. if (d_really_is_positive(dentry)) {
  112. error = -EEXIST;
  113. goto out1;
  114. }
  115. inode = new_inode(dir->i_sb);
  116. if (!inode) {
  117. error = -ENOMEM;
  118. goto out1;
  119. }
  120. inode->i_ino = get_next_ino();
  121. inode->i_mode = mode;
  122. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  123. inode->i_private = data;
  124. if (S_ISDIR(mode)) {
  125. inode->i_op = &simple_dir_inode_operations;
  126. inode->i_fop = &simple_dir_operations;
  127. inc_nlink(inode);
  128. inc_nlink(dir);
  129. } else if (S_ISLNK(mode)) {
  130. inode->i_op = iops ? iops : &simple_symlink_inode_operations;
  131. inode->i_link = data;
  132. } else {
  133. inode->i_fop = fops;
  134. }
  135. d_instantiate(dentry, inode);
  136. dget(dentry);
  137. inode_unlock(dir);
  138. return dentry;
  139. out1:
  140. dput(dentry);
  141. dentry = ERR_PTR(error);
  142. out:
  143. inode_unlock(dir);
  144. simple_release_fs(&mount, &mount_count);
  145. return dentry;
  146. }
  147. /**
  148. * securityfs_create_file - create a file in the securityfs filesystem
  149. *
  150. * @name: a pointer to a string containing the name of the file to create.
  151. * @mode: the permission that the file should have
  152. * @parent: a pointer to the parent dentry for this file. This should be a
  153. * directory dentry if set. If this parameter is %NULL, then the
  154. * file will be created in the root of the securityfs filesystem.
  155. * @data: a pointer to something that the caller will want to get to later
  156. * on. The inode.i_private pointer will point to this value on
  157. * the open() call.
  158. * @fops: a pointer to a struct file_operations that should be used for
  159. * this file.
  160. *
  161. * This function creates a file in securityfs with the given @name.
  162. *
  163. * This function returns a pointer to a dentry if it succeeds. This
  164. * pointer must be passed to the securityfs_remove() function when the file is
  165. * to be removed (no automatic cleanup happens if your module is unloaded,
  166. * you are responsible here). If an error occurs, the function will return
  167. * the error value (via ERR_PTR).
  168. *
  169. * If securityfs is not enabled in the kernel, the value %-ENODEV is
  170. * returned.
  171. */
  172. struct dentry *securityfs_create_file(const char *name, umode_t mode,
  173. struct dentry *parent, void *data,
  174. const struct file_operations *fops)
  175. {
  176. return securityfs_create_dentry(name, mode, parent, data, fops, NULL);
  177. }
  178. EXPORT_SYMBOL_GPL(securityfs_create_file);
  179. /**
  180. * securityfs_create_dir - create a directory in the securityfs filesystem
  181. *
  182. * @name: a pointer to a string containing the name of the directory to
  183. * create.
  184. * @parent: a pointer to the parent dentry for this file. This should be a
  185. * directory dentry if set. If this parameter is %NULL, then the
  186. * directory will be created in the root of the securityfs filesystem.
  187. *
  188. * This function creates a directory in securityfs with the given @name.
  189. *
  190. * This function returns a pointer to a dentry if it succeeds. This
  191. * pointer must be passed to the securityfs_remove() function when the file is
  192. * to be removed (no automatic cleanup happens if your module is unloaded,
  193. * you are responsible here). If an error occurs, the function will return
  194. * the error value (via ERR_PTR).
  195. *
  196. * If securityfs is not enabled in the kernel, the value %-ENODEV is
  197. * returned.
  198. */
  199. struct dentry *securityfs_create_dir(const char *name, struct dentry *parent)
  200. {
  201. return securityfs_create_file(name, S_IFDIR | 0755, parent, NULL, NULL);
  202. }
  203. EXPORT_SYMBOL_GPL(securityfs_create_dir);
  204. /**
  205. * securityfs_create_symlink - create a symlink in the securityfs filesystem
  206. *
  207. * @name: a pointer to a string containing the name of the symlink to
  208. * create.
  209. * @parent: a pointer to the parent dentry for the symlink. This should be a
  210. * directory dentry if set. If this parameter is %NULL, then the
  211. * directory will be created in the root of the securityfs filesystem.
  212. * @target: a pointer to a string containing the name of the symlink's target.
  213. * If this parameter is %NULL, then the @iops parameter needs to be
  214. * setup to handle .readlink and .get_link inode_operations.
  215. * @iops: a pointer to the struct inode_operations to use for the symlink. If
  216. * this parameter is %NULL, then the default simple_symlink_inode
  217. * operations will be used.
  218. *
  219. * This function creates a symlink in securityfs with the given @name.
  220. *
  221. * This function returns a pointer to a dentry if it succeeds. This
  222. * pointer must be passed to the securityfs_remove() function when the file is
  223. * to be removed (no automatic cleanup happens if your module is unloaded,
  224. * you are responsible here). If an error occurs, the function will return
  225. * the error value (via ERR_PTR).
  226. *
  227. * If securityfs is not enabled in the kernel, the value %-ENODEV is
  228. * returned.
  229. */
  230. struct dentry *securityfs_create_symlink(const char *name,
  231. struct dentry *parent,
  232. const char *target,
  233. const struct inode_operations *iops)
  234. {
  235. struct dentry *dent;
  236. char *link = NULL;
  237. if (target) {
  238. link = kstrdup(target, GFP_KERNEL);
  239. if (!link)
  240. return ERR_PTR(-ENOMEM);
  241. }
  242. dent = securityfs_create_dentry(name, S_IFLNK | 0444, parent,
  243. link, NULL, iops);
  244. if (IS_ERR(dent))
  245. kfree(link);
  246. return dent;
  247. }
  248. EXPORT_SYMBOL_GPL(securityfs_create_symlink);
  249. /**
  250. * securityfs_remove - removes a file or directory from the securityfs filesystem
  251. *
  252. * @dentry: a pointer to a the dentry of the file or directory to be removed.
  253. *
  254. * This function removes a file or directory in securityfs that was previously
  255. * created with a call to another securityfs function (like
  256. * securityfs_create_file() or variants thereof.)
  257. *
  258. * This function is required to be called in order for the file to be
  259. * removed. No automatic cleanup of files will happen when a module is
  260. * removed; you are responsible here.
  261. */
  262. void securityfs_remove(struct dentry *dentry)
  263. {
  264. struct inode *dir;
  265. if (!dentry || IS_ERR(dentry))
  266. return;
  267. dir = d_inode(dentry->d_parent);
  268. inode_lock(dir);
  269. if (simple_positive(dentry)) {
  270. if (d_is_dir(dentry))
  271. simple_rmdir(dir, dentry);
  272. else
  273. simple_unlink(dir, dentry);
  274. dput(dentry);
  275. }
  276. inode_unlock(dir);
  277. simple_release_fs(&mount, &mount_count);
  278. }
  279. EXPORT_SYMBOL_GPL(securityfs_remove);
  280. #ifdef CONFIG_SECURITY
  281. static struct dentry *lsm_dentry;
  282. static ssize_t lsm_read(struct file *filp, char __user *buf, size_t count,
  283. loff_t *ppos)
  284. {
  285. return simple_read_from_buffer(buf, count, ppos, lsm_names,
  286. strlen(lsm_names));
  287. }
  288. static const struct file_operations lsm_ops = {
  289. .read = lsm_read,
  290. .llseek = generic_file_llseek,
  291. };
  292. #endif
  293. static int __init securityfs_init(void)
  294. {
  295. int retval;
  296. retval = sysfs_create_mount_point(kernel_kobj, "security");
  297. if (retval)
  298. return retval;
  299. retval = register_filesystem(&fs_type);
  300. if (retval) {
  301. sysfs_remove_mount_point(kernel_kobj, "security");
  302. return retval;
  303. }
  304. #ifdef CONFIG_SECURITY
  305. lsm_dentry = securityfs_create_file("lsm", 0444, NULL, NULL,
  306. &lsm_ops);
  307. #endif
  308. return 0;
  309. }
  310. core_initcall(securityfs_init);
  311. MODULE_LICENSE("GPL");