man-pages/man2/init_module.2.html
2021-03-31 01:06:50 +01:00

521 lines
12 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of INIT_MODULE</TITLE>
</HEAD><BODY>
<H1>INIT_MODULE</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
init_module, finit_module - load a kernel module
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>int init_module(void *</B><I>module_image</I><B>, unsigned long </B><I>len</I><B>,</B>
<B> const char *</B><I>param_values</I><B>);</B>
<B>int finit_module(int </B><I>fd</I><B>, const char *</B><I>param_values</I><B>,</B>
<B> int </B><I>flags</I><B>);</B>
</PRE>
<P>
<I>Note</I>:
glibc provides no header file declaration of
<B>init_module</B>()
and no wrapper function for
<B>finit_module</B>();
see NOTES.
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>init_module</B>()
loads an ELF image into kernel space,
performs any necessary symbol relocations,
initializes module parameters to values provided by the caller,
and then runs the module's
<I>init</I>
function.
This system call requires privilege.
<P>
The
<I>module_image</I>
argument points to a buffer containing the binary image
to be loaded;
<I>len</I>
specifies the size of that buffer.
The module image should be a valid ELF image, built for the running kernel.
<P>
The
<I>param_values</I>
argument is a string containing space-delimited specifications of the
values for module parameters (defined inside the module using
<B>module_param</B>()
and
<B>module_param_array</B>()).
The kernel parses this string and initializes the specified
parameters.
Each of the parameter specifications has the form:
<P>
<I>name</I>[<B>=</B><I>value</I>[<B>,</B><I>value</I>...]]
<P>
The parameter
<I>name</I>
is one of those defined within the module using
<I>module_param</I>()
(see the Linux kernel source file
<I>include/linux/moduleparam.h</I>).
The parameter
<I>value</I>
is optional in the case of
<I>bool</I>
and
<I>invbool</I>
parameters.
Values for array parameters are specified as a comma-separated list.
<A NAME="lbAE">&nbsp;</A>
<H3>finit_module()</H3>
The
<B>finit_module</B>()
system call is like
<B>init_module</B>(),
but reads the module to be loaded from the file descriptor
<I>fd</I>.
It is useful when the authenticity of a kernel module
can be determined from its location in the filesystem;
in cases where that is possible,
the overhead of using cryptographically signed modules to
determine the authenticity of a module can be avoided.
The
<I>param_values</I>
argument is as for
<B>init_module</B>().
<P>
The
<I>flags</I>
argument modifies the operation of
<B>finit_module</B>().
It is a bit mask value created by ORing
together zero or more of the following flags:
<DL COMPACT>
<DT id="1"><B>MODULE_INIT_IGNORE_MODVERSIONS</B>
<DD>
Ignore symbol version hashes.
<DT id="2"><B>MODULE_INIT_IGNORE_VERMAGIC</B>
<DD>
Ignore kernel version magic.
</DL>
<P>
There are some safety checks built into a module to ensure that
it matches the kernel against which it is loaded.
These checks are recorded when the module is built and
verified when the module is loaded.
First, the module records a &quot;vermagic&quot; string containing
the kernel version number and prominent features (such as the CPU type).
Second, if the module was built with the
<B>CONFIG_MODVERSIONS</B>
configuration option enabled,
a version hash is recorded for each symbol the module uses.
This hash is based on the types of the arguments and return value
for the function named by the symbol.
In this case, the kernel version number within the
&quot;vermagic&quot; string is ignored,
as the symbol version hashes are assumed to be sufficiently reliable.
<P>
Using the
<B>MODULE_INIT_IGNORE_VERMAGIC</B>
flag indicates that the &quot;vermagic&quot; string is to be ignored, and the
<B>MODULE_INIT_IGNORE_MODVERSIONS</B>
flag indicates that the symbol version hashes are to be ignored.
If the kernel is built to permit forced loading (i.e., configured with
<B>CONFIG_MODULE_FORCE_LOAD</B>),
then loading continues, otherwise it fails with the error
<B>ENOEXEC</B>
as expected for malformed modules.
<A NAME="lbAF">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, these system calls return 0.
On error, -1 is returned and
<I>errno</I>
is set appropriately.
<A NAME="lbAG">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="3"><B>EBADMSG</B> (since Linux 3.7)
<DD>
Module signature is misformatted.
<DT id="4"><B>EBUSY</B>
<DD>
Timeout while trying to resolve a symbol reference by this module.
<DT id="5"><B>EFAULT</B>
<DD>
An address argument referred to a location that
is outside the process's accessible address space.
<DT id="6"><B>ENOKEY</B> (since Linux 3.7)
<DD>
Module signature is invalid or
the kernel does not have a key for this module.
This error is returned only if the kernel was configured with
<B>CONFIG_MODULE_SIG_FORCE</B>;
if the kernel was not configured with this option,
then an invalid or unsigned module simply taints the kernel.
<DT id="7"><B>ENOMEM</B>
<DD>
Out of memory.
<DT id="8"><B>EPERM</B>
<DD>
The caller was not privileged
(did not have the
<B>CAP_SYS_MODULE</B>
capability),
or module loading is disabled
(see
<I>/proc/sys/kernel/modules_disabled</I>
in
<B><A HREF="/cgi-bin/man/man2html?5+proc">proc</A></B>(5)).
</DL>
<P>
The following errors may additionally occur for
<B>init_module</B>():
<DL COMPACT>
<DT id="9"><B>EEXIST</B>
<DD>
A module with this name is already loaded.
<DT id="10"><B>EINVAL</B>
<DD>
<I>param_values</I>
is invalid, or some part of the ELF image in
<I>module_image</I>
contains inconsistencies.
<DT id="11"><B>ENOEXEC</B>
<DD>
The binary image supplied in
<I>module_image</I>
is not an ELF image,
or is an ELF image that is invalid or for a different architecture.
</DL>
<P>
The following errors may additionally occur for
<B>finit_module</B>():
<DL COMPACT>
<DT id="12"><B>EBADF</B>
<DD>
The file referred to by
<I>fd</I>
is not opened for reading.
<DT id="13"><B>EFBIG</B>
<DD>
The file referred to by
<I>fd</I>
is too large.
<DT id="14"><B>EINVAL</B>
<DD>
<I>flags</I>
is invalid.
<DT id="15"><B>ENOEXEC</B>
<DD>
<I>fd</I>
does not refer to an open file.
</DL>
<P>
In addition to the above errors, if the module's
<I>init</I>
function is executed and returns an error, then
<B>init_module</B>()
or
<B>finit_module</B>()
fails and
<I>errno</I>
is set to the value returned by the
<I>init</I>
function.
<A NAME="lbAH">&nbsp;</A>
<H2>VERSIONS</H2>
<B>finit_module</B>()
is available since Linux 3.8.
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>init_module</B>()
and
<B>finit_module</B>()
are Linux-specific.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
The
<B>init_module</B>()
system call is not supported by glibc.
No declaration is provided in glibc headers, but, through a quirk of history,
glibc versions before 2.23 did export an ABI for this system call.
Therefore, in order to employ this system call,
it is (before glibc 2.23) sufficient to
manually declare the interface in your code;
alternatively, you can invoke the system call using
<B><A HREF="/cgi-bin/man/man2html?2+syscall">syscall</A></B>(2).
<P>
Glibc does not provide a wrapper for
<B>finit_module</B>();
call it using
<B><A HREF="/cgi-bin/man/man2html?2+syscall">syscall</A></B>(2).
<P>
Information about currently loaded modules can be found in
<I>/proc/modules</I>
and in the file trees under the per-module subdirectories under
<I>/sys/module</I>.
<P>
See the Linux kernel source file
<I>include/linux/module.h</I>
for some useful background information.
<A NAME="lbAK">&nbsp;</A>
<H3>Linux 2.4 and earlier</H3>
<P>
In Linux 2.4 and earlier, the
<B>init_module</B>()
system call was rather different:
<P>
<B> #include &lt;<A HREF="file:///usr/include/linux/module.h">linux/module.h</A>&gt;</B>
<P>
<B> int init_module(const char *</B><I>name</I><B>, struct module *</B><I>image</I><B>);</B>
<P>
(User-space applications can detect which version of
<B>init_module</B>()
is available by calling
<B>query_module</B>();
the latter call fails with the error
<B>ENOSYS</B>
on Linux 2.6 and later.)
<P>
The older version of the system call
loads the relocated module image pointed to by
<I>image</I>
into kernel space and runs the module's
<I>init</I>
function.
The caller is responsible for providing the relocated image (since
Linux 2.6, the
<B>init_module</B>()
system call does the relocation).
<P>
The module image begins with a module structure and is followed by
code and data as appropriate.
Since Linux 2.2, the module structure is defined as follows:
<P>
struct module {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;long&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size_of_struct;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;module&nbsp;&nbsp;&nbsp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;&nbsp;&nbsp;&nbsp;void&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(*cleanup)(void);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;struct&nbsp;exception_table_entry&nbsp;*ex_table_start;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;struct&nbsp;exception_table_entry&nbsp;*ex_table_end;
#ifdef __alpha__
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;long&nbsp;gp;
#endif
};
<P>
All of the pointer fields, with the exception of
<I>next</I>
and
<I>refs</I>,
are expected to point within the module body and be
initialized as appropriate for kernel space, that is, relocated with
the rest of the module.
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+create_module">create_module</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+delete_module">delete_module</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+query_module">query_module</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?8+lsmod">lsmod</A></B>(8),
<B><A HREF="/cgi-bin/man/man2html?8+modprobe">modprobe</A></B>(8)
<A NAME="lbAM">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="16"><A HREF="#lbAB">NAME</A><DD>
<DT id="17"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="18"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="19"><A HREF="#lbAE">finit_module()</A><DD>
</DL>
<DT id="20"><A HREF="#lbAF">RETURN VALUE</A><DD>
<DT id="21"><A HREF="#lbAG">ERRORS</A><DD>
<DT id="22"><A HREF="#lbAH">VERSIONS</A><DD>
<DT id="23"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="24"><A HREF="#lbAJ">NOTES</A><DD>
<DL>
<DT id="25"><A HREF="#lbAK">Linux 2.4 and earlier</A><DD>
</DL>
<DT id="26"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="27"><A HREF="#lbAM">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:32 GMT, March 31, 2021
</BODY>
</HTML>