Hi,

just stumbled over this, quick question:

I recently played with UFFD_WP and notices that write protection is
only effective on pages/ranges that have already pages populated (IOW:
!pte_none() in the kernel).

In case memory was never populated (or was discarded using e.g.,
madvice(DONTNEED)), write-protection will be skipped silently and you
won't get WP events for applicable pages.

So if someone writes to a yet unpoupulated page ("zero"), you won't
get WP events.

I can spot that you do a single uffd_change_protection() on the whole
RAMBlock.

How are you handling that scenario, or why don't you have to handle
that scenario?

Hi David,

I really wonder if such a problem exists.. If we are talking about a

I immediately ran into this issue with my simplest test cases. :)

write to an unpopulated page, we should get first page fault on
non-present page and populate it with protection bits from respective vma.
For UFFD_WP vma's  page will be populated non-writable. So we'll get
another page fault on present but read-only page and go to handle_userfault.

See the attached test program. Triggers for me on 5.11.0-rc6+ and 5.9.13-200.fc33

gcc -lpthread uffdio_wp.c -o uffdio_wp
./uffdio_wp
WP did not fire

Uncomment the placement of the zeropage just before registering to make the WP actually trigger. If there is no PTE, there is nothing to protect.


And it makes sense: How should the fault handler know which ranges you wp-ed, if there is no place to store that information (the PTEs!). The VMA cannot tell that story, it only knows that someone registered UFFD_WP to selectively wp some parts.

You might have to register also for MISSING faults and place zero pages.

--
Thanks,

David / dhildenb
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <poll.h>
#include <linux/userfaultfd.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>

static int page_size;
volatile bool wp_fired;

static void *fault_handler_thread(void *arg)
{
    const long uffd = (long) arg;
    struct pollfd pollfd = {
        .fd = uffd,
        .events = POLLIN,
    };
    int ret;

    while (true) {
        struct uffdio_writeprotect wp;
        struct uffd_msg msg;
        ssize_t nread;

        if (poll(&pollfd, 1, -1) == -1) {
            fprintf(stderr, "POLL failed: %s\n", strerror(errno));
            exit(-1);
        }
        if (read(uffd, &msg, sizeof(msg)) != sizeof(msg)) {
            fprintf(stderr, "READ failed\n");
            exit(-1);
        }
        if (msg.event == UFFD_EVENT_REMOVE) {
            continue;
        }
        if (msg.event != UFFD_EVENT_PAGEFAULT) {
            fprintf(stderr, "Not UFFD_EVENT_PAGEFAULT\n");
            exit(-1);
        }

        wp_fired = true;

        /* Simply unprotect and wake */
        wp.range.start = msg.arg.pagefault.address;
        wp.range.len = page_size;
        wp.mode = 0;
        if (ioctl(uffd, UFFDIO_WRITEPROTECT, &wp) == -1) {
            fprintf(stderr, "UFFDIO_WRITEPROTECT failed: %s\n", strerror(errno));
            exit(-1);
        }
    }
}

int main(void)
{
    struct uffdio_writeprotect wp;
    struct uffdio_zeropage zeropage;
    struct uffdio_register reg;
    struct uffdio_api api = {
        .api = UFFD_API,
        .features = UFFD_FEATURE_EVENT_REMOVE,
    };
    pthread_t fault, discard;
    long uffd;
    char *area;

    page_size = sysconf(_SC_PAGE_SIZE);

    uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
    if (uffd == -1) {
        fprintf(stderr, "Could not create uffd: %s\n", strerror(errno));
        exit(-1);
    }
    if (ioctl(uffd, UFFDIO_API, &api) == -1) {
        fprintf(stderr, "UFFDIO_API failed: %s\n", strerror(errno));
        exit(-1);
    }

    area = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
                MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (area == MAP_FAILED) {
        fprintf(stderr, "Could not allocate memory");
        exit(-1);
    }

    /* make sure the area is empty */
    if (madvise(area, page_size, MADV_DONTNEED)) {
        fprintf(stderr, "MADV_DONTNEED failed:%s\n", strerror(errno));
        exit(-1);
    }

    /* register a WP handler */
    reg.range.start = (uint64_t) area;
    reg.range.len = page_size,
    reg.mode = UFFDIO_REGISTER_MODE_WP;
    if (ioctl(uffd, UFFDIO_REGISTER, &reg) == -1) {
        fprintf(stderr, "UFFDIO_REGISTER failed: %s\n", strerror(errno));
        exit(-1);
    }

    /*
     * comment this out to make the wp actually fire
    zeropage.range = reg.range;
    zeropage.mode = UFFDIO_ZEROPAGE_MODE_DONTWAKE;
    if (ioctl(uffd, UFFDIO_ZEROPAGE, &zeropage) == -1) {
        fprintf(stderr, "UFFDIO_ZEROPAGE failed: %s\n", strerror(errno));
        exit(-1);
    }
    */

    /* protect the page */
    wp.range = reg.range;
    wp.mode = UFFDIO_WRITEPROTECT_MODE_WP;
    if (ioctl(uffd, UFFDIO_WRITEPROTECT, &wp) == -1) {
        fprintf(stderr, "UFFDIO_WRITEPROTECT failed: %s\n", strerror(errno));
        exit(-1);
    }

    if (pthread_create(&fault, NULL, fault_handler_thread,
                       (void *) uffd)) {
        fprintf(stderr, "Could not create fault handing thread");
        exit(-1);
    }

    *area = 0;
    if (wp_fired) {
        printf("WP fired\n");
        return 0;
    }
    printf("WP did not fire\n");
    return -1;
}

Reply via email to