-
-
Notifications
You must be signed in to change notification settings - Fork 756
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
x86-64: Preserve RCX and R11 when calling mprotect_asm (syscall) #673
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Test Output (Linux x86_64)
|
Very good catch, I'll make sure we merge this quick |
Grazfather
reviewed
Jul 6, 2021
On x86-64, the syscall instruction will clobber RCX and R11. It uses RCX to save the return address and uses R11 to save the RFLAGS. This means we will lose our old RCX and R11 value after the syscall returns to userspace. Bug reproduction steps (see how RCX and R11 change): $ gdb any-program # Run it inside the GDB starti # Set RCX and R11 before set-permission p $rcx=0 p $r11=0 # Do mprotect syscall set-permission $sp # Now the value of RCX and R11 have changed p $rcx p $r11 This commit fixes the bug by preserving RCX and R11. Signed-off-by: Ammar Faizi <[email protected]>
Thanks for this! Good find. |
5 tasks
ammarfaizi2
added a commit
to ammarfaizi2/gef
that referenced
this pull request
Jul 7, 2021
This commit introduces 2 changes: 1) Change "info registers" to "info registers all". This will track more registers and make sure they are not changed due to syscall. 2) Change `gdb_start_silent_cmd` to `gdb_run_cmd`. We don't need to use `gdb_start_silent_cmd` because our `before` commands have already started the process. And we can't see the register before we do `set-permission` command if the process has not been started yet. Therefore, it makes sense not to append `"entry-break"` (calling `gdb_start_silent_cmd`). Before this commit the result in commands will be like this: before = [ # These two do the entry-break job! "starti", "si", "printf \"match_before\\n\"", "info registers all", "printf \"match_before\\n\"", "gef config context.clear_screen False", "gef config context.layout '-code -stack'", # This is unnecessary, because we have `starti` and `si`. # We can't reorder it because it is appended inside the # `gdb_start_silent_cmd`. "entry-break" ] cmd = "set-permission $sp" after = [ "printf \"match_after\\n\"", "info registers all", "printf \"match_after\\n\"" ] After this commit: before = [ "entry-break", "printf \"match_before\\n\"", "info registers all", "printf \"match_before\\n\"" ] cmd = "set-permission $sp" after = [ "printf \"match_after\\n\"", "info registers all", "printf \"match_after\\n\"" ] Link: hugsy#673 (comment) Fixes: 5eb3b24 ("x86-64: Preserve RCX and R11 when calling mprotect_asm (syscall)") Cc: Grazfather <[email protected]> Signed-off-by: Ammar Faizi <[email protected]>
ammarfaizi2
added a commit
to ammarfaizi2/gef
that referenced
this pull request
Jul 7, 2021
This commit introduces 2 changes: 1) Change "info registers" to "info registers all". This will track more registers and make sure they are not changed due to syscall. 2) Change `gdb_start_silent_cmd` to `gdb_run_cmd`. We don't need to use `gdb_start_silent_cmd` because our `before` commands have already started the process. And we can't see the register before we do `set-permission` command if the process has not been started yet. Therefore, it makes sense not to append `"entry-break"` (calling `gdb_start_silent_cmd`). Before this commit the result in commands will be like this: before = [ # These two do the entry-break job! "starti", "si", "printf \"match_before\\n\"", "info registers all", "printf \"match_before\\n\"", "gef config context.clear_screen False", "gef config context.layout '-code -stack'", # This is unnecessary, because we have `starti` and `si`. # We can't reorder it because it is appended inside the # `gdb_start_silent_cmd`. "entry-break" ] cmd = "set-permission $sp" after = [ "printf \"match_after\\n\"", "info registers all", "printf \"match_after\\n\"" ] After this commit: before = [ "entry-break", "printf \"match_before\\n\"", "info registers all", "printf \"match_before\\n\"" ] cmd = "set-permission $sp" after = [ "printf \"match_after\\n\"", "info registers all", "printf \"match_after\\n\"" ] Link: hugsy#673 (comment) Fixes: 5eb3b24 ("x86-64: Preserve RCX and R11 when calling mprotect_asm (syscall)") Cc: Grazfather <[email protected]> Signed-off-by: Ammar Faizi <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
x86-64: Preserve RCX and R11 when calling mprotect_asm (syscall)
How Has This Been Tested?
make tests
Checklist
dev
branch, notmaster
.