protection-keys.rst 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ======================
  3. Memory Protection Keys
  4. ======================
  5. Memory Protection Keys provide a mechanism for enforcing page-based
  6. protections, but without requiring modification of the page tables when an
  7. application changes protection domains.
  8. Pkeys Userspace (PKU) is a feature which can be found on:
  9. * Intel server CPUs, Skylake and later
  10. * Intel client CPUs, Tiger Lake (11th Gen Core) and later
  11. * Future AMD CPUs
  12. Pkeys work by dedicating 4 previously Reserved bits in each page table entry to
  13. a "protection key", giving 16 possible keys.
  14. Protections for each key are defined with a per-CPU user-accessible register
  15. (PKRU). Each of these is a 32-bit register storing two bits (Access Disable
  16. and Write Disable) for each of 16 keys.
  17. Being a CPU register, PKRU is inherently thread-local, potentially giving each
  18. thread a different set of protections from every other thread.
  19. There are two instructions (RDPKRU/WRPKRU) for reading and writing to the
  20. register. The feature is only available in 64-bit mode, even though there is
  21. theoretically space in the PAE PTEs. These permissions are enforced on data
  22. access only and have no effect on instruction fetches.
  23. Syscalls
  24. ========
  25. There are 3 system calls which directly interact with pkeys::
  26. int pkey_alloc(unsigned long flags, unsigned long init_access_rights)
  27. int pkey_free(int pkey);
  28. int pkey_mprotect(unsigned long start, size_t len,
  29. unsigned long prot, int pkey);
  30. Before a pkey can be used, it must first be allocated with
  31. pkey_alloc(). An application calls the WRPKRU instruction
  32. directly in order to change access permissions to memory covered
  33. with a key. In this example WRPKRU is wrapped by a C function
  34. called pkey_set().
  35. ::
  36. int real_prot = PROT_READ|PROT_WRITE;
  37. pkey = pkey_alloc(0, PKEY_DISABLE_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. Now, if the application needs to update the data at 'ptr', it can
  42. gain access, do the update, then remove its write access::
  43. pkey_set(pkey, 0); // clear PKEY_DISABLE_WRITE
  44. *ptr = foo; // assign something
  45. pkey_set(pkey, PKEY_DISABLE_WRITE); // set PKEY_DISABLE_WRITE again
  46. Now when it frees the memory, it will also free the pkey since it
  47. is no longer in use::
  48. munmap(ptr, PAGE_SIZE);
  49. pkey_free(pkey);
  50. .. note:: pkey_set() is a wrapper for the RDPKRU and WRPKRU instructions.
  51. An example implementation can be found in
  52. tools/testing/selftests/x86/protection_keys.c.
  53. Behavior
  54. ========
  55. The kernel attempts to make protection keys consistent with the
  56. behavior of a plain mprotect(). For instance if you do this::
  57. mprotect(ptr, size, PROT_NONE);
  58. something(ptr);
  59. you can expect the same effects with protection keys when doing this::
  60. pkey = pkey_alloc(0, PKEY_DISABLE_WRITE | PKEY_DISABLE_READ);
  61. pkey_mprotect(ptr, size, PROT_READ|PROT_WRITE, pkey);
  62. something(ptr);
  63. That should be true whether something() is a direct access to 'ptr'
  64. like::
  65. *ptr = foo;
  66. or when the kernel does the access on the application's behalf like
  67. with a read()::
  68. read(fd, ptr, 1);
  69. The kernel will send a SIGSEGV in both cases, but si_code will be set
  70. to SEGV_PKERR when violating protection keys versus SEGV_ACCERR when
  71. the plain mprotect() permissions are violated.