Micrified
Micrified

Reputation: 3650

Anomalous Bus Error for accessing protected memory on macOS?

Context

I've written a small program with the intention of using it to generate a SIGSEGV signal, which I'll catch with a handler and then print out.

Problem

The program I wrote works fine (or so it seems) on Linux. However, when compiled and executed on macOS 10.11.6, it fails to generate a SIGSEGV, but instead generates a Bus error: 10.

Solutions Attempted

When researching whether or not others had encountered this error, I came across the following SO Post:

https://stackoverflow.com/a/18829327/1883304

Sadly, while I did switch from using posix_memalign to mmap, I am still getting a bus error. I'm unable to find related questions that can help besides this one.

Program

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

/*
 *******************************************************************************
 *                              Global Variables                               *
 *******************************************************************************
*/

// Page size.
int pagesize;

// Pointer to allocated memory page. 
void *page;

/*
 *******************************************************************************
 *                               Signal Handlers                               *
 *******************************************************************************
*/

// Handler: Invalid memory reference.
void handler_sigseg (int sig, siginfo_t *si, void *ignore) {

    // Mask SIGSEGV.
    if (sig != SIGSEGV) {
        return;
    }

    // Output siginfo fields.
    printf("si_signo = %d\n", si->si_signo);
    printf("si_code = %d\n", si->si_code);
    printf("si_value.sival_int = %d\n", si->si_value.sival_int);
    printf("si_errno = %d\n", si->si_errno);
    printf("si_pid = %d\n", si->si_pid);
    printf("si_uid = %d\n", si->si_uid);
    printf("si_statis = %d\n", si->si_status);

    // Grant writing protection.
    if (mprotect(page, pagesize, PROT_WRITE) == -1) {
        fprintf(stderr, "Error: Attempt to set memory access perms failed!\n");
        exit(EXIT_FAILURE);
    }

    return;
} 


/*
 *******************************************************************************
 *                                Main Routine                                 *
 *******************************************************************************
*/

int main (void) {

    /*
     ***************************************************************************
     *                              Memory Setup                               *
     ***************************************************************************
    */

    // Set page size.  
    pagesize = sysconf(_SC_PAGE_SIZE);

    printf("pagesize = %d\n", pagesize);

    // Allocate memory page, created at nearby page boundary. 
    if ((page = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
        fprintf(stderr, "Error: mmap allocation failed!\n");
        exit(EXIT_FAILURE);
    }

    // Zero out memory block. 
    memset(page, 0, pagesize);

    // Set protections.
    if (mprotect(page, pagesize, PROT_READ) == -1) {
        fprintf(stderr, "Error: Attempt to set memory access perms failed!\n");
        exit(EXIT_FAILURE);
    }

    /*
     ***************************************************************************
     *                              Signal Setup                               *
     ***************************************************************************
    */

    // Setup signal handler.
    struct sigaction segHandler;

    // Configure args.
    segHandler.sa_flags = SA_SIGINFO;

    // Reset signal-mask.
    sigemptyset(&segHandler.sa_mask);

    // Setup handler routine.
    segHandler.sa_sigaction = handler_sigseg;

    // Setup signals to be intercepted.
    sigaction(SIGSEGV, &segHandler, NULL);


    // Test the signal handler.
    printf("Poking protected memory...\n");
    char *p = (char *)page;
    *p = 0;

    return 0;
}

So in conclusion, could someone please inform me on the latest proper way to reserve memory such that I may raise a SIGSEGV on macOS?

Upvotes: 1

Views: 289

Answers (0)

Related Questions