Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 177407
b: refs/heads/master
c: 7715b52
h: refs/heads/master
i:
  177405: 88f7286
  177403: 60add8c
  177399: 9f3a5b3
  177391: 9502b05
  177375: 914754e
  177343: 5efb123
  177279: 00084cf
  177151: 4efd7d3
v: v3
  • Loading branch information
Al Viro committed Dec 16, 2009
1 parent 5c56d0c commit b509307
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 54 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: 85a17f552dfe77efb44b971615e4f221a5f28f37
refs/heads/master: 7715b521222b6ebb6e927fa261ed91ed687fe454
109 changes: 56 additions & 53 deletions trunk/fs/namei.c
Original file line number Diff line number Diff line change
Expand Up @@ -1444,69 +1444,52 @@ int may_open(struct path *path, int acc_mode, int flag)
if (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.
*/
if (IS_APPEND(inode)) {
error = -EPERM;
if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
goto err_out;
return -EPERM;
if (flag & O_TRUNC)
goto err_out;
return -EPERM;
}

/* O_NOATIME can only be set by the owner or superuser */
if (flag & O_NOATIME)
if (!is_owner_or_cap(inode)) {
error = -EPERM;
goto err_out;
}
if (flag & O_NOATIME && !is_owner_or_cap(inode))
return -EPERM;

/*
* Ensure there are no outstanding leases on the file.
*/
error = break_lease(inode, flag);
if (error)
goto err_out;

if (flag & O_TRUNC) {
error = get_write_access(inode);
if (error)
goto err_out;

/*
* Refuse to truncate files with mandatory locks held on them.
*/
error = locks_verify_locked(inode);
if (!error)
error = security_path_truncate(path, 0,
ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
if (!error) {
vfs_dq_init(inode);
return error;

error = do_truncate(dentry, 0,
ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
NULL);
}
put_write_access(inode);
if (error)
goto err_out;
} else
if (flag & FMODE_WRITE)
vfs_dq_init(inode);
return ima_path_check(path, acc_mode ?
acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC) :
ACC_MODE(flag) & (MAY_READ | MAY_WRITE),
IMA_COUNT_UPDATE);
}

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));
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.
*/
error = locks_verify_locked(inode);
if (!error)
error = security_path_truncate(path, 0,
ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
if (!error) {
error = do_truncate(path->dentry, 0,
ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
NULL);
}
put_write_access(inode);
return error;
}

Expand Down Expand Up @@ -1561,7 +1544,7 @@ static inline int open_to_namei_flags(int 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
Expand All @@ -1586,7 +1569,7 @@ struct file *do_filp_open(int dfd, const char *pathname,
struct path path, save;
struct dentry *dir;
int count = 0;
int will_write;
int will_truncate;
int flag = open_to_namei_flags(open_flag);

/*
Expand Down Expand Up @@ -1752,28 +1735,48 @@ struct file *do_filp_open(int dfd, const char *pathname,
* be avoided. Taking this mnt write here
* ensures that (2) can not occur.
*/
will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode);
if (will_write) {
will_truncate = open_will_truncate(flag, nd.path.dentry->d_inode);
if (will_truncate) {
error = mnt_want_write(nd.path.mnt);
if (error)
goto exit;
}
error = may_open(&nd.path, acc_mode, flag);
if (error) {
if (will_write)
if (will_truncate)
mnt_drop_write(nd.path.mnt);
goto exit;
}
filp = nameidata_to_filp(&nd, open_flag);
if (IS_ERR(filp))
if (IS_ERR(filp)) {
ima_counts_put(&nd.path,
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
* because the filp has had a write taken
* on its behalf.
*/
if (will_write)
if (will_truncate)
mnt_drop_write(nd.path.mnt);
if (nd.root.mnt)
path_put(&nd.root);
Expand Down

0 comments on commit b509307

Please sign in to comment.