linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* sys_tee() bug: after tee() of partial page, both pipes can merge, clobbering each other's data
@ 2018-10-15  2:13 Jann Horn
  2018-10-15 14:21 ` Jann Horn
  0 siblings, 1 reply; 2+ messages in thread
From: Jann Horn @ 2018-10-15  2:13 UTC (permalink / raw)
  To: Al Viro, Miklos Szeredi, Jens Axboe, Jens Axboe
  Cc: kernel list, linux-fsdevel

Hi!

I noticed the following behavior; basically, after copying part of a
normal pipe buffer (anon_pipe_buf_ops) from pipe A to pipe B, both
pipe A and pipe B can merge new writes into the existing page,
clobbering each other's data:

============
$ cat tee_test.c
#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
#include <err.h>
#include <stdio.h>

int main(void) {
  int pipe_a[2];
  if (pipe(pipe_a)) err(1, "pipe");
  int pipe_b[2];
  if (pipe(pipe_b)) err(1, "pipe");
  if (write(pipe_a[1], "abcd", 4) != 4) err(1, "write");
  if (tee(pipe_a[0], pipe_b[1], 2, 0) != 2) err(1, "tee");
  if (write(pipe_b[1], "xx", 2) != 2) err(1, "write");

  char buf[5];
  if (read(pipe_a[0], buf, 4) != 4) err(1, "read");
  buf[4] = 0;
  printf("got back: '%s'\n", buf);
}
$ gcc -o tee_test tee_test.c
$ ./tee_test
got back: 'abxx'
$
============

splice_pipe_to_pipe() probably has the same problem?

I'm not sure what the cleanest way to fix this would be. Replace
anon_pipe_buf_ops with packet_pipe_buf_ops when copying a buffer? Or
add a new buffer flag for marking a buffer as mergeable, and get rid
of buf->ops->can_merge?

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-10-15 14:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-15  2:13 sys_tee() bug: after tee() of partial page, both pipes can merge, clobbering each other's data Jann Horn
2018-10-15 14:21 ` Jann Horn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).