I am trying to write a /proc file created by a loadable kernel module. I am using fopen() to open the file for write but am getting errno : 13 (permission denied).
FILE *fp;
fp = fopen("/proc/file1","w");
if(fp == NULL){
printf("Errno : %d",errno); // prints 13
}
The LKM contains the following code:
static struct proc_dir_entry *proc_entry;
static ssize_t proc_write(struct file *filp, const char __user *buff, unsigned long len, void *data)
{
// code writes from buffer to local variable
return len;
}
static ssize_t proc_read(char *page, char **start, off_t off, int count, int *eof, void *data)
{
// code for reading file
return 0;
}
int proc_open(struct inode *inode, struct file *file)
{
try_module_get(THIS_MODULE);
return 0;
}
int proc_close(struct inode *inode, struct file *file)
{
module_put(THIS_MODULE);
return 0;
}
Any suggestions on how to overcome this?
Thanks.
The most probable answer is that the procfs node that is created does not have the correct permissions for the user.
When run as root, it bypasses most of the permission checking for the node, so you do not receive an error (there are exceptions; this is the general case).
in the kernel loadable module, where it creates the procfs node (somewhere in the .c file):
You need to make sure that the second parameter, the mode is set to something that permits opening for writing by users other than root in order to support your desired open option; e.g.
0666makes the file globally openable as R/W by anyone.Generally, nodes in procfs are created with the flags
0444(i.e. R/O only for all users). Some are created in mode0644(R/W by root, R/O for all other users), some are created with permissions0400(R/O for root, all others stay away).