linux - What does the usage of mprotect() as an ASM syscall look like with respect to its third argument? -
in i386 architecture linux know can build syscall loading identity of syscall eax , arguments ebx, ecx, etc.
i confused third argument mprotect in case; assuming wanted make memory segment within binary's allocated memory executable, how encoding work prot_exec argument (arg 3)? i'm aware first 2 arguments (1) pointer start of allocated block, , (2) length of allocated block relatively easy conceptualize in asm (as hexadecimal addresses in memory).
how format third argument mprotect() interrupt-issued syscall in i386 assembly on linux?
thanks.
tl;dr: an integer passed third parameter.
now let's answer question in comments. if open mman-common.h, should sit in /usr/include/asm-generic, find these values.
#define prot_read 0x1 /* page can read */ #define prot_write 0x2 /* page can written */ #define prot_exec 0x4 /* page can executed */ #define prot_sem 0x8 /* page may used atomic ops */
just before compiling, preprocessor replaces parameters numbers above. if had call:
mprotect(myaddress, 256, prot_read | prot_write);
it replaced code:
mprotect(myaddress, 256, 0x1 | 0x2);
now @ values different parameters can take: haven’t been chosen randomly, powers of two, in binary notation represented 1 one digit , zeros.
prot_read = 0x1 = 00000001 prot_write = 0x2 = 00000010 prot_exec = 0x4 = 00000100
choosing powers of 2 handy because when use binary or, number obtain combine 2 previous values, both information contained or-ed number.
prot_write | prot_exec = 00000010 | 00000100 = 00000110
so our call:
if had called mprotect(myaddress, 256, prot_read | prot_write)
, have happened prot_read | prot_write
have been combined 0x1 | 0x2
, 0x3
.
now on kernel side, suppose prot_read | prot_write
written user. kernel receives argument 0x3
, , wants check whether prot_read written. way write this:
if (prot_read & uservalue) { }
it works because uservalue contains combined version of prot_read , prot_write, in binary:
prot_read & uservalue = 00000001 && 00000011 = 00000001
if flag set, number non-zero, kernel knows flagged passed.
hope helps.
Comments
Post a Comment