• Uncategorized

About random : IOCTL-from-kernel-space

Question Detail

Roughly speaking, I am trying to issue an IOCTL call from kernel space without going to user space. (All the answers I found in SO propose going through user space).

Specifically, I try to fill the entropy pool (/dev/random) from kernel space (using a kernel module) [I know the dangers of doing this ;)]. Filling up the entropy pool from user space is done using IOCTL, e.g., rngaddentropy. Is there a way to do the same thing from kernel space?

Question Answer

You can use ioctl from the kernel space too.

Because ioctl command RNDADDENTROPY is file-specific, its processing should be implemented in the .unlocked_ioctl operation for /dev/random file (and it is actually implemented this way, see function random_ioctl).

For file-specific ioctl commands you may call .unlocked_ioctl file’s operation directly:

// Open file
struct file* f = filp_open(“/dev/random”, O_WRONLY, 0);
// Replace user space with kernel space
mm_segment_t old_fs = get_fs();

f->f_op->unlocked_ioctl(f, RNDADDENTROPY, entropy);

// Restore space

// Close file
filp_close(f, 0);

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.