-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashc.h
More file actions
321 lines (268 loc) · 7.85 KB
/
bashc.h
File metadata and controls
321 lines (268 loc) · 7.85 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
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
#ifndef BASHC_H
#define BASHC_H
#ifndef BASHC_BIN_COUNT
#error "BASHC_BIN_COUNT must be defined before including bashc.h"
#endif
#ifndef BASHC_LIB_COUNT
#error "BASHC_LIB_COUNT must be defined before including bashc.h"
#endif
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>
extern char **environ;
/* --- Data Structures --- */
struct bin {
const unsigned char *data;
size_t size;
int fd;
bool loaded;
size_t *libs;
size_t lib_count;
bool interpreted;
size_t interpreter;
};
struct lib {
const unsigned char *data;
size_t size;
int fd;
bool loaded;
};
/* --- Global Storage --- */
/* Marked extern to allow inclusion in multiple files */
extern struct bin bins[BASHC_BIN_COUNT];
extern struct lib libs[BASHC_LIB_COUNT];
/* --- Macros --- */
/* * Helper to construct the library list.
*/
#define BINLIBS(...) \
({ \
size_t stacklibs[] = {__VA_ARGS__}; \
size_t count = sizeof(stacklibs) / sizeof(size_t); \
size_t *heaplibs = calloc(count, sizeof(size_t)); \
if (heaplibs) { \
for (size_t i = 0; i < count; ++i) \
heaplibs[i] = stacklibs[i]; \
} \
heaplibs; \
})
/* --- Function Prototypes --- */
/**
* Executes a binary defined in the bins array.
* @param bin_index Index of the binary in the bins array.
* @param argvin Null-terminated array of argument strings.
* @param envin Null-terminated array of environment strings.
* @param pstdout stdout for program
* @param pstdin stdin for program
* @param pstderr stderr for program
* @return Exit status of the command, or -1 on setup failure.
*/
int run_command(size_t bin_index, char *const argvin[], char *const envin[],
int fin, int fout, int ferr);
/* --- Implementation Section --- */
#ifdef BASHC_IMPLEMENTATION
struct bin bins[BASHC_BIN_COUNT] = {0};
struct lib libs[BASHC_LIB_COUNT] = {0};
static char ARGV0_ARG[] = "--argv0";
static char INHIBIT_CACHE_ARG[] = "--inhibit-cache";
/* Helper: Load data into a memfd */
static int load_memfd_resource(const unsigned char *data, size_t size,
int *out_fd, bool *loaded) {
if (*loaded && *out_fd > 0) {
return *out_fd;
}
int fd = memfd_create("bashc_memfd", 0);
if (fd < 0) {
perror("bashc: memfd_create failed");
return -1;
}
if (write(fd, data, size) != (ssize_t)size) {
perror("bashc: write failed");
close(fd);
return -1;
}
*out_fd = fd;
*loaded = true;
return fd;
}
int lib_get(size_t index) {
if (index >= BASHC_LIB_COUNT)
return -1;
return load_memfd_resource(libs[index].data, libs[index].size,
&libs[index].fd, &libs[index].loaded);
}
int bin_get(size_t index) {
if (index >= BASHC_BIN_COUNT)
return -1;
return load_memfd_resource(bins[index].data, bins[index].size,
&bins[index].fd, &bins[index].loaded);
}
static size_t ptr_array_len(char *const arr[]) {
size_t count = 0;
if (!arr)
return 0;
while (arr[count] != NULL)
count++;
return count;
}
static char *get_fd_path(int fd) {
char buf[64];
snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
return strdup(buf);
}
int run_command(size_t b, char *const argvin[], char *const envin[], int fin,
int fout, int ferr) {
if (b >= BASHC_BIN_COUNT) {
fprintf(stderr, "bashc: Invalid bin index %zu\n", b);
return -1;
}
struct bin *curr_bin = &bins[b];
// Load the binary
int binfd = bin_get(b);
if (binfd < 0)
return -1;
// Prepare Environment (LD_PRELOAD injection)
size_t envin_size = ptr_array_len(envin);
// Allocate space for existing env + LD_PRELOADs + NULL terminator
char **env = calloc(curr_bin->lib_count + envin_size + 1, sizeof(char *));
if (!env)
return -1;
// Copy existing environment
for (size_t i = 0; i < envin_size; i++) {
env[i] = envin[i];
}
// Inject libraries
if (curr_bin->lib_count > 0) {
// Calculate buffer size:
// "LD_PRELOAD=" (11 chars) + (~20 chars per path) + (colons) + (null
// terminator) We allocate 64 bytes per lib to be extremely safe and avoid
// truncation.
size_t buf_size = 12 + (curr_bin->lib_count * 64);
char *preload_str = calloc(buf_size, sizeof(char));
if (!preload_str) {
free(env);
return -1;
}
// Start the string
strcpy(preload_str, "LD_PRELOAD=");
for (size_t i = 0; i < curr_bin->lib_count; i++) {
int libfd = lib_get(curr_bin->libs[i]);
if (libfd < 0) {
free(preload_str);
free(env);
return -1;
}
// Format the path into a temporary buffer
char path[64];
snprintf(path, sizeof(path), "/proc/self/fd/%d", libfd);
// Append path to the main string
strcat(preload_str, path);
// Append a colon separator if this is not the last library
if (i < curr_bin->lib_count - 1) {
strcat(preload_str, ":");
}
}
env[envin_size] = preload_str;
}
// Prepare Arguments
size_t argvin_size = ptr_array_len(argvin);
size_t offset = curr_bin->interpreted ? 4 : 0;
char **argv = calloc(argvin_size + offset + 1, sizeof(char *));
if (!argv) {
// Cleanup env
for (size_t i = 0; i < curr_bin->lib_count; i++)
free(env[envin_size + i]);
free(env);
return -1;
}
int execfd = binfd;
char *bin_path = get_fd_path(binfd);
char *interp_path = NULL;
if (curr_bin->interpreted) {
int interpfd = lib_get(curr_bin->interpreter);
if (interpfd < 0) {
// Cleanup
free(bin_path);
for (size_t i = 0; i < curr_bin->lib_count; i++)
free(env[envin_size + i]);
free(env);
free(argv);
return -1;
}
execfd = interpfd;
interp_path = get_fd_path(interpfd);
argv[0] = interp_path;
argv[1] = INHIBIT_CACHE_ARG;
argv[2] = ARGV0_ARG;
argv[3] = argvin[0]; // The original program name
argv[4] = bin_path; // The actual binary path
for (size_t i = 1; i < argvin_size; i++) {
argv[i + 4] = argvin[i];
}
} else {
for (size_t i = 0; i < argvin_size; i++) {
argv[i] = argvin[i];
}
}
// Fork and Exec
pid_t pid = fork();
if (pid < 0) {
perror("bashc: fork failed");
// Cleanup everything
free(bin_path);
if (interp_path)
free(interp_path);
for (size_t i = 0; i < curr_bin->lib_count; i++)
free(env[envin_size + i]);
free(env);
free(argv);
return -1;
}
if (pid == 0) {
if (fin != STDIN_FILENO) {
if (dup2(fin, STDIN_FILENO) == -1) {
perror("dup2 stdin");
exit(1);
}
close(fin);
}
if (fout != STDOUT_FILENO) {
if (dup2(fout, STDOUT_FILENO) == -1) {
perror("dup2 stdout");
exit(1);
}
close(fout);
}
if (ferr != STDERR_FILENO) {
if (dup2(ferr, STDERR_FILENO) == -1) {
perror("dup2 stderr");
exit(1);
}
close(ferr);
}
fexecve(execfd, argv, env);
perror("bashc: fexecve failed");
exit(127);
}
int status;
waitpid(pid, &status, 0);
// Cleanup Heap Memory
free(bin_path);
if (interp_path)
free(interp_path);
// Free the LD_PRELOAD strings
for (size_t i = 0; i < curr_bin->lib_count; i++) {
free(env[envin_size + i]);
}
free(env);
free(argv);
return status;
}
#endif // BASHC_IMPLEMENTATION
#endif // BASHC_H