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

systemd / systemd / 16280725298

14 Jul 2025 08:16PM UTC coverage: 72.166% (-0.006%) from 72.172%
16280725298

push

github

web-flow
Two fixlets for coverage test (#38183)

302135 of 418667 relevant lines covered (72.17%)

773261.64 hits per line

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

71.58
/src/login/user-runtime-dir.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <linux/magic.h>
4
#include <sys/mount.h>
5

6
#include "sd-bus.h"
7

8
#include "bus-error.h"
9
#include "bus-locator.h"
10
#include "devnum-util.h"
11
#include "errno-util.h"
12
#include "fd-util.h"
13
#include "format-util.h"
14
#include "fs-util.h"
15
#include "label-util.h"
16
#include "limits-util.h"
17
#include "log.h"
18
#include "main-func.h"
19
#include "mkdir-label.h"
20
#include "mount-util.h"
21
#include "mountpoint-util.h"
22
#include "path-util.h"
23
#include "quota-util.h"
24
#include "rm-rf.h"
25
#include "set.h"
26
#include "smack-util.h"
27
#include "stat-util.h"
28
#include "stdio-util.h"
29
#include "string-util.h"
30
#include "strv.h"
31
#include "user-util.h"
32
#include "userdb.h"
33

34
static int acquire_runtime_dir_properties(uint64_t *ret_size, uint64_t *ret_inodes) {
179✔
35
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
36
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
179✔
37
        uint64_t size, inodes;
179✔
38
        int r;
179✔
39

40
        assert(ret_size);
179✔
41
        assert(ret_inodes);
179✔
42

43
        r = sd_bus_default_system(&bus);
179✔
44
        if (r < 0)
179✔
45
                return log_error_errno(r, "Failed to connect to system bus: %m");
×
46

47
        r = bus_get_property_trivial(bus, bus_login_mgr, "RuntimeDirectorySize", &error, 't', &size);
179✔
48
        if (r < 0) {
179✔
49
                log_warning_errno(r, "Failed to acquire runtime directory size, ignoring: %s", bus_error_message(&error, r));
×
50
                sd_bus_error_free(&error);
×
51

52
                size = physical_memory_scale(10U, 100U); /* 10% */
×
53
        }
54

55
        r = bus_get_property_trivial(bus, bus_login_mgr, "RuntimeDirectoryInodesMax", &error, 't', &inodes);
179✔
56
        if (r < 0) {
179✔
57
                log_warning_errno(r, "Failed to acquire number of inodes for runtime directory, ignoring: %s", bus_error_message(&error, r));
×
58
                sd_bus_error_free(&error);
×
59

60
                inodes = DIV_ROUND_UP(size, 4096);
×
61
        }
62

63
        *ret_size = size;
179✔
64
        *ret_inodes = inodes;
179✔
65

66
        return 0;
179✔
67
}
68

69
static int user_mkdir_runtime_path(
179✔
70
                const char *runtime_path,
71
                uid_t uid,
72
                gid_t gid,
73
                uint64_t runtime_dir_size,
74
                uint64_t runtime_dir_inodes) {
75

76
        int r;
179✔
77

78
        assert(runtime_path);
179✔
79
        assert(path_is_absolute(runtime_path));
179✔
80
        assert(uid_is_valid(uid));
179✔
81
        assert(gid_is_valid(gid));
179✔
82

83
        r = mkdir_safe_label("/run/user", 0755, 0, 0, MKDIR_WARN_MODE);
179✔
84
        if (r < 0)
179✔
85
                return log_error_errno(r, "Failed to create /run/user: %m");
×
86

87
        if (path_is_mount_point(runtime_path) > 0)
179✔
88
                log_debug("%s is already a mount point", runtime_path);
×
89
        else {
90
                char options[STRLEN("mode=0700,uid=,gid=,size=,nr_inodes=,smackfsroot=*")
179✔
91
                             + DECIMAL_STR_MAX(uid_t)
92
                             + DECIMAL_STR_MAX(gid_t)
93
                             + DECIMAL_STR_MAX(uint64_t)
94
                             + DECIMAL_STR_MAX(uint64_t)];
95

96
                xsprintf(options,
358✔
97
                         "mode=0700,uid=" UID_FMT ",gid=" GID_FMT ",size=%" PRIu64 ",nr_inodes=%" PRIu64 "%s",
98
                         uid, gid, runtime_dir_size, runtime_dir_inodes,
99
                         mac_smack_use() ? ",smackfsroot=*" : "");
100

101
                _cleanup_free_ char *d = strdup(runtime_path);
179✔
102
                if (!d)
179✔
103
                        return log_oom();
×
104

105
                r = mkdir_label(runtime_path, 0700);
179✔
106
                if (r < 0 && r != -EEXIST)
179✔
107
                        return log_error_errno(r, "Failed to create %s: %m", runtime_path);
×
108

109
                _cleanup_(rmdir_and_freep) char *destroy = TAKE_PTR(d); /* auto-destroy */
×
110

111
                r = mount_nofollow_verbose(LOG_DEBUG, "tmpfs", runtime_path, "tmpfs", MS_NODEV|MS_NOSUID, options);
179✔
112
                if (r < 0) {
179✔
113
                        if (!ERRNO_IS_PRIVILEGE(r))
×
114
                                return log_error_errno(r, "Failed to mount per-user tmpfs directory %s: %m", runtime_path);
×
115

116
                        log_debug_errno(r,
×
117
                                        "Failed to mount per-user tmpfs directory %s.\n"
118
                                        "Assuming containerized execution, ignoring: %m", runtime_path);
119

120
                        r = chmod_and_chown(runtime_path, 0700, uid, gid);
×
121
                        if (r < 0)
×
122
                                return log_error_errno(r, "Failed to change ownership and mode of \"%s\": %m", runtime_path);
×
123
                }
124

125
                destroy = mfree(destroy); /* deactivate auto-destroy */
179✔
126

127
                r = label_fix(runtime_path, 0);
179✔
128
                if (r < 0)
179✔
129
                        log_warning_errno(r, "Failed to fix label of \"%s\", ignoring: %m", runtime_path);
179✔
130
        }
131

132
        return 0;
133
}
134

135
static int do_mount(UserRecord *ur) {
179✔
136
        int r;
179✔
137

138
        assert(ur);
179✔
139

140
        if (!uid_is_valid(ur->uid) || !gid_is_valid(user_record_gid(ur)))
179✔
141
                return log_error_errno(SYNTHETIC_ERRNO(ENOMSG), "User '%s' lacks UID or GID, refusing.", ur->user_name);
×
142

143
        uint64_t runtime_dir_size, runtime_dir_inodes;
179✔
144
        r = acquire_runtime_dir_properties(&runtime_dir_size, &runtime_dir_inodes);
179✔
145
        if (r < 0)
179✔
146
                return r;
147

148
        char runtime_path[STRLEN("/run/user/") + DECIMAL_STR_MAX(uid_t)];
179✔
149
        xsprintf(runtime_path, "/run/user/" UID_FMT, ur->uid);
179✔
150

151
        log_debug("Will mount %s owned by "UID_FMT":"GID_FMT, runtime_path, ur->uid, user_record_gid(ur));
179✔
152
        return user_mkdir_runtime_path(runtime_path, ur->uid, user_record_gid(ur), runtime_dir_size, runtime_dir_inodes);
179✔
153
}
154

155
static int user_remove_runtime_path(const char *runtime_path) {
179✔
156
        int r;
179✔
157

158
        assert(runtime_path);
179✔
159
        assert(path_is_absolute(runtime_path));
179✔
160

161
        r = rm_rf(runtime_path, 0);
179✔
162
        if (r < 0)
179✔
163
                log_debug_errno(r, "Failed to remove runtime directory %s (before unmounting), ignoring: %m", runtime_path);
×
164

165
        /* Ignore cases where the directory isn't mounted, as that's quite possible, if we lacked the permissions to
166
         * mount something */
167
        r = RET_NERRNO(umount2(runtime_path, MNT_DETACH));
179✔
168
        if (r < 0 && !IN_SET(r, -EINVAL, -ENOENT))
×
169
                log_debug_errno(r, "Failed to unmount user runtime directory %s, ignoring: %m", runtime_path);
×
170

171
        r = rm_rf(runtime_path, REMOVE_ROOT);
179✔
172
        if (r < 0 && r != -ENOENT)
179✔
173
                return log_error_errno(r, "Failed to remove runtime directory %s (after unmounting): %m", runtime_path);
×
174

175
        return 0;
176
}
177

178
static int do_umount(const char *user) {
179✔
179
        char runtime_path[STRLEN("/run/user/") + DECIMAL_STR_MAX(uid_t)];
179✔
180
        uid_t uid;
179✔
181
        int r;
179✔
182

183
        /* The user may be already removed. So, first try to parse the string by parse_uid(),
184
         * and if it fails, fall back to get_user_creds(). */
185
        if (parse_uid(user, &uid) < 0) {
179✔
186
                r = get_user_creds(&user, &uid, NULL, NULL, NULL, 0);
×
187
                if (r < 0)
×
188
                        return log_error_errno(r,
×
189
                                               r == -ESRCH ? "No such user \"%s\"" :
190
                                               r == -ENOMSG ? "UID \"%s\" is invalid or has an invalid main group"
191
                                                            : "Failed to look up user \"%s\": %m",
192
                                               user);
193
        }
194

195
        xsprintf(runtime_path, "/run/user/" UID_FMT, uid);
179✔
196

197
        log_debug("Will remove %s", runtime_path);
179✔
198
        return user_remove_runtime_path(runtime_path);
179✔
199
}
200

201
static int apply_tmpfs_quota(
234✔
202
                char **paths,
203
                uid_t uid,
204
                uint64_t limit,
205
                uint32_t scale) {
206

207
        _cleanup_set_free_ Set *processed = NULL;
234✔
208
        int r;
234✔
209

210
        assert(uid_is_valid(uid));
234✔
211

212
        STRV_FOREACH(p, paths) {
585✔
213
                _cleanup_close_ int fd = open(*p, O_DIRECTORY|O_CLOEXEC);
702✔
214
                if (fd < 0) {
351✔
215
                        log_warning_errno(errno, "Failed to open '%s' in order to set quota, ignoring: %m", *p);
×
216
                        continue;
×
217
                }
218

219
                struct stat st;
351✔
220
                if (fstat(fd, &st) < 0) {
351✔
221
                        log_warning_errno(errno, "Failed to stat '%s' in order to set quota, ignoring: %m", *p);
×
222
                        continue;
×
223
                }
224

225
                /* Cover for bind mounted or symlinked /var/tmp/ + /tmp/ */
226
                if (set_contains(processed, DEVNUM_TO_PTR(st.st_dev))) {
351✔
227
                        log_debug("Not setting quota on '%s', since already processed.", *p);
×
228
                        continue;
×
229
                }
230

231
                /* Remember we already dealt with this fs, even if the subsequent operation fails, since
232
                 * there's no point in appyling quota twice, regardless if it succeeds or not. */
233
                if (set_ensure_put(&processed, /* hash_ops= */ NULL, DEVNUM_TO_PTR(st.st_dev)) < 0)
351✔
234
                        return log_oom();
×
235

236
                struct statfs sfs;
351✔
237
                if (fstatfs(fd, &sfs) < 0) {
351✔
238
                        log_warning_errno(errno, "Failed to statfs '%s' in order to set quota, ignoring: %m", *p);
×
239
                        continue;
×
240
                }
241

242
                if (!is_fs_type(&sfs, TMPFS_MAGIC)) {
351✔
243
                        log_debug("Not setting quota on '%s', since not tmpfs.", *p);
117✔
244
                        continue;
117✔
245
                }
246

247
                struct dqblk req;
234✔
248
                r = RET_NERRNO(quotactl_fd(fd, QCMD_FIXED(Q_GETQUOTA, USRQUOTA), uid, &req));
234✔
249
                if (r == -ESRCH)
130✔
250
                        zero(req);
×
251
                else if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) {
234✔
252
                        log_debug_errno(r, "No UID quota support on %s, not setting quota: %m", *p);
×
253
                        continue;
×
254
                } else if (ERRNO_IS_NEG_PRIVILEGE(r)) {
234✔
255
                        log_debug_errno(r, "Lacking privileges to query UID quota on %s, not setting quota: %m", *p);
130✔
256
                        continue;
130✔
257
                } else if (r < 0) {
104✔
258
                        log_warning_errno(r, "Failed to query disk quota on %s for UID " UID_FMT ", ignoring: %m", *p, uid);
×
259
                        continue;
×
260
                }
261

262
                uint64_t v =
208✔
263
                        (scale == 0) ? 0 :
104✔
264
                        (scale == UINT32_MAX) ? UINT64_MAX :
104✔
265
                        (uint64_t) ((double) (sfs.f_blocks * sfs.f_frsize) * scale / UINT32_MAX);
104✔
266

267
                v = MIN(v, limit);
104✔
268
                v /= QIF_DQBLKSIZE;
104✔
269

270
                if (FLAGS_SET(req.dqb_valid, QIF_BLIMITS) && v == req.dqb_bhardlimit) {
104✔
271
                        /* Shortcut things if everything is set up properly already */
272
                        log_debug("Configured quota on '%s' already matches the intended setting, not updating quota.", *p);
6✔
273
                        continue;
6✔
274
                }
275

276
                req.dqb_valid = QIF_BLIMITS;
98✔
277
                req.dqb_bsoftlimit = req.dqb_bhardlimit = v;
98✔
278

279
                r = RET_NERRNO(quotactl_fd(fd, QCMD_FIXED(Q_SETQUOTA, USRQUOTA), uid, &req));
98✔
280
                if (r == -ESRCH) {
×
281
                        log_debug_errno(r, "Not setting UID quota on %s since UID quota is not supported: %m", *p);
×
282
                        continue;
×
283
                } else if (ERRNO_IS_NEG_PRIVILEGE(r)) {
98✔
284
                        log_debug_errno(r, "Lacking privileges to set UID quota on %s, skipping: %m", *p);
×
285
                        continue;
×
286
                } else if (r < 0) {
98✔
287
                        log_warning_errno(r, "Failed to set disk quota limit to '%s' on %s for UID " UID_FMT ", ignoring: %m", FORMAT_BYTES(v), *p, uid);
×
288
                        continue;
×
289
                }
290

291
                log_info("Successfully configured disk quota for UID " UID_FMT " on %s to %s", uid, *p, FORMAT_BYTES(v * QIF_DQBLKSIZE));
98✔
292
        }
293

294
        return 0;
295
}
296

297
static int do_tmpfs_quota(UserRecord *ur) {
179✔
298
        int r;
179✔
299

300
        assert(ur);
179✔
301

302
        if (user_record_is_root(ur)) {
179✔
303
                log_debug("Not applying tmpfs quota to root user.");
62✔
304
                return 0;
62✔
305
        }
306

307
        if (!uid_is_valid(ur->uid))
117✔
308
                return log_error_errno(SYNTHETIC_ERRNO(ENOMSG), "User '%s' lacks UID, refusing.", ur->user_name);
×
309

310
        r = apply_tmpfs_quota(STRV_MAKE("/tmp", "/var/tmp"), ur->uid, ur->tmp_limit.limit, user_record_tmp_limit_scale(ur));
117✔
311
        if (r < 0)
117✔
312
                return r;
313

314
        r = apply_tmpfs_quota(STRV_MAKE("/dev/shm"), ur->uid, ur->dev_shm_limit.limit, user_record_dev_shm_limit_scale(ur));
117✔
315
        if (r < 0)
117✔
316
                return r;
×
317

318
        return 0;
319
}
320

321
static int run(int argc, char *argv[]) {
358✔
322
        int r;
358✔
323

324
        log_setup();
358✔
325

326
        if (argc != 3)
358✔
327
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
358✔
328
                                       "This program takes two arguments.");
329

330
        const char *verb = argv[1], *user = argv[2];
358✔
331

332
        if (!STR_IN_SET(verb, "start", "stop"))
358✔
333
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
334
                                       "First argument must be either \"start\" or \"stop\".");
335

336
        umask(0022);
358✔
337

338
        r = mac_init();
358✔
339
        if (r < 0)
358✔
340
                return r;
341

342
        if (streq(verb, "start")) {
358✔
343
                _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
179✔
344
                r = userdb_by_name(user, /* match= */ NULL, USERDB_PARSE_NUMERIC|USERDB_SUPPRESS_SHADOW, &ur);
179✔
345
                if (r == -ESRCH)
179✔
346
                        return log_error_errno(r, "User '%s' does not exist: %m", user);
×
347
                if (r < 0)
179✔
348
                        return log_error_errno(r, "Failed to resolve user '%s': %m", user);
×
349

350
                /* We do two things here: mount the per-user XDG_RUNTIME_DIR, and set up tmpfs quota on /tmp/
351
                 * and /dev/shm/. */
352

353
                r = 0;
179✔
354
                RET_GATHER(r, do_mount(ur));
179✔
355
                RET_GATHER(r, do_tmpfs_quota(ur));
179✔
356
                return r;
179✔
357
        }
358

359
        if (streq(verb, "stop"))
179✔
360
                return do_umount(user);
179✔
361

362
        assert_not_reached();
×
363
}
364

365
DEFINE_MAIN_FUNCTION(run);
358✔
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