24

I developed a kernel module and some functions on it. Now i need to develop a program in the user space and call some functions which are in the kernel module.

I also need to access some global variable that are in the kernel module on my program at the user space.

Fopa Léon Constantin
  • 11,863
  • 8
  • 48
  • 82
Ricardo
  • 612
  • 2
  • 9
  • 16
  • You can just call any function from other space. You can call syscall function, or interact via procfs or interact with some device file, which is handled by your module. Can you say what the module is? – osgx May 30 '12 at 13:01
  • The module have some network management functions. If i want to send some Ethernet data, i need to request to someone if it's is possible, using a function developed at the kernel module. So, i need to acess that function for instance, before i send a packet. That function also has some paramenters that i need insert. – Ricardo May 30 '12 at 13:18

4 Answers4

25

There is complete overview of linux-kernel module and user-space program interacting http://wiki.tldp.org/kernel_user_space_howto "Kernel Space, User Space Interfaces" by Ariane Keller (it is from 2008-09-28, but about 2.6 kernels; only major new way is relayfs)

No ordinary function call from user space to kernel space is listed, only syscall (adding new syscall is not easy) and upcall (call in inverse direction).

One of easiest interface is ioctl; but you can't start to use ioctl before creating procfs, sysfs or similiar file.

Other is sysctl; but sysctl is more eligible to reading/writing to global variable. (It is hard to pass several parameters via sysctl interface).

osgx
  • 90,338
  • 53
  • 357
  • 513
  • 2
    I'm afraid that link died, though it's still accessible [through web.archive.org](https://web.archive.org/web/20160214015410/http://people.ee.ethz.ch/~arkeller/linux/kernel_user_space_howto.html) – Cimbali May 16 '16 at 13:48
  • 1
    Cimbali, thank you. Also here - http://wiki.tldp.org/kernel_user_space_howto, the "Kernel Space, User Space Interfaces" by Ariane Keller, 2008-10-04 – osgx May 16 '16 at 15:45
6

You seem to be missing the point of kernel and userland separation. If your user program could modify data inside the kernel directly, that would quickly lead to disaster.

There's only one conventional way for a user program to explicitly request services from the kernel - make a system call.

There are also traps and some Linux-specific userland-kernel communication mechanisms, but those are not relevant here.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • 1
    But http://stackoverflow.com/questions/2394985/linux-kernel-add-system-call-dynamically-through-module http://stackoverflow.com/questions/463332/is-it-possible-to-add-a-system-call-via-a-lkm - module can't add a system call – osgx May 30 '12 at 13:19
  • 5
    But it can implement an existing one, like `ioctl(2)`. – Nikolai Fetissov May 30 '12 at 13:20
  • Well that's not strictly true. There are ways of of modifying the system call table (at least on x86 architectures). It involves changing the pointer from the IDTR register to point to a new section in memory where the new hijacked sys_call table (of arbitrary length) exists. see http://www.defcon.org/images/defcon-13/dc13-presentations/dc-13_grizzard.pdf for a general idea of the attack. – ajpyles May 31 '12 at 20:51
  • Yes agreed, not for the faint of heart :) – ajpyles May 31 '12 at 21:26
5

As other posters have mentioned, there is a clear distinction between kernel and user space. So no you can't call a kernel function directly from user space. I think the easiest way to send messages between userspace and kernel space is via netlink sockets. A netlink socket allows you to easily pass arbitrary data structures between user level and kernel level.

Yes ioctl, system calls are viable alternatives, they are not as flexible as the netlink socket for passing arbitrary information.

Noam
  • 3
  • 3
ajpyles
  • 628
  • 3
  • 12
  • Just a note: The code needs the following minor change to make it run on the newer kernels: change the following line in gnKernel.c: rc = genlmsg_unicast(skb,info->snd_pid ); to rc = genlmsg_unicast(genl_info_net(info), skb,info->snd_pid ); The kernel I was testing this on was 2.6.35-22-generic – lithiumhead Feb 20 '14 at 08:49
1

You'll need to install a new kernel to make use of the new call unless you already have some mechanism to update the kernel ... http://www.cyberciti.biz/tips/how-to-patch-running-linux-kernel.html

Jay
  • 3,276
  • 1
  • 28
  • 38