linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Weird writev() behaviour on EFAULT - also successfully modifying the file
@ 2016-08-11 18:46 Tuomas Tynkkynen
  2016-08-11 22:10 ` Al Viro
  0 siblings, 1 reply; 6+ messages in thread
From: Tuomas Tynkkynen @ 2016-08-11 18:46 UTC (permalink / raw)
  To: linux-fsdevel

Greetings,

I've noticed a corner case with writev() both modifying the file and
returning -EFAULT at the same time. This happens on filesystems using
generic_perform_write() (i.e. ext4, vfat) on 4.6.3 kernel and below,
down to 3.16. Here's the reproducer:

// 8<---- cut here ------------------------ >8
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/uio.h>

int main(int argc, char** argv) {
    int fd;
    ssize_t ret;
    struct iovec iov[] = {
        { .iov_base = NULL, .iov_len = 0, },
        { .iov_base = NULL, .iov_len = 4096, },
    };

    system("dd if=/dev/zero bs=8k count=1 | tr '\\0' 'A' > foo");

    fd = open("foo", O_RDWR);
    if (fd < 0) {
        perror("open()");
        return 1;
    }

    ret = writev(fd, iov, 2);
    if (ret < 0) {
        perror("writev()");
        return 1;
    }

    return 0;
}
// 8<---- cut here ------------------------ >8

Running that prints "writev(): Bad address" but also some NUL bytes have
appeared at the beginning file, in addition to the 'A's by the dd.

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001000  41 41 41 41 41 41 41 41  41 41 41 41 41 41 41 41  |AAAAAAAAAAAAAAAA|
*
00002000

Is that intented behaviour?

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

end of thread, other threads:[~2016-08-11 23:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-11 18:46 Weird writev() behaviour on EFAULT - also successfully modifying the file Tuomas Tynkkynen
2016-08-11 22:10 ` Al Viro
2016-08-11 22:39   ` Linus Torvalds
2016-08-11 23:03     ` Al Viro
2016-08-11 23:10       ` Linus Torvalds
2016-08-11 23:13       ` Linus Torvalds

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).