A note about mprotect (2)

Overview

Note that I didn't really understand the regular mprotect as a result of strace

What is mprotect (2)?

mprotect (2) is a system call that controls memory area permissions.

man mprotect(2)

#include <sys/mman.h>

int mprotect(const void *addr, size_t len, int prot);

Pass the pointer, size, and bitwise OR.

flag

Currently, protection bits that can be combined with the following ORs

Flag name Overview
PROT_NONE No protection at all
PROT_READ Page is readable
PROT_WRITE Page is writable
PROT_EXEC Page is executable

error

If successful, mprotect () returns 0. In case of an error, -1 is returned and errno is set appropriately.

errno Overview
EACCES Unable to set specified access to memory
EINVAL addr is not a valid pointer or is not a multiple of the system page size
ENOMEM Could not allocate structure inside kernel
ENOMEM [addr, addr+len-1]The address in the range is invalid as the process's address space, or the address in the range points to one or more pages that are not mapped.

Example of use

A sample program that generates SIGSEGV by writing to the memory in the READ_ONLY area.

SIGSEGV itself executes the processing handled using sigaction.

mprotect.c


#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>

#define handle_error(msg) \
    do { perror(msg); exit(EXIT_FAILURE); } while (0)

static char *buffer;

static void handler(int sig, siginfo_t *si, void *unused)
{
    printf("Got SIGSEGV at address: 0x%lx\n",
        (long) si->si_addr);
    exit(EXIT_FAILURE);
}

int main(int argc, char **argv)
{
    char *p;
    int pagesize;
    

    //Changes in signal behavior
    struct sigaction sa;
    sa.sa_flags = SA_SIGINFO;
    sigemptyset(&sa.sa_mask);
    sa.sa_sigaction = handler;
    if (sigaction(SIGSEGV, &sa, NULL) == -1)
        handle_error("sigaction");

    pagesize = sysconf(_SC_PAGE_SIZE);
    if (pagesize == -1)
        handle_error("sysconf");

    //Allocate aligned memory
    buffer = memalign(pagesize, 4 * pagesize);
    if (buffer == NULL)
        handle_error("memalign");

    printf("Start of region:        0x%lx\n", (long) buffer);

    //Control memory area permissions
    if (mprotect(buffer + pagesize * 2, pagesize, PROT_READ) == -1)
        handle_error("mprotect");

    for (p = buffer ; ; )
        *(p++) = 'a';

    printf("Loop completed\n");     /* Should never happen */
    exit(EXIT_SUCCESS);
}

Recommended Posts

A note about mprotect (2)
A note about __call__
A note about subprocess
A note about KornShell (ksh)
A note about TensorFlow Introduction
A note about [python] __debug__
Python: A Note About Classes 1 "Abstract"
A note about get_scorer in sklearn
A note about mock (Python mock library)
Note about awk
Just a note
A note about doing the Pyramid tutorial
Note about pointers (Go)
A memorandum about Nan.
Data analysis in Python: A note about line_profiler
A note about the new style base class
A note about checking modifiers with Max Plus
A memorandum about correlation [Python]
A memorandum about Python mock
A little more about FIFO
A small note following printf
Note
Python Note: About comparison using is
A refreshing story about Python's Slice
A sloppy story about Python's Slice
I have a question about whitespace
A small sample note of list_head
Note
A note for writing Python-like code
A note that prints numpy.array nicely
A story about using Python's reduce
[Translation] A note about structured concurrency .. or rather go statements seem harmful
A note about the functions of the Linux standard library that handles time
A note where a Python beginner got stuck
[Note] Read a file from another directory
A story about remodeling Lubuntu into a Chromebook
I touched "Orator" so I made a note
A Java programmer studied Python. (About type)
A story about machine learning with Kyasuket
A memorandum of understanding about django's QueryDict
A note on enabling PostgreSQL with Django
A story about Python pop and append
Memo about Sphinx Part 1 (Creating a project)
A story about a 503 error on Heroku open
About February 02, 2020 * This is a Python article.
[Note] A story about not being able to break through a proxy with pip