自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解如何在內(nèi)核中操作某個(gè)文件?

系統(tǒng) Linux
有粉絲問(wèn)我如何在內(nèi)核中操作某個(gè)文件?本篇就這個(gè)問(wèn)題給大家詳細(xì)介紹一下,希望能夠幫助到你!

[[386034]]

一、問(wèn)題描述

如何在內(nèi)核中操作某個(gè)文件?


問(wèn)題

二、操作函數(shù)

1. 分析

在用戶態(tài),讀寫文件可以通過(guò)read和write這兩個(gè)系統(tǒng)調(diào)用來(lái)完成(C庫(kù)函數(shù)實(shí)際上是對(duì)系統(tǒng)調(diào)用的封裝)。但是,在內(nèi)核態(tài)沒有這樣的系統(tǒng)調(diào)用,我們又該如何讀寫文件呢?

閱讀Linux內(nèi)核源碼,可以知道陷入內(nèi)核執(zhí)行的是實(shí)際執(zhí)行的是sys_read和sys_write這兩個(gè)函數(shù),但是這兩個(gè)函數(shù)沒有使用EXPORT_SYMBOL導(dǎo)出,也就是說(shuō)其他模塊不能使用。

在fs/open.c中系統(tǒng)調(diào)用具體實(shí)現(xiàn)如下(內(nèi)核版本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()函數(shù),

  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; 

就會(huì)發(fā)現(xiàn)它主要使用了do_filp_open()函數(shù)該函數(shù)在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; 

該函數(shù)最終打開了文件,并返回file類型指針。所以我們只需要找到其他調(diào)用了do_filp_open()函數(shù)的地方,就可找到我們需要的文件操作函數(shù)。

而在文件fs/open.c中,filp_open函數(shù)也是調(diào)用了file_open_name函數(shù),

  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); 

函數(shù)file_open_name調(diào)用了do_filp_open,并且接口和sys_open函數(shù)極為相似,調(diào)用參數(shù)也和sys_open一樣,并且使用EXPORT_SYMBOL導(dǎo)出了,所以在內(nèi)核中可以使用該函數(shù)打開文件,功能非常類似于應(yīng)用層的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. 所有操作函數(shù)

使用同樣的方法,找出了一組在內(nèi)核操作文件的函數(shù),如下:

這些函數(shù)的參數(shù)非常類似于應(yīng)用層文件IO函數(shù),open、read、write、close。

3. 用戶空間地址

雖然我們找到了這些函數(shù),但是我們還不能直接使用。

因?yàn)樵趘fs_read和vfs_write函數(shù)中,其參數(shù)buf指向的用戶空間的內(nèi)存地址,如果我們直接使用內(nèi)核空間的指針,則會(huì)返回-EFALUT。

這是因?yàn)槭褂玫木彌_區(qū)超過(guò)了用戶空間的地址范圍。一般系統(tǒng)調(diào)用會(huì)要求你使用的緩沖區(qū)不能在內(nèi)核區(qū)。這個(gè)可以用set_fs()、get_fs()來(lái)解決。

在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)) 

如果使用,可以按照如下順序執(zhí)行:

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

詳解:系統(tǒng)調(diào)用本來(lái)是提供給用戶空間的程序訪問(wèn)的,所以,對(duì)傳遞給它的參數(shù)(比如上面的buf),它默認(rèn)會(huì)認(rèn)為來(lái)自用戶空間,在read或write()函數(shù)中,為了保護(hù)內(nèi)核空間,一般會(huì)用get_fs()得到的值來(lái)和USER_DS進(jìn)行比較,從而防止用戶空間程序“蓄意”破壞內(nèi)核空間。

而現(xiàn)在要在內(nèi)核空間使用系統(tǒng)調(diào)用,此時(shí)傳遞給read或write()的參數(shù)地址就是內(nèi)核空間的地址了,在USER_DS之上(USER_DS ~ KERNEL_DS),如果不做任何其它處理,在write()函數(shù)中,會(huì)認(rèn)為該地址超過(guò)了USER_DS范圍,所以會(huì)認(rèn)為是用戶空間的“蓄意破壞”,從而不允許進(jìn)一步的執(zhí)行。

為了解決這個(gè)問(wèn)題, set_fs(KERNEL_DS),將其能訪問(wèn)的空間限制擴(kuò)大到KERNEL_DS,這樣就可以在內(nèi)核順利使用系統(tǒng)調(diào)用了!

在VFS的支持下,用戶態(tài)進(jìn)程讀寫任何類型的文件系統(tǒng)都可以使用read和write這兩個(gè)系統(tǒng)調(diào)用,但是在linux內(nèi)核中沒有這樣的系統(tǒng)調(diào)用我們?nèi)绾尾僮魑募?

我們知道read和write在進(jìn)入內(nèi)核態(tài)之后,實(shí)際執(zhí)行的是sys_read和sys_write,但是查看內(nèi)核源代碼,發(fā)現(xiàn)這些操作文件的函數(shù)都沒有導(dǎo)出(使用EXPORT_SYMBOL導(dǎo)出),也就是說(shuō)在內(nèi)核模塊中是不能使用的,那如何是好?

通過(guò)查看sys_open的源碼我們發(fā)現(xiàn),其主要使用了do_filp_open()函數(shù),該函數(shù)在fs/namei.c中,而在改文件中,filp_open函數(shù)也是間接調(diào)用了do_filp_open函數(shù),并且接口和sys_open函數(shù)極為相似,調(diào)用參數(shù)也和sys_open一樣,并且使用EXPORT_SYMBOL導(dǎo)出了,所以我們猜想該函數(shù)可以打開文件,功能和open一樣。

三、實(shí)例

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); 

編譯:


安裝模塊:


查看操作的文件:


查看文件內(nèi)容:


可見在內(nèi)核模塊中成功操作了文件。

 

責(zé)任編輯:姜華 來(lái)源: 一口Linux
相關(guān)推薦

2019-01-07 10:24:41

2010-04-12 11:19:47

編譯內(nèi)核模塊

2016-08-10 12:52:31

2010-04-23 14:50:29

Aix操作系統(tǒng)

2009-12-08 12:22:05

內(nèi)核Makefile軟鏈接

2022-06-22 09:56:19

PythonMySQL數(shù)據(jù)庫(kù)

2021-04-09 18:01:03

前端ReactDOM

2010-02-25 17:56:39

Linux操作系統(tǒng)

2017-03-09 17:02:23

UbuntuLinux升級(jí)

2016-08-29 21:36:55

nginxWeb緩存

2019-08-19 11:55:10

UbuntuLinux內(nèi)核

2017-03-17 16:30:23

2017-03-13 15:55:11

Windows 10WindowsDeskScapes

2009-04-29 16:05:23

Oracle連接輸出SQL

2011-08-22 16:26:25

IOS開發(fā)Sqlite數(shù)據(jù)庫(kù)

2024-04-03 00:10:24

C#System數(shù)據(jù)

2023-03-15 10:34:47

Linux文件數(shù)

2011-06-03 09:26:11

2019-04-12 15:25:15

Fedora 30內(nèi)核命令行

2021-08-30 13:08:54

Linux實(shí)時(shí)監(jiān)控日志文件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)