Find the answer to your Linux question:
Results 1 to 2 of 2
I need to read contents of the file into the kernel's memory(without any userspace process). I've found nice solution, but it makes my kernel "Oops". Any ideas of how to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! Alex_Justes's Avatar
    Join Date
    Apr 2010
    Posts
    11

    [SOLVED] Read file from the linux kernel


    I need to read contents of the file into the kernel's memory(without any userspace process). I've found nice solution, but it makes my kernel "Oops".

    Any ideas of how to fix it?

    Kernel Version - 2.6.31

    Code:
    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/fs.h>
    #include <asm/uaccess.h>
    
    MODULE_LICENSE("GPL");
    
    struct file *f;
    mm_segment_t fs;
    char str[9];
    
    int init_module(void){
    	f = filp_open("/home/justes/test",O_RDONLY,0644);
    	fs = get_fs();
    	set_fs(get_ds());
    
    	vfs_read(f, str,8,0);
    
    	set_fs(fs);
    	filp_close(f,NULL);
    
    	printk(KERN_ALERT "Hello: %s\n",str);
    
    	return 0;
    }
    
    void cleanup_module(void){
    	printk(KERN_ALERT "Goodbye\n");
    }
    Code:
     [  102.377364] BUG: unable to handle kernel NULL pointer dereference at (null)
    [  102.377378] IP: [<c01e7bd0>] rw_verify_area+0x20/0xe0
    [  102.377410] *pde = 00000000 
    [  102.377416] Oops: 0000 [#1] SMP 
    [  102.377422] last sysfs file: /sys/devices/LNXSYSTM:00/device:00/PNP0A03:00/PNP0C0A:00/power_supply/BAT0/energy_full
    [  102.377433] Modules linked in: file_read(+) vboxvideo drm agpgart binfmt_misc vboxvfs snd_intel8x0 snd_ac97_codec ac97_bus snd_pcm_oss snd_mixer_oss snd_pcm snd_seq_dummy snd_seq_oss snd_seq_midi snd_rawmidi iptable_filter snd_seq_midi_event ip_tables x_tables snd_seq snd_timer snd_seq_device ppdev snd soundcore psmouse serio_raw i2c_piix4 snd_page_alloc vboxguest parport_pc lp parport pcnet32 mii floppy
    [  102.377465] 
    [  102.377480] Pid: 1889, comm: insmod Not tainted (2.6.31-20-generic #58-Ubuntu) VirtualBox
    [  102.377486] EIP: 0060:[<c01e7bd0>] EFLAGS: 00010202 CPU: 0
    [  102.377492] EIP is at rw_verify_area+0x20/0xe0
    [  102.377498] EAX: d717e198 EBX: c3110400 ECX: 00000000 EDX: 00000008
    [  102.377503] ESI: 00000000 EDI: d71d5a6c EBP: d77ebf24 ESP: d77ebf04
    [  102.377509]  DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068
    [  102.377515] Process insmod (pid: 1889, ti=d77ea000 task=d7818000 task.ti=d77ea000)
    [  102.377519] Stack:
    [  102.377524]  d77ebf78 00000000 d77ebf78 d77ebf20 00000001 c3110400 00000000 d88c54e4
    [  102.377537] <0> d77ebf4c c01e8141 00000008 fffffffc d88c5380 00000000 d77ebf4c fffffffc
    [  102.377546] <0> d88c5380 00000000 d77ebf5c d88c506a 00000000 c016106f d77ebf88 c010112c
    [  102.377560] Call Trace:
    [  102.377568]  [<c01e8141>] ? vfs_read+0x71/0x190
    [  102.377576]  [<d88c506a>] ? init_module+0x4a/0x80 [file_read]
    [  102.377583]  [<c016106f>] ? __blocking_notifier_call_chain+0x4f/0x60
    [  102.377591]  [<c010112c>] ? do_one_initcall+0x2c/0x190
    [  102.377598]  [<d88c5020>] ? init_module+0x0/0x80 [file_read]
    [  102.377606]  [<c0173781>] ? sys_init_module+0xb1/0x1f0
    [  102.377613]  [<c01033ac>] ? syscall_call+0x7/0xb
    [  102.377618] Code: 90 90 90 90 90 90 90 90 90 90 90 55 89 e5 83 ec 20 89 5d f4 89 d3 89 75 f8 89 c6 89 7d fc 8b 42 0c 8b 55 08 8b 78 10 85 d2 78 67 <8b> 11 8b 49 04 85 c9 78 5e 8b 45 08 c7 45 f0 00 00 00 00 89 45 
    [  102.377655] EIP: [<c01e7bd0>] rw_verify_area+0x20/0xe0 SS:ESP 0068:d77ebf04
    [  102.377663] CR2: 0000000000000000
    [  102.377670] ---[ end trace b319b9095262621d ]---
    P.S.
    Trying f->f_op->read brings similar error.

  2. #2
    Just Joined! Alex_Justes's Avatar
    Join Date
    Apr 2010
    Posts
    11

    Arrow Solution

    I've solved it...

    it was my fault. here is the solution:

    loff_t offset;
    offset = 0;

    .....

    vfs_read(f,str,..,&offset); // it needs reference to loff_t, not simple int or smth like it

    .....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •