An attempt was made to protect a static variable array with Mprotect, and when the protected portion of the static variable is written, the program core dump
#include <stdio.h> #include <stdlib.h> #include <sys/mman.h> #include < UNISTD.H>INT&NBSP;PROTECT_BSS (Void * p, size_t len) { size_t page_size = sysconf (_sc_page_size); if (page_size == -1) { return -1; } size_t start = (size_t) p; size_t new_start = (start + (page_size - 1) / page_size * page_size; size_t new_len = (Start + len - new_start) / page_size * page_size; if (Mprotect (void *) new_start, new_len, prot_read) == 0) { printf ( "bss [%p, %p), protected [%p, %p) \ n ", p, p+len, (void*) new_start, (void*) new_start+new_len); return 0; } return -1;} Char bss[20000];int main (void) { printf ("%d\n", &NBSP;PROTECT_BSS (bss, sizeof (BSS)); bss[10000] = 1; return 0;}
"Practice" attempts to protect a static variable through Mprotect