C linux中的文件访问控制
我正在写一个模块来控制文件访问。我现在正试图实施禁止打开文件的规定C linux中的文件访问控制,c,linux,assembly,module,linux-kernel,C,Linux,Assembly,Module,Linux Kernel,我正在写一个模块来控制文件访问。我现在正试图实施禁止打开文件的规定我已经尝试过内核模块系统打开重载: 我还尝试了系统读取过载: 第一个问题是,我只能在控制台中限制对目标文件的访问(它在图形界面中不起作用,例如gedit)。 第二个选项不起作用,因为系统在使用目标文件运行gedit时停止响应 如何禁止在不使用SELinux的情况下打开文件? asmlinkage int custom_open(const char __user *file_name, int flags, mode_t mod)
我已经尝试过内核模块系统打开重载: 我还尝试了系统读取过载: 第一个问题是,我只能在控制台中限制对目标文件的访问(它在图形界面中不起作用,例如gedit)。 第二个选项不起作用,因为系统在使用目标文件运行gedit时停止响应
如何禁止在不使用SELinux的情况下打开文件?
asmlinkage int custom_open(const char __user *file_name, int flags, mode_t mod)
{
printk("hook: open(\"%s\")", file_name);
if (!strcmp(file_name, "test"))
return -1;
return original_open(file_name, flags, mod);
}
struct file *fget(unsigned int fd)
{
struct file *file;
struct files_struct *files = current->files;
rcu_read_lock();
file = fcheck_files(files, fd);
if (file) {
/* File object ref couldn't be taken */
if (file->f_mode & FMODE_PATH ||
!atomic_long_inc_not_zero(&file->f_count))
file = NULL;
}
rcu_read_unlock();
return file;
}
您可以直接在SO上发布代码。请这样做,我无法打开链接。@请在问题正文中发布代码,单击“编辑”并添加,用4个空格缩进以使系统高亮显示。您确定用用户提交的文件名标识文件是否明智?如果用户
open()。。。使用gedit打开时,filename=path/to/filename。
asmlinkage int custom_read(int fd, void *buf, size_t noct)
{
struct file *filp = fget(fd);
unsigned char f_name[DNAME_INLINE_LEN];
strcpy(f_name,filp->f_path.dentry->d_name.name);
if (!strcmp(f_name, "_ttt.c")){
printk("hook1: read fd=(%d: -> %s)\n",fd,f_name);
return -1;
}
return original_read(fd, buf, noct);
}