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