pkeys(7) Miscellaneous Information Manual pkeys(7) pkeys - (pkeys) -- . , TLB. . pkeys, <<>> (tag) pkey. . PROT_READ, PROT_WRITE PROT_EXEC , mprotect(2) mmap(2), . , pkey, SIGSEGV. sigaction(2). pkeys, , . 2016 Intel x86, 16 . pkey 0 , 15. , pkey pkey_mprotect(2). , . , , . , WRPKRU -- , pkeys , PKRU . , << >>. , pkey_free(2) , pkey . pkey, pkey , . pkey_free(2) pkeys, . . , pkey, /proc/pid/smaps. proc(5). , , . - , , , , . pkey_alloc(2) , - . , cpuid. , , Intel. /proc/cpuinfo <>. <> , , <> , . , . clone(2). , clone(2) . , ( ), , . , , . . - , x86 PKRU ( ) (XSAVE) . . Linux pkey: pkey_mprotect(2), pkey_alloc(2) pkey_free(2). Linux pkey , CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS. , , . . WRPKRU. , , , . $ ./a.out : 73 ... Segmentation fault (core dumped) #define _GNU_SOURCE #include #include #include #include #include int main(void) { int status; int pkey; int *buffer; /* * Allocate one page of memory. */ buffer = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (buffer == MAP_FAILED) err(EXIT_FAILURE, "mmap"); /* * Put some random data into the page (still OK to touch). */ *buffer = __LINE__; printf("buffer contains: %d\n", *buffer); /* * Allocate a protection key: */ pkey = pkey_alloc(0, 0); if (pkey == -1) err(EXIT_FAILURE, "pkey_alloc"); /* * Disable access to any memory with "pkey" set, * even though there is none right now. */ status = pkey_set(pkey, PKEY_DISABLE_ACCESS); if (status) err(EXIT_FAILURE, "pkey_set"); /* * Set the protection key on "buffer". * Note that it is still read/write as far as mprotect() is * concerned and the previous pkey_set() overrides it. */ status = pkey_mprotect(buffer, getpagesize(), PROT_READ | PROT_WRITE, pkey); if (status == -1) err(EXIT_FAILURE, "pkey_mprotect"); printf("about to read buffer again...\n"); /* * This will crash, because we have disallowed access. */ printf("buffer contains: %d\n", *buffer); status = pkey_free(pkey); if (status == -1) err(EXIT_FAILURE, "pkey_free"); exit(EXIT_SUCCESS); } pkey_alloc(2), pkey_free(2), pkey_mprotect(2), sigaction(2) () Alexey, Azamat Hackimov , kogamatranslator49 , Darima Kogan , Max Is , Yuri Kozlov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.9.1 15 2024 . pkeys(7)