Page 1 of 1

Wie schreibe ich Linux -Sicherheitsmodul zum Verweigern des Löschens (Unglied) für Kernel 6.x?

Posted: 17 Mar 2025, 03:02
by Anonymous
Ich möchte lernen, wie man Linux-Sicherheitsmodul schreibt, um das Löschen der Datei zu leugnen. mit dem Verenden in Verbindung zu haben. Also habe ich einen von ihnen ausgewählt und versucht, das anzuschließen, um zu sehen, wie und ob es funktioniert.

Code: Select all

#include 
#include 
#include 
#include 

static int hello_lsm_inode_unlink(struct inode *dir, struct dentry *dentry) {
pr_info("LSM Hello World: Preventing unlink for file: %s\n", dentry->d_name.name);
return -EPERM;  // Prevent the file from being deleted
}

static struct security_hook_list hello_hooks[] = {
LSM_HOOK_INIT(inode_unlink, hello_lsm_inode_unlink),
};

static int __init hello_lsm_init(void) {
pr_info("LSM Hello World: Initializing\n");
security_add_hooks(hello_hooks, ARRAY_SIZE(hello_hooks), "lsm_hello");
return 0;
}

static void __exit hello_lsm_exit(void) {
pr_info("LSM Hello World: Exiting\n");
}

module_init(hello_lsm_init);
module_exit(hello_lsm_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("You");
MODULE_DESCRIPTION("Minimal LSM with inode_unlink hook");
< /code>
und Makefile: < /p>
EXTRA_CFLAGS += -DCONFIG_SECURITY_WRITABLE_HOOKS

obj-m := lsm_hello.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
< /code>
Aber wenn ich es kompile, erhalte ich diese Fehler und die .KO-Datei wird nicht generiert: < /p>
make[1]: Entering directory '/usr/src/linux-headers-6.1.0-31-amd64'
CC [M]  /root/lsm_hello/lsm_hello.o
MODPOST /root/lsm_hello/Module.symvers
ERROR: modpost: "security_add_hooks" [/root/lsm_hello/lsm_hello.ko] undefined!
ERROR: modpost: "security_hook_heads" [/root/lsm_hello/lsm_hello.ko] undefined!
make[2]: *** [/usr/src/linux-headers-6.1.0-31-common/scripts/Makefile.modpost:127: /root/lsm_hello/Module.symvers] Error 1
make[1]: *** [/usr/src/linux-headers-6.1.0-31-common/Makefile:1986: modpost] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-6.1.0-31-amd64'
make: *** [Makefile:6: all] Error 2
< /code>
Mit einem Versuch und Irrtum fand ich, dass das Problem aus dieser Zeile stammt: < /p>
security_add_hooks(hello_hooks, ARRAY_SIZE(hello_hooks), "lsm_hello");
< /code>
Wenn ich das kommentiere, dann kompiliert es gut und ich kann überprüfen, ob es funktioniert: < /p>
root@debian:~/lsm_hello# insmod lsm_hello.ko
root@debian:~/lsm_hello# rmmod lsm_hello
root@debian:~/lsm_hello# journalctl --since "1 hour ago" | grep kernel.
Mar 16 15:21:33 debian kernel: LSM Hello World: Initializing
Mar 16 15:21:46 debian kernel: LSM Hello World: Exiting
Wie schreibt dies die richtige Art, dies zu schreiben, da es scheint, dass sich der richtige Weg von der Version zu Version unterscheidet?