• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 16082515961

04 Jul 2025 08:23PM UTC coverage: 72.095% (-0.1%) from 72.193%
16082515961

push

github

poettering
seccomp-util: allowlist open_tree() as part of @file-system

Now that we make use of open_tree() in places we previously used
openat() with O_PATH, it makes sense to move it from @mount to
@file-system. Without the OPEN_TREE_CLONE flag open_tree() is after all
unprivileged.

Note that open_tree_attr() I left in @mount, since it's purpose is
really to set mount options when cloning, and that's clearly a mount
related thing, not so much something you could use unpriv.

Follow-up for: c5de7b14a

This addresses an issue tracked down by Antonio Feijoo: since the commit
that started to use open_tree() various apps started to crash because
they used seccomp filters and sd-device started to use open_tree()
internally.

300842 of 417287 relevant lines covered (72.09%)

715300.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.4
/src/basic/rlimit-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include "alloc-util.h"
4
#include "errno-util.h"
5
#include "extract-word.h"
6
#include "fd-util.h"
7
#include "fileio.h"
8
#include "format-util.h"
9
#include "log.h"
10
#include "parse-util.h"
11
#include "process-util.h"
12
#include "rlimit-util.h"
13
#include "string-table.h"
14
#include "strv.h"
15
#include "time-util.h"
16

17
int setrlimit_closest(int resource, const struct rlimit *rlim) {
43,305✔
18
        struct rlimit highest, fixed;
43,305✔
19

20
        assert(rlim);
43,305✔
21

22
        if (setrlimit(resource, rlim) >= 0)
43,305✔
23
                return 0;
43,305✔
24

25
        if (errno != EPERM)
239✔
26
                return -errno;
1✔
27

28
        /* So we failed to set the desired setrlimit, then let's try
29
         * to get as close as we can */
30
        if (getrlimit(resource, &highest) < 0)
238✔
31
                return -errno;
×
32

33
        /* If the hard limit is unbounded anyway, then the EPERM had other reasons, let's propagate the original EPERM
34
         * then */
35
        if (highest.rlim_max == RLIM_INFINITY)
238✔
36
                return -EPERM;
37

38
        fixed = (struct rlimit) {
238✔
39
                .rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max),
238✔
40
                .rlim_max = MIN(rlim->rlim_max, highest.rlim_max),
238✔
41
        };
42

43
        /* Shortcut things if we wouldn't change anything. */
44
        if (fixed.rlim_cur == highest.rlim_cur &&
238✔
45
            fixed.rlim_max == highest.rlim_max)
46
                return 0;
47

48
        log_debug("Failed at setting rlimit " RLIM_FMT " for resource RLIMIT_%s. Will attempt setting value " RLIM_FMT " instead.", rlim->rlim_max, rlimit_to_string(resource), fixed.rlim_max);
120✔
49

50
        return RET_NERRNO(setrlimit(resource, &fixed));
120✔
51
}
52

53
int setrlimit_closest_all(const struct rlimit *const *rlim, int *which_failed) {
9,905✔
54
        int r;
9,905✔
55

56
        assert(rlim);
9,905✔
57

58
        /* On failure returns the limit's index that failed in *which_failed, but only if non-NULL */
59

60
        for (int i = 0; i < _RLIMIT_MAX; i++) {
168,385✔
61
                if (!rlim[i])
158,480✔
62
                        continue;
137,079✔
63

64
                r = setrlimit_closest(i, rlim[i]);
21,401✔
65
                if (r < 0) {
21,401✔
66
                        if (which_failed)
×
67
                                *which_failed = i;
×
68

69
                        return r;
×
70
                }
71
        }
72

73
        if (which_failed)
9,905✔
74
                *which_failed = -1;
9,905✔
75

76
        return 0;
77
}
78

79
static int rlimit_parse_u64(const char *val, rlim_t *ret) {
23,415✔
80
        uint64_t u;
23,415✔
81
        int r;
23,415✔
82

83
        assert(val);
23,415✔
84
        assert(ret);
23,415✔
85

86
        if (streq(val, "infinity")) {
23,415✔
87
                *ret = RLIM_INFINITY;
12✔
88
                return 0;
12✔
89
        }
90

91
        /* setrlimit(2) suggests rlim_t is always 64-bit on Linux. */
92
        assert_cc(sizeof(rlim_t) == sizeof(uint64_t));
23,403✔
93

94
        r = safe_atou64(val, &u);
23,403✔
95
        if (r < 0)
23,403✔
96
                return r;
97
        if (u >= (uint64_t) RLIM_INFINITY)
23,400✔
98
                return -ERANGE;
99

100
        *ret = (rlim_t) u;
23,400✔
101
        return 0;
23,400✔
102
}
103

104
static int rlimit_parse_size(const char *val, rlim_t *ret) {
11,752✔
105
        uint64_t u;
11,752✔
106
        int r;
11,752✔
107

108
        assert(val);
11,752✔
109
        assert(ret);
11,752✔
110

111
        if (streq(val, "infinity")) {
11,752✔
112
                *ret = RLIM_INFINITY;
6✔
113
                return 0;
6✔
114
        }
115

116
        r = parse_size(val, 1024, &u);
11,746✔
117
        if (r < 0)
11,746✔
118
                return r;
119
        if (u >= (uint64_t) RLIM_INFINITY)
11,746✔
120
                return -ERANGE;
121

122
        *ret = (rlim_t) u;
11,746✔
123
        return 0;
11,746✔
124
}
125

126
static int rlimit_parse_sec(const char *val, rlim_t *ret) {
15✔
127
        uint64_t u;
15✔
128
        usec_t t;
15✔
129
        int r;
15✔
130

131
        assert(val);
15✔
132
        assert(ret);
15✔
133

134
        r = parse_sec(val, &t);
15✔
135
        if (r < 0)
15✔
136
                return r;
15✔
137
        if (t == USEC_INFINITY) {
15✔
138
                *ret = RLIM_INFINITY;
1✔
139
                return 0;
1✔
140
        }
141

142
        u = (uint64_t) DIV_ROUND_UP(t, USEC_PER_SEC);
14✔
143
        if (u >= (uint64_t) RLIM_INFINITY)
14✔
144
                return -ERANGE;
145

146
        *ret = (rlim_t) u;
14✔
147
        return 0;
14✔
148
}
149

150
static int rlimit_parse_usec(const char *val, rlim_t *ret) {
18✔
151
        usec_t t;
18✔
152
        int r;
18✔
153

154
        assert(val);
18✔
155
        assert(ret);
18✔
156

157
        r = parse_time(val, &t, 1);
18✔
158
        if (r < 0)
18✔
159
                return r;
18✔
160
        if (t == USEC_INFINITY) {
18✔
161
                *ret = RLIM_INFINITY;
5✔
162
                return 0;
5✔
163
        }
164

165
        *ret = (rlim_t) t;
13✔
166
        return 0;
13✔
167
}
168

169
static int rlimit_parse_nice(const char *val, rlim_t *ret) {
25✔
170
        uint64_t rl;
25✔
171
        int r;
25✔
172

173
        /* So, Linux is weird. The range for RLIMIT_NICE is 40..1, mapping to the nice levels -20..19. However, the
174
         * RLIMIT_NICE limit defaults to 0 by the kernel, i.e. a value that maps to nice level 20, which of course is
175
         * bogus and does not exist. In order to permit parsing the RLIMIT_NICE of 0 here we hence implement a slight
176
         * asymmetry: when parsing as positive nice level we permit 0..19. When parsing as negative nice level, we
177
         * permit -20..0. But when parsing as raw resource limit value then we also allow the special value 0.
178
         *
179
         * Yeah, Linux is quality engineering sometimes... */
180

181
        if (val[0] == '+') {
25✔
182

183
                /* Prefixed with "+": Parse as positive user-friendly nice value */
184
                r = safe_atou64(val + 1, &rl);
5✔
185
                if (r < 0)
5✔
186
                        return r;
25✔
187

188
                if (rl >= PRIO_MAX)
5✔
189
                        return -ERANGE;
190

191
                rl = 20 - rl;
4✔
192

193
        } else if (val[0] == '-') {
20✔
194

195
                /* Prefixed with "-": Parse as negative user-friendly nice value */
196
                r = safe_atou64(val + 1, &rl);
4✔
197
                if (r < 0)
4✔
198
                        return r;
199

200
                if (rl > (uint64_t) (-PRIO_MIN))
4✔
201
                        return -ERANGE;
202

203
                rl = 20 + rl;
3✔
204
        } else {
205

206
                /* Not prefixed: parse as raw resource limit value */
207
                r = safe_atou64(val, &rl);
16✔
208
                if (r < 0)
16✔
209
                        return r;
210

211
                if (rl > (uint64_t) (20 - PRIO_MIN))
16✔
212
                        return -ERANGE;
213
        }
214

215
        *ret = (rlim_t) rl;
22✔
216
        return 0;
22✔
217
}
218

219
static int (*const rlimit_parse_table[_RLIMIT_MAX])(const char *val, rlim_t *ret) = {
220
        [RLIMIT_CPU]        = rlimit_parse_sec,
221
        [RLIMIT_FSIZE]      = rlimit_parse_size,
222
        [RLIMIT_DATA]       = rlimit_parse_size,
223
        [RLIMIT_STACK]      = rlimit_parse_size,
224
        [RLIMIT_CORE]       = rlimit_parse_size,
225
        [RLIMIT_RSS]        = rlimit_parse_size,
226
        [RLIMIT_NOFILE]     = rlimit_parse_u64,
227
        [RLIMIT_AS]         = rlimit_parse_size,
228
        [RLIMIT_NPROC]      = rlimit_parse_u64,
229
        [RLIMIT_MEMLOCK]    = rlimit_parse_size,
230
        [RLIMIT_LOCKS]      = rlimit_parse_u64,
231
        [RLIMIT_SIGPENDING] = rlimit_parse_u64,
232
        [RLIMIT_MSGQUEUE]   = rlimit_parse_size,
233
        [RLIMIT_NICE]       = rlimit_parse_nice,
234
        [RLIMIT_RTPRIO]     = rlimit_parse_u64,
235
        [RLIMIT_RTTIME]     = rlimit_parse_usec,
236
};
237

238
int rlimit_parse_one(int resource, const char *val, rlim_t *ret) {
35,225✔
239
        assert(val);
35,225✔
240
        assert(ret);
35,225✔
241

242
        if (resource < 0)
35,225✔
243
                return -EINVAL;
244
        if (resource >= _RLIMIT_MAX)
35,225✔
245
                return -EINVAL;
246

247
        return rlimit_parse_table[resource](val, ret);
35,225✔
248
}
249

250
int rlimit_parse(int resource, const char *val, struct rlimit *ret) {
24,298✔
251
        _cleanup_free_ char *hard = NULL, *soft = NULL;
24,298✔
252
        rlim_t hl, sl;
24,298✔
253
        int r;
24,298✔
254

255
        assert(val);
24,298✔
256
        assert(ret);
24,298✔
257

258
        r = extract_first_word(&val, &soft, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
24,298✔
259
        if (r < 0)
24,298✔
260
                return r;
261
        if (r == 0)
24,298✔
262
                return -EINVAL;
263

264
        r = rlimit_parse_one(resource, soft, &sl);
24,298✔
265
        if (r < 0)
24,298✔
266
                return r;
267

268
        r = extract_first_word(&val, &hard, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
24,293✔
269
        if (r < 0)
24,293✔
270
                return r;
271
        if (!isempty(val))
48,591✔
272
                return -EINVAL;
273
        if (r == 0)
24,291✔
274
                hl = sl;
13,364✔
275
        else {
276
                r = rlimit_parse_one(resource, hard, &hl);
10,927✔
277
                if (r < 0)
10,927✔
278
                        return r;
279
                if (sl > hl)
10,926✔
280
                        return -EILSEQ;
281
        }
282

283
        *ret = (struct rlimit) {
24,288✔
284
                .rlim_cur = sl,
285
                .rlim_max = hl,
286
        };
287

288
        return 0;
24,288✔
289
}
290

291
int rlimit_format(const struct rlimit *rl, char **ret) {
12,223✔
292
        _cleanup_free_ char *s = NULL;
12,223✔
293
        int r;
12,223✔
294

295
        assert(rl);
12,223✔
296
        assert(ret);
12,223✔
297

298
        if (rl->rlim_cur >= RLIM_INFINITY && rl->rlim_max >= RLIM_INFINITY)
12,223✔
299
                r = free_and_strdup(&s, "infinity");
3,406✔
300
        else if (rl->rlim_cur >= RLIM_INFINITY)
8,817✔
301
                r = asprintf(&s, "infinity:" RLIM_FMT, rl->rlim_max);
1✔
302
        else if (rl->rlim_max >= RLIM_INFINITY)
8,816✔
303
                r = asprintf(&s, RLIM_FMT ":infinity", rl->rlim_cur);
976✔
304
        else if (rl->rlim_cur == rl->rlim_max)
7,840✔
305
                r = asprintf(&s, RLIM_FMT, rl->rlim_cur);
5,313✔
306
        else
307
                r = asprintf(&s, RLIM_FMT ":" RLIM_FMT, rl->rlim_cur, rl->rlim_max);
2,527✔
308
        if (r < 0)
12,223✔
309
                return -ENOMEM;
310

311
        *ret = TAKE_PTR(s);
12,223✔
312
        return 0;
12,223✔
313
}
314

315
static const char* const rlimit_table[_RLIMIT_MAX] = {
316
        [RLIMIT_AS]         = "AS",
317
        [RLIMIT_CORE]       = "CORE",
318
        [RLIMIT_CPU]        = "CPU",
319
        [RLIMIT_DATA]       = "DATA",
320
        [RLIMIT_FSIZE]      = "FSIZE",
321
        [RLIMIT_LOCKS]      = "LOCKS",
322
        [RLIMIT_MEMLOCK]    = "MEMLOCK",
323
        [RLIMIT_MSGQUEUE]   = "MSGQUEUE",
324
        [RLIMIT_NICE]       = "NICE",
325
        [RLIMIT_NOFILE]     = "NOFILE",
326
        [RLIMIT_NPROC]      = "NPROC",
327
        [RLIMIT_RSS]        = "RSS",
328
        [RLIMIT_RTPRIO]     = "RTPRIO",
329
        [RLIMIT_RTTIME]     = "RTTIME",
330
        [RLIMIT_SIGPENDING] = "SIGPENDING",
331
        [RLIMIT_STACK]      = "STACK",
332
};
333

334
DEFINE_STRING_TABLE_LOOKUP(rlimit, int);
76,836✔
335

336
void rlimits_list(const char *prefix) {
11✔
337
        FOREACH_ELEMENT(field, rlimit_table)
187✔
338
                printf("%s%s\n", strempty(prefix), *field);
176✔
339
}
11✔
340

341
int rlimit_from_string_harder(const char *s) {
54✔
342
        const char *suffix;
54✔
343

344
        /* The official prefix */
345
        suffix = startswith(s, "RLIMIT_");
54✔
346
        if (suffix)
54✔
347
                return rlimit_from_string(suffix);
17✔
348

349
        /* Our own unit file setting prefix */
350
        suffix = startswith(s, "Limit");
37✔
351
        if (suffix)
37✔
352
                return rlimit_from_string(suffix);
17✔
353

354
        return rlimit_from_string(s);
20✔
355
}
356

357
void rlimit_free_all(struct rlimit **rl) {
63,442✔
358
        free_many((void**) rl, _RLIMIT_MAX);
63,442✔
359
}
63,442✔
360

361
int rlimit_copy_all(struct rlimit* target[static _RLIMIT_MAX], struct rlimit* const source[static _RLIMIT_MAX]) {
265✔
362
        struct rlimit* copy[_RLIMIT_MAX] = {};
265✔
363

364
        assert(target);
265✔
365
        assert(source);
265✔
366

367
        for (int i = 0; i < _RLIMIT_MAX; i++) {
4,505✔
368
                if (!source[i])
4,240✔
369
                        continue;
3,710✔
370

371
                copy[i] = newdup(struct rlimit, source[i], 1);
530✔
372
                if (!copy[i]) {
530✔
373
                        rlimit_free_all(copy);
×
374
                        return -ENOMEM;
×
375
                }
376
        }
377

378
        memcpy(target, copy, sizeof(struct rlimit*) * _RLIMIT_MAX);
265✔
379
        return 0;
265✔
380
}
381

382
int rlimit_nofile_bump(int limit) {
21,417✔
383
        int r;
21,417✔
384

385
        /* Bumps the (soft) RLIMIT_NOFILE resource limit as close as possible to the specified limit. If a negative
386
         * limit is specified, bumps it to the maximum the kernel and the hard resource limit allows. This call should
387
         * be used by all our programs that might need a lot of fds, and that know how to deal with high fd numbers
388
         * (i.e. do not use select() — which chokes on fds >= 1024) */
389

390
        if (limit < 0)
21,417✔
391
                limit = read_nr_open();
56✔
392

393
        if (limit < 3)
21,417✔
394
                limit = 3;
×
395

396
        r = setrlimit_closest(RLIMIT_NOFILE, &RLIMIT_MAKE_CONST(limit));
21,417✔
397
        if (r < 0)
21,417✔
398
                return log_debug_errno(r, "Failed to set RLIMIT_NOFILE: %m");
×
399

400
        return 0;
401
}
402

403
int rlimit_nofile_safe(void) {
18,796✔
404
        struct rlimit rl;
18,796✔
405

406
        /* Resets RLIMIT_NOFILE's soft limit FD_SETSIZE (i.e. 1024), for compatibility with software still using
407
         * select() */
408

409
        if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
18,796✔
410
                return log_debug_errno(errno, "Failed to query RLIMIT_NOFILE: %m");
×
411

412
        if (rl.rlim_cur <= FD_SETSIZE)
18,796✔
413
                return 0;
414

415
        /* So we might have inherited a hard limit that's larger than the kernel's maximum limit as stored in
416
         * /proc/sys/fs/nr_open. If we pass this hard limit unmodified to setrlimit(), we'll get EPERM. To
417
         * make sure that doesn't happen, let's limit our hard limit to the value from nr_open. */
418
        rl.rlim_max = MIN(rl.rlim_max, (rlim_t) read_nr_open());
16,897✔
419
        rl.rlim_cur = MIN((rlim_t) FD_SETSIZE, rl.rlim_max);
16,897✔
420
        if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
16,897✔
421
                return log_debug_errno(errno, "Failed to lower RLIMIT_NOFILE's soft limit to " RLIM_FMT ": %m", rl.rlim_cur);
×
422

423
        return 1;
424
}
425

426
int pid_getrlimit(pid_t pid, int resource, struct rlimit *ret) {
986✔
427

428
        static const char * const prefix_table[_RLIMIT_MAX] = {
986✔
429
                [RLIMIT_CPU]        = "Max cpu time",
430
                [RLIMIT_FSIZE]      = "Max file size",
431
                [RLIMIT_DATA]       = "Max data size",
432
                [RLIMIT_STACK]      = "Max stack size",
433
                [RLIMIT_CORE]       = "Max core file size",
434
                [RLIMIT_RSS]        = "Max resident set",
435
                [RLIMIT_NPROC]      = "Max processes",
436
                [RLIMIT_NOFILE]     = "Max open files",
437
                [RLIMIT_MEMLOCK]    = "Max locked memory",
438
                [RLIMIT_AS]         = "Max address space",
439
                [RLIMIT_LOCKS]      = "Max file locks",
440
                [RLIMIT_SIGPENDING] = "Max pending signals",
441
                [RLIMIT_MSGQUEUE]   = "Max msgqueue size",
442
                [RLIMIT_NICE]       = "Max nice priority",
443
                [RLIMIT_RTPRIO]     = "Max realtime priority",
444
                [RLIMIT_RTTIME]     = "Max realtime timeout",
445
        };
446

447
        int r;
986✔
448

449
        assert(resource >= 0);
986✔
450
        assert(resource < _RLIMIT_MAX);
986✔
451
        assert(pid >= 0);
986✔
452
        assert(ret);
986✔
453

454
        if (pid == 0 || pid == getpid_cached())
986✔
455
                return RET_NERRNO(getrlimit(resource, ret));
986✔
456

457
        r = RET_NERRNO(prlimit(pid, resource, /* new_limit= */ NULL, ret));
986✔
458
        if (!ERRNO_IS_NEG_PRIVILEGE(r))
40✔
459
                return r;
946✔
460

461
        /* We don't have access? Then try to go via /proc/$PID/limits. Weirdly that's world readable in
462
         * contrast to querying the data via prlimit() */
463

464
        const char *p = procfs_file_alloca(pid, "limits");
40✔
465
        _cleanup_free_ char *limits = NULL;
40✔
466

467
        r = read_full_file(p, &limits, /* ret_size = */ NULL);
40✔
468
        if (r < 0)
40✔
469
                return -EPERM; /* propagate original permission error if we can't access the limits file */
470

471
        _cleanup_strv_free_ char **l = NULL;
40✔
472
        l = strv_split_newlines(limits);
40✔
473
        if (!l)
40✔
474
                return -ENOMEM;
475

476
        STRV_FOREACH(i, strv_skip(l, 1)) {
364✔
477
                _cleanup_free_ char *soft = NULL, *hard = NULL;
364✔
478
                uint64_t sv, hv;
364✔
479
                const char *e;
364✔
480

481
                e = startswith(*i, prefix_table[resource]);
364✔
482
                if (!e)
364✔
483
                        continue;
324✔
484

485
                if (*e != ' ')
40✔
486
                        continue;
×
487

488
                e += strspn(e, WHITESPACE);
40✔
489

490
                size_t n;
40✔
491
                n = strcspn(e, WHITESPACE);
40✔
492
                if (n == 0)
40✔
493
                        continue;
×
494

495
                soft = strndup(e, n);
40✔
496
                if (!soft)
40✔
497
                        return -ENOMEM;
498

499
                e += n;
40✔
500
                if (*e != ' ')
40✔
501
                        continue;
×
502

503
                e += strspn(e, WHITESPACE);
40✔
504
                n = strcspn(e, WHITESPACE);
40✔
505
                if (n == 0)
40✔
506
                        continue;
×
507

508
                hard = strndup(e, n);
40✔
509
                if (!hard)
40✔
510
                        return -ENOMEM;
511

512
                if (streq(soft, "unlimited"))
40✔
513
                        sv = RLIM_INFINITY;
8✔
514
                else {
515
                        r = safe_atou64(soft, &sv);
32✔
516
                        if (r < 0)
32✔
517
                                return r;
518
                }
519

520
                if (streq(hard, "unlimited"))
40✔
521
                        hv = RLIM_INFINITY;
9✔
522
                else {
523
                        r = safe_atou64(hard, &hv);
31✔
524
                        if (r < 0)
31✔
525
                                return r;
526
                }
527

528
                *ret = (struct rlimit) {
40✔
529
                        .rlim_cur = sv,
530
                        .rlim_max = hv,
531
                };
532

533
                return 0;
40✔
534
        }
535

536
        return -ENOTRECOVERABLE;
537
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc