inode.c 11 KB

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