详解如何在内核中操作某个文件?

系统 Linux
有粉丝问我如何在内核中操作某个文件?本篇就这个问题给大家详细介绍一下,希望能够帮助到你!

[[386034]]

一、问题描述

如何在内核中操作某个文件?


问题

二、操作函数

1. 分析

在用户态,读写文件可以通过read和write这两个系统调用来完成(C库函数实际上是对系统调用的封装)。但是,在内核态没有这样的系统调用,我们又该如何读写文件呢?

阅读Linux内核源码,可以知道陷入内核执行的是实际执行的是sys_read和sys_write这两个函数,但是这两个函数没有使用EXPORT_SYMBOL导出,也就是说其他模块不能使用。

在fs/open.c中系统调用具体实现如下(内核版本3.14):

  1. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) 
  2.  if (force_o_largefile()) 
  3.   flags |= O_LARGEFILE; 
  4.  
  5.  return do_sys_open(AT_FDCWD, filename, flags, mode); 

跟踪do_sys_open()函数,

  1. long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode) 
  2.  struct open_flags op; 
  3.  int fd = build_open_flags(flags, mode, &op); 
  4.  struct filename *tmp; 
  5.  
  6.  if (fd) 
  7.   return fd; 
  8.  
  9.  tmp = getname(filename); 
  10.  if (IS_ERR(tmp)) 
  11.   return PTR_ERR(tmp); 
  12.  
  13.  fd = get_unused_fd_flags(flags); 
  14.  if (fd >= 0) { 
  15.   struct file *f = do_filp_open(dfd, tmp, &op); 
  16.   if (IS_ERR(f)) { 
  17.    put_unused_fd(fd); 
  18.    fd = PTR_ERR(f); 
  19.   } else { 
  20.    fsnotify_open(f); 
  21.    fd_install(fd, f); 
  22.   } 
  23.  } 
  24.  putname(tmp); 
  25.  return fd; 

就会发现它主要使用了do_filp_open()函数该函数在fs/namei.c中,

  1. struct file *do_filp_open(int dfd, struct filename *pathname, 
  2.   const struct open_flags *op) 
  3.  struct nameidata nd; 
  4.  int flags = op->lookup_flags; 
  5.  struct file *filp; 
  6.  
  7.  filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 
  8.  if (unlikely(filp == ERR_PTR(-ECHILD))) 
  9.   filp = path_openat(dfd, pathname, &nd, op, flags); 
  10.  if (unlikely(filp == ERR_PTR(-ESTALE))) 
  11.   filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 
  12.  return filp; 

该函数最终打开了文件,并返回file类型指针。所以我们只需要找到其他调用了do_filp_open()函数的地方,就可找到我们需要的文件操作函数。

而在文件fs/open.c中,filp_open函数也是调用了file_open_name函数,

  1. /** 
  2.  * filp_open - open file and return file pointer 
  3.  * 
  4.  * @filename: path to open 
  5.  * @flags: open flags as per the open(2) second argument 
  6.  * @mode: mode for the new file if O_CREAT is setelse ignored 
  7.  * 
  8.  * This is the helper to open a file from kernelspace if you really 
  9.  * have to.  But in generally you should not do this, so please move 
  10.  * along, nothing to see here.. 
  11.  */ 
  12. struct file *filp_open(const char *filename, int flags, umode_t mode) 
  13.  struct filename name = {.name = filename}; 
  14.  return file_open_name(&name, flags, mode); 
  15. EXPORT_SYMBOL(filp_open); 

函数file_open_name调用了do_filp_open,并且接口和sys_open函数极为相似,调用参数也和sys_open一样,并且使用EXPORT_SYMBOL导出了,所以在内核中可以使用该函数打开文件,功能非常类似于应用层的open。

  1. /** 
  2.  * file_open_name - open file and return file pointer 
  3.  * 
  4.  * @name: struct filename containing path to open 
  5.  * @flags: open flags as per the open(2) second argument 
  6.  * @mode: mode for the new file if O_CREAT is setelse ignored 
  7.  * 
  8.  * This is the helper to open a file from kernelspace if you really 
  9.  * have to.  But in generally you should not do this, so please move 
  10.  * along, nothing to see here.. 
  11.  */ 
  12. struct file *file_open_name(struct filename *nameint flags, umode_t mode) 
  13.  struct open_flags op; 
  14.  int err = build_open_flags(flags, mode, &op); 
  15.  return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op); 

2. 所有操作函数

使用同样的方法,找出了一组在内核操作文件的函数,如下:

这些函数的参数非常类似于应用层文件IO函数,open、read、write、close。

3. 用户空间地址

虽然我们找到了这些函数,但是我们还不能直接使用。

因为在vfs_read和vfs_write函数中,其参数buf指向的用户空间的内存地址,如果我们直接使用内核空间的指针,则会返回-EFALUT。

这是因为使用的缓冲区超过了用户空间的地址范围。一般系统调用会要求你使用的缓冲区不能在内核区。这个可以用set_fs()、get_fs()来解决。

在include/asm/uaccess.h中,有如下定义:

  1. #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) 
  2. #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF) 
  3. #define USER_DS MAKE_MM_SEG(PAGE_OFFSET) 
  4. #define get_ds() (KERNEL_DS) 
  5. #define get_fs() (current->addr_limit) 
  6. #define set_fs(x) (current->addr_limit = (x)) 

如果使用,可以按照如下顺序执行:

  1. mm_segment_t fs = get_fs(); 
  2. set_fs(KERNEL_FS); 
  3. //vfs_write(); 
  4. //vfs_read(); 
  5. set_fs(fs); 

详解:系统调用本来是提供给用户空间的程序访问的,所以,对传递给它的参数(比如上面的buf),它默认会认为来自用户空间,在read或write()函数中,为了保护内核空间,一般会用get_fs()得到的值来和USER_DS进行比较,从而防止用户空间程序“蓄意”破坏内核空间。

而现在要在内核空间使用系统调用,此时传递给read或write()的参数地址就是内核空间的地址了,在USER_DS之上(USER_DS ~ KERNEL_DS),如果不做任何其它处理,在write()函数中,会认为该地址超过了USER_DS范围,所以会认为是用户空间的“蓄意破坏”,从而不允许进一步的执行。

为了解决这个问题, set_fs(KERNEL_DS),将其能访问的空间限制扩大到KERNEL_DS,这样就可以在内核顺利使用系统调用了!

在VFS的支持下,用户态进程读写任何类型的文件系统都可以使用read和write这两个系统调用,但是在linux内核中没有这样的系统调用我们如何操作文件呢?

我们知道read和write在进入内核态之后,实际执行的是sys_read和sys_write,但是查看内核源代码,发现这些操作文件的函数都没有导出(使用EXPORT_SYMBOL导出),也就是说在内核模块中是不能使用的,那如何是好?

通过查看sys_open的源码我们发现,其主要使用了do_filp_open()函数,该函数在fs/namei.c中,而在改文件中,filp_open函数也是间接调用了do_filp_open函数,并且接口和sys_open函数极为相似,调用参数也和sys_open一样,并且使用EXPORT_SYMBOL导出了,所以我们猜想该函数可以打开文件,功能和open一样。

三、实例

Makefile

  1. ifneq ($(KERNELRELEASE),) 
  2. obj-m:=sysopen.o 
  3. else 
  4. KDIR :=/lib/modules/$(shell uname -r)/build 
  5. PWD  :=$(shell pwd) 
  6. all
  7.  $(info "1st"
  8.  make -C $(KDIR) M=$(PWD) modules 
  9. clean: 
  10.  rm -f *.ko *.o *.mod.o *.symvers *.cmd  *.mod.c *.order 
  11. endif 

sysopen.c

  1. #include <linux/module.h> 
  2. #include <linux/syscalls.h> 
  3. #include <linux/file.h> 
  4. #include <linux/fcntl.h> 
  5. #include <linux/delay.h> 
  6. #include <linux/slab.h> 
  7. #include <linux/uaccess.h> 
  8.  
  9. MODULE_LICENSE("GPL"); 
  10. MODULE_AUTHOR("yikoulinux"); 
  11.  
  12. void test(void) 
  13.  struct file *file = NULL
  14.  mm_segment_t old_fs; 
  15.  loff_t  pos; 
  16.  
  17.  char buf[64]="yikoulinux"
  18.  
  19.  printk("test()"); 
  20.  file = filp_open("/home/peng/open/test.txt\n",O_RDWR|O_APPEND|O_CREAT,0644); 
  21.  if(IS_ERR(file)){ 
  22.   return ; 
  23.  } 
  24.  old_fs = get_fs(); 
  25.  set_fs(KERNEL_DS); 
  26.  pos = 0; 
  27.  vfs_write(file,buf,sizeof(buf),&pos); 
  28.  
  29.  pos =0; 
  30.  vfs_read(file, buf, sizeof(buf), &pos); 
  31.  printk("buf:%s\n",buf); 
  32.   
  33.  filp_close(file,NULL); 
  34.  set_fs(old_fs); 
  35.  return
  36.  
  37.  
  38. static int hello_init(void) 
  39.  printk("hello_init \n"); 
  40.  test(); 
  41.  return 0; 
  42. static void hello_exit(void) 
  43.  printk("hello_exit \n"); 
  44.  return
  45.  
  46. module_init(hello_init); 
  47. module_exit(hello_exit); 

编译:


安装模块:


查看操作的文件:


查看文件内容:


可见在内核模块中成功操作了文件。

 

责任编辑:姜华 来源: 一口Linux
相关推荐

2019-01-07 10:24:41

2010-04-12 11:19:47

编译内核模块

2010-04-23 14:50:29

Aix操作系统

2016-08-10 12:52:31

2009-12-08 12:22:05

内核Makefile软链接

2017-03-09 17:02:23

UbuntuLinux升级

2022-06-22 09:56:19

PythonMySQL数据库

2019-08-19 11:55:10

UbuntuLinux内核

2009-04-29 16:05:23

Oracle连接输出SQL

2011-08-22 16:26:25

IOS开发Sqlite数据库

2021-04-09 18:01:03

前端ReactDOM

2023-04-17 16:17:19

LinuxPDF

2010-02-25 17:56:39

Linux操作系统

2019-04-12 15:25:15

Fedora 30内核命令行

2023-03-17 11:33:18

Linux

2009-07-21 14:57:41

iBatis中调用存储iBatis

2023-03-15 10:34:47

Linux文件数

2016-08-29 21:36:55

nginxWeb缓存

2020-07-06 15:50:41

Python文件Linux

2017-03-17 16:30:23

点赞
收藏

51CTO技术栈公众号