-
Notifications
You must be signed in to change notification settings - Fork 0
/
nerfed.c
211 lines (185 loc) · 5.19 KB
/
nerfed.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
/*
* MIT License
*
* Copyright (c) 2022 Jonas Eriksson, Up to Code AB
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/wait.h>
#include <getopt.h>
#include <errno.h>
/* Use included .so unless path is given */
#ifndef PRELOAD_SO_PATH
extern const uint8_t libeat_function_so[];
extern const size_t libeat_function_so_len;
#define TEMPFILE_PATTERN "/tmp/libeat-function.XXXXXX.so"
#endif
#define STRINGIFY(x) _STRINGIFY(x)
#define _STRINGIFY(x) #x
#define NERF_ENV(part) ("LIBEAT_FUNCTION_" part "=1")
int main(int argc, char *argv[]) {
/* Parse arguments */
bool options_help = false;
bool options_chmod = false;
bool options_mknod = false;
while (1) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{"help", no_argument, 0, 0 },
{"chmod", no_argument, 0, 0 },
{"mknod", no_argument, 0, 0 },
{0, 0, 0, 0 }
};
/* Check that we are still within supplied arguments */
if (this_option_optind >= argc) {
fprintf(stderr, "nerfed: missing command\n");
options_help = true;
break;
}
/* Only allow options before command by aborting at the first
* non-option argument */
if (argv[this_option_optind][0] != '-')
break;
int c = getopt_long(argc, argv, "hcn",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
/* Handle long-opts */
switch (option_index) {
case 0:
options_help = true;
case 1:
options_chmod = true;
case 2:
options_mknod = true;
}
break;
case 'h':
options_help = true;
break;
case 'c':
options_chmod = true;
break;
case 'n':
options_mknod = true;
break;
case '?':
options_help = true;
break;
default:
options_help = true;
}
/* Quick exit if help requested */
if (options_help)
break;
}
if (options_help) {
/* Show help and exit */
fprintf(stderr,
"Usage: nerfed [OPTIONS] <COMMAND> [COMMAND OPTIONS]\n"
"Overloads chosen libc functions using LD_PRELOAD and runs command.\n"
"Nerfing means to have the functions not do anything, and return success.\n"
"\n"
" -c, --chmod Nerf chmod, fchmodat\n"
" -m, --mknod Nerf mknod, mknodat\n"
" -h, --help Show this help\n"
"\n"
"https://github.com/zqad/nerfed/\n");
return 1;
}
/* Act on options, use environment to communicate with LD_PRELOAD
* library */
if (options_chmod) {
if (putenv(NERF_ENV("CHMOD")) < 0) {
perror("putenv");
return 1;
}
}
if (options_mknod) {
if (putenv(NERF_ENV("MKNOD")) < 0) {
perror("putenv");
return 1;
}
}
#ifdef PRELOAD_SO_PATH
/* Prepare preload environment to point to the .so on disk */
char ld_preload_env[] = "LD_PRELOAD=" STRINGIFY(PRELOAD_SO_PATH);
#else
/* Write the .so to a temporary file, and use that as LD_PRELOAD */
/* Create tempfile */
char file[] = TEMPFILE_PATTERN;
int fd = mkstemps(file, 3);
if (fd < 0) {
perror("Failed to create tempfile");
return 1;
}
/* Write .so content to temp file */
int written = 0;
while (written < libeat_function_so_len) {
int r = write(fd, &libeat_function_so[written], libeat_function_so_len - written);
if (r < 0) {
perror("Failed to write LD_PRELOAD so");
return 1;
}
written += r;
}
close(fd);
/* Set up preload environment */
char ld_preload_env[] = "LD_PRELOAD=" TEMPFILE_PATTERN;
strcpy(&ld_preload_env[11], file);
#endif
/* Deploy LD_PRELOAD environment */
if (putenv(ld_preload_env) < 0) {
perror("putenv");
return 1;
}
#ifndef PRELOAD_SO_PATH
/* Run command in a child to be able to clean up temp file */
int child = fork();
if (child < 0) {
perror("fork");
return 1;
}
if (child > 0) {
/* Still in parent. Cleanup and replicate exit status */
int status, r;
r = waitpid(child, &status, 0);
unlink(file);
if (r < 0) {
perror("waitpid");
return 1;
}
return WEXITSTATUS(status);
}
#endif
/* exec requested command */
execvp(argv[optind], &argv[optind]);
/* Exec failed */
fprintf(stderr, "nerfed: %s: %s\n", argv[optind], strerror(errno));
return 127;
}