How to make a call to the command line execution function (exec() or something similar) from kernel mode?
I compiled the code in userspace mode and it worked:
[root@vm1 mymod]# cat tst2.c
#include <unistd.h>int main()
{
char *cmd = { "ls", "-l", (char *)0 };
execv ("/bin/ls", cmd);
}
[root@vm1 mymod]# gcc tst2.c
[root@vm1 mymod]# ll
total 52
-rw-r--r--. 1 root root 213 Feb 11 10:24 Makefile
-rw-r--r--. 1 root root 214 Feb 11 10:19 Makefile_1
-rwxr-xr-x. 1 root root 17440 Feb 11 10:29 a.out
-rw-r--r--. 1 root root 221 Feb 10 15:58 test.c
-rw-r--r--. 1 root root 259 Feb 11 10:18 test2.c
-rw-r--r--. 1 root root 205 Feb 10 15:41 test3.c
-rw-r--r--. 1 root root 330 Feb 10 15:55 test4.c
-rw-r--r--. 1 root root 152 Feb 10 16:06 tst.c
-rw-r--r--. 1 root root 110 Feb 11 09:53 tst2.c
[root@vm1 mymod]# ./a.out
total 52
-rw-r--r--. 1 root root 213 Feb 11 10:24 Makefile
-rw-r--r--. 1 root root 214 Feb 11 10:19 Makefile_1
-rwxr-xr-x. 1 root root 17440 Feb 11 10:29 a.out
-rw-r--r--. 1 root root 221 Feb 10 15:58 test.c
-rw-r--r--. 1 root root 259 Feb 11 10:18 test2.c
-rw-r--r--. 1 root root 205 Feb 10 15:41 test3.c
-rw-r--r--. 1 root root 330 Feb 10 15:55 test4.c
-rw-r--r--. 1 root root 152 Feb 10 16:06 tst.c
-rw-r--r--. 1 root root 110 Feb 11 09:53 tst2.c
I need to make something similar as a kernel module.
This is work:
[root@vm1 mymod]# cat test2.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/unistd.h>int init_module(void)
{ printk("Hello World\n This is a test\n");return 0; }
void cleanup_module(void)
{ printk("Good Bye World"); }MODULE_LICENSE("GPL");
[root@vm1 mymod]# make clean
make -C /lib/modules/uname -r
/build M=$PWD clean
make[1]: Entering directory '/usr/src/kernels/5.14.0-503.23.1.el9_5.x86_64'
CLEAN /root/mymod/Module.symvers
make[1]: Leaving directory '/usr/src/kernels/5.14.0-503.23.1.el9_5.x86_64'
[root@vm1 mymod]# make
make -C /lib/modules/uname -r
/build M=$PWD
make[1]: Entering directory '/usr/src/kernels/5.14.0-503.23.1.el9_5.x86_64'
CC [M] /root/mymod/test2.o
MODPOST /root/mymod/Module.symvers
CC [M] /root/mymod/test2.mod.o
LD [M] /root/mymod/test2.ko
BTF [M] /root/mymod/test2.ko
Skipping BTF generation for /root/mymod/test2.ko due to unavailability of vmlinux
make[1]: Leaving directory '/usr/src/kernels/5.14.0-503.23.1.el9_5.x86_64'
But this doesn't work ! ((( :
[root@vm1 mymod]# cat test2.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/unistd.h>int init_module(void)
{ printk("Hello World\n This is a test\n");int main()
{
char *cmd = { "ls", "-l", (char *)0 };
execv ("/bin/ls", cmd);
}return 0; }
void cleanup_module(void)
{ printk("Good Bye World"); }MODULE_LICENSE("GPL");
[root@vm1 mymod]# make
make -C /lib/modules/uname -r
/build M=$PWD
make[1]: Entering directory '/usr/src/kernels/5.14.0-503.23.1.el9_5.x86_64'
CC [M] /root/mymod/test2.o
/root/mymod/test2.c: In function 'init_module':
/root/mymod/test2.c:8:5: error: function declaration isn't a prototype [-Werror=strict-prototypes]
8 | int main()
| ^~~~
/root/mymod/test2.c: In function 'main':
/root/mymod/test2.c:11:1: error: implicit declaration of function 'execv' [-Werror=implicit-function-declaration]
11 | execv ("/bin/ls", cmd);
| ^~~~~
At top level:
/root/mymod/test2.c:8:5: warning: 'main' defined but not used [-Wunused-function]
8 | int main()
| ^~~~
cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:249: /root/mymod/test2.o] Error 1
make[1]: *** [Makefile:1944: /root/mymod] Error 2
make[1]: Leaving directory '/usr/src/kernels/5.14.0-503.23.1.el9_5.x86_64'
make: *** [Makefile:6: default] Error 2
Please help, how to do it correctly?!....
Obviously, in kernel mode the exec() call is done differently...