blob: 6da7689601d1997b76557f2112a3055f6eb581e3 [file] [log] [blame]
Dave Hansen591b1d82015-12-14 11:06:34 -08001Memory Protection Keys for Userspace (PKU aka PKEYs) is a CPU feature
2which will be found on future Intel CPUs.
3
4Memory Protection Keys provides a mechanism for enforcing page-based
5protections, but without requiring modification of the page tables
6when an application changes protection domains. It works by
7dedicating 4 previously ignored bits in each page table entry to a
8"protection key", giving 16 possible keys.
9
10There is also a new user-accessible register (PKRU) with two separate
11bits (Access Disable and Write Disable) for each key. Being a CPU
12register, PKRU is inherently thread-local, potentially giving each
13thread a different set of protections from every other thread.
14
15There are two new instructions (RDPKRU/WRPKRU) for reading and writing
16to the new register. The feature is only available in 64-bit mode,
17even though there is theoretically space in the PAE PTEs. These
18permissions are enforced on data access only and have no effect on
19instruction fetches.
20
Dave Hansenc74fe392016-07-29 09:30:20 -070021=========================== Syscalls ===========================
22
23There are 2 system calls which directly interact with pkeys:
24
25 int pkey_alloc(unsigned long flags, unsigned long init_access_rights)
26 int pkey_free(int pkey);
27 int pkey_mprotect(unsigned long start, size_t len,
28 unsigned long prot, int pkey);
29
30Before a pkey can be used, it must first be allocated with
31pkey_alloc(). An application calls the WRPKRU instruction
32directly in order to change access permissions to memory covered
33with a key. In this example WRPKRU is wrapped by a C function
34called pkey_set().
35
36 int real_prot = PROT_READ|PROT_WRITE;
37 pkey = pkey_alloc(0, PKEY_DENY_WRITE);
38 ptr = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
39 ret = pkey_mprotect(ptr, PAGE_SIZE, real_prot, pkey);
40 ... application runs here
41
42Now, if the application needs to update the data at 'ptr', it can
43gain access, do the update, then remove its write access:
44
45 pkey_set(pkey, 0); // clear PKEY_DENY_WRITE
46 *ptr = foo; // assign something
47 pkey_set(pkey, PKEY_DENY_WRITE); // set PKEY_DENY_WRITE again
48
49Now when it frees the memory, it will also free the pkey since it
50is no longer in use:
51
52 munmap(ptr, PAGE_SIZE);
53 pkey_free(pkey);
54
55=========================== Behavior ===========================
56
57The kernel attempts to make protection keys consistent with the
58behavior of a plain mprotect(). For instance if you do this:
59
60 mprotect(ptr, size, PROT_NONE);
61 something(ptr);
62
63you can expect the same effects with protection keys when doing this:
64
65 pkey = pkey_alloc(0, PKEY_DISABLE_WRITE | PKEY_DISABLE_READ);
66 pkey_mprotect(ptr, size, PROT_READ|PROT_WRITE, pkey);
67 something(ptr);
68
69That should be true whether something() is a direct access to 'ptr'
70like:
71
72 *ptr = foo;
73
74or when the kernel does the access on the application's behalf like
75with a read():
76
77 read(fd, ptr, 1);
78
79The kernel will send a SIGSEGV in both cases, but si_code will be set
80to SEGV_PKERR when violating protection keys versus SEGV_ACCERR when
81the plain mprotect() permissions are violated.
82
Dave Hansen591b1d82015-12-14 11:06:34 -080083=========================== Config Option ===========================
84
85This config option adds approximately 1.5kb of text. and 50 bytes of
86data to the executable. A workload which does large O_DIRECT reads
87of holes in XFS files was run to exercise get_user_pages_fast(). No
88performance delta was observed with the config option
89enabled or disabled.