1 From a0ce2f0aa6ad97c3d4927bf2ca54bcebdf062d55 Mon Sep 17 00:00:00 2001
2 From: Jann Horn <jannh@google.com>
3 Date: Wed, 23 Jan 2019 15:19:17 +0100
4 Subject: splice: don't merge into linked buffers
6 From: Jann Horn <jannh@google.com>
8 commit a0ce2f0aa6ad97c3d4927bf2ca54bcebdf062d55 upstream.
10 Before this patch, it was possible for two pipes to affect each other after
11 data had been transferred between them with tee():
18 if (pipe(pipe_a)) err(1, "pipe");
20 if (pipe(pipe_b)) err(1, "pipe");
21 if (write(pipe_a[1], "abcd", 4) != 4) err(1, "write");
22 if (tee(pipe_a[0], pipe_b[1], 2, 0) != 2) err(1, "tee");
23 if (write(pipe_b[1], "xx", 2) != 2) err(1, "write");
26 if (read(pipe_a[0], buf, 4) != 4) err(1, "read");
28 printf("got back: '%s'\n", buf);
30 $ gcc -o tee_test tee_test.c
36 As suggested by Al Viro, fix it by creating a separate type for
37 non-mergeable pipe buffers, then changing the types of buffers in
38 splice_pipe_to_pipe() and link_pipe().
40 Cc: <stable@vger.kernel.org>
41 Fixes: 7c77f0b3f920 ("splice: implement pipe to pipe splicing")
42 Fixes: 70524490ee2e ("[PATCH] splice: add support for sys_tee()")
43 Suggested-by: Al Viro <viro@zeniv.linux.org.uk>
44 Signed-off-by: Jann Horn <jannh@google.com>
45 Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
46 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
49 fs/pipe.c | 14 ++++++++++++++
51 include/linux/pipe_fs_i.h | 1 +
52 3 files changed, 19 insertions(+)
56 @@ -239,6 +239,14 @@ static const struct pipe_buf_operations
57 .get = generic_pipe_buf_get,
60 +static const struct pipe_buf_operations anon_pipe_buf_nomerge_ops = {
62 + .confirm = generic_pipe_buf_confirm,
63 + .release = anon_pipe_buf_release,
64 + .steal = anon_pipe_buf_steal,
65 + .get = generic_pipe_buf_get,
68 static const struct pipe_buf_operations packet_pipe_buf_ops = {
70 .confirm = generic_pipe_buf_confirm,
71 @@ -247,6 +255,12 @@ static const struct pipe_buf_operations
72 .get = generic_pipe_buf_get,
75 +void pipe_buf_mark_unmergeable(struct pipe_buffer *buf)
77 + if (buf->ops == &anon_pipe_buf_ops)
78 + buf->ops = &anon_pipe_buf_nomerge_ops;
82 pipe_read(struct kiocb *iocb, struct iov_iter *to)
86 @@ -1580,6 +1580,8 @@ retry:
88 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
90 + pipe_buf_mark_unmergeable(obuf);
94 ibuf->offset += obuf->len;
95 @@ -1654,6 +1656,8 @@ static int link_pipe(struct pipe_inode_i
97 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
99 + pipe_buf_mark_unmergeable(obuf);
104 --- a/include/linux/pipe_fs_i.h
105 +++ b/include/linux/pipe_fs_i.h
106 @@ -183,6 +183,7 @@ void generic_pipe_buf_get(struct pipe_in
107 int generic_pipe_buf_confirm(struct pipe_inode_info *, struct pipe_buffer *);
108 int generic_pipe_buf_steal(struct pipe_inode_info *, struct pipe_buffer *);
109 void generic_pipe_buf_release(struct pipe_inode_info *, struct pipe_buffer *);
110 +void pipe_buf_mark_unmergeable(struct pipe_buffer *buf);
112 extern const struct pipe_buf_operations nosteal_pipe_buf_ops;