sig sev trouble

Im trying to write a program where im supposed to register a sigsev handler where i get a segmentation fault because of an illegal attempt to wrye to a piece of memory that is read only. when the handler is triggered it will alter the memory protection for the page you tried to wirte to to make it readable and writeable.

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

#define MSPACE 256

int page_size;
char*mem;

void *aligned_malloc(size_t size,size_t psize){
int align_mask = psize - 1;
char *ptr=(char *)malloc(size + psize);
char *aligned_ptr=ptr + psize - ((size_t)ptr & align_mask);
return aligned_ptr;
}

void handler (int signo, struct siginfo *si)
{
char err = mprotect((void*)mem, page_size,PROT_READ|PROT_WRITE);
}
int main(int argc, char** argv) {
struct sigaction sa;
sa.sa_flags = SA_SIGSEV;
sa.sa_sigaction = handler;



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

printf("begin allocating\n");
mem = (char *)aligned_malloc(256,4096);
char err = mprotect((void*)mem, page_size,PROT_READ);
if (err=-1) sigaction(handler);
if (err<0) perror("mprotect");
printf("Done allocating mem0=%lx err=%d\n",mem,err);

printf("reading mem[0]%d\n",mem[0]);
printf("writing to mem[0]:\n");
mem[0]=20;
printf("mem[0] is now %d\n", mem[0]);
printf("All done!");
}

Im not even close. Any help is appreciated
Ok, this must be for an operating systems class, I take it?

Your SIGSEGV handler has to look inside the siginfo struct to find the address of the fault, then a) determine the base address of the page on which the write was attempted, and b) call mprotect() to change its permissions to read/write (as you are doing).

if( err=-1 ) is an assignment, not a comparison.
Also, mprotect returns int, not char.

ok,i think im getting closer.

this is my current code:
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#define MSPACE 256

int page_size;
char*mem;

void *aligned_malloc(size_t size,size_t psize){
int align_mask = psize - 1;
char *ptr=(char *)malloc(size + psize);
char *aligned_ptr=ptr + psize - ((size_t)ptr & align_mask);
return aligned_ptr;
}

void handler (int signo, struct siginfo *si)
{
int err = mprotect((void*)mem, page_size,PROT_READ|PROT_WRITE);
}
int main(int argc, char** argv) {
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
sigaction(SIGSEGV, &sa, NULL)



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

printf("begin allocating\n");
mem = (char *)aligned_malloc(256,4096);
char err = mprotect((void*)mem, page_size,PROT_READ);
if (err=-1) sigaction(SIGSEGV, &sa, NULL));
if (err<0) perror("mprotect");
printf("Done allocating mem0=%lx err=%d\n",mem,err);

printf("reading mem[0]%d\n",mem[0]);
printf("writing to mem[0]:\n");
mem[0]=20;
printf("mem[0] is now %d\n", mem[0]);
printf("All done!");
}

But now im getting these errors:
memthor.c: In function 'main':
memthor.c:25: warning: assignment from incompatible pointer type
memthor.c:30: error: expected ';' before 'page_size'
memthor.c:36: error: expected ';' before ')' token
memthor.c:36: error: expected statement before ')' token
memthor.c:45:2: warning: no newline at end of file

am i actually getting the code right and can someone help me with these batch of errors?
Dude, please post your code with appropriate tags, it is just so annoying to read the code without it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#define MSPACE 256

int page_size;
char*mem;

void *aligned_malloc(size_t size,size_t psize){
int align_mask = psize - 1;
char *ptr=(char *)malloc(size + psize);
char *aligned_ptr=ptr + psize - ((size_t)ptr & align_mask);
return aligned_ptr;
}

void handler (int signo, struct siginfo *si)
{
int err = mprotect((void*)mem, page_size,PROT_READ|PROT_WRITE);
}
int main(int argc, char** argv) {
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
sigaction(SIGSEGV, &sa, NULL)



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

printf("begin allocating\n");
mem = (char *)aligned_malloc(256,4096);
char err = mprotect((void*)mem, page_size,PROT_READ);
if (err=-1) sigaction(SIGSEGV, &sa, NULL));
if (err<0) perror("mprotect");
printf("Done allocating mem0=%lx err=%d\n",mem,err);

printf("reading mem[0]%d\n",mem[0]);
printf("writing to mem[0]:\n");
mem[0]=20;
printf("mem[0] is now %d\n", mem[0]);
printf("All done!");
}
 
Now about your problem, your errors are clearly saying what you need to do. So please from next time atleast give it a shot. Anyways, here is the updated version of your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
this is my current code:
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#define MSPACE 256

int page_size;
char*mem;

void *aligned_malloc(size_t size,size_t psize){
int align_mask = psize - 1;
char *ptr=(char *)malloc(size + psize);
char *aligned_ptr=ptr + psize - ((size_t)ptr & align_mask);
return aligned_ptr;
}

void handler (int signo, struct siginfo *si)
{
int err = mprotect((void*)mem, page_size,PROT_READ|PROT_WRITE);
}
int main(int argc, char** argv) {
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
sigaction(SIGSEGV, &sa, NULL);



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

printf("begin allocating\n");
mem = (char *)aligned_malloc(256,4096);
char err = mprotect((void*)mem, page_size,PROT_READ);
if (err == -1) sigaction(SIGSEGV, &sa, NULL);
if (err<0) perror("mprotect");
printf("Done allocating mem0=%lx err=%d\n",mem,err);

printf("reading mem[0]%d\n",mem[0]);
printf("writing to mem[0]:\n");
mem[0]=20;
printf("mem[0] is now %d\n", mem[0]);
printf("All done!");
}


Based on your posted errors it should work now. And other than that, if you notice then in the line 36 of your code, you have written if (err = -1) I am pretty sure it should be
if (err == -1) Remember = is for assignment and == is for comparison. ( I have updated it to == in the code above.

Hope this helps !
Topic archived. No new replies allowed.