Skip to content

Handle detour patches across page boundaries#1535

Merged
asherkin merged 3 commits intomasterfrom
bug-984
Jul 17, 2021
Merged

Handle detour patches across page boundaries#1535
asherkin merged 3 commits intomasterfrom
bug-984

Conversation

@asherkin
Copy link
Member

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

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
@asherkin asherkin requested a review from dvander July 17, 2021 17:53
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);
Copy link
Member

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?

Copy link
Member Author

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).

Copy link
Member

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.

Copy link
Member Author

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

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);
Copy link
Member

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.

@asherkin asherkin merged commit 5b7c9c5 into master Jul 17, 2021
@asherkin asherkin deleted the bug-984 branch July 17, 2021 19:53
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

Successfully merging this pull request may close these issues.

ProtectMemory called with wrong address in public/CDetour/detourhelpers.h

2 participants