// SPDX-License-Identifier: GPL-2.0/* * Tests Memory Protection Keys (see Documentation/vm/protection-keys.txt) * * There are examples in here of: * * how to set protection keys on memory * * how to set/clear bits in pkey registers (the rights register) * * how to handle SEGV_PKUERR signals and extract pkey-relevant * information from the siginfo * * Things to add: * make sure KSM and KSM COW breaking works * prefault pages in at malloc, or not * protect MPX bounds tables with protection keys? * make sure VMA splitting/merging is working correctly * OOMs can destroy mm->mmap (see exit_mmap()), so make sure it is immune to pkeys * look for pkey "leaks" where it is still set on a VMA but "freed" back to the kernel * do a plain mprotect() to a mprotect_pkey() area and make sure the pkey sticks * * Compile like this: * gcc -o protection_keys -O2 -g -std=gnu99 -pthread -Wall protection_keys.c -lrt -ldl -lm * gcc -m32 -o protection_keys_32 -O2 -g -std=gnu99 -pthread -Wall protection_keys.c -lrt -ldl -lm */#define _GNU_SOURCE#define __SANE_USERSPACE_TYPES__#include<errno.h>#include<linux/futex.h>#include<time.h>#include<sys/time.h>#include<sys/syscall.h>#include<string.h>#include<stdio.h>#include<stdint.h>#include<stdbool.h>#include<signal.h>#include<assert.h>#include<stdlib.h>#include<ucontext.h>#include<sys/mman.h>#include<sys/types.h>#include<sys/wait.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>#include<sys/ptrace.h>#include<setjmp.h>