Skip to content

Commit 4d7ba34

Browse files
committed
will now detect library SIGSEGV and let Java know it
1 parent 9b56351 commit 4d7ba34

3 files changed

Lines changed: 126 additions & 1 deletion

File tree

cSploitClient/crash.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* cSploit - a simple penetration testing suite
2+
* Copyright (C) 2014 Massimo Dragano aka tux_mind <tux_mind@csploit.org>
3+
*
4+
* cSploit is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* cSploit is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with cSploit. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include <string.h>
19+
#include <errno.h>
20+
#include <sys/types.h>
21+
#include <sys/stat.h>
22+
#include <fcntl.h>
23+
#include <unistd.h>
24+
#include <signal.h>
25+
26+
#include "crash.h"
27+
#include "log.h"
28+
29+
static void (* old_sa_handler)(int) = NULL;
30+
static void (* old_sigaction)(int, siginfo_t *, void *) = NULL;
31+
32+
/**
33+
* @brief handle a library crash by creating ::CRASH_FLAG_FILEPATH
34+
*/
35+
void crash_handler(int signal, siginfo_t *info, void * context) {
36+
37+
if(creat(CRASH_FLAG_FILEPATH, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH) == -1) {
38+
LOGE("%s: unable to create '%s': %s", __func__, CRASH_FLAG_FILEPATH, strerror(errno));
39+
}
40+
41+
if(old_sa_handler) {
42+
old_sa_handler(signal);
43+
} else if(old_sigaction) {
44+
old_sigaction(signal, info, context);
45+
}
46+
}
47+
48+
/**
49+
* @brief check if library has previously crashed.
50+
* @return JNI_TRUE if ::CRASH_FLAG_FILEPATH exists, JNI_FALSE otherwise
51+
*
52+
* remove ::CRASH_FLAG_FILEPATH if it exists.
53+
*/
54+
jboolean have_crash_flag(JNIEnv *env _U_, jclass clazz _U_) {
55+
56+
if(!unlink(CRASH_FLAG_FILEPATH))
57+
return JNI_TRUE;
58+
59+
return JNI_FALSE;
60+
}
61+
62+
/**
63+
* @brief register our crash handler as SIGSEGV handler
64+
* @return 0 on success, -1 on error.
65+
*
66+
* it also set SIGPIPE as ignored.
67+
*/
68+
int register_crash_handler() {
69+
struct sigaction new, old;
70+
71+
new.sa_sigaction = crash_handler;
72+
sigemptyset(&(new.sa_mask));
73+
new.sa_flags = SA_SIGINFO;
74+
75+
if(sigaction(SIGSEGV, &new, &old)) {
76+
LOGE("%s: sigaction(SIGSEGV): %s", __func__, strerror(errno));
77+
return -1;
78+
}
79+
80+
if(old.sa_flags & SA_SIGINFO) {
81+
old_sigaction = old.sa_sigaction;
82+
} else {
83+
old_sa_handler = old.sa_handler;
84+
}
85+
86+
signal(SIGPIPE, SIG_IGN);
87+
88+
return 0;
89+
}

cSploitClient/crash.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* cSploit - a simple penetration testing suite
2+
* Copyright (C) 2014 Massimo Dragano aka tux_mind <tux_mind@csploit.org>
3+
*
4+
* cSploit is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* cSploit is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with cSploit. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#ifndef CRASH_H
19+
#define CRASH_H
20+
21+
#include <jni.h>
22+
23+
int register_crash_handler(void);
24+
jboolean have_crash_flag(JNIEnv *, jclass);
25+
26+
/// the existence of this file means that a previously running instance has crashed.
27+
#define CRASH_FLAG_FILEPATH "/data/data/org.csploit.android/files/JNI_CRASH"
28+
29+
#endif

cSploitClient/init.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <fcntl.h>
2525
#include <sys/stat.h>
2626
#include <sys/types.h>
27+
#include <signal.h>
2728

2829
#include "log.h"
2930
#include "init.h"
@@ -38,6 +39,7 @@
3839
#include "auth.h"
3940
#include "logger.h"
4041
#include "fini.h"
42+
#include "crash.h"
4143

4244
#define NUMELEM(a) (sizeof(a)/sizeof(a[0]))
4345

@@ -61,7 +63,6 @@ int init_controls() {
6163
return 0;
6264
}
6365

64-
6566
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* pVm, void* reserved _U_) {
6667
JNIEnv *env;
6768
jint ret;
@@ -77,6 +78,7 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* pVm, void* reserved _U_) {
7778
{ "isAuthenticated", "()Z", is_authenticated },
7879
{ "getHandlers", "()[Ljava/lang/String;", get_handlers },
7980
{ "Shutdown", "()V", request_shutdown },
81+
{ "hadCrashed", "()Z", have_crash_flag },
8082
};
8183

8284
ret = (*pVm)->GetEnv(pVm, (void **)&env, JNI_VERSION_1_6);
@@ -89,6 +91,11 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* pVm, void* reserved _U_) {
8991
init_structs();
9092
set_logger(android_logger);
9193

94+
if(register_crash_handler()) {
95+
LOGF("%s: cannot register crash handler", __func__);
96+
goto error;
97+
}
98+
9299
if(init_controls()) {
93100
LOGF("%s: cannot init controls", __func__);
94101
goto error;

0 commit comments

Comments
 (0)