[oclug] Reading a file from the kernel.

ELSID Software Systems LTD. elsid at crbn.com
Sat Mar 16 11:04:48 EST 2002


Greetings Linux Experts:

I am trying to read a file from a the kernel.  From looking at the file
system codeI created this function.  It opens the file and then it
causes a seg violation.

Can anyone tell me what is wrong or does any one have a routine that
will read a file from kernel space or can amyone point me at a working
example.

All help will be appreciated, financially if required.

Regards,
Robert
elsid at crbn.com

<< this is the kernel mod >>

void doTest()
{
  int fd;
  int res;
  char buffer[ 256 ];
  loff_t l = (loff_t)0;
  struct file *f;
  ssize_t (*read)(struct file *, char *, size_t, loff_t * );

  printk("fsm_open:in block - mod 21\n");

  sprintf( buffer,  "/proc/%d/cmdline", current->pid );
  printk( "fsm_open:file to open<%s>\n", &buffer[0] );

  fd = get_unused_fd();
  if( fd >= 0 )
  {
    f = filp_open( buffer, O_RDONLY, 0 );
    if( IS_ERR(f) )
    {
      put_unused_fd( fd );
      printk("fms_open:unable to open <%s>\n", &buffer[0]);
    }
    else
    {
      printk("fms_open:file was opened<%s> fd<%d>\n", &buffer[0], fd);
      fd_install( fd, f );
      fput(f);

      printk("fms_open:fd was installed f<%x> fd<%d>\n", f, fd );

      f = fget(fd);

      printk("fms_open:fd was fget(fd), fd<%d>, f<%x>\n", fd, f);
      if( f == NULL )
        printk("f came back NULL\n");
      else
      {
        read = f->f_op->read;
        if( read == NULL )
          printk("read came back as NULL \n");
        else
        {
          printk("fms_open:read<%x> f<%x>\n", read, f );
          res = read( f, &buffer[0], 256, &l /* &f->f_pos */ );  << seg
violation happens here
          printk("fsm_open:res=%d, command line<%s>\n", res, &buffer[0]
);
          sys_close( fd );
        }
        fput(f);
      }
    }
  }
  else
  {
    printk("fsm_open:fd=<%d>\n", fd );
  }
}

<< end prog >>

<< this is the dump from 'dmesg' >>

fsm_open:in block - mod 21
fsm_open:file to open</proc/614/cmdline>
fms_open:file was opened</proc/614/cmdline> fd<5>
fms_open:fd was installed f<c05aca20> fd<5>
fms_open:fd was fget(fd), fd<5>, f<c05aca20>
fms_open:read<c0149808> f<c05aca20>
Unable to handle kernel NULL pointer dereference at virtual address
00000008
current->tss.cr3 = 006df000, %cr3 = 006df000
*pde = 00000000
Oops: 0000
CPU:    0
EIP:    0010:[<c014981a>]
EFLAGS: 00010282
eax: 00000000   ebx: c05aca20   ecx: 00000005   edx: c1902000
esi: c0149808   edi: 00000005   ebp: 00000100   esp: c0701e08
ds: 0018   es: 0018   ss: 0018
Process vi (pid: 614, process nr: 44, stackpage=c0701000)
Stack: 00000005 c0701e64 00000001 c01145bd c05aca20 c0149808 00000005
c0701e64
       00000024 c0127870 c05aca20 c0701e64 00000100 c0701e5c c01e4b74
c0149808
       c05aca20 c05ac9c0 000003fc c011e884 000003fc 00000000 00000000
6f72702f
Call Trace: [<c01145bd>] [<c0149808>] [<c0127870>] [<c01e4b74>]
[<c0149808>] [<c011e884>] [<c010ae9b>]
       [<c017f9f4>] [<c0111798>] [<c0135832>] [<c011e824>] [<c011e900>]
[<c01285fa>] [<c0109e18>]
Code: 8b 78 08 81 fd 00 0c 00 00 76 05 bd 00 0c 00 00 31 d2 b8 15
Unable to handle kernel NULL pointer dereference at virtual address
00000008
current->tss.cr3 = 00101000, %cr3 = 00101000
*pde = 00000000
Oops: 0000
CPU:    0
EIP:    0010:[<c0126178>]
EFLAGS: 00010246
eax: 00000000   ebx: c05aca20   ecx: c111f4b0   edx: c05aca20
esi: 00000000   edi: 00000000   ebp: 00000001   esp: c0701d54
ds: 0018   es: 0018   ss: 0018
Process vi (pid: 614, process nr: 44, stackpage=c0701000)
Stack: c05c6240 c0118031 c05aca20 c05c6240 c0701dcc 00000008 c0700000
00000100
       00000005 c0700000 c010a28d 0000000b c0701dcc c01e0c18 c01e216e
00000000
       00000000 c010f503 c01e216e c0701dcc 00000000 c0700000 c0149808
00000005
Call Trace: [<c0118031>] [<c010a28d>] [<c01e0c18>] [<c01e216e>]
[<c010f503>] [<c01e216e>] [<c0149808>]
       [<c0109f1d>] [<c0149808>] [<c014981a>] [<c0149808>] [<c01145bd>]
[<c0149808>] [<c0127870>] [<c01e4b74>]
       [<c0149808>] [<c011e884>] [<c010ae9b>] [<c017f9f4>] [<c0111798>]
[<c0135832>] [<c011e824>] [<c011e900>]
       [<c01285fa>] [<c0109e18>]
Code: 83 7f 08 00 74 0d ff 74 24 14 53 e8 f0 cd 00 00 83 c4 08

<< end trace >>


-






More information about the OCLUG mailing list