Linux kernel module, concept of configuration and persistent state - linux

I work on Linux kernel loadable module and I look for proper way to configure it and way to load/store binary data when module is loaded/unloaded. The module needs to read configuration data in time of loading, but it may change data and save them in run-time. I read on many places that reading/writing to file is not recommended and I also read that sysfs can be used for such purpose. Can be binary configuration data stored in sysfs ? Or exist more suitable solution ? Can you provide link to some example or doc where I can found some details about how to load/save persistent configuration by module ?
Peter

Maybe you can use the firmware interface in the module.
It should be able to load a binary file which include your settings with request_firmware(..)
I think it isn't required to write this binary file to the hardware...?

There are probably a lot of ways to do this. The way that leaps to my mind is with a character device and a script running in userspace. You could arrange that the script loads your module and then writes the binary data to the character device file created by the module. The script could proceed to loop on a read of the device file. When the module wants to export new state, it unblocks the read. The script then copies out the data, and repeats.

Related

Linux kernel module configuration

For my university project I'm doing a module which will allow or disallow a process to perform a system calls (e. g. A little loadable selinux). For now I nave code that controls syscalls. For each process I store a link to the structure which contains permissions config. However, now I've just hardcoded two configs: one is default (allow all) and another one is to allow everything except opening '/testfile'.
My question is how to load configs dynamically?
I have a parser for config files but I've read that accessing files from the kernel is bad idea.
How should I store configs and how should I load them?
I've read that reading files from the kernel is bad idea
Description of filp_open function in the kernel sources says:
This is the helper to open a file from kernelspace if you really have to. But in generally you should not do this, so please move along, nothing to see here..
So, if you need to load/store content of the file into/from the kernel module, then do that. But use appropriate functions, as described in that question.

Resolving file descriptor to file name / file path

I am currently developing a simple kernel module that can steal system calls such as open, read, write and replace them with a simple function which logs the files being opened, read, written, into a file and return the original system calls.
My query is, I am able to get the File Descriptor in read and write system calls, but I am not able to understand how to obtain file name using the same.
Currently I am able to access the file structure associated with given FD using following code:
struct file *file;
file = fcheck(fd);
This file structure has two important entities in it, which are of my concern I believe:
f_path
f_inode
Can anybody help me get dentry or inode or the path name associated with this fd using the file structure associated with it?
Is my approach correct? Or do I need to do something different?
I am using Ubuntu 14.04 and my kernel version is 3.19.0-25-generic, for the kernel module development.
.f_inode is actually an inode.
.f_path->dentry is a dentry.
Traversing this dentry via ->d_parent link, until f_path.mnt.mnt_root dentry will be touched, and collecting dentry->d_name components, will construct the file's path, relative to the mount point. This is done, e.g., with d_path, but in more carefull way.
Instead of fcheck(fd), which should be used inside RCU read section, you can also use fget(fd), which should be paired with fput().
The approach is completely incorrect - see http://www.watson.org/~robert/2007woot/
Linux already has a reliable mechanism for doing this thing (audit). If you want to implement it anyway (for fun I presume), you want to place your hooks roughly where audit is doing that. Chances are LSM hooks are in appropriate places, have not checked.

How to add files in /proc without using Loadable kernel module (LKM)

I am trying to make some changes in the kernel of Linux and then want to print some debugging statements and also want to count some parameters in compile time of the kernel.
I want to store the content in /proc file how can I achieve this task and store the info in /proc file without using LKM.
I would recommend you to read this article which helps you in creating, removing entries in /proc filesystem
http://www.thegeekstuff.com/2012/04/create-proc-files/
Using LKM approach is the best way to create proc entries and store the requisite info as "proc" itself is a filesystem provided by linux kernel for debugging purposes from user space.So, writing a module of your own using the proc related system calls would be easier and better for your purpose.Moreover, rather than building it as static module, you can compile it as a dynamic module to load and unload it at run-time.
If you don't want to use LKM, all the basic operations like proc_registration, creating entries,implementing read/write has to be done separately from scratch like writing your own system calls, registering it in kernel space and using them in your codes.

reading and writing from a file in linux kernel

I'm writing a patch for VFS FAT implmentation on kernel 3.0
I want to add posix attributes to FAT files that are created in linux.
to achive that, I must save a file that contains all the relevant information on the mounted drive.
I know that reading and writing files from kernel space is something normally shouldn't be done, and I'm looking for another way to read/write the data.
I saw articles on the net that suggested to use /proc or to create a userspace daemon that will do the IO for me. I wanted to know if anyone saw or know where can I look at an implmentation of a thing like that,because I didn't find any examples for that over the net.
I'm not looking for a read/write to proc example, I want to see an entire solution for this issue.
Have a look at the quota implementation; this is a mechanism (ok, presumably not available on vfat) which reads/writes files from the kernel.
Additionally, the "loop" block device is another example of a kernel facility which does file IO.

How to read/write from/to a linux /proc file from kernel space?

I am writing a program consisting of user program and a kernel module. The kernel module needs to gather data that it will then "send" to the user program. This has to be done via a /proc file. Now, I create the file, everything is fine, and spent ages reading the internet for answer and still cannot find one. How do you read/write a /proc file from the kernel space ? The write_proc and read_proc supplied to the procfile are used to read and write data from USER space, whereas I need the module to be able to write the /proc file itself.
That's not how it works. When a userspace program opens the files, they are generated on the fly on a case-by-case basis. Most of them are readonly and generated by a common mechanism:
Register an entry with create_proc_read_entry
Supply a callback function (called read_proc by convention) which is called when the file is read
This callback function should populate a supplied buffer and (typically) call proc_calc_metrics to update the file pointer etc supplied to userspace.
You (from the kernel) do not "write" to procfs files, you supply the results dynamically when userspace requests them.
One of the approaches to get data across to the user space would be seq_files. In order to configure (write) kernel parameters you may want to consider sys-fs nodes.
Thanks,
Vijay

Resources