提交 7715b521 编写于 作者: A Al Viro

O_TRUNC open shouldn't fail after file truncation

* take truncate logics into a helper (handle_truncate())
* rip it out of may_open()
* call it from the only caller of may_open() that might pass
O_TRUNC
* and do that after we'd finished with opening.
Signed-off-by: NAl Viro <viro@zeniv.linux.org.uk>
上级 85a17f55
...@@ -1444,43 +1444,39 @@ int may_open(struct path *path, int acc_mode, int flag) ...@@ -1444,43 +1444,39 @@ int may_open(struct path *path, int acc_mode, int flag)
if (error) if (error)
return error; return error;
error = ima_path_check(path, acc_mode ?
acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC) :
ACC_MODE(flag) & (MAY_READ | MAY_WRITE),
IMA_COUNT_UPDATE);
if (error)
return error;
/* /*
* An append-only file must be opened in append mode for writing. * An append-only file must be opened in append mode for writing.
*/ */
if (IS_APPEND(inode)) { if (IS_APPEND(inode)) {
error = -EPERM;
if ((flag & FMODE_WRITE) && !(flag & O_APPEND)) if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
goto err_out; return -EPERM;
if (flag & O_TRUNC) if (flag & O_TRUNC)
goto err_out; return -EPERM;
} }
/* O_NOATIME can only be set by the owner or superuser */ /* O_NOATIME can only be set by the owner or superuser */
if (flag & O_NOATIME) if (flag & O_NOATIME && !is_owner_or_cap(inode))
if (!is_owner_or_cap(inode)) { return -EPERM;
error = -EPERM;
goto err_out;
}
/* /*
* Ensure there are no outstanding leases on the file. * Ensure there are no outstanding leases on the file.
*/ */
error = break_lease(inode, flag); error = break_lease(inode, flag);
if (error) if (error)
goto err_out; return error;
if (flag & O_TRUNC) { return ima_path_check(path, acc_mode ?
error = get_write_access(inode); acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC) :
if (error) ACC_MODE(flag) & (MAY_READ | MAY_WRITE),
goto err_out; IMA_COUNT_UPDATE);
}
static int handle_truncate(struct path *path)
{
struct inode *inode = path->dentry->d_inode;
int error = get_write_access(inode);
if (error)
return error;
/* /*
* Refuse to truncate files with mandatory locks held on them. * Refuse to truncate files with mandatory locks held on them.
*/ */
...@@ -1489,24 +1485,11 @@ int may_open(struct path *path, int acc_mode, int flag) ...@@ -1489,24 +1485,11 @@ int may_open(struct path *path, int acc_mode, int flag)
error = security_path_truncate(path, 0, error = security_path_truncate(path, 0,
ATTR_MTIME|ATTR_CTIME|ATTR_OPEN); ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
if (!error) { if (!error) {
vfs_dq_init(inode); error = do_truncate(path->dentry, 0,
error = do_truncate(dentry, 0,
ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
NULL); NULL);
} }
put_write_access(inode); put_write_access(inode);
if (error)
goto err_out;
} else
if (flag & FMODE_WRITE)
vfs_dq_init(inode);
return 0;
err_out:
ima_counts_put(path, acc_mode ?
acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC) :
ACC_MODE(flag) & (MAY_READ | MAY_WRITE));
return error; return error;
} }
...@@ -1561,7 +1544,7 @@ static inline int open_to_namei_flags(int flag) ...@@ -1561,7 +1544,7 @@ static inline int open_to_namei_flags(int flag)
return flag; return flag;
} }
static int open_will_write_to_fs(int flag, struct inode *inode) static int open_will_truncate(int flag, struct inode *inode)
{ {
/* /*
* We'll never write to the fs underlying * We'll never write to the fs underlying
...@@ -1586,7 +1569,7 @@ struct file *do_filp_open(int dfd, const char *pathname, ...@@ -1586,7 +1569,7 @@ struct file *do_filp_open(int dfd, const char *pathname,
struct path path, save; struct path path, save;
struct dentry *dir; struct dentry *dir;
int count = 0; int count = 0;
int will_write; int will_truncate;
int flag = open_to_namei_flags(open_flag); int flag = open_to_namei_flags(open_flag);
/* /*
...@@ -1752,28 +1735,48 @@ struct file *do_filp_open(int dfd, const char *pathname, ...@@ -1752,28 +1735,48 @@ struct file *do_filp_open(int dfd, const char *pathname,
* be avoided. Taking this mnt write here * be avoided. Taking this mnt write here
* ensures that (2) can not occur. * ensures that (2) can not occur.
*/ */
will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode); will_truncate = open_will_truncate(flag, nd.path.dentry->d_inode);
if (will_write) { if (will_truncate) {
error = mnt_want_write(nd.path.mnt); error = mnt_want_write(nd.path.mnt);
if (error) if (error)
goto exit; goto exit;
} }
error = may_open(&nd.path, acc_mode, flag); error = may_open(&nd.path, acc_mode, flag);
if (error) { if (error) {
if (will_write) if (will_truncate)
mnt_drop_write(nd.path.mnt); mnt_drop_write(nd.path.mnt);
goto exit; goto exit;
} }
filp = nameidata_to_filp(&nd, open_flag); filp = nameidata_to_filp(&nd, open_flag);
if (IS_ERR(filp)) if (IS_ERR(filp)) {
ima_counts_put(&nd.path, ima_counts_put(&nd.path,
acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC)); acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC));
if (will_truncate)
mnt_drop_write(nd.path.mnt);
if (nd.root.mnt)
path_put(&nd.root);
return filp;
}
if (acc_mode & MAY_WRITE)
vfs_dq_init(nd.path.dentry->d_inode);
if (will_truncate) {
error = handle_truncate(&nd.path);
if (error) {
mnt_drop_write(nd.path.mnt);
fput(filp);
if (nd.root.mnt)
path_put(&nd.root);
return ERR_PTR(error);
}
}
/* /*
* It is now safe to drop the mnt write * It is now safe to drop the mnt write
* because the filp has had a write taken * because the filp has had a write taken
* on its behalf. * on its behalf.
*/ */
if (will_write) if (will_truncate)
mnt_drop_write(nd.path.mnt); mnt_drop_write(nd.path.mnt);
if (nd.root.mnt) if (nd.root.mnt)
path_put(&nd.root); path_put(&nd.root);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册