-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
daemon-preload.c
195 lines (155 loc) · 4.87 KB
/
daemon-preload.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#define _GNU_SOURCE
#include <dlfcn.h>
#include <sys/time.h>
#include <stdlib.h>
#include <bits/time.h>
#include <sys/timeb.h>
#include <stdio.h>
#include <sys/utsname.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/resource.h>
struct timeval diff;
int offset = 0;
int (*org_gettimeofday)(struct timeval *tp, void *tzp);
time_t (*org_time)(time_t *tloc);
int (*org_clock_gettime)(clockid_t clk_id, struct timespec *tp);
struct tm *(*org_localtime_r)(const time_t *timep, struct tm *result);
int (*org__xstat)(int __ver, const char *__filename, struct stat *__stat_buf);
void _setrlimit(int resource, int max) {
struct rlimit limits;
limits.rlim_cur = max;
limits.rlim_max = max;
if (setrlimit(resource, &limits)) {
perror("setlimit failed");
exit(205);
}
}
void _init(void) {
org_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");
org_time = dlsym(RTLD_NEXT, "time");
org_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
org_localtime_r = dlsym(RTLD_NEXT, "localtime_r");
org__xstat = dlsym(RTLD_NEXT, "__xstat");
if (getenv("TIME") != NULL)
offset = atoi(getenv("TIME"));
if (0 == offset) {
fprintf(stderr, "\nSomeone set us up the bomb, please report to [email protected]: %s\n", getenv("TIME"));
exit(2);
}
// don't change usec, this makes us start at offset.0
org_gettimeofday(&diff, NULL);
diff.tv_sec -= offset;
unsetenv("TIME");
unsetenv("LD_PRELOAD");
// this doesn't strictly belong here, but go doesn't allow setting this on os.Exec-ed threads
_setrlimit(RLIMIT_CPU, 2);
_setrlimit(RLIMIT_DATA, 128 * 1024 * 1024);
_setrlimit(RLIMIT_AS, 192 * 1024 * 1024);
_setrlimit(RLIMIT_FSIZE, 64 * 1024);
_setrlimit(RLIMIT_NPROC, 64);
_setrlimit(RLIMIT_NOFILE, 2048);
_setrlimit(RLIMIT_CORE, 0);
nice(5);
}
int gettimeofday(struct timeval *__restrict tp, void *__restrict tzp) {
static bool setsServerRequestTime = true;
if (setsServerRequestTime) {
tp->tv_sec = offset;
tp->tv_usec = 100;
setsServerRequestTime = false;
return 0;
}
else
org_gettimeofday(tp, tzp);
tp->tv_sec -= diff.tv_sec;
if (tp->tv_usec < diff.tv_usec) {
tp->tv_sec--;
tp->tv_usec += 1000*1000;
}
tp->tv_usec -= diff.tv_usec;
//fprintf(stderr, "\n%s using offset: %ld.%06ld, returning %ld.%06ld\n", __FUNCTION__, diff.tv_sec, diff.tv_usec, tp->tv_sec, tp->tv_usec);
return 0;
}
time_t time(time_t *t) {
// cannot use org_time here, diff.tv_usec possibly causes decrease of tv_sec
struct timeval a;
// don't use gettimeofday as it triggers setsServerRequestTime. PHP 5.4.4+ perform an extra time() call too
org_gettimeofday(&a, NULL);
time_t r = (time_t)a.tv_sec;
r -= diff.tv_sec;
if (a.tv_usec < diff.tv_usec)
r--;
if (t)
*t = r;
//fprintf(stderr, "\n%s using offset: %ld.%06ld, returning %ld\n", __FUNCTION__, diff.tv_sec, diff.tv_usec, r);
return r;
}
int clock_gettime(clockid_t clk_id, struct timespec *tp) {
int r = org_clock_gettime(clk_id, tp);
if (0 != r || (CLOCK_REALTIME != clk_id && CLOCK_REALTIME_COARSE != clk_id))
return r;
tp->tv_sec -= diff.tv_sec;
if (tp->tv_nsec < (1000*diff.tv_usec)) {
tp->tv_sec--;
tp->tv_nsec += 1000*1000*1000;
}
tp->tv_nsec -= 1000*diff.tv_usec;
//fprintf(stderr, "\n%s using offset: %ld.%06ld, returning %ld.%06ld\n", __FUNCTION__, diff.tv_sec, diff.tv_usec, tp->tv_sec, tp->tv_nsec);
return 0;
}
struct tm *localtime_r(time_t *timep, struct tm *result) {
if (!timep) {
struct timeval a;
gettimeofday(&a, NULL);
*timep = (time_t)a.tv_sec;
}
return org_localtime_r(timep, result);
}
int ftime(struct timeb *tp) {
ftime(tp);
tp->time -= diff.tv_sec;
if (tp->millitm < diff.tv_usec) {
tp->time--;
tp->millitm += 1000*1000;
}
tp->millitm -= diff.tv_usec;
//fprintf(stderr, "\n%s using offset: %ld.%06ld, returning %ld.%d\n", __FUNCTION__, diff.tv_sec, diff.tv_usec, tp->time, tp->millitm);
return 0;
}
// misc other overloads
int uname(struct utsname *buf) {
strcpy(buf->sysname, "Linux");
strcpy(buf->nodename,"php_shell");
strcpy(buf->release, "4.8.6-1-ARCH");
strcpy(buf->version, "#1 SMP PREEMPT Mon Oct 31 18:51:30 CET 2016");
strcpy(buf->machine, "x86_64");
strcpy(buf->domainname, "");
return 0;
}
// f/l/stat is provided by __xstat - see https://stackoverflow.com/q/5478780
int statCtr = 0;
int __xstat (int __ver, const char *__filename, struct stat *__stat_buf) {
int s = org__xstat(__ver, __filename, __stat_buf);
__stat_buf->st_dev = ++statCtr;
__stat_buf->st_ino = ++statCtr;
__stat_buf->st_atim.tv_sec = offset;
__stat_buf->st_mtim.tv_sec = offset;
__stat_buf->st_ctim.tv_sec = offset;
//fprintf(stderr, "\n%s for %s returning st_ino: %ld\n", __FUNCTION__, __filename, __stat_buf->st_ino);
return s;
}
pid_t getpid(void) {
return 2;
}
bool forkPrintedMsg = false;
pid_t fork(void) {
if (!forkPrintedMsg) {
fprintf(stderr, "\n%s has been disabled for security reasons\n", __FUNCTION__);
forkPrintedMsg=true;
}
return 0;
}