├── .gitignore
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── iofomo
│ │ └── opensrc
│ │ └── abyss
│ │ ├── AbyssApplication.java
│ │ └── MainActivity.java
│ ├── jni
│ ├── Android.mk
│ ├── Application.mk
│ ├── arch.h
│ ├── attribute.h
│ ├── build.h
│ ├── cmn
│ │ ├── cmn_back_call_stack.c
│ │ ├── cmn_back_call_stack.h
│ │ ├── cmn_proc.c
│ │ ├── cmn_proc.h
│ │ ├── cmn_pthread_cond.c
│ │ ├── cmn_pthread_cond.h
│ │ ├── cmn_utils.c
│ │ ├── cmn_utils.h
│ │ ├── cmn_vlarray.c
│ │ └── cmn_vlarray.h
│ ├── compat.h
│ ├── constant.h
│ ├── loader
│ │ ├── assembly-arm.h
│ │ ├── assembly-arm64.h
│ │ ├── assembly-x86.h
│ │ ├── assembly-x86_64.h
│ │ ├── assembly.S
│ │ ├── loader.c
│ │ └── script.h
│ ├── path
│ │ ├── binding.c
│ │ ├── binding.h
│ │ ├── canon.c
│ │ ├── canon.h
│ │ ├── glue.c
│ │ ├── glue.h
│ │ ├── path.c
│ │ ├── path.h
│ │ ├── proc.c
│ │ ├── proc.h
│ │ ├── temp.c
│ │ └── temp.h
│ └── test
│ │ └── ptrace
│ │ ├── inject.cpp
│ │ ├── inject.h
│ │ └── ptracedemo.cpp
│ └── res
│ ├── drawable-v24
│ └── ic_launcher_foreground.xml
│ ├── drawable
│ ├── ic_launcher_background.xml
│ ├── list_item_bg_single_selected.xml
│ ├── list_item_bg_single_selector.xml
│ └── list_item_bg_single_unselected.xml
│ ├── layout
│ └── activity_main.xml
│ ├── mipmap-anydpi-v26
│ ├── ic_launcher.xml
│ └── ic_launcher_round.xml
│ ├── mipmap-hdpi
│ ├── ic_launcher.webp
│ └── ic_launcher_round.webp
│ ├── mipmap-mdpi
│ ├── ic_launcher.webp
│ └── ic_launcher_round.webp
│ ├── mipmap-xhdpi
│ ├── ic_launcher.webp
│ └── ic_launcher_round.webp
│ ├── mipmap-xxhdpi
│ ├── ic_launcher.webp
│ └── ic_launcher_round.webp
│ ├── mipmap-xxxhdpi
│ ├── ic_launcher.webp
│ └── ic_launcher_round.webp
│ ├── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
│ └── xml
│ ├── backup_rules.xml
│ └── data_extraction_rules.xml
├── build.gradle
├── build.sh
├── config.gradle
├── doc
└── README.assets
│ └── thanks.png
├── files
└── fireyer.keystore
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── library
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── iofomo
│ │ └── opensrc
│ │ └── abyss
│ │ └── sdk
│ │ ├── Logger.java
│ │ ├── Native.java
│ │ ├── Nativee.java
│ │ └── component
│ │ ├── MTSTCProvider.java
│ │ └── MTSTCService.java
│ └── jni
│ ├── Android.mk
│ ├── Application.mk
│ ├── CMakeLists.txt
│ ├── arch.h
│ ├── attribute.h
│ ├── build.h
│ ├── cmn
│ ├── cmn_back_call_stack.c
│ ├── cmn_back_call_stack.h
│ ├── cmn_proc.c
│ ├── cmn_proc.h
│ ├── cmn_pthread_cond.c
│ ├── cmn_pthread_cond.h
│ ├── cmn_utils.c
│ ├── cmn_utils.h
│ ├── cmn_vlarray.c
│ └── cmn_vlarray.h
│ ├── compat.h
│ ├── constant.h
│ ├── execve
│ ├── aoxp.c
│ ├── aoxp.h
│ ├── auxv.c
│ ├── auxv.h
│ ├── elf.c
│ ├── elf.h
│ ├── enter.c
│ ├── execve.h
│ ├── exit.c
│ ├── ldso.c
│ ├── ldso.h
│ ├── shebang.c
│ └── shebang.h
│ ├── library.c
│ ├── library.h
│ ├── loader
│ ├── assembly-arm.h
│ ├── assembly-arm64.h
│ ├── assembly-x86.h
│ ├── assembly-x86_64.h
│ ├── assembly.S
│ ├── loader.c
│ └── script.h
│ ├── note.c
│ ├── note.h
│ ├── path
│ ├── binding.c
│ ├── binding.h
│ ├── canon.c
│ ├── canon.h
│ ├── glue.c
│ ├── glue.h
│ ├── path.c
│ ├── path.h
│ ├── proc.c
│ ├── proc.h
│ ├── temp.c
│ └── temp.h
│ ├── ptrace
│ ├── ptrace.c
│ ├── ptrace.h
│ ├── user.c
│ ├── user.h
│ ├── wait.c
│ └── wait.h
│ ├── syscall
│ ├── chain.c
│ ├── chain.h
│ ├── enter.c
│ ├── exit.c
│ ├── heap.c
│ ├── heap.h
│ ├── rlimit.c
│ ├── rlimit.h
│ ├── seccomp.c
│ ├── seccomp.h
│ ├── socket.c
│ ├── socket.h
│ ├── syscall.c
│ ├── syscall.h
│ ├── sysnum.c
│ ├── sysnum.h
│ ├── sysnums-arm.h
│ ├── sysnums-arm64.h
│ ├── sysnums-i386.h
│ ├── sysnums-sh4.h
│ ├── sysnums-x32.h
│ ├── sysnums-x86_64.h
│ └── sysnums.list
│ ├── test
│ ├── crashdemo.c
│ ├── execvedemo.c
│ ├── fopen_demo.c
│ ├── main.c
│ ├── multithreaddemo.c
│ ├── myecho.c
│ ├── ptrace
│ │ ├── inject.cpp
│ │ ├── inject.h
│ │ └── ptracedemo.cpp
│ ├── ptraceemudemo.c
│ ├── test.c
│ ├── test2.c
│ ├── test3.c
│ └── usedemo.c
│ └── tracee
│ ├── abi.h
│ ├── event.c
│ ├── event.h
│ ├── mem.c
│ ├── mem.h
│ ├── reg.c
│ ├── reg.h
│ ├── tracee.c
│ └── tracee.h
├── settings.gradle
└── svcer
├── build.gradle
├── proguard-rules.pro
└── src
└── main
├── AndroidManifest.xml
└── jni
├── Android.mk
├── Application.mk
├── inc
├── linux_syscalls.h
├── linux_syscalls_arm.h
├── linux_syscalls_arm64.h
├── linux_syscalls_x86_32.h
├── linux_syscalls_x86_64.h
├── seccomp_macro.h
├── svcer_cmn.h
├── svcer_def.h
├── svcer_dumper.h
├── svcer_finder.h
├── svcer_hooker.h
├── svcer_qemu.h
└── svcer_syscall.h
└── src
├── svcer_dumper.cpp
├── svcer_finder.cpp
├── svcer_hooker.cpp
└── svcer_syscall.cpp
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.aar
4 | *.ap_
5 | *.aab
6 |
7 | .idea
8 |
9 | libs
10 |
11 | # Files for the ART/Dalvik VM
12 | *.dex
13 |
14 | # Java class files
15 | *.class
16 |
17 | # Generated files
18 | bin/
19 | gen/
20 | out/
21 | # Uncomment the following line in case you need and you don't have the release build type files in your app
22 | # release/
23 |
24 | # Gradle files
25 | .gradle/
26 | build/
27 |
28 | # Local configuration file (sdk path, etc)
29 | local.properties
30 |
31 | # Proguard folder generated by Eclipse
32 | proguard/
33 |
34 | # Log Files
35 | *.log
36 |
37 | # Android Studio Navigation editor temp files
38 | .navigation/
39 |
40 | # Android Studio captures folder
41 | captures/
42 |
43 | # IntelliJ
44 | *.iml
45 | .idea/workspace.xml
46 | .idea/tasks.xml
47 | .idea/gradle.xml
48 | .idea/assetWizardSettings.xml
49 | .idea/dictionaries
50 | .idea/libraries
51 | # Android Studio 3 in .gitignore file.
52 | .idea/caches
53 | .idea/modules.xml
54 | # Comment next line if keeping position of elements in Navigation Editor is relevant for you
55 | .idea/navEditor.xml
56 |
57 | # Keystore files
58 | # Uncomment the following lines if you do not want to check your keystore files in.
59 | #*.jks
60 | #*.keystore
61 |
62 | # External native build folder generated in Android Studio 2.2 and later
63 | .externalNativeBuild
64 | .cxx/
65 |
66 | # Google Services (e.g. APIs or Firebase)
67 | # google-services.json
68 |
69 | # Freeline
70 | freeline.py
71 | freeline/
72 | freeline_project_description.json
73 |
74 | # fastlane
75 | fastlane/report.xml
76 | fastlane/Preview.html
77 | fastlane/screenshots
78 | fastlane/test_output
79 | fastlane/readme.md
80 |
81 | # Version control
82 | vcs.xml
83 |
84 | # lint
85 | lint/intermediates/
86 | lint/generated/
87 | lint/outputs/
88 | lint/tmp/
89 | # lint/reports/
90 |
91 | # Android Profiling
92 | *.hprof
93 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Abyss
2 |
3 | ## 说明
4 |
5 | `Abyss`是`Android`平台下应用级系统调用拦截框架。
6 |
7 | 详细的文档可参考:
8 |
9 | [【Abyss】Android平台应用级系统调用拦截框架](https://www.iofomo.com/blog/opensrcabyss)
10 |
11 | ## 快速开始
12 |
13 | 本项目在以下开发环境中编译通过:
14 |
15 | - Android Studio:4.2
16 | - Gradle:6.9.2
17 | - CMakeLists:3.6.0
18 | - NDK:21.4.7075529
19 |
20 | ```shell
21 | $ ./gradlew build
22 | ```
23 |
24 | ## 感谢小伙伴们
25 |
26 | 
27 |
28 | ## 许可协议
29 |
30 | 本项目基于`MIT`许可协议,详情查看 [许可协议](doc/LICENSE) 文档。
31 |
32 | > 本项目和所有的工具都是MIT许可证下的开源工具,这意味着你可以完全访问源代码,并可以根据自己的需求进行修改。
33 |
34 | ## 参考
35 |
36 | [https://github.com/proot-me/proot](https://github.com/proot-me/proot)
37 |
38 | [https://github.com/termux/proot](https://github.com/termux/proot)
39 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | buildToolsVersion "30.0.3"
5 | compileSdkVersion 30
6 |
7 | defaultConfig {
8 | applicationId 'com.iofomo.opensrc.abyss'
9 | minSdkVersion 29
10 | targetSdkVersion 30
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | ndk {
15 | abiFilters 'armeabi-v7a', 'arm64-v8a'
16 | }
17 | }
18 |
19 | signingConfigs {
20 | debug {
21 | storeFile file('../files/fireyer.keystore')
22 | storePassword 'e99c85dc41111f82f7c3290fc87c42bd'
23 | keyAlias 'ifmafireyer'
24 | keyPassword 'e99c85dc41111f82f7c3290fc87c42bd'
25 | }
26 | release {
27 | storeFile file('../files/fireyer.keystore')
28 | storePassword 'e99c85dc41111f82f7c3290fc87c42bd'
29 | keyAlias 'ifmafireyer'
30 | keyPassword 'e99c85dc41111f82f7c3290fc87c42bd'
31 | }
32 | }
33 |
34 | buildTypes {
35 | release {
36 | minifyEnabled false
37 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
38 | signingConfig signingConfigs.release
39 | }
40 | debug {
41 | jniDebuggable true
42 | }
43 | }
44 | compileOptions {
45 | sourceCompatibility JavaVersion.VERSION_1_8
46 | targetCompatibility JavaVersion.VERSION_1_8
47 | }
48 |
49 | // buildFeatures {
50 | // viewBinding true
51 | // }
52 | }
53 |
54 | dependencies {
55 | api project(path: ':library')
56 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
17 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/java/com/iofomo/opensrc/abyss/AbyssApplication.java:
--------------------------------------------------------------------------------
1 | package com.iofomo.opensrc.abyss;
2 |
3 | import android.app.ActivityManager;
4 | import android.app.Application;
5 | import android.content.Context;
6 |
7 | import com.iofomo.opensrc.abyss.sdk.Logger;
8 | import com.iofomo.opensrc.abyss.sdk.Nativee;
9 |
10 | import java.util.List;
11 |
12 | public class AbyssApplication extends Application {
13 |
14 | @Override
15 | protected void attachBaseContext(Context base) {
16 | super.attachBaseContext(base);
17 | Logger.setDefTag("INTERCEPT");
18 | Logger.e("build Type:" + BuildConfig.BUILD_TYPE);
19 |
20 | /**
21 | * ------------------ 拦截集成逻辑 START----
22 | */
23 | initSvcSdk();
24 | /**
25 | * ------------------ 拦截集成逻辑 END----
26 | */
27 | }
28 |
29 | public void initSvcSdk() {
30 | if (isTraceeProcess()) {
31 | int ret = Nativee.attachMe(this);
32 | Logger.d("attachMe ret:" + ret);
33 | if (ret != 0) {
34 | Logger.e("attach error");
35 | return;
36 | }
37 | Nativee.tracee_init();
38 | test();
39 | } else {
40 | Logger.d("not tracee process,ignore");
41 | }
42 | }
43 |
44 | private void test() {
45 | // String content = FileIOUtils.readFile2String(new File("/proc/self/status"));
46 | // Logger.d("status:"+content);
47 | }
48 |
49 | /**
50 | * 是否是tracee进程(被ptrace控制的进程)
51 | */
52 | boolean isTraceeProcess() {
53 | //demo中,非MyContentProvider所在的:process进程
54 | String name = getCurrentProcessName();
55 | Logger.d("cur process:"+name);
56 | if (name == null || !name.endsWith(":MTSTCProc")) {
57 | return true;
58 | }
59 | return false;
60 | }
61 |
62 | String getCurrentProcessName() {
63 | ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
64 | List runningProcesses = am.getRunningAppProcesses();
65 | for (ActivityManager.RunningAppProcessInfo info : runningProcesses) {
66 | if (info.pid == android.os.Process.myPid()) {
67 | String processName = info.processName;
68 | return processName;
69 | }
70 | }
71 | return "unknown";
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/app/src/main/java/com/iofomo/opensrc/abyss/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.iofomo.opensrc.abyss;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 |
6 | public class MainActivity extends Activity {
7 |
8 | @Override
9 | protected void onCreate(Bundle savedInstanceState) {
10 | super.onCreate(savedInstanceState);
11 | setContentView(R.layout.activity_main);
12 | }
13 |
14 | }
--------------------------------------------------------------------------------
/app/src/main/jni/Android.mk:
--------------------------------------------------------------------------------
1 | LOCAL_PATH := $(call my-dir)
2 |
3 | include $(CLEAR_VARS)
4 |
5 | LOCAL_MODULE := loader
6 |
7 | LOCAL_C_INCLUDES := $(LOCAL_PATH)/.
8 | LOCAL_SRC_FILES := loader/loader.c
9 | LOCAL_CFLAGS := -fPIC -ffreestanding -mregparm=3
10 | ifeq ($(TARGET_ARCH_ABI), armeabi-v7a)
11 | LOCAL_LDFLAGS := -static -nostdlib -Wl,-Ttext=0x10000000,-z,noexecstack
12 | else ifeq ($(TARGET_ARCH_ABI), arm64-v8a)
13 | LOCAL_LDFLAGS := -static -nostdlib -Wl,-Ttext=0x2000000000,-z,noexecstack
14 | else
15 | endif
16 | include $(BUILD_EXECUTABLE)
--------------------------------------------------------------------------------
/app/src/main/jni/Application.mk:
--------------------------------------------------------------------------------
1 | APP_ABI := arm64-v8a armeabi-v7a
2 | APP_PLATFORM := android-14
3 |
--------------------------------------------------------------------------------
/app/src/main/jni/attribute.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef ATTRIBUTE_H
24 | #define ATTRIBUTE_H
25 |
26 | #define UNUSED __attribute__((unused))
27 | #define FORMAT(a, b, c) __attribute__ ((format (a, b, c)))
28 | #define DONT_INSTRUMENT __attribute__((no_instrument_function))
29 | #define PACKED __attribute__((packed))
30 | #define WEAK __attribute__((weak))
31 |
32 | #endif /* ATTRIBUTE_H */
33 |
--------------------------------------------------------------------------------
/app/src/main/jni/build.h:
--------------------------------------------------------------------------------
1 | /* This file is auto-generated, edit at your own risk. */
2 | #ifndef BUILD_H
3 | #define BUILD_H
4 | #undef VERSION
5 | #define VERSION "v5.4.0-5f780cba"
6 | //新的linux api,__ANDROID_API__ >= 23时可以
7 | #define HAVE_PROCESS_VM
8 | #define HAVE_SECCOMP_FILTER
9 |
10 | //是否使用模版的loader程序来载入exe
11 | //#define USE_LOADER_EXE
12 |
13 | //是否处理系统调用
14 | #define HANDLE_SYSCALL
15 |
16 | //仅仅为了方便调试的代码(为了排查问题,可能会拖慢效率)
17 | #define DEBUG_ONLY
18 |
19 | //是否启用日志
20 | #define ENABLE_LOG
21 | //使用printf代替android_log_print
22 | //#define LOG_PRINTF
23 |
24 | #endif /* BUILD_H */
25 |
--------------------------------------------------------------------------------
/app/src/main/jni/cmn/cmn_back_call_stack.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/15.
3 | //
4 |
5 | #ifndef CMN_BACK_CALL_STACK_H
6 | #define CMN_BACK_CALL_STACK_H
7 | #include
8 | #include
9 | #include
10 | #include "tracee/tracee.h"
11 |
12 |
13 | /**
14 | * 打印native异常栈
15 | */
16 | void sig_handler_call_stack(int sig, siginfo_t *info, void *context);
17 |
18 | #if defined(__aarch64__)
19 | /**
20 | * tracer打印tracee的堆栈
21 | */
22 | void print_remote_call_stack_arm64(const Tracee *tracee);
23 | #endif
24 |
25 | #endif //CMN_BACK_CALL_STACK_H
26 |
--------------------------------------------------------------------------------
/app/src/main/jni/cmn/cmn_proc.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/19.
3 | //
4 |
5 | #include
6 | #include
7 | #include "cmn_proc.h"
8 | #include "../constant.h"
9 |
10 | void print_maps(char *filter){
11 | FILE * fp = fopen("/proc/self/maps","r");
12 | if (!fp){
13 | LOGE("fp is null")
14 | return;
15 | }
16 | LOGD("start print %d maps,filter:%s",getpid(),filter);
17 | size_t len = 0;
18 | ssize_t nread;
19 | char * line = NULL;
20 |
21 | while ((nread = getline(&line, &len, fp)) != -1) {
22 | // printf("Retrieved line of length %zu:\n", nread);
23 | line[nread] = '\0';
24 | if (filter != NULL && strlen(filter) > 0){
25 | if (strstr(line,filter) != NULL){
26 | LOGD("proc_maps:%s",line)
27 | }
28 | }else{
29 | LOGD("proc_maps:%s",line)
30 | }
31 | }
32 | fclose(fp);
33 | LOGD("end print %d maps",getpid());
34 | }
--------------------------------------------------------------------------------
/app/src/main/jni/cmn/cmn_proc.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/19.
3 | //
4 |
5 | #ifndef CMN_PROC_H
6 | #define CMN_PROC_H
7 | /**
8 | * 打印maps
9 | */
10 | void print_maps(char *filter);
11 | #endif //CMN_PROC_H
12 |
--------------------------------------------------------------------------------
/app/src/main/jni/cmn/cmn_pthread_cond.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/14.
3 | //
4 |
5 |
6 | #include "cmn_pthread_cond.h"
7 |
8 |
9 | /**
10 | * 初始化
11 | */
12 | void cmn_pt_context_init(struct PCond_Context* ctx){
13 | pthread_mutex_init(&ctx->mutex, NULL);
14 | pthread_cond_init(&ctx->cond, NULL);
15 | }
16 |
17 | /**
18 | * 等待事件发生
19 | */
20 | void cmn_pt_wait(struct PCond_Context* ctx){
21 | pthread_mutex_lock(&ctx->mutex);
22 | pthread_cond_wait(&ctx->cond, &ctx->mutex);
23 | pthread_mutex_unlock(&ctx->mutex);
24 | }
25 |
26 | void cmn_pt_wait2(struct PCond_Context* ctx,bool (*check)()){
27 | pthread_mutex_lock(&ctx->mutex);
28 | if (check()){
29 | pthread_cond_wait(&ctx->cond, &ctx->mutex);
30 | }
31 | pthread_mutex_unlock(&ctx->mutex);
32 | }
33 |
34 | /**
35 | * 通知所有等待线程
36 | */
37 | int cmn_pt_broadcast(struct PCond_Context* ctx){
38 | return pthread_cond_broadcast(&ctx->cond);
39 | }
40 |
41 | /**
42 | * 通知第一个等待的线程
43 | */
44 | int cmn_pt_signal(struct PCond_Context* ctx){
45 | return pthread_cond_signal(&ctx->cond);
46 | }
47 |
48 | int cmn_pt_signal2(struct PCond_Context* ctx,bool (*check)()){
49 | pthread_mutex_lock(&ctx->mutex);
50 | int ret = 0;
51 | if (check()){
52 | ret = pthread_cond_signal(&ctx->cond);
53 | }
54 | pthread_mutex_unlock(&ctx->mutex);
55 | return ret;
56 | }
57 |
58 |
59 | /**
60 | * 清除资源
61 | */
62 | int cmn_pt_destroy(struct PCond_Context* ctx){
63 | return pthread_cond_destroy(&ctx->cond);
64 | }
--------------------------------------------------------------------------------
/app/src/main/jni/cmn/cmn_pthread_cond.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/14.
3 | //
4 |
5 | #ifndef CMN_PTHREAD_COND_H
6 | #define CMN_PTHREAD_COND_H
7 | #include
8 | #include
9 |
10 | struct PCond_Context{
11 | pthread_mutex_t mutex;
12 | pthread_cond_t cond;
13 | };
14 |
15 | /**
16 | * 初始化
17 | */
18 | void cmn_pt_context_init(struct PCond_Context* ctx);
19 |
20 | /**
21 | * 等待事件发生
22 | */
23 | void cmn_pt_wait(struct PCond_Context* ctx);
24 |
25 | void cmn_pt_wait2(struct PCond_Context* ctx,bool (*check)());
26 |
27 | /**
28 | * 通知所有等待线程
29 | */
30 | int cmn_pt_broadcast(struct PCond_Context* ctx);
31 |
32 | /**
33 | * 通知第一个等待的线程
34 | */
35 | int cmn_pt_signal(struct PCond_Context* ctx);
36 |
37 | int cmn_pt_signal2(struct PCond_Context* ctx,bool (*check)());
38 |
39 |
40 | /**
41 | * 清除资源
42 | */
43 | int cmn_pt_destroy(struct PCond_Context* ctx);
44 |
45 |
46 | #endif //CMN_PTHREAD_COND_H
47 |
--------------------------------------------------------------------------------
/app/src/main/jni/cmn/cmn_utils.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2024/1/15.
3 | //
4 |
5 | #include "cmn_utils.h"
6 | #include
7 | #include
8 | #include
9 | #include //AT_*
10 |
11 | bool cmn_utils_string_starts_With(const char* str,const char* sub_str){
12 | if (!str) return !sub_str;
13 | if(!sub_str) return false;
14 | size_t str_len = strlen(str);
15 | size_t sub_str_len = strlen(sub_str);
16 | if (!str_len) return !sub_str_len;
17 | return strstr(str,sub_str) == str;
18 | }
19 |
20 | bool cmn_utils_string_ends_With(const char* str,const char* sub_str){
21 | if (!str) return !sub_str;
22 | if(!sub_str) return false;
23 | size_t str_len = strlen(str);
24 | size_t sub_str_len = strlen(sub_str);
25 | if (!str_len) return !sub_str_len;
26 | if (str_len < sub_str_len) return false;
27 | return !strncmp(&str[str_len - sub_str_len],sub_str,str_len - sub_str_len);
28 | }
29 |
30 |
31 | char* cmn_utils_string_replace_With(const char* str,const char* sub_str,const char* rep_str){
32 | //不支持NULL的替换
33 | if (!sub_str || !rep_str) return NULL;
34 | size_t sub_str_len = strlen(sub_str);
35 | size_t rep_str_len = strlen(rep_str);
36 | size_t str_len = strlen(str);
37 |
38 | const char* find = strstr(str,sub_str);
39 | char *ret = NULL;
40 | if (!find){
41 | ret = malloc(sizeof(char)*sizeof(str_len + 1));
42 | strcpy(ret,str);
43 | return ret;
44 | }
45 | ret = malloc(sizeof(char)*sizeof(str_len + rep_str_len - sub_str_len));
46 | strncpy(ret,str,find - str);
47 | strncpy(&ret[find - str],rep_str,rep_str_len);
48 | strcpy(&ret[find - str + rep_str_len],&find[sub_str_len]);
49 | return ret;
50 | }
51 |
52 |
53 |
54 |
55 | ssize_t cmn_utils_readlink(pid_t pid,int fd, char* buf,size_t max_size) {
56 | int ret = 0;
57 | sprintf(buf, "/proc/%d/fd/%d",pid,fd);
58 | ret = readlinkat(AT_FDCWD, buf, buf, max_size);
59 | if (0 <= ret) {
60 | buf[ret] = '\0';
61 | }
62 | return ret;
63 | }
64 |
65 | int cmn_utils_string_split(char* src, char ch, char* items[], int items_len) {
66 | if (!src || !items) return 0;
67 |
68 | int i = 0, cnt = 0;
69 | memset(items, 0, items_len*sizeof(char*));
70 | while (i < items_len && *src) {
71 | if (!items[i]) {
72 | ++ cnt;
73 | items[i] = src;
74 | }
75 | if (*src == ch) {
76 | ++ i;
77 | *src = '\0';
78 | }
79 | ++ src;
80 | }
81 | return cnt;
82 | }
83 | //字符串hash函数
84 | uint32_t cmn_utils_str_hash(const char* str){
85 | uint32_t h = 0, g;
86 | const char* ptr = str;
87 | while (*ptr) {
88 | h = (h << 4) + *ptr++;
89 | g = h & 0xf0000000;
90 | h ^= g;
91 | h ^= g >> 24;
92 | }
93 | return h;
94 | }
--------------------------------------------------------------------------------
/app/src/main/jni/cmn/cmn_utils.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2024/1/15.
3 | //
4 |
5 | #ifndef CMN_UTILS_H
6 | #define CMN_UTILS_H
7 | #include
8 | #include
9 | #include
10 |
11 | bool cmn_utils_string_starts_With(const char* str,const char* sub_str);
12 | bool cmn_utils_string_ends_With(const char* str,const char* sub_str);
13 | char* cmn_utils_string_replace_With(const char* str,const char* sub_str,const char* rep_str);
14 | ssize_t cmn_utils_readlink(pid_t pid,int fd, char* buf,size_t max_size);
15 | int cmn_utils_string_split(char* src, char ch, char* items[], int items_len);
16 | uint32_t cmn_utils_str_hash(const char* str);
17 |
18 | #endif //CMN_UTILS_H
19 |
--------------------------------------------------------------------------------
/app/src/main/jni/cmn/cmn_vlarray.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/13.
3 | //
4 |
5 | #include "cmn_vlarray.h"
6 | #include
7 |
8 | static int head_size(){
9 | return sizeof(int);
10 | }
11 |
12 | static void* org_ptr(void * ctx){
13 | return ctx - head_size();
14 | }
15 |
16 | static void* arr_ptr(void* org_ptr){
17 | return org_ptr + head_size();
18 | }
19 | static void set_arr_length(void* org_ptr,int size){
20 | int* size_ptr = org_ptr;
21 | *size_ptr = size;
22 | }
23 |
24 | void * vl_new_array(int ele_size,int count){
25 | int cap = ele_size * count + head_size();
26 | void * ptr = malloc(cap);
27 | if (!ptr) return NULL;
28 | set_arr_length(ptr,count);
29 | return arr_ptr(ptr);
30 | }
31 |
32 | //获取数组长度
33 | int vl_array_length(void * ctx){
34 | int * ptr = org_ptr(ctx);
35 | return *ptr;
36 | }
37 |
38 | //改变数组长度
39 | void * vl_array_realloc(void * ctx,int ele_size,int count){
40 | int except = ele_size * count + head_size();
41 | void * ptr = realloc(org_ptr(ctx),except);
42 | if (!ptr) return NULL;
43 | set_arr_length(ptr,count);
44 | return arr_ptr(ptr);
45 | }
46 |
47 | void vl_array_free(void * ctx){
48 | free(org_ptr(ctx));
49 | }
50 |
--------------------------------------------------------------------------------
/app/src/main/jni/cmn/cmn_vlarray.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/13.
3 | //
4 |
5 | #ifndef CMN_VLARRAY_H
6 | #define CMN_VLARRAY_H
7 | /**
8 | * 内存结构"size(int) + [e1,e2...en]"
9 | eg.
10 | struct AAA{
11 | int a;
12 | int b;
13 | };
14 | void * ctx = vl_new_array(sizeof(struct AAA),0);
15 | printf("数组长度:%d\n", vl_array_length(ctx));
16 | ctx = vl_array_realloc(ctx, sizeof(struct AAA),5);
17 | printf("数组长度:%d\n", vl_array_length(ctx));
18 | struct AAA * arr = ctx;
19 | printf("数组3,a=%d,b=%d\n",arr[3].a,arr[3].b);
20 | for (int i = 0; i < vl_array_length(ctx); ++i) {
21 | arr[i].a = arr[i].b = i;
22 | }
23 | printf("数组3,a=%d,b=%d\n",arr[3].a,arr[3].b);
24 | vl_array_free(ctx);
25 | */
26 |
27 | void * vl_new_array(int ele_size,int count);
28 |
29 | int vl_array_length(void * ctx);
30 |
31 |
32 | void * vl_array_realloc(void * ctx,int ele_size,int count);
33 |
34 | void vl_array_free(void * ctx);
35 |
36 | #endif //CMN_VLARRAY_H
37 |
--------------------------------------------------------------------------------
/app/src/main/jni/constant.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/11.
3 | //
4 |
5 | #ifndef INTERCEPTSYSCALL_CONSTANT_H
6 | #define INTERCEPTSYSCALL_CONSTANT_H
7 | #include "build.h"
8 | #include "library.h"
9 |
10 | #include
11 | #include
12 |
13 | #define TAG "INTERCEPT"
14 | #define TAG_SYS "INTERCEPT/SYS"
15 | #define TAG_SYSW "INTERCEPT/SYSW"
16 | #define TAG_SYSE "INTERCEPT/SYSE"
17 |
18 | #if defined(ENABLE_LOG)
19 |
20 | #if defined(LOG_PRINTF)
21 |
22 | //set_seccomp_filters信息
23 | #define LOGSECOMP(...) { printf(__VA_ARGS__);printf("\n");}
24 | //#define LOGSECOMP(...) {}
25 |
26 | #define LOGD(...) { printf(__VA_ARGS__);printf("\n");}
27 | #define LOGI(...) { printf(__VA_ARGS__);printf("\n");}
28 | #define LOGW(...) { printf(__VA_ARGS__);printf("\n");}
29 | #define LOGE(...) { printf(__VA_ARGS__);printf("\n");}
30 |
31 |
32 | #define LOGSYS(...) { printf(__VA_ARGS__);printf("\n");}
33 | #define LOGSYSW(...) { printf(__VA_ARGS__);printf("\n");}
34 | #define LOGSYSE(...) { printf(__VA_ARGS__);printf("\n");}
35 | #else
36 |
37 | //set_seccomp_filters信息
38 | #define LOGSECOMP(...) { __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__);}
39 | //#define LOGSECOMP(...) {}
40 |
41 | #define LOGD(...) { __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__);}
42 | #define LOGI(...) { __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__);}
43 | #define LOGW(...) { __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__);}
44 | #define LOGE(...) { __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__);}
45 |
46 |
47 | #define LOGSYS(...) { __android_log_print(ANDROID_LOG_ERROR,TAG_SYS,__VA_ARGS__);}
48 | #define LOGSYSW(...) { __android_log_print(ANDROID_LOG_ERROR,TAG_SYSW,__VA_ARGS__);}
49 | #define LOGSYSE(...) { __android_log_print(ANDROID_LOG_ERROR,TAG_SYSE,__VA_ARGS__);}
50 | #endif
51 | #else
52 |
53 | //set_seccomp_filters信息
54 | #define LOGSECOMP(...) {}
55 |
56 | #define LOGD(...) {}
57 | #define LOGI(...) {}
58 | #define LOGW(...) {}
59 | #define LOGE(...) {}
60 |
61 | #define LOGSYS(...) {}
62 | #define LOGSYSW(...) {}
63 | #define LOGSYSE(...) {}
64 |
65 | #endif
66 |
67 |
68 | #define __LIKELY(x) __builtin_expect(!!(x), true)
69 | #define __UNLIKELY(x) __builtin_expect(!!(x), false)
70 |
71 |
72 | typedef enum readlink_type{
73 | BUSINESS, //业务逻辑数据
74 | NORMAL, //常规的tracee调用
75 | }readlink_type;
76 |
77 | typedef struct readlink_context{
78 | readlink_type type;
79 | void* data;
80 | }readlink_context;
81 |
82 |
83 | typedef void (*on_sys_event_t)(syscall_data* data);
84 |
85 | extern on_sys_event_t global_on_sysenter;
86 | extern on_sys_event_t global_on_sysexit;
87 |
88 | extern pthread_t work_tid; //工作线程pid
89 | extern pid_t snew_attach_pid;
90 | extern struct PCond_Context spctx;
91 |
92 | #endif //INTERCEPTSYSCALL_CONSTANT_H
93 |
--------------------------------------------------------------------------------
/app/src/main/jni/loader/assembly-arm.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | /* According to the ARM EABI, all registers have undefined values at
24 | * program startup except:
25 | *
26 | * - the instruction pointer (r15)
27 | * - the stack pointer (r13)
28 | * - the rtld_fini pointer (r0)
29 | */
30 | #define BRANCH(stack_pointer, destination) do { \
31 | asm volatile ( \
32 | "// Restore initial stack pointer. \n\t" \
33 | "mov sp, %0 \n\t" \
34 | " \n\t" \
35 | "// Clear rtld_fini. \n\t" \
36 | "mov r0, #0 \n\t" \
37 | " \n\t" \
38 | "// Start the program. \n\t" \
39 | "mov pc, %1 \n" \
40 | : /* no output */ \
41 | : "r" (stack_pointer), "r" (destination) \
42 | : "memory", "sp", "r0", "pc"); \
43 | __builtin_unreachable(); \
44 | } while (0)
45 |
46 | #define PREPARE_ARGS_1(arg1_) \
47 | register word_t arg1 asm("r0") = arg1_; \
48 |
49 | #define PREPARE_ARGS_3(arg1_, arg2_, arg3_) \
50 | PREPARE_ARGS_1(arg1_) \
51 | register word_t arg2 asm("r1") = arg2_; \
52 | register word_t arg3 asm("r2") = arg3_; \
53 |
54 | #define PREPARE_ARGS_6(arg1_, arg2_, arg3_, arg4_, arg5_, arg6_) \
55 | PREPARE_ARGS_3(arg1_, arg2_, arg3_) \
56 | register word_t arg4 asm("r3") = arg4_; \
57 | register word_t arg5 asm("r4") = arg5_; \
58 | register word_t arg6 asm("r5") = arg6_;
59 |
60 | #define OUTPUT_CONTRAINTS_1 \
61 | "r" (arg1)
62 |
63 | #define OUTPUT_CONTRAINTS_3 \
64 | OUTPUT_CONTRAINTS_1, \
65 | "r" (arg2), "r" (arg3)
66 |
67 | #define OUTPUT_CONTRAINTS_6 \
68 | OUTPUT_CONTRAINTS_3, \
69 | "r" (arg4), "r" (arg5), "r" (arg6)
70 |
71 | #define SYSCALL(number_, nb_args, args...) \
72 | ({ \
73 | register word_t number asm("r7") = number_; \
74 | register word_t result asm("r0"); \
75 | PREPARE_ARGS_##nb_args(args) \
76 | asm volatile ( \
77 | "svc #0x00000000 \n\t" \
78 | : "=r" (result) \
79 | : "r" (number), \
80 | OUTPUT_CONTRAINTS_##nb_args \
81 | : "memory"); \
82 | result; \
83 | })
84 |
85 | #define OPEN 5
86 | #define CLOSE 6
87 | #define MMAP 192
88 | #define MMAP_OFFSET_SHIFT 12
89 | #define EXECVE 11
90 | #define EXIT 1
91 | #define PRCTL 172
92 | #define MPROTECT 125
93 |
94 |
--------------------------------------------------------------------------------
/app/src/main/jni/loader/assembly-arm64.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | /* According to the ARM64 EABI, all registers have undefined values at
24 | * program startup except:
25 | *
26 | * - the instruction pointer (pc)
27 | * - the stack pointer (sp)
28 | * - the rtld_fini pointer (x0)
29 | */
30 | #define BRANCH(stack_pointer, destination) do { \
31 | asm volatile ( \
32 | "// Restore initial stack pointer. \n\t" \
33 | "mov sp, %0 \n\t" \
34 | " \n\t" \
35 | "// Clear rtld_fini. \n\t" \
36 | "mov x0, #0 \n\t" \
37 | " \n\t" \
38 | "// Start the program. \n\t" \
39 | "br %1 \n" \
40 | : /* no output */ \
41 | : "r" (stack_pointer), "r" (destination) \
42 | : "memory", "sp", "x0"); \
43 | __builtin_unreachable(); \
44 | } while (0)
45 |
46 | #define PREPARE_ARGS_1(arg1_) \
47 | register word_t arg1 asm("x0") = arg1_; \
48 |
49 | #define PREPARE_ARGS_3(arg1_, arg2_, arg3_) \
50 | PREPARE_ARGS_1(arg1_) \
51 | register word_t arg2 asm("x1") = arg2_; \
52 | register word_t arg3 asm("x2") = arg3_; \
53 |
54 | #define PREPARE_ARGS_4(arg1_, arg2_, arg3_, arg4_) \
55 | PREPARE_ARGS_3(arg1_, arg2_, arg3_) \
56 | register word_t arg4 asm("x3") = arg4_; \
57 |
58 | #define PREPARE_ARGS_6(arg1_, arg2_, arg3_, arg4_, arg5_, arg6_) \
59 | PREPARE_ARGS_4(arg1_, arg2_, arg3_, arg4_) \
60 | register word_t arg5 asm("x4") = arg5_; \
61 | register word_t arg6 asm("x5") = arg6_;
62 |
63 | #define OUTPUT_CONTRAINTS_1 \
64 | "r" (arg1)
65 |
66 | #define OUTPUT_CONTRAINTS_3 \
67 | OUTPUT_CONTRAINTS_1, \
68 | "r" (arg2), "r" (arg3)
69 |
70 | #define OUTPUT_CONTRAINTS_4 \
71 | OUTPUT_CONTRAINTS_3, \
72 | "r" (arg4)
73 |
74 | #define OUTPUT_CONTRAINTS_6 \
75 | OUTPUT_CONTRAINTS_4, \
76 | "r" (arg5), "r" (arg6)
77 |
78 | #define SYSCALL(number_, nb_args, args...) \
79 | ({ \
80 | register word_t number asm("x8") = number_; \
81 | register word_t result asm("x0"); \
82 | PREPARE_ARGS_##nb_args(args) \
83 | asm volatile ( \
84 | "svc #0x00000000 \n\t" \
85 | : "=r" (result) \
86 | : "r" (number), \
87 | OUTPUT_CONTRAINTS_##nb_args \
88 | : "memory"); \
89 | result; \
90 | })
91 |
92 | #define OPENAT 56
93 | #define CLOSE 57
94 | #define MMAP 222
95 | #define EXECVE 221
96 | #define EXIT 93
97 | #define PRCTL 167
98 | #define MPROTECT 226
99 |
--------------------------------------------------------------------------------
/app/src/main/jni/loader/assembly-x86.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | /* According to the x86 ABI, all registers have undefined values at
24 | * program startup except:
25 | *
26 | * - the instruction pointer (rip)
27 | * - the stack pointer (rsp)
28 | * - the rtld_fini pointer (rdx)
29 | * - the system flags (eflags)
30 | */
31 | #define BRANCH(stack_pointer, destination) do { \
32 | asm volatile ( \
33 | "// Restore initial stack pointer. \n\t" \
34 | "movl %0, %%esp \n\t" \
35 | " \n\t" \
36 | "// Clear state flags. \n\t" \
37 | "pushl $0 \n\t" \
38 | "popfl \n\t" \
39 | " \n\t" \
40 | "// Clear rtld_fini. \n\t" \
41 | "movl $0, %%edx \n\t" \
42 | " \n\t" \
43 | "// Start the program. \n\t" \
44 | "jmpl *%%eax \n" \
45 | : /* no output */ \
46 | : "irm" (stack_pointer), "a" (destination) \
47 | : "memory", "cc", "esp", "edx"); \
48 | __builtin_unreachable(); \
49 | } while (0)
50 |
51 | extern word_t syscall_6(word_t number,
52 | word_t arg1, word_t arg2, word_t arg3,
53 | word_t arg4, word_t arg5, word_t arg6);
54 |
55 | extern word_t syscall_3(word_t number, word_t arg1, word_t arg2, word_t arg3);
56 |
57 | extern word_t syscall_1(word_t number, word_t arg1);
58 |
59 | #define SYSCALL(number, nb_args, args...) syscall_##nb_args(number, args)
60 |
61 | #define OPEN 5
62 | #define CLOSE 6
63 | #define MMAP 192
64 | #define MMAP_OFFSET_SHIFT 12
65 | #define EXECVE 11
66 | #define EXIT 1
67 | #define PRCTL 172
68 | #define MPROTECT 125
69 |
--------------------------------------------------------------------------------
/app/src/main/jni/loader/assembly-x86_64.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | /* According to the x86_64 ABI, all registers have undefined values at
24 | * program startup except:
25 | *
26 | * - the instruction pointer (rip)
27 | * - the stack pointer (rsp)
28 | * - the rtld_fini pointer (rdx)
29 | * - the system flags (rflags)
30 | */
31 | #define BRANCH(stack_pointer, destination) do { \
32 | asm volatile ( \
33 | "// Restore initial stack pointer. \n\t" \
34 | "movq %0, %%rsp \n\t" \
35 | " \n\t" \
36 | "// Clear state flags. \n\t" \
37 | "pushq $0 \n\t" \
38 | "popfq \n\t" \
39 | " \n\t" \
40 | "// Clear rtld_fini. \n\t" \
41 | "movq $0, %%rdx \n\t" \
42 | " \n\t" \
43 | "// Start the program. \n\t" \
44 | "jmpq *%%rax \n" \
45 | : /* no output */ \
46 | : "irm" (stack_pointer), "a" (destination) \
47 | : "memory", "cc", "rsp", "rdx"); \
48 | __builtin_unreachable(); \
49 | } while (0)
50 |
51 | #define PREPARE_ARGS_1(arg1_) \
52 | register word_t arg1 asm("rdi") = arg1_; \
53 |
54 | #define PREPARE_ARGS_3(arg1_, arg2_, arg3_) \
55 | PREPARE_ARGS_1(arg1_) \
56 | register word_t arg2 asm("rsi") = arg2_; \
57 | register word_t arg3 asm("rdx") = arg3_; \
58 |
59 | #define PREPARE_ARGS_6(arg1_, arg2_, arg3_, arg4_, arg5_, arg6_) \
60 | PREPARE_ARGS_3(arg1_, arg2_, arg3_) \
61 | register word_t arg4 asm("r10") = arg4_; \
62 | register word_t arg5 asm("r8") = arg5_; \
63 | register word_t arg6 asm("r9") = arg6_;
64 |
65 | #define OUTPUT_CONTRAINTS_1 \
66 | "r" (arg1)
67 |
68 | #define OUTPUT_CONTRAINTS_3 \
69 | OUTPUT_CONTRAINTS_1, \
70 | "r" (arg2), "r" (arg3)
71 |
72 | #define OUTPUT_CONTRAINTS_6 \
73 | OUTPUT_CONTRAINTS_3, \
74 | "r" (arg4), "r" (arg5), "r" (arg6)
75 |
76 | #define SYSCALL(number_, nb_args, args...) \
77 | ({ \
78 | register word_t number asm("rax") = number_; \
79 | register word_t result asm("rax"); \
80 | PREPARE_ARGS_##nb_args(args) \
81 | asm volatile ( \
82 | "syscall \n\t" \
83 | : "=r" (result) \
84 | : "r" (number), \
85 | OUTPUT_CONTRAINTS_##nb_args \
86 | : "memory", "cc", "rcx", "r11"); \
87 | result; \
88 | })
89 |
90 | #define OPEN 2
91 | #define CLOSE 3
92 | #define MMAP 9
93 | #define EXECVE 59
94 | #define EXIT 60
95 | #define PRCTL 157
96 | #define MPROTECT 10
97 |
--------------------------------------------------------------------------------
/app/src/main/jni/loader/assembly.S:
--------------------------------------------------------------------------------
1 | #if defined(__i386__)
2 | .text
3 |
4 | /*
5 | ABI user-land kernel-land
6 | ====== ========= ===========
7 | number %eax %eax
8 | arg1 %edx %ebx
9 | arg2 %ecx %ecx
10 | arg3 16(%esp) %edx
11 | arg4 12(%esp) %esi
12 | arg5 8(%esp) %edi
13 | arg6 4(%esp) %ebp
14 | result N/A %eax
15 | */
16 | .globl syscall_6
17 | .type syscall_6, @function
18 | syscall_6:
19 | /* Callee-saved registers. */
20 | pushl %ebp // %esp -= 0x04
21 | pushl %edi // %esp -= 0x08
22 | pushl %esi // %esp -= 0x0c
23 | pushl %ebx // %esp -= 0x10
24 |
25 | // mov %eax, %eax // number
26 | mov %edx, %ebx // arg1
27 | // mov %ecx, %ecx // arg2
28 | mov 0x14(%esp), %edx // arg3
29 | mov 0x18(%esp), %esi // arg4
30 | mov 0x1c(%esp), %edi // arg5
31 | mov 0x20(%esp), %ebp // arg6
32 |
33 | int $0x80
34 |
35 | popl %ebx
36 | popl %esi
37 | popl %edi
38 | popl %ebp
39 |
40 | // mov %eax, %eax // result
41 | ret
42 |
43 | .globl syscall_3
44 | .type syscall_3, @function
45 | syscall_3:
46 | pushl %ebx
47 | mov %edx, %ebx
48 | mov 0x8(%esp), %edx
49 | int $0x80
50 | popl %ebx
51 | ret
52 |
53 | .globl syscall_1
54 | .type syscall_1, @function
55 | syscall_1:
56 | pushl %ebx
57 | mov %edx, %ebx
58 | int $0x80
59 | popl %ebx
60 | ret
61 |
62 | #endif /* defined(__i386__) */
63 |
--------------------------------------------------------------------------------
/app/src/main/jni/loader/script.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef SCRIPT
24 | #define SCRIPT
25 |
26 | #include "arch.h"
27 | #include "attribute.h"
28 |
29 | struct load_statement {
30 | word_t action;
31 |
32 | union {
33 | struct {
34 | word_t string_address;
35 | } open;
36 |
37 | struct {
38 | word_t addr;
39 | word_t length;
40 | word_t prot;
41 | word_t offset;
42 | word_t clear_length;
43 | } mmap;
44 |
45 | struct {
46 | word_t start;
47 | } make_stack_exec;
48 |
49 | struct {
50 | word_t stack_pointer;
51 | word_t entry_point;
52 | word_t at_phdr;
53 | word_t at_phent;
54 | word_t at_phnum;
55 | word_t at_entry;
56 | word_t at_execfn;
57 | } start;
58 | };
59 | } PACKED;
60 |
61 | typedef struct load_statement LoadStatement;
62 |
63 | #define LOAD_STATEMENT_SIZE(statement, type) \
64 | (sizeof((statement).action) + sizeof((statement).type))
65 |
66 | /* Don't use enum, since sizeof(enum) doesn't have to be equal to
67 | * sizeof(word_t). Keep values in the same order as their respective
68 | * actions appear in loader.c to get a change GCC produces a jump
69 | * table. */
70 | #define LOAD_ACTION_OPEN_NEXT 0
71 | #define LOAD_ACTION_OPEN 1
72 | #define LOAD_ACTION_MMAP_FILE 2
73 | #define LOAD_ACTION_MMAP_ANON 3
74 | #define LOAD_ACTION_MAKE_STACK_EXEC 4
75 | #define LOAD_ACTION_START_TRACED 5
76 | #define LOAD_ACTION_START 6
77 |
78 | #endif /* SCRIPT */
79 |
--------------------------------------------------------------------------------
/app/src/main/jni/path/binding.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef BINDING_H
24 | #define BINDING_H
25 |
26 | #include /* PATH_MAX, */
27 | #include
28 |
29 | #include "tracee/tracee.h"
30 | #include "path.h"
31 |
32 | typedef struct binding {
33 | Path host;
34 | Path guest;
35 |
36 | bool need_substitution;
37 | bool must_exist;
38 |
39 | struct {
40 | CIRCLEQ_ENTRY(binding) pending;
41 | CIRCLEQ_ENTRY(binding) guest;
42 | CIRCLEQ_ENTRY(binding) host;
43 | } link;
44 | } Binding;
45 |
46 | typedef CIRCLEQ_HEAD(bindings, binding) Bindings;
47 |
48 | extern Binding *insort_binding3(const Tracee *tracee, const void *context,
49 | const char host_path[PATH_MAX], const char guest_path[PATH_MAX]);
50 | extern Binding *new_binding(Tracee *tracee, const char *host, const char *guest, bool must_exist);
51 | extern int initialize_bindings(Tracee *tracee);
52 | extern const char *get_path_binding(const Tracee* tracee, Side side, const char path[PATH_MAX]);
53 | extern Binding *get_binding(const Tracee *tracee, Side side, const char path[PATH_MAX]);
54 | extern const char *get_root(const Tracee* tracee);
55 | extern int substitute_binding(const Tracee* tracee, Side side, char path[PATH_MAX]);
56 | extern void remove_binding_from_all_lists(const Tracee *tracee, Binding *binding);
57 |
58 | #endif /* BINDING_H */
59 |
--------------------------------------------------------------------------------
/app/src/main/jni/path/canon.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef CANON_H
24 | #define CANON_H
25 |
26 | #include
27 | #include
28 |
29 | #include "tracee/tracee.h"
30 |
31 | extern int canonicalize(Tracee *tracee, const char *user_path, bool deref_final,
32 | char guest_path[PATH_MAX], unsigned int nb_recursion);
33 |
34 | #endif /* CANON_H */
35 |
--------------------------------------------------------------------------------
/app/src/main/jni/path/glue.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef GLUE_H
24 | #define GLUE_H
25 |
26 | #include /* PATH_MAX, */
27 |
28 | #include "tracee/tracee.h"
29 | #include "path.h"
30 |
31 | extern mode_t build_glue(Tracee *tracee, const char *guest_path, char host_path[PATH_MAX],
32 | Finality finality);
33 |
34 | #endif /* GLUE_H */
35 |
--------------------------------------------------------------------------------
/app/src/main/jni/path/path.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef PATH_H
24 | #define PATH_H
25 |
26 | #include /* pid_t, */
27 | #include /* AT_FDCWD, */
28 | #include /* PATH_MAX, */
29 | #include
30 |
31 | #include "tracee/tracee.h"
32 |
33 | /* File type. */
34 | typedef enum {
35 | REGULAR,
36 | SYMLINK,
37 | } Type;
38 |
39 | /* Path point-of-view. */
40 | typedef enum {
41 | GUEST,
42 | HOST,
43 |
44 | /* Used for bindings as specified by the user but not
45 | * canonicalized yet (new_binding, initialize_binding). */
46 | PENDING,
47 | } Side;
48 |
49 | /* Path with cached attributes. */
50 | typedef struct {
51 | char path[PATH_MAX];
52 | size_t length;
53 | Side side;
54 | } Path;
55 |
56 | /* Path ending type. */
57 | typedef enum {
58 | NOT_FINAL,
59 | FINAL_NORMAL,
60 | FINAL_SLASH,
61 | FINAL_DOT
62 | } Finality;
63 |
64 | #define IS_FINAL(a) ((a) != NOT_FINAL)
65 |
66 | /* Comparison between two paths. */
67 | typedef enum Comparison {
68 | PATHS_ARE_EQUAL,
69 | PATH1_IS_PREFIX,
70 | PATH2_IS_PREFIX,
71 | PATHS_ARE_NOT_COMPARABLE,
72 | } Comparison;
73 |
74 | extern int which(Tracee *tracee, const char *paths, char host_path[PATH_MAX], const char *command);
75 | extern int realpath2(Tracee *tracee, char host_path[PATH_MAX], const char *path, bool deref_final);
76 | extern int getcwd2(Tracee *tracee, char guest_path[PATH_MAX]);
77 | extern void chop_finality(char *path);
78 |
79 | extern int translate_path(Tracee *tracee, char host_path[PATH_MAX],
80 | int dir_fd, const char *guest_path, bool deref_final);
81 |
82 | extern int detranslate_path(Tracee *tracee, char path[PATH_MAX], const char t_referrer[PATH_MAX]);
83 | extern bool belongs_to_guestfs(const Tracee *tracee, const char *path);
84 |
85 | extern int join_paths(int number_paths, char result[PATH_MAX], ...);
86 | extern int list_open_fd(const Tracee *tracee);
87 |
88 | extern Comparison compare_paths(const char *path1, const char *path2);
89 | extern Comparison compare_paths2(const char *path1, size_t length1, const char *path2, size_t length2);
90 |
91 | extern size_t substitute_path_prefix(char path[PATH_MAX], size_t old_prefix_length,
92 | const char *new_prefix, size_t new_prefix_length);
93 |
94 | extern int readlink_proc_pid_fd(pid_t pid, int fd, char path[PATH_MAX]);
95 |
96 | /* Check if path interpretable relatively to dirfd, see openat(2) for details. */
97 | #define AT_FD(dirfd, path) ((dirfd) != AT_FDCWD && ((path) != NULL && (path)[0] != '/'))
98 |
99 | #endif /* PATH_H */
100 |
--------------------------------------------------------------------------------
/app/src/main/jni/path/proc.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef PROC_H
24 | #define PROC_H
25 |
26 | #include
27 |
28 | #include "tracee/tracee.h"
29 | #include "path/path.h"
30 |
31 | /* Action to do after a call to readlink_proc(). */
32 | typedef enum {
33 | DEFAULT, /* Nothing special to do, treat it as a regular link. */
34 | CANONICALIZE, /* The symlink was dereferenced, now canonicalize it. */
35 | DONT_CANONICALIZE, /* The symlink shouldn't be dereferenced nor canonicalized. */
36 | } Action;
37 |
38 |
39 | extern Action readlink_proc(const Tracee *tracee, char result[PATH_MAX], const char path[PATH_MAX],
40 | const char component[NAME_MAX], Comparison comparison);
41 |
42 | extern ssize_t readlink_proc2(const Tracee *tracee, char result[PATH_MAX], const char path[PATH_MAX]);
43 |
44 | #endif /* PROC_H */
45 |
--------------------------------------------------------------------------------
/app/src/main/jni/path/temp.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef TEMP_H
24 | #define TEMP_H
25 |
26 | //#include
27 |
28 | extern char *create_temp_name(void *context, const char *prefix);
29 | extern const char *create_temp_directory(void *context, const char *prefix);
30 | extern const char *create_temp_file(void *context, const char *prefix);
31 | extern FILE* open_temp_file(void *context, const char *prefix);
32 | extern const char *get_temp_directory();
33 |
34 | #endif /* TEMP_H */
35 |
--------------------------------------------------------------------------------
/app/src/main/jni/test/ptrace/inject.h:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2015, Simone 'evilsocket' Margaritelli
2 | Copyright (c) 2015-2019, Jorrit 'Chainfire' Jongma
3 | See LICENSE file for details */
4 |
5 | #ifndef INJECT_H
6 | #define INJECT_H
7 |
8 | #include
9 | #include
10 | #include
11 |
12 |
13 | #if defined(__arm__)
14 | #define CPSR_T_MASK ( 1u << 5 )
15 | #define PARAMS_IN_REGS 4
16 | #elif defined(__aarch64__)
17 | #define CPSR_T_MASK ( 1u << 5 )
18 | #define PARAMS_IN_REGS 8
19 | #define pt_regs user_pt_regs
20 | #define uregs regs
21 | #define ARM_pc pc
22 | #define ARM_sp sp
23 | #define ARM_cpsr pstate
24 | #define ARM_lr regs[30]
25 | #define ARM_r0 regs[0]
26 | #endif
27 |
28 | #if defined(__LP64__)
29 | #define PATH_LINKER_BIONIC "/bionic/bin/linker64"
30 | #define PATH_LIBDL_BIONIC "/bionic/lib64/libdl.so"
31 | #define PATH_LIBC_BIONIC "/bionic/lib64/libc.so"
32 | #define PATH_LINKER "/system/bin/linker64"
33 | #define PATH_LIBDL "/system/lib64/libdl.so"
34 | #define PATH_LIBC "/system/lib64/libc.so"
35 | #define PATH_LIBANDROID_RUNTIME "/system/lib64/libandroid_runtime.so"
36 | #else
37 | #define PATH_LINKER_BIONIC "/bionic/bin/linker"
38 | #define PATH_LIBDL_BIONIC "/bionic/lib/libdl.so"
39 | #define PATH_LIBC_BIONIC "/bionic/lib/libc.so"
40 | #define PATH_LINKER "/system/bin/linker"
41 | #define PATH_LIBDL "/system/lib/libdl.so"
42 | #define PATH_LIBC "/system/lib/libc.so"
43 | #define PATH_LIBANDROID_RUNTIME "/system/lib/libandroid_runtime.so"
44 | #endif
45 |
46 |
47 | // No need to reference manually, use HOOKLOG
48 | //extern const char* _libinject_log_tag;
49 | //extern int _libinject_log;
50 |
51 | // Pass NULL to disable logging
52 | //void libinject_log(const char* log_tag);
53 |
54 |
55 | #define INJECTLOG(F,...) { printf(F,##__VA_ARGS__);printf("\n");}
56 | //#define INJECTLOG(F,...) {}
57 |
58 | // Find pid for process
59 | //pid_t libinject_find_pid_of(const char* process);
60 |
61 | extern pid_t _pid;
62 |
63 | // Load library in process pid, returns 0 on success
64 | int libinject_injectvm(pid_t pid, char* library, char* param);
65 |
66 | void trace_getregs(const char* debug, struct pt_regs * regs);
67 |
68 | #endif
69 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/list_item_bg_single_selected.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/list_item_bg_single_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/list_item_bg_single_unselected.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
17 |
18 |
23 |
24 |
25 |
33 |
34 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iofomo/abyss/80340a7a33e396fb64da0769feec35c3dabaf5ef/app/src/main/res/mipmap-hdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iofomo/abyss/80340a7a33e396fb64da0769feec35c3dabaf5ef/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iofomo/abyss/80340a7a33e396fb64da0769feec35c3dabaf5ef/app/src/main/res/mipmap-mdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iofomo/abyss/80340a7a33e396fb64da0769feec35c3dabaf5ef/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iofomo/abyss/80340a7a33e396fb64da0769feec35c3dabaf5ef/app/src/main/res/mipmap-xhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iofomo/abyss/80340a7a33e396fb64da0769feec35c3dabaf5ef/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iofomo/abyss/80340a7a33e396fb64da0769feec35c3dabaf5ef/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iofomo/abyss/80340a7a33e396fb64da0769feec35c3dabaf5ef/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iofomo/abyss/80340a7a33e396fb64da0769feec35c3dabaf5ef/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iofomo/abyss/80340a7a33e396fb64da0769feec35c3dabaf5ef/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #ffffffff
4 | #77000000
5 | #66000000
6 | #66000000
7 | #ff000000
8 | #99000000
9 | #66ffffff
10 | #ffffffff
11 | #ffff0000
12 | #ffcccccc
13 | #ffc4c4c4
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Abyss
3 | UnitTest
4 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/backup_rules.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/data_extraction_rules.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
12 |
13 |
19 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | maven { url 'https://maven.aliyun.com/repository/public' }
4 | maven { url 'https://maven.aliyun.com/repository/google' }
5 | maven { url 'https://www.jitpack.io' }
6 | maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
7 | }
8 |
9 | dependencies {
10 | // classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6"
11 | classpath 'com.android.tools.build:gradle:4.2.0'
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | maven { url 'https://maven.aliyun.com/repository/public' }
18 | maven { url 'https://maven.aliyun.com/repository/google' }
19 | maven { url 'https://www.jitpack.io' }
20 | maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
21 | }
22 | }
23 |
24 | task clean(type: Delete) {
25 | delete rootProject.buildDir
26 | }
--------------------------------------------------------------------------------
/build.sh:
--------------------------------------------------------------------------------
1 | #/bin/bash
2 | set -e
3 | #brew install cmake
4 | #cd out/
5 | #make clean
6 | #ABI=arm64-v8a #armeabi-v7a
7 | #MINSDKVERSION=23
8 | #NDK=/Users/mac/Library/Android/sdk/ndk/25.2.9519653
9 | #
10 | #CMAKE_ROOT=/Users/mac/Library/Android/sdk/cmake/3.6.0
11 | #CMAKE_ROOT=/Users/mac/Library/Android/sdk/cmake/3.6.0
12 |
13 | #使用android下的cmake,不要使用系统的cmake
14 | #DCMAKE_LIBRARY_OUTPUT_DIRECTORY DCMAKE_RUNTIME_OUTPUT_DIRECTORY 输出目录
15 | #DCMAKE_MAKE_PROGRAM make程序
16 | #https://developer.android.com/ndk/guides/cmake?hl=zh-cn#command-line_1
17 | #$CMAKE_ROOT/bin/cmake \
18 | # -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake \
19 | # -DANDROID_ABI=$ABI \
20 | # -DANDROID_PLATFORM=android-$MINSDKVERSION \
21 | # -DCMAKE_BUILD_TYPE=Debug \
22 | # -DCMAKE_ANDROID_NDK=$NDK \
23 | # -DCMAKE_ANDROID_ARCH_ABI=$ABI \
24 | # -DCMAKE_SYSTEM_NAME=Android \
25 | # -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=bin\
26 | # -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=bin\
27 | # -DCMAKE_MAKE_PROGRAM=$CMAKE_ROOT/bin/ninja \
28 | # -DCMAKE_SYSTEM_VERSION=$MINSDKVERSION ../app/src/main/jni/
29 | #
30 | #make
31 | #adb push bin/testsvc /data/local/tmp
32 |
33 | #构建loader
34 | #ndk 14
35 | #cd app/src/main/jni
36 | #ndk-build
37 | #cd ..
38 | #adb push ./libs/arm64-v8a/loader /data/local/tmp/loader_arm64
39 | #adb push ./libs/armeabi-v7a/loader /data/local/tmp/loader_arm32
40 | #adb push ./libs/arm64-v8a/loader /sdcard/loader_arm64
41 | #adb push ./libs/armeabi-v7a/loader /sdcard/loader_arm32
42 | #rm -rf ./libs
43 | #rm -rf ./obj
44 | #cd ../../../
45 |
46 | #会同时编译arm32和arm64
47 | ./gradlew build
48 | adb push ./app/build/intermediates/cmake/debug/obj/arm64-v8a/testsvc /data/local/tmp
49 |
50 | #推送源码
51 | adb shell rm -rf /data/local/tmp/source
52 | adb push ./app/src/main/jni/ /data/local/tmp/source
53 | #set substitute-path /Users/mac/dev/code/InterceptSysCall/app/src/main/jni /data/local/tmp/source
54 |
55 |
56 | #adb push ./app/build/intermediates/cmake/debug/obj/arm64-v8a/crashdemo /data/local/tmp
57 | #adb push ./app/build/intermediates/cmake/debug/obj/arm64-v8a/test1 /data/local/tmp
58 | #adb push ./app/build/intermediates/cmake/debug/obj/arm64-v8a/test2 /data/local/tmp
59 | adb push ./app/build/intermediates/cmake/debug/obj/arm64-v8a/usedemo /data/local/tmp
60 |
61 | adb push ./app/build/intermediates/cmake/debug/obj/arm64-v8a/myecho /data/local/tmp
62 | adb push ./app/build/intermediates/cmake/debug/obj/arm64-v8a/execvedemo /data/local/tmp
63 | adb push ./app/build/intermediates/cmake/debug/obj/arm64-v8a/execvedemo /sdcard/
64 |
65 | #adb push ./app/build/intermediates/cmake/debug/obj/armeabi-v7a/myecho /data/local/tmp
66 | #adb push ./app/build/intermediates/cmake/debug/obj/arm64-v8a/execvedemo /data/local/tmp
67 |
68 | adb push ./app/build/intermediates/cmake/debug/obj/arm64-v8a/ptracedemo /data/local/tmp
69 | adb push ./app/build/intermediates/cmake/debug/obj/arm64-v8a/ptraceemudemo /data/local/tmp
70 | adb push ./app/build/intermediates/cmake/debug/obj/arm64-v8a/ptraceemudemo /sdcard/
71 | #adb push ./app/build/intermediates/cmake/debug/obj/armeabi-v7a/ptraceemudemo /data/local/tmp
72 |
73 | adb push ./app/build/intermediates/cmake/debug/obj/arm64-v8a/multithreaddemo /data/local/tmp
74 |
75 | #adb shell kill -9 `adb shell ps -ef|grep testsvc$ | awk '{print $2}' | head -1`
76 | #find . -name liblibrary_static.a
77 | #./app/.cxx/Debug/5b471h49/armeabi-v7a/liblibrary_static.a
78 | #./app/.cxx/Debug/5b471h49/arm64-v8a/liblibrary_static.a
79 | # set substitute-path /Users/mac/dev/code/InterceptSysCall/app/src/main/jni /data/local/tmp/source
--------------------------------------------------------------------------------
/config.gradle:
--------------------------------------------------------------------------------
1 | ext {
2 |
3 | // 安装包版本配置
4 | compileSdk = 32
5 | // minSdk = 28 //9.0
6 | minSdk = 23
7 | targetSdk = 32
8 | javaVersion = JavaVersion.VERSION_1_8
9 | applicationId = 'com.iofomo.intercept_syscall'
10 | versionCode = 1
11 | versionName = '1.0.0'
12 |
13 | // 第三方库配置
14 | dependencies = [
15 | 'appcompat': 'androidx.appcompat:appcompat:1.4.1',
16 | 'material': 'com.google.android.material:material:1.5.0',
17 | 'constraintlayout': 'androidx.constraintlayout:constraintlayout:2.1.3',
18 | //工具库
19 | 'utilcodex':'com.blankj:utilcodex:1.31.1'
20 | ]
21 | }
--------------------------------------------------------------------------------
/doc/README.assets/thanks.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iofomo/abyss/80340a7a33e396fb64da0769feec35c3dabaf5ef/doc/README.assets/thanks.png
--------------------------------------------------------------------------------
/files/fireyer.keystore:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iofomo/abyss/80340a7a33e396fb64da0769feec35c3dabaf5ef/files/fireyer.keystore
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Enables namespacing of each library's R class so that its R class includes only the
19 | # resources declared in the library itself and none from the library's dependencies,
20 | # thereby reducing the size of the R class for that library
21 | android.nonTransitiveRClass=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iofomo/abyss/80340a7a33e396fb64da0769feec35c3dabaf5ef/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 11 23:11:12 CST 2023
2 | distributionBase=GRADLE_USER_HOME
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.9.2-all.zip
4 | distributionPath=wrapper/dists
5 | zipStorePath=wrapper/dists
6 | zipStoreBase=GRADLE_USER_HOME
7 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | buildToolsVersion "30.0.3"
5 | compileSdkVersion 30
6 |
7 | defaultConfig {
8 | minSdkVersion 29
9 | targetSdkVersion 30
10 |
11 | externalNativeBuild {
12 | cmake {
13 | cppFlags ''
14 | }
15 | }
16 | ndk {
17 | abiFilters 'armeabi-v7a', 'arm64-v8a'
18 | }
19 | }
20 |
21 | buildTypes {
22 | release {
23 | minifyEnabled false
24 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
25 | jniDebuggable true
26 | debuggable true
27 | }
28 | debug {
29 | jniDebuggable true
30 | }
31 | }
32 | compileOptions {
33 | sourceCompatibility JavaVersion.VERSION_1_8
34 | targetCompatibility JavaVersion.VERSION_1_8
35 | }
36 | externalNativeBuild {
37 | cmake {
38 | path file('src/main/jni/CMakeLists.txt')
39 | version '3.6.0'
40 | }
41 | }
42 | }
43 |
44 | dependencies {
45 | }
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
11 |
12 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/library/src/main/java/com/iofomo/opensrc/abyss/sdk/Logger.java:
--------------------------------------------------------------------------------
1 | package com.iofomo.opensrc.abyss.sdk;
2 |
3 | import android.util.Log;
4 |
5 | public class Logger {
6 | private static String sDefTag = "undefine";
7 |
8 | public static final void setDefTag(String defTag){
9 | sDefTag = defTag;
10 | }
11 |
12 |
13 | public static void d(String TAG,String message){
14 | Log.e(TAG,message);
15 | }
16 |
17 | public static void e(String TAG,String message){
18 | Log.e(TAG,message);
19 | }
20 |
21 | public static void d(String message){
22 | Log.e(sDefTag,message);
23 | }
24 |
25 | public static void e(String message){
26 | Log.e(sDefTag,message);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/library/src/main/java/com/iofomo/opensrc/abyss/sdk/Native.java:
--------------------------------------------------------------------------------
1 | package com.iofomo.opensrc.abyss.sdk;
2 |
3 | /**
4 | * tracer(ptrace所在进程)
5 | */
6 | public class Native {
7 | static {
8 | System.loadLibrary("abyss");
9 | }
10 | public static native void init();
11 | public static native int trace_pid(int pid);
12 | }
13 |
--------------------------------------------------------------------------------
/library/src/main/java/com/iofomo/opensrc/abyss/sdk/Nativee.java:
--------------------------------------------------------------------------------
1 | package com.iofomo.opensrc.abyss.sdk;
2 |
3 | import android.content.Context;
4 | import android.net.Uri;
5 | import android.os.Bundle;
6 | import android.os.Process;
7 | import android.util.Log;
8 |
9 | import com.iofomo.opensrc.abyss.sdk.component.MTSTCProvider;
10 |
11 | /**
12 | * tracee (应用业务所在进程)
13 | */
14 | public class Nativee {
15 | private static final int ERRNO_MAX = 133;
16 |
17 | private static final int FILTERED_FUNC_OPEN = 0x1;
18 | private static final int FILTERED_FUNC_CLOSE = 0x2;
19 |
20 | static {
21 | System.loadLibrary("abyss");
22 | }
23 |
24 | /**
25 | * 初始化
26 | */
27 | private static native boolean before_attach();
28 | private static native boolean tracee_init_native(int flags);
29 |
30 |
31 | public static void tracee_init(){
32 | tracee_init_native(FILTERED_FUNC_OPEN|FILTERED_FUNC_CLOSE);
33 | }
34 |
35 | public static void tracee_init(int flags){
36 | tracee_init_native(flags);
37 | }
38 |
39 | /**
40 | *
41 | * @param appCtx
42 | * @return 0 成功,非0 失败
43 | */
44 | public static int attachMe(Context appCtx){
45 | Nativee.before_attach();
46 | Bundle extras = new Bundle();
47 | extras.putInt(MTSTCProvider.PARAMS_NEW_COMPONENT_PID, Process.myPid());
48 | int code = 0;
49 | Log.e("zzz","attachMe:" + "content://"+appCtx.getPackageName()+".component.MTSTCProvider/");
50 | Bundle ret = appCtx.getContentResolver().call(Uri.parse("content://"+appCtx.getPackageName()+".component.MTSTCProvider/"), MTSTCProvider.METHOD_ATTACH_NEW_COMPONENT,null,extras);
51 | if (ret != null){
52 | code = ret.getInt(MTSTCProvider.RET_PARAMS_CODE,-1);
53 | if (code == 0){
54 | Logger.d("trace success,pid:"+Process.myPid());
55 | }else{
56 | Logger.e("trace error,pid:"+Process.myPid()+",code:"+code);
57 | }
58 | }else {
59 | Logger.e("trace error bundle is null,pid:"+Process.myPid());
60 | return ERRNO_MAX + 1;
61 | }
62 | Logger.d("tracee init");
63 | return code;
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/library/src/main/java/com/iofomo/opensrc/abyss/sdk/component/MTSTCProvider.java:
--------------------------------------------------------------------------------
1 | package com.iofomo.opensrc.abyss.sdk.component;
2 |
3 | import android.content.ContentProvider;
4 | import android.content.ContentValues;
5 | import android.database.Cursor;
6 | import android.net.Uri;
7 | import android.os.Bundle;
8 |
9 | import com.iofomo.opensrc.abyss.sdk.Logger;
10 | import com.iofomo.opensrc.abyss.sdk.Native;
11 |
12 | public class MTSTCProvider extends ContentProvider {
13 | private static final String TAG = MTSTCProvider.class.getSimpleName();
14 |
15 |
16 | public static final String METHOD_ATTACH_NEW_COMPONENT = "1";
17 | public static final String PARAMS_NEW_COMPONENT_PID = "params_pid";
18 | public static final String RET_PARAMS_CODE = "ret_params_code";
19 | public MTSTCProvider() {
20 |
21 | }
22 |
23 | @Override
24 | public int delete(Uri uri, String selection, String[] selectionArgs) {
25 | // Implement this to handle requests to delete one or more rows.
26 | throw new UnsupportedOperationException("Not yet implemented");
27 | }
28 |
29 | @Override
30 | public String getType(Uri uri) {
31 | // at the given URI.
32 | throw new UnsupportedOperationException("Not yet implemented");
33 | }
34 |
35 | @Override
36 | public Uri insert(Uri uri, ContentValues values) {
37 | throw new UnsupportedOperationException("Not yet implemented");
38 | }
39 |
40 | @Override
41 | public boolean onCreate() {
42 | MTSTCService.start(getContext());
43 | Native.init();
44 | return true;
45 | }
46 |
47 | @Override
48 | public Cursor query(Uri uri, String[] projection, String selection,
49 | String[] selectionArgs, String sortOrder) {
50 | throw new UnsupportedOperationException("Not yet implemented");
51 | }
52 |
53 | @Override
54 | public int update(Uri uri, ContentValues values, String selection,
55 | String[] selectionArgs) {
56 | throw new UnsupportedOperationException("Not yet implemented");
57 | }
58 |
59 | @Override
60 | public Bundle call(String method, String arg, Bundle extras) {
61 | if (METHOD_ATTACH_NEW_COMPONENT.equals(method)){
62 | int pid = extras.getInt(PARAMS_NEW_COMPONENT_PID,-1);
63 | if (pid == -1) return null;
64 | // Logger.d(TAG,"waitForDebugger --------------------");
65 | // Debug.waitForDebugger();
66 | Logger.e(TAG,"pid:"+pid);
67 |
68 | int retCode = Native.trace_pid(pid);
69 | Logger.d(TAG,"retCode:"+retCode);
70 | Bundle ret = new Bundle();
71 | ret.putInt(RET_PARAMS_CODE,retCode);
72 | return ret;
73 | }
74 | return super.call(method, arg, extras);
75 | }
76 |
77 |
78 | }
--------------------------------------------------------------------------------
/library/src/main/java/com/iofomo/opensrc/abyss/sdk/component/MTSTCService.java:
--------------------------------------------------------------------------------
1 | package com.iofomo.opensrc.abyss.sdk.component;
2 |
3 | import android.app.Service;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.os.IBinder;
7 |
8 | import com.iofomo.opensrc.abyss.sdk.Logger;
9 |
10 | public class MTSTCService extends Service {
11 | private static final String TAG = "PtService";
12 |
13 | public static void start(Context ctx){
14 | Intent intent = new Intent(ctx, MTSTCService.class);
15 | if (ctx.startService(intent) == null){
16 | Logger.e("startService error---");
17 | }
18 | }
19 |
20 | public MTSTCService() {
21 | }
22 |
23 | @Override
24 | public void onCreate() {
25 | super.onCreate();
26 | Logger.d(TAG,"onCreate----");
27 | }
28 |
29 | @Override
30 | public IBinder onBind(Intent intent) {
31 | Logger.d(TAG,"onBind----");
32 | return null;
33 | }
34 |
35 | @Override
36 | public int onStartCommand(Intent intent, int flags, int startId) {
37 | Logger.d(TAG,"onStartCommand----");
38 | return START_STICKY;
39 | }
40 | }
--------------------------------------------------------------------------------
/library/src/main/jni/Android.mk:
--------------------------------------------------------------------------------
1 | LOCAL_PATH := $(call my-dir)
2 |
3 | include $(CLEAR_VARS)
4 |
5 | LOCAL_MODULE := loader
6 |
7 | LOCAL_C_INCLUDES := $(LOCAL_PATH)/.
8 | LOCAL_SRC_FILES := loader/loader.c
9 | LOCAL_CFLAGS := -fPIC -ffreestanding -mregparm=3
10 | ifeq ($(TARGET_ARCH_ABI), armeabi-v7a)
11 | LOCAL_LDFLAGS := -static -nostdlib -Wl,-Ttext=0x10000000,-z,noexecstack
12 | else ifeq ($(TARGET_ARCH_ABI), arm64-v8a)
13 | LOCAL_LDFLAGS := -static -nostdlib -Wl,-Ttext=0x2000000000,-z,noexecstack
14 | else
15 | endif
16 | include $(BUILD_EXECUTABLE)
--------------------------------------------------------------------------------
/library/src/main/jni/Application.mk:
--------------------------------------------------------------------------------
1 | APP_ABI := arm64-v8a armeabi-v7a
2 | APP_PLATFORM := android-14
3 |
--------------------------------------------------------------------------------
/library/src/main/jni/CMakeLists.txt:
--------------------------------------------------------------------------------
1 |
2 | # For more information about using CMake with Android Studio, read the
3 | # documentation: https://d.android.com/studio/projects/add-native-code.html
4 |
5 | # Sets the minimum version of CMake required to build the native library.
6 |
7 | cmake_minimum_required(VERSION 3.6.0)
8 |
9 | # Declares and names the project.
10 |
11 | project("library")
12 |
13 | set(CMAKE_ANDROID_ARCH_ABI "armeabi-v7a")
14 | set(CMAKE_ANDROID_ARCH_ABI "arm64-v8a")
15 | # Creates and names a library, sets it as either STATIC
16 | # or SHARED, and provides the relative paths to its source code.
17 | # You can define multiple libraries, and CMake builds them for you.
18 | # Gradle automatically packages shared libraries with your APK.
19 |
20 | file(GLOB SOURCE_FILES
21 | cmn/cmn_vlarray.c
22 | cmn/cmn_pthread_cond.c
23 | cmn/cmn_back_call_stack.c
24 | cmn/cmn_proc.c
25 | cmn/cmn_utils.c
26 | ptrace/wait.c
27 | ptrace/ptrace.c
28 | syscall/sysnum.c
29 | syscall/seccomp.c
30 | syscall/syscall.c
31 | syscall/enter.c
32 | syscall/exit.c
33 | syscall/chain.c
34 | execve/elf.c
35 | execve/enter.c
36 | execve/exit.c
37 | execve/auxv.c
38 | execve/shebang.c
39 | # execve/aoxp.c
40 | path/path.c
41 | path/temp.c
42 | tracee/mem.c
43 | tracee/tracee.c
44 | tracee/reg.c
45 | tracee/event.c
46 | library.c
47 | note.c)
48 |
49 |
50 |
51 |
52 |
53 | include_directories(.)
54 | include_directories(./tracee)
55 |
56 | # Searches for a specified prebuilt library and stores the path as a
57 | # variable. Because CMake includes system libraries in the search path by
58 | # default, you only need to specify the name of the public NDK library
59 | # you want to add. CMake verifies that the library exists before
60 | # completing its build.
61 |
62 | find_library( # Sets the name of the path variable.
63 | log-lib
64 |
65 | # Specifies the name of the NDK library that
66 | # you want CMake to locate.
67 | log )
68 |
69 | # Specifies libraries CMake should link to your target library. You
70 | # can link multiple libraries, such as libraries you define in this
71 | # build script, prebuilt third-party libraries, or system libraries.
72 |
73 |
74 |
75 | add_library( # Sets the name of the library.
76 | abyss
77 | SHARED
78 | # Provides a relative path to your source file(s).
79 | ${SOURCE_FILES}
80 | )
81 |
82 | target_link_libraries( # Specifies the target library.
83 | abyss
84 | # Links the target library to the log library
85 | # included in the NDK.
86 | ${log-lib} )
87 |
88 | #----结束生成lirary 库
89 |
90 | add_executable(
91 | testsvc
92 | test/main.c
93 | ${SOURCE_FILES}
94 | )
95 |
96 | target_link_libraries( # Specifies the target library.
97 | testsvc
98 |
99 | # Links the target library to the log library
100 | # included in the NDK.
101 | ${log-lib} )
102 |
103 | #add_executable(
104 | # crashdemo
105 | # test/crashdemo.c
106 | #)
107 | #
108 | #add_executable(
109 | # test1
110 | # test/test.c
111 | #)
112 | #
113 | #add_executable(
114 | # test2
115 | # test/test2.c
116 | # cmn/cmn_pthread_cond.c
117 | #)
118 | #
119 |
120 |
121 | add_executable(
122 | multithreaddemo
123 | test/multithreaddemo.c
124 | ${SOURCE_FILES}
125 | )
126 | target_link_libraries( # Specifies the target library.
127 | multithreaddemo
128 |
129 | # Links the target library to the log library
130 | # included in the NDK.
131 | ${log-lib} )
132 |
133 | add_executable(
134 | myecho
135 | test/myecho.c
136 | )
137 |
138 | add_executable(
139 | fopen_demo
140 | test/fopen_demo.c
141 | )
142 |
143 | add_executable(
144 | execvedemo
145 | test/execvedemo.c
146 | ${SOURCE_FILES}
147 | )
148 | target_link_libraries( # Specifies the target library.
149 | execvedemo
150 | # Links the target library to the log library
151 | # included in the NDK.
152 | ${log-lib} )
153 |
154 |
155 | add_executable(
156 | ptraceemudemo
157 | test/ptraceemudemo.c
158 | ${SOURCE_FILES}
159 | )
160 | target_link_libraries( # Specifies the target library.
161 | ptraceemudemo
162 | # Links the target library to the log library
163 | # included in the NDK.
164 | ${log-lib} )
165 |
166 | add_executable(
167 | ptracedemo
168 | test/ptrace/ptracedemo.cpp
169 | test/ptrace/inject.cpp
170 | )
171 | target_link_libraries( # Specifies the target library.
172 | ptracedemo
173 | # Links the target library to the log library
174 | # included in the NDK.
175 | ${log-lib} )
--------------------------------------------------------------------------------
/library/src/main/jni/attribute.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef ATTRIBUTE_H
24 | #define ATTRIBUTE_H
25 |
26 | #define UNUSED __attribute__((unused))
27 | #define FORMAT(a, b, c) __attribute__ ((format (a, b, c)))
28 | #define DONT_INSTRUMENT __attribute__((no_instrument_function))
29 | #define PACKED __attribute__((packed))
30 | #define WEAK __attribute__((weak))
31 |
32 | #endif /* ATTRIBUTE_H */
33 |
--------------------------------------------------------------------------------
/library/src/main/jni/build.h:
--------------------------------------------------------------------------------
1 | /* This file is auto-generated, edit at your own risk. */
2 | #ifndef BUILD_H
3 | #define BUILD_H
4 | #undef VERSION
5 | #define VERSION "v5.4.0-5f780cba"
6 | //新的linux api,__ANDROID_API__ >= 23时可以
7 | #define HAVE_PROCESS_VM
8 | #define HAVE_SECCOMP_FILTER
9 |
10 | //是否使用模版的loader程序来载入exe
11 | //#define USE_LOADER_EXE
12 |
13 | //是否处理系统调用
14 | #define HANDLE_SYSCALL
15 |
16 | //仅仅为了方便调试的代码(为了排查问题,可能会拖慢效率)
17 | #define DEBUG_ONLY
18 |
19 | //是否启用日志
20 | #define ENABLE_LOG
21 | //使用printf代替android_log_print
22 | //#define LOG_PRINTF
23 |
24 | #endif /* BUILD_H */
25 |
--------------------------------------------------------------------------------
/library/src/main/jni/cmn/cmn_back_call_stack.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/15.
3 | //
4 |
5 | #ifndef CMN_BACK_CALL_STACK_H
6 | #define CMN_BACK_CALL_STACK_H
7 | #include
8 | #include
9 | #include
10 | #include "tracee/tracee.h"
11 |
12 |
13 | /**
14 | * 打印native异常栈
15 | */
16 | void sig_handler_call_stack(int sig, siginfo_t *info, void *context);
17 |
18 | #if defined(__aarch64__)
19 | /**
20 | * tracer打印tracee的堆栈
21 | */
22 | void print_remote_call_stack_arm64(const Tracee *tracee);
23 | #endif
24 |
25 | #endif //CMN_BACK_CALL_STACK_H
26 |
--------------------------------------------------------------------------------
/library/src/main/jni/cmn/cmn_proc.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/19.
3 | //
4 |
5 | #include
6 | #include
7 | #include "cmn_proc.h"
8 | #include "../constant.h"
9 |
10 | void print_maps(char *filter){
11 | FILE * fp = fopen("/proc/self/maps","r");
12 | if (!fp){
13 | LOGE("fp is null")
14 | return;
15 | }
16 | LOGD("start print %d maps,filter:%s",getpid(),filter);
17 | size_t len = 0;
18 | ssize_t nread;
19 | char * line = NULL;
20 |
21 | while ((nread = getline(&line, &len, fp)) != -1) {
22 | // printf("Retrieved line of length %zu:\n", nread);
23 | line[nread] = '\0';
24 | if (filter != NULL && strlen(filter) > 0){
25 | if (strstr(line,filter) != NULL){
26 | LOGD("proc_maps:%s",line)
27 | }
28 | }else{
29 | LOGD("proc_maps:%s",line)
30 | }
31 | }
32 | fclose(fp);
33 | LOGD("end print %d maps",getpid());
34 | }
--------------------------------------------------------------------------------
/library/src/main/jni/cmn/cmn_proc.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/19.
3 | //
4 |
5 | #ifndef CMN_PROC_H
6 | #define CMN_PROC_H
7 | /**
8 | * 打印maps
9 | */
10 | void print_maps(char *filter);
11 | #endif //CMN_PROC_H
12 |
--------------------------------------------------------------------------------
/library/src/main/jni/cmn/cmn_pthread_cond.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/14.
3 | //
4 |
5 |
6 | #include "cmn_pthread_cond.h"
7 |
8 |
9 | /**
10 | * 初始化
11 | */
12 | void cmn_pt_context_init(struct PCond_Context* ctx){
13 | pthread_mutex_init(&ctx->mutex, NULL);
14 | pthread_cond_init(&ctx->cond, NULL);
15 | }
16 |
17 | /**
18 | * 等待事件发生
19 | */
20 | void cmn_pt_wait(struct PCond_Context* ctx){
21 | pthread_mutex_lock(&ctx->mutex);
22 | pthread_cond_wait(&ctx->cond, &ctx->mutex);
23 | pthread_mutex_unlock(&ctx->mutex);
24 | }
25 |
26 | void cmn_pt_wait2(struct PCond_Context* ctx,bool (*check)()){
27 | pthread_mutex_lock(&ctx->mutex);
28 | if (check()){
29 | pthread_cond_wait(&ctx->cond, &ctx->mutex);
30 | }
31 | pthread_mutex_unlock(&ctx->mutex);
32 | }
33 |
34 | /**
35 | * 通知所有等待线程
36 | */
37 | int cmn_pt_broadcast(struct PCond_Context* ctx){
38 | return pthread_cond_broadcast(&ctx->cond);
39 | }
40 |
41 | /**
42 | * 通知第一个等待的线程
43 | */
44 | int cmn_pt_signal(struct PCond_Context* ctx){
45 | return pthread_cond_signal(&ctx->cond);
46 | }
47 |
48 | int cmn_pt_signal2(struct PCond_Context* ctx,bool (*check)()){
49 | pthread_mutex_lock(&ctx->mutex);
50 | int ret = 0;
51 | if (check()){
52 | ret = pthread_cond_signal(&ctx->cond);
53 | }
54 | pthread_mutex_unlock(&ctx->mutex);
55 | return ret;
56 | }
57 |
58 |
59 | /**
60 | * 清除资源
61 | */
62 | int cmn_pt_destroy(struct PCond_Context* ctx){
63 | return pthread_cond_destroy(&ctx->cond);
64 | }
--------------------------------------------------------------------------------
/library/src/main/jni/cmn/cmn_pthread_cond.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/14.
3 | //
4 |
5 | #ifndef CMN_PTHREAD_COND_H
6 | #define CMN_PTHREAD_COND_H
7 | #include
8 | #include
9 |
10 | struct PCond_Context{
11 | pthread_mutex_t mutex;
12 | pthread_cond_t cond;
13 | };
14 |
15 | /**
16 | * 初始化
17 | */
18 | void cmn_pt_context_init(struct PCond_Context* ctx);
19 |
20 | /**
21 | * 等待事件发生
22 | */
23 | void cmn_pt_wait(struct PCond_Context* ctx);
24 |
25 | void cmn_pt_wait2(struct PCond_Context* ctx,bool (*check)());
26 |
27 | /**
28 | * 通知所有等待线程
29 | */
30 | int cmn_pt_broadcast(struct PCond_Context* ctx);
31 |
32 | /**
33 | * 通知第一个等待的线程
34 | */
35 | int cmn_pt_signal(struct PCond_Context* ctx);
36 |
37 | int cmn_pt_signal2(struct PCond_Context* ctx,bool (*check)());
38 |
39 |
40 | /**
41 | * 清除资源
42 | */
43 | int cmn_pt_destroy(struct PCond_Context* ctx);
44 |
45 |
46 | #endif //CMN_PTHREAD_COND_H
47 |
--------------------------------------------------------------------------------
/library/src/main/jni/cmn/cmn_utils.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2024/1/15.
3 | //
4 |
5 | #include "cmn_utils.h"
6 | #include
7 | #include
8 | #include
9 | #include //AT_*
10 |
11 | bool cmn_utils_string_starts_With(const char* str,const char* sub_str){
12 | if (!str) return !sub_str;
13 | if(!sub_str) return false;
14 | size_t str_len = strlen(str);
15 | size_t sub_str_len = strlen(sub_str);
16 | if (!str_len) return !sub_str_len;
17 | return strstr(str,sub_str) == str;
18 | }
19 |
20 | bool cmn_utils_string_ends_With(const char* str,const char* sub_str){
21 | if (!str) return !sub_str;
22 | if(!sub_str) return false;
23 | size_t str_len = strlen(str);
24 | size_t sub_str_len = strlen(sub_str);
25 | if (!str_len) return !sub_str_len;
26 | if (str_len < sub_str_len) return false;
27 | return !strncmp(&str[str_len - sub_str_len],sub_str,str_len - sub_str_len);
28 | }
29 |
30 |
31 | char* cmn_utils_string_replace_With(const char* str,const char* sub_str,const char* rep_str){
32 | //不支持NULL的替换
33 | if (!sub_str || !rep_str) return NULL;
34 | size_t sub_str_len = strlen(sub_str);
35 | size_t rep_str_len = strlen(rep_str);
36 | size_t str_len = strlen(str);
37 |
38 | const char* find = strstr(str,sub_str);
39 | char *ret = NULL;
40 | if (!find){
41 | ret = malloc(sizeof(char)*sizeof(str_len + 1));
42 | strcpy(ret,str);
43 | return ret;
44 | }
45 | ret = malloc(sizeof(char)*sizeof(str_len + rep_str_len - sub_str_len));
46 | strncpy(ret,str,find - str);
47 | strncpy(&ret[find - str],rep_str,rep_str_len);
48 | strcpy(&ret[find - str + rep_str_len],&find[sub_str_len]);
49 | return ret;
50 | }
51 |
52 |
53 |
54 |
55 | ssize_t cmn_utils_readlink(pid_t pid,int fd, char* buf,size_t max_size) {
56 | int ret = 0;
57 | sprintf(buf, "/proc/%d/fd/%d",pid,fd);
58 | ret = readlinkat(AT_FDCWD, buf, buf, max_size);
59 | if (0 <= ret) {
60 | buf[ret] = '\0';
61 | }
62 | return ret;
63 | }
64 |
65 | int cmn_utils_string_split(char* src, char ch, char* items[], int items_len) {
66 | if (!src || !items) return 0;
67 |
68 | int i = 0, cnt = 0;
69 | memset(items, 0, items_len*sizeof(char*));
70 | while (i < items_len && *src) {
71 | if (!items[i]) {
72 | ++ cnt;
73 | items[i] = src;
74 | }
75 | if (*src == ch) {
76 | ++ i;
77 | *src = '\0';
78 | }
79 | ++ src;
80 | }
81 | return cnt;
82 | }
83 | //字符串hash函数
84 | uint32_t cmn_utils_str_hash(const char* str){
85 | uint32_t h = 0, g;
86 | const char* ptr = str;
87 | while (*ptr) {
88 | h = (h << 4) + *ptr++;
89 | g = h & 0xf0000000;
90 | h ^= g;
91 | h ^= g >> 24;
92 | }
93 | return h;
94 | }
--------------------------------------------------------------------------------
/library/src/main/jni/cmn/cmn_utils.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2024/1/15.
3 | //
4 |
5 | #ifndef CMN_UTILS_H
6 | #define CMN_UTILS_H
7 | #include
8 | #include
9 | #include
10 |
11 | bool cmn_utils_string_starts_With(const char* str,const char* sub_str);
12 | bool cmn_utils_string_ends_With(const char* str,const char* sub_str);
13 | char* cmn_utils_string_replace_With(const char* str,const char* sub_str,const char* rep_str);
14 | ssize_t cmn_utils_readlink(pid_t pid,int fd, char* buf,size_t max_size);
15 | int cmn_utils_string_split(char* src, char ch, char* items[], int items_len);
16 | uint32_t cmn_utils_str_hash(const char* str);
17 |
18 | #endif //CMN_UTILS_H
19 |
--------------------------------------------------------------------------------
/library/src/main/jni/cmn/cmn_vlarray.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/13.
3 | //
4 |
5 | #include "cmn_vlarray.h"
6 | #include
7 |
8 | static int head_size(){
9 | return sizeof(int);
10 | }
11 |
12 | static void* org_ptr(void * ctx){
13 | return ctx - head_size();
14 | }
15 |
16 | static void* arr_ptr(void* org_ptr){
17 | return org_ptr + head_size();
18 | }
19 | static void set_arr_length(void* org_ptr,int size){
20 | int* size_ptr = org_ptr;
21 | *size_ptr = size;
22 | }
23 |
24 | void * vl_new_array(int ele_size,int count){
25 | int cap = ele_size * count + head_size();
26 | void * ptr = malloc(cap);
27 | if (!ptr) return NULL;
28 | set_arr_length(ptr,count);
29 | return arr_ptr(ptr);
30 | }
31 |
32 | //获取数组长度
33 | int vl_array_length(void * ctx){
34 | int * ptr = org_ptr(ctx);
35 | return *ptr;
36 | }
37 |
38 | //改变数组长度
39 | void * vl_array_realloc(void * ctx,int ele_size,int count){
40 | int except = ele_size * count + head_size();
41 | void * ptr = realloc(org_ptr(ctx),except);
42 | if (!ptr) return NULL;
43 | set_arr_length(ptr,count);
44 | return arr_ptr(ptr);
45 | }
46 |
47 | void vl_array_free(void * ctx){
48 | free(org_ptr(ctx));
49 | }
50 |
--------------------------------------------------------------------------------
/library/src/main/jni/cmn/cmn_vlarray.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/13.
3 | //
4 |
5 | #ifndef CMN_VLARRAY_H
6 | #define CMN_VLARRAY_H
7 | /**
8 | * 内存结构"size(int) + [e1,e2...en]"
9 | eg.
10 | struct AAA{
11 | int a;
12 | int b;
13 | };
14 | void * ctx = vl_new_array(sizeof(struct AAA),0);
15 | printf("数组长度:%d\n", vl_array_length(ctx));
16 | ctx = vl_array_realloc(ctx, sizeof(struct AAA),5);
17 | printf("数组长度:%d\n", vl_array_length(ctx));
18 | struct AAA * arr = ctx;
19 | printf("数组3,a=%d,b=%d\n",arr[3].a,arr[3].b);
20 | for (int i = 0; i < vl_array_length(ctx); ++i) {
21 | arr[i].a = arr[i].b = i;
22 | }
23 | printf("数组3,a=%d,b=%d\n",arr[3].a,arr[3].b);
24 | vl_array_free(ctx);
25 | */
26 |
27 | void * vl_new_array(int ele_size,int count);
28 |
29 | int vl_array_length(void * ctx);
30 |
31 |
32 | void * vl_array_realloc(void * ctx,int ele_size,int count);
33 |
34 | void vl_array_free(void * ctx);
35 |
36 | #endif //CMN_VLARRAY_H
37 |
--------------------------------------------------------------------------------
/library/src/main/jni/constant.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/11.
3 | //
4 |
5 | #ifndef INTERCEPTSYSCALL_CONSTANT_H
6 | #define INTERCEPTSYSCALL_CONSTANT_H
7 | #include "build.h"
8 | #include "library.h"
9 |
10 | #include
11 | #include
12 | #include
13 |
14 | #define TAG "INTERCEPT"
15 | #define TAG_SYS "INTERCEPT/SYS"
16 | #define TAG_SYSW "INTERCEPT/SYSW"
17 | #define TAG_SYSE "INTERCEPT/SYSE"
18 |
19 | #if defined(ENABLE_LOG)
20 |
21 | #if defined(LOG_PRINTF)
22 |
23 | //set_seccomp_filters信息
24 | #define LOGSECOMP(...) { printf(__VA_ARGS__);printf("\n");}
25 | //#define LOGSECOMP(...) {}
26 |
27 | #define LOGD(...) { printf(__VA_ARGS__);printf("\n");}
28 | #define LOGI(...) { printf(__VA_ARGS__);printf("\n");}
29 | #define LOGW(...) { printf(__VA_ARGS__);printf("\n");}
30 | #define LOGE(...) { printf(__VA_ARGS__);printf("\n");}
31 |
32 |
33 | #define LOGSYS(...) { printf(__VA_ARGS__);printf("\n");}
34 | #define LOGSYSW(...) { printf(__VA_ARGS__);printf("\n");}
35 | #define LOGSYSE(...) { printf(__VA_ARGS__);printf("\n");}
36 | #else
37 |
38 | //set_seccomp_filters信息
39 | #define LOGSECOMP(...) { __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__);}
40 | //#define LOGSECOMP(...) {}
41 |
42 | #define LOGD(...) { __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__);}
43 | #define LOGI(...) { __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__);}
44 | #define LOGW(...) { __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__);}
45 | #define LOGE(...) { __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__);}
46 |
47 |
48 | #define LOGSYS(...) { __android_log_print(ANDROID_LOG_ERROR,TAG_SYS,__VA_ARGS__);}
49 | #define LOGSYSW(...) { __android_log_print(ANDROID_LOG_ERROR,TAG_SYSW,__VA_ARGS__);}
50 | #define LOGSYSE(...) { __android_log_print(ANDROID_LOG_ERROR,TAG_SYSE,__VA_ARGS__);}
51 | #endif
52 | #else
53 |
54 | //set_seccomp_filters信息
55 | #define LOGSECOMP(...) {}
56 |
57 | #define LOGD(...) {}
58 | #define LOGI(...) {}
59 | #define LOGW(...) {}
60 | #define LOGE(...) {}
61 |
62 | #define LOGSYS(...) {}
63 | #define LOGSYSW(...) {}
64 | #define LOGSYSE(...) {}
65 |
66 | #endif
67 |
68 |
69 | #define __LIKELY(x) __builtin_expect(!!(x), true)
70 | #define __UNLIKELY(x) __builtin_expect(!!(x), false)
71 |
72 |
73 | typedef enum readlink_type{
74 | BUSINESS, //业务逻辑数据
75 | NORMAL, //常规的tracee调用
76 | }readlink_type;
77 |
78 | typedef struct readlink_context{
79 | readlink_type type;
80 | void* data;
81 | }readlink_context;
82 |
83 |
84 | typedef void (*on_sys_event_t)(syscall_data* data);
85 |
86 | extern on_sys_event_t global_on_sysenter;
87 | extern on_sys_event_t global_on_sysexit;
88 |
89 | extern pthread_t work_tid; //工作线程pid
90 | extern pid_t snew_attach_pid;
91 | extern struct PCond_Context spctx;
92 |
93 | #endif //INTERCEPTSYSCALL_CONSTANT_H
94 |
--------------------------------------------------------------------------------
/library/src/main/jni/execve/aoxp.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef AOXP_H
24 | #define AOXP_H
25 |
26 | #include
27 |
28 | #include "tracee/reg.h"
29 | #include "arch.h"
30 |
31 | typedef struct array_of_xpointers ArrayOfXPointers;
32 | typedef int (*read_xpointee_t)(ArrayOfXPointers *array, size_t index, void **object);
33 | typedef int (*write_xpointee_t)(ArrayOfXPointers *array, size_t index, const void *object);
34 | typedef int (*compare_xpointee_t)(ArrayOfXPointers *array, size_t index, const void *reference);
35 | typedef int (*sizeof_xpointee_t)(ArrayOfXPointers *array, size_t index);
36 |
37 | typedef struct mixed_pointer XPointer;
38 | struct array_of_xpointers {
39 | XPointer *_xpointers;
40 | size_t length;
41 |
42 | read_xpointee_t read_xpointee;
43 | write_xpointee_t write_xpointee;
44 | compare_xpointee_t compare_xpointee;
45 | sizeof_xpointee_t sizeof_xpointee;
46 | };
47 |
48 | static inline int read_xpointee(ArrayOfXPointers *array, size_t index, void **object)
49 | {
50 | return array->read_xpointee(array, index, object);
51 | }
52 |
53 | static inline int write_xpointee(ArrayOfXPointers *array, size_t index, const void *object)
54 | {
55 | return array->write_xpointee(array, index, object);
56 | }
57 |
58 | static inline int compare_xpointee(ArrayOfXPointers *array, size_t index, const void *reference)
59 | {
60 | return array->compare_xpointee(array, index, reference);
61 | }
62 |
63 | static inline int sizeof_xpointee(ArrayOfXPointers *array, size_t index)
64 | {
65 | return array->sizeof_xpointee(array, index);
66 | }
67 |
68 | extern int find_xpointee(ArrayOfXPointers *array, const void *reference);
69 | extern int resize_array_of_xpointers(ArrayOfXPointers *array, size_t index, ssize_t nb_delta_entries);
70 | extern int fetch_array_of_xpointers(Tracee *tracee, ArrayOfXPointers **array, Reg reg, size_t nb_entries);
71 | extern int push_array_of_xpointers(ArrayOfXPointers *array, Reg reg);
72 |
73 | extern int read_xpointee_as_object(ArrayOfXPointers *array, size_t index, void **object);
74 | extern int read_xpointee_as_string(ArrayOfXPointers *array, size_t index, char **string);
75 | extern int write_xpointee_as_string(ArrayOfXPointers *array, size_t index, const char *string);
76 | extern int write_xpointees(ArrayOfXPointers *array, size_t index, size_t nb_xpointees, ...);
77 | extern int compare_xpointee_generic(ArrayOfXPointers *array, size_t index, const void *reference);
78 | extern int sizeof_xpointee_as_string(ArrayOfXPointers *array, size_t index);
79 |
80 | #endif /* AOXP_H */
81 |
--------------------------------------------------------------------------------
/library/src/main/jni/execve/auxv.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef AUXV
24 | #define AUXV
25 |
26 | #include "tracee/tracee.h"
27 | #include "arch.h"
28 |
29 | typedef struct elf_aux_vector {
30 | word_t type;
31 | word_t value;
32 | } ElfAuxVector;
33 |
34 | extern word_t get_elf_aux_vectors_address(const Tracee *tracee);
35 | extern ElfAuxVector *fetch_elf_aux_vectors(const Tracee *tracee, word_t address);
36 | extern int add_elf_aux_vector(ElfAuxVector **vectors, word_t type, word_t value);
37 | extern int push_elf_aux_vectors(const Tracee* tracee, ElfAuxVector *vectors, word_t address);
38 |
39 | #endif /* AUXV */
40 |
--------------------------------------------------------------------------------
/library/src/main/jni/execve/execve.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef EXECVE_H
24 | #define EXECVE_H
25 |
26 | #include /* PATH_MAX, */
27 |
28 | #include "tracee/tracee.h"
29 | #include "execve/elf.h"
30 | #include "arch.h"
31 |
32 | extern int translate_execve_enter(Tracee *tracee);
33 | extern void translate_execve_exit(Tracee *tracee);
34 | extern int translate_and_check_exec(Tracee *tracee, char host_path[PATH_MAX], const char *user_path);
35 |
36 | typedef struct mapping {
37 | word_t addr;
38 | word_t length;
39 | word_t clear_length;
40 | word_t prot;
41 | word_t flags;
42 | word_t fd;
43 | word_t offset;
44 | } Mapping;
45 |
46 | typedef struct load_info {
47 | char *host_path;
48 | char *user_path;
49 | char *raw_path;
50 | Mapping *mappings;
51 | ElfHeader elf_header;
52 | bool needs_executable_stack;
53 |
54 | struct load_info *interp;
55 | } LoadInfo;
56 |
57 | #define IS_NOTIFICATION_PTRACED_LOAD_DONE(tracee) ( \
58 | (tracee)->as_ptracee.ptracer != NULL \
59 | && peek_reg((tracee), ORIGINAL, SYSARG_1) == (word_t) 1 \
60 | && peek_reg((tracee), ORIGINAL, SYSARG_4) == (word_t) 2 \
61 | && peek_reg((tracee), ORIGINAL, SYSARG_5) == (word_t) 3 \
62 | && peek_reg((tracee), ORIGINAL, SYSARG_6) == (word_t) 4)
63 |
64 | #endif /* EXECVE_H */
65 |
--------------------------------------------------------------------------------
/library/src/main/jni/execve/ldso.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef LDSO_H
24 | #define LDSO_H
25 |
26 | #include
27 | #include
28 |
29 | #include "execve/aoxp.h"
30 | #include "execve/elf.h"
31 |
32 | extern int ldso_env_passthru(const Tracee *tracee, ArrayOfXPointers *envp, ArrayOfXPointers *argv,
33 | const char *define, const char *undefine, size_t offset);
34 |
35 | extern int rebuild_host_ldso_paths(Tracee *tracee, const char t_program[PATH_MAX],
36 | ArrayOfXPointers *envp);
37 |
38 | extern int compare_xpointee_env(ArrayOfXPointers *envp, size_t index, const char *name);
39 |
40 | extern bool is_env_name(const char *variable, const char *name);
41 |
42 | #endif /* LDSO_H */
43 |
--------------------------------------------------------------------------------
/library/src/main/jni/execve/shebang.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef SHEBANG_H
24 | #define SHEBANG_H
25 |
26 | #include /* PATH_MAX, ARG_MAX, */
27 |
28 | #include "tracee/tracee.h"
29 |
30 | extern int expand_shebang(Tracee *tracee, char host_path[PATH_MAX], char user_path[PATH_MAX]);
31 |
32 | #endif /* SHEBANG_H */
33 |
--------------------------------------------------------------------------------
/library/src/main/jni/library.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/15.
3 | //
4 |
5 | #ifndef LIBRARY_H
6 | #define LIBRARY_H
7 |
8 | #include
9 | #include
10 | #include "syscall/seccomp.h"
11 |
12 |
13 |
14 | #define FILTERED_SYSNUM(SYSNUM) { PR_ ## SYSNUM, 0 }
15 | #include "syscall/sysnum.h"
16 | #include "syscall/seccomp.h"
17 |
18 | typedef struct syscall_data {
19 | //tracee的进程id
20 | pid_t tracee_pid;
21 | //系统调用号
22 | Sysnum sysnum;
23 | //系统调用参数
24 | // unsigned long sysargs[6];
25 | //内部数据,请勿修改
26 | void* _internal;
27 | //上下文数据。使用者可以在系统调用进入前赋值,然后系统调用返回后自己使用
28 | void* user_context_data;
29 | //业务数据
30 | }syscall_data;
31 |
32 | //readlinkat传过来的业务数据
33 | typedef struct business_data{
34 | //tracee的进程id
35 | pid_t tracee_pid;
36 | char data_in[PATH_MAX]; //传入的数据信息
37 | char result[PATH_MAX]; //响应结果
38 | }business_data;
39 |
40 | bool tracee_init(FilteredSysnum* add_filtered_sysnums,bool exclude_libc);
41 |
42 | void tracer_init();
43 | int trace_new_pid(int pid);
44 | //系统调用进入时回调(在tracer进程)
45 | void set_syscall_event_callback(void (*on_sysenter)(syscall_data* data),void (*on_sysexit)(syscall_data* data));
46 | /**
47 | * 内存、寄存器操作
48 | */
49 |
50 | word_t syscall_peek_reg(const syscall_data* sysdata,Reg reg);
51 | void syscall_poke_reg(const syscall_data* sysdata,Reg reg, word_t value);
52 | /**
53 | * 复制位于tracer中的tracer_ptr指向的data_size字节到tracee进程内。并让寄存器reg指向该buffer。
54 | */
55 | int syscall_set_sysarg_data(const syscall_data* sysdata,const void *tracer_ptr, word_t data_size, Reg reg);
56 |
57 | //赋值寄存器的值为c字符串
58 | int syscall_set_sysarg_str(const syscall_data* sysdata,const char* tracer_ptr, Reg reg);
59 | //从寄存器里获取数据缓冲区
60 | int syscall_get_sysarg_data(const syscall_data* sysdata, char* dest_tracer,word_t max_size, Reg reg);
61 | //从寄存器里获取c字符串值
62 | int syscall_get_sysarg_str(const syscall_data* sysdata, char* dest_tracer,word_t max_size, Reg reg);
63 |
64 | int syscall_write_data(const syscall_data* sysdata, word_t dest_tracee, const void *src_tracer, word_t size);
65 |
66 | //int syscall_read_data(const syscall_data sysdata, void *dest_tracer, word_t src_tracee, word_t size);
67 | //
68 | //int syscall_read_string(const syscall_data sysdata, char *dest_tracer, word_t src_tracee, word_t max_size);
69 |
70 | #endif //LIBRARY_H
71 |
--------------------------------------------------------------------------------
/library/src/main/jni/loader/assembly-arm.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | /* According to the ARM EABI, all registers have undefined values at
24 | * program startup except:
25 | *
26 | * - the instruction pointer (r15)
27 | * - the stack pointer (r13)
28 | * - the rtld_fini pointer (r0)
29 | */
30 | #define BRANCH(stack_pointer, destination) do { \
31 | asm volatile ( \
32 | "// Restore initial stack pointer. \n\t" \
33 | "mov sp, %0 \n\t" \
34 | " \n\t" \
35 | "// Clear rtld_fini. \n\t" \
36 | "mov r0, #0 \n\t" \
37 | " \n\t" \
38 | "// Start the program. \n\t" \
39 | "mov pc, %1 \n" \
40 | : /* no output */ \
41 | : "r" (stack_pointer), "r" (destination) \
42 | : "memory", "sp", "r0", "pc"); \
43 | __builtin_unreachable(); \
44 | } while (0)
45 |
46 | #define PREPARE_ARGS_1(arg1_) \
47 | register word_t arg1 asm("r0") = arg1_; \
48 |
49 | #define PREPARE_ARGS_3(arg1_, arg2_, arg3_) \
50 | PREPARE_ARGS_1(arg1_) \
51 | register word_t arg2 asm("r1") = arg2_; \
52 | register word_t arg3 asm("r2") = arg3_; \
53 |
54 | #define PREPARE_ARGS_6(arg1_, arg2_, arg3_, arg4_, arg5_, arg6_) \
55 | PREPARE_ARGS_3(arg1_, arg2_, arg3_) \
56 | register word_t arg4 asm("r3") = arg4_; \
57 | register word_t arg5 asm("r4") = arg5_; \
58 | register word_t arg6 asm("r5") = arg6_;
59 |
60 | #define OUTPUT_CONTRAINTS_1 \
61 | "r" (arg1)
62 |
63 | #define OUTPUT_CONTRAINTS_3 \
64 | OUTPUT_CONTRAINTS_1, \
65 | "r" (arg2), "r" (arg3)
66 |
67 | #define OUTPUT_CONTRAINTS_6 \
68 | OUTPUT_CONTRAINTS_3, \
69 | "r" (arg4), "r" (arg5), "r" (arg6)
70 |
71 | #define SYSCALL(number_, nb_args, args...) \
72 | ({ \
73 | register word_t number asm("r7") = number_; \
74 | register word_t result asm("r0"); \
75 | PREPARE_ARGS_##nb_args(args) \
76 | asm volatile ( \
77 | "svc #0x00000000 \n\t" \
78 | : "=r" (result) \
79 | : "r" (number), \
80 | OUTPUT_CONTRAINTS_##nb_args \
81 | : "memory"); \
82 | result; \
83 | })
84 |
85 | #define OPEN 5
86 | #define CLOSE 6
87 | #define MMAP 192
88 | #define MMAP_OFFSET_SHIFT 12
89 | #define EXECVE 11
90 | #define EXIT 1
91 | #define PRCTL 172
92 | #define MPROTECT 125
93 |
94 |
--------------------------------------------------------------------------------
/library/src/main/jni/loader/assembly-arm64.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | /* According to the ARM64 EABI, all registers have undefined values at
24 | * program startup except:
25 | *
26 | * - the instruction pointer (pc)
27 | * - the stack pointer (sp)
28 | * - the rtld_fini pointer (x0)
29 | */
30 | #define BRANCH(stack_pointer, destination) do { \
31 | asm volatile ( \
32 | "// Restore initial stack pointer. \n\t" \
33 | "mov sp, %0 \n\t" \
34 | " \n\t" \
35 | "// Clear rtld_fini. \n\t" \
36 | "mov x0, #0 \n\t" \
37 | " \n\t" \
38 | "// Start the program. \n\t" \
39 | "br %1 \n" \
40 | : /* no output */ \
41 | : "r" (stack_pointer), "r" (destination) \
42 | : "memory", "sp", "x0"); \
43 | __builtin_unreachable(); \
44 | } while (0)
45 |
46 | #define PREPARE_ARGS_1(arg1_) \
47 | register word_t arg1 asm("x0") = arg1_; \
48 |
49 | #define PREPARE_ARGS_3(arg1_, arg2_, arg3_) \
50 | PREPARE_ARGS_1(arg1_) \
51 | register word_t arg2 asm("x1") = arg2_; \
52 | register word_t arg3 asm("x2") = arg3_; \
53 |
54 | #define PREPARE_ARGS_4(arg1_, arg2_, arg3_, arg4_) \
55 | PREPARE_ARGS_3(arg1_, arg2_, arg3_) \
56 | register word_t arg4 asm("x3") = arg4_; \
57 |
58 | #define PREPARE_ARGS_6(arg1_, arg2_, arg3_, arg4_, arg5_, arg6_) \
59 | PREPARE_ARGS_4(arg1_, arg2_, arg3_, arg4_) \
60 | register word_t arg5 asm("x4") = arg5_; \
61 | register word_t arg6 asm("x5") = arg6_;
62 |
63 | #define OUTPUT_CONTRAINTS_1 \
64 | "r" (arg1)
65 |
66 | #define OUTPUT_CONTRAINTS_3 \
67 | OUTPUT_CONTRAINTS_1, \
68 | "r" (arg2), "r" (arg3)
69 |
70 | #define OUTPUT_CONTRAINTS_4 \
71 | OUTPUT_CONTRAINTS_3, \
72 | "r" (arg4)
73 |
74 | #define OUTPUT_CONTRAINTS_6 \
75 | OUTPUT_CONTRAINTS_4, \
76 | "r" (arg5), "r" (arg6)
77 |
78 | #define SYSCALL(number_, nb_args, args...) \
79 | ({ \
80 | register word_t number asm("x8") = number_; \
81 | register word_t result asm("x0"); \
82 | PREPARE_ARGS_##nb_args(args) \
83 | asm volatile ( \
84 | "svc #0x00000000 \n\t" \
85 | : "=r" (result) \
86 | : "r" (number), \
87 | OUTPUT_CONTRAINTS_##nb_args \
88 | : "memory"); \
89 | result; \
90 | })
91 |
92 | #define OPENAT 56
93 | #define CLOSE 57
94 | #define MMAP 222
95 | #define EXECVE 221
96 | #define EXIT 93
97 | #define PRCTL 167
98 | #define MPROTECT 226
99 |
--------------------------------------------------------------------------------
/library/src/main/jni/loader/assembly-x86.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | /* According to the x86 ABI, all registers have undefined values at
24 | * program startup except:
25 | *
26 | * - the instruction pointer (rip)
27 | * - the stack pointer (rsp)
28 | * - the rtld_fini pointer (rdx)
29 | * - the system flags (eflags)
30 | */
31 | #define BRANCH(stack_pointer, destination) do { \
32 | asm volatile ( \
33 | "// Restore initial stack pointer. \n\t" \
34 | "movl %0, %%esp \n\t" \
35 | " \n\t" \
36 | "// Clear state flags. \n\t" \
37 | "pushl $0 \n\t" \
38 | "popfl \n\t" \
39 | " \n\t" \
40 | "// Clear rtld_fini. \n\t" \
41 | "movl $0, %%edx \n\t" \
42 | " \n\t" \
43 | "// Start the program. \n\t" \
44 | "jmpl *%%eax \n" \
45 | : /* no output */ \
46 | : "irm" (stack_pointer), "a" (destination) \
47 | : "memory", "cc", "esp", "edx"); \
48 | __builtin_unreachable(); \
49 | } while (0)
50 |
51 | extern word_t syscall_6(word_t number,
52 | word_t arg1, word_t arg2, word_t arg3,
53 | word_t arg4, word_t arg5, word_t arg6);
54 |
55 | extern word_t syscall_3(word_t number, word_t arg1, word_t arg2, word_t arg3);
56 |
57 | extern word_t syscall_1(word_t number, word_t arg1);
58 |
59 | #define SYSCALL(number, nb_args, args...) syscall_##nb_args(number, args)
60 |
61 | #define OPEN 5
62 | #define CLOSE 6
63 | #define MMAP 192
64 | #define MMAP_OFFSET_SHIFT 12
65 | #define EXECVE 11
66 | #define EXIT 1
67 | #define PRCTL 172
68 | #define MPROTECT 125
69 |
--------------------------------------------------------------------------------
/library/src/main/jni/loader/assembly-x86_64.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | /* According to the x86_64 ABI, all registers have undefined values at
24 | * program startup except:
25 | *
26 | * - the instruction pointer (rip)
27 | * - the stack pointer (rsp)
28 | * - the rtld_fini pointer (rdx)
29 | * - the system flags (rflags)
30 | */
31 | #define BRANCH(stack_pointer, destination) do { \
32 | asm volatile ( \
33 | "// Restore initial stack pointer. \n\t" \
34 | "movq %0, %%rsp \n\t" \
35 | " \n\t" \
36 | "// Clear state flags. \n\t" \
37 | "pushq $0 \n\t" \
38 | "popfq \n\t" \
39 | " \n\t" \
40 | "// Clear rtld_fini. \n\t" \
41 | "movq $0, %%rdx \n\t" \
42 | " \n\t" \
43 | "// Start the program. \n\t" \
44 | "jmpq *%%rax \n" \
45 | : /* no output */ \
46 | : "irm" (stack_pointer), "a" (destination) \
47 | : "memory", "cc", "rsp", "rdx"); \
48 | __builtin_unreachable(); \
49 | } while (0)
50 |
51 | #define PREPARE_ARGS_1(arg1_) \
52 | register word_t arg1 asm("rdi") = arg1_; \
53 |
54 | #define PREPARE_ARGS_3(arg1_, arg2_, arg3_) \
55 | PREPARE_ARGS_1(arg1_) \
56 | register word_t arg2 asm("rsi") = arg2_; \
57 | register word_t arg3 asm("rdx") = arg3_; \
58 |
59 | #define PREPARE_ARGS_6(arg1_, arg2_, arg3_, arg4_, arg5_, arg6_) \
60 | PREPARE_ARGS_3(arg1_, arg2_, arg3_) \
61 | register word_t arg4 asm("r10") = arg4_; \
62 | register word_t arg5 asm("r8") = arg5_; \
63 | register word_t arg6 asm("r9") = arg6_;
64 |
65 | #define OUTPUT_CONTRAINTS_1 \
66 | "r" (arg1)
67 |
68 | #define OUTPUT_CONTRAINTS_3 \
69 | OUTPUT_CONTRAINTS_1, \
70 | "r" (arg2), "r" (arg3)
71 |
72 | #define OUTPUT_CONTRAINTS_6 \
73 | OUTPUT_CONTRAINTS_3, \
74 | "r" (arg4), "r" (arg5), "r" (arg6)
75 |
76 | #define SYSCALL(number_, nb_args, args...) \
77 | ({ \
78 | register word_t number asm("rax") = number_; \
79 | register word_t result asm("rax"); \
80 | PREPARE_ARGS_##nb_args(args) \
81 | asm volatile ( \
82 | "syscall \n\t" \
83 | : "=r" (result) \
84 | : "r" (number), \
85 | OUTPUT_CONTRAINTS_##nb_args \
86 | : "memory", "cc", "rcx", "r11"); \
87 | result; \
88 | })
89 |
90 | #define OPEN 2
91 | #define CLOSE 3
92 | #define MMAP 9
93 | #define EXECVE 59
94 | #define EXIT 60
95 | #define PRCTL 157
96 | #define MPROTECT 10
97 |
--------------------------------------------------------------------------------
/library/src/main/jni/loader/assembly.S:
--------------------------------------------------------------------------------
1 | #if defined(__i386__)
2 | .text
3 |
4 | /*
5 | ABI user-land kernel-land
6 | ====== ========= ===========
7 | number %eax %eax
8 | arg1 %edx %ebx
9 | arg2 %ecx %ecx
10 | arg3 16(%esp) %edx
11 | arg4 12(%esp) %esi
12 | arg5 8(%esp) %edi
13 | arg6 4(%esp) %ebp
14 | result N/A %eax
15 | */
16 | .globl syscall_6
17 | .type syscall_6, @function
18 | syscall_6:
19 | /* Callee-saved registers. */
20 | pushl %ebp // %esp -= 0x04
21 | pushl %edi // %esp -= 0x08
22 | pushl %esi // %esp -= 0x0c
23 | pushl %ebx // %esp -= 0x10
24 |
25 | // mov %eax, %eax // number
26 | mov %edx, %ebx // arg1
27 | // mov %ecx, %ecx // arg2
28 | mov 0x14(%esp), %edx // arg3
29 | mov 0x18(%esp), %esi // arg4
30 | mov 0x1c(%esp), %edi // arg5
31 | mov 0x20(%esp), %ebp // arg6
32 |
33 | int $0x80
34 |
35 | popl %ebx
36 | popl %esi
37 | popl %edi
38 | popl %ebp
39 |
40 | // mov %eax, %eax // result
41 | ret
42 |
43 | .globl syscall_3
44 | .type syscall_3, @function
45 | syscall_3:
46 | pushl %ebx
47 | mov %edx, %ebx
48 | mov 0x8(%esp), %edx
49 | int $0x80
50 | popl %ebx
51 | ret
52 |
53 | .globl syscall_1
54 | .type syscall_1, @function
55 | syscall_1:
56 | pushl %ebx
57 | mov %edx, %ebx
58 | int $0x80
59 | popl %ebx
60 | ret
61 |
62 | #endif /* defined(__i386__) */
63 |
--------------------------------------------------------------------------------
/library/src/main/jni/loader/script.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef SCRIPT
24 | #define SCRIPT
25 |
26 | #include "arch.h"
27 | #include "attribute.h"
28 |
29 | struct load_statement {
30 | word_t action;
31 |
32 | union {
33 | struct {
34 | word_t string_address;
35 | } open;
36 |
37 | struct {
38 | word_t addr;
39 | word_t length;
40 | word_t prot;
41 | word_t offset;
42 | word_t clear_length;
43 | } mmap;
44 |
45 | struct {
46 | word_t start;
47 | } make_stack_exec;
48 |
49 | struct {
50 | word_t stack_pointer;
51 | word_t entry_point;
52 | word_t at_phdr;
53 | word_t at_phent;
54 | word_t at_phnum;
55 | word_t at_entry;
56 | word_t at_execfn;
57 | } start;
58 | };
59 | } PACKED;
60 |
61 | typedef struct load_statement LoadStatement;
62 |
63 | #define LOAD_STATEMENT_SIZE(statement, type) \
64 | (sizeof((statement).action) + sizeof((statement).type))
65 |
66 | /* Don't use enum, since sizeof(enum) doesn't have to be equal to
67 | * sizeof(word_t). Keep values in the same order as their respective
68 | * actions appear in loader.c to get a change GCC produces a jump
69 | * table. */
70 | #define LOAD_ACTION_OPEN_NEXT 0
71 | #define LOAD_ACTION_OPEN 1
72 | #define LOAD_ACTION_MMAP_FILE 2
73 | #define LOAD_ACTION_MMAP_ANON 3
74 | #define LOAD_ACTION_MAKE_STACK_EXEC 4
75 | #define LOAD_ACTION_START_TRACED 5
76 | #define LOAD_ACTION_START 6
77 |
78 | #endif /* SCRIPT */
79 |
--------------------------------------------------------------------------------
/library/src/main/jni/note.c:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #include /* errno, */
24 | #include /* strerror(3), */
25 | #include /* va_*, */
26 | #include /* vfprintf(3), */
27 | #include /* INT_MAX, */
28 |
29 | #include "note.h"
30 | #include "tracee/tracee.h"
31 | #include "constant.h"
32 |
33 | int global_verbose_level;
34 | const char *global_tool_name;
35 |
36 | /**
37 | * Print @message to the standard error stream according to its
38 | * @severity and @origin.
39 | */
40 | void note(const Tracee *tracee, Severity severity, Origin origin, const char *message, ...)
41 | {
42 | const char *tool_name;
43 | va_list extra_params;
44 | int verbose_level;
45 | char line[40] = {0};
46 | char extra_line[1024] = {0};
47 |
48 | if (tracee == NULL) {
49 | verbose_level = global_verbose_level;
50 | tool_name = global_tool_name ?: "";
51 | }
52 | else {
53 | verbose_level = tracee->verbose;
54 | tool_name = tracee->tool_name;
55 | }
56 |
57 | if (verbose_level < 0 && severity != ERROR)
58 | return;
59 |
60 | switch (severity) {
61 | case WARNING:
62 | // LOGW("%s warning: ", tool_name);
63 | snprintf(line, sizeof(line),"%s warning: ", tool_name);
64 | break;
65 |
66 | case ERROR:
67 | // LOGE( "%s error: ", tool_name);
68 | snprintf(line, sizeof(line),"%s error: ", tool_name);
69 |
70 | break;
71 |
72 | case INFO:
73 | default:
74 | // LOGD("%s info: ", tool_name);
75 | snprintf(line, sizeof(line),"%s info: ",tool_name);
76 | break;
77 | }
78 |
79 | if (origin == TALLOC){
80 | snprintf(line,sizeof(line),"talloc:");
81 | }
82 | // LOGE( "talloc: ");
83 |
84 | va_start(extra_params, message);
85 | // vfprintf(stderr, message, extra_params);
86 | vsprintf(extra_line, message, extra_params);
87 | va_end(extra_params);
88 | // char * log = strcat(line,extra_line);
89 | LOGSYS("%s%s",line,extra_line)
90 | switch (origin) {
91 | case SYSTEM:
92 | LOGSYS( ": ");
93 | perror(NULL);
94 | break;
95 |
96 | case TALLOC:
97 | break;
98 |
99 | case INTERNAL:
100 | case USER:
101 | default:
102 | // LOGSYS( "\n");
103 | break;
104 | }
105 |
106 | return;
107 | }
108 |
109 |
--------------------------------------------------------------------------------
/library/src/main/jni/note.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef NOTE_H
24 | #define NOTE_H
25 |
26 | #include "tracee/tracee.h"
27 | #include "attribute.h"
28 |
29 | /* Specify where a notice is coming from. */
30 | typedef enum {
31 | SYSTEM,
32 | INTERNAL,
33 | USER,
34 | TALLOC,
35 | } Origin;
36 |
37 | /* Specify the severity of a notice. */
38 | typedef enum {
39 | ERROR,
40 | WARNING,
41 | INFO,
42 | } Severity;
43 |
44 | #define VERBOSE(tracee, level, message, args...) do { \
45 | if (tracee == NULL || tracee->verbose >= (level)) \
46 | note(tracee, INFO, INTERNAL, (message), ## args); \
47 | } while (0)
48 |
49 | extern void note(const Tracee *tracee, Severity severity, Origin origin, const char *message, ...) FORMAT(printf, 4, 5);
50 |
51 | extern int global_verbose_level;
52 | extern const char *global_tool_name;
53 |
54 | #endif /* NOTE_H */
55 |
--------------------------------------------------------------------------------
/library/src/main/jni/path/binding.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef BINDING_H
24 | #define BINDING_H
25 |
26 | #include /* PATH_MAX, */
27 | #include
28 |
29 | #include "tracee/tracee.h"
30 | #include "path.h"
31 |
32 | typedef struct binding {
33 | Path host;
34 | Path guest;
35 |
36 | bool need_substitution;
37 | bool must_exist;
38 |
39 | struct {
40 | CIRCLEQ_ENTRY(binding) pending;
41 | CIRCLEQ_ENTRY(binding) guest;
42 | CIRCLEQ_ENTRY(binding) host;
43 | } link;
44 | } Binding;
45 |
46 | typedef CIRCLEQ_HEAD(bindings, binding) Bindings;
47 |
48 | extern Binding *insort_binding3(const Tracee *tracee, const void *context,
49 | const char host_path[PATH_MAX], const char guest_path[PATH_MAX]);
50 | extern Binding *new_binding(Tracee *tracee, const char *host, const char *guest, bool must_exist);
51 | extern int initialize_bindings(Tracee *tracee);
52 | extern const char *get_path_binding(const Tracee* tracee, Side side, const char path[PATH_MAX]);
53 | extern Binding *get_binding(const Tracee *tracee, Side side, const char path[PATH_MAX]);
54 | extern const char *get_root(const Tracee* tracee);
55 | extern int substitute_binding(const Tracee* tracee, Side side, char path[PATH_MAX]);
56 | extern void remove_binding_from_all_lists(const Tracee *tracee, Binding *binding);
57 |
58 | #endif /* BINDING_H */
59 |
--------------------------------------------------------------------------------
/library/src/main/jni/path/canon.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef CANON_H
24 | #define CANON_H
25 |
26 | #include
27 | #include
28 |
29 | #include "tracee/tracee.h"
30 |
31 | extern int canonicalize(Tracee *tracee, const char *user_path, bool deref_final,
32 | char guest_path[PATH_MAX], unsigned int nb_recursion);
33 |
34 | #endif /* CANON_H */
35 |
--------------------------------------------------------------------------------
/library/src/main/jni/path/glue.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef GLUE_H
24 | #define GLUE_H
25 |
26 | #include /* PATH_MAX, */
27 |
28 | #include "tracee/tracee.h"
29 | #include "path.h"
30 |
31 | extern mode_t build_glue(Tracee *tracee, const char *guest_path, char host_path[PATH_MAX],
32 | Finality finality);
33 |
34 | #endif /* GLUE_H */
35 |
--------------------------------------------------------------------------------
/library/src/main/jni/path/path.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef PATH_H
24 | #define PATH_H
25 |
26 | #include /* pid_t, */
27 | #include /* AT_FDCWD, */
28 | #include /* PATH_MAX, */
29 | #include
30 |
31 | #include "tracee/tracee.h"
32 |
33 | /* File type. */
34 | typedef enum {
35 | REGULAR,
36 | SYMLINK,
37 | } Type;
38 |
39 | /* Path point-of-view. */
40 | typedef enum {
41 | GUEST,
42 | HOST,
43 |
44 | /* Used for bindings as specified by the user but not
45 | * canonicalized yet (new_binding, initialize_binding). */
46 | PENDING,
47 | } Side;
48 |
49 | /* Path with cached attributes. */
50 | typedef struct {
51 | char path[PATH_MAX];
52 | size_t length;
53 | Side side;
54 | } Path;
55 |
56 | /* Path ending type. */
57 | typedef enum {
58 | NOT_FINAL,
59 | FINAL_NORMAL,
60 | FINAL_SLASH,
61 | FINAL_DOT
62 | } Finality;
63 |
64 | #define IS_FINAL(a) ((a) != NOT_FINAL)
65 |
66 | /* Comparison between two paths. */
67 | typedef enum Comparison {
68 | PATHS_ARE_EQUAL,
69 | PATH1_IS_PREFIX,
70 | PATH2_IS_PREFIX,
71 | PATHS_ARE_NOT_COMPARABLE,
72 | } Comparison;
73 |
74 | extern int which(Tracee *tracee, const char *paths, char host_path[PATH_MAX], const char *command);
75 | extern int realpath2(Tracee *tracee, char host_path[PATH_MAX], const char *path, bool deref_final);
76 | extern int getcwd2(Tracee *tracee, char guest_path[PATH_MAX]);
77 | extern void chop_finality(char *path);
78 |
79 | extern int translate_path(Tracee *tracee, char host_path[PATH_MAX],
80 | int dir_fd, const char *guest_path, bool deref_final);
81 |
82 | extern int detranslate_path(Tracee *tracee, char path[PATH_MAX], const char t_referrer[PATH_MAX]);
83 | extern bool belongs_to_guestfs(const Tracee *tracee, const char *path);
84 |
85 | extern int join_paths(int number_paths, char result[PATH_MAX], ...);
86 | extern int list_open_fd(const Tracee *tracee);
87 |
88 | extern Comparison compare_paths(const char *path1, const char *path2);
89 | extern Comparison compare_paths2(const char *path1, size_t length1, const char *path2, size_t length2);
90 |
91 | extern size_t substitute_path_prefix(char path[PATH_MAX], size_t old_prefix_length,
92 | const char *new_prefix, size_t new_prefix_length);
93 |
94 | extern int readlink_proc_pid_fd(pid_t pid, int fd, char path[PATH_MAX]);
95 |
96 | /* Check if path interpretable relatively to dirfd, see openat(2) for details. */
97 | #define AT_FD(dirfd, path) ((dirfd) != AT_FDCWD && ((path) != NULL && (path)[0] != '/'))
98 |
99 | #endif /* PATH_H */
100 |
--------------------------------------------------------------------------------
/library/src/main/jni/path/proc.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef PROC_H
24 | #define PROC_H
25 |
26 | #include
27 |
28 | #include "tracee/tracee.h"
29 | #include "path/path.h"
30 |
31 | /* Action to do after a call to readlink_proc(). */
32 | typedef enum {
33 | DEFAULT, /* Nothing special to do, treat it as a regular link. */
34 | CANONICALIZE, /* The symlink was dereferenced, now canonicalize it. */
35 | DONT_CANONICALIZE, /* The symlink shouldn't be dereferenced nor canonicalized. */
36 | } Action;
37 |
38 |
39 | extern Action readlink_proc(const Tracee *tracee, char result[PATH_MAX], const char path[PATH_MAX],
40 | const char component[NAME_MAX], Comparison comparison);
41 |
42 | extern ssize_t readlink_proc2(const Tracee *tracee, char result[PATH_MAX], const char path[PATH_MAX]);
43 |
44 | #endif /* PROC_H */
45 |
--------------------------------------------------------------------------------
/library/src/main/jni/path/temp.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef TEMP_H
24 | #define TEMP_H
25 |
26 | //#include
27 |
28 | extern char *create_temp_name(void *context, const char *prefix);
29 | extern const char *create_temp_directory(void *context, const char *prefix);
30 | extern const char *create_temp_file(void *context, const char *prefix);
31 | extern FILE* open_temp_file(void *context, const char *prefix);
32 | extern const char *get_temp_directory();
33 |
34 | #endif /* TEMP_H */
35 |
--------------------------------------------------------------------------------
/library/src/main/jni/ptrace/ptrace.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef PTRACE_H
24 | #define PTRACE_H
25 |
26 | #include "tracee/tracee.h"
27 |
28 | extern int translate_ptrace_enter(Tracee *tracee);
29 | extern int translate_ptrace_exit(Tracee *tracee);
30 | extern void attach_to_ptracer(Tracee *ptracee, Tracee *ptracer);
31 | extern void detach_from_ptracer(Tracee *ptracee);
32 |
33 | #define PTRACEE (ptracee->as_ptracee)
34 | #define PTRACER (ptracer->as_ptracer)
35 |
36 | #endif /* PTRACE_H */
37 |
--------------------------------------------------------------------------------
/library/src/main/jni/ptrace/user.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #include
24 | #include
25 | #include
26 |
27 | #include "arch.h"
28 | #include "attribute.h"
29 |
30 | #if defined(ARCH_X86_64)
31 |
32 | #define USER32_NB_REGS 17
33 | #define USER32_NB_FPREGS 27
34 |
35 | extern word_t convert_user_offset(word_t offset);
36 | extern void convert_user_regs_struct(bool reverse, uint64_t *user_regs64,
37 | uint32_t user_regs32[USER32_NB_REGS]);
38 |
39 | #else
40 |
41 | #define USER32_NB_REGS 0
42 | #define USER32_NB_FPREGS 0
43 |
44 | static inline word_t convert_user_offset(word_t offset UNUSED)
45 | {
46 | assert(0);
47 | }
48 |
49 | static inline void convert_user_regs_struct(bool reverse UNUSED,
50 | uint64_t *user_regs64 UNUSED,
51 | uint32_t user_regs32[USER32_NB_REGS] UNUSED)
52 | {
53 | assert(0);
54 | }
55 |
56 | #endif
57 |
--------------------------------------------------------------------------------
/library/src/main/jni/ptrace/wait.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef PTRACE_WAIT_H
24 | #define PTRACE_WAIT_H
25 |
26 | #include "tracee/tracee.h"
27 |
28 | extern int translate_wait_enter(Tracee *ptracer);
29 | extern int translate_wait_exit(Tracee *ptracer, bool *set_result);
30 | extern bool handle_ptracee_event(Tracee *ptracee, int wait_status);
31 |
32 | /* __WCLONE: Wait for "clone" children only. If omitted then wait for
33 | * "non-clone" children only. (A "clone" child is one which delivers
34 | * no signal, or a signal other than SIGCHLD to its parent upon
35 | * termination.) This option is ignored if __WALL is also specified.
36 | *
37 | * __WALL: Wait for all children, regardless of type ("clone" or
38 | * "non-clone").
39 | *
40 | * -- wait(2) man-page
41 | */
42 | #define EXPECTED_WAIT_CLONE(wait_options, tracee) \
43 | ((((wait_options) & __WALL) != 0) \
44 | || ((((wait_options) & __WCLONE) != 0) && (tracee)->clone) \
45 | || ((((wait_options) & __WCLONE) == 0) && !(tracee)->clone))
46 |
47 | #endif /* PTRACE_WAIT_H */
48 |
--------------------------------------------------------------------------------
/library/src/main/jni/syscall/chain.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef CHAIN_H
24 | #define CHAIN_H
25 |
26 | #include "tracee/tracee.h"
27 | #include "syscall/sysnum.h"
28 | #include "arch.h"
29 |
30 | extern int register_chained_syscall(Tracee *tracee, Sysnum sysnum,
31 | word_t sysarg_1, word_t sysarg_2, word_t sysarg_3,
32 | word_t sysarg_4, word_t sysarg_5, word_t sysarg_6);
33 |
34 | extern void force_chain_final_result(Tracee *tracee, word_t forced_result);
35 |
36 | extern int restart_original_syscall(Tracee *tracee);
37 |
38 | extern void chain_next_syscall(Tracee *tracee);
39 |
40 |
41 | #endif /* CHAIN_H */
42 |
--------------------------------------------------------------------------------
/library/src/main/jni/syscall/heap.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef HEAP_H
24 | #define HEAP_H
25 |
26 | #include "tracee/tracee.h"
27 |
28 | extern void translate_brk_enter(Tracee *tracee);
29 | extern void translate_brk_exit(Tracee *tracee);
30 |
31 | #endif /* HEAP_H */
32 |
--------------------------------------------------------------------------------
/library/src/main/jni/syscall/rlimit.c:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #include /* bool, */
24 | #include /* prlimit(2), */
25 | #include /* prlimit(2), */
26 |
27 | #include "tracee/tracee.h"
28 | #include "tracee/reg.h"
29 | #include "tracee/mem.h"
30 | #include "tracee/abi.h"
31 | #include "cli/note.h"
32 |
33 | /**
34 | * Set PRoot's stack soft limit to @tracee's one if this latter is
35 | * greater. This allows to workaround a Linux kernel bug that
36 | * prevents a tracer to access a tracee's stack beyond its last mapped
37 | * page, as it might by the case under PRoot. This function returns
38 | * -errno if an error occurred, otherwise 0.
39 | *
40 | * Details: when a tracer tries to access a tracee's stack beyond its
41 | * last mapped page, the Linux kernel should be able to increase
42 | * tracee's stack up to its soft limit. Unfortunately the Linux
43 | * kernel checks the limit of the tracer instead the limit of the
44 | * tracee. This bug was exposed using UMEQ under PRoot.
45 | *
46 | * Ref.: https://bugzilla.kernel.org/show_bug.cgi?id=91791
47 | *
48 | * Three strategies were possible:
49 | *
50 | * - set PRoot's stack soft limit to the hard limit; this might make
51 | * the system collapse if PRoot starts to recurses indefinitely.
52 | *
53 | * - as it's done here; this appears to be a good compromise between
54 | * the strategy above and the one below.
55 | *
56 | * - as it's done here + reduce PRoot's stack soft limit as soon as
57 | * it's possible; this would be overly complicated.
58 | */
59 | int translate_setrlimit_exit(const Tracee *tracee, bool is_prlimit)
60 | {
61 | struct rlimit proot_stack;
62 | word_t resource;
63 | word_t address;
64 | word_t tracee_stack_limit;
65 | Reg sysarg;
66 | int status;
67 |
68 | sysarg = (is_prlimit ? SYSARG_2 : SYSARG_1);
69 |
70 | resource = peek_reg(tracee, ORIGINAL, sysarg);
71 | address = peek_reg(tracee, ORIGINAL, sysarg + 1);
72 |
73 | /* Not the resource we're looking for? */
74 | if (resource != RLIMIT_STACK)
75 | return 0;
76 |
77 | /* Retrieve new tracee's stack limit. */
78 | if (is_prlimit) {
79 | /* Not the prlimit usage we're looking for? */
80 | if (address == 0)
81 | return 0;
82 |
83 | tracee_stack_limit = peek_uint64(tracee, address);
84 | }
85 | else {
86 | tracee_stack_limit = peek_word(tracee, address);
87 |
88 | /* Convert this special value from 32-bit to 64-bit,
89 | * if needed. */
90 | if (is_32on64_mode(tracee) && tracee_stack_limit == (uint32_t) -1)
91 | tracee_stack_limit = RLIM_INFINITY;
92 | }
93 | if (errno != 0)
94 | return -errno;
95 |
96 | /* Get current PRoot's stack limit. */
97 | status = prlimit(0, RLIMIT_STACK, NULL, &proot_stack);
98 | if (status < 0) {
99 | VERBOSE(tracee, 1, "can't get stack limit.");
100 | return 0; /* Not fatal. */
101 | }
102 |
103 | /* No need to increase current PRoot's stack limit? */
104 | if (proot_stack.rlim_cur >= tracee_stack_limit)
105 | return 0;
106 |
107 | proot_stack.rlim_cur = tracee_stack_limit;
108 |
109 | /* Increase current PRoot's stack limit. */
110 | status = prlimit(0, RLIMIT_STACK, &proot_stack, NULL);
111 | if (status < 0)
112 | VERBOSE(tracee, 1, "can't set stack limit.");
113 | return 0; /* Not fatal. */
114 |
115 | VERBOSE(tracee, 1, "stack soft limit increased to %ld bytes", proot_stack.rlim_cur);
116 | return 0;
117 | }
118 |
--------------------------------------------------------------------------------
/library/src/main/jni/syscall/rlimit.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef RLIMIT_H
24 | #define RLIMIT_H
25 |
26 | #include
27 | #include "tracee/tracee.h"
28 |
29 | extern int translate_setrlimit_exit(const Tracee *tracee, bool is_prlimit);
30 |
31 | #endif /* RLIMIT_H */
32 |
--------------------------------------------------------------------------------
/library/src/main/jni/syscall/seccomp.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef SECCOMP_H
24 | #define SECCOMP_H
25 |
26 | #include "syscall/sysnum.h"
27 | #include "tracee/tracee.h"
28 | #include "attribute.h"
29 | #include "arch.h"
30 |
31 | typedef struct {
32 | Sysnum value;
33 | word_t flags;
34 | } FilteredSysnum;
35 |
36 | typedef struct {
37 | unsigned int value;
38 | size_t nb_abis;
39 | Abi abis[NB_MAX_ABIS];
40 | } SeccompArch;
41 |
42 | #define FILTERED_SYSNUM_END { PR_void, 0 }
43 |
44 | #define FILTER_SYSEXIT 0x1
45 |
46 | extern int enable_syscall_filtering(const Tracee *tracee,FilteredSysnum* add_filtered_sysnums,bool exclude_libc);
47 |
48 | #endif /* SECCOMP_H */
49 |
--------------------------------------------------------------------------------
/library/src/main/jni/syscall/socket.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef SOCKET_H
24 | #define SOCKET_H
25 |
26 | #include "arch.h" /* word_t */
27 | #include "tracee/tracee.h"
28 |
29 | int translate_socketcall_enter(Tracee *tracee, word_t *sock_addr, int size);
30 | int translate_socketcall_exit(Tracee *tracee, word_t sock_addr, word_t size_addr, word_t max_size);
31 |
32 | #endif /* SOCKET_H */
33 |
--------------------------------------------------------------------------------
/library/src/main/jni/syscall/syscall.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef SYSCALL_H
24 | #define SYSCALL_H
25 |
26 | #include /* PATH_MAX, */
27 |
28 | #include "tracee/tracee.h"
29 | #include "tracee/reg.h"
30 |
31 | extern int get_sysarg_path(const Tracee *tracee, char path[PATH_MAX], Reg reg);
32 | extern int set_sysarg_path(Tracee *tracee, const char path[PATH_MAX], Reg reg);
33 |
34 | extern int set_sysarg_data(Tracee *tracee, const void *tracer_ptr, word_t size, Reg reg);
35 |
36 | extern void translate_syscall(Tracee *tracee);
37 | extern int translate_syscall_enter(Tracee *tracee);
38 | extern void translate_syscall_exit(Tracee *tracee);
39 |
40 | #endif /* SYSCALL_H */
41 |
--------------------------------------------------------------------------------
/library/src/main/jni/syscall/sysnum.c:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #include
24 |
25 | #include "syscall/sysnum.h"
26 | #include "tracee/tracee.h"
27 | #include "tracee/abi.h"
28 | #include "tracee/reg.h"
29 | #include "arch.h"
30 | //#include "cli/note.h"
31 |
32 | #include SYSNUMS_HEADER1
33 |
34 | #ifdef SYSNUMS_HEADER2
35 | #include SYSNUMS_HEADER2
36 | #endif
37 |
38 | #ifdef SYSNUMS_HEADER3
39 | #include SYSNUMS_HEADER3
40 | #endif
41 |
42 | typedef struct {
43 | const Sysnum *table;
44 | word_t offset;
45 | word_t length;
46 | } Sysnums;
47 |
48 | /**
49 | * Update @sysnums' fields with the sysnum table for the given @abi.
50 | */
51 | static void get_sysnums(Abi abi, Sysnums *sysnums)
52 | {
53 | switch (abi) {
54 | case ABI_DEFAULT:
55 | sysnums->table = SYSNUMS_ABI1;
56 | sysnums->length = sizeof(SYSNUMS_ABI1) / sizeof(Sysnum);
57 | sysnums->offset = 0;
58 | return;
59 | #ifdef SYSNUMS_ABI2
60 | case ABI_2:
61 | sysnums->table = SYSNUMS_ABI2;
62 | sysnums->length = sizeof(SYSNUMS_ABI2) / sizeof(Sysnum);
63 | sysnums->offset = 0;
64 | return;
65 | #endif
66 | #ifdef SYSNUMS_ABI3
67 | case ABI_3:
68 | sysnums->table = SYSNUMS_ABI3;
69 | sysnums->length = sizeof(SYSNUMS_ABI3) / sizeof(Sysnum);
70 | sysnums->offset = 0x40000000; /* x32 */
71 | return;
72 | #endif
73 | default:
74 | assert(0);
75 | }
76 | }
77 |
78 | /**
79 | * Return the neutral value of @sysnum from the given @abi.
80 | */
81 | static Sysnum translate_sysnum(Abi abi, word_t sysnum)
82 | {
83 | Sysnums sysnums;
84 | word_t index;
85 |
86 | get_sysnums(abi, &sysnums);
87 |
88 | /* Sanity checks. */
89 | if (sysnum < sysnums.offset)
90 | return PR_void;
91 |
92 | index = sysnum - sysnums.offset;
93 |
94 | /* Sanity checks. */
95 | if (index > sysnums.length)
96 | return PR_void;
97 |
98 | return sysnums.table[index];
99 | }
100 |
101 | /**
102 | * Return the architecture value of @sysnum for the given @abi.
103 | */
104 | word_t detranslate_sysnum(Abi abi, Sysnum sysnum)
105 | {
106 | Sysnums sysnums;
107 | size_t i;
108 |
109 | /* Very special case. */
110 | if (sysnum == PR_void)
111 | return SYSCALL_AVOIDER;
112 |
113 | get_sysnums(abi, &sysnums);
114 |
115 | for (i = 0; i < sysnums.length; i++) {
116 | if (sysnums.table[i] != sysnum)
117 | continue;
118 |
119 | return i + sysnums.offset;
120 | }
121 |
122 | return SYSCALL_AVOIDER;
123 | }
124 |
125 | /**
126 | * Return the neutral value of the @tracee's current syscall number.
127 | */
128 | Sysnum get_sysnum(const Tracee *tracee, RegVersion version)
129 | {
130 | return translate_sysnum(get_abi(tracee), peek_reg(tracee, version, SYSARG_NUM));
131 | }
132 |
133 | /**
134 | * Overwrite the @tracee's current syscall number with @sysnum. Note:
135 | * this neutral value is automatically converted into the architecture
136 | * value.
137 | */
138 | void set_sysnum(Tracee *tracee, Sysnum sysnum)
139 | {
140 | poke_reg(tracee, SYSARG_NUM, detranslate_sysnum(get_abi(tracee), sysnum));
141 | }
142 |
143 | /**
144 | * Return the human readable name of @sysnum.
145 | */
146 | const char *stringify_sysnum(Sysnum sysnum)
147 | {
148 | #define SYSNUM(item) [ PR_ ## item ] = #item,
149 | static const char *names[] = {
150 | #include "syscall/sysnums.list"
151 | };
152 | #undef SYSNUM
153 |
154 | if (sysnum == 0)
155 | return "void";
156 |
157 | if (sysnum >= PR_NB_SYSNUM)
158 | return "";
159 |
160 | return names[sysnum];
161 | }
162 |
--------------------------------------------------------------------------------
/library/src/main/jni/syscall/sysnum.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef SYSNUM_H
24 | #define SYSNUM_H
25 |
26 | #include
27 |
28 | #include "tracee/tracee.h"
29 | #include "tracee/abi.h"
30 | #include "tracee/reg.h"
31 |
32 | #define SYSNUM(item) PR_ ## item,
33 | typedef enum {
34 | PR_void = 0,
35 | #include "syscall/sysnums.list"
36 | PR_NB_SYSNUM
37 | } Sysnum;
38 | #undef SYSNUM
39 |
40 | extern Sysnum get_sysnum(const Tracee *tracee, RegVersion version);
41 | extern void set_sysnum(Tracee *tracee, Sysnum sysnum);
42 | extern word_t detranslate_sysnum(Abi abi, Sysnum sysnum);
43 | extern const char *stringify_sysnum(Sysnum sysnum);
44 |
45 | #endif /* SYSNUM_H */
46 |
--------------------------------------------------------------------------------
/library/src/main/jni/test/crashdemo.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | void abc(){
4 | long* ptr = 10;
5 | printf("val:%ld\n",*ptr);
6 | }
7 |
8 | int main(){
9 | printf("hi\n");
10 | abc();
11 | return 0;
12 | }
--------------------------------------------------------------------------------
/library/src/main/jni/test/fopen_demo.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/27.
3 | //
4 |
5 | #include
6 | #include
7 |
8 | static FILE * fp;
9 |
10 | void write_line(char* line){
11 | if (fp == NULL){
12 | fp = fopen("./a.txt","a");
13 | }
14 | fwrite(line,strlen(line),1,fp);
15 | fwrite("\n",1,1,fp);
16 | fflush(fp);
17 | }
18 |
19 | int main(int argc,const char * argv[]){
20 | write_line("haha1");
21 | write_line("hahah2");
22 |
23 | char message_line[2000] = {0};
24 | sprintf(message_line,"%s%s","add","fff");
25 | write_line(message_line);
26 | return 0;
27 | }
--------------------------------------------------------------------------------
/library/src/main/jni/test/main.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/15.
3 | //
4 |
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include "library.h"
13 | #include "ptrace/ptrace.h"
14 | #include "constant.h"
15 | #include "event.h"
16 | #include "cmn/cmn_pthread_cond.h"
17 | #include "cmn/cmn_proc.h"
18 |
19 |
20 | #define USE_PTRACE
21 |
22 | //是否多tracee进程
23 | //#define USE_MUL_TRACEE_PROCESS
24 |
25 | static void test(){
26 | //test sigev
27 | // uint64_t* fp = __builtin_frame_address(0);
28 | // LOGD("test cur fp %p",fp);
29 | // long* ptr = 10;
30 | // LOGD("val:%ld\n",*ptr);
31 |
32 | errno = 0;
33 | char file_path[40];
34 | sprintf(file_path,"/proc/%d/maps",getpid());
35 | LOGD("pid:%d,before open:%s",getpid(),file_path) //第二个线程从这里开始没有东西
36 | int fd = open(file_path,O_RDONLY); //有open时 就会报栈破坏,没有就没事
37 | LOGD("pid:%d,after open:%s",getpid(),file_path)
38 |
39 | // LOGI("tracee open fd,%d,err:%d,%s",fd,errno, strerror(errno));
40 | close(fd); //这个貌似被过滤忽略掉了
41 | // char path[40];
42 | // for (int i = 0; i < 10; ++i) {
43 | // sprintf(path,"/proc/self/maps%d",i);
44 | // fd = open(path,O_RDONLY); //有open时 就会报栈破坏,没有就没事
45 | // close(fd);
46 | // sleep(1);
47 | // }
48 | }
49 |
50 | static FilteredSysnum add_filtered_sysnums[] = {
51 | FILTERED_SYSNUM(openat),
52 | FILTERED_SYSNUM(close),
53 | FILTERED_SYSNUM_END
54 | };
55 | int new_child_tracee(int index){
56 | LOGD("new_child_tracee ----,%d",index)
57 | int pipefd_to_parent[2];
58 | int pipefd_to_child[2];
59 | pid_t ppid = getpid();
60 |
61 | if (pipe(pipefd_to_parent) < 0 ||
62 | pipe(pipefd_to_child) < 0){
63 | perror("failed to create pipe");
64 | return -1;
65 | };
66 | //fuck https://blog.csdn.net/qq_42961603/article/details/129236882
67 | //fork后,会复制发起调用的线程
68 | pid_t pid = fork();
69 |
70 | if (pid == 0){ //child tracee
71 | LOGD("new child------,cur pid %d,parent pid %d",getpid(),ppid);
72 | char message[20];
73 | close(pipefd_to_parent[0]);
74 | close(pipefd_to_child[1]);
75 | stpcpy(message,"trace me!");
76 | write(pipefd_to_parent[1],message, sizeof(message));
77 |
78 | LOGD("wait for trace-------%d",getpid())
79 |
80 | int size = read(pipefd_to_child[0],message, sizeof(message)); //TODO 第二个进程,到这出现了signal 5
81 | if (size <= 0){
82 | LOGD("child read error,%d,%d,%s",size,errno, strerror(errno))
83 | return 0;
84 | }
85 | LOGD("finish traced,[%s]",message)
86 | LOGD("child cont,%s-------------------------------------------",message);
87 | close(pipefd_to_parent[1]);
88 | close(pipefd_to_child[0]);
89 | if (index == 1 || index == 2){
90 | #if defined(USE_PTRACE)
91 | tracee_init(add_filtered_sysnums,false);
92 | #endif
93 | }
94 | test();
95 | LOGD("child exit,%d",getpid());
96 | exit(0);
97 | }else if (pid > 0){ //parent tracer
98 | LOGD("new_child_tracee invoked,parent");
99 | char message[20] = "go!";
100 | close(pipefd_to_parent[1]);
101 | close(pipefd_to_child[0]);
102 |
103 | read(pipefd_to_parent[0],message, sizeof(message));
104 | LOGD("received trace request,[%s]",message)
105 | int ret = 0;
106 | if (index == 1 || index == 2){
107 | #if defined(USE_PTRACE)
108 | ret = trace_new_pid(pid);
109 | #endif
110 | }
111 | LOGD("trace finished,notify child")
112 | sprintf(message,"%d",ret);
113 | write(pipefd_to_child[1],message, sizeof(message));
114 | LOGD("resume child:---------------%d",pid);
115 |
116 | close(pipefd_to_parent[0]);
117 | close(pipefd_to_child[1]);
118 |
119 | }else{
120 | printf("error\n");
121 | }
122 | return pid;
123 | }
124 |
125 | void* new_child_tracee_thread(void* data){
126 | LOGD("new_child_tracee_thread----%d",getpid())
127 | pid_t child1 = new_child_tracee(1);
128 | LOGD("new_child_tracee_thread end")
129 | return NULL;
130 | }
131 |
132 | int main(int argc,char * const argv[]){
133 | LOGD("sss testsvc welcome,%d,argc:%d",getpid(),argc);
134 | // print_maps(NULL);
135 | if (argc > 1){
136 | test();
137 | printf("just test done----\n");
138 | return 0;
139 | }
140 | #if defined(USE_PTRACE)
141 | tracer_init();
142 | #endif
143 | LOGD("test before sleep,%d",getpid());
144 | sleep(3);
145 | LOGD("test after sleep,%d",getpid());
146 |
147 | //用新线程去fork
148 | pthread_t tid1;
149 | pthread_create(&tid1,NULL,new_child_tracee_thread,NULL);
150 |
151 | pthread_join(tid1,NULL);
152 |
153 | #if defined(USE_MUL_TRACEE_PROCESS)
154 | LOGD("tid1 done,before child 2---------------------------,%d",getpid())
155 | pthread_t tid2;
156 | pthread_create(&tid2,NULL,new_child_tracee_thread,NULL);
157 | #endif
158 |
159 |
160 | int * ret;
161 | if (pthread_join(work_tid,NULL) != 0){
162 | printf("failed to join,cur_pid:%d\n",getpid());
163 | }
164 |
165 | LOGD("main exit");
166 |
167 | return 0;
168 | }
--------------------------------------------------------------------------------
/library/src/main/jni/test/myecho.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/26.
3 | //
4 |
5 | /* myecho.c */
6 |
7 | #include
8 | #include
9 | #include
10 | #include
11 |
12 | int
13 | main(int argc, char *argv[])
14 | {
15 |
16 | char * path = "/proc/self/mapsecho";
17 | printf("before open /proc/self/mapsecho,%p\n",path);
18 | int fd = open(path,O_RDONLY);
19 | printf("after open /proc/self/mapsecho,%d\n",fd);
20 | close(fd);
21 |
22 | int j;
23 | for (j = 0; j < argc; j++)
24 | printf("argv[%d]: %s\n", j, argv[j]);
25 |
26 | exit(EXIT_SUCCESS);
27 | }
--------------------------------------------------------------------------------
/library/src/main/jni/test/ptrace/inject.h:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2015, Simone 'evilsocket' Margaritelli
2 | Copyright (c) 2015-2019, Jorrit 'Chainfire' Jongma
3 | See LICENSE file for details */
4 |
5 | #ifndef INJECT_H
6 | #define INJECT_H
7 |
8 | #include
9 | #include
10 | #include
11 |
12 |
13 | #if defined(__arm__)
14 | #define CPSR_T_MASK ( 1u << 5 )
15 | #define PARAMS_IN_REGS 4
16 | #elif defined(__aarch64__)
17 | #define CPSR_T_MASK ( 1u << 5 )
18 | #define PARAMS_IN_REGS 8
19 | #define pt_regs user_pt_regs
20 | #define uregs regs
21 | #define ARM_pc pc
22 | #define ARM_sp sp
23 | #define ARM_cpsr pstate
24 | #define ARM_lr regs[30]
25 | #define ARM_r0 regs[0]
26 | #endif
27 |
28 | #if defined(__LP64__)
29 | #define PATH_LINKER_BIONIC "/bionic/bin/linker64"
30 | #define PATH_LIBDL_BIONIC "/bionic/lib64/libdl.so"
31 | #define PATH_LIBC_BIONIC "/bionic/lib64/libc.so"
32 | #define PATH_LINKER "/system/bin/linker64"
33 | #define PATH_LIBDL "/system/lib64/libdl.so"
34 | #define PATH_LIBC "/system/lib64/libc.so"
35 | #define PATH_LIBANDROID_RUNTIME "/system/lib64/libandroid_runtime.so"
36 | #else
37 | #define PATH_LINKER_BIONIC "/bionic/bin/linker"
38 | #define PATH_LIBDL_BIONIC "/bionic/lib/libdl.so"
39 | #define PATH_LIBC_BIONIC "/bionic/lib/libc.so"
40 | #define PATH_LINKER "/system/bin/linker"
41 | #define PATH_LIBDL "/system/lib/libdl.so"
42 | #define PATH_LIBC "/system/lib/libc.so"
43 | #define PATH_LIBANDROID_RUNTIME "/system/lib/libandroid_runtime.so"
44 | #endif
45 |
46 |
47 | // No need to reference manually, use HOOKLOG
48 | //extern const char* _libinject_log_tag;
49 | //extern int _libinject_log;
50 |
51 | // Pass NULL to disable logging
52 | //void libinject_log(const char* log_tag);
53 |
54 |
55 | #define INJECTLOG(F,...) { printf(F,##__VA_ARGS__);printf("\n");}
56 | //#define INJECTLOG(F,...) {}
57 |
58 | // Find pid for process
59 | //pid_t libinject_find_pid_of(const char* process);
60 |
61 | extern pid_t _pid;
62 |
63 | // Load library in process pid, returns 0 on success
64 | int libinject_injectvm(pid_t pid, char* library, char* param);
65 |
66 | void trace_getregs(const char* debug, struct pt_regs * regs);
67 |
68 | #endif
69 |
--------------------------------------------------------------------------------
/library/src/main/jni/test/test.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/21.
3 | //
4 |
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | //堆栈打印
11 | //ARM64
12 |
13 | int def(){
14 | //x29
15 | //打印堆栈
16 | uint64_t* fp = __builtin_frame_address(0);
17 | //TODO 暂时忽略当前地址和寄存器
18 |
19 | //基于bp指针和lr指针的位置关系得到堆栈
20 | while (fp != NULL)
21 | {
22 | // printf("stack:,fp:%p,lr_addr:%p lr:%lx\n",fp,(fp + 1),*((uint64_t *)(fp + 1)));
23 | printf("bt:0x%lx\n",*((uint64_t *)(fp + 1)));
24 | fp = *fp;
25 | }
26 |
27 | printf("bt finished");
28 | return 0;
29 | }
30 |
31 | void abc(){
32 | // long* ptr = 10;
33 | // printf("val:%ld\n",*ptr);
34 |
35 | long ptr = 10;
36 | printf("val:%ld\n",ptr);
37 | def();
38 | //printf("val:%ld\n",++ptr);
39 | }
40 |
41 |
42 |
43 | int main(){
44 | // printf("hi\n");
45 | abc();
46 | }
--------------------------------------------------------------------------------
/library/src/main/jni/test/test2.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/22.
3 | //
4 | //pthread相关研究
5 |
6 | #include "cmn/cmn_pthread_cond.h"
7 | #include
8 | #include
9 | //#include "constant.h"
10 |
11 | #define LOGD(...) { printf(__VA_ARGS__);printf("\n");}
12 |
13 | static struct PCond_Context ctx;
14 |
15 | void* work1(void* data){
16 | LOGD("work1 wait for lock----")
17 | cmn_pt_wait(&ctx);
18 | LOGD("work1 after lock-------")
19 | return NULL;
20 | }
21 |
22 | void* work2(void* data){
23 | LOGD("work2 begin sleep -----")
24 | sleep(3);
25 | LOGD("work2 after sleep,notify")
26 | cmn_pt_signal(&ctx);
27 | LOGD("work2 notify done")
28 | return NULL;
29 | }
30 | int main(int argc,const char * argv[]){
31 | LOGD("test %s",argv[0])
32 | cmn_pt_context_init(&ctx);
33 | pthread_t t1;
34 | pthread_create(&t1,NULL,work1,NULL);
35 |
36 | pthread_t t2;
37 | pthread_create(&t2,NULL,work2,NULL);
38 |
39 | pthread_join(t1,NULL);
40 | pthread_join(t2,NULL);
41 | LOGD("main after thread exit")
42 | cmn_pt_destroy(&ctx);
43 | LOGD("main exit")
44 | return 0;
45 | }
46 |
--------------------------------------------------------------------------------
/library/src/main/jni/test/test3.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by mac on 2023/12/22.
3 | //
4 | /*
5 | * This sample show how to use futex betwen two process, and use system v
6 | * shared memory to store data
7 | */
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include
21 |
22 | #if __GLIBC_PREREQ(2, 3)
23 | #if defined FUTEX_WAIT || defined FUTEX_WAKE
24 | #include
25 | #else
26 | #define FUTEX_WAIT 0
27 | #define FUTEX_WAKE 1
28 | #endif
29 |
30 | #ifndef __NR_futex
31 | #define __NR_futex 202
32 | #endif
33 | #endif
34 |
35 | #define FILE_MODE (S_IRUSR | S_IWUSR)
36 |
37 | const char shmfile[] = "/tmp";
38 | const int size = 100;
39 |
40 | struct namelist
41 | {
42 | int id;
43 | char name[20];
44 | };
45 |
46 | int
47 | main(void)
48 | {
49 | int fd, pid, status;
50 | int *ptr;
51 | struct stat stat;
52 |
53 | // create a Posix shared memory
54 | int flags = O_RDWR | O_CREAT;
55 | fd = shm_open(shmfile, flags, FILE_MODE);
56 | if (fd < 0)
57 | {
58 | printf("shm_open failed, errormsg=%s errno=%d", strerror(errno), errno);
59 | return 0;
60 | }
61 | ftruncate(fd, size);
62 | ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
63 |
64 | pid = fork();
65 | if (pid == 0) { // child process
66 | sleep(5);
67 | printf("Child %d: start/n", getpid());
68 |
69 | fd = shm_open(shmfile, flags, FILE_MODE);
70 | fstat(fd, &stat);
71 | ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
72 | close(fd);
73 | struct namelist tmp;
74 |
75 | // store total num in ptr[0];
76 | *ptr = 3;
77 |
78 | namelist *cur = (namelist *)(ptr+1);
79 |
80 | // store items
81 | tmp.id = 1;
82 | strcpy(tmp.name, "Nellson");
83 | *cur++ = tmp;
84 | tmp.id = 2;
85 | strcpy(tmp.name, "Daisy");
86 | *cur++ = tmp;
87 | tmp.id = 3;
88 | strcpy(tmp.name, "Robbie");
89 | *cur++ = tmp;
90 |
91 | printf("wake up parent/n");
92 | syscall(__NR_futex ,ptr, FUTEX_WAKE, 1, NULL );
93 |
94 | exit(0);
95 | } else{ // parent process
96 | printf("parent start waiting/n");
97 | syscall(__NR_futex , ptr, FUTEX_WAIT, *(int *)ptr, NULL );
98 | printf("parent end waiting/n");
99 |
100 | struct namelist tmp;
101 |
102 | int total = *ptr;
103 | printf("/nThere is %d item in the shm/n", total);
104 |
105 | ptr++;
106 | namelist *cur = (namelist *)ptr;
107 |
108 | for (int i = 0; i< total; i++) {
109 | tmp = *cur;
110 | printf("%d: %s/n", tmp.id, tmp.name);
111 | cur++;
112 | }
113 |
114 | printf("/n");
115 | waitpid(pid, &status, 0);
116 | }
117 |
118 | // remvoe a Posix shared memory from system
119 | printf("Parent %d get child status:%d/n", getpid(), status);
120 | return 0;
121 | }
122 |
--------------------------------------------------------------------------------
/library/src/main/jni/tracee/abi.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef TRACEE_ABI_H
24 | #define TRACEE_ABI_H
25 |
26 | #include
27 | #include /* offsetof(), */
28 |
29 | #include "tracee/tracee.h"
30 | #include "tracee/reg.h"
31 | #include "arch.h"
32 |
33 | #include "attribute.h"
34 |
35 | typedef enum {
36 | ABI_DEFAULT = 0,
37 | ABI_2, /* x86_32 on x86_64. */
38 | ABI_3, /* x32 on x86_64. */
39 | NB_MAX_ABIS,
40 | } Abi;
41 |
42 | /**
43 | * Return the ABI currently used by the given @tracee.
44 | */
45 | #if defined(ARCH_X86_64)
46 | static inline Abi get_abi(const Tracee *tracee)
47 | {
48 | /* The ABI can be changed by a syscall ("execve" typically),
49 | * however the change is only effective once the syscall has
50 | * *fully* returned, hence the use of _regs[ORIGINAL]. */
51 | switch (tracee->_regs[ORIGINAL].cs) {
52 | case 0x23:
53 | return ABI_2;
54 |
55 | case 0x33:
56 | if (tracee->_regs[ORIGINAL].ds == 0x2B)
57 | return ABI_3;
58 | /* Fall through. */
59 | default:
60 | return ABI_DEFAULT;
61 | }
62 | }
63 |
64 | /**
65 | * Return true if @tracee is a 32-bit process running on a 64-bit
66 | * kernel.
67 | */
68 | static inline bool is_32on64_mode(const Tracee *tracee)
69 | {
70 | /* Unlike the ABI, 32-bit/64-bit mode change is effective
71 | * immediately, hence _regs[CURRENT].cs. */
72 | switch (tracee->_regs[CURRENT].cs) {
73 | case 0x23:
74 | return true;
75 |
76 | case 0x33:
77 | if (tracee->_regs[CURRENT].ds == 0x2B)
78 | return true;
79 | /* Fall through. */
80 | default:
81 | return false;
82 | }
83 | }
84 | #elif defined(ARCH_ARM64)
85 | static inline Abi get_abi(const Tracee *tracee)
86 | {
87 | if (tracee->is_aarch32) {
88 | return ABI_2;
89 | }
90 |
91 | return ABI_DEFAULT;
92 | }
93 |
94 | /**
95 | * Return true if @tracee is a 32-bit process running on a 64-bit
96 | * kernel.
97 | */
98 | static inline bool is_32on64_mode(const Tracee *tracee)
99 | {
100 | return tracee->is_aarch32;
101 | }
102 | #else
103 | static inline Abi get_abi(const Tracee *tracee UNUSED)
104 | {
105 | return ABI_DEFAULT;
106 | }
107 |
108 | static inline bool is_32on64_mode(const Tracee *tracee UNUSED)
109 | {
110 | return false;
111 | }
112 | #endif
113 |
114 | /**
115 | * Return the size of a word according to the ABI currently used by
116 | * the given @tracee.
117 | */
118 | static inline size_t sizeof_word(const Tracee *tracee)
119 | {
120 | return (is_32on64_mode(tracee)
121 | ? sizeof(word_t) / 2
122 | : sizeof(word_t));
123 | }
124 |
125 | #include
126 |
127 | /**
128 | * Return the offset of the 'uid' field in a 'stat' structure
129 | * according to the ABI currently used by the given @tracee.
130 | */
131 | static inline off_t offsetof_stat_uid(const Tracee *tracee)
132 | {
133 | return (is_32on64_mode(tracee)
134 | ? OFFSETOF_STAT_UID_32
135 | : offsetof(struct stat, st_uid));
136 | }
137 |
138 | /**
139 | * Return the offset of the 'gid' field in a 'stat' structure
140 | * according to the ABI currently used by the given @tracee.
141 | */
142 | static inline off_t offsetof_stat_gid(const Tracee *tracee)
143 | {
144 | return (is_32on64_mode(tracee)
145 | ? OFFSETOF_STAT_GID_32
146 | : offsetof(struct stat, st_gid));
147 | }
148 |
149 | #endif /* TRACEE_ABI_H */
150 |
--------------------------------------------------------------------------------
/library/src/main/jni/tracee/event.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef TRACEE_EVENT_H
24 | #define TRACEE_EVENT_H
25 |
26 | #include
27 |
28 | #include "tracee/tracee.h"
29 |
30 | extern int launch_process(Tracee *tracee, char *const argv[]);
31 | extern int event_loop();
32 | extern int handle_tracee_event(Tracee *tracee, int tracee_status);
33 | extern bool restart_tracee(Tracee *tracee, int signal);
34 |
35 | #endif /* TRACEE_EVENT_H */
36 |
--------------------------------------------------------------------------------
/library/src/main/jni/tracee/mem.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef TRACEE_MEM_H
24 | #define TRACEE_MEM_H
25 |
26 | #include /* PATH_MAX, */
27 | #include /* pid_t, size_t, */
28 | #include /* pid_t, size_t, */
29 | #include /* struct iovec, */
30 | #include /* ENAMETOOLONG, */
31 |
32 | #include "arch.h" /* word_t, */
33 | #include "tracee/tracee.h"
34 |
35 | extern int write_data(const Tracee *tracee, word_t dest_tracee, const void *src_tracer, word_t size);
36 | extern int writev_data(const Tracee *tracee, word_t dest_tracee, const struct iovec *src_tracer, int src_tracer_count);
37 | extern int read_data(const Tracee *tracee, void *dest_tracer, word_t src_tracee, word_t size);
38 | extern int read_string(const Tracee *tracee, char *dest_tracer, word_t src_tracee, word_t max_size);
39 | extern word_t peek_word(const Tracee *tracee, word_t address);
40 | extern void poke_word(const Tracee *tracee, word_t address, word_t value);
41 | extern word_t alloc_mem(Tracee *tracee, ssize_t size);
42 | extern int clear_mem(const Tracee *tracee, word_t address, size_t size);
43 |
44 | /**
45 | * Copy to @dest_tracer at most PATH_MAX bytes -- including the
46 | * end-of-string terminator -- from the string pointed to by
47 | * @src_tracee within the memory space of the @tracee process. This
48 | * function returns -errno on error, otherwise it returns the number
49 | * in bytes of the string, including the end-of-string terminator.
50 | */
51 | static inline int read_path(const Tracee *tracee, char dest_tracer[PATH_MAX], word_t src_tracee)
52 | {
53 | int status;
54 |
55 | status = read_string(tracee, dest_tracer, src_tracee, PATH_MAX);
56 | if (status < 0)
57 | return status;
58 | if (status >= PATH_MAX)
59 | return -ENAMETOOLONG;
60 |
61 | return status;
62 | }
63 |
64 | /**
65 | * Generate a function that returns the value of the @type at the
66 | * given @address in the @tracee's memory space. The caller must test
67 | * errno to check if an error occured.
68 | */
69 | #define GENERATE_peek(type) \
70 | static inline type ## _t peek_ ## type(const Tracee *tracee, word_t address) \
71 | { \
72 | type ## _t result; \
73 | errno = -read_data(tracee, &result, address, sizeof(type ## _t)); \
74 | return result; \
75 | }
76 |
77 | GENERATE_peek(uint8);
78 | GENERATE_peek(uint16);
79 | GENERATE_peek(uint32);
80 | GENERATE_peek(uint64);
81 |
82 | GENERATE_peek(int8);
83 | GENERATE_peek(int16);
84 | GENERATE_peek(int32);
85 | GENERATE_peek(int64);
86 |
87 | #undef GENERATE_peek
88 |
89 | /**
90 | * Generate a function that set the @type at the given @address in the
91 | * @tracee's memory space to the given @value. The caller must test
92 | * errno to check if an error occured.
93 | */
94 | #define GENERATE_poke(type) \
95 | static inline void poke_ ## type(const Tracee *tracee, word_t address, type ## _t value) \
96 | { \
97 | errno = -write_data(tracee, address, &value, sizeof(type ## _t)); \
98 | }
99 |
100 | GENERATE_poke(uint8);
101 | GENERATE_poke(uint16);
102 | GENERATE_poke(uint32);
103 | GENERATE_poke(uint64);
104 |
105 | GENERATE_poke(int8);
106 | GENERATE_poke(int16);
107 | GENERATE_poke(int32);
108 | GENERATE_poke(int64);
109 |
110 | #undef GENERATE_poke
111 |
112 | #endif /* TRACEE_MEM_H */
113 |
--------------------------------------------------------------------------------
/library/src/main/jni/tracee/reg.h:
--------------------------------------------------------------------------------
1 | /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
2 | *
3 | * This file is part of PRoot.
4 | *
5 | * Copyright (C) 2015 STMicroelectronics
6 | *
7 | * This program is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU General Public License as
9 | * published by the Free Software Foundation; either version 2 of the
10 | * License, or (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful, but
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU General Public License
18 | * along with this program; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 | * 02110-1301 USA.
21 | */
22 |
23 | #ifndef TRACEE_REG_H
24 | #define TRACEE_REG_H
25 |
26 | #include "tracee/tracee.h"
27 | #include "arch.h"
28 |
29 | typedef enum {
30 | SYSARG_NUM = 0,
31 | SYSARG_1,
32 | SYSARG_2,
33 | SYSARG_3,
34 | SYSARG_4,
35 | SYSARG_5,
36 | SYSARG_6,
37 | SYSARG_RESULT,
38 | STACK_POINTER,
39 | INSTR_POINTER,
40 | RTLD_FINI,
41 | STATE_FLAGS,
42 | USERARG_1,
43 | } Reg;
44 |
45 | extern int fetch_regs(Tracee *tracee);
46 | extern int push_regs(Tracee *tracee);
47 |
48 | extern word_t peek_reg(const Tracee *tracee, RegVersion version, Reg reg);
49 | extern void poke_reg(Tracee *tracee, Reg reg, word_t value);
50 |
51 | extern void print_current_regs(Tracee *tracee, int verbose_level, const char *message);
52 | extern void save_current_regs(Tracee *tracee, RegVersion version);
53 |
54 | #endif /* TRACEE_REG_H */
55 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | include ':library'
3 | include ':svcer'
--------------------------------------------------------------------------------
/svcer/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | buildToolsVersion "30.0.3"
5 | compileSdkVersion 30
6 |
7 | defaultConfig {
8 | minSdkVersion 29
9 | targetSdkVersion 30
10 |
11 | externalNativeBuild {
12 | cmake {
13 | cppFlags ''
14 | }
15 | }
16 | ndk {
17 | abiFilters 'armeabi-v7a', 'arm64-v8a'
18 | }
19 | }
20 |
21 | buildTypes {
22 | release {
23 | minifyEnabled false
24 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
25 | jniDebuggable true
26 | debuggable true
27 | }
28 | debug {
29 | jniDebuggable true
30 | }
31 | }
32 | compileOptions {
33 | sourceCompatibility JavaVersion.VERSION_1_8
34 | targetCompatibility JavaVersion.VERSION_1_8
35 | }
36 | }
37 |
38 | dependencies {
39 | }
--------------------------------------------------------------------------------
/svcer/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/svcer/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/svcer/src/main/jni/Android.mk:
--------------------------------------------------------------------------------
1 | LOCAL_PATH := $(call my-dir)
2 |
3 | MODULE_SRC_FILES := \
4 | src/svcer_hooker.cpp \
5 | src/svcer_finder.cpp \
6 | src/svcer_syscall.cpp \
7 | src/svcer_dumper.cpp \
8 |
9 |
10 | ########################### build for static library
11 | include $(CLEAR_VARS)
12 | LOCAL_MODULE := libhookersvcer
13 | LOCAL_C_INCLUDES += $(LOCAL_PATH)/inc
14 | LOCAL_SRC_FILES += $(MODULE_SRC_FILES)
15 | LOCAL_CFLAGS += -fvisibility=hidden
16 | LOCAL_CFLAGS += -DHAVE_PTHREADS
17 | include $(BUILD_STATIC_LIBRARY)
18 | # LOCAL_CFLAGS += -DHAVE_PTHREADS -D__ENABLE_MODULE_JNIER__
19 |
--------------------------------------------------------------------------------
/svcer/src/main/jni/Application.mk:
--------------------------------------------------------------------------------
1 | NDK_MODULE_PATH := $(call my-dir)
2 | APP_OPTIM := release
3 | APP_ABI := armeabi-v7a arm64-v8a
4 | APP_PLATFORM := android-28
5 | APP_STL := c++_static
6 | APP_MODULES := libhookersvcer
7 | #APP_CPPFLAGS += -fpermissive
--------------------------------------------------------------------------------
/svcer/src/main/jni/inc/linux_syscalls.h:
--------------------------------------------------------------------------------
1 | // This header will be kept up to date so that we can compile system-call
2 | // policies even when system headers are old.
3 | // System call numbers are accessible through __NR_syscall_name.
4 |
5 | #ifndef __LINUX_SYSCALLS_H__
6 | #define __LINUX_SYSCALLS_H__
7 |
8 | #if defined(__arm__)
9 | #include "linux_syscalls_arm.h"
10 | #endif
11 |
12 | #if defined(__aarch64__)
13 | #include "linux_syscalls_arm64.h"
14 | #endif
15 |
16 | #if defined(__x86_64__)
17 | #include "linux_syscalls_x86_64.h"
18 | #endif
19 |
20 | #if defined(__i386__)
21 | #include "linux_syscalls_x86_32.h"
22 | #endif
23 |
24 | #endif // __LINUX_SYSCALLS_H__
25 |
26 |
--------------------------------------------------------------------------------
/svcer/src/main/jni/inc/svcer_cmn.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include "svcer_def.h"
--------------------------------------------------------------------------------
/svcer/src/main/jni/inc/svcer_def.h:
--------------------------------------------------------------------------------
1 | #ifndef __SVCER_DEF_H__
2 | #define __SVCER_DEF_H__
3 |
4 | #include
5 | #include "linux_syscalls.h"
6 |
7 | #define __ENABLE_LOG_SVC_D__ 1
8 | #define __ENABLE_LOG_SVC_I__ 1
9 | #define __ENABLE_LOG_SVC_W__ 1
10 | #define __ENABLE_LOG_SVC_E__ 1
11 |
12 | #define __TAG_SVCER__ "svcer"
13 |
14 | #if defined(__ENABLE_LOG_SVC_D__) || defined(__ENABLE_LOG_SVC_I__)
15 | #define __ENABLE_LOG_SVC__ 1
16 | #endif
17 |
18 | #ifdef __ENABLE_LOG_SVC_D__
19 | #define LOGSVCD(...) __android_log_print(ANDROID_LOG_DEBUG, __TAG_SVCER__, __VA_ARGS__);
20 | #else
21 | #define LOGSVCD(...)
22 | #endif
23 |
24 | #ifdef __ENABLE_LOG_SVC_I__
25 | #define LOGSVCI(...) __android_log_print(ANDROID_LOG_INFO, __TAG_SVCER__, __VA_ARGS__);
26 | #else
27 | #define LOGSVCI(...)
28 | #endif
29 |
30 | #ifdef __ENABLE_LOG_SVC_W__
31 | #define LOGSVCW(...) __android_log_print(ANDROID_LOG_WARN, __TAG_SVCER__, __VA_ARGS__);
32 | #else
33 | #define LOGSVCW(...)
34 | #endif
35 |
36 | #ifdef __ENABLE_LOG_SVC_E__
37 | #define LOGSVCE(...) __android_log_print(ANDROID_LOG_ERROR, __TAG_SVCER__, __VA_ARGS__);
38 | #else
39 | #define LOGSVCE(...)
40 | #endif
41 |
42 | /* Used to retry syscalls that can return EINTR. */
43 | #define HANDLE_EINTR(exp) ({ \
44 | __typeof__(exp) _rc; \
45 | while (1) { \
46 | _rc = (exp); \
47 | if (_rc == -1) \
48 | { \
49 | if (_rc == EINTR) \
50 | continue; \
51 | } \
52 | break; \
53 | } \
54 | _rc; })
55 |
56 | #endif// end of __SVCER_DEF_H__
57 |
--------------------------------------------------------------------------------
/svcer/src/main/jni/inc/svcer_dumper.h:
--------------------------------------------------------------------------------
1 | #ifndef __SVCER_DUMPER_H__
2 | #define __SVCER_DUMPER_H__
3 |
4 | #include "svcer_hooker.h"
5 |
6 | class SvcerDumper {
7 | public:
8 | static void addAll();
9 | static void addDump(TSVCER_SYSCALL_Type type);
10 |
11 | static const char* index2name(int sc);
12 | };
13 |
14 | #endif// end of __SVCER_DUMPER_H__
15 |
--------------------------------------------------------------------------------
/svcer/src/main/jni/inc/svcer_finder.h:
--------------------------------------------------------------------------------
1 | #ifndef __MTS_SVCER_FINDER_H__
2 | #define __MTS_SVCER_FINDER_H__
3 |
4 | #include "svcer_def.h"
5 |
6 | class SvcerFinder {
7 | public:
8 | SvcerFinder(const char* selfLibName);
9 |
10 | // return: 0(success), otherwise fail
11 | int search();
12 |
13 | __always_inline const uintptr_t& getVdsoAddrStart() const { return mVdsoAddrStart; }
14 | __always_inline const uintptr_t& getVdsoAddrEnd() const { return mVdsoAddrEnd; }
15 |
16 | __always_inline const uintptr_t& getLibcAddrStart() const { return mLibcAddrStart; }
17 | __always_inline const uintptr_t& getLibcAddrEnd() const { return mLibcAddrEnd; }
18 |
19 | __always_inline const uintptr_t& getLinkerAddrStart() const { return mLinkerAddrStart; }
20 | __always_inline const uintptr_t& getLinkerAddrEnd() const { return mLinkerAddrEnd; }
21 |
22 | __always_inline const uintptr_t& getSelfAddrStart() const { return mSelfAddrStart; }
23 | __always_inline const uintptr_t& getSelfAddrEnd() const { return mSelfAddrEnd; }
24 |
25 | bool isValid() const;
26 | void print() const;
27 |
28 | protected:
29 | int doInitSysLibPath(char* libc_real_name, char* linker_real_name);
30 | void doSearchLine(const char* line, const char* libc, size_t libcLen, const char* linker, size_t linkerLen);
31 |
32 | private:
33 | const char* mSelfLibName;
34 | size_t mSelfLibNameLen;
35 | uintptr_t mVdsoAddrStart, mVdsoAddrEnd;
36 | uintptr_t mLibcAddrStart, mLibcAddrEnd;
37 | uintptr_t mLinkerAddrStart, mLinkerAddrEnd;
38 | uintptr_t mSelfAddrStart, mSelfAddrEnd;
39 | };
40 |
41 | #endif // __MTS_SVCER_FINDER_H__
42 |
--------------------------------------------------------------------------------
/svcer/src/main/jni/inc/svcer_hooker.h:
--------------------------------------------------------------------------------
1 | #ifndef __SVCER_HOOKER_H__
2 | #define __SVCER_HOOKER_H__
3 |
4 | #include "svcer_def.h"
5 |
6 | typedef enum {
7 | SVCER_SYSCALL_None = 0,
8 | SVCER_SYSCALL_open = SVCER_SYSCALL_None,
9 | SVCER_SYSCALL_openat,
10 | SVCER_SYSCALL_faccessat,
11 | SVCER_SYSCALL_fchmodat,
12 | SVCER_SYSCALL_fchownat,
13 | SVCER_SYSCALL_renameat,
14 | SVCER_SYSCALL_renameat2,
15 | SVCER_SYSCALL_fstatat,
16 | SVCER_SYSCALL_statfs,
17 | SVCER_SYSCALL_mkdirat,
18 | SVCER_SYSCALL_mknodat,
19 | SVCER_SYSCALL_truncate,
20 | SVCER_SYSCALL_linkat,
21 | SVCER_SYSCALL_unlinkat,
22 | SVCER_SYSCALL_readlinkat,
23 | SVCER_SYSCALL_symlinkat,
24 | SVCER_SYSCALL_utimensat,
25 | SVCER_SYSCALL_getcwd,
26 | SVCER_SYSCALL_chdir,
27 | SVCER_SYSCALL_execve,
28 | SVCER_SYSCALL_execveat,
29 | SVCER_SYSCALL_fcntl,
30 | SVCER_SYSCALL_prctl,
31 | SVCER_SYSCALL_sigaction,
32 | // add more syscall here ...
33 |
34 |
35 | SVCER_SYSCALL_Max,
36 | } TSVCER_SYSCALL_Type;
37 |
38 | class SvcerHookerArgument;
39 | typedef void (*SvcerHookerCallback)(int sn, SvcerHookerArgument* arg/*Not NULL*/);
40 |
41 | class SvcerHookerItem {
42 | public:
43 | SvcerHookerItem(SvcerHookerCallback cb) : mNext(nullptr), mCallback(cb)
44 | {}
45 |
46 | void addNext(SvcerHookerItem* item) {
47 | if (mNext) {
48 | mNext->addNext(item);
49 | } else {
50 | mNext = item;
51 | }
52 | }
53 |
54 | __always_inline SvcerHookerItem* next() const { return mNext; }
55 | __always_inline SvcerHookerCallback callback() const { return mCallback; }
56 |
57 | private:
58 | SvcerHookerItem* mNext;
59 | SvcerHookerCallback mCallback;
60 | };
61 |
62 | class SvcerHookerArgument {
63 | public:
64 | SvcerHookerArgument(void* info, void *uc, SvcerHookerItem* item);
65 |
66 | __always_inline void setArgument1(const intptr_t& p1);
67 | __always_inline void setArgument2(const intptr_t& p2);
68 | __always_inline void setArgument3(const intptr_t& p3);
69 | __always_inline void setArgument4(const intptr_t& p5);
70 | __always_inline void setArgument5(const intptr_t& p5);
71 | __always_inline void setArgument6(const intptr_t& p6);
72 |
73 | __always_inline void setReturn(const intptr_t& p);
74 |
75 | __always_inline intptr_t getArgument1();
76 | __always_inline intptr_t getArgument2();
77 | __always_inline intptr_t getArgument3();
78 | __always_inline intptr_t getArgument4();
79 | __always_inline intptr_t getArgument5();
80 | __always_inline intptr_t getArgument6();
81 |
82 | __always_inline intptr_t getReturn();
83 |
84 | void doSyscall();
85 |
86 | protected:
87 | SvcerHookerItem* moveToNext() {
88 | mItem = mItem->next();
89 | return mItem;
90 | }
91 |
92 | private:
93 | void* mInfo;
94 | void* mContext;
95 | SvcerHookerItem* mItem;
96 | };
97 |
98 | enum {
99 | ESvcerHookerMode_None = 0,
100 | ESvcerHookerMode_IgnoreVdso = 0x1,
101 | ESvcerHookerMode_IgnoreLibc = 0x2,
102 | ESvcerHookerMode_IgnoreLinker = 0x4,
103 |
104 | ESvcerHookerMode_IgnoreAll = ESvcerHookerMode_IgnoreVdso|ESvcerHookerMode_IgnoreLibc|ESvcerHookerMode_IgnoreLinker,
105 | };
106 |
107 | class SvcerHooker {
108 | public:
109 |
110 | /**
111 | * @param selfLibName such as: "libifmamts.so"
112 | * @return 0: success, otherwise fail
113 | * */
114 | static int init(int mode, const char* selfLibName);
115 |
116 | static void registerCallback(TSVCER_SYSCALL_Type type, SvcerHookerCallback cb);
117 | static void unregisterCallback(TSVCER_SYSCALL_Type type, SvcerHookerCallback cb);
118 |
119 | static SvcerHookerItem* getHeader(int type);
120 | static int getDefaultSecomp();
121 | static void setSigaction(const struct sigaction* act);
122 | static void getSigaction(struct sigaction* act);
123 |
124 | private:
125 | static bool sInited;
126 | };
127 |
128 | #endif// end of __SVCER_HOOKER_H__
129 |
--------------------------------------------------------------------------------
/svcer/src/main/jni/inc/svcer_qemu.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "svcer_cmn.h"
3 | #include
4 |
5 | #if defined(__arm__)
6 | #define TARGET_ARM
7 | #elif defined(__aarch64__)
8 | #define TARGET_AARCH64
9 | #elif defined(__i386__)
10 | #define TARGET_I386
11 | #elif defined(__x86_64__)
12 | #define TARGET_X86_64
13 | #else
14 | #error "Unsupported architecture."
15 | #endif
16 |
17 | typedef unsigned char abi_ubyte;
18 | typedef char abi_byte;
19 | typedef unsigned short abi_ushort;
20 | typedef short abi_short;
21 | typedef unsigned int abi_uint;
22 | typedef int abi_int;
23 | typedef uintptr_t abi_ulong;
24 | typedef intptr_t abi_long;
25 | typedef uint64_t abi_ullong;
26 | typedef int64_t abi_llong;
27 |
28 | typedef abi_ubyte target_ubyte;
29 | typedef abi_byte target_byte;
30 | typedef abi_ushort target_ushort;
31 | typedef abi_short target_short;
32 | typedef abi_uint target_uint;
33 | typedef abi_int target_int;
34 | typedef abi_ulong target_ulong;
35 | typedef abi_long target_long;
36 | typedef abi_ullong target_ullong;
37 | typedef abi_llong target_llong;
38 |
39 | #define qemu_host_page_size (1<<12)
40 | #define qemu_host_page_mask (~(qemu_host_page_size-1))
41 | #define qemu_host_page_align(addr) (((addr)+qemu_host_page_size-1)&qemu_host_page_mask)
42 |
43 | #define qemu_real_host_page_size qemu_host_page_size
44 |
45 | #define TARGET_PAGE_SIZE (1<<12)
46 | #define TARGET_PAGE_MASK (~(TARGET_PAGE_SIZE-1))
47 | #define TARGET_PAGE_ALIGN(addr) (((addr)+TARGET_PAGE_SIZE-1)&TARGET_PAGE_MASK)
48 |
49 | #define HOST_PAGE_SIZE (1<<12)
50 | #define HOST_PAGE_MASK (~(HOST_PAGE_SIZE-1))
51 | #define HOST_PAGE_ALIGN(addr) (((addr)+HOST_PAGE_SIZE-1)&HOST_PAGE_MASK)
52 |
53 | #define REAL_HOST_PAGE_SIZE (1<<12)
54 | #define REAL_HOST_PAGE_MASK (~(REAL_HOST_PAGE_SIZE-1))
55 | #define REAL_HOST_PAGE_ALIGN(addr) (((addr)+REAL_HOST_PAGE_SIZE-1)&REAL_HOST_PAGE_MASK)
56 |
57 | extern abi_ulong mmap_min_addr;
58 | extern abi_ulong reserved_va;
59 | extern bool have_guest_base;
60 | extern abi_ulong guest_base;
61 | extern abi_ulong guest_stack_size;
62 |
63 | #define put_user_ual(val, addr) *((abi_ulong*)(addr)) = (val)
64 | #define g2h(addr) addr
65 |
66 | /* same as PROT_xxx */
67 | #define PAGE_READ 0x0001
68 | #define PAGE_WRITE 0x0002
69 | #define PAGE_EXEC 0x0004
70 | #define PAGE_BITS (PAGE_READ | PAGE_WRITE | PAGE_EXEC)
71 | #define PAGE_VALID 0x0008
72 | /* original state of the write flag (used when tracking self-modifying
73 | code */
74 | #define PAGE_WRITE_ORG 0x0010
75 |
76 | #ifndef MAX
77 | #define MAX(a,b) ((a)>=(b))?(a):(b)
78 | #endif
79 | #ifndef MIN
80 | #define MIN(a,b) ((a)<=(b))?(a):(b)
81 | #endif
82 |
83 | #define get_user_ual(x, gaddr) x = *(abi_ulong*)(gaddr)
84 |
85 | enum arm_cpu_mode {
86 | ARM_CPU_MODE_USR = 0x10,
87 | ARM_CPU_MODE_FIQ = 0x11,
88 | ARM_CPU_MODE_IRQ = 0x12,
89 | ARM_CPU_MODE_SVC = 0x13,
90 | ARM_CPU_MODE_MON = 0x16,
91 | ARM_CPU_MODE_ABT = 0x17,
92 | ARM_CPU_MODE_HYP = 0x1a,
93 | ARM_CPU_MODE_UND = 0x1b,
94 | ARM_CPU_MODE_SYS = 0x1f
95 | };
96 |
97 | #define CPSR_M (0x1fU)
98 | #define CPSR_T (1U << 5)
99 | #define CPSR_F (1U << 6)
100 | #define CPSR_I (1U << 7)
101 | #define CPSR_A (1U << 8)
102 | #define CPSR_E (1U << 9)
103 | #define CPSR_IT_2_7 (0xfc00U)
104 | #define CPSR_GE (0xfU << 16)
105 | #define CPSR_IL (1U << 20)
106 | /* Note that the RESERVED bits include bit 21, which is PSTATE_SS in
107 | * an AArch64 SPSR but RES0 in AArch32 SPSR and CPSR. In QEMU we use
108 | * env->uncached_cpsr bit 21 to store PSTATE.SS when executing in AArch32,
109 | * where it is live state but not accessible to the AArch32 code.
110 | */
111 | #define CPSR_RESERVED (0x7U << 21)
112 | #define CPSR_J (1U << 24)
113 | #define CPSR_IT_0_1 (3U << 25)
114 | #define CPSR_Q (1U << 27)
115 | #define CPSR_V (1U << 28)
116 | #define CPSR_C (1U << 29)
117 | #define CPSR_Z (1U << 30)
118 | #define CPSR_N (1U << 31)
119 | #define CPSR_NZCV (CPSR_N | CPSR_Z | CPSR_C | CPSR_V)
120 | #define CPSR_AIF (CPSR_A | CPSR_I | CPSR_F)
121 |
122 | #define CPSR_IT (CPSR_IT_0_1 | CPSR_IT_2_7)
123 | #define CACHED_CPSR_BITS (CPSR_T | CPSR_AIF | CPSR_GE | CPSR_IT | CPSR_Q \
124 | | CPSR_NZCV)
125 | /* Bits writable in user mode. */
126 | #define CPSR_USER (CPSR_NZCV | CPSR_Q | CPSR_GE)
127 | /* Execution state bits. MRS read as zero, MSR writes ignored. */
128 | #define CPSR_EXEC (CPSR_T | CPSR_IT | CPSR_J | CPSR_IL)
129 | /* Mask of bits which may be set by exception return copying them from SPSR */
130 | #define CPSR_ERET_MASK (~CPSR_RESERVED)
131 |
--------------------------------------------------------------------------------