Linux Kernel - Add System Call Dynamically Through Module

How to use the function from a custom kernel module?


Use sysfs

Checkout the list of various Linux kernel <--> Userspace interfaces.

To allow userspace to interact with a loadable kernel module, consider using sysfs.

To add support for sysfs within your loadable module, checkout the basics of a sys-fs entry.

A good guide with the best practices of creating sysfs entries should get you started the right way.

The userspace test will then change from

int retval = getpeuid(pid, uid);

to something that uses open, write() and read()

to interact with the sysfs entry just like a regular file.

( Why file? because everything is a file on UNIX. )

You could further simplify this to using a shell-script that uses echo/cat commands to pass/gather data from the loadable kernel module via the sysfs entry.



Alternate option : A beautiful/ugly Hack

Disclaimer: I agree that trying to use syscalls within a loadable kernel module is neither a proper solution, nor guaranteed to always work. I know what i am doing.

(Hover the mouse over the following block, ONLY if you agree to the above)

Checkout this answer and related code that describes a potential "hack" to allow implementing custom syscalls in loadable modules in any unused locations within the current syscall table of the kernel.




Also carefully go through the several answers/comments to this question. They deal with overcoming the problem of not being able to modify the syscall table. One of the comments also emphasises the fact that hypervisors implementing their own extensions are not likely to be affected by this "exploit" as they offer better protection of the syscall table.




Note that such non-standard interfaces may not always work and even if they do, they can stop working anytime. Stick to standard interfaces for reliability.

Dynamically creating variable in Kernel Module

If your variables are actually one per cpu, you might want to use the per_cpu macros. The gist is, you declare such a variable with:

DEFINE_PER_CPU(struct task_struct, my_tasks);

and then access the variable using

get_cpu_var(my_tasks).foo = bar;

You can get more info at See http://www.makelinux.net/ldd3/chp-8-sect-5 (or percpu.h) for more details.



Related Topics



Leave a reply



Submit