TheFrozenDuck
TheFrozenDuck

Reputation: 11

Linux Kernel - Read/Write to a File

I'm working on a LKM which needs to retrieve and write a certain set of information to files. I looked up common ways to do so, but could not find a working one for Linux 4.x. I also found out that it is possible to retrieve system calls from memory and effectively call them.

As I found currently no better way I'd be interested if it'd be feasible to find the system call table, and call open, read/write and close this way.

Upvotes: 0

Views: 2777

Answers (1)

sinback
sinback

Reputation: 1004

This is strongly discouraged in most situations.

https://www.linuxjournal.com/article/8110 was a really good read for me the first time I thought I had to do this as well.

From within the Linux kernel, however, reading data out of a file for configuration information is considered to be forbidden. This is due to a vast array of different problems that could result if a developer tries to do this.

Indeed this is possible to do using system calls from within the kernel, but the practice of calling system calls from within the kernel is also generally discouraged. They're designed as interfaces for userspace applications to ask things of the kernel, not for the kernel to get itself to do work.

What kind of files do you want to manipulate from within the kernel? If the kind of file you'd like to manipulate is provided by the proc filesystem or the sysfs filesystem or the dev filesystem, you can modify the contents of the file from within the kernel (since the kernel provides these to userspace itself) -- this should be done NOT with file manipulation calls. If it's a normal userspace file, almost never do you want the kernel to be able to modify it.

If you provide more specifics I'd be interested to hear them, but this is usually a bad idea.

Upvotes: 3

Related Questions