inode.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/magic.h>
  23. static struct vfsmount *mount;
  24. static int mount_count;
  25. static inline int positive(struct dentry *dentry)
  26. {
  27. return dentry->d_inode && !d_unhashed(dentry);
  28. }
  29. static int fill_super(struct super_block *sb, void *data, int silent)
  30. {
  31. static struct tree_descr files[] = {{""}};
  32. return simple_fill_super(sb, SECURITYFS_MAGIC, files);
  33. }
  34. static struct dentry *get_sb(struct file_system_type *fs_type,
  35. int flags, const char *dev_name,
  36. void *data)
  37. {
  38. return mount_single(fs_type, flags, data, fill_super);
  39. }
  40. static struct file_system_type fs_type = {
  41. .owner = THIS_MODULE,
  42. .name = "securityfs",
  43. .mount = get_sb,
  44. .kill_sb = kill_litter_super,
  45. };
  46. /**
  47. * securityfs_create_file - create a file in the securityfs filesystem
  48. *
  49. * @name: a pointer to a string containing the name of the file to create.
  50. * @mode: the permission that the file should have
  51. * @parent: a pointer to the parent dentry for this file. This should be a
  52. * directory dentry if set. If this parameter is %NULL, then the
  53. * file will be created in the root of the securityfs filesystem.
  54. * @data: a pointer to something that the caller will want to get to later
  55. * on. The inode.i_private pointer will point to this value on
  56. * the open() call.
  57. * @fops: a pointer to a struct file_operations that should be used for
  58. * this file.
  59. *
  60. * This is the basic "create a file" function for securityfs. It allows for a
  61. * wide range of flexibility in creating a file, or a directory (if you
  62. * want to create a directory, the securityfs_create_dir() function is
  63. * recommended to be used instead).
  64. *
  65. * This function returns a pointer to a dentry if it succeeds. This
  66. * pointer must be passed to the securityfs_remove() function when the file is
  67. * to be removed (no automatic cleanup happens if your module is unloaded,
  68. * you are responsible here). If an error occurs, the function will return
  69. * the erorr value (via ERR_PTR).
  70. *
  71. * If securityfs is not enabled in the kernel, the value %-ENODEV is
  72. * returned.
  73. */
  74. struct dentry *securityfs_create_file(const char *name, umode_t mode,
  75. struct dentry *parent, void *data,
  76. const struct file_operations *fops)
  77. {
  78. struct dentry *dentry;
  79. int is_dir = S_ISDIR(mode);
  80. struct inode *dir, *inode;
  81. int error;
  82. if (!is_dir) {
  83. BUG_ON(!fops);
  84. mode = (mode & S_IALLUGO) | S_IFREG;
  85. }
  86. pr_debug("securityfs: creating file '%s'\n",name);
  87. error = simple_pin_fs(&fs_type, &mount, &mount_count);
  88. if (error)
  89. return ERR_PTR(error);
  90. if (!parent)
  91. parent = mount->mnt_root;
  92. dir = parent->d_inode;
  93. mutex_lock(&dir->i_mutex);
  94. dentry = lookup_one_len2(name, mount, parent, strlen(name));
  95. if (IS_ERR(dentry))
  96. goto out;
  97. if (dentry->d_inode) {
  98. error = -EEXIST;
  99. goto out1;
  100. }
  101. inode = new_inode(dir->i_sb);
  102. if (!inode) {
  103. error = -ENOMEM;
  104. goto out1;
  105. }
  106. inode->i_ino = get_next_ino();
  107. inode->i_mode = mode;
  108. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  109. inode->i_private = data;
  110. if (is_dir) {
  111. inode->i_op = &simple_dir_inode_operations;
  112. inode->i_fop = &simple_dir_operations;
  113. inc_nlink(inode);
  114. inc_nlink(dir);
  115. } else {
  116. inode->i_fop = fops;
  117. }
  118. d_instantiate(dentry, inode);
  119. dget(dentry);
  120. mutex_unlock(&dir->i_mutex);
  121. return dentry;
  122. out1:
  123. dput(dentry);
  124. dentry = ERR_PTR(error);
  125. out:
  126. mutex_unlock(&dir->i_mutex);
  127. simple_release_fs(&mount, &mount_count);
  128. return dentry;
  129. }
  130. EXPORT_SYMBOL_GPL(securityfs_create_file);
  131. /**
  132. * securityfs_create_dir - create a directory in the securityfs filesystem
  133. *
  134. * @name: a pointer to a string containing the name of the directory to
  135. * create.
  136. * @parent: a pointer to the parent dentry for this file. This should be a
  137. * directory dentry if set. If this parameter is %NULL, then the
  138. * directory will be created in the root of the securityfs filesystem.
  139. *
  140. * This function creates a directory in securityfs with the given @name.
  141. *
  142. * This function returns a pointer to a dentry if it succeeds. This
  143. * pointer must be passed to the securityfs_remove() function when the file is
  144. * to be removed (no automatic cleanup happens if your module is unloaded,
  145. * you are responsible here). If an error occurs, %NULL will be returned.
  146. *
  147. * If securityfs is not enabled in the kernel, the value %-ENODEV is
  148. * returned. It is not wise to check for this value, but rather, check for
  149. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  150. * code.
  151. */
  152. struct dentry *securityfs_create_dir(const char *name, struct dentry *parent)
  153. {
  154. return securityfs_create_file(name,
  155. S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  156. parent, NULL, NULL);
  157. }
  158. EXPORT_SYMBOL_GPL(securityfs_create_dir);
  159. /**
  160. * securityfs_remove - removes a file or directory from the securityfs filesystem
  161. *
  162. * @dentry: a pointer to a the dentry of the file or directory to be removed.
  163. *
  164. * This function removes a file or directory in securityfs that was previously
  165. * created with a call to another securityfs function (like
  166. * securityfs_create_file() or variants thereof.)
  167. *
  168. * This function is required to be called in order for the file to be
  169. * removed. No automatic cleanup of files will happen when a module is
  170. * removed; you are responsible here.
  171. */
  172. void securityfs_remove(struct dentry *dentry)
  173. {
  174. struct dentry *parent;
  175. if (!dentry || IS_ERR(dentry))
  176. return;
  177. parent = dentry->d_parent;
  178. if (!parent || !parent->d_inode)
  179. return;
  180. mutex_lock(&parent->d_inode->i_mutex);
  181. if (positive(dentry)) {
  182. if (dentry->d_inode) {
  183. if (S_ISDIR(dentry->d_inode->i_mode))
  184. simple_rmdir(parent->d_inode, dentry);
  185. else
  186. simple_unlink(parent->d_inode, dentry);
  187. dput(dentry);
  188. }
  189. }
  190. mutex_unlock(&parent->d_inode->i_mutex);
  191. simple_release_fs(&mount, &mount_count);
  192. }
  193. EXPORT_SYMBOL_GPL(securityfs_remove);
  194. static struct kobject *security_kobj;
  195. static int __init securityfs_init(void)
  196. {
  197. int retval;
  198. security_kobj = kobject_create_and_add("security", kernel_kobj);
  199. if (!security_kobj)
  200. return -EINVAL;
  201. retval = register_filesystem(&fs_type);
  202. if (retval)
  203. kobject_put(security_kobj);
  204. return retval;
  205. }
  206. core_initcall(securityfs_init);
  207. MODULE_LICENSE("GPL");