Skip to content

Commit 2030a42

Browse files
author
Al Viro
committed
[PATCH] sanitize anon_inode_getfd()
a) none of the callers even looks at inode or file returned by anon_inode_getfd() b) any caller that would try to look at those would be racy, since by the time it returns we might have raced with close() from another thread and that file would be pining for fjords. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 9f3acc3 commit 2030a42

7 files changed

Lines changed: 29 additions & 74 deletions

File tree

‎fs/anon_inodes.c‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ static struct dentry_operations anon_inodefs_dentry_operations = {
5757
* anonymous inode, and a dentry that describe the "class"
5858
* of the file
5959
*
60-
* @pfd: [out] pointer to the file descriptor
61-
* @dpinode: [out] pointer to the inode
62-
* @pfile: [out] pointer to the file struct
6360
* @name: [in] name of the "class" of the new file
6461
* @fops [in] file operations for the new file
6562
* @priv [in] private data for the new file (will be file's private_data)
@@ -68,10 +65,9 @@ static struct dentry_operations anon_inodefs_dentry_operations = {
6865
* that do not need to have a full-fledged inode in order to operate correctly.
6966
* All the files created with anon_inode_getfd() will share a single inode,
7067
* hence saving memory and avoiding code duplication for the file/inode/dentry
71-
* setup.
68+
* setup. Returns new descriptor or -error.
7269
*/
73-
int anon_inode_getfd(int *pfd, struct inode **pinode, struct file **pfile,
74-
const char *name, const struct file_operations *fops,
70+
int anon_inode_getfd(const char *name, const struct file_operations *fops,
7571
void *priv)
7672
{
7773
struct qstr this;
@@ -125,10 +121,7 @@ int anon_inode_getfd(int *pfd, struct inode **pinode, struct file **pfile,
125121

126122
fd_install(fd, file);
127123

128-
*pfd = fd;
129-
*pinode = anon_inode_inode;
130-
*pfile = file;
131-
return 0;
124+
return fd;
132125

133126
err_dput:
134127
dput(dentry);

‎fs/eventfd.c‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,8 @@ struct file *eventfd_fget(int fd)
200200

201201
asmlinkage long sys_eventfd(unsigned int count)
202202
{
203-
int error, fd;
203+
int fd;
204204
struct eventfd_ctx *ctx;
205-
struct file *file;
206-
struct inode *inode;
207205

208206
ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
209207
if (!ctx)
@@ -216,12 +214,9 @@ asmlinkage long sys_eventfd(unsigned int count)
216214
* When we call this, the initialization must be complete, since
217215
* anon_inode_getfd() will install the fd.
218216
*/
219-
error = anon_inode_getfd(&fd, &inode, &file, "[eventfd]",
220-
&eventfd_fops, ctx);
221-
if (!error)
222-
return fd;
223-
224-
kfree(ctx);
225-
return error;
217+
fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx);
218+
if (fd < 0)
219+
kfree(ctx);
220+
return fd;
226221
}
227222

‎fs/eventpoll.c‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,8 +1050,6 @@ asmlinkage long sys_epoll_create(int size)
10501050
{
10511051
int error, fd = -1;
10521052
struct eventpoll *ep;
1053-
struct inode *inode;
1054-
struct file *file;
10551053

10561054
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n",
10571055
current, size));
@@ -1061,29 +1059,24 @@ asmlinkage long sys_epoll_create(int size)
10611059
* structure ( "struct eventpoll" ).
10621060
*/
10631061
error = -EINVAL;
1064-
if (size <= 0 || (error = ep_alloc(&ep)) != 0)
1062+
if (size <= 0 || (error = ep_alloc(&ep)) < 0) {
1063+
fd = error;
10651064
goto error_return;
1065+
}
10661066

10671067
/*
10681068
* Creates all the items needed to setup an eventpoll file. That is,
1069-
* a file structure, and inode and a free file descriptor.
1069+
* a file structure and a free file descriptor.
10701070
*/
1071-
error = anon_inode_getfd(&fd, &inode, &file, "[eventpoll]",
1072-
&eventpoll_fops, ep);
1073-
if (error)
1074-
goto error_free;
1071+
fd = anon_inode_getfd("[eventpoll]", &eventpoll_fops, ep);
1072+
if (fd < 0)
1073+
ep_free(ep);
10751074

1075+
error_return:
10761076
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
10771077
current, size, fd));
10781078

10791079
return fd;
1080-
1081-
error_free:
1082-
ep_free(ep);
1083-
error_return:
1084-
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
1085-
current, size, error));
1086-
return error;
10871080
}
10881081

10891082
/*

‎fs/signalfd.c‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,8 @@ static const struct file_operations signalfd_fops = {
207207

208208
asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemask)
209209
{
210-
int error;
211210
sigset_t sigmask;
212211
struct signalfd_ctx *ctx;
213-
struct file *file;
214-
struct inode *inode;
215212

216213
if (sizemask != sizeof(sigset_t) ||
217214
copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
@@ -230,12 +227,11 @@ asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemas
230227
* When we call this, the initialization must be complete, since
231228
* anon_inode_getfd() will install the fd.
232229
*/
233-
error = anon_inode_getfd(&ufd, &inode, &file, "[signalfd]",
234-
&signalfd_fops, ctx);
235-
if (error)
236-
goto err_fdalloc;
230+
ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx);
231+
if (ufd < 0)
232+
kfree(ctx);
237233
} else {
238-
file = fget(ufd);
234+
struct file *file = fget(ufd);
239235
if (!file)
240236
return -EBADF;
241237
ctx = file->private_data;
@@ -252,9 +248,4 @@ asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemas
252248
}
253249

254250
return ufd;
255-
256-
err_fdalloc:
257-
kfree(ctx);
258-
return error;
259251
}
260-

‎fs/timerfd.c‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,8 @@ static struct file *timerfd_fget(int fd)
181181

182182
asmlinkage long sys_timerfd_create(int clockid, int flags)
183183
{
184-
int error, ufd;
184+
int ufd;
185185
struct timerfd_ctx *ctx;
186-
struct file *file;
187-
struct inode *inode;
188186

189187
if (flags)
190188
return -EINVAL;
@@ -200,12 +198,9 @@ asmlinkage long sys_timerfd_create(int clockid, int flags)
200198
ctx->clockid = clockid;
201199
hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
202200

203-
error = anon_inode_getfd(&ufd, &inode, &file, "[timerfd]",
204-
&timerfd_fops, ctx);
205-
if (error) {
201+
ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx);
202+
if (ufd < 0)
206203
kfree(ctx);
207-
return error;
208-
}
209204

210205
return ufd;
211206
}

‎include/linux/anon_inodes.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
#ifndef _LINUX_ANON_INODES_H
99
#define _LINUX_ANON_INODES_H
1010

11-
int anon_inode_getfd(int *pfd, struct inode **pinode, struct file **pfile,
12-
const char *name, const struct file_operations *fops,
11+
int anon_inode_getfd(const char *name, const struct file_operations *fops,
1312
void *priv);
1413

1514
#endif /* _LINUX_ANON_INODES_H */

‎virt/kvm/kvm_main.c‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -834,16 +834,9 @@ static const struct file_operations kvm_vcpu_fops = {
834834
*/
835835
static int create_vcpu_fd(struct kvm_vcpu *vcpu)
836836
{
837-
int fd, r;
838-
struct inode *inode;
839-
struct file *file;
840-
841-
r = anon_inode_getfd(&fd, &inode, &file,
842-
"kvm-vcpu", &kvm_vcpu_fops, vcpu);
843-
if (r) {
837+
int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu);
838+
if (fd < 0)
844839
kvm_put_kvm(vcpu->kvm);
845-
return r;
846-
}
847840
return fd;
848841
}
849842

@@ -1168,19 +1161,15 @@ static const struct file_operations kvm_vm_fops = {
11681161

11691162
static int kvm_dev_ioctl_create_vm(void)
11701163
{
1171-
int fd, r;
1172-
struct inode *inode;
1173-
struct file *file;
1164+
int fd;
11741165
struct kvm *kvm;
11751166

11761167
kvm = kvm_create_vm();
11771168
if (IS_ERR(kvm))
11781169
return PTR_ERR(kvm);
1179-
r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
1180-
if (r) {
1170+
fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm);
1171+
if (fd < 0)
11811172
kvm_put_kvm(kvm);
1182-
return r;
1183-
}
11841173

11851174
return fd;
11861175
}

0 commit comments

Comments
 (0)