-
Notifications
You must be signed in to change notification settings - Fork 168
/
util.c
324 lines (277 loc) · 6.35 KB
/
util.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
/*
* Process Isolator -- Utility Functions
*
* (c) 2012-2023 Martin Mares <[email protected]>
* (c) 2012-2014 Bernard Blackham <[email protected]>
*/
#include "isolate.h"
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fsuid.h>
#include <sys/stat.h>
#include <unistd.h>
void *
xmalloc(size_t size)
{
void *p = malloc(size);
if (!p)
die("Out of memory");
return p;
}
char *
xstrdup(char *str)
{
char *p = strdup(str);
if (!p)
die("Out of memory");
return p;
}
char *xsprintf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
char *out;
int res = vasprintf(&out, fmt, args);
if (res < 0)
die("Out of memory");
va_end(args);
return out;
}
int
dir_exists(char *path)
{
struct stat st;
return (stat(path, &st) >= 0 && S_ISDIR(st.st_mode));
}
void
make_dir(char *path)
{
char *sep = (path[0] == '/' ? path+1 : path);
for (;;)
{
sep = strchr(sep, '/');
if (sep)
*sep = 0;
if (mkdir(path, 0777) < 0 && errno != EEXIST)
die("Cannot create directory %s: %m", path);
if (!sep)
break;
*sep++ = '/';
}
// mkdir() above may have returned EEXIST even if the path was not
// a directory. Ensure that it is.
struct stat st;
if (stat(path, &st) < 0)
die("Cannot stat %s: %m", path);
if (!S_ISDIR(st.st_mode))
die("Cannot create %s: already exists, but not a directory", path);
}
void make_dir_for(char *path)
{
char *copy = xstrdup(path);
char *last_slash = strrchr(copy, '/');
if (last_slash)
{
*last_slash = 0;
make_dir(copy);
}
free(copy);
}
/*
* Once upon a time, we used nftw() for traversing directory trees.
* It was simple, but unfortunately prone to symlink swapping attacks.
* Using FTW_CHDIR would prevent the attacks, but it interacts badly with
* FTW_DEPTH which we need when removing directory trees. See bug report at
* https://sourceware.org/bugzilla/show_bug.cgi?id=28831.
*
* We therefore switched to our implementation based on using openat(),
* fstatat() and similar functions.
*/
struct walk_context {
// Current item
int dir_fd;
const char *name;
bool is_dir;
struct stat st;
// Common for the whole walk
dev_t root_dev;
void (*callback)(struct walk_context *ctx);
// Used by our callbacks
uid_t chown_uid;
gid_t chown_gid;
bool keep_special_files;
};
static void
walktree_ctx(struct walk_context *ctx)
{
DIR *dir = fdopendir(ctx->dir_fd);
if (!dir)
die("fdopendir failed: %m");
struct dirent *de;
while (de = readdir(dir))
{
ctx->name = de->d_name;
if (!strcmp(ctx->name, ".") || !strcmp(ctx->name, ".."))
continue;
if (fstatat(ctx->dir_fd, ctx->name, &ctx->st, AT_SYMLINK_NOFOLLOW) < 0)
die("Cannot stat %s: %m", ctx->name);
if (ctx->st.st_dev != ctx->root_dev)
die("Unexpected mountpoint: %s", ctx->name);
if (S_ISDIR(ctx->st.st_mode))
{
struct walk_context subdir = *ctx;
subdir.dir_fd = openat(ctx->dir_fd, ctx->name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
if (subdir.dir_fd < 0)
die("Cannot open directory %s: %m", ctx->name);
walktree_ctx(&subdir);
ctx->is_dir = true;
ctx->callback(ctx);
}
else
{
ctx->is_dir = false;
ctx->callback(ctx);
}
}
closedir(dir);
}
static void
walktree(struct walk_context *ctx, const char *path, void (*callback)(struct walk_context *ctx))
{
ctx->callback = callback;
ctx->dir_fd = AT_FDCWD;
ctx->name = path;
struct walk_context top = *ctx;
top.dir_fd = open(path, O_RDONLY | O_DIRECTORY);
if (top.dir_fd < 0)
die("Cannot open directory %s: %m", path);
if (fstat(top.dir_fd, &ctx->st) < 0)
die("Cannot stat %s: %m", path);
assert(S_ISDIR(ctx->st.st_mode));
top.root_dev = ctx->st.st_dev;
walktree_ctx(&top);
ctx->is_dir = true;
ctx->callback(ctx);
}
static void
rmtree_helper(struct walk_context *ctx)
{
if (ctx->is_dir)
{
if (unlinkat(ctx->dir_fd, ctx->name, AT_REMOVEDIR) < 0)
die("Cannot rmdir %s: %m", ctx->name);
}
else
{
if (unlinkat(ctx->dir_fd, ctx->name, 0) < 0)
die("Cannot unlink %s: %m", ctx->name);
}
}
void
rmtree(char *path)
{
struct walk_context ctx = { };
walktree(&ctx, path, rmtree_helper);
}
static void
chowntree_helper(struct walk_context *ctx)
{
if (S_ISREG(ctx->st.st_mode) || S_ISDIR(ctx->st.st_mode) || ctx->keep_special_files)
{
if (fchownat(ctx->dir_fd, ctx->name, ctx->chown_uid, ctx->chown_gid, AT_SYMLINK_NOFOLLOW) < 0)
die("Cannot chown %s: %m", ctx->name);
}
else
{
if (unlinkat(ctx->dir_fd, ctx->name, 0) < 0)
die("Cannot unlink special file %s: %m", ctx->name);
}
}
void
chowntree(char *path, uid_t uid, gid_t gid, bool keep_special_files)
{
struct walk_context ctx = {
.chown_uid = uid,
.chown_gid = gid,
.keep_special_files = keep_special_files,
};
walktree(&ctx, path, chowntree_helper);
}
static int fds_to_keep[4];
static int num_kept_fds;
void
keep_fd(int fd)
{
assert(num_kept_fds < ARRAY_SIZE(fds_to_keep));
fds_to_keep[num_kept_fds++] = fd;
}
static bool
fd_is_kept(int fd)
{
for (int i=0; i < num_kept_fds; i++)
if (fds_to_keep[i] == fd)
return true;
return false;
}
void
close_all_fds(void)
{
/* Close all file descriptors except 0, 1, 2 */
DIR *dir = opendir("/proc/self/fd");
if (!dir)
die("Cannot open /proc/self/fd: %m");
int dir_fd = dirfd(dir);
struct dirent *e;
while (e = readdir(dir))
{
char *end;
long int fd = strtol(e->d_name, &end, 10);
if (*end)
continue;
if (fd >= 0 && fd <= 2 || fd == dir_fd || fd_is_kept(fd))
continue;
close(fd);
}
closedir(dir);
}
/*** Meta-files ***/
static FILE *metafile;
void
meta_open(const char *name)
{
if (!strcmp(name, "-"))
{
metafile = stdout;
return;
}
if (setfsuid(getuid()) < 0)
die("Failed to switch FS UID: %m");
metafile = fopen(name, "w");
if (setfsuid(geteuid()) < 0)
die("Failed to switch FS UID back: %m");
if (!metafile)
die("Failed to open metafile '%s'",name);
keep_fd(fileno(metafile));
}
void
meta_close(void)
{
if (metafile && metafile != stdout)
fclose(metafile);
}
void
meta_printf(const char *fmt, ...)
{
if (!metafile)
return;
va_list args;
va_start(args, fmt);
vfprintf(metafile, fmt, args);
va_end(args);
}