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

systemd / systemd / 23624534405

26 Mar 2026 07:08PM UTC coverage: 72.113% (-0.2%) from 72.361%
23624534405

push

github

web-flow
Make imds networking unlocked by default (#41359)

316838 of 439364 relevant lines covered (72.11%)

1174324.03 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
int dlopen_libkmod(void) {
263✔
31
        SD_ELF_NOTE_DLOPEN(
263✔
32
                        "kmod",
33
                        "Support for loading kernel modules",
34
                        SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED,
35
                        "libkmod.so.2");
36

37
        return dlopen_many_sym_or_warn(
263✔
38
                        &libkmod_dl,
39
                        "libkmod.so.2",
40
                        LOG_DEBUG,
41
                        DLSYM_ARG(kmod_list_next),
42
                        DLSYM_ARG(kmod_load_resources),
43
                        DLSYM_ARG(kmod_module_get_initstate),
44
                        DLSYM_ARG(kmod_module_get_module),
45
                        DLSYM_ARG(kmod_module_get_name),
46
                        DLSYM_ARG(kmod_module_new_from_lookup),
47
                        DLSYM_ARG(kmod_module_probe_insert_module),
48
                        DLSYM_ARG(kmod_module_unref),
49
                        DLSYM_ARG(kmod_module_unref_list),
50
                        DLSYM_ARG(kmod_new),
51
                        DLSYM_ARG(kmod_set_log_fn),
52
                        DLSYM_ARG(kmod_unref),
53
                        DLSYM_ARG(kmod_validate_resources));
54
}
55

56
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
×
57
        char ***denylist = ASSERT_PTR(data);
×
58
        int r;
×
59

60
        if (proc_cmdline_key_streq(key, "module_blacklist")) {
×
61

62
                if (proc_cmdline_value_missing(key, value))
×
63
                        return 0;
64

65
                r = strv_split_and_extend(denylist, value, ",", /* filter_duplicates= */ true);
×
66
                if (r < 0)
×
67
                        return r;
×
68
        }
69

70
        return 0;
71
}
72

73
int module_load_and_warn(struct kmod_ctx *ctx, const char *module, bool verbose) {
4,096✔
74
        _cleanup_(kmod_module_unref_listp) struct kmod_list *modlist = NULL;
4,096✔
75
        _cleanup_strv_free_ char **denylist = NULL;
4,096✔
76
        bool denylist_parsed = false;
4,096✔
77
        struct kmod_list *itr;
4,096✔
78
        int r;
4,096✔
79

80
        assert(ctx);
4,096✔
81
        assert(module);
4,096✔
82

83
        /* verbose==true means we should log at non-debug level if we
84
         * fail to find or load the module. */
85

86
        log_debug("Loading module: %s", module);
4,096✔
87

88
        r = sym_kmod_module_new_from_lookup(ctx, module, &modlist);
4,096✔
89
        if (r < 0)
4,096✔
90
                return log_full_errno(verbose ? LOG_ERR : LOG_DEBUG, r,
×
91
                                      "Failed to look up module alias '%s': %m", module);
92

93
        if (!modlist)
4,096✔
94
                return log_full_errno(verbose ? LOG_ERR : LOG_DEBUG,
1,533✔
95
                                      SYNTHETIC_ERRNO(ENOENT),
96
                                      "Failed to find module '%s'", module);
97

98
        sym_kmod_list_foreach(itr, modlist) {
6,290✔
99
                _cleanup_(kmod_module_unrefp) struct kmod_module *mod = NULL;
3,727✔
100
                int state, err;
3,727✔
101

102
                mod = sym_kmod_module_get_module(itr);
3,727✔
103
                state = sym_kmod_module_get_initstate(mod);
3,727✔
104

105
                switch (state) {
3,727✔
106
                case KMOD_MODULE_BUILTIN:
107
                        log_full(verbose ? LOG_INFO : LOG_DEBUG,
1,672✔
108
                                 "Module '%s' is built in", sym_kmod_module_get_name(mod));
109
                        break;
110

111
                case KMOD_MODULE_LIVE:
112
                        log_debug("Module '%s' is already loaded", sym_kmod_module_get_name(mod));
579✔
113
                        break;
114

115
                default:
1,476✔
116
                        err = sym_kmod_module_probe_insert_module(
1,476✔
117
                                        mod,
118
                                        KMOD_PROBE_APPLY_BLACKLIST,
119
                                        /* extra_options= */ NULL,
120
                                        /* run_install= */ NULL,
121
                                        /* data= */ NULL,
122
                                        /* print_action= */ NULL);
123
                        if (err == 0)
1,476✔
124
                                log_full(verbose ? LOG_INFO : LOG_DEBUG,
1,153✔
125
                                         "Inserted module '%s'", sym_kmod_module_get_name(mod));
126
                        else if (err == KMOD_PROBE_APPLY_BLACKLIST)
323✔
127
                                log_full(verbose ? LOG_INFO : LOG_DEBUG,
×
128
                                         "Module '%s' is deny-listed (by kmod)", sym_kmod_module_get_name(mod));
129
                        else {
130
                                assert(err < 0);
323✔
131

132
                                if (err == -EPERM) {
323✔
133
                                        if (!denylist_parsed) {
×
134
                                                r = proc_cmdline_parse(parse_proc_cmdline_item, &denylist, 0);
×
135
                                                if (r < 0)
×
136
                                                        log_full_errno(!verbose ? LOG_DEBUG : LOG_WARNING,
×
137
                                                                       r,
138
                                                                       "Failed to parse kernel command line, ignoring: %m");
139

140
                                                denylist_parsed = true;
141
                                        }
142
                                        if (strv_contains(denylist, sym_kmod_module_get_name(mod))) {
×
143
                                                log_full(verbose ? LOG_INFO : LOG_DEBUG,
×
144
                                                         "Module '%s' is deny-listed (by kernel)", sym_kmod_module_get_name(mod));
145
                                                continue;
×
146
                                        }
147
                                }
148

149
                                log_full_errno(!verbose ? LOG_DEBUG :
323✔
150
                                               err == -ENODEV ? LOG_NOTICE :
151
                                               err == -ENOENT ? LOG_WARNING :
152
                                                                LOG_ERR,
153
                                               err,
154
                                               "Failed to insert module '%s': %m",
155
                                               sym_kmod_module_get_name(mod));
156
                                if (!IN_SET(err, -ENODEV, -ENOENT))
323✔
157
                                        r = err;
×
158
                        }
159
                }
160
        }
161

162
        return r;
163
}
164

165
_printf_(6,0) static void systemd_kmod_log(
×
166
                void *data,
167
                int priority,
168
                const char *file,
169
                int line,
170
                const char *fn,
171
                const char *format,
172
                va_list args) {
173

174
        log_internalv(priority, 0, file, line, fn, format, args);
×
175
}
×
176

177
int module_setup_context(struct kmod_ctx **ret) {
204✔
178
        _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
204✔
179
        int r;
204✔
180

181
        assert(ret);
204✔
182

183
        r = dlopen_libkmod();
204✔
184
        if (r < 0)
204✔
185
                return r;
186

187
        ctx = sym_kmod_new(NULL, NULL);
204✔
188
        if (!ctx)
204✔
189
                return -ENOMEM;
190

191
        (void) sym_kmod_load_resources(ctx);
204✔
192
        sym_kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
204✔
193

194
        *ret = TAKE_PTR(ctx);
204✔
195
        return 0;
204✔
196
}
197

198
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc