Search code examples
cmemorybus-error

Bus error: 10 when scanning address space in c


I am trying to scan the address space to find my chunks of memory that have read/write permissions. It is acceptable to check a single address per page as each page have the same permissions. I know I should be getting Segmentation Fault: 11 when trying to write to a piece of memory I shouldn't be able to. This happens when I am trying to access higher addresses but when I am in the lower portion, say 0x00000100, I get the Bus error: 10.

NOTE: The code is compiled with the -m32 flag so it simulates a 32 bit machine.

ALSO NOTE: The memory for chunk_list has already been malloc'ed before this function is called.

I have copied the code below:

#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include "memchunk.h" 


int get_mem_layout (struct memchunk *chunk_list, int size)
{
//grab the page size
long page = sysconf(_SC_PAGESIZE);
printf("The page size for this system is %ld bytes\n", page);

//page size is 4069 bytes

//test printing the number of words on a page
long words = page / 4;  
printf("Which works out to %ld words per page\n", words);

//works out to 1024 words a page
//1024 = 0x400

//create the addy pointer
    //start will be used after bus error: 10 is solved
void *start;
char * currAddy;
currAddy = (char*)0x01000000;

//someplace to store the addy to write to
//char * testWrite;


//looping through the first size pages
int i;
for(i = 0; i < size; i++){

    //chunk start - wrong addy being written just testing
    chunk_list[i].start = currAddy;
    printf("addy is %p\n",currAddy);
    sleep(1);

    //try and write to the current addy
    //testWrite = currAddy;
    //*testWrite = 'a';

    *currAddy = '1';


    //+= 0x400 to get to next page
    currAddy += 0x400;
}


//while loop here for all the addys - not there yet because still dealing with bus error: 10

return 0;


}

Any help would be greatly appreciated. I also left some other attempts at it commented out in the code, still all produce a bus error: 10 in the lower portion of the memory space.

EDIT: I will be dealing with seg faults using signals. I know how to deal with the seg fault, so is there a way to handle a bus error: 10 using signals as well?


Solution

  • Reading from or writing to unmapped memory is supposed to cause a bus fault. To discover whether a memory is there, install a handler for SEGFAULTs to react accordingly.

    In a Linux SE (Security Enhanced) process, the program sections are loaded at randomized locations to frustrate viruses being able to rely on stable addresses.

    In most virtual memory systems, a non-mapped space is usually left from address zero up a ways so attempts to dereference a NULL pointer or a structure based on a NULL pointer cause an exception. In the 1980s, the blank space was often 64K to 256K. On modern architectures, 16M is a reasonable choice to detect NULL-based accesses.

    On many virtual memory systems, there is a system call to obtain per process mapped memory locations. On Linux, inspect the contents of /proc/self/maps.