-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfakesyscalls.c
More file actions
161 lines (107 loc) · 4.7 KB
/
fakesyscalls.c
File metadata and controls
161 lines (107 loc) · 4.7 KB
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
#include <stdio.h>
#include <string.h>
/* good tutorials on LD_PRELOAD
http://www.goldsborough.me/c/low-level/kernel/2016/08/29/16-48-53-the_-ld_preload-_trick/
https://rafalcieslak.wordpress.com/2013/04/02/dynamic-linker-tricks-using-ld_preload-to-cheat-inject-features-and-investigate-programs/
*/
// variable arguments
#include <stdarg.h>
#define _GNU_SOURCE
#include <dlfcn.h>
#define BUF_SIZE 256
#define HARD_USR_PREFIX "/usr/share"
#define HARD_ETC_PREFIX "/etc/primx"
typedef FILE * (* orig_fopen64_f_type)(const char *filename, const char *type);
typedef int (* orig__openat64_2_f_type) (int dirfd, const char *path, int flags);
typedef int (* orig_open64_f_type) (const char *file, int oflag, ...);
typedef int (*orig_openat_f_type) (int fd, const char * path, int oflag);
typedef int (*orig_openat64_f_type) (int fd, const char * path, int oflag, ...);
#include <nl_types.h>
#include <stdlib.h>
typedef nl_catd (*orig_catopen_f_type)(const char *pathname, int flags);
#include <sys/stat.h>
#include <unistd.h>
typedef int (*orig__xstat64_f_type) (int ver, const char * path, struct stat64 * stat_buf);
#define INIT_MATCH_AND_REPLACE(STRING) \
char buf[BUF_SIZE]; \
memset(buf, 0, BUF_SIZE); \
const char * new_prefix = NULL; \
memcpy(buf, STRING, strlen(STRING))
#define MATCH_AND_REPLACE(STRING, FUNCNAME, PREFIX, ENVPREFIX) do { \
new_prefix = getenv(ENVPREFIX); \
if (new_prefix != NULL && \
strlen(STRING) > sizeof(PREFIX) && \
memcmp(STRING, PREFIX, sizeof(PREFIX) -1) == 0 \
) { \
int new_prefix_len = strnlen(new_prefix, 256); \
memmove(buf, new_prefix, new_prefix_len); \
memmove(buf + new_prefix_len, STRING + sizeof(PREFIX), strlen(STRING) - sizeof(PREFIX)); \
printf(FUNCNAME ": changing path for %s (new path %s)\n", STRING, buf); \
} \
} while (0)
FILE *fopen64(const char *filename, const char *type) {
INIT_MATCH_AND_REPLACE(filename);
orig_fopen64_f_type orig_fopen64;
orig_fopen64 = (orig_fopen64_f_type)dlsym(RTLD_NEXT,"fopen64");
MATCH_AND_REPLACE(filename, "fopen64", HARD_USR_PREFIX, "NIX_ZED_USR_PREFIX");
MATCH_AND_REPLACE(filename, "fopen64", HARD_ETC_PREFIX, "NIX_ZED_ETC_PREFIX");
return orig_fopen64(buf, type);
}
int __openat64_2(int dirfd, const char *path, int flags) {
INIT_MATCH_AND_REPLACE(path);
orig__openat64_2_f_type orig__openat64_2;
orig__openat64_2 = (orig__openat64_2_f_type)dlsym(RTLD_NEXT, "__openat64_2");
MATCH_AND_REPLACE(path, "__openat64_2", HARD_USR_PREFIX, "NIX_ZED_USR_PREFIX");
MATCH_AND_REPLACE(path, "__openat64_2", HARD_ETC_PREFIX, "NIX_ZED_ETC_PREFIX");
return orig__openat64_2(dirfd, buf, flags);
}
int open64(const char *file, int oflag, ...) {
INIT_MATCH_AND_REPLACE(file);
int mode = 0;
va_list arg;
va_start (arg, oflag);
mode = va_arg (arg, int);
va_end (arg);
orig_open64_f_type orig_open64;
orig_open64 = (orig_open64_f_type)dlsym(RTLD_NEXT, "open64");
MATCH_AND_REPLACE(file, "open64", HARD_USR_PREFIX, "NIX_ZED_USR_PREFIX");
MATCH_AND_REPLACE(file, "open64", HARD_ETC_PREFIX, "NIX_ZED_ETC_PREFIX");
return orig_open64(buf, oflag, mode);
}
int openat64 (int fd, const char * path, int oflag, ...) {
INIT_MATCH_AND_REPLACE(path);
int mode = 0;
va_list arg;
va_start (arg, oflag);
mode = va_arg (arg, int);
va_end (arg);
orig_openat64_f_type orig_openat64;
orig_openat64 = (orig_openat64_f_type)dlsym(RTLD_NEXT, "openat64");
MATCH_AND_REPLACE(path, "openat64", HARD_USR_PREFIX, "NIX_ZED_USR_PREFIX");
MATCH_AND_REPLACE(path, "openat64", HARD_ETC_PREFIX, "NIX_ZED_ETC_PREFIX");
return orig_openat64(fd, buf, oflag, mode);
}
int openat(int fd, const char * path, int oflag) {
INIT_MATCH_AND_REPLACE(path);
orig_openat_f_type orig_openat;
orig_openat = (orig_openat_f_type)dlsym(RTLD_NEXT, "openat");
MATCH_AND_REPLACE(path, "openat", HARD_USR_PREFIX, "NIX_ZED_USR_PREFIX");
MATCH_AND_REPLACE(path, "openat", HARD_ETC_PREFIX, "NIX_ZED_ETC_PREFIX");
return orig_openat(fd, buf, oflag);
}
nl_catd catopen(const char *name, int flag) {
INIT_MATCH_AND_REPLACE(name);
orig_catopen_f_type orig_catopen;
orig_catopen = (orig_catopen_f_type)dlsym(RTLD_NEXT,"catopen");
MATCH_AND_REPLACE(name, "catopen", HARD_USR_PREFIX, "NIX_ZED_USR_PREFIX");
MATCH_AND_REPLACE(name, "catopen", HARD_ETC_PREFIX, "NIX_ZED_ETC_PREFIX");
return orig_catopen(buf, flag);
}
int __xstat64(int ver, const char * path, struct stat64 * stat_buf) {
INIT_MATCH_AND_REPLACE(path);
orig__xstat64_f_type orig_xstat64;
orig_xstat64 = (orig__xstat64_f_type) dlsym(RTLD_NEXT, "__xstat64");
MATCH_AND_REPLACE(path, "__xstat64", HARD_USR_PREFIX, "NIX_ZED_USR_PREFIX");
MATCH_AND_REPLACE(path, "__xstat64", HARD_ETC_PREFIX, "NIX_ZED_ETC_PREFIX");
return orig_xstat64(ver, buf, stat_buf);
}