blob: ec575e72d0b2ab7da24924aacd3ed61324533c53 [file] [log] [blame]
Changbin Du28e21ea2019-05-08 23:21:26 +08001.. SPDX-License-Identifier: GPL-2.0
2
3======================
4Memory Protection Keys
5======================
6
Dave Hansenc51ff2c2017-11-10 16:12:28 -08007Memory Protection Keys for Userspace (PKU aka PKEYs) is a feature
Babu Moger38f3e772020-05-28 11:08:23 -05008which is found on Intel's Skylake (and later) "Scalable Processor"
9Server CPUs. It will be available in future non-server Intel parts
10and future AMD processors.
Dave Hansenc51ff2c2017-11-10 16:12:28 -080011
12For anyone wishing to test or use this feature, it is available in
13Amazon's EC2 C5 instances and is known to work there using an Ubuntu
1417.04 image.
Dave Hansen591b1d82015-12-14 11:06:34 -080015
16Memory Protection Keys provides a mechanism for enforcing page-based
17protections, but without requiring modification of the page tables
18when an application changes protection domains. It works by
19dedicating 4 previously ignored bits in each page table entry to a
20"protection key", giving 16 possible keys.
21
22There is also a new user-accessible register (PKRU) with two separate
23bits (Access Disable and Write Disable) for each key. Being a CPU
24register, PKRU is inherently thread-local, potentially giving each
25thread a different set of protections from every other thread.
26
27There are two new instructions (RDPKRU/WRPKRU) for reading and writing
28to the new register. The feature is only available in 64-bit mode,
29even though there is theoretically space in the PAE PTEs. These
30permissions are enforced on data access only and have no effect on
31instruction fetches.
32
Changbin Du28e21ea2019-05-08 23:21:26 +080033Syscalls
34========
Dave Hansenc74fe392016-07-29 09:30:20 -070035
Changbin Du28e21ea2019-05-08 23:21:26 +080036There are 3 system calls which directly interact with pkeys::
Dave Hansenc74fe392016-07-29 09:30:20 -070037
38 int pkey_alloc(unsigned long flags, unsigned long init_access_rights)
39 int pkey_free(int pkey);
40 int pkey_mprotect(unsigned long start, size_t len,
41 unsigned long prot, int pkey);
42
43Before a pkey can be used, it must first be allocated with
44pkey_alloc(). An application calls the WRPKRU instruction
45directly in order to change access permissions to memory covered
46with a key. In this example WRPKRU is wrapped by a C function
47called pkey_set().
Changbin Du28e21ea2019-05-08 23:21:26 +080048::
Dave Hansenc74fe392016-07-29 09:30:20 -070049
50 int real_prot = PROT_READ|PROT_WRITE;
Wang Kaif90e2d92017-07-24 21:03:46 +080051 pkey = pkey_alloc(0, PKEY_DISABLE_WRITE);
Dave Hansenc74fe392016-07-29 09:30:20 -070052 ptr = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
53 ret = pkey_mprotect(ptr, PAGE_SIZE, real_prot, pkey);
54 ... application runs here
55
56Now, if the application needs to update the data at 'ptr', it can
Changbin Du28e21ea2019-05-08 23:21:26 +080057gain access, do the update, then remove its write access::
Dave Hansenc74fe392016-07-29 09:30:20 -070058
Wang Kaif90e2d92017-07-24 21:03:46 +080059 pkey_set(pkey, 0); // clear PKEY_DISABLE_WRITE
Dave Hansenc74fe392016-07-29 09:30:20 -070060 *ptr = foo; // assign something
Wang Kaif90e2d92017-07-24 21:03:46 +080061 pkey_set(pkey, PKEY_DISABLE_WRITE); // set PKEY_DISABLE_WRITE again
Dave Hansenc74fe392016-07-29 09:30:20 -070062
63Now when it frees the memory, it will also free the pkey since it
Changbin Du28e21ea2019-05-08 23:21:26 +080064is no longer in use::
Dave Hansenc74fe392016-07-29 09:30:20 -070065
66 munmap(ptr, PAGE_SIZE);
67 pkey_free(pkey);
68
Changbin Du28e21ea2019-05-08 23:21:26 +080069.. note:: pkey_set() is a wrapper for the RDPKRU and WRPKRU instructions.
70 An example implementation can be found in
71 tools/testing/selftests/x86/protection_keys.c.
Dave Hansen6679dac2016-10-04 09:38:57 -070072
Changbin Du28e21ea2019-05-08 23:21:26 +080073Behavior
74========
Dave Hansenc74fe392016-07-29 09:30:20 -070075
76The kernel attempts to make protection keys consistent with the
Changbin Du28e21ea2019-05-08 23:21:26 +080077behavior of a plain mprotect(). For instance if you do this::
Dave Hansenc74fe392016-07-29 09:30:20 -070078
79 mprotect(ptr, size, PROT_NONE);
80 something(ptr);
81
Changbin Du28e21ea2019-05-08 23:21:26 +080082you can expect the same effects with protection keys when doing this::
Dave Hansenc74fe392016-07-29 09:30:20 -070083
84 pkey = pkey_alloc(0, PKEY_DISABLE_WRITE | PKEY_DISABLE_READ);
85 pkey_mprotect(ptr, size, PROT_READ|PROT_WRITE, pkey);
86 something(ptr);
87
88That should be true whether something() is a direct access to 'ptr'
Changbin Du28e21ea2019-05-08 23:21:26 +080089like::
Dave Hansenc74fe392016-07-29 09:30:20 -070090
91 *ptr = foo;
92
93or when the kernel does the access on the application's behalf like
Changbin Du28e21ea2019-05-08 23:21:26 +080094with a read()::
Dave Hansenc74fe392016-07-29 09:30:20 -070095
96 read(fd, ptr, 1);
97
98The kernel will send a SIGSEGV in both cases, but si_code will be set
99to SEGV_PKERR when violating protection keys versus SEGV_ACCERR when
100the plain mprotect() permissions are violated.