3

I have written a kernel module which I'm loading on kernel 4.2.3 . I am trying to read a simple text file in my init_module which basically loads some configuration data by reading the contents of the text file. This same code works on previous versions of kernel but not on 4.2.3 Below is my code snippet for reference :

struct file* pFile = NULL;
pFile = filp_open(fileName, mode, 0);
if(pFile != NULL){
if(IS_ERR(pFile))
{
  printk("<1>error %p for %s**\n", pFile, fileName);
  pFile = NULL;
}
else if(pFile->f_op->read == NULL || pFile->f_op->write == NULL)
{
  filp_close(pFile, 0);
  pFile = NULL;
}

In my case I am getting pFile->f_op->read function pointer as NULL. This code works fine for non text files like /proc/kallsyms which I am able to open & read. Please provide me some pointers as to is this a 4.2.3 kernel specific issue, how can i workaround this in my kernel module code ? Any pointers would be very helpful.

Tejus Prasad
  • 6,322
  • 7
  • 47
  • 75
atish
  • 31
  • 1
  • 3
  • It's usually bad idea to mess with files in kernel. Why do you need to do that in the first place? Also, I'm encouraging you to stick to [kernel coding style](https://www.kernel.org/doc/Documentation/CodingStyle) if you are expecting your code to go to the upstream (and frankly I've just skipped reading your code as it's completely unreadable for me). – Sam Protsenko Oct 17 '15 at 11:34
  • 1
    yeah i know all that , but what is it in 4.2.3 whats causing this behaviour . Its a pretty simple piece of code and anyone who has dealt with kernel modules should be able to comprehend it easily . – atish Oct 17 '15 at 17:08

1 Answers1

2

.read is not the only interface which can implement reading from file. Files also may use .read_iter for that.

For reading a file, instead of direct call to ->read, use

ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)

which takes into account every possibility.

Similarly, for writing a file

ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)

should be used.

UPDATE: Since Linux kernel 4.14 for read/write files in the kernel space functions kernel_read and kernel_write should be used instead. See that my answer for more info.


Reading from file to kernel's buffer (before Linux kernel 4.14)

Because vfs_read expects buffer pointed to user-space memory (__user type attribute denotes that), passing in-kernel buffer will not work: it may cause compiler warning about inconsysency between expected and actual type of second parameter to vfs_read, and, more important, vfs_read will reject (by returning -EFAULT) buffer as pointed not to user space. But one can overcome this behaviour by changing user-space memory segment:

/*
 * Assume that `kernel_buf` points to kernel's memory and has type char*.
 */
char __user *user_buf = (__force char __user *)kernel_buf; // Make compiler happy.
mm_segment_t oldfs = get_fs(); // Store current use-space memory segment.
set_fs(KERNEL_DS); // Set user-space memory segment equal to kernel's one.

vfs_read(file, user_buf, count, pos);

set_fs(oldfs); // Restore user-space memory segment after reading.
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • What exactly you get with `vfs_read`? Does function call fail with error? Which error code it returns? `vfs_read` is what syscall `read` uses after it resolves file object. So, if `vfs_read` fails, reading file from user-space would likely fail too. – Tsyvarev Oct 18 '15 at 15:08
  • I m getting error code EFAULT as return value from vfs_read . – atish Oct 18 '15 at 17:51
  • EFAULT means that `buf` is not allocated by user. You probably use in-kernel buffer, which is not quite correct here. You may try to use `__vfs_read` instead `vfs_read`: it doesn't check buffer using `access_ok`. But nothing prevents `.read` interface of file to perform same check. – Tsyvarev Oct 18 '15 at 21:08
  • I am using kmalloc (size , GFP_KERNEL ) to allocate buffer where i wish to read the contents of the file . Passing this buffer as the 2nd argument in vfs_read call . – atish Oct 19 '15 at 03:52
  • Thanks a lot ... i am now able to read contents using vfs_read , I wasnt doing the get_fs() & set_fs() properly which was causing the EFAULT error . – atish Oct 19 '15 at 07:18
  • I added info about `get_fs`/`set_fs` to my post. Probably, it will helps others. – Tsyvarev Oct 19 '15 at 07:33