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+ }
0 commit comments