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

systemd / systemd / 15232239991

24 May 2025 08:01PM UTC coverage: 72.053% (-0.02%) from 72.07%
15232239991

push

github

web-flow
docs: add man pages for sd_device_enumerator_[new,ref,unref,unrefp] (#37586)

For #20929.

299160 of 415197 relevant lines covered (72.05%)

703671.29 hits per line

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

83.78
/src/shared/shift-uid.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <sys/statvfs.h>
4
#include <unistd.h>
5

6
#include "acl-util.h"
7
#include "alloc-util.h"
8
#include "dirent-util.h"
9
#include "fd-util.h"
10
#include "fileio.h"
11
#include "fs-util.h"
12
#include "log.h"
13
#include "missing_magic.h"
14
#include "shift-uid.h"
15
#include "stat-util.h"
16
#include "string-util.h"
17
#include "user-util.h"
18

19
/* While we are chmod()ing a directory tree, we set the top-level UID base to this "busy" base, so that we can always
20
 * recognize trees we are were chmod()ing recursively and got interrupted in */
21
#define UID_BUSY_BASE ((uid_t) UINT32_C(0xFFFE0000))
22
#define UID_BUSY_MASK ((uid_t) UINT32_C(0xFFFF0000))
23

24
#if HAVE_ACL
25

26
static int get_acl(int fd, const char *name, acl_type_t type, acl_t *ret) {
31,804✔
27
        acl_t acl;
31,804✔
28

29
        assert(fd >= 0);
31,804✔
30
        assert(ret);
31,804✔
31

32
        if (name) {
31,804✔
33
                _cleanup_close_ int child_fd = -EBADF;
27,388✔
34

35
                child_fd = openat(fd, name, O_PATH|O_CLOEXEC|O_NOFOLLOW);
27,388✔
36
                if (child_fd < 0)
27,388✔
37
                        return -errno;
×
38

39
                acl = acl_get_file(FORMAT_PROC_FD_PATH(child_fd), type);
27,388✔
40
        } else if (type == ACL_TYPE_ACCESS)
4,416✔
41
                acl = acl_get_fd(fd);
2,208✔
42
        else
43
                acl = acl_get_file(FORMAT_PROC_FD_PATH(fd), type);
2,208✔
44
        if (!acl)
31,804✔
45
                return -errno;
×
46

47
        *ret = acl;
31,804✔
48
        return 0;
31,804✔
49
}
50

51
static int set_acl(int fd, const char *name, acl_type_t type, acl_t acl) {
4✔
52
        int r;
4✔
53

54
        assert(fd >= 0);
4✔
55
        assert(acl);
4✔
56

57
        if (name) {
4✔
58
                _cleanup_close_ int child_fd = -EBADF;
×
59

60
                child_fd = openat(fd, name, O_PATH|O_CLOEXEC|O_NOFOLLOW);
×
61
                if (child_fd < 0)
×
62
                        return -errno;
×
63

64
                r = acl_set_file(FORMAT_PROC_FD_PATH(child_fd), type, acl);
×
65
        } else if (type == ACL_TYPE_ACCESS)
4✔
66
                r = acl_set_fd(fd, acl);
2✔
67
        else
68
                r = acl_set_file(FORMAT_PROC_FD_PATH(fd), type, acl);
2✔
69
        if (r < 0)
4✔
70
                return -errno;
×
71

72
        return 0;
73
}
74

75
static int shift_acl(acl_t acl, uid_t shift, acl_t *ret) {
31,804✔
76
        _cleanup_(acl_freep) acl_t copy = NULL;
31,804✔
77
        acl_entry_t i;
31,804✔
78
        int r;
31,804✔
79

80
        assert(acl);
31,804✔
81
        assert(ret);
31,804✔
82

83
        r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
31,804✔
84
        if (r < 0)
31,804✔
85
                return -errno;
×
86
        while (r > 0) {
120,618✔
87
                uid_t *old_uid, new_uid;
88,814✔
88
                bool modify = false;
88,814✔
89
                acl_tag_t tag;
88,814✔
90

91
                if (acl_get_tag_type(i, &tag) < 0)
88,814✔
92
                        return -errno;
×
93

94
                if (IN_SET(tag, ACL_USER, ACL_GROUP)) {
88,814✔
95

96
                        /* We don't distinguish here between uid_t and gid_t, let's make sure the compiler checks that
97
                         * this is actually OK */
98
                        assert_cc(sizeof(uid_t) == sizeof(gid_t));
12✔
99

100
                        old_uid = acl_get_qualifier(i);
12✔
101
                        if (!old_uid)
12✔
102
                                return -errno;
×
103

104
                        new_uid = shift | (*old_uid & UINT32_C(0xFFFF));
12✔
105
                        if (!uid_is_valid(new_uid))
12✔
106
                                return -EINVAL;
107

108
                        modify = new_uid != *old_uid;
12✔
109
                        if (modify && !copy) {
12✔
110
                                int n;
4✔
111

112
                                /* There's no copy of the ACL yet? if so, let's create one, and start the loop from the
113
                                 * beginning, so that we copy all entries, starting from the first, this time. */
114

115
                                n = acl_entries(acl);
4✔
116
                                if (n < 0)
4✔
117
                                        return -errno;
×
118

119
                                copy = acl_init(n);
4✔
120
                                if (!copy)
4✔
121
                                        return -errno;
×
122

123
                                /* Seek back to the beginning */
124
                                r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
4✔
125
                                if (r < 0)
4✔
126
                                        return -errno;
×
127
                                continue;
4✔
128
                        }
129
                }
130

131
                if (copy) {
88,810✔
132
                        acl_entry_t new_entry;
24✔
133

134
                        if (acl_create_entry(&copy, &new_entry) < 0)
24✔
135
                                return -errno;
×
136

137
                        if (acl_copy_entry(new_entry, i) < 0)
24✔
138
                                return -errno;
×
139

140
                        if (modify)
24✔
141
                                if (acl_set_qualifier(new_entry, &new_uid) < 0)
8✔
142
                                        return -errno;
×
143
                }
144

145
                r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i);
88,810✔
146
                if (r < 0)
88,810✔
147
                        return -errno;
×
148
        }
149

150
        *ret = TAKE_PTR(copy);
31,804✔
151

152
        return !!*ret;
31,804✔
153
}
154

155
static int patch_acls(int fd, const char *name, const struct stat *st, uid_t shift) {
38,724✔
156
        _cleanup_(acl_freep) acl_t acl = NULL, shifted = NULL;
68,320✔
157
        bool changed = false;
38,724✔
158
        int r;
38,724✔
159

160
        assert(fd >= 0);
38,724✔
161
        assert(st);
38,724✔
162

163
        /* ACLs are not supported on symlinks, there's no point in trying */
164
        if (S_ISLNK(st->st_mode))
38,724✔
165
                return 0;
166

167
        r = get_acl(fd, name, ACL_TYPE_ACCESS, &acl);
29,596✔
168
        if (r == -EOPNOTSUPP)
29,596✔
169
                return 0;
170
        if (r < 0)
29,596✔
171
                return r;
172

173
        r = shift_acl(acl, shift, &shifted);
29,596✔
174
        if (r < 0)
29,596✔
175
                return r;
176
        if (r > 0) {
29,596✔
177
                r = set_acl(fd, name, ACL_TYPE_ACCESS, shifted);
2✔
178
                if (r < 0)
2✔
179
                        return r;
180

181
                changed = true;
182
        }
183

184
        if (S_ISDIR(st->st_mode)) {
29,596✔
185
                acl_free(acl);
2,208✔
186

187
                if (shifted)
2,208✔
188
                        acl_free(shifted);
2✔
189

190
                acl = shifted = NULL;
2,208✔
191

192
                r = get_acl(fd, name, ACL_TYPE_DEFAULT, &acl);
2,208✔
193
                if (r < 0)
2,208✔
194
                        return r;
195

196
                r = shift_acl(acl, shift, &shifted);
2,208✔
197
                if (r < 0)
2,208✔
198
                        return r;
199
                if (r > 0) {
2,208✔
200
                        r = set_acl(fd, name, ACL_TYPE_DEFAULT, shifted);
2✔
201
                        if (r < 0)
2✔
202
                                return r;
203

204
                        changed = true;
205
                }
206
        }
207

208
        return changed;
29,596✔
209
}
210

211
#else
212

213
static int patch_acls(int fd, const char *name, const struct stat *st, uid_t shift) {
214
        return 0;
215
}
216

217
#endif
218

219
static int patch_fd(int fd, const char *name, const struct stat *st, uid_t shift) {
38,724✔
220
        uid_t new_uid;
38,724✔
221
        gid_t new_gid;
38,724✔
222
        bool changed = false;
38,724✔
223
        int r;
38,724✔
224

225
        assert(fd >= 0);
38,724✔
226
        assert(st);
38,724✔
227

228
        new_uid =         shift | (st->st_uid & UINT32_C(0xFFFF));
38,724✔
229
        new_gid = (gid_t) shift | (st->st_gid & UINT32_C(0xFFFF));
38,724✔
230

231
        if (!uid_is_valid(new_uid) || !gid_is_valid(new_gid))
77,448✔
232
                return -EINVAL;
×
233

234
        if (st->st_uid != new_uid || st->st_gid != new_gid) {
38,724✔
235
                if (name)
36,580✔
236
                        r = fchownat(fd, name, new_uid, new_gid, AT_SYMLINK_NOFOLLOW);
34,372✔
237
                else
238
                        r = fchown(fd, new_uid, new_gid);
2,208✔
239
                if (r < 0)
36,580✔
240
                        return -errno;
×
241

242
                /* The Linux kernel alters the mode in some cases of chown(). Let's undo this. */
243
                if (name) {
36,580✔
244
                        if (!S_ISLNK(st->st_mode))
34,372✔
245
                                r = fchmodat(fd, name, st->st_mode, 0);
25,244✔
246
                        else /* Changing the mode of a symlink is not supported by Linux kernel. Don't bother. */
247
                                r = 0;
248
                } else
249
                        r = fchmod(fd, st->st_mode);
2,208✔
250
                if (r < 0)
27,452✔
251
                        return -errno;
×
252

253
                changed = true;
254
        }
255

256
        r = patch_acls(fd, name, st, shift);
38,724✔
257
        if (r < 0)
38,724✔
258
                return r;
259

260
        return r > 0 || changed;
38,724✔
261
}
262

263
/*
264
 * Check if the filesystem is fully compatible with user namespaces or
265
 * UID/GID patching. Some filesystems in this list can be fully mounted inside
266
 * user namespaces, however their inodes may relate to host resources or only
267
 * valid in the global user namespace, therefore no patching should be applied.
268
 */
269
static int is_fs_fully_userns_compatible(const struct statfs *sfs) {
2,208✔
270

271
        assert(sfs);
2,208✔
272

273
        return F_TYPE_EQUAL(sfs->f_type, BINFMTFS_MAGIC) ||
2,208✔
274
               F_TYPE_EQUAL(sfs->f_type, CGROUP_SUPER_MAGIC) ||
2,208✔
275
               F_TYPE_EQUAL(sfs->f_type, CGROUP2_SUPER_MAGIC) ||
2,208✔
276
               F_TYPE_EQUAL(sfs->f_type, DEBUGFS_MAGIC) ||
2,208✔
277
               F_TYPE_EQUAL(sfs->f_type, DEVPTS_SUPER_MAGIC) ||
2,208✔
278
               F_TYPE_EQUAL(sfs->f_type, EFIVARFS_MAGIC) ||
2,208✔
279
               F_TYPE_EQUAL(sfs->f_type, HUGETLBFS_MAGIC) ||
2,208✔
280
               F_TYPE_EQUAL(sfs->f_type, MQUEUE_MAGIC) ||
2,208✔
281
               F_TYPE_EQUAL(sfs->f_type, PROC_SUPER_MAGIC) ||
2,208✔
282
               F_TYPE_EQUAL(sfs->f_type, PSTOREFS_MAGIC) ||
2,208✔
283
               F_TYPE_EQUAL(sfs->f_type, SELINUX_MAGIC) ||
2,208✔
284
               F_TYPE_EQUAL(sfs->f_type, SMACK_MAGIC) ||
2,208✔
285
               F_TYPE_EQUAL(sfs->f_type, SECURITYFS_MAGIC) ||
2,208✔
286
               F_TYPE_EQUAL(sfs->f_type, BPF_FS_MAGIC) ||
2,208✔
287
               F_TYPE_EQUAL(sfs->f_type, TRACEFS_MAGIC) ||
4,416✔
288
               F_TYPE_EQUAL(sfs->f_type, SYSFS_MAGIC);
289
}
290

291
static int recurse_fd(int fd, const struct stat *st, uid_t shift, bool is_toplevel) {
2,208✔
292
        _cleanup_closedir_ DIR *d = NULL;
4,416✔
293
        bool changed = false;
2,208✔
294
        struct statfs sfs;
2,208✔
295
        int r;
2,208✔
296

297
        assert(fd >= 0);
2,208✔
298

299
        if (fstatfs(fd, &sfs) < 0)
2,208✔
300
                return -errno;
×
301

302
        /* We generally want to permit crossing of mount boundaries when patching the UIDs/GIDs. However, we probably
303
         * shouldn't do this for /proc and /sys if that is already mounted into place. Hence, let's stop the recursion
304
         * when we hit procfs, sysfs or some other special file systems. */
305

306
        r = is_fs_fully_userns_compatible(&sfs);
2,208✔
307
        if (r < 0)
2,208✔
308
                return r;
309
        if (r > 0) {
2,208✔
310
                r = 0; /* don't recurse */
2,208✔
311
                return r;
312
        }
313

314
        /* Also, if we hit a read-only file system, then don't bother, skip the whole subtree */
315
        if ((sfs.f_flags & ST_RDONLY) ||
4,416✔
316
            access_fd(fd, W_OK) == -EROFS)
2,208✔
317
                goto read_only;
×
318

319
        if (S_ISDIR(st->st_mode)) {
2,208✔
320
                d = take_fdopendir(&fd);
2,208✔
321
                if (!d)
2,208✔
322
                        return -errno;
×
323

324
                FOREACH_DIRENT_ALL(de, d, return -errno) {
45,340✔
325
                        struct stat fst;
43,132✔
326

327
                        if (dot_or_dot_dot(de->d_name))
43,132✔
328
                                continue;
4,416✔
329

330
                        if (fstatat(dirfd(d), de->d_name, &fst, AT_SYMLINK_NOFOLLOW) < 0)
38,716✔
331
                                return -errno;
×
332

333
                        if (S_ISDIR(fst.st_mode)) {
38,716✔
334
                                int subdir_fd;
2,200✔
335

336
                                subdir_fd = openat(dirfd(d), de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
2,200✔
337
                                if (subdir_fd < 0)
2,200✔
338
                                        return -errno;
×
339

340
                                r = recurse_fd(subdir_fd, &fst, shift, false);
2,200✔
341
                                if (r < 0)
2,200✔
342
                                        return r;
343
                                if (r > 0)
2,200✔
344
                                        changed = true;
2,200✔
345

346
                        } else {
347
                                r = patch_fd(dirfd(d), de->d_name, &fst, shift);
36,516✔
348
                                if (r < 0)
36,516✔
349
                                        return r;
350
                                if (r > 0)
36,516✔
351
                                        changed = true;
34,372✔
352
                        }
353
                }
354
        }
355

356
        /* After we descended, also patch the directory itself. It's key to do this in this order so that the top-level
357
         * directory is patched as very last object in the tree, so that we can use it as quick indicator whether the
358
         * tree is properly chown()ed already. */
359
        r = patch_fd(d ? dirfd(d) : fd, NULL, st, shift);
2,208✔
360
        if (r == -EROFS)
2,208✔
361
                goto read_only;
×
362
        if (r > 0)
2,208✔
363
                changed = true;
2,208✔
364

365
        return changed;
2,208✔
366

367
read_only:
×
368
        if (!is_toplevel) {
×
369
                _cleanup_free_ char *name = NULL;
×
370

371
                /* When we hit a read-only subtree we simply skip it, but log about it. */
372
                (void) fd_get_path(fd, &name);
×
373
                log_debug("Skipping read-only file or directory %s.", strna(name));
×
374
                r = changed;
×
375
        }
376

377
        return r;
378
}
379

380
int path_patch_uid(const char *path, uid_t shift, uid_t range) {
10✔
381
        _cleanup_close_ int fd = -EBADF;
10✔
382
        struct stat st;
10✔
383

384
        assert(path);
10✔
385

386
        fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
10✔
387
        if (fd < 0)
10✔
388
                return -errno;
×
389

390
        /* Recursively adjusts the UID/GIDs of all files of a directory tree. This is used to automatically fix up an
391
         * OS tree to the used user namespace UID range. Note that this automatic adjustment only works for UID ranges
392
         * following the concept that the upper 16-bit of a UID identify the container, and the lower 16-bit are the actual
393
         * UID within the container. */
394

395
        /* We only support containers where the shift starts at a 2^16 boundary */
396
        if ((shift & 0xFFFF) != 0)
10✔
397
                return -EOPNOTSUPP;
398

399
        if (shift == UID_BUSY_BASE)
10✔
400
                return -EINVAL;
401

402
        /* We only support containers with 16-bit UID ranges for the patching logic */
403
        if (range != 0x10000)
10✔
404
                return -EOPNOTSUPP;
405

406
        if (fstat(fd, &st) < 0)
10✔
407
                return -errno;
×
408

409
        /* We only support containers where the uid/gid container ID match */
410
        if ((uint32_t) st.st_uid >> 16 != (uint32_t) st.st_gid >> 16)
10✔
411
                return -EBADE;
412

413
        /* Try to detect if the range is already right. Of course, this a pretty drastic optimization, as we assume
414
         * that if the top-level dir has the right upper 16-bit assigned, then everything below will have too... */
415
        if (((uint32_t) (st.st_uid ^ shift) >> 16) == 0)
10✔
416
                return 0;
417

418
        /* Before we start recursively chowning, mark the top-level dir as "busy" by chowning it to the "busy"
419
         * range. Should we be interrupted in the middle of our work, we'll see it owned by this user and will start
420
         * chown()ing it again, unconditionally, as the busy UID is not a valid UID we'd everpick for ourselves. */
421

422
        if ((st.st_uid & UID_BUSY_MASK) != UID_BUSY_BASE)
8✔
423
                if (fchown(fd,
8✔
424
                           UID_BUSY_BASE | (st.st_uid & ~UID_BUSY_MASK),
8✔
425
                           (gid_t) UID_BUSY_BASE | (st.st_gid & ~(gid_t) UID_BUSY_MASK)) < 0)
8✔
426
                        return -errno;
×
427

428
        return recurse_fd(TAKE_FD(fd), &st, shift, true);
8✔
429
}
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