protection-keys.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * arm64 CPUs implementing the Permission Overlay Extension (FEAT_S1POE)
  13. x86_64
  14. ======
  15. Pkeys work by dedicating 4 previously Reserved bits in each page table entry to
  16. a "protection key", giving 16 possible keys.
  17. Protections for each key are defined with a per-CPU user-accessible register
  18. (PKRU). Each of these is a 32-bit register storing two bits (Access Disable
  19. and Write Disable) for each of 16 keys.
  20. Being a CPU register, PKRU is inherently thread-local, potentially giving each
  21. thread a different set of protections from every other thread.
  22. There are two instructions (RDPKRU/WRPKRU) for reading and writing to the
  23. register. The feature is only available in 64-bit mode, even though there is
  24. theoretically space in the PAE PTEs. These permissions are enforced on data
  25. access only and have no effect on instruction fetches.
  26. arm64
  27. =====
  28. Pkeys use 3 bits in each page table entry, to encode a "protection key index",
  29. giving 8 possible keys.
  30. Protections for each key are defined with a per-CPU user-writable system
  31. register (POR_EL0). This is a 64-bit register encoding read, write and execute
  32. overlay permissions for each protection key index.
  33. Being a CPU register, POR_EL0 is inherently thread-local, potentially giving
  34. each thread a different set of protections from every other thread.
  35. Unlike x86_64, the protection key permissions also apply to instruction
  36. fetches.
  37. Syscalls
  38. ========
  39. There are 3 system calls which directly interact with pkeys::
  40. int pkey_alloc(unsigned long flags, unsigned long init_access_rights)
  41. int pkey_free(int pkey);
  42. int pkey_mprotect(unsigned long start, size_t len,
  43. unsigned long prot, int pkey);
  44. Before a pkey can be used, it must first be allocated with pkey_alloc(). An
  45. application writes to the architecture specific CPU register directly in order
  46. to change access permissions to memory covered with a key. In this example
  47. this is wrapped by a C function called pkey_set().
  48. ::
  49. int real_prot = PROT_READ|PROT_WRITE;
  50. pkey = pkey_alloc(0, PKEY_DISABLE_WRITE);
  51. ptr = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  52. ret = pkey_mprotect(ptr, PAGE_SIZE, real_prot, pkey);
  53. ... application runs here
  54. Now, if the application needs to update the data at 'ptr', it can
  55. gain access, do the update, then remove its write access::
  56. pkey_set(pkey, 0); // clear PKEY_DISABLE_WRITE
  57. *ptr = foo; // assign something
  58. pkey_set(pkey, PKEY_DISABLE_WRITE); // set PKEY_DISABLE_WRITE again
  59. Now when it frees the memory, it will also free the pkey since it
  60. is no longer in use::
  61. munmap(ptr, PAGE_SIZE);
  62. pkey_free(pkey);
  63. .. note:: pkey_set() is a wrapper around writing to the CPU register.
  64. Example implementations can be found in
  65. tools/testing/selftests/mm/pkey-{arm64,powerpc,x86}.h
  66. Behavior
  67. ========
  68. The kernel attempts to make protection keys consistent with the
  69. behavior of a plain mprotect(). For instance if you do this::
  70. mprotect(ptr, size, PROT_NONE);
  71. something(ptr);
  72. you can expect the same effects with protection keys when doing this::
  73. pkey = pkey_alloc(0, PKEY_DISABLE_WRITE | PKEY_DISABLE_READ);
  74. pkey_mprotect(ptr, size, PROT_READ|PROT_WRITE, pkey);
  75. something(ptr);
  76. That should be true whether something() is a direct access to 'ptr'
  77. like::
  78. *ptr = foo;
  79. or when the kernel does the access on the application's behalf like
  80. with a read()::
  81. read(fd, ptr, 1);
  82. The kernel will send a SIGSEGV in both cases, but si_code will be set
  83. to SEGV_PKERR when violating protection keys versus SEGV_ACCERR when
  84. the plain mprotect() permissions are violated.
  85. Note that kernel accesses from a kthread (such as io_uring) will use a default
  86. value for the protection key register and so will not be consistent with
  87. userspace's value of the register or mprotect().