Skip to content

Commit 939a942

Browse files
Amerigo WangJames Morris
authored andcommitted
vfs: allow file truncations when both suid and write permissions set
When suid is set and the non-owner user has write permission, any writing into this file should be allowed and suid should be removed after that. However, current kernel only allows writing without truncations, when we do truncations on that file, we get EPERM. This is a bug. Steps to reproduce this bug: % ls -l rootdir/file1 -rwsrwsrwx 1 root root 3 Jun 25 15:42 rootdir/file1 % echo h > rootdir/file1 zsh: operation not permitted: rootdir/file1 % ls -l rootdir/file1 -rwsrwsrwx 1 root root 3 Jun 25 15:42 rootdir/file1 % echo h >> rootdir/file1 % ls -l rootdir/file1 -rwxrwxrwx 1 root root 5 Jun 25 16:34 rootdir/file1 Signed-off-by: WANG Cong <[email protected]> Cc: Eric Sandeen <[email protected]> Acked-by: Eric Paris <[email protected]> Cc: Eugene Teo <[email protected]> Cc: Al Viro <[email protected]> Cc: OGAWA Hirofumi <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Stephen Smalley <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: James Morris <[email protected]>
1 parent bc6a600 commit 939a942

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

fs/open.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user
199199
int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
200200
struct file *filp)
201201
{
202-
int err;
202+
int ret;
203203
struct iattr newattrs;
204204

205205
/* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
@@ -214,12 +214,14 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
214214
}
215215

216216
/* Remove suid/sgid on truncate too */
217-
newattrs.ia_valid |= should_remove_suid(dentry);
217+
ret = should_remove_suid(dentry);
218+
if (ret)
219+
newattrs.ia_valid |= ret | ATTR_FORCE;
218220

219221
mutex_lock(&dentry->d_inode->i_mutex);
220-
err = notify_change(dentry, &newattrs);
222+
ret = notify_change(dentry, &newattrs);
221223
mutex_unlock(&dentry->d_inode->i_mutex);
222-
return err;
224+
return ret;
223225
}
224226

225227
static long do_sys_truncate(const char __user *pathname, loff_t length)

0 commit comments

Comments
 (0)