views:

54

answers:

2

I have a little problem. I install this module into my kernel and its written under /proc

When I try to open() it from user mode I get the following message:

"Can't open device file: my_dev"

static int module_permission(struct inode *inode, int op, struct nameidata *foo)
{
 //if its write
 if ((op == 2)&&(writer == DOESNT_EXIST)){
  writer = EXIST ;
  return 0;
 }
 //if its read
 if (op == 4 ){
  numOfReaders++;
  return 0;
 }
 return -EACCES;
}

int procfs_open(struct inode *inode, struct file *file)
{
 try_module_get(THIS_MODULE);
 return 0;
}

static struct file_operations File_Ops_4_Our_Proc_File = {
 .read   = procfs_read,
 .write   = procfs_write,
 .open   = procfs_open,
 .release = procfs_close,
};

static struct inode_operations Inode_Ops_4_Our_Proc_File = {
 .permission = module_permission, /* check for permissions */
};

int init_module()
{
 /* create the /proc file */
 Our_Proc_File = create_proc_entry(PROC_ENTRY_FILENAME, 0644, NULL);
 /* check if the /proc file was created successfuly */
 if (Our_Proc_File == NULL){
  printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
         PROC_ENTRY_FILENAME);
  return -ENOMEM;
 }

 Our_Proc_File->owner = THIS_MODULE;
 Our_Proc_File->proc_iops = &Inode_Ops_4_Our_Proc_File;
 Our_Proc_File->proc_fops = &File_Ops_4_Our_Proc_File;
 Our_Proc_File->mode = S_IFREG | S_IRUGO | S_IWUSR;
 Our_Proc_File->uid = 0;
 Our_Proc_File->gid = 0;
 Our_Proc_File->size = 80;

 //i added init the writewr status
 writer = DOESNT_EXIST;
 numOfReaders = 0 ;
 printk(KERN_INFO "/proc/%s created\n", PROC_ENTRY_FILENAME);
 return 0;
}
A: 

the only thing this program do is: sprintf(moduleName,"/%s/%s",DEVICE_DIRECTORY,DEVICE_FILE_NAME); int file_desc = open(moduleName, O_WRONLY); this is what i get in the strace: execve("./writer", ["./writer"], [/* 40 vars */]) = 0 brk(0) = 0x9606000 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) mmap2(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f9d000 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) open("/etc/ld.so.cache", O_RDONLY) = 3 fstat64(3, {st_mode=S_IFREG|0644, st_size=59825, ...}) = 0 mmap2(NULL, 59825, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7f8e000 close(3) = 0 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) open("/lib/tls/i686/cmov/libc.so.6", O_RDONLY) = 3 read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\320h\1\0004\0\0\0\344"..., 512) = 512 fstat64(3, {st_mode=S_IFREG|0755, st_size=1442180, ...}) = 0 mmap2(NULL, 1451632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7e2b000 mprotect(0xb7f87000, 4096, PROT_NONE) = 0 mmap2(0xb7f88000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x15c) = 0xb7f88000 mmap2(0xb7f8b000, 9840, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb7f8b000 close(3) = 0 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7e2a000 set_thread_area({entry_number:-1 -> 6, base_addr:0xb7e2a6c0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0 open("/dev/urandom", O_RDONLY) = 3 read(3, "\306\330B\200"..., 4) = 4 close(3) = 0 mprotect(0xb7f88000, 8192, PROT_READ) = 0 mprotect(0x8049000, 4096, PROT_READ) = 0 mprotect(0xb7fbc000, 4096, PROT_READ) = 0 munmap(0xb7f8e000, 59825) = 0 brk(0) = 0x9606000 brk(0x9627000) = 0x9627000 open("/proc/ex3mod", O_WRONLY) = -1 EACCES (Permission denied) fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), ...}) = 0 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f9c000 write(1, "Can't open device file: ex3mod\n"..., 31Can't open device file: ex3mod ) = 31 exit_group(-1) = ?

yoavstr
A: 
open("/proc/ex3mod", O_WRONLY) = -1 EACCES (Permission denied)

Like the previous commenter said, you don't have permissions to open the proc file. Try fixing the permissions with chmod.

Eric Seppanen
although i use chmod o+w /proc/ex3mod i am still not permitted am i doing something wrong (most probably )?
yoavstr
yoavstr