Skip to content

Commit 9b91b6b

Browse files
author
Miklos Szeredi
committed
ovl: fix deadlock in splice write
There's possibility of an ABBA deadlock in case of a splice write to an overlayfs file and a concurrent splice write to a corresponding real file. The call chain for splice to an overlay file: -> do_splice [takes sb_writers on overlay file] -> do_splice_from -> iter_file_splice_write [takes pipe->mutex] -> vfs_iter_write ... -> ovl_write_iter [takes sb_writers on real file] And the call chain for splice to a real file: -> do_splice [takes sb_writers on real file] -> do_splice_from -> iter_file_splice_write [takes pipe->mutex] Syzbot successfully bisected this to commit 82a763e ("ovl: simplify file splice"). Fix by reverting the write part of the above commit and by adding missing bits from ovl_write_iter() into ovl_splice_write(). Fixes: 82a763e ("ovl: simplify file splice") Reported-and-tested-by: syzbot+579885d1a9a833336209@syzkaller.appspotmail.com Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
1 parent 9011c27 commit 9b91b6b

1 file changed

Lines changed: 46 additions & 1 deletion

File tree

‎fs/overlayfs/file.c‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,51 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
392392
return ret;
393393
}
394394

395+
/*
396+
* Calling iter_file_splice_write() directly from overlay's f_op may deadlock
397+
* due to lock order inversion between pipe->mutex in iter_file_splice_write()
398+
* and file_start_write(real.file) in ovl_write_iter().
399+
*
400+
* So do everything ovl_write_iter() does and call iter_file_splice_write() on
401+
* the real file.
402+
*/
403+
static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
404+
loff_t *ppos, size_t len, unsigned int flags)
405+
{
406+
struct fd real;
407+
const struct cred *old_cred;
408+
struct inode *inode = file_inode(out);
409+
struct inode *realinode = ovl_inode_real(inode);
410+
ssize_t ret;
411+
412+
inode_lock(inode);
413+
/* Update mode */
414+
ovl_copyattr(realinode, inode);
415+
ret = file_remove_privs(out);
416+
if (ret)
417+
goto out_unlock;
418+
419+
ret = ovl_real_fdget(out, &real);
420+
if (ret)
421+
goto out_unlock;
422+
423+
old_cred = ovl_override_creds(inode->i_sb);
424+
file_start_write(real.file);
425+
426+
ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
427+
428+
file_end_write(real.file);
429+
/* Update size */
430+
ovl_copyattr(realinode, inode);
431+
revert_creds(old_cred);
432+
fdput(real);
433+
434+
out_unlock:
435+
inode_unlock(inode);
436+
437+
return ret;
438+
}
439+
395440
static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
396441
{
397442
struct fd real;
@@ -603,7 +648,7 @@ const struct file_operations ovl_file_operations = {
603648
.fadvise = ovl_fadvise,
604649
.flush = ovl_flush,
605650
.splice_read = generic_file_splice_read,
606-
.splice_write = iter_file_splice_write,
651+
.splice_write = ovl_splice_write,
607652

608653
.copy_file_range = ovl_copy_file_range,
609654
.remap_file_range = ovl_remap_file_range,

0 commit comments

Comments
 (0)