Repository navigation
Expand file tree
/
Copy pathpatch.c
More file actions
232 lines (220 loc) · 7.73 KB
/
Copy pathpatch.c
File metadata and controls
232 lines (220 loc) · 7.73 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
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
/*
* thj_patch - TerHijack --all binary-level interception shim.
*
* Placed in place of a real binary (the original is kept as <name>.thj_orig
* next to it). On every invocation it consults the state file (records,
* one per line, TAB-separated, '#' starts a comment):
*
* name\tOUT\tpayload -> print payload, exit 0
* name\tBLOCK -> refuse access to /proc and /sys args (stderr
* "Permission denied", exit 1); everything else
* is passed through to the real binary
* name\tFAKE\tmap -> for `type name` lookups answer "name is path";
* map is a comma list of "name=path"; other args
* are passed through
* (no record) -> execute the real binary at <argv0>.thj_orig
*
* State file: $THJ_DAT_FILE, else $HOME/.thj_patch.dat.
*
* The real binary must exist at "<argv0>.thj_orig"; when it is missing the
* shim refuses to exec anything (prevents a symlink->shim infinite loop).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
static char *read_state(const char *path, size_t *len)
{
FILE *f = fopen(path, "rb");
if (!f) return NULL;
if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return NULL; }
long sz = ftell(f);
if (sz < 0) { fclose(f); return NULL; }
rewind(f);
char *buf = malloc((size_t)sz + 1);
if (!buf) { fclose(f); return NULL; }
size_t got = fread(buf, 1, (size_t)sz, f);
fclose(f);
buf[got] = '\0';
*len = got;
return buf;
}
static char *state_path(void)
{
const char *env = getenv("THJ_DAT_FILE");
if (env && *env) return strdup(env);
const char *home = getenv("HOME");
if (home && *home) {
size_t n = strlen(home) + 16;
char *p = malloc(n);
if (!p) return NULL;
snprintf(p, n, "%s/.thj_patch.dat", home);
return p;
}
return strdup(".thj_patch.dat");
}
/*
* find the record for `name`; returns type token (OUT/BLOCK/FAKE) and, for
* OUT, points *payload at the (first TAB +) payload region of `data`.
*/
static const char *lookup(const char *data, const char *name,
const char **payload)
{
*payload = NULL;
const char *line = data;
while (line && *line) {
const char *nl = strchr(line, '\n');
size_t ll = nl ? (size_t)(nl - line) : strlen(line);
if (ll > 0 && *line != '#') {
const char *t1 = memchr(line, '\t', ll);
const char *t2 = t1 ? memchr(t1 + 1, '\t',
ll - (size_t)(t1 - line) - 1)
: NULL;
if (t1) {
size_t nlen = (size_t)(t1 - line);
const char *rest = t1 + 1;
size_t tlen = t2 ? (size_t)(t2 - rest)
: ll - (size_t)(rest - line);
if (nlen == strlen(name) &&
strncmp(line, name, nlen) == 0) {
static const char *kinds[] = {"OUT", "BLOCK", "FAKE"};
for (unsigned k = 0; k < 3; k++) {
if (tlen == strlen(kinds[k]) &&
strncmp(rest, kinds[k], tlen) == 0) {
size_t pl = t2 ? ll - (size_t)((t2 + 1) - line)
: 0;
char *dp = malloc(pl + 1);
if (!dp) return NULL;
if (pl) memcpy(dp, t2 + 1, pl);
dp[pl] = '\0';
*payload = dp;
return kinds[k];
}
}
}
}
}
line = nl ? nl + 1 : NULL;
}
return NULL;
}
int main(int argc, char **argv)
{
const char *arg0 = argv[0];
const char *slash = strrchr(arg0, '/');
const char *name = slash ? slash + 1 : arg0;
char *sp = state_path();
if (!sp) return 127;
size_t dlen = 0;
char *data = read_state(sp, &dlen);
free(sp);
const char *payload = NULL;
const char *ty = NULL;
if (data) {
ty = lookup(data, name, &payload);
if (ty && strcmp(ty, "OUT") != 0 && strcmp(ty, "BLOCK") != 0 &&
strcmp(ty, "FAKE") != 0)
ty = NULL;
}
if (ty && strcmp(ty, "OUT") == 0) {
if (payload) {
puts(payload);
free((void *)payload);
}
free(data);
return 0;
}
if (ty && strcmp(ty, "BLOCK") == 0) {
for (int i = 1; i < argc; i++) {
if (strncmp(argv[i], "/proc", 5) == 0 ||
strncmp(argv[i], "/sys", 4) == 0) {
fprintf(stderr, "%s: %s: Permission denied\n", name,
argv[i]);
free((void *)payload);
free(data);
return 1;
}
}
}
if (ty && strcmp(ty, "FAKE") == 0 && payload) {
int handled = 0;
for (int i = 1; i < argc; i++) {
if (*argv[i] == '-') continue;
const char *p = payload;
while (*p) {
const char *comma = strchr(p, ',');
size_t seg = comma ? (size_t)(comma - p) : strlen(p);
const char *eq = memchr(p, '=', seg);
if (eq) {
if ((size_t)(eq - p) == strlen(argv[i]) &&
strncmp(p, argv[i], (size_t)(eq - p)) == 0) {
size_t pathlen = comma
? (size_t)(comma - (eq + 1))
: strlen(eq + 1);
printf("%s is %.*s\n", argv[i], (int)pathlen,
eq + 1);
handled = 1;
break;
}
}
if (!comma) break;
p = comma + 1;
}
if (handled) break;
}
if (handled) { free((void *)payload); free(data); return 0; }
}
free((void *)payload);
free(data);
/* fall through: run the real binary saved next to us.
*
* argv[0] may carry the full path (<argv0>.thj_orig works) OR be a bare
* command name (bash passes the typed name when a symlink in $PATH is
* exec'd). Cover both: try argv[0].thj_orig, then scan $PATH for
* <dir>/<name>.thj_orig. A backup is always created by the installer, so
* we never exec an unbacked shim (which would symlink-loop). */
char *real = NULL;
if (slash) {
size_t rl = strlen(arg0) + 10;
real = malloc(rl);
if (!real) return 127;
snprintf(real, rl, "%s.thj_orig", arg0);
if (access(real, X_OK) != 0) {
free(real);
real = NULL;
}
}
if (!real) {
const char *path = getenv("PATH");
if (!path) path = "/bin:/usr/bin";
char *pc = strdup(path);
if (pc) {
char *save = NULL;
for (char *tok = strtok_r(pc, ":", &save); tok;
tok = strtok_r(NULL, ":", &save)) {
const char *dir = *tok ? tok : ".";
size_t rl = strlen(dir) + strlen(name) + 14;
char *cand = malloc(rl);
if (!cand) break;
snprintf(cand, rl, "%s/%s.thj_orig", dir, name);
if (access(cand, X_OK) == 0) {
real = cand;
break;
}
free(cand);
}
free(pc);
}
}
if (!real) {
fprintf(stderr, "%s: real binary not found\n", name);
return 127;
}
argv[0] = real;
execv(real, argv);
perror("thj_patch exec");
free(real);
return 127;
}