0

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?

Community
  • 1
  • 1
footy
  • 5,803
  • 13
  • 48
  • 96
  • 3
    FYI: `sizeof(" ")` is 2, not 1 (it counts the null terminator). – nneonneo Oct 26 '12 at 00:57
  • 2
    How do you know when to stop? `count` is how many bytes you've written out, which is greater than 0, until you run out of disk space. You need a better terminating condition. – John Szakmeister Oct 26 '12 at 01:04

1 Answers1

1

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.

ssgriffonuser
  • 271
  • 1
  • 2