How to get the "file name" from struct file* in the kernel? - c

I want to know on what file in /sys/* the functionstatic ssize_t
sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) were called. the question is how to get the file name/path by having struct file *file ?
struct file {
mode_t f_mode;
loff_t f_pos;
unsigned short f_flags;
unsigned short f_count;
unsigned long f_reada, f_ramax, f_raend, f_ralen, f_rawin;
struct file *f_next, *f_prev;
int f_owner; /* pid or -pgrp where SIGIO should be sent */
struct inode * f_inode;
struct file_operations * f_op;
unsigned long f_version;
void *private_data; /* needed for tty driver, and maybe others */
};
EDIT:
I understand there is no one-to-one mapping between inode to a name, but I must know the major/minor/file descriptor and can search for some of the inode's name in the file system.

Actuall you can retrieve the file path used to open the file. In linux struct file represents an opened file and has the path used to open that file associate with the structure. Before 2.6.20 it was a member called f_dentry and from 2.6.20 it is a new member called f_path. You can use function d_path() to retrieve the full path of your file directly.
Refer to the following Linux source code:
http://lxr.free-electrons.com/source/include/linux/fs.h?v=3.10#L763
http://lxr.free-electrons.com/source/fs/dcache.c?v=3.0#L2626
And also answer:
How can I get a filename from a file descriptor inside a kernel module?

In Linux and other Unix-like OSs, a file object can be pointed to by any number of names, and the file object itself has no pointers back to any of them. Names are not a feature of the file, they are just an external index.

Related

Understanding linux kernel data structures for handling file descriptors

My goal is to modify a process's open file descriptor's access permission. For eg, there is a process whose PID is known, which has 2 file descriptors open apart from the standard 3. One was opened with read only permission and other was opened with write only permission. I want to modify the file descriptors permission from read only to read and write. After which, the file descriptor can be used to write the object it was created with.
I have written a kernel module that gives me access to the file descriptor of the process identified by its PID. I searched the header and forums to understand how the linux data structure works for handling the file descriptor but I am still confused. What I found out is, every process has its own task_struct, which contains a member for all open files under, which contains an array of open file descriptor. I do not know how it links from here on with the inode.
I found out that there is member of struct file, called as f_mode that gives us the permissions but I cannot find a method to call it. When I access it directly, it gives me an unsigned int but I don't know what value maps to what? Also, I am not sure if this is the data member which stores the access permissions. If I modify this would it change the access permissions for file descriptor?
The code is given below:
static int __init mainInit(void){
int pid=13433;
struct task_struct * task;
struct files_struct * files;
struct fdtable * filestable;
struct path files_path;
//Get Task structure from PID
task = pid_task(find_vpid(pid), PIDTYPE_PID );
//Get open FIles from the task tstructure
files = task->files;
filestable=files_fdtable(files);
int i=0;
char *cwd;
char *buf = (char *)kmalloc(GFP_KERNEL,100*sizeof(char));
while(filestable->fd[i] != NULL){
files_path = filestable->fd[i]->f_path;
cwd=d_path(&files_path,buf, 100*sizeof(char));
printk(KERN_INFO "Open FD with %d with name %s with access %x\n", i, cwd,filestable->fd[i]->f_mode);
//printk(KERN_INFO "FMode read:%x Fmodewrite:%x\n",FMODE_READ,FMODE_WRITE);
//Check access mode
if(filestable->fd[i]->f_mode==FMODE_READ){
printk(KERN_INFO "File has access FMODE_READ\n");
}else if(filestable->fd[i]->f_mode==FMODE_WRITE){
printk(KERN_INFO "File has access FMODE_WRTIE\n");
}
i++;
}
return 0;
}
static void __exit mainExit(void){
printk(KERN_INFO "Goodbye Kernel!. Returning to normal useless world!\n");
}
module_init(mainInit);
module_exit(mainExit);

How to check the given folder is a mount point

I have to test the given folder path is a mount point or it is in local to my system with c code.
I read on net that /etc/mtab file have entry for mounted point. I can check that file but is their any system call or any function to check mount point.
My kernel version is 2.6.28.10.
These functions can be used for data structure access the mtab file:
FILE * setmntent(const char *file, const char *mode)
int endmntent (FILE *stream)
struct mntent * getmntent (FILE *stream)
struct mntent * getmntent_r (FILE *stream, struct mentent *result, char *buffer, int bufsize)
int addmntent (FILE *stream, const struct mntent *mnt)
char * hasmntopt (const struct mntent *mnt, const char *opt)
For more details about these functions refer the man page.
On linux, getmntent() is suitable for parsing /etc/mtab.

Check /proc file from C kernel module

I'd like to share a variable between kernel and user space and I've found that it's possible with procfs.
The kernel module must act in certain way if given value is set. The user space program is responsible for changing this value, but the kernel module must read it when necessary.
I know that I must create the /proc file in the kernel module.
My question is, how to read the file from the kernel module?
Source : linux.die.net/lkmpg/x769.html
/**
* This function is called with the /proc file is written
*
*/
int procfile_write(struct file *file, const char *buffer, unsigned long count,
void *data)
{
/* get buffer size */
procfs_buffer_size = count;
if (procfs_buffer_size > PROCFS_MAX_SIZE ) {
procfs_buffer_size = PROCFS_MAX_SIZE;
}
/* write data to the buffer */
if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) {
return -EFAULT;
}
return procfs_buffer_size;
}
To clarify, in Your module whenever user writes to Your file in procfs, this example shows how to handle such write.
In kernel >= 3.10 proc_write is moved to structure file_operations where declaration of write is different, so in newest your solution won't work.
You can implement typical file_operations.write(struct file *, const char __user *, size_t, loff_t *) and reference this to:
struct proc_dir_entry your_proc_dir_entry{
.proc_fops = &your_fops,
}

Casting to a File Pointer

I'm setting up the FUSE filesystem, and I need to get a file pointer to any open files so that I can encrypt them as they get written. The FUSE system uses a special struct. One of the components of the struct is called fh and is of type uint64_t. When a file is opened, it is captured in an int variable as you can see in the following code:
/* File open operation */
int bb_open(const char *path, struct fuse_file_info *fi)
{
int fd;
fd = open(path, fi->flags);
fi->fh = fd;
return fd;
}
This fi struct is available to me in the write function, and I'm hoping that I can use it to create a file pointer that I can pass to my encryption function. Here is the code for the write function as I have it set up at the moment:
/* Write data to an open file */
int bb_write(const char *path, const char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
char* password;
FILE* fp;
//malloc and instantiate password
fp = (FILE*) fi->fh; //my lame attempt to get a file pointer
return encrypt(<inpFilePtr>, <outFilePtr>, 1, password);
}
Finally, the signature of my encryption function looks like this:
extern int encrypt(FILE* in, FILE* out, int action, char* key_str);
I'd like to take that fi->fh variable and turn it into a file pointer so that I can use it as my input file pointer argument. Since it was originally created by an "open" operation, it seems like there should be a way to do this, but I can't make it work.
Can anyone help? Thanks!
File descriptors and FILE * pointers aren't the same thing. A FILE * pointer is a pointer to an opaque type provided by your C implementation - the APIs that deal with FILE * (fopen, fread, fwrite, fclose, etc.) are part of standard C. File descriptors, on the other hand, are operated on using the POSIX system calls (open, read, write, close, etc.) that are often used to implement the C-level abstractions.
Luckily there are functions that let you get one from the other. In your case, you'll want to use fdopen(3) to get a FILE * from the file descriptor. If you wanted to go the other way, you'd want to look into fileno(3).

get fd from file pointer in kernel space

Given a struct file, is it possible to get the associated file descriptor in linux kernel space? I am trying to change permissions using either sys_chmod or sys_fchmod. One takes a file descriptor the other expects a filename from user space. I can figure out how to get the filename but how would I cast it to a user space pointer?
Thanks
The function you're after is chmod_common:
static int chmod_common(struct path *path, umode_t mode)
Which takes a path and the mode you want to set. Unfortunately, as you noticed, it's static and obviously not exported. So you could go multiple ways:
Replicate whatever it does in a function of your own
Get "the file descriptor" from struct file (ugly)
Find a way to call sys_chmod
Now sys_chmod expects a user pointer but you're in the kernel. Here's what you could do to trick it:
mm_segment_t oldfs = get_fs();
char __user *userptr;
userptr = (char __user __force *) kernptr;
set_fs(KERNEL_DS);
/* call sys_chmod */
set_fs(oldfs);
All this is very much in line with "things you never should do in the Kernel".

Resources