Skip to content

Commit bcce010

Browse files
Miklos Szeredigregkh
authored andcommitted
ovl: fix deadlock in splice write
commit 9b91b6b upstream. 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> Cc: Stan Hu <stanhu@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent dca4f9a commit bcce010

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
@@ -422,6 +422,51 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
422422
return ret;
423423
}
424424

425+
/*
426+
* Calling iter_file_splice_write() directly from overlay's f_op may deadlock
427+
* due to lock order inversion between pipe->mutex in iter_file_splice_write()
428+
* and file_start_write(real.file) in ovl_write_iter().
429+
*
430+
* So do everything ovl_write_iter() does and call iter_file_splice_write() on
431+
* the real file.
432+
*/
433+
static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
434+
loff_t *ppos, size_t len, unsigned int flags)
435+
{
436+
struct fd real;
437+
const struct cred *old_cred;
438+
struct inode *inode = file_inode(out);
439+
struct inode *realinode = ovl_inode_real(inode);
440+
ssize_t ret;
441+
442+
inode_lock(inode);
443+
/* Update mode */
444+
ovl_copyattr(realinode, inode);
445+
ret = file_remove_privs(out);
446+
if (ret)
447+
goto out_unlock;
448+
449+
ret = ovl_real_fdget(out, &real);
450+
if (ret)
451+
goto out_unlock;
452+
453+
old_cred = ovl_override_creds(inode->i_sb);
454+
file_start_write(real.file);
455+
456+
ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
457+
458+
file_end_write(real.file);
459+
/* Update size */
460+
ovl_copyattr(realinode, inode);
461+
revert_creds(old_cred);
462+
fdput(real);
463+
464+
out_unlock:
465+
inode_unlock(inode);
466+
467+
return ret;
468+
}
469+
425470
static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
426471
{
427472
struct fd real;
@@ -731,7 +776,7 @@ const struct file_operations ovl_file_operations = {
731776
.compat_ioctl = ovl_compat_ioctl,
732777
#endif
733778
.splice_read = generic_file_splice_read,
734-
.splice_write = iter_file_splice_write,
779+
.splice_write = ovl_splice_write,
735780

736781
.copy_file_range = ovl_copy_file_range,
737782
.remap_file_range = ovl_remap_file_range,

0 commit comments

Comments
 (0)