Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
144 commits
Select commit Hold shift + click to select a range
b46d41c
major changes due to difference between RISC-V and x64. Updated versi…
Apr 30, 2024
402a337
Trampoline doesn't prematurely clobber used registers anymore
May 1, 2024
3efb0e3
sp value changes now keep it 16-byte boundary aligned
May 1, 2024
ce40f2f
Now instruction following ecall must not use ra as operand to be able…
May 2, 2024
0a206ab
context and its references are now coherent with RISC-V and stack lay…
May 2, 2024
1ca3f27
Assembly files now follow the correct logic. Clone should now be hand…
May 2, 2024
d1cb7a8
pattern1 work in progress
Jun 5, 2024
07af378
code cleanup
Jun 11, 2024
b5499cc
pointer small fix
Jun 11, 2024
8b4ce8d
small pointer fix and added prototype of a function
Jun 12, 2024
d6572cc
small fix, code cleanup and disabled CMake PERFORM_STYLE_CHECKS and T…
Jun 12, 2024
463eafa
Created one more simple test
Jun 12, 2024
8390aad
deleted SYS_vfork references and substituted SYS_open with SYS_openat
Jun 12, 2024
165b36d
Correctly patching 490 out of 492 ecall in libc. To do: debug return …
Sep 16, 2024
db7244f
ECALL can now be successfully patched even with just 6 overwritable b…
Oct 1, 2024
3164838
added simple openat interception test
Oct 4, 2024
dc0bde7
Commented out useless debug_dump calls
Oct 10, 2024
fc2c796
changed two flags and corrected link_directories
Oct 10, 2024
eed7bdd
added trampoline creation in 6 byte patch case
Oct 10, 2024
8a51ff9
updated .gitignore and one assembly test
Oct 24, 2024
2db363e
test/CMakeLists.txt test push
Oct 24, 2024
a864c32
restored original version of test/CMakeLists.txt
Oct 24, 2024
76e7e5c
Fixed relocation bug and fixed bug for leaves procedure; now returni…
SilenceDesigner Dec 30, 2024
dbb4538
added some tests
SilenceDesigner Dec 30, 2024
0f6f411
some more tests
SilenceDesigner Dec 30, 2024
2f88c94
corrected some more tests
SilenceDesigner Dec 30, 2024
93ec711
example.c test corrected
SilenceDesigner Dec 30, 2024
8962c5c
c.nop placement bug solved
SilenceDesigner Jan 3, 2025
3e7d0e5
Cleaning arch-specific dependencies
GlassOfWhiskey Jan 4, 2025
2fa201b
temporary registers are now saved and restored as expected by libc
SilenceDesigner Jan 4, 2025
02eb5fb
t5 is now correctly preserved across the interception
SilenceDesigner Jan 9, 2025
951a8d8
Use of in following instruction is no more forbidding, added C.JR ca…
SilenceDesigner Jan 11, 2025
ca2581a
Library is now architecture independent. It works on both x86_64 and …
Feb 3, 2025
2874db5
Solved some warnings
Feb 3, 2025
971ab4b
Solved last warning
Feb 3, 2025
a8e6c3b
Corrected comment to RISC-V specific files
Feb 4, 2025
3a9f3d7
Made architecture indepentent
Feb 5, 2025
279be19
Added missing syscall formats
Feb 6, 2025
808c090
Fixed and enabled perl coding style check
Feb 6, 2025
9b0ae57
Removed unused flag
Feb 6, 2025
f220d8e
Disabling perl style check again
Feb 6, 2025
db3308f
Commented lines cleanup
Feb 6, 2025
eb98da3
Removed unused patcher.c from arch-independent directory
Feb 6, 2025
70f43e6
Started writing arch-specific READMEs
Feb 6, 2025
b8e9877
Updated READMEs and CMakeLists.txt builds test and examples just if a…
Feb 7, 2025
9314b64
Restored original x64 test
Feb 7, 2025
71b38aa
Updated READMEs
SilenceDesigner Feb 12, 2025
1f3f035
Corrected copyright headers
SilenceDesigner Feb 12, 2025
f9cee3f
Removed executable in test
SilenceDesigner Feb 12, 2025
384830a
Updated REAME.md
SilenceDesigner Feb 12, 2025
4a97559
Restored vfork handling in intercept_routine
Feb 14, 2025
d3a00ba
Corrected copyright notes
Feb 21, 2025
0ff4b72
Cleaned up commented lines, placed TRAMPOLINE_SIZE definition here back
Feb 21, 2025
519c6fb
Smarter definition of macros and boolean flags depending on the targe…
Feb 21, 2025
6007fe6
Refined arch-dependant definition
Feb 21, 2025
2ec36ea
Refined arch-dependant definition
Feb 21, 2025
cce5d53
Cleaned up unnecessary inclusions and fixed copyright notes
Feb 21, 2025
a1da724
Updated team roles
Feb 21, 2025
e1186a1
SARGS definition are now dependant on SYS_* macro definition instead …
Feb 22, 2025
367e7df
Added simple brk test
Feb 24, 2025
1319a0f
Added statx test
Feb 25, 2025
64eac89
Made write and openat tests assertive
Feb 26, 2025
97670a8
Corrected openat_test
Feb 26, 2025
a795f91
Fixed clone() handling
Feb 28, 2025
311633c
Added clone test
Feb 28, 2025
4c73235
Comment, formatting
Feb 28, 2025
9100592
modified .gitignore
Feb 28, 2025
163a11b
Created a temporary folder for RISC-V testing
SilenceDesigner Mar 4, 2025
5f55f0c
Added Makefile
SilenceDesigner Mar 4, 2025
e747d2d
All tests are updated
SilenceDesigner Mar 4, 2025
66eddc8
Reformatting
SilenceDesigner Mar 4, 2025
58500df
Switched to mkdirat since mkdir is not defined on RISC-V
SilenceDesigner Mar 4, 2025
64423d9
Reinforced clone test
SilenceDesigner Mar 4, 2025
7249ac2
Corrected relative paths
SilenceDesigner Mar 5, 2025
f411c56
Corrected clone test
SilenceDesigner Mar 5, 2025
aca8415
Updated READMEs
Mar 6, 2025
66554ab
Solved relative inclusions
Mar 6, 2025
5e5434b
Comments cleanup
Mar 6, 2025
fe44157
Ditched clone test as clone handling is WIP
Mar 6, 2025
3ef8a80
Corrected one instruction
Mar 6, 2025
74b0471
Updated test/riscv/Makefile
SilenceDesigner Apr 24, 2025
d569ae8
Clone handling with NULL arg1
May 2, 2025
890c794
Fixed unhandled ecall case context restoring
SilenceDesigner May 8, 2025
95232d2
Refined clone_test
SilenceDesigner May 8, 2025
ebfaac8
Added fork test
May 12, 2025
13be19c
Capstone 5.0.6 is now automatically fetched and compiled
May 21, 2025
3e8695c
fix: linking x64 tests and capstone
May 21, 2025
bf3a719
update: cmake minimum required version is now 3.10
SilenceDesigner Jun 24, 2025
623aa84
Bump capstone project came_minimum_required
marcoSanti Jun 24, 2025
30be91c
update: post_clone for stack space sharing clone is called from norma…
SilenceDesigner Jul 2, 2025
c2dee0f
update: capstone compilation, clone full support
Jul 4, 2025
91b673d
chore: syntax correction
Jul 4, 2025
e0fdaac
calling same post_clone routine both from C routine and ASM wrapper
Jul 4, 2025
61004fe
code cleanup
Jul 7, 2025
91d9d52
solving minor issues with pmem tests
SilenceDesigner Jul 8, 2025
aac467f
updated original pmem x64 tests to work with newer glibc versions
Jul 10, 2025
f63d4c3
updated CI pipeline to run on Ubuntu 22
Jul 10, 2025
0436c23
fine-tuned fstat logging
SilenceDesigner Jul 11, 2025
218287c
tailored tests to work on different glibc versions
SilenceDesigner Jul 11, 2025
4495eae
rearranged riscv tests in dedicated directory
SilenceDesigner Jul 12, 2025
339e822
updated riscv test directory
SilenceDesigner Jul 12, 2025
4744756
test list order is now consistent
SilenceDesigner Jul 12, 2025
06876b0
Merge branch 'master' into riscv-dev
SilenceDesigner Jul 12, 2025
d1bfc3d
refined trampoline allocation
SilenceDesigner Jul 12, 2025
bfae1e3
Update README.md
SilenceDesigner Jul 15, 2025
e94f6c8
Update README.md
SilenceDesigner Jul 15, 2025
66d954f
Specified referenced capstone version
SilenceDesigner Jul 15, 2025
d5d5bd6
rearranged arch-specific tests
SilenceDesigner Jul 15, 2025
96d012b
Now building tests on both x86_64 and riscv
SilenceDesigner Jul 15, 2025
3dd50f0
optional inclusion of arch_prctl
SilenceDesigner Jul 15, 2025
dbe5686
optional inclusion of sys/io.h
SilenceDesigner Jul 15, 2025
9750686
optional inclusion of system calls not defined on RISCV
SilenceDesigner Jul 15, 2025
4f99e4f
moved vfork test to x86_64 tests directory
SilenceDesigner Jul 15, 2025
63d7d67
Test suite is built with cmake on both x86_64 and RISC-V
SilenceDesigner Jul 15, 2025
7f32061
solved unused variables warnings
SilenceDesigner Jul 15, 2025
ed4bd5e
added -Wno-unused-result for tests
SilenceDesigner Jul 15, 2025
ff4fe29
added -Wno-unused-variable for test
SilenceDesigner Jul 15, 2025
e4a1321
fine-tuning for cmake-independent execution
SilenceDesigner Jul 15, 2025
185d3f7
excluded generic test generated files and directories
SilenceDesigner Jul 15, 2025
f38bb29
Updated tests description
Jul 16, 2025
732fc31
removed lines added by github merge
Jul 17, 2025
fb6c8fd
poll and ppoll test logging fine-tuning
Jul 18, 2025
8e54554
faccessat test logging fixed
Jul 18, 2025
107221f
dup2/dup3 logging test discrepancy solved
Jul 18, 2025
eb3713c
replaced token with where just letters can be present
Jul 18, 2025
77fd75a
revert token substitution
Jul 18, 2025
489606b
Created different log match files for each arch
Jul 18, 2025
10755aa
Fine-tuned riscv syscall format logging match file
Jul 18, 2025
51e070c
create_module format declaration is now optional
Jul 18, 2025
cafd156
typo fix
Jul 19, 2025
15a38d8
fixed accpt4 expetations
Jul 19, 2025
8bcc45e
fixed accept4 expectations
Jul 19, 2025
3a369aa
typo fix
Jul 19, 2025
5ac4f55
typo fix
Jul 19, 2025
c1f160c
Full test suite passed with glibc 2.39 on x64
SilenceDesigner Jul 20, 2025
df43ff0
Updated riscv syscall_format_log match file
SilenceDesigner Jul 20, 2025
ea36858
typo fix
SilenceDesigner Jul 20, 2025
962291d
Fixed semctl args expectations
SilenceDesigner Jul 20, 2025
d5d316b
typo fix
SilenceDesigner Jul 20, 2025
170fd6c
fixed renameat2 log expectations
SilenceDesigner Jul 20, 2025
09c0637
fixed renameat2 log expectations
SilenceDesigner Jul 20, 2025
132ae5d
added delete_module and quotactl to log match file
SilenceDesigner Jul 20, 2025
cfece63
fixed epoll_create log expectation
SilenceDesigner Jul 20, 2025
9271486
fixed inotify_init log expectation
SilenceDesigner Jul 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
# The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
# You can convert this to a matrix build if you need cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
runs-on: [ubuntu-20.04]
runs-on: [ubuntu-24.04]

steps:
- uses: actions/checkout@v3
Expand Down
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ build/
.idea/
cmake-build-debug/
test/*.so
test/riscv/bin/
test/riscv/build/
test/riscv/*.txt
test/bin/
test/build/
test/testdir/
test/testfile.txt
test/testfile2.txt
36 changes: 28 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cmake_minimum_required(VERSION 3.3)
cmake_minimum_required(VERSION 3.10)
project(syscall_intercept C ASM)

set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
Expand All @@ -43,11 +43,10 @@ endif()

option(PERFORM_STYLE_CHECKS
"check coding style, license headers (requires perl)" OFF)
option(BUILD_TESTS "build and enable tests" ON)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86(_64)?)$")
option(BUILD_TESTS "build and enable tests" ON)
option(BUILD_EXAMPLES "build examples" ON)
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(riscv(32|64))$")
option(BUILD_TESTS "build and enable tests" OFF)
option(BUILD_EXAMPLES "build examples" OFF)
endif()
option(TREAT_WARNINGS_AS_ERRORS
Expand All @@ -56,6 +55,28 @@ option(EXPECT_SPURIOUS_SYSCALLS
"account for some unexpected syscalls in tests - enable while using sanitizers, gcov" OFF)
option(STATIC_CAPSTONE "statically link libcapstone into the shared library" OFF)

#####################################
# External projects
#####################################
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/capstone)

set(CAPSTONE_INCLUDE_FOLDER "${CMAKE_CURRENT_BINARY_DIR}/capstone/_deps/capstone-src/include/capstone")
set(CAPSTONE_LIB_FOLDER "${CMAKE_CURRENT_BINARY_DIR}/capstone/_deps/capstone-build")

execute_process(
COMMAND ${CMAKE_COMMAND}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DBUILD_SHARED_LIBS=ON
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
"${CMAKE_CURRENT_SOURCE_DIR}/capstone"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/capstone"
)

execute_process(COMMAND make
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/capstone"
)

find_program(CTAGS ctags)
if(CTAGS)
option(AUTO_RUN_CTAGS "create tags file every on every rebuild" ON)
Expand All @@ -68,7 +89,7 @@ set(SYSCALL_INTERCEPT_VERSION_PATCH 0)
set(SYSCALL_INTERCEPT_VERSION
${SYSCALL_INTERCEPT_VERSION_MAJOR}.${SYSCALL_INTERCEPT_VERSION_MINOR}.${SYSCALL_INTERCEPT_VERSION_PATCH})

include(cmake/find_capstone.cmake)

include(GNUInstallDirs)
include(cmake/toolchain_features.cmake)
include(CheckLanguage)
Expand Down Expand Up @@ -105,10 +126,9 @@ elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(riscv(32|64))$")
)
endif()

# set the following directories according with your installation of capstone
include_directories(include "/usr/local/include/capstone")
include_directories(${PROJECT_SOURCE_DIR}/src)
link_directories("/usr/local/lib")

include_directories(include ${PROJECT_SOURCE_DIR}/src ${CAPSTONE_INCLUDE_FOLDER})
link_directories(${CAPSTONE_LIB_FOLDER})

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

Expand Down
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
# syscall_intercept


[//]: # ([![Build Status]&#40;https://travis-ci.org/pmem/syscall_intercept.svg&#41;]&#40;https://travis-ci.org/pmem/syscall_intercept&#41;)

[//]: # ([![Coverage Status]&#40;https://codecov.io/github/pmem/syscall_intercept/coverage.svg&#41;]&#40;https://codecov.io/gh/pmem/syscall_intercept&#41;)

[//]: # ([![Coverity Scan Build Status]&#40;https://scan.coverity.com/projects/12890/badge.svg&#41;]&#40;https://scan.coverity.com/projects/syscall_intercept&#41;)

This repository contains a multi-architecture porting of [syscall_intercept](https://github.com/pmem/syscall_intercept) working on both x86_64 and RISC-V
This repository contains a multi-architecture porting of [syscall_intercept](https://github.com/pmem/syscall_intercept) working on both **x86_64** and **RISC-V**.
Full-featured support for **aarch64** (ARM64) is in progress.

[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
# Dependencies #


## Runtime dependencies ##

* libcapstone -- the disassembly engine used under the hood
(RISC-V support requires version **5.0** or higher, while apt installs version 4.0.2 by default, as february 2025).
Compiling capstone by hand will require setting [CMakeLists.txt](CMakeLists.txt#L109) accordingly
Since a manual installation of Capstone could not be automatically detected while compiling syscall_intercept, we ensured that libcapstone v5.0.6 is **automatically compiled** from source during the syscall_intercept cmake execution.

## Build dependencies ##

Expand Down Expand Up @@ -56,20 +60,14 @@ There is an install target. For now, all it does, is cp.
make install
```

Testing on **x86_64**:
Running test suite:
```shell
make test
```

Testing on **RISC-V**:\
Go to `syscall_intercept/test/riscv/`, then
```shell
make
make test
```

# Synopsis #


```c
#include <libsyscall_intercept_hook_point.h>
```
Expand Down Expand Up @@ -188,6 +186,7 @@ ls: reading directory '.': Operation not supported

# Under the hood: #


##### Assumptions: #####
In order to handle syscalls in user space, the library relies
on the following assumptions:
Expand Down Expand Up @@ -311,20 +310,20 @@ aa20a: bltu a5,a0,aa262 <__open+0xaa> | aa20a: bltu a5,a0,aa262 <__ope
```

# Limitations: #


* Only Linux is supported
* Only x86\_64 and RISC-V are supported
* Only tested with glibc, although perhaps it works
with some other libc implementations as well
* RISC-V version assumes `$t6` is not used as base pointer or as source
register without being reinitialized after an `ecall` and before the ending of
a function - tested with glibc 2.35, 2.37 and 2.39
* :warning: **Clone** is not fully handled on **RISC-V**. Whereas in x86_64
version it is possible to define post-clone hook functions for both the
parent and child threads, on RISC-V it is only possible to define a pre-clone
hook function therefore providing a similar interception to every other system
calls. An example is present in [intercept_sys_clone.c](test/riscv/src/intercept_sys_clone.c)
a function. While this assumption involves heuristics, this choice was tested
on different glibc version, i.e. 2.35, 2.37 and 2.39, and on different kernel
implementations manifesting a consistent and working behaviour.

# Debugging: #

Besides logging, the most important factor during debugging is to make
sure the system calls in the debugger are not intercepted. To achieve this, use
the INTERCEPT_HOOK_CMDLINE_FILTER variable described above.
Expand All @@ -340,7 +339,8 @@ process itself.

# RISC-V porting #

Ottavio Monticelli <[email protected]> (Maintainer) \

Ottavio Monticelli <[email protected]> (Maintainer) \
Marco Edoardo Santimaria <[email protected]> (Maintainer) \
Marco Aldinucci <[email protected]> (Maintainer and Principal Investigator) \
Iacopo Colonnelli <[email protected]> (Maintainer and Principal Investigator)
15 changes: 15 additions & 0 deletions capstone/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.10)
project(capstone)
include(FetchContent)

#####################################
# Import external project from git
#####################################
FetchContent_Declare(capstone
GIT_REPOSITORY https://github.com/capstone-engine/capstone.git
# the following git tag refers to capstone version 5.0.6
GIT_TAG accf4df62f1fba6f92cae692985d27063552601c
)

FetchContent_MakeAvailable(capstone)

27 changes: 26 additions & 1 deletion src/arch/riscv/intercept_template.S
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ intercept_asm_wrapper_tmpl:
sd t3, 48(sp)
sd t4, 56(sp)
sd t5, 64(sp)
/*
* t5 content will be evaluated in intercept_wrapper to determine which
* interception routine must be called. 1 for post_clone, 0 otherwise
*/
li t5, 0 # choose intercept_routine
j intercept_asm_wrapper_patch_desc_addr
post_clone:
addi sp, sp, -72
addi t6, sp, 72 # sp+72 is the sp value before interception
sd t6, 0(sp)
sd ra, 16(sp)
sd t0, 24(sp)
sd t1, 32(sp)
sd t2, 40(sp)
sd t3, 48(sp)
sd t4, 56(sp)
sd t5, 64(sp)
li t5, 1 # choose intercept_routine_post_clone
intercept_asm_wrapper_patch_desc_addr:
/* load patch_desc address in t0 */

Expand Down Expand Up @@ -135,11 +153,18 @@ intercept_asm_wrapper_wrapper_level1_addr:
* If t6 is zero, a0 contains a syscall number, and that syscall
* is executed here.
* If t6 is 1, a0 contains the return value of the hooked syscall.
* If t6 is 2, a clone syscall is executed here.
*/
beqz t6, execute_unhandled_ecall
srli t6, t6, 1
beqz t6, handled_ecall
ebreak # if t6 holds invalid value, i. e. different from 0 or 1
srli t6, t6, 1
beqz t6, execute_clone
ebreak # if t6 holds invalid value, i. e. different from 0, 1 or 2

execute_clone:
ecall
j post_clone

execute_unhandled_ecall:
ecall
Expand Down
17 changes: 15 additions & 2 deletions src/arch/riscv/intercept_wrapper.S
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,14 @@ intercept_wrapper:
/* argument passed to intercept_routine */
mv a0, sp

jal intercept_routine
li t0, 1
beq t5, t0, l0 # which function should be called?

jal intercept_routine
j l1
l0:
jal intercept_routine_post_clone
l1:
/*
* At this point, the return value of the C
* function (a struct wrapper_ret instance) is in a0, a1.
Expand All @@ -130,7 +136,14 @@ intercept_wrapper:
ld t3, 32(sp)
ld t4, 40(sp)

/* no point in restoring in context values at 48(sp), 56(sp) and 64(sp) */
/*
* No point in restoring in context values at 48(sp) and 56(sp), while a0
* must be restored only if system call has not been handled yet
*/
li a1, 1
beq t6, a1, handled_ecall
ld a0, 64(sp)
handled_ecall:
ld a1, 72(sp)
ld a2, 80(sp)
ld a3, 88(sp)
Expand Down
4 changes: 2 additions & 2 deletions src/arch/riscv/patcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,8 @@ create_j(unsigned char *from, void *to)
uint32_t *instructions = (uint32_t *)from;
debug_dump("%p: ecall -> jalr %ld\t# %p\n", from, delta, to);

const ptrdiff_t JALR_MAX_OFFSET = 2147481598; // ((2^31-1)-4095)+(2^11-1) = 0x7ffff000 + 0x7ff
const ptrdiff_t JALR_MIN_OFFSET = -2147483648; // (-2^31) = -0x80000000
const ptrdiff_t JALR_MAX_OFFSET = ABS_MAX_POS_OFFSET; // ((2^31-1)-4095)+(2^11-2) = 0x7ffff000 + 0x7fe
const ptrdiff_t JALR_MIN_OFFSET = -(ABS_MAX_NEG_OFFSET); // (-2^31) = -0x80000000 - 0x800

if ((delta & 0x1) != 0) {

Expand Down
60 changes: 37 additions & 23 deletions src/intercept.c
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,25 @@ get_syscall_in_context(struct context *context, struct syscall_desc *sys)
sys->args[5] = SIXTH_ARG_REG;
}

/*
* intercept_routine_post_clone
* The routine called by an assembly wrapper when a clone syscall returns zero,
* and a new stack pointer is used in the child thread.
*/
struct wrapper_ret
intercept_routine_post_clone(struct context *context)
{
if (THREAD_PID == 0) {
if (intercept_hook_point_clone_child != NULL)
intercept_hook_point_clone_child();
} else {
if (intercept_hook_point_clone_parent != NULL)
intercept_hook_point_clone_parent(THREAD_PID);
}

return (struct wrapper_ret){FIRST_RET_REG = THREAD_PID, SECOND_RET_REG = 1 };
}

/*
* intercept_routine(...)
* This is the function called from the asm wrappers,
Expand Down Expand Up @@ -679,7 +698,7 @@ intercept_routine(struct context *context)
#if defined(__x86_64__) || defined(_M_X64)
if (desc.nr == SYS_vfork || desc.nr == SYS_rt_sigreturn) {
#elif defined(__riscv)
if (desc.nr == SYS_clone || desc.nr == SYS_rt_sigreturn) {
if (desc.nr == SYS_rt_sigreturn) {
#endif
/* can't handle these syscalls the normal way */
return (struct wrapper_ret){FIRST_RET_REG = SYSCALL_NR, SECOND_RET_REG = 0 };
Expand All @@ -698,7 +717,7 @@ intercept_routine(struct context *context)
* the clone_child_intercept_routine instead, executing
* it on the new child threads stack, then returns to libc.
*/
#if defined(__x86_64__) || defined(_M_X64)

if (desc.nr == SYS_clone && desc.args[1] != 0) {
return (struct wrapper_ret){
FIRST_RET_REG = SYSCALL_NR, SECOND_RET_REG = 2 };
Expand All @@ -711,36 +730,31 @@ intercept_routine(struct context *context)
}
#endif
else
#endif

result = syscall_no_intercept(desc.nr,
desc.args[0],
desc.args[1],
desc.args[2],
desc.args[3],
desc.args[4],
desc.args[5]);

/*
* Here clone calls with arg[1] == 0 are granted the execution
* of their post_clone hook functions
*/
if (desc.nr == SYS_clone) {
THREAD_PID = result;
intercept_routine_post_clone(context);
}
#ifdef SYS_clone3
else if (desc.nr == SYS_clone3) {
THREAD_PID = result;
intercept_routine_post_clone(context);
}
#endif
}

intercept_log_syscall(patch, &desc, KNOWN, result);
return (struct wrapper_ret){ FIRST_RET_REG = result, SECOND_RET_REG = 1 };
}

/*
* intercept_routine_post_clone
* The routine called by an assembly wrapper when a clone syscall returns zero,
* and a new stack pointer is used in the child thread.
*/

struct wrapper_ret
intercept_routine_post_clone(struct context *context)
{
if (THREAD_PID == 0) {
if (intercept_hook_point_clone_child != NULL)
intercept_hook_point_clone_child();
} else {
if (intercept_hook_point_clone_parent != NULL)
intercept_hook_point_clone_parent(THREAD_PID);
}

return (struct wrapper_ret){FIRST_RET_REG = THREAD_PID, SECOND_RET_REG = 1 };
}
2 changes: 2 additions & 0 deletions src/intercept.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ void activate_patches(struct intercept_desc *desc);
#define SIXTH_ARG_REG PARAM_BY_ARCH(context->r9,context->a[5])
#define FIRST_RET_REG PARAM_BY_ARCH(.rax,.a[0])
#define SECOND_RET_REG PARAM_BY_ARCH(.rdx,.a[1])
#define ABS_MAX_NEG_OFFSET PARAM_BY_ARCH(INT32_MAX,((long)INT32_MAX+0x800))
#define ABS_MAX_POS_OFFSET PARAM_BY_ARCH(INT32_MAX,INT32_MAX-0x801)

bool is_overwritable_nop(const struct intercept_disasm_result *ins);

Expand Down
Loading