-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr.h
610 lines (487 loc) · 15.2 KB
/
r.h
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// libr 0.2.0 (1dbe1799252e4b97a820e44d38c2726ce8a3af16) (https://github.com/rootmos/libr.git) (2022-11-07T15:06:05+01:00)
// modules: fail logging now no_new_privs seccomp landlock rlimit util lua
#ifndef LIBR_HEADER
#define LIBR_HEADER
// libr: fail.h
#define CHECK(res, format, ...) CHECK_NOT(res, -1, format, ##__VA_ARGS__)
#define CHECK_NOT(res, err, format, ...) \
CHECK_IF(res == err, format, ##__VA_ARGS__)
#define CHECK_IF(cond, format, ...) do { \
if(cond) { \
r_failwith(__extension__ __FUNCTION__, __extension__ __FILE__, \
__extension__ __LINE__, 1, \
format "\n", ##__VA_ARGS__); \
} \
} while(0)
#define CHECK_MALLOC(x) CHECK_NOT(x, NULL, "memory allocation failed")
#define failwith(format, ...) \
r_failwith(__extension__ __FUNCTION__, __extension__ __FILE__, \
__extension__ __LINE__, 0, format "\n", ##__VA_ARGS__)
#define not_implemented() failwith("not implemented")
void r_failwith(const char* const caller,
const char* const file,
const unsigned int line,
const int include_errno,
const char* const fmt, ...)
__attribute__ ((noreturn, format (printf, 5, 6)));
// libr: logging.h
#include <stdarg.h>
#define LOG_QUIET 0
#define LOG_ERROR 1
#define LOG_WARNING 2
#define LOG_INFO 3
#define LOG_DEBUG 4
#define LOG_TRACE 5
#ifndef LOG_LEVEL
#define LOG_LEVEL LOG_INFO
#endif
#define __r_log(level, format, ...) do { \
r_log(level, __extension__ __FUNCTION__, __extension__ __FILE__, \
__extension__ __LINE__, format "\n", ##__VA_ARGS__); \
} while(0)
#ifdef __cplusplus
void r_dummy(...);
#else
void r_dummy();
#endif
#if LOG_LEVEL >= LOG_ERROR
#define error(format, ...) __r_log(LOG_ERROR, format, ##__VA_ARGS__)
#else
#define error(format, ...) do { if(0) r_dummy(__VA_ARGS__); } while(0)
#endif
#if LOG_LEVEL >= LOG_WARNING
#define warning(format, ...) __r_log(LOG_WARNING, format, ##__VA_ARGS__)
#else
#define warning(format, ...) do { if(0) r_dummy(__VA_ARGS__); } while(0)
#endif
#if LOG_LEVEL >= LOG_INFO
#define info(format, ...) __r_log(LOG_INFO, format, ##__VA_ARGS__)
#else
#define info(format, ...) do { if(0) r_dummy(__VA_ARGS__); } while(0)
#endif
#if LOG_LEVEL >= LOG_DEBUG
#define debug(format, ...) __r_log(LOG_DEBUG, format, ##__VA_ARGS__)
#else
#define debug(format, ...) do { if(0) r_dummy(__VA_ARGS__); } while(0)
#endif
#if LOG_LEVEL >= LOG_TRACE
#define trace(format, ...) __r_log(LOG_TRACE, format, ##__VA_ARGS__)
#else
#define trace(format, ...) do { if(0) r_dummy(__VA_ARGS__); } while(0)
#endif
void r_log(int level,
const char* const caller,
const char* const file,
const unsigned int line,
const char* const fmt, ...)
__attribute__ ((format (printf, 5, 6)));
void r_vlog(int level,
const char* const caller,
const char* const file,
const unsigned int line,
const char* const fmt, va_list vl);
// libr: now.h
// returns current time formated as compact ISO8601: 20190123T182628Z
const char* now_iso8601_compact(void);
// libr: no_new_privs.h
void no_new_privs(void);
// libr: seccomp.h
#ifndef seccomp
int seccomp(unsigned int operation, unsigned int flags, void *args);
#endif
// libr: landlock.h
#include <linux/types.h>
int landlock_abi_version(void);
int landlock_new_ruleset(void);
void landlock_allow(int rsfd, const char* path, __u64 allowed_access);
void landlock_allow_read(int fd, const char* path);
void landlock_allow_read_write(int rsfd, const char* path);
void landlock_apply(int fd);
// libr: rlimit.h
#include <stddef.h>
enum rlimit_action {
RLIMIT_ACTION_INHERIT = 0,
RLIMIT_ACTION_ZERO = 1,
RLIMIT_ACTION_ABS = 2,
RLIMIT_ACTION_EQUAL = 3,
};
struct rlimit_spec {
const char* name;
int resource;
enum rlimit_action action;
unsigned long value;
};
void rlimit_default(struct rlimit_spec rlimits[], size_t len);
void rlimit_inherit(struct rlimit_spec rlimits[], size_t len);
int rlimit_parse(struct rlimit_spec rlimits[], size_t len, const char* str);
void rlimit_apply(const struct rlimit_spec rlimits[], size_t len);
// libr: util.h
#ifndef LENGTH
#define LENGTH(xs) (sizeof(xs)/sizeof((xs)[0]))
#endif
#ifndef LIT
#define LIT(x) x,sizeof(x)
#endif
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
// libr: lua.h
#define CHECK_LUA(L, err, format, ...) do { \
if(err != LUA_OK) { \
r_failwith(__extension__ __FUNCTION__, __extension__ __FILE__, \
__extension__ __LINE__, 0, \
format ": %s\n", ##__VA_ARGS__, lua_tostring(L, -1)); \
} \
} while(0)
#define LUA_EXPECT_TYPE(L, t, expected, format, ...) do { \
if(t != expected) {\
r_failwith(__extension__ __FUNCTION__, __extension__ __FILE__, \
__extension__ __LINE__, 0, \
format ": unexpected type %s (expected %s)\n", \
##__VA_ARGS__, lua_typename(L, t), \
lua_typename(L, expected)); \
} \
} while(0)
#ifndef LUA_STACK_NEUTRAL_TERM
#define LUA_STACK_NEUTRAL_TERM __lua_stack_top
#endif
#define lua_stack_neutral_begin(L) int LUA_STACK_NEUTRAL_TERM = lua_gettop(L)
#define lua_stack_neutral_end(L) \
CHECK_IF(LUA_STACK_NEUTRAL_TERM != lua_gettop(L), \
"redundant stack elements present")
#endif // LIBR_HEADER
#ifdef LIBR_IMPLEMENTATION
// libr: fail.c
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
void r_failwith(const char* const caller,
const char* const file,
const unsigned int line,
const int include_errno,
const char* const fmt, ...)
{
va_list vl;
va_start(vl, fmt);
if(include_errno) {
r_log(LOG_ERROR, caller, file, line, "(%s) ", strerror(errno));
vfprintf(stderr, fmt, vl);
} else {
r_vlog(LOG_ERROR, caller, file, line, fmt, vl);
}
va_end(vl);
abort();
}
// libr: logging.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#ifdef __cplusplus
void r_dummy(...)
#else
void r_dummy()
#endif
{
abort();
}
void r_vlog(int level,
const char* const caller,
const char* const file,
const unsigned int line,
const char* const fmt, va_list vl)
{
fprintf(stderr, "%s:%d:%s:%s:%u ",
now_iso8601_compact(), getpid(), caller, file, line);
vfprintf(stderr, fmt, vl);
}
void r_log(int level,
const char* const caller,
const char* const file,
const unsigned int line,
const char* const fmt, ...)
{
va_list vl;
va_start(vl, fmt);
r_vlog(level, caller, file, line, fmt, vl);
va_end(vl);
}
// libr: now.c
#include <time.h>
#include <stdlib.h>
const char* now_iso8601_compact(void)
{
static char buf[17];
const time_t t = time(NULL);
size_t r = strftime(buf, sizeof(buf), "%Y%m%dT%H%M%SZ", gmtime(&t));
if(r <= 0) abort();
return buf;
}
// libr: no_new_privs.c
#include <sys/prctl.h>
void no_new_privs(void)
{
int r = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
CHECK(r, "prctl(PR_SET_NO_NEW_PRIVS, 1)");
}
// libr: seccomp.c
#include <sys/syscall.h>
#include <unistd.h>
#ifndef seccomp
int seccomp(unsigned int operation, unsigned int flags, void *args)
{
return syscall(SYS_seccomp, operation, flags, args);
}
#endif
// libr: landlock.c
#include <fcntl.h>
#include <unistd.h>
#include <linux/unistd.h>
#include <linux/landlock.h>
#ifndef landlock_create_ruleset
static inline int landlock_create_ruleset(
const struct landlock_ruleset_attr* const attr,
const size_t size, const __u32 flags)
{
return syscall(__NR_landlock_create_ruleset, attr, size, flags);
}
#endif
#ifndef landlock_add_rule
static inline int landlock_add_rule(const int ruleset_fd,
const enum landlock_rule_type rule_type,
const void* const rule_attr,
const __u32 flags)
{
return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr,
flags);
}
#endif
#ifndef landlock_restrict_self
static inline int landlock_restrict_self(const int ruleset_fd,
const __u32 flags)
{
return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
}
#endif
int landlock_abi_version(void)
{
int r = landlock_create_ruleset(
NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
CHECK(r, "landlock ABI version");
return r;
if(r < 1) {
failwith("landlock not supported (ABI=%d)", r);
}
}
int landlock_new_ruleset(void)
{
struct landlock_ruleset_attr rs = {
.handled_access_fs =
LANDLOCK_ACCESS_FS_EXECUTE
| LANDLOCK_ACCESS_FS_WRITE_FILE
| LANDLOCK_ACCESS_FS_READ_FILE
| LANDLOCK_ACCESS_FS_READ_DIR
| LANDLOCK_ACCESS_FS_REMOVE_DIR
| LANDLOCK_ACCESS_FS_REMOVE_FILE
| LANDLOCK_ACCESS_FS_MAKE_CHAR
| LANDLOCK_ACCESS_FS_MAKE_DIR
| LANDLOCK_ACCESS_FS_MAKE_REG
| LANDLOCK_ACCESS_FS_MAKE_SOCK
| LANDLOCK_ACCESS_FS_MAKE_FIFO
| LANDLOCK_ACCESS_FS_MAKE_BLOCK
| LANDLOCK_ACCESS_FS_MAKE_SYM
// TODO: | LANDLOCK_ACCESS_FS_REFER,
};
int rsfd = landlock_create_ruleset(&rs, sizeof(rs), 0);
CHECK(rsfd, "landlock_create_ruleset");
return rsfd;
}
void landlock_allow(int rsfd, const char* path, __u64 allowed_access)
{
struct landlock_path_beneath_attr pb = {
.allowed_access = allowed_access,
};
pb.parent_fd = open(path, __O_PATH|O_CLOEXEC);
CHECK(pb.parent_fd, "open(%s, O_PATH)", path);
int r = landlock_add_rule(rsfd, LANDLOCK_RULE_PATH_BENEATH, &pb, 0);
CHECK(r, "landlock_add_rule");
r = close(pb.parent_fd); CHECK(r, "close(%s)", path);
}
void landlock_allow_read(int rsfd, const char* path)
{
landlock_allow(rsfd, path,
LANDLOCK_ACCESS_FS_READ_FILE
);
}
void landlock_allow_read_write(int rsfd, const char* path)
{
landlock_allow(rsfd, path,
LANDLOCK_ACCESS_FS_READ_FILE
| LANDLOCK_ACCESS_FS_WRITE_FILE
| LANDLOCK_ACCESS_FS_MAKE_REG
| LANDLOCK_ACCESS_FS_REMOVE_FILE
);
}
void landlock_apply(int fd)
{
int r = landlock_restrict_self(fd, 0);
CHECK(r, "landlock_restrict_self");
}
// libr: rlimit.c
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#ifndef RLIMIT_DEFAULT_CPU
#define RLIMIT_DEFAULT_CPU (0)
#endif
#ifndef RLIMIT_DEFAULT_FSIZE
#define RLIMIT_DEFAULT_FSIZE (0)
#endif
#ifndef RLIMIT_DEFAULT_DATA
#define RLIMIT_DEFAULT_DATA (0)
#endif
#ifndef RLIMIT_DEFAULT_STACK
#define RLIMIT_DEFAULT_STACK (0)
#endif
#ifndef RLIMIT_DEFAULT_CORE
#define RLIMIT_DEFAULT_CORE (0)
#endif
#ifndef RLIMIT_DEFAULT_RSS
#define RLIMIT_DEFAULT_RSS (0)
#endif
#ifndef RLIMIT_DEFAULT_NPROC
#define RLIMIT_DEFAULT_NPROC (0)
#endif
#ifndef RLIMIT_DEFAULT_NOFILE
#define RLIMIT_DEFAULT_NOFILE (0),
#endif
#ifndef RLIMIT_DEFAULT_MEMLOCK
#define RLIMIT_DEFAULT_MEMLOCK (0)
#endif
#ifndef RLIMIT_DEFAULT_AS
#define RLIMIT_DEFAULT_AS (0),
#endif
#ifndef RLIMIT_DEFAULT_LOCKS
#define RLIMIT_DEFAULT_LOCKS (0)
#endif
#ifndef RLIMIT_DEFAULT_SIGPENDING
#define RLIMIT_DEFAULT_SIGPENDING (0)
#endif
#ifndef RLIMIT_DEFAULT_MSGQUEUE
#define RLIMIT_DEFAULT_MSGQUEUE (0)
#endif
#ifndef RLIMIT_DEFAULT_NICE
#define RLIMIT_DEFAULT_NICE (0)
#endif
#ifndef RLIMIT_DEFAULT_RTPRIO
#define RLIMIT_DEFAULT_RTPRIO (0)
#endif
#ifndef RLIMIT_DEFAULT_RTTIME
#define RLIMIT_DEFAULT_RTTIME (0)
#endif
#define RLIMIT_SPEC(rl, v) (struct rlimit_spec) { .name = #rl, .resource = RLIMIT_##rl, .action = RLIMIT_ACTION_ABS, .value = v }
void rlimit_default(struct rlimit_spec rlimits[], size_t len)
{
struct rlimit_spec defaults[] = {
RLIMIT_SPEC(CPU, RLIMIT_DEFAULT_CPU),
RLIMIT_SPEC(FSIZE, RLIMIT_DEFAULT_FSIZE),
RLIMIT_SPEC(DATA, RLIMIT_DEFAULT_DATA),
RLIMIT_SPEC(STACK, RLIMIT_DEFAULT_STACK),
RLIMIT_SPEC(CORE, RLIMIT_DEFAULT_CORE),
RLIMIT_SPEC(RSS, RLIMIT_DEFAULT_RSS),
RLIMIT_SPEC(NPROC, RLIMIT_DEFAULT_NPROC),
RLIMIT_SPEC(NOFILE, RLIMIT_DEFAULT_NOFILE),
RLIMIT_SPEC(MEMLOCK, RLIMIT_DEFAULT_MEMLOCK),
RLIMIT_SPEC(AS, RLIMIT_DEFAULT_AS),
RLIMIT_SPEC(LOCKS, RLIMIT_DEFAULT_LOCKS),
RLIMIT_SPEC(SIGPENDING, RLIMIT_DEFAULT_SIGPENDING),
RLIMIT_SPEC(MSGQUEUE, RLIMIT_DEFAULT_MSGQUEUE),
RLIMIT_SPEC(NICE, RLIMIT_DEFAULT_NICE),
RLIMIT_SPEC(RTPRIO, RLIMIT_DEFAULT_RTPRIO),
RLIMIT_SPEC(RTTIME, RLIMIT_DEFAULT_RTTIME),
};
assert(RLIMIT_NLIMITS == LENGTH(defaults));
for(int i = 0; i < (int)len; i++) {
assert(i == defaults[i].resource);
memcpy(&rlimits[i], &defaults[i], sizeof(struct rlimit_spec));
}
if(len < LENGTH(defaults)) {
debug("not copying all rlimits: %zu < %zu", len, LENGTH(defaults));
}
}
void rlimit_inherit(struct rlimit_spec rlimits[], size_t len)
{
for(size_t i = 0; i < len; i++) {
rlimits[i].action = RLIMIT_ACTION_INHERIT;
}
}
int rlimit_parse(struct rlimit_spec rlimits[], size_t len, const char* str)
{
debug("parsing: %s", str);
size_t L = strlen(str);
size_t l = 0;
while(str[l] != '=' && l < L) {
l += 1;
}
if(l == L) {
info("unable to parse: %s (unexpected end)", str);
return 1;
}
for(size_t i = 0; i < len; i++) {
if(strlen(rlimits[i].name) != l) {
continue;
}
if(0 == strncasecmp(str, rlimits[i].name, l)) {
unsigned long v;
int r = sscanf(str + l + 1, "%lu", &v);
if(r != 1) {
info("unable to parse: %s (value not an unsigned int)", str + l + 1);
return 1;
}
info("rlimit %s: %lu", rlimits[i].name, v);
rlimits[i].action = RLIMIT_ACTION_ABS;
rlimits[i].value = v;
return 0;
}
}
info("unable to parse: %s (no such limit found)", str);
return 1;
}
void rlimit_apply(const struct rlimit_spec rlimits[], size_t len)
{
debug("applying rlimits");
for(size_t i = 0; i < len; i++) {
struct rlimit rlp;
int r = getrlimit(rlimits[i].resource, &rlp);
CHECK(r, "getrlimit(%s)", rlimits[i].name);
debug("get rlimit %s: soft=%lu hard=%lu",
rlimits[i].name, rlp.rlim_cur, rlp.rlim_max);
if(rlimits[i].action == RLIMIT_ACTION_INHERIT) {
continue;
}
switch(rlimits[i].action) {
case RLIMIT_ACTION_ZERO:
rlp.rlim_cur = 0;
rlp.rlim_max = 0;
break;
case RLIMIT_ACTION_ABS:
rlp.rlim_cur = rlimits[i].value;
rlp.rlim_max = rlimits[i].value;
break;
case RLIMIT_ACTION_EQUAL:
rlp.rlim_max = rlp.rlim_cur;
break;
default:
failwith("unexpected action: %d", rlimits[i].action);
}
debug("set rlimit %s: soft=%lu hard=%lu",
rlimits[i].name, rlp.rlim_cur, rlp.rlim_max);
r = setrlimit(rlimits[i].resource, &rlp);
CHECK(r, "setrlimit(%s)", rlimits[i].name);
}
}
#endif // LIBR_IMPLEMENTATION