Changbin Du | 28e21ea | 2019-05-08 23:21:26 +0800 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | ====================== |
| 4 | Memory Protection Keys |
| 5 | ====================== |
| 6 | |
Ira Weiny | f8c1d4c | 2022-04-19 10:06:06 -0700 | [diff] [blame] | 7 | Memory Protection Keys provide a mechanism for enforcing page-based |
| 8 | protections, but without requiring modification of the page tables when an |
| 9 | application changes protection domains. |
Dave Hansen | c51ff2c | 2017-11-10 16:12:28 -0800 | [diff] [blame] | 10 | |
Ira Weiny | f8c1d4c | 2022-04-19 10:06:06 -0700 | [diff] [blame] | 11 | Pkeys Userspace (PKU) is a feature which can be found on: |
| 12 | * Intel server CPUs, Skylake and later |
| 13 | * Intel client CPUs, Tiger Lake (11th Gen Core) and later |
| 14 | * Future AMD CPUs |
Dave Hansen | 591b1d8 | 2015-12-14 11:06:34 -0800 | [diff] [blame] | 15 | |
Ira Weiny | f8c1d4c | 2022-04-19 10:06:06 -0700 | [diff] [blame] | 16 | Pkeys work by dedicating 4 previously Reserved bits in each page table entry to |
| 17 | a "protection key", giving 16 possible keys. |
Dave Hansen | 591b1d8 | 2015-12-14 11:06:34 -0800 | [diff] [blame] | 18 | |
Ira Weiny | f8c1d4c | 2022-04-19 10:06:06 -0700 | [diff] [blame] | 19 | Protections for each key are defined with a per-CPU user-accessible register |
| 20 | (PKRU). Each of these is a 32-bit register storing two bits (Access Disable |
| 21 | and Write Disable) for each of 16 keys. |
| 22 | |
| 23 | Being a CPU register, PKRU is inherently thread-local, potentially giving each |
Dave Hansen | 591b1d8 | 2015-12-14 11:06:34 -0800 | [diff] [blame] | 24 | thread a different set of protections from every other thread. |
| 25 | |
Ira Weiny | f8c1d4c | 2022-04-19 10:06:06 -0700 | [diff] [blame] | 26 | There are two instructions (RDPKRU/WRPKRU) for reading and writing to the |
| 27 | register. The feature is only available in 64-bit mode, even though there is |
| 28 | theoretically space in the PAE PTEs. These permissions are enforced on data |
| 29 | access only and have no effect on instruction fetches. |
Dave Hansen | 591b1d8 | 2015-12-14 11:06:34 -0800 | [diff] [blame] | 30 | |
Changbin Du | 28e21ea | 2019-05-08 23:21:26 +0800 | [diff] [blame] | 31 | Syscalls |
| 32 | ======== |
Dave Hansen | c74fe39 | 2016-07-29 09:30:20 -0700 | [diff] [blame] | 33 | |
Changbin Du | 28e21ea | 2019-05-08 23:21:26 +0800 | [diff] [blame] | 34 | There are 3 system calls which directly interact with pkeys:: |
Dave Hansen | c74fe39 | 2016-07-29 09:30:20 -0700 | [diff] [blame] | 35 | |
| 36 | int pkey_alloc(unsigned long flags, unsigned long init_access_rights) |
| 37 | int pkey_free(int pkey); |
| 38 | int pkey_mprotect(unsigned long start, size_t len, |
| 39 | unsigned long prot, int pkey); |
| 40 | |
| 41 | Before a pkey can be used, it must first be allocated with |
| 42 | pkey_alloc(). An application calls the WRPKRU instruction |
| 43 | directly in order to change access permissions to memory covered |
| 44 | with a key. In this example WRPKRU is wrapped by a C function |
| 45 | called pkey_set(). |
Changbin Du | 28e21ea | 2019-05-08 23:21:26 +0800 | [diff] [blame] | 46 | :: |
Dave Hansen | c74fe39 | 2016-07-29 09:30:20 -0700 | [diff] [blame] | 47 | |
| 48 | int real_prot = PROT_READ|PROT_WRITE; |
Wang Kai | f90e2d9 | 2017-07-24 21:03:46 +0800 | [diff] [blame] | 49 | pkey = pkey_alloc(0, PKEY_DISABLE_WRITE); |
Dave Hansen | c74fe39 | 2016-07-29 09:30:20 -0700 | [diff] [blame] | 50 | ptr = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); |
| 51 | ret = pkey_mprotect(ptr, PAGE_SIZE, real_prot, pkey); |
| 52 | ... application runs here |
| 53 | |
| 54 | Now, if the application needs to update the data at 'ptr', it can |
Changbin Du | 28e21ea | 2019-05-08 23:21:26 +0800 | [diff] [blame] | 55 | gain access, do the update, then remove its write access:: |
Dave Hansen | c74fe39 | 2016-07-29 09:30:20 -0700 | [diff] [blame] | 56 | |
Wang Kai | f90e2d9 | 2017-07-24 21:03:46 +0800 | [diff] [blame] | 57 | pkey_set(pkey, 0); // clear PKEY_DISABLE_WRITE |
Dave Hansen | c74fe39 | 2016-07-29 09:30:20 -0700 | [diff] [blame] | 58 | *ptr = foo; // assign something |
Wang Kai | f90e2d9 | 2017-07-24 21:03:46 +0800 | [diff] [blame] | 59 | pkey_set(pkey, PKEY_DISABLE_WRITE); // set PKEY_DISABLE_WRITE again |
Dave Hansen | c74fe39 | 2016-07-29 09:30:20 -0700 | [diff] [blame] | 60 | |
| 61 | Now when it frees the memory, it will also free the pkey since it |
Changbin Du | 28e21ea | 2019-05-08 23:21:26 +0800 | [diff] [blame] | 62 | is no longer in use:: |
Dave Hansen | c74fe39 | 2016-07-29 09:30:20 -0700 | [diff] [blame] | 63 | |
| 64 | munmap(ptr, PAGE_SIZE); |
| 65 | pkey_free(pkey); |
| 66 | |
Changbin Du | 28e21ea | 2019-05-08 23:21:26 +0800 | [diff] [blame] | 67 | .. note:: pkey_set() is a wrapper for the RDPKRU and WRPKRU instructions. |
| 68 | An example implementation can be found in |
| 69 | tools/testing/selftests/x86/protection_keys.c. |
Dave Hansen | 6679dac | 2016-10-04 09:38:57 -0700 | [diff] [blame] | 70 | |
Changbin Du | 28e21ea | 2019-05-08 23:21:26 +0800 | [diff] [blame] | 71 | Behavior |
| 72 | ======== |
Dave Hansen | c74fe39 | 2016-07-29 09:30:20 -0700 | [diff] [blame] | 73 | |
| 74 | The kernel attempts to make protection keys consistent with the |
Changbin Du | 28e21ea | 2019-05-08 23:21:26 +0800 | [diff] [blame] | 75 | behavior of a plain mprotect(). For instance if you do this:: |
Dave Hansen | c74fe39 | 2016-07-29 09:30:20 -0700 | [diff] [blame] | 76 | |
| 77 | mprotect(ptr, size, PROT_NONE); |
| 78 | something(ptr); |
| 79 | |
Changbin Du | 28e21ea | 2019-05-08 23:21:26 +0800 | [diff] [blame] | 80 | you can expect the same effects with protection keys when doing this:: |
Dave Hansen | c74fe39 | 2016-07-29 09:30:20 -0700 | [diff] [blame] | 81 | |
| 82 | pkey = pkey_alloc(0, PKEY_DISABLE_WRITE | PKEY_DISABLE_READ); |
| 83 | pkey_mprotect(ptr, size, PROT_READ|PROT_WRITE, pkey); |
| 84 | something(ptr); |
| 85 | |
| 86 | That should be true whether something() is a direct access to 'ptr' |
Changbin Du | 28e21ea | 2019-05-08 23:21:26 +0800 | [diff] [blame] | 87 | like:: |
Dave Hansen | c74fe39 | 2016-07-29 09:30:20 -0700 | [diff] [blame] | 88 | |
| 89 | *ptr = foo; |
| 90 | |
| 91 | or when the kernel does the access on the application's behalf like |
Changbin Du | 28e21ea | 2019-05-08 23:21:26 +0800 | [diff] [blame] | 92 | with a read():: |
Dave Hansen | c74fe39 | 2016-07-29 09:30:20 -0700 | [diff] [blame] | 93 | |
| 94 | read(fd, ptr, 1); |
| 95 | |
| 96 | The kernel will send a SIGSEGV in both cases, but si_code will be set |
| 97 | to SEGV_PKERR when violating protection keys versus SEGV_ACCERR when |
| 98 | the plain mprotect() permissions are violated. |