footy
footy

Reputation: 5931

Why is this kernel function going to infinite loop?

I am trying to overwrite all the data in a file in a kernel system call (Yes I know I shouldnt do a file read/write in kernel but I am just trying it out! ) based on the answer here How to read/write files within a Linux kernel module?

Here is the code:

int sys_mycompleteerase(const char __user * inputFileUser)  {

    struct file* filp = NULL;   
    int err = 0,count = 0;
    unsigned long long offset =0;

    mm_segment_t old_fs;
    old_fs = get_fs();
    set_fs(KERNEL_DS);

    filp = filp_open(inputFileUser, O_WRONLY, 0644);
    if(IS_ERR(filp)) {
        err = PTR_ERR(filp);
        return NULL;
    }
    do {
        count = vfs_write(filp," ",sizeof(" "), &offset);
        offset+=sizeof(" ");
    }while(count > 0);
    filp_close(filp, NULL);

    set_fs(old_fs);

    return 0;

}

If I give a proper file name in the user space program, it just keeps on writing without stopping. Why?

Upvotes: 0

Views: 443

Answers (1)

ssgriffonuser
ssgriffonuser

Reputation: 281

It looks like you are continually writing to a file, so the file length is growing. There is no reason for the vfs_write to fail. If I understand correctly, what you want to do is overwrite the entire file. So you would have to first find the size of the file, then write that many bytes to the file, then close the file. As it stands you are just growing the file with the 'space' character.

Upvotes: 1

Related Questions