Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why mprotect next page memory in function unprotect_address? #2

Open
yeyuzhen opened this issue Jul 6, 2018 · 1 comment
Open

Comments

@yeyuzhen
Copy link

yeyuzhen commented Jul 6, 2018

#include <stdio.h>
#include "ExtremeCMock.h"

static int test() {
	int i = 10;
	return i;
}

int test_mock() {
	return (100);
}

int main(void) {
	MOCK_FUNC(test, test_mock);
	printf("test result is %d\n", test());
	unmock_all();
	printf("test result is %d\n", test());
	return 0;
}

I use ExtremeCMock to mock self static function,console show "could not set protection on next page: Cannot allocate memory".I found the reason is function unprotect_address try to mprotect next page memory of real function. What is the purpose to mprotect next page memory?

static void unprotect_address(mocked_function_t * functionp) {
	char *p;
	long psize = sysconf(_SC_PAGESIZE);
	for (p = (char *)functionp->addr; (unsigned long)p % psize; --p)
		;
	if (-1 == mprotect(p,128, PROT_WRITE|PROT_READ|PROT_EXEC)){ 
		perror("could not set protection");
		exit(-1);
	}
	if (-1 == mprotect(p+psize,128, PROT_WRITE|PROT_READ|PROT_EXEC)){  // why unprotect next page ?
		perror("could not set protection on next page");
		exit(-1);
	}
	my_memcpy(functionp->backup_function_data,functionp->addr,STUB_SIZE);
}
@acgreek
Copy link
Owner

acgreek commented Nov 15, 2019

Sorry, it's been a while since I looked at this and didn't realize I had an issue filed. In hi_jack_function, we overwrite the machine code at my_memcpy(functionp->addr, hijack_stub, sizeof(hijack_stub));. I unprotecting the next page just in case that over-write crosses into the next page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants