aboutsummaryrefslogtreecommitdiffstats
path: root/fs/debugfs/file.c
diff options
context:
space:
mode:
authorNicolai Stange <nicstange@gmail.com>2016-03-22 09:11:13 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2016-04-12 17:14:21 -0400
commit9fd4dcece43a53e5a9e65a973df5693702ee6401 (patch)
tree70fe5fc79019aec56775a74d71c26094e211abd5 /fs/debugfs/file.c
parent3a3a5fece6f28c14d3d05c74fb7696412e53a067 (diff)
debugfs: prevent access to possibly dead file_operations at file open
Nothing prevents a dentry found by path lookup before a return of __debugfs_remove() to actually get opened after that return. Now, after the return of __debugfs_remove(), there are no guarantees whatsoever regarding the memory the corresponding inode's file_operations object had been kept in. Since __debugfs_remove() is seldomly invoked, usually from module exit handlers only, the race is hard to trigger and the impact is very low. A discussion of the problem outlined above as well as a suggested solution can be found in the (sub-)thread rooted at http://lkml.kernel.org/g/20130401203445.GA20862@ZenIV.linux.org.uk ("Yet another pipe related oops.") Basically, Greg KH suggests to introduce an intermediate fops and Al Viro points out that a pointer to the original ones may be stored in ->d_fsdata. Follow this line of reasoning: - Add SRCU as a reverse dependency of DEBUG_FS. - Introduce a srcu_struct object for the debugfs subsystem. - In debugfs_create_file(), store a pointer to the original file_operations object in ->d_fsdata. - Make debugfs_remove() and debugfs_remove_recursive() wait for a SRCU grace period after the dentry has been delete()'d and before they return to their callers. - Introduce an intermediate file_operations object named "debugfs_open_proxy_file_operations". It's ->open() functions checks, under the protection of a SRCU read lock, whether the dentry is still alive, i.e. has not been d_delete()'d and if so, tries to acquire a reference on the owning module. On success, it sets the file object's ->f_op to the original file_operations and forwards the ongoing open() call to the original ->open(). - For clarity, rename the former debugfs_file_operations to debugfs_noop_file_operations -- they are in no way canonical. The choice of SRCU over "normal" RCU is justified by the fact, that the former may also be used to protect ->i_private data from going away during the execution of a file's readers and writers which may (and do) sleep. Finally, introduce the fs/debugfs/internal.h header containing some declarations internal to the debugfs implementation. Signed-off-by: Nicolai Stange <nicstange@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'fs/debugfs/file.c')
-rw-r--r--fs/debugfs/file.c91
1 files changed, 90 insertions, 1 deletions
diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c
index d2ba12e23ed9..736ab3c988f2 100644
--- a/fs/debugfs/file.c
+++ b/fs/debugfs/file.c
@@ -22,6 +22,9 @@
22#include <linux/slab.h> 22#include <linux/slab.h>
23#include <linux/atomic.h> 23#include <linux/atomic.h>
24#include <linux/device.h> 24#include <linux/device.h>
25#include <linux/srcu.h>
26
27#include "internal.h"
25 28
26static ssize_t default_read_file(struct file *file, char __user *buf, 29static ssize_t default_read_file(struct file *file, char __user *buf,
27 size_t count, loff_t *ppos) 30 size_t count, loff_t *ppos)
@@ -35,13 +38,99 @@ static ssize_t default_write_file(struct file *file, const char __user *buf,
35 return count; 38 return count;
36} 39}
37 40
38const struct file_operations debugfs_file_operations = { 41const struct file_operations debugfs_noop_file_operations = {
39 .read = default_read_file, 42 .read = default_read_file,
40 .write = default_write_file, 43 .write = default_write_file,
41 .open = simple_open, 44 .open = simple_open,
42 .llseek = noop_llseek, 45 .llseek = noop_llseek,
43}; 46};
44 47
48/**
49 * debugfs_use_file_start - mark the beginning of file data access
50 * @dentry: the dentry object whose data is being accessed.
51 * @srcu_idx: a pointer to some memory to store a SRCU index in.
52 *
53 * Up to a matching call to debugfs_use_file_finish(), any
54 * successive call into the file removing functions debugfs_remove()
55 * and debugfs_remove_recursive() will block. Since associated private
56 * file data may only get freed after a successful return of any of
57 * the removal functions, you may safely access it after a successful
58 * call to debugfs_use_file_start() without worrying about
59 * lifetime issues.
60 *
61 * If -%EIO is returned, the file has already been removed and thus,
62 * it is not safe to access any of its data. If, on the other hand,
63 * it is allowed to access the file data, zero is returned.
64 *
65 * Regardless of the return code, any call to
66 * debugfs_use_file_start() must be followed by a matching call
67 * to debugfs_use_file_finish().
68 */
69static int debugfs_use_file_start(const struct dentry *dentry, int *srcu_idx)
70 __acquires(&debugfs_srcu)
71{
72 *srcu_idx = srcu_read_lock(&debugfs_srcu);
73 barrier();
74 if (d_unlinked(dentry))
75 return -EIO;
76 return 0;
77}
78
79/**
80 * debugfs_use_file_finish - mark the end of file data access
81 * @srcu_idx: the SRCU index "created" by a former call to
82 * debugfs_use_file_start().
83 *
84 * Allow any ongoing concurrent call into debugfs_remove() or
85 * debugfs_remove_recursive() blocked by a former call to
86 * debugfs_use_file_start() to proceed and return to its caller.
87 */
88static void debugfs_use_file_finish(int srcu_idx) __releases(&debugfs_srcu)
89{
90 srcu_read_unlock(&debugfs_srcu, srcu_idx);
91}
92
93#define F_DENTRY(filp) ((filp)->f_path.dentry)
94
95#define REAL_FOPS_DEREF(dentry) \
96 ((const struct file_operations *)(dentry)->d_fsdata)
97
98static int open_proxy_open(struct inode *inode, struct file *filp)
99{
100 const struct dentry *dentry = F_DENTRY(filp);
101 const struct file_operations *real_fops = NULL;
102 int srcu_idx, r;
103
104 r = debugfs_use_file_start(dentry, &srcu_idx);
105 if (r) {
106 r = -ENOENT;
107 goto out;
108 }
109
110 real_fops = REAL_FOPS_DEREF(dentry);
111 real_fops = fops_get(real_fops);
112 if (!real_fops) {
113 /* Huh? Module did not clean up after itself at exit? */
114 WARN(1, "debugfs file owner did not clean up at exit: %pd",
115 dentry);
116 r = -ENXIO;
117 goto out;
118 }
119 replace_fops(filp, real_fops);
120
121 if (real_fops->open)
122 r = real_fops->open(inode, filp);
123
124out:
125 fops_put(real_fops);
126 debugfs_use_file_finish(srcu_idx);
127 return r;
128}
129
130const struct file_operations debugfs_open_proxy_file_operations = {
131 .open = open_proxy_open,
132};
133
45static struct dentry *debugfs_create_mode(const char *name, umode_t mode, 134static struct dentry *debugfs_create_mode(const char *name, umode_t mode,
46 struct dentry *parent, void *value, 135 struct dentry *parent, void *value,
47 const struct file_operations *fops, 136 const struct file_operations *fops,