From: Eric Biggers <ebiggers3@gmail.com>
To: linux-fsdevel@vger.kernel.org
Subject: Data in source pipe can be modified after sys_tee()?
Date: Mon, 20 Mar 2017 10:35:31 -0700 [thread overview]
Message-ID: <20170320173531.GB26906@gmail.com> (raw)
Hello,
I noticed a strange behavior involving the tee() system call. Here's a sample
program:
#define _GNU_SOURCE
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main()
{
int pipe_A[2];
int pipe_B[2];
char buffer[2] = { 0 };
pipe(pipe_A);
pipe(pipe_B);
write(pipe_A[1], "AA", 2);
tee(pipe_A[0], pipe_B[1], 1, 0);
write(pipe_B[1], "B", 1);
read(pipe_A[0], buffer, 2);
printf("%.*s\n", 2, buffer);
assert(!memcmp(buffer, "AA", 2));
return 0;
}
It creates two pipes A and B, then writes two bytes "AA" to pipe A, then tee()s
one byte from pipe A to pipe B, then writes one byte "B" to pipe B.
At that point, I expected pipe A to still contain the bytes "AA", because tee()
does not consume the source data, nor was any data written to pipe A; nor should
it be possible to modify data already in a pipe (unless it was put there by
splice() or vmsplice()).
However, the actual behavior is that pipe A contains the bytes "AB". This
happens because the second byte was overwritten by the write to pipe B, as the
data in both pipes are backed by the same page, and the pipe is using
anon_pipe_buf_ops which has .can_merge = 1, so the backing page can be written
to by pipe_write().
Is this a bug? It certainly seems like it; you should *not* be able to modify
data in a pipe, given only the read end.
- Eric
reply other threads:[~2017-03-20 17:35 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170320173531.GB26906@gmail.com \
--to=ebiggers3@gmail.com \
--cc=linux-fsdevel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.