3

I know this is strongly not recommended. But does is possible to do this in kernel space.

Given the file path, can we remove the corresponding file in kernel space?

Van Yu
  • 129
  • 1
  • 11
  • 1
    Using `call_usermodehelper_fns` to exec `rm` in kernel space is the way I could think out. The real example in kernel could be found [here](http://lxr.free-electrons.com/source/kernel/kmod.c#L69), by which kernel calls `/sbin/modprobe` to load module. – Chris Tsui Feb 18 '16 at 07:45
  • 1
    Looks like you look for function [vfs_unlink](http://lxr.free-electrons.com/source/fs/namei.c#L3785), but its usage is relatively complex. It is called from [do_unlinkat](http://lxr.free-electrons.com/source/fs/namei.c#L3831) with simple usage, but that function is *static*, so it is inaccessible for you. – Tsyvarev Feb 18 '16 at 09:18
  • likely similar techniques to: http://stackoverflow.com/questions/1184274/how-to-read-write-files-within-a-linux-kernel-module – Ciro Santilli OurBigBook.com May 10 '17 at 07:06

1 Answers1

0

Maybe it's too late, I'll try to reply. As Tsyvarev said in his comment probably you are looking for the vfs_unlink function that you can find here namei.c. Before the implementation there is a description, but a simple example can be this one /* fcheck's prototype is in linux/fdtable.h and returns a file pointer given a given a file descriptor */

struct file *filp= fcheck(fd);
struct inode *parent_inode = filp->f_path.dentry->d_parent->d_inode;
inode_lock(parent_inode);
vfs_unlink(parent_inode, filp->f_path.dentry, NULL);    
inode_unlock(parent_inode);

I hope it's can be useful to someone.

Federico A.
  • 256
  • 2
  • 8