Project

General

Profile

Bug #1261 ยป open-fault.c

Mathieu Desnoyers, 02/12/2021 09:52 AM

 
1
#include <sys/types.h>
2
#include <sys/stat.h>
3
#include <fcntl.h>
4
#include <stdint.h>
5
#include <syscall.h>
6
#include <unistd.h>
7
#include <sys/mman.h>
8
#include <stdio.h>
9
#include <error.h>
10
#include <stdlib.h>
11
#include <string.h>
12

    
13
#define NAME_FILE "/tmp/filefile1"
14
#define PATH_STR "/tmp/filefile2"
15

    
16
int main()
17
{
18
	int ret;
19
	int fd;
20
	char *name;
21
	ssize_t len;
22

    
23
	fd = open(NAME_FILE, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR,
24
			0700);
25
	if (fd < 0) {
26
		perror("open1");
27
		abort();
28
	}
29
	len = write(fd, PATH_STR, strlen(PATH_STR) + 1);
30
	if (len < strlen(PATH_STR) + 1) {
31
		abort();
32
	}
33
	/* mmap the name file which will be used as input string for openat
34
	 * filename parameter, triggering a page fault because the pages of this
35
	 * mapping are not populated in the page cache.
36
	 */
37
	name = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
38
	if (name == MAP_FAILED) {
39
		perror("mmap");
40
		abort();
41
	}
42
	openat(42, name, 0, 0);
43
}
44

    
    (1-1/1)