You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Break long lines, adjust phrasing, and add minor clarifications to make
the text more consistent and easier to follow. This is a
formatting-only change with no semantic modifications.
Signed-off-by: Chia-Hao Chiu <[email protected]>
This is because some environment variables are specified by the security policy and cannot be inherited.
279
279
The default security policy is \verb|sudoers|.
280
280
In the \verb|sudoers| security policy, \verb|env_reset| is enabled by default, which restricts environment variables.
281
-
Specifically, path variables are not retained from the user environment; they are set to default values (for more information, see: \href{https://www.sudo.ws/docs/man/sudoers.man/}{sudoers manual}).
281
+
Specifically, path variables are not retained from the user environment;
282
+
they are set to default values (for more information, see: \href{https://www.sudo.ws/docs/man/sudoers.man/}{sudoers manual}).
You now know the basics of creating, compiling, installing and removing modules.
406
407
Now for more of a description of how this module works.
407
408
408
-
Kernel modules must have at least two functions: a "start" (initialization) function called \cpp|init_module()| which is called when the module is \sh|insmod|ed into the kernel, and an "end" (cleanup) function called \cpp|cleanup_module()| which is called just before it is removed from the kernel.
409
+
Kernel modules must have at least two functions: a "start" (initialization) function called \cpp|init_module()| which is called when the module is \sh|insmod|ed into the kernel,
410
+
and an "end" (cleanup) function called \cpp|cleanup_module()| which is called just before it is removed from the kernel.
409
411
Actually, things have changed starting with kernel 2.3.13.
410
412
% TODO: adjust the section anchor
411
-
You can now use whatever name you like for the start and end functions of a module, and you will learn how to do this in Section \ref{hello_n_goodbye}.
413
+
You can now use whatever name you like for the start and end functions of a module,
414
+
and you will learn how to do this in Section \ref{hello_n_goodbye}.
412
415
In fact, the new method is the preferred method.
413
416
However, many people still use \cpp|init_module()| and \cpp|cleanup_module()| for their start and end functions.
414
417
415
-
Typically, \cpp|init_module()| either registers a handler for something with the kernel, or it replaces one of the kernel functions with its own code (usually code to do something and then call the original function).
418
+
Typically, \cpp|init_module()| either registers a handler for something with the kernel,
419
+
or it replaces one of the kernel functions with its own code (usually code to do something and then call the original function).
416
420
The \cpp|cleanup_module()| function is supposed to undo whatever \cpp|init_module()| did, so the module can be unloaded safely.
417
421
418
422
Lastly, every kernel module needs to include \verb|<linux/module.h>|.
Former kernel versions required us to care much about these settings, which are usually stored in Makefiles.
442
446
Although hierarchically organized, many redundant settings accumulated in sublevel Makefiles and made them large and rather difficult to maintain.
443
447
Fortunately, there is a new way of doing these things, called kbuild, and the build process for external loadable modules is now fully integrated into the standard kernel build mechanism.
444
-
To learn more on how to compile modules which are not part of the official kernel (such as all the examples you will find in this guide), see file \src{Documentation/kbuild/modules.rst}.
448
+
To learn more about how to compile modules which are not part of the official kernel (such as all the examples you will find in this guide), see file \src{Documentation/kbuild/modules.rst}.
445
449
446
-
Additional details about Makefiles for kernel modules are available in \src{Documentation/kbuild/makefiles.rst}. Be sure to read this and the related files before starting to hack Makefiles. It will probably save you lots of work.
450
+
Additional details about Makefiles for kernel modules are available in \src{Documentation/kbuild/makefiles.rst}.
451
+
Be sure to read this and the related files before starting to hack Makefiles. It will probably save you lots of work.
In early kernel versions you had to use the \cpp|init_module| and \cpp|cleanup_module| functions, as in the first hello world example, but these days you can name those anything you want by using the \cpp|module_init| and \cpp|module_exit| macros.
463
+
In early kernel versions you had to use the \cpp|init_module| and \cpp|cleanup_module| functions, as in the first hello world example,
464
+
but these days you can name those anything you want by using the \cpp|module_init| and \cpp|module_exit| macros.
459
465
These macros are defined in \src{include/linux/module.h}.
460
466
The only requirement is that your init and cleanup functions must be defined before calling those macros, otherwise you will get compilation errors.
461
467
Here is an example of this technique:
@@ -480,8 +486,10 @@ \subsection{Hello and Goodbye}
480
486
Now have a look at \src{drivers/char/Makefile} for a real world example.
481
487
As you can see, some things got hardwired into the kernel (\verb|obj-y|) but where have all those \verb|obj-m| gone?
482
488
Those familiar with shell scripts will easily be able to spot them.
483
-
For those who are not, the \verb|obj-$(CONFIG_FOO)| entries you see everywhere expand into \verb|obj-y| or \verb|obj-m|, depending on whether the \verb|CONFIG_FOO| variable has been set to \verb|y| or \verb|m|.
484
-
While we are at it, those were exactly the kind of variables that you have set in the \verb|.config| file in the top-level directory of the Linux kernel source tree, the last time you ran \sh|make menuconfig| or something similar.
489
+
For those who are not, the \verb|obj-$(CONFIG_FOO)| entries you see everywhere expand into \verb|obj-y| or \verb|obj-m|,
490
+
depending on whether the \verb|CONFIG_FOO| variable has been set to \verb|y| or \verb|m|.
491
+
While we are at it, those were exactly the kind of variables that you have set in the \verb|.config| file in the top-level directory of the Linux kernel source tree,
492
+
the last time you ran \sh|make menuconfig| or something similar.
485
493
486
494
\subsection{The \_\_init and \_\_exit Macros}
487
495
\label{init_n_exit}
@@ -513,15 +521,15 @@ \subsection{Licensing and Module Documentation}
513
521
They are defined within \src{include/linux/module.h}.
514
522
515
523
To reference what license you are using, a macro is available called \cpp|MODULE_LICENSE|.
516
-
This and a few other macros describing the module are illustrated in the below example.
524
+
This and a few other macros describing the module are illustrated in the example below.
517
525
518
526
\samplec{examples/hello-4.c}
519
527
520
528
\subsection{Passing Command Line Arguments to a Module}
521
529
\label{modparam}
522
530
Modules can take command line arguments, but not with the argc/argv you might be used to.
523
531
524
-
To allow arguments to be passed to your module, declare the variables that will take the values of the command line arguments as global and then use the \cpp|module_param()| macro, (defined in \src{include/linux/moduleparam.h}) to set the mechanism up.
532
+
To allow arguments to be passed to your module, declare the variables that will take the values of the command line arguments as global and then use the \cpp|module_param()| macro (defined in \src{include/linux/moduleparam.h}) to set the mechanism up.
525
533
At runtime, \sh|insmod| will fill the variables with any command line arguments that are given, like \sh|insmod mymodule.ko myvariable=5|.
526
534
The variable declarations and macros should be placed at the beginning of the module for clarity.
527
535
The example code should clear up my admittedly lousy explanation.
@@ -535,7 +543,7 @@ \subsection{Passing Command Line Arguments to a Module}
535
543
\end{code}
536
544
537
545
Arrays are supported too, but things are a bit different now than they were in the olden days.
538
-
To keep track of the number of parameters you need to pass a pointer to a count variable as third parameter.
546
+
To keep track of the number of parameters, you need to pass a pointer to a count variable as third parameter.
539
547
At your option, you could also ignore the count and pass \cpp|NULL| instead. We show both possibilities here:
From v4.14 to v4.15, the timer API made a series of changes to improve memory safety.
1842
1850
A buffer overflow in the area of a \cpp|timer_list| structure may be able to overwrite the \cpp|function| and \cpp|data| fields, providing the attacker with a way to use return-oriented programming (ROP) to call arbitrary functions within the kernel.
1843
-
Also, the function prototype of the callback, containing an\cpp|unsigned long| argument, will prevent work from any type checking.
1851
+
Also, the function prototype of the callback, containing a\cpp|unsigned long| argument, will prevent the compiler from performing type checking.
1844
1852
Furthermore, the function prototype with \cpp|unsigned long| argument may be an obstacle to the forward-edge protection of \textit{control-flow integrity}.
1845
1853
Thus, it is better to use a unique prototype to separate from the cluster that takes an \cpp|unsigned long| argument.
1846
1854
The timer callback should be passed a pointer to the \cpp|timer_list| structure rather than an \cpp|unsigned long| argument.
0 commit comments