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

systemd / systemd / 25834419893

13 May 2026 10:51PM UTC coverage: 72.584% (+0.07%) from 72.513%
25834419893

push

github

bluca
nsresourced: re-link GID delegation file after atomic UID file write

userns_registry_remove() restores a sub-delegated UID range by writing
the previous owner's data to u<UID>.delegate with WRITE_STRING_FILE_ATOMIC.
Atomic writes go via a temp file and rename, which replaces the directory
entry with a fresh inode and severs the hardlink to g<GID>.delegate. The
stale GID side then keeps pointing at the prior inode with outdated owner
and ancestor data, so subsequent lookups via GID return wrong results.

Re-create the hardlink after the atomic write so the two views stay in
sync, matching what userns_registry_store() already does after writing
a new delegation.

5 of 8 new or added lines in 1 file covered. (62.5%)

4652 existing lines in 93 files now uncovered.

328958 of 453210 relevant lines covered (72.58%)

1305869.89 hits per line

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

68.18
/src/shared/module-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <syslog.h>
4

5
#include "sd-dlopen.h"
6

7
#include "log.h"
8
#include "module-util.h"
9
#include "proc-cmdline.h"
10
#include "strv.h"
11

12
#if HAVE_KMOD
13

14
static void *libkmod_dl = NULL;
15

16
DLSYM_PROTOTYPE(kmod_list_next) = NULL;
17
DLSYM_PROTOTYPE(kmod_load_resources) = NULL;
18
DLSYM_PROTOTYPE(kmod_module_get_initstate) = NULL;
19
DLSYM_PROTOTYPE(kmod_module_get_module) = NULL;
20
DLSYM_PROTOTYPE(kmod_module_get_name) = NULL;
21
DLSYM_PROTOTYPE(kmod_module_new_from_lookup) = NULL;
22
DLSYM_PROTOTYPE(kmod_module_probe_insert_module) = NULL;
23
DLSYM_PROTOTYPE(kmod_module_unref) = NULL;
24
DLSYM_PROTOTYPE(kmod_module_unref_list) = NULL;
25
DLSYM_PROTOTYPE(kmod_new) = NULL;
26
DLSYM_PROTOTYPE(kmod_set_log_fn) = NULL;
27
DLSYM_PROTOTYPE(kmod_unref) = NULL;
28
DLSYM_PROTOTYPE(kmod_validate_resources) = NULL;
29

30
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
×
31
        char ***denylist = ASSERT_PTR(data);
×
32
        int r;
×
33

34
        if (proc_cmdline_key_streq(key, "module_blacklist")) {
×
35

36
                if (proc_cmdline_value_missing(key, value))
×
37
                        return 0;
38

39
                r = strv_split_and_extend(denylist, value, ",", /* filter_duplicates= */ true);
×
40
                if (r < 0)
×
41
                        return r;
×
42
        }
43

44
        return 0;
45
}
46

47
int module_load_and_warn(struct kmod_ctx *ctx, const char *module, bool verbose) {
3,611✔
48
        _cleanup_(kmod_module_unref_listp) struct kmod_list *modlist = NULL;
3,611✔
49
        _cleanup_strv_free_ char **denylist = NULL;
3,611✔
50
        bool denylist_parsed = false;
3,611✔
51
        struct kmod_list *itr;
3,611✔
52
        int r;
3,611✔
53

54
        assert(ctx);
3,611✔
55
        assert(module);
3,611✔
56

57
        /* verbose==true means we should log at non-debug level if we
58
         * fail to find or load the module. */
59

60
        log_debug("Loading module: %s", module);
3,611✔
61

62
        r = sym_kmod_module_new_from_lookup(ctx, module, &modlist);
3,611✔
63
        if (r < 0)
3,611✔
64
                return log_full_errno(verbose ? LOG_ERR : LOG_DEBUG, r,
×
65
                                      "Failed to look up module alias '%s': %m", module);
66

67
        if (!modlist)
3,611✔
68
                return log_full_errno(verbose ? LOG_ERR : LOG_DEBUG,
1,416✔
69
                                      SYNTHETIC_ERRNO(ENOENT),
70
                                      "Failed to find module '%s'", module);
71

72
        sym_kmod_list_foreach(itr, modlist) {
5,170✔
73
                _cleanup_(kmod_module_unrefp) struct kmod_module *mod = NULL;
2,975✔
74
                int state, err;
2,975✔
75

76
                mod = sym_kmod_module_get_module(itr);
2,975✔
77
                state = sym_kmod_module_get_initstate(mod);
2,975✔
78

79
                switch (state) {
2,975✔
80
                case KMOD_MODULE_BUILTIN:
81
                        log_full(verbose ? LOG_INFO : LOG_DEBUG,
1,405✔
82
                                 "Module '%s' is built in", sym_kmod_module_get_name(mod));
83
                        break;
84

85
                case KMOD_MODULE_LIVE:
86
                        log_debug("Module '%s' is already loaded", sym_kmod_module_get_name(mod));
517✔
87
                        break;
88

89
                default:
1,053✔
90
                        err = sym_kmod_module_probe_insert_module(
1,053✔
91
                                        mod,
92
                                        KMOD_PROBE_APPLY_BLACKLIST,
93
                                        /* extra_options= */ NULL,
94
                                        /* run_install= */ NULL,
95
                                        /* data= */ NULL,
96
                                        /* print_action= */ NULL);
97
                        if (err == 0)
1,053✔
98
                                log_full(verbose ? LOG_INFO : LOG_DEBUG,
896✔
99
                                         "Inserted module '%s'", sym_kmod_module_get_name(mod));
100
                        else if (err == KMOD_PROBE_APPLY_BLACKLIST)
157✔
101
                                log_full(verbose ? LOG_INFO : LOG_DEBUG,
×
102
                                         "Module '%s' is deny-listed (by kmod)", sym_kmod_module_get_name(mod));
103
                        else {
104
                                assert(err < 0);
157✔
105

106
                                if (err == -EPERM) {
157✔
107
                                        if (!denylist_parsed) {
×
108
                                                r = proc_cmdline_parse(parse_proc_cmdline_item, &denylist, 0);
×
109
                                                if (r < 0)
×
110
                                                        log_full_errno(!verbose ? LOG_DEBUG : LOG_WARNING,
×
111
                                                                       r,
112
                                                                       "Failed to parse kernel command line, ignoring: %m");
113

114
                                                denylist_parsed = true;
115
                                        }
116
                                        if (strv_contains(denylist, sym_kmod_module_get_name(mod))) {
×
117
                                                log_full(verbose ? LOG_INFO : LOG_DEBUG,
×
118
                                                         "Module '%s' is deny-listed (by kernel)", sym_kmod_module_get_name(mod));
119
                                                continue;
×
120
                                        }
121
                                }
122

123
                                log_full_errno(!verbose ? LOG_DEBUG :
157✔
124
                                               err == -ENODEV ? LOG_NOTICE :
125
                                               err == -ENOENT ? LOG_WARNING :
126
                                                                LOG_ERR,
127
                                               err,
128
                                               "Failed to insert module '%s': %m",
129
                                               sym_kmod_module_get_name(mod));
130
                                if (!IN_SET(err, -ENODEV, -ENOENT))
157✔
UNCOV
131
                                        r = err;
×
132
                        }
133
                }
134
        }
135

136
        return r;
137
}
138

139
_printf_(6,0) static void systemd_kmod_log(
×
140
                void *data,
141
                int priority,
142
                const char *file,
143
                int line,
144
                const char *fn,
145
                const char *format,
146
                va_list args) {
147

148
        log_internalv(priority, 0, file, line, fn, format, args);
×
149
}
×
150

151
int module_setup_context(struct kmod_ctx **ret) {
201✔
152
        _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
201✔
153
        int r;
201✔
154

155
        assert(ret);
201✔
156

157
        r = dlopen_libkmod(LOG_DEBUG);
201✔
158
        if (r < 0)
201✔
159
                return r;
160

161
        ctx = sym_kmod_new(NULL, NULL);
201✔
162
        if (!ctx)
201✔
163
                return -ENOMEM;
164

165
        (void) sym_kmod_load_resources(ctx);
201✔
166
        sym_kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
201✔
167

168
        *ret = TAKE_PTR(ctx);
201✔
169
        return 0;
201✔
170
}
171

172
#endif
173

174
int dlopen_libkmod(int log_level) {
257✔
175
#if HAVE_KMOD
176
        SD_ELF_NOTE_DLOPEN(
257✔
177
                        "kmod",
178
                        "Support for loading kernel modules",
179
                        SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED,
180
                        "libkmod.so.2");
181

182
        return dlopen_many_sym_or_warn(
257✔
183
                        &libkmod_dl,
184
                        "libkmod.so.2",
185
                        log_level,
186
                        DLSYM_ARG(kmod_list_next),
187
                        DLSYM_ARG(kmod_load_resources),
188
                        DLSYM_ARG(kmod_module_get_initstate),
189
                        DLSYM_ARG(kmod_module_get_module),
190
                        DLSYM_ARG(kmod_module_get_name),
191
                        DLSYM_ARG(kmod_module_new_from_lookup),
192
                        DLSYM_ARG(kmod_module_probe_insert_module),
193
                        DLSYM_ARG(kmod_module_unref),
194
                        DLSYM_ARG(kmod_module_unref_list),
195
                        DLSYM_ARG(kmod_new),
196
                        DLSYM_ARG(kmod_set_log_fn),
197
                        DLSYM_ARG(kmod_unref),
198
                        DLSYM_ARG(kmod_validate_resources));
199
#else
200
        return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
201
                              "libkmod support is not compiled in.");
202
#endif
203
}
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