Skip to content

Commit

Permalink
cifs: Check for existing directory when opening file with O_CREAT
Browse files Browse the repository at this point in the history
commit 8d9535b upstream.

When opening a file with O_CREAT flag, check to see if the file opened
is an existing directory.

This prevents the directory from being opened which subsequently causes
a crash when the close function for directories cifs_closedir() is called
which frees up the file->private_data memory while the file is still
listed on the open file list for the tcon.

Signed-off-by: Sachin Prabhu <sprabhu@redhat.com>
Signed-off-by: Steve French <smfrench@gmail.com>
Reported-by: Xiaoli Feng <xifeng@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
spuiuk authored and gregkh committed Aug 20, 2016
1 parent a636a9b commit 36e6321
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions fs/cifs/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid,
goto cifs_create_get_file_info;
}

if (S_ISDIR(newinode->i_mode)) {
CIFSSMBClose(xid, tcon, fid->netfid);
iput(newinode);
rc = -EISDIR;
goto out;
}

if (!S_ISREG(newinode->i_mode)) {
/*
* The server may allow us to open things like
Expand Down Expand Up @@ -415,17 +422,28 @@ cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid,
if (rc != 0) {
cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n",
rc);
if (server->ops->close)
server->ops->close(xid, tcon, fid);
goto out;
goto out_err;
}

if (S_ISDIR(newinode->i_mode)) {
rc = -EISDIR;
goto out_err;
}

d_drop(direntry);
d_add(direntry, newinode);

out:
kfree(buf);
kfree(full_path);
return rc;

out_err:
if (server->ops->close)
server->ops->close(xid, tcon, fid);
if (newinode)
iput(newinode);
goto out;
}

int
Expand Down

0 comments on commit 36e6321

Please sign in to comment.