1init_module(2) System Calls Manual init_module(2)
2
3
4
6 init_module, finit_module - load a kernel module
7
9 Standard C library (libc, -lc)
10
12 #include <linux/module.h> /* Definition of MODULE_* constants */
13 #include <sys/syscall.h> /* Definition of SYS_* constants */
14 #include <unistd.h>
15
16 int syscall(SYS_init_module, void module_image[.len], unsigned long len,
17 const char *param_values);
18 int syscall(SYS_finit_module, int fd,
19 const char *param_values, int flags);
20
21 Note: glibc provides no wrappers for these system calls, necessitating
22 the use of syscall(2).
23
25 init_module() loads an ELF image into kernel space, performs any neces‐
26 sary symbol relocations, initializes module parameters to values pro‐
27 vided by the caller, and then runs the module's init function. This
28 system call requires privilege.
29
30 The module_image argument points to a buffer containing the binary im‐
31 age to be loaded; len specifies the size of that buffer. The module
32 image should be a valid ELF image, built for the running kernel.
33
34 The param_values argument is a string containing space-delimited speci‐
35 fications of the values for module parameters (defined inside the mod‐
36 ule using module_param() and module_param_array()). The kernel parses
37 this string and initializes the specified parameters. Each of the pa‐
38 rameter specifications has the form:
39
40 name[=value[,value...]]
41
42 The parameter name is one of those defined within the module using mod‐
43 ule_param() (see the Linux kernel source file include/linux/mod‐
44 uleparam.h). The parameter value is optional in the case of bool and
45 invbool parameters. Values for array parameters are specified as a
46 comma-separated list.
47
48 finit_module()
49 The finit_module() system call is like init_module(), but reads the
50 module to be loaded from the file descriptor fd. It is useful when the
51 authenticity of a kernel module can be determined from its location in
52 the filesystem; in cases where that is possible, the overhead of using
53 cryptographically signed modules to determine the authenticity of a
54 module can be avoided. The param_values argument is as for init_mod‐
55 ule().
56
57 The flags argument modifies the operation of finit_module(). It is a
58 bit mask value created by ORing together zero or more of the following
59 flags:
60
61 MODULE_INIT_IGNORE_MODVERSIONS
62 Ignore symbol version hashes.
63
64 MODULE_INIT_IGNORE_VERMAGIC
65 Ignore kernel version magic.
66
67 There are some safety checks built into a module to ensure that it
68 matches the kernel against which it is loaded. These checks are
69 recorded when the module is built and verified when the module is
70 loaded. First, the module records a "vermagic" string containing the
71 kernel version number and prominent features (such as the CPU type).
72 Second, if the module was built with the CONFIG_MODVERSIONS configura‐
73 tion option enabled, a version hash is recorded for each symbol the
74 module uses. This hash is based on the types of the arguments and re‐
75 turn value for the function named by the symbol. In this case, the
76 kernel version number within the "vermagic" string is ignored, as the
77 symbol version hashes are assumed to be sufficiently reliable.
78
79 Using the MODULE_INIT_IGNORE_VERMAGIC flag indicates that the "ver‐
80 magic" string is to be ignored, and the MODULE_INIT_IGNORE_MODVERSIONS
81 flag indicates that the symbol version hashes are to be ignored. If
82 the kernel is built to permit forced loading (i.e., configured with
83 CONFIG_MODULE_FORCE_LOAD), then loading continues, otherwise it fails
84 with the error ENOEXEC as expected for malformed modules.
85
87 On success, these system calls return 0. On error, -1 is returned and
88 errno is set to indicate the error.
89
91 EBADMSG (since Linux 3.7)
92 Module signature is misformatted.
93
94 EBUSY Timeout while trying to resolve a symbol reference by this mod‐
95 ule.
96
97 EFAULT An address argument referred to a location that is outside the
98 process's accessible address space.
99
100 ENOKEY (since Linux 3.7)
101 Module signature is invalid or the kernel does not have a key
102 for this module. This error is returned only if the kernel was
103 configured with CONFIG_MODULE_SIG_FORCE; if the kernel was not
104 configured with this option, then an invalid or unsigned module
105 simply taints the kernel.
106
107 ENOMEM Out of memory.
108
109 EPERM The caller was not privileged (did not have the CAP_SYS_MODULE
110 capability), or module loading is disabled (see /proc/sys/ker‐
111 nel/modules_disabled in proc(5)).
112
113 The following errors may additionally occur for init_module():
114
115 EEXIST A module with this name is already loaded.
116
117 EINVAL param_values is invalid, or some part of the ELF image in mod‐
118 ule_image contains inconsistencies.
119
120 ENOEXEC
121 The binary image supplied in module_image is not an ELF image,
122 or is an ELF image that is invalid or for a different architec‐
123 ture.
124
125 The following errors may additionally occur for finit_module():
126
127 EBADF The file referred to by fd is not opened for reading.
128
129 EFBIG The file referred to by fd is too large.
130
131 EINVAL flags is invalid.
132
133 ENOEXEC
134 fd does not refer to an open file.
135
136 ETXTBSY (since Linux 4.7)
137 The file referred to by fd is opened for read-write.
138
139 In addition to the above errors, if the module's init function is exe‐
140 cuted and returns an error, then init_module() or finit_module() fails
141 and errno is set to the value returned by the init function.
142
144 Linux.
145
147 finit_module()
148 Linux 3.8.
149
150 The init_module() system call is not supported by glibc. No declara‐
151 tion is provided in glibc headers, but, through a quirk of history,
152 glibc versions before glibc 2.23 did export an ABI for this system
153 call. Therefore, in order to employ this system call, it is (before
154 glibc 2.23) sufficient to manually declare the interface in your code;
155 alternatively, you can invoke the system call using syscall(2).
156
157 Linux 2.4 and earlier
158 In Linux 2.4 and earlier, the init_module() system call was rather dif‐
159 ferent:
160
161 #include <linux/module.h>
162
163 int init_module(const char *name, struct module *image);
164
165 (User-space applications can detect which version of init_module() is
166 available by calling query_module(); the latter call fails with the er‐
167 ror ENOSYS on Linux 2.6 and later.)
168
169 The older version of the system call loads the relocated module image
170 pointed to by image into kernel space and runs the module's init func‐
171 tion. The caller is responsible for providing the relocated image
172 (since Linux 2.6, the init_module() system call does the relocation).
173
174 The module image begins with a module structure and is followed by code
175 and data as appropriate. Since Linux 2.2, the module structure is de‐
176 fined as follows:
177
178 struct module {
179 unsigned long size_of_struct;
180 struct module *next;
181 const char *name;
182 unsigned long size;
183 long usecount;
184 unsigned long flags;
185 unsigned int nsyms;
186 unsigned int ndeps;
187 struct module_symbol *syms;
188 struct module_ref *deps;
189 struct module_ref *refs;
190 int (*init)(void);
191 void (*cleanup)(void);
192 const struct exception_table_entry *ex_table_start;
193 const struct exception_table_entry *ex_table_end;
194 #ifdef __alpha__
195 unsigned long gp;
196 #endif
197 };
198
199 All of the pointer fields, with the exception of next and refs, are ex‐
200 pected to point within the module body and be initialized as appropri‐
201 ate for kernel space, that is, relocated with the rest of the module.
202
204 Information about currently loaded modules can be found in /proc/mod‐
205 ules and in the file trees under the per-module subdirectories under
206 /sys/module.
207
208 See the Linux kernel source file include/linux/module.h for some useful
209 background information.
210
212 create_module(2), delete_module(2), query_module(2), lsmod(8), mod‐
213 probe(8)
214
215
216
217Linux man-pages 6.04 2023-03-30 init_module(2)