Blame man2/init_module.2

Packit 7cfc04
.\" Copyright (C) 2012 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\" A few fragments remain from a version
Packit 7cfc04
.\" Copyright (C) 1996 Free Software Foundation, Inc.
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH INIT_MODULE 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
init_module, finit_module \- load a kernel module
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.BI "int init_module(void *" module_image ", unsigned long " len ,
Packit 7cfc04
.BI "                const char *" param_values );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int finit_module(int " fd ", const char *" param_values ,
Packit 7cfc04
.BI "                 int " flags );
Packit 7cfc04
.fi
Packit 7cfc04
.PP
Packit 7cfc04
.IR Note :
Packit 7cfc04
glibc provides no header file declaration of
Packit 7cfc04
.BR init_module ()
Packit 7cfc04
and no wrapper function for
Packit 7cfc04
.BR finit_module ();
Packit 7cfc04
see NOTES.
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.BR init_module ()
Packit 7cfc04
loads an ELF image into kernel space,
Packit 7cfc04
performs any necessary symbol relocations,
Packit 7cfc04
initializes module parameters to values provided by the caller,
Packit 7cfc04
and then runs the module's
Packit 7cfc04
.I init
Packit 7cfc04
function.
Packit 7cfc04
This system call requires privilege.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I module_image
Packit 7cfc04
argument points to a buffer containing the binary image
Packit 7cfc04
to be loaded;
Packit 7cfc04
.I len
Packit 7cfc04
specifies the size of that buffer.
Packit 7cfc04
The module image should be a valid ELF image, built for the running kernel.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I param_values
Packit 7cfc04
argument is a string containing space-delimited specifications of the
Packit 7cfc04
values for module parameters (defined inside the module using
Packit 7cfc04
.BR module_param ()
Packit 7cfc04
and
Packit 7cfc04
.BR module_param_array ()).
Packit 7cfc04
The kernel parses this string and initializes the specified
Packit 7cfc04
parameters.
Packit 7cfc04
Each of the parameter specifications has the form:
Packit 7cfc04
.PP
Packit 7cfc04
.RI "        " name [\c
Packit 7cfc04
.BI = value\c
Packit 7cfc04
.RB [ ,\c
Packit 7cfc04
.IR value ...]]
Packit 7cfc04
.PP
Packit 7cfc04
The parameter
Packit 7cfc04
.I name
Packit 7cfc04
is one of those defined within the module using
Packit 7cfc04
.IR module_param ()
Packit 7cfc04
(see the Linux kernel source file
Packit 7cfc04
.IR include/linux/moduleparam.h ).
Packit 7cfc04
The parameter
Packit 7cfc04
.I value
Packit 7cfc04
is optional in the case of
Packit 7cfc04
.I bool
Packit 7cfc04
and
Packit 7cfc04
.I invbool
Packit 7cfc04
parameters.
Packit 7cfc04
Values for array parameters are specified as a comma-separated list.
Packit 7cfc04
.SS finit_module()
Packit 7cfc04
The
Packit 7cfc04
.BR finit_module ()
Packit 7cfc04
.\" commit 34e1169d996ab148490c01b65b4ee371cf8ffba2
Packit 7cfc04
.\" https://lwn.net/Articles/519010/
Packit 7cfc04
system call is like
Packit 7cfc04
.BR init_module (),
Packit 7cfc04
but reads the module to be loaded from the file descriptor
Packit 7cfc04
.IR fd .
Packit 7cfc04
It is useful when the authenticity of a kernel module
Packit 7cfc04
can be determined from its location in the filesystem;
Packit 7cfc04
in cases where that is possible,
Packit 7cfc04
the overhead of using cryptographically signed modules to
Packit 7cfc04
determine the authenticity of a module can be avoided.
Packit 7cfc04
The
Packit 7cfc04
.I param_values
Packit 7cfc04
argument is as for
Packit 7cfc04
.BR init_module ().
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I flags
Packit 7cfc04
argument modifies the operation of
Packit 7cfc04
.BR finit_module ().
Packit 7cfc04
It is a bit mask value created by ORing
Packit 7cfc04
together zero or more of the following flags:
Packit 7cfc04
.\" commit 2f3238aebedb243804f58d62d57244edec4149b2
Packit 7cfc04
.TP
Packit 7cfc04
.B MODULE_INIT_IGNORE_MODVERSIONS
Packit 7cfc04
Ignore symbol version hashes.
Packit 7cfc04
.TP
Packit 7cfc04
.B MODULE_INIT_IGNORE_VERMAGIC
Packit 7cfc04
Ignore kernel version magic.
Packit 7cfc04
.PP
Packit 7cfc04
There are some safety checks built into a module to ensure that
Packit 7cfc04
it matches the kernel against which it is loaded.
Packit 7cfc04
.\" http://www.tldp.org/HOWTO/Module-HOWTO/basekerncompat.html
Packit 7cfc04
.\" is dated, but informative
Packit 7cfc04
These checks are recorded when the module is built and
Packit 7cfc04
verified when the module is loaded.
Packit 7cfc04
First, the module records a "vermagic" string containing
Packit 7cfc04
the kernel version number and prominent features (such as the CPU type).
Packit 7cfc04
Second, if the module was built with the
Packit 7cfc04
.B CONFIG_MODVERSIONS
Packit 7cfc04
configuration option enabled,
Packit 7cfc04
a version hash is recorded for each symbol the module uses.
Packit 7cfc04
This hash is based on the types of the arguments and return value
Packit 7cfc04
for the function named by the symbol.
Packit 7cfc04
In this case, the kernel version number within the
Packit 7cfc04
"vermagic" string is ignored,
Packit 7cfc04
as the symbol version hashes are assumed to be sufficiently reliable.
Packit 7cfc04
.PP
Packit 7cfc04
Using the
Packit 7cfc04
.B MODULE_INIT_IGNORE_VERMAGIC
Packit 7cfc04
flag indicates that the "vermagic" string is to be ignored, and the
Packit 7cfc04
.B MODULE_INIT_IGNORE_MODVERSIONS
Packit 7cfc04
flag indicates that the symbol version hashes are to be ignored.
Packit 7cfc04
If the kernel is built to permit forced loading (i.e., configured with
Packit 7cfc04
.BR CONFIG_MODULE_FORCE_LOAD ),
Packit 7cfc04
then loading continues, otherwise it fails with the error
Packit 7cfc04
.B ENOEXEC
Packit 7cfc04
as expected for malformed modules.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, these system calls return 0.
Packit 7cfc04
On error, \-1 is returned and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.BR EBADMSG " (since Linux 3.7)"
Packit 7cfc04
Module signature is misformatted.
Packit 7cfc04
.TP
Packit 7cfc04
.B EBUSY
Packit 7cfc04
Timeout while trying to resolve a symbol reference by this module.
Packit 7cfc04
.TP
Packit 7cfc04
.B EFAULT
Packit 7cfc04
An address argument referred to a location that
Packit 7cfc04
is outside the process's accessible address space.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOKEY " (since Linux 3.7)"
Packit 7cfc04
.\" commit 48ba2462ace6072741fd8d0058207d630ce93bf1
Packit 7cfc04
.\" commit 1d0059f3a468825b5fc5405c636a2f6e02707ffa
Packit 7cfc04
.\" commit 106a4ee258d14818467829bf0e12aeae14c16cd7
Packit 7cfc04
Module signature is invalid or
Packit 7cfc04
the kernel does not have a key for this module.
Packit 7cfc04
This error is returned only if the kernel was configured with
Packit 7cfc04
.BR CONFIG_MODULE_SIG_FORCE ;
Packit 7cfc04
if the kernel was not configured with this option,
Packit 7cfc04
then an invalid or unsigned module simply taints the kernel.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
Out of memory.
Packit 7cfc04
.TP
Packit 7cfc04
.B EPERM
Packit 7cfc04
The caller was not privileged
Packit 7cfc04
(did not have the
Packit 7cfc04
.B CAP_SYS_MODULE
Packit 7cfc04
capability),
Packit 7cfc04
or module loading is disabled
Packit 7cfc04
(see
Packit 7cfc04
.IR /proc/sys/kernel/modules_disabled
Packit 7cfc04
in
Packit 7cfc04
.BR proc (5)).
Packit 7cfc04
.PP
Packit 7cfc04
The following errors may additionally occur for
Packit 7cfc04
.BR init_module ():
Packit 7cfc04
.TP
Packit 7cfc04
.B EEXIST
Packit 7cfc04
A module with this name is already loaded.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.I param_values
Packit 7cfc04
is invalid, or some part of the ELF image in
Packit 7cfc04
.IR module_image
Packit 7cfc04
contains inconsistencies.
Packit 7cfc04
.\" .TP
Packit 7cfc04
.\" .BR EINVAL " (Linux 2.4 and earlier)"
Packit 7cfc04
.\" Some
Packit 7cfc04
.\" .I image
Packit 7cfc04
.\" slot is filled in incorrectly,
Packit 7cfc04
.\" .I image\->name
Packit 7cfc04
.\" does not correspond to the original module name, some
Packit 7cfc04
.\" .I image\->deps
Packit 7cfc04
.\" entry does not correspond to a loaded module,
Packit 7cfc04
.\" or some other similar inconsistency.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOEXEC
Packit 7cfc04
The binary image supplied in
Packit 7cfc04
.I module_image
Packit 7cfc04
is not an ELF image,
Packit 7cfc04
or is an ELF image that is invalid or for a different architecture.
Packit 7cfc04
.PP
Packit 7cfc04
The following errors may additionally occur for
Packit 7cfc04
.BR finit_module ():
Packit 7cfc04
.TP
Packit 7cfc04
.B EBADF
Packit 7cfc04
The file referred to by
Packit 7cfc04
.I fd
Packit 7cfc04
is not opened for reading.
Packit 7cfc04
.TP
Packit 7cfc04
.B EFBIG
Packit 7cfc04
The file referred to by
Packit 7cfc04
.I fd
Packit 7cfc04
is too large.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.I flags
Packit 7cfc04
is invalid.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOEXEC
Packit 7cfc04
.I fd
Packit 7cfc04
does not refer to an open file.
Packit 7cfc04
.PP
Packit 7cfc04
In addition to the above errors, if the module's
Packit 7cfc04
.I init
Packit 7cfc04
function is executed and returns an error, then
Packit 7cfc04
.BR init_module ()
Packit 7cfc04
or
Packit 7cfc04
.BR finit_module ()
Packit 7cfc04
fails and
Packit 7cfc04
.I errno
Packit 7cfc04
is set to the value returned by the
Packit 7cfc04
.I init
Packit 7cfc04
function.
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
.BR finit_module ()
Packit 7cfc04
is available since Linux 3.8.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
.BR init_module ()
Packit 7cfc04
and
Packit 7cfc04
.BR finit_module ()
Packit 7cfc04
are Linux-specific.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
The
Packit 7cfc04
.BR init_module ()
Packit 7cfc04
system call is not supported by glibc.
Packit 7cfc04
No declaration is provided in glibc headers, but, through a quirk of history,
Packit 7cfc04
glibc versions before 2.23 did export an ABI for this system call.
Packit 7cfc04
Therefore, in order to employ this system call,
Packit 7cfc04
it is (before glibc 2.23) sufficient to
Packit 7cfc04
manually declare the interface in your code;
Packit 7cfc04
alternatively, you can invoke the system call using
Packit 7cfc04
.BR syscall (2).
Packit 7cfc04
.PP
Packit 7cfc04
Glibc does not provide a wrapper for
Packit 7cfc04
.BR finit_module ();
Packit 7cfc04
call it using
Packit 7cfc04
.BR syscall (2).
Packit 7cfc04
.PP
Packit 7cfc04
Information about currently loaded modules can be found in
Packit 7cfc04
.IR /proc/modules
Packit 7cfc04
and in the file trees under the per-module subdirectories under
Packit 7cfc04
.IR /sys/module .
Packit 7cfc04
.PP
Packit 7cfc04
See the Linux kernel source file
Packit 7cfc04
.I include/linux/module.h
Packit 7cfc04
for some useful background information.
Packit 7cfc04
.SS Linux 2.4 and earlier
Packit 7cfc04
.PP
Packit 7cfc04
In Linux 2.4 and earlier, the
Packit 7cfc04
.BR init_module ()
Packit 7cfc04
system call was rather different:
Packit 7cfc04
.PP
Packit 7cfc04
.B "    #include <linux/module.h>"
Packit 7cfc04
.PP
Packit 7cfc04
.BI "    int init_module(const char *" name ", struct module *" image );
Packit 7cfc04
.PP
Packit 7cfc04
(User-space applications can detect which version of
Packit 7cfc04
.BR init_module ()
Packit 7cfc04
is available by calling
Packit 7cfc04
.BR query_module ();
Packit 7cfc04
the latter call fails with the error
Packit 7cfc04
.BR ENOSYS
Packit 7cfc04
on Linux 2.6 and later.)
Packit 7cfc04
.PP
Packit 7cfc04
The older version of the system call
Packit 7cfc04
loads the relocated module image pointed to by
Packit 7cfc04
.I image
Packit 7cfc04
into kernel space and runs the module's
Packit 7cfc04
.I init
Packit 7cfc04
function.
Packit 7cfc04
The caller is responsible for providing the relocated image (since
Packit 7cfc04
Linux 2.6, the
Packit 7cfc04
.BR init_module ()
Packit 7cfc04
system call does the relocation).
Packit 7cfc04
.PP
Packit 7cfc04
The module image begins with a module structure and is followed by
Packit 7cfc04
code and data as appropriate.
Packit 7cfc04
Since Linux 2.2, the module structure is defined as follows:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct module {
Packit 7cfc04
    unsigned long         size_of_struct;
Packit 7cfc04
    struct module        *next;
Packit 7cfc04
    const char           *name;
Packit 7cfc04
    unsigned long         size;
Packit 7cfc04
    long                  usecount;
Packit 7cfc04
    unsigned long         flags;
Packit 7cfc04
    unsigned int          nsyms;
Packit 7cfc04
    unsigned int          ndeps;
Packit 7cfc04
    struct module_symbol *syms;
Packit 7cfc04
    struct module_ref    *deps;
Packit 7cfc04
    struct module_ref    *refs;
Packit 7cfc04
    int                 (*init)(void);
Packit 7cfc04
    void                (*cleanup)(void);
Packit 7cfc04
    const struct exception_table_entry *ex_table_start;
Packit 7cfc04
    const struct exception_table_entry *ex_table_end;
Packit 7cfc04
#ifdef __alpha__
Packit 7cfc04
    unsigned long gp;
Packit 7cfc04
#endif
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
All of the pointer fields, with the exception of
Packit 7cfc04
.I next
Packit 7cfc04
and
Packit 7cfc04
.IR refs ,
Packit 7cfc04
are expected to point within the module body and be
Packit 7cfc04
initialized as appropriate for kernel space, that is, relocated with
Packit 7cfc04
the rest of the module.
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR create_module (2),
Packit 7cfc04
.BR delete_module (2),
Packit 7cfc04
.BR query_module (2),
Packit 7cfc04
.BR lsmod (8),
Packit 7cfc04
.BR modprobe (8)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.