-
-
Notifications
You must be signed in to change notification settings - Fork 423
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
Handle detour patches across page boundaries #1535
Conversation
On Linux if a detour crossed a page boundary we would only change the memory protection of the first page (as we were aligning the address as required, but not taking into account the length). I don't have an easy way to test this but it looks correct. `addr + len` doesn't appear to need to be aligned though, so another option could be to use `(addr - startPage) + length` as len. Also fixed a non-zero offset being passed into CDetour's ApplyPatch function - this is never done internally anywhere, but it doesn't hurt to fix it. Fixes #984
public/CDetour/detourhelpers.h
Outdated
mprotect(addr2, sysconf(_SC_PAGESIZE), prot); | ||
long pageSize = sysconf(_SC_PAGESIZE); | ||
void *startPage = ke::AlignedBase(addr, pageSize); | ||
void *endPage = ke::AlignedBase((intptr_t)addr + length, pageSize); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be addr + length - 1 if the sequence runs right up to the page boundary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did thing that initially but I think this is correct, if you imagine a zero-length sequence doing -1 would take us one page back, and a sequence equal to the length of a page seems like it'll do the right thing here as well (endPage would be the same as startPage, but one more byte would take it to the next page).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
startPage = AlignedBase(0, 4096) = 0
endPage = AlignedBase(0 + 4096, 4096) = 4096
len = endPage - startPage + pageSize = 4096 - 0 + 4096 = 8192
So I think you'd wind up with two pages? That said, it's pretty edge casey and doesn't really matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes, that does make sense! Yeah, I think I'd prefer going over in that edge case than trying to special-case zero length.
The code here can only use a maximum length of 20 currently :D
public/CDetour/detourhelpers.h
Outdated
mprotect(addr2, sysconf(_SC_PAGESIZE), prot); | ||
long pageSize = sysconf(_SC_PAGESIZE); | ||
void *startPage = ke::AlignedBase(addr, pageSize); | ||
void *endPage = ke::AlignedBase((intptr_t)addr + length, pageSize); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
startPage = AlignedBase(0, 4096) = 0
endPage = AlignedBase(0 + 4096, 4096) = 4096
len = endPage - startPage + pageSize = 4096 - 0 + 4096 = 8192
So I think you'd wind up with two pages? That said, it's pretty edge casey and doesn't really matter.
On Linux if a detour crossed a page boundary we would only change the
memory protection of the first page (as we were aligning the address as
required, but not taking into account the length).
I don't have an easy way to test this but it looks correct.
addr + len
doesn't appear to need to be aligned though, so another option could be
to use
(addr - startPage) + length
as len.Also fixed a non-zero offset being passed into CDetour's ApplyPatch
function - this is never done internally anywhere, but it doesn't hurt
to fix it.
Fixes #984