Skip to content

Commit d3a3b0a

Browse files
Mathieu Desnoyersgregkh
authored andcommitted
debugfs: fix create mutex racy fops and private data
Setting fops and private data outside of the mutex at debugfs file creation introduces a race where the files can be opened with the wrong file operations and private data. It is easy to trigger with a process waiting on file creation notification. Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: stable <stable@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent 18ef545 commit d3a3b0a

1 file changed

Lines changed: 32 additions & 23 deletions

File tree

‎fs/debugfs/inode.c‎

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ static struct vfsmount *debugfs_mount;
3232
static int debugfs_mount_count;
3333
static bool debugfs_registered;
3434

35-
static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t dev)
35+
static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t dev,
36+
void *data, const struct file_operations *fops)
37+
3638
{
3739
struct inode *inode = new_inode(sb);
3840

@@ -44,14 +46,18 @@ static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t d
4446
init_special_inode(inode, mode, dev);
4547
break;
4648
case S_IFREG:
47-
inode->i_fop = &debugfs_file_operations;
49+
inode->i_fop = fops ? fops : &debugfs_file_operations;
50+
inode->i_private = data;
4851
break;
4952
case S_IFLNK:
5053
inode->i_op = &debugfs_link_operations;
54+
inode->i_fop = fops;
55+
inode->i_private = data;
5156
break;
5257
case S_IFDIR:
5358
inode->i_op = &simple_dir_inode_operations;
54-
inode->i_fop = &simple_dir_operations;
59+
inode->i_fop = fops ? fops : &simple_dir_operations;
60+
inode->i_private = data;
5561

5662
/* directory inodes start off with i_nlink == 2
5763
* (for "." entry) */
@@ -64,15 +70,16 @@ static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t d
6470

6571
/* SMP-safe */
6672
static int debugfs_mknod(struct inode *dir, struct dentry *dentry,
67-
int mode, dev_t dev)
73+
int mode, dev_t dev, void *data,
74+
const struct file_operations *fops)
6875
{
6976
struct inode *inode;
7077
int error = -EPERM;
7178

7279
if (dentry->d_inode)
7380
return -EEXIST;
7481

75-
inode = debugfs_get_inode(dir->i_sb, mode, dev);
82+
inode = debugfs_get_inode(dir->i_sb, mode, dev, data, fops);
7683
if (inode) {
7784
d_instantiate(dentry, inode);
7885
dget(dentry);
@@ -81,31 +88,34 @@ static int debugfs_mknod(struct inode *dir, struct dentry *dentry,
8188
return error;
8289
}
8390

84-
static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
91+
static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, int mode,
92+
void *data, const struct file_operations *fops)
8593
{
8694
int res;
8795

8896
mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
89-
res = debugfs_mknod(dir, dentry, mode, 0);
97+
res = debugfs_mknod(dir, dentry, mode, 0, data, fops);
9098
if (!res) {
9199
inc_nlink(dir);
92100
fsnotify_mkdir(dir, dentry);
93101
}
94102
return res;
95103
}
96104

97-
static int debugfs_link(struct inode *dir, struct dentry *dentry, int mode)
105+
static int debugfs_link(struct inode *dir, struct dentry *dentry, int mode,
106+
void *data, const struct file_operations *fops)
98107
{
99108
mode = (mode & S_IALLUGO) | S_IFLNK;
100-
return debugfs_mknod(dir, dentry, mode, 0);
109+
return debugfs_mknod(dir, dentry, mode, 0, data, fops);
101110
}
102111

103-
static int debugfs_create(struct inode *dir, struct dentry *dentry, int mode)
112+
static int debugfs_create(struct inode *dir, struct dentry *dentry, int mode,
113+
void *data, const struct file_operations *fops)
104114
{
105115
int res;
106116

107117
mode = (mode & S_IALLUGO) | S_IFREG;
108-
res = debugfs_mknod(dir, dentry, mode, 0);
118+
res = debugfs_mknod(dir, dentry, mode, 0, data, fops);
109119
if (!res)
110120
fsnotify_create(dir, dentry);
111121
return res;
@@ -139,7 +149,9 @@ static struct file_system_type debug_fs_type = {
139149

140150
static int debugfs_create_by_name(const char *name, mode_t mode,
141151
struct dentry *parent,
142-
struct dentry **dentry)
152+
struct dentry **dentry,
153+
void *data,
154+
const struct file_operations *fops)
143155
{
144156
int error = 0;
145157

@@ -164,13 +176,16 @@ static int debugfs_create_by_name(const char *name, mode_t mode,
164176
if (!IS_ERR(*dentry)) {
165177
switch (mode & S_IFMT) {
166178
case S_IFDIR:
167-
error = debugfs_mkdir(parent->d_inode, *dentry, mode);
179+
error = debugfs_mkdir(parent->d_inode, *dentry, mode,
180+
data, fops);
168181
break;
169182
case S_IFLNK:
170-
error = debugfs_link(parent->d_inode, *dentry, mode);
183+
error = debugfs_link(parent->d_inode, *dentry, mode,
184+
data, fops);
171185
break;
172186
default:
173-
error = debugfs_create(parent->d_inode, *dentry, mode);
187+
error = debugfs_create(parent->d_inode, *dentry, mode,
188+
data, fops);
174189
break;
175190
}
176191
dput(*dentry);
@@ -221,19 +236,13 @@ struct dentry *debugfs_create_file(const char *name, mode_t mode,
221236
if (error)
222237
goto exit;
223238

224-
error = debugfs_create_by_name(name, mode, parent, &dentry);
239+
error = debugfs_create_by_name(name, mode, parent, &dentry,
240+
data, fops);
225241
if (error) {
226242
dentry = NULL;
227243
simple_release_fs(&debugfs_mount, &debugfs_mount_count);
228244
goto exit;
229245
}
230-
231-
if (dentry->d_inode) {
232-
if (data)
233-
dentry->d_inode->i_private = data;
234-
if (fops)
235-
dentry->d_inode->i_fop = fops;
236-
}
237246
exit:
238247
return dentry;
239248
}

0 commit comments

Comments
 (0)