Kernel module - How to test if file doesn't exist


 
Thread Tools Search this Thread
Top Forums Programming Kernel module - How to test if file doesn't exist
# 1  
Old 03-03-2011
Kernel module - How to test if file doesn't exist

Hey,
I'm currently getting into some kernel module progamming. As a little exercise I want to read the headers out of an ELF file. My code is very simple, here is the important part:
Code:
struct file *fp;
/* ... */
fp = filp_open("some/file/on/my/pc", O_RDONLY, 0);
if(fp == NULL) {
     ALERT("NULLPOINTER"); // predefinied makro for output
     return;
}
if(fp->f_dentry == NULL) {
     ALERT("NULLPOINTER"); 
     return;
}
printk(KERN_INFO "Filname: %\n", fp->f_dentry->d_name.name);

(I know that might not make much sense, it's just to play around and see how the functions and data structures work).

As long as I choose a valid path and filename in the filp_open everything works fine, just as it should.
But if I enter a non-existent filename it always gets killed. The log says:
BUG: Unable to handle kernel NULL pointer dereference at 0000000a
Interestingly, the module always seems to get killed at the 2nd if-clause. That means that even if the file doesn't exist fd itself is not NULL, but then, once I simply check if f_dentry is a NULLpointer, the module crashes.
Can anyone tell me why and how can I write this short code in such a way that it can handle non-existent files (without creating them, but rather exiting with a simple error msg)
# 2  
Old 03-03-2011
I wonder if NULL is not zero for some odd reason here. Try (void *)0L instead of NULL.
# 3  
Old 03-03-2011
Nope. Smilie
I tried
if(fp->f_dentry == NULL)

if(!fp->f_dentry)

and now
if(fp->f_dentry == (void *) 0L)

Always the same crash:
Code:
[  967.249027] BUG: unable to handle kernel NULL pointer dereference at 0000000a
[  967.249032] IP: [<f086c147>] openFile+0x57/0x1a8 [elfreader]
[  967.249044] *pde = 205ca067 *pte = 00000000 
[  967.249048] Oops: 0000 [#1] SMP


But thanks for your input nonetheless
# 4  
Old 03-03-2011
Can we see the full source code? There is nothing wrong with the snippet that you have shown us as far as I can tell. Your use of NULL is fine.
# 5  
Old 03-03-2011
Code:
[  967.249032] IP: [<f086c147>] openFile+0x57/0x1a8 [elfreader]

Hmmmm! It looks like it's openFile that's crashing -- or at least something filp_open calls.. It's never getting to the bits of code where you check pointers.
# 6  
Old 03-03-2011
openFile is just the function all the stuff happens in...
Here is the full code:
Code:
void openFile(void) {
    char *buf;
    int size;
    struct elfhdr elfH;
    Elf32_Shdr *elfSec;
    struct file *fp;
    fp = filp_open("/home/chris/Test/test", O_RDONLY, 0);
        
    if(fp == (void *)0L) {
        PRINT("FILE NICHT DA");
        return;
    }

    if(fp->f_dentry == (void *)0L) {
        PRINT("F_DENTRY NICHT DA");
        return;
    }

    if(!fp->f_dentry->d_name.name) {
        PRINT("F_DENTRY NICHT DA");
        return;
    }
    
    printk(KERN_INFO "FILENAME: %s\n", fp->f_dentry->d_name.name);
    
    if(!(buf = kmalloc(BINPRM_BUF_SIZE, GFP_KERNEL))) {
        PRINT("KMALLOC");
        return;
    }

    kernel_read(fp, 0, buf, BINPRM_BUF_SIZE);
    elfH = *((struct elfhdr *) buf);

    size = elfH.e_shnum * sizeof(Elf32_Shdr);
    if(!(elfSec = (Elf32_Shdr *) kmalloc(size, GFP_KERNEL))) {
        PRINT("KMALLOC2");
        kfree(buf);
        return;
    }

    readElf(&elfH, elfSec); // another function, works fine when we get here
    kfree(elfSec);
}

I copied the read_kernel from another program, not really sure yet what it does Smilie But as I told you: The crash happens in the part mentioned above
# 7  
Old 03-03-2011
Great. Now post readElf and kernel_read. Or better yet, post your entire file already. And a complete list of messages from when you load the module until it crashes.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Group Doesn't Exist

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I'm able to create a group but when I'm trying to delete the group it keeps stating Group Doesn't Exist. I know... (2 Replies)
Discussion started by: GoBoyGo
2 Replies

2. Linux

Unload kernel module at boot time (Debian Wheezy 7.2, 3.2.0-4-686-pae kernel)

Hi everyone, I am trying to prevent the ehci_hcd kernel module to load at boot time. Here's what I've tried so far: 1) Add the following line to /etc/modprobe.d/blacklist.conf (as suggested here): 2) Blacklisted the module by adding the following string to 3) Tried to blacklist the module... (0 Replies)
Discussion started by: gacanepa
0 Replies

3. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

4. Programming

Kernel module - Check whether file (/dev node) exists

Hi, I'm pretty new to kernel coding and I'm working on a device driver that works with an existing framework. Basically my module will be loaded/unloaded multiple times and I'd like to create a register a class, driver, and create a /dev node on the first load only. The existing framework... (0 Replies)
Discussion started by: ThomasBrez
0 Replies

5. Solaris

User directory doesn't exist

Hii all, i create the user useradd -d /home/kk kk passwd kk when i tried to login to kk i get a error user directory doesn't exist then i tried useradd kkk passwd kkkwhen i tried to login to kkk i get the same error user directory doesn't exist. (4 Replies)
Discussion started by: vipinkumarr89
4 Replies

6. Shell Programming and Scripting

ln -s creates symlink in symlink, if [ -f ... ] says file that exists doesn't exist

Hi Forums, I got a little problem, I made a few modifications to the code of the launch script of a testing server(minecraft) and now updating is broken aswell as the automatic directory creation. These Lines somehow create an endless symlink that refers to itself and I don't know how to fix... (0 Replies)
Discussion started by: Xaymar
0 Replies

7. Shell Programming and Scripting

File exist test

Can someone please shed light on why this may not be working, file does exist, but I get an error if ] then echo "No ${source_path}/${file_mask} found - ">> ${logfile} result=1 check_result ${result} "Failed to find file... (4 Replies)
Discussion started by: Pokermad
4 Replies

8. Shell Programming and Scripting

sftp mget where file doesn't exist BASH

I have a script that is working: #!/bin/bash sftp user@domain.com <<EOF cd somedir mget *.csv quit EOF but on a crontab I want to only pull newer files, so I want to do something like: while read ls current dir local file != true do mget that new file but I'm not sure the syntax... (2 Replies)
Discussion started by: unclecameron
2 Replies

9. Linux

How to convert Linux Kernel built-in module into a loadable module

Hi all, I am working on USB data monitoring on Fedora Core 9. Kernel 2.6.25 has a built-in module (the one that isn't loadable, but compiles and links statically with the kernel during compilation) to snoop USB data. It is in <kernel_source_code>/drivers/usb/mon/. I need to know if I can... (0 Replies)
Discussion started by: anitemp
0 Replies

10. SuSE

max number of slabs per kernel module (kernel 2.6.17, suse)

Hi All, Is there a max number of slabs that can be used per kernel module? I'm having a tough time finding out that kind of information, but the array 'node_zonelists' (mmzone.h) has a size of 5. I just want to avoid buffer overruns and other bad stuff. Cheers, Brendan (4 Replies)
Discussion started by: Brendan Kennedy
4 Replies
Login or Register to Ask a Question