protection-keys.txt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. Memory Protection Keys for Userspace (PKU aka PKEYs) is a feature
  2. which is found on Intel's Skylake "Scalable Processor" Server CPUs.
  3. It will be avalable in future non-server parts.
  4. For anyone wishing to test or use this feature, it is available in
  5. Amazon's EC2 C5 instances and is known to work there using an Ubuntu
  6. 17.04 image.
  7. Memory Protection Keys provides a mechanism for enforcing page-based
  8. protections, but without requiring modification of the page tables
  9. when an application changes protection domains. It works by
  10. dedicating 4 previously ignored bits in each page table entry to a
  11. "protection key", giving 16 possible keys.
  12. There is also a new user-accessible register (PKRU) with two separate
  13. bits (Access Disable and Write Disable) for each key. Being a CPU
  14. register, PKRU is inherently thread-local, potentially giving each
  15. thread a different set of protections from every other thread.
  16. There are two new instructions (RDPKRU/WRPKRU) for reading and writing
  17. to the new register. The feature is only available in 64-bit mode,
  18. even though there is theoretically space in the PAE PTEs. These
  19. permissions are enforced on data access only and have no effect on
  20. instruction fetches.
  21. =========================== Syscalls ===========================
  22. There are 3 system calls which directly interact with pkeys:
  23. int pkey_alloc(unsigned long flags, unsigned long init_access_rights)
  24. int pkey_free(int pkey);
  25. int pkey_mprotect(unsigned long start, size_t len,
  26. unsigned long prot, int pkey);
  27. Before a pkey can be used, it must first be allocated with
  28. pkey_alloc(). An application calls the WRPKRU instruction
  29. directly in order to change access permissions to memory covered
  30. with a key. In this example WRPKRU is wrapped by a C function
  31. called pkey_set().
  32. int real_prot = PROT_READ|PROT_WRITE;
  33. pkey = pkey_alloc(0, PKEY_DISABLE_WRITE);
  34. ptr = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  35. ret = pkey_mprotect(ptr, PAGE_SIZE, real_prot, pkey);
  36. ... application runs here
  37. Now, if the application needs to update the data at 'ptr', it can
  38. gain access, do the update, then remove its write access:
  39. pkey_set(pkey, 0); // clear PKEY_DISABLE_WRITE
  40. *ptr = foo; // assign something
  41. pkey_set(pkey, PKEY_DISABLE_WRITE); // set PKEY_DISABLE_WRITE again
  42. Now when it frees the memory, it will also free the pkey since it
  43. is no longer in use:
  44. munmap(ptr, PAGE_SIZE);
  45. pkey_free(pkey);
  46. (Note: pkey_set() is a wrapper for the RDPKRU and WRPKRU instructions.
  47. An example implementation can be found in
  48. tools/testing/selftests/x86/protection_keys.c)
  49. =========================== Behavior ===========================
  50. The kernel attempts to make protection keys consistent with the
  51. behavior of a plain mprotect(). For instance if you do this:
  52. mprotect(ptr, size, PROT_NONE);
  53. something(ptr);
  54. you can expect the same effects with protection keys when doing this:
  55. pkey = pkey_alloc(0, PKEY_DISABLE_WRITE | PKEY_DISABLE_READ);
  56. pkey_mprotect(ptr, size, PROT_READ|PROT_WRITE, pkey);
  57. something(ptr);
  58. That should be true whether something() is a direct access to 'ptr'
  59. like:
  60. *ptr = foo;
  61. or when the kernel does the access on the application's behalf like
  62. with a read():
  63. read(fd, ptr, 1);
  64. The kernel will send a SIGSEGV in both cases, but si_code will be set
  65. to SEGV_PKERR when violating protection keys versus SEGV_ACCERR when
  66. the plain mprotect() permissions are violated.