-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathld-preload-socket.c
340 lines (277 loc) · 9.14 KB
/
ld-preload-socket.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#define _GNU_SOURCE
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <malloc.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#define UNIX_SOCKET_PATHS_ENV_VAR "LD_PRELOAD_SOCKET_UNIX_SOCK_MAP"
#define INET_SOCKET_PORTS_ENV_VAR "LD_PRELOAD_SOCKET_INET_PORT_MAP"
#define SEPARATORS ":,\n"
// internals
#define LOG_PREFIX "LD_PRELOAD_SOCKET"
#define SOURCE 1
#define TARGET 2
typedef int (*orig_bind_func_type)(int fd, const struct sockaddr *addr, socklen_t len);
typedef int (*orig_connect_func_type)(int fd, const struct sockaddr *addr, socklen_t len);
typedef int (*orig_unlink_func_type)(const char *pathname);
typedef struct
{
int source;
int target;
} inet_socket_port_mapping;
typedef struct
{
char *source;
char *target;
} unix_socket_path_mapping;
__thread inet_socket_port_mapping *inet_socket_ports = NULL;
__thread int inet_socket_ports_len = 0;
__thread unix_socket_path_mapping *unix_socket_paths = NULL;
__thread int unix_socket_paths_len = 0;
static void trim(char *str)
{
int i;
int begin = 0;
int end = strlen(str) - 1;
while (isspace((unsigned char)str[begin]))
{
begin++;
}
while (end >= begin && isspace((unsigned char)str[end]))
{
end--;
}
// Shift all characters back to the start of the string array
for (i = begin; i <= end; i++)
{
str[i - begin] = str[i];
}
str[i - begin] = '\0';
}
static void parse_unix_socket_paths()
{
char *env_variable = getenv(UNIX_SOCKET_PATHS_ENV_VAR);
if (env_variable == NULL)
{
return;
}
char *content = malloc(strlen(env_variable) + 1);
strcpy(content, env_variable); // strtok needs a copy because if modifies the string
if (unix_socket_paths != NULL)
{
for (size_t i = 0; i < unix_socket_paths_len; i++)
{
free(unix_socket_paths[i].source);
free(unix_socket_paths[i].target);
}
free(unix_socket_paths);
unix_socket_paths = NULL;
unix_socket_paths_len = 0;
}
int next = SOURCE;
char *source_path = NULL;
char *target_path = NULL;
char *token = strtok(content, SEPARATORS);
while (token != NULL)
{
switch (next)
{
case SOURCE:
unix_socket_paths_len++;
unix_socket_paths = realloc(unix_socket_paths, sizeof(*unix_socket_paths) * unix_socket_paths_len);
source_path = malloc(sizeof(char) * (strlen(token) + 1));
strcpy(source_path, token);
trim(source_path);
unix_socket_paths[unix_socket_paths_len - 1].source = source_path;
unix_socket_paths[unix_socket_paths_len - 1].target = source_path;
next = TARGET;
break;
case TARGET:
target_path = malloc(sizeof(char) * (strlen(token) + 1));
strcpy(target_path, token);
trim(target_path);
unix_socket_paths[unix_socket_paths_len - 1].target = target_path;
next = SOURCE;
break;
}
token = strtok(NULL, SEPARATORS);
}
free(content);
}
static const char *map_unix_socket_path(const char *source_path)
{
if (unix_socket_paths == NULL) // parse env variable only once
{
parse_unix_socket_paths();
}
for (int i = 0; i < unix_socket_paths_len; i++)
{
if (strlen(source_path) > 0 && strncmp(unix_socket_paths[i].source, source_path, strlen(source_path)) == 0)
{
return unix_socket_paths[i].target;
}
}
return NULL; // no mapping found
}
static void update_unix_socket_path(struct sockaddr_un *addr, const char *func_name)
{
const char *mapped_path = map_unix_socket_path(addr->sun_path);
if (mapped_path == NULL) // No mapping defined
{
return;
}
fprintf(stderr, "%s: Mapping AF_UNIX %s(%s) to %s(%s)\n", LOG_PREFIX, func_name, addr->sun_path, func_name, mapped_path);
strncpy(addr->sun_path, mapped_path, sizeof(addr->sun_path));
addr->sun_path[sizeof(addr->sun_path) - 1] = '\0';
}
static void parse_inet_socket_ports()
{
char *env_variable = getenv(INET_SOCKET_PORTS_ENV_VAR);
if (env_variable == NULL)
{
return;
}
char *content = malloc(sizeof(char) * (strlen(env_variable) + 1));
strcpy(content, env_variable); // strtok needs a copy because it modifies the string
if (inet_socket_ports != NULL)
{
free(inet_socket_ports);
inet_socket_ports = NULL;
inet_socket_ports_len = 0;
}
int next = SOURCE;
int source_port = 0;
int target_port = 0;
char *token = strtok(content, SEPARATORS);
while (token != NULL)
{
switch (next)
{
case SOURCE:
inet_socket_ports_len++;
inet_socket_ports = realloc(inet_socket_ports, sizeof(*inet_socket_ports) * inet_socket_ports_len);
source_port = atoi(token);
inet_socket_ports[inet_socket_ports_len - 1].source = source_port;
inet_socket_ports[inet_socket_ports_len - 1].target = 0;
next = TARGET;
break;
case TARGET:
target_port = atoi(token);
inet_socket_ports[inet_socket_ports_len - 1].target = target_port;
next = SOURCE;
break;
}
token = strtok(NULL, SEPARATORS);
}
free(content);
}
static int map_inet_socket_port(int source_port)
{
if (inet_socket_ports == NULL) // parse env variable only once
{
parse_inet_socket_ports();
}
for (size_t i = 0; i < inet_socket_ports_len; i++)
{
if (inet_socket_ports[i].source == source_port)
{
return inet_socket_ports[i].target;
}
}
return source_port; // no mapping found
}
static void update_inet_socket_port(struct sockaddr_in *addr, const char *func_name)
{
int source_port = ntohs(addr->sin_port); // addr->sin_port is in network byte order
int target_port = map_inet_socket_port(source_port);
if (target_port == source_port) // No mapping defined
{
return;
}
fprintf(stderr, "%s: Mapping AF_INET %s(%d) to %s(%d)\n", LOG_PREFIX, func_name, source_port, func_name, target_port);
addr->sin_port = htons(target_port);
}
static void update_inet6_socket_port(struct sockaddr_in6 *addr, const char *func_name)
{
int source_port = ntohs(addr->sin6_port); // addr->sin6_port is in network byte order
int target_port = map_inet_socket_port(source_port);
if (target_port == source_port) // No mapping defined
{
return;
}
fprintf(stderr, "%s: Mapping AF_INET6 %s(%d) to %s(%d)\n", LOG_PREFIX, func_name, source_port, func_name, target_port);
addr->sin6_port = htons(target_port);
}
void update_socket(const struct sockaddr *addr, const char *func_name)
{
switch (addr->sa_family)
{
case AF_UNIX:
update_unix_socket_path((struct sockaddr_un *)addr, func_name);
break;
case AF_INET:
update_inet_socket_port((struct sockaddr_in *)addr, func_name);
break;
case AF_INET6:
update_inet6_socket_port((struct sockaddr_in6 *)addr, func_name);
break;
default:
break;
}
}
// Intercept bind() for socket servers.
int bind(int fd, const struct sockaddr *addr, socklen_t len)
{
update_socket(addr, "bind");
orig_bind_func_type orig_func;
orig_func = (orig_bind_func_type)dlsym(RTLD_NEXT, "bind");
return orig_func(fd, addr, len);
}
// Intercept connect() for socket clients.
int connect(int fd, const struct sockaddr *addr, socklen_t len)
{
update_socket(addr, "connect");
orig_connect_func_type orig_func;
orig_func = (orig_connect_func_type)dlsym(RTLD_NEXT, "connect");
return orig_func(fd, addr, len);
}
// Socket servers usually use the unlink-then-bind pattern when opening AF_UNIX sockets.
// Intercept unlink() to make that work correctly.
int unlink(const char *pathname)
{
const char *path_to_unlink = pathname;
const char *mapped_pathname = map_unix_socket_path(pathname);
if (mapped_pathname != NULL)
{
fprintf(stderr, "%s: Mapping AF_UNIX unlink(%s) to unlink(%s)\n", LOG_PREFIX, pathname, mapped_pathname);
path_to_unlink = mapped_pathname;
}
orig_unlink_func_type orig_func;
orig_func = (orig_unlink_func_type)dlsym(RTLD_NEXT, "unlink");
return orig_func(path_to_unlink);
}
// Print all mappings once at startup
__attribute__((constructor)) static void setup()
{
if (unix_socket_paths == NULL)
{
parse_unix_socket_paths();
}
fprintf(stderr, "%s: %d mappings defined for UNIX socket paths\n", LOG_PREFIX, unix_socket_paths_len);
for (size_t i = 0; i < unix_socket_paths_len; i++)
{
fprintf(stderr, "%s: '%s' -> '%s'\n", LOG_PREFIX, unix_socket_paths[i].source, unix_socket_paths[i].target);
}
if (inet_socket_ports == NULL)
{
parse_inet_socket_ports();
}
fprintf(stderr, "%s: %d mappings defined for INET socket ports\n", LOG_PREFIX, inet_socket_ports_len);
for (size_t i = 0; i < inet_socket_ports_len; i++)
{
fprintf(stderr, "%s: %d -> %d\n", LOG_PREFIX, inet_socket_ports[i].source, inet_socket_ports[i].target);
}
}