1

This is my code to write to a file from kernel.(i know it is not a good idea to read and write directly from kernel but this is just a test).

static void write_startup_file(char *startstring)
{
  mm_segment_t oldfs;
  struct file *f;
  loff_t pos=0;

  f = filp_open("/bin/startupcheck",O_WRONLY|O_CREAT, 0644);
  oldfs = get_fs();
  set_fs (KERNEL_DS);
  if(f)
  {
   vfs_write(f,startstring, strlen(startstring), &pos);
   set_fs(oldfs);
   filp_close(f, NULL);
  }
  else
  printk(KERN_CRIT "Unable to open startfile...\n");
 }

And i call it in another function ourVeryOwnFunction() as

if(dbval==NULL) { write_startup_file("FAILED");}

Where dbval is a value being read.But when i compile the kernel and run it( Im running it on vmware). The kernel panic i get is here

Clearly im doing something wrong. Looking for some help. Thanks.

linuxnoob
  • 43
  • 1
  • 7

2 Answers2

3

First of all, initialize your struct file * pointer. Don't rely on simple check of returned pointer against NULL. Use IS_ERR macro instead. Honestly, in general, it's hard to say what else might be wrong. Read something like this http://ytliu.info/notes/linux/file_ops_in_kernel.html or refer to these questions on SO

How to read/write files within a Linux kernel module?

File I/O in a Linux kernel module

Maybe it gives you some insight or sheds some light on things. And, of course, don't write to fs from kernel :)

HTH.

Community
  • 1
  • 1
rfx
  • 394
  • 1
  • 6
  • 16
  • Ive made all those changes i.e. the `set_fs()` before `filp_open` , initialized the `struct file*` and on file open i check using the `IS_ERR` macro but i still get the kernel panic at `vfs_read`. – linuxnoob Mar 25 '17 at 11:29
  • Once again, it's difficult to say what causes panic in your case. It is always difficult without access to complete source code and additional knowledge about specific conditions. This is especially true for kernels. Here is small and rather shoddy testing kernel-mode writer http://pastebin.com/kPPLqdcd You can use these small convenience `file_xxx` APIs. After inserting, the module creates file `/root/test.txt` and writes to it from kernel, on unloading it reads 6 bytes from the file and closes it. Use dmesg to observe the writer. Tested on Linux 4.10.5 x86-64, built with gcc 6.3. HTH. – rfx Mar 25 '17 at 14:37
  • And sorry about unintentional desinformation regarding `filp_open()` and `get | set_fs()`. My bad. Theres nothing wrong with calling order. But still it is important with routines like `vfs_write()` and `vfs_read()`. – rfx Mar 25 '17 at 14:42
  • Thankyou! ill try out the `file_xxx` API's and let you know. – linuxnoob Mar 25 '17 at 17:01
  • I ran that kernel-mode writer as a module and it runs perfectly but crashes when i try to do it at boot. Im guessing it is trying to dereference a null pointer or something. – linuxnoob Mar 26 '17 at 12:38
  • It might be because you call VFS code too early. Indeed, there should be no problem on booted and normally running system. Boot is somewhat different. This is probably not a VFS problem at all. You have to take care not to call VFS code until FS is mounted, VFS layer initialized and other conditions met. – rfx Mar 26 '17 at 17:38
  • yep i thought so too that VFS is being called before FS has mounted. Thanks for all your help! – linuxnoob Mar 27 '17 at 15:59
1

Maybe startstring can't from user space, but as a module, module driver can put it into the kernel, not in user space. I think you can try a simple string in the stack and remove the paramter startstring ,which can verifies whether my guess is right.

Eric Liu
  • 11
  • 2