訂閱
糾錯(cuò)
加入自媒體

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

一、問(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)沒(méi)有這樣的系統(tǒng)調(diào)用,我們又該如何讀寫文件呢?

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

在fs/open.c中系統(tǒng)調(diào)用具體實(shí)現(xiàn)如下(內(nèi)核版本3.14):

SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)

if (force_o_largefile())
 flags |= O_LARGEFILE;
return do_sys_open(AT_FDCWD, filename, flags, mode);

跟蹤do_sys_open()函數(shù),

long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)

struct open_flags op;
int fd = build_open_flags(flags, mode, &op);
struct filename *tmp;
if (fd)
 return fd;
tmp = getname(filename);
if (IS_ERR(tmp))
 return PTR_ERR(tmp);
fd = get_unused_fd_flags(flags);
if (fd >= 0) {
 struct file *f = do_filp_open(dfd, tmp, &op);
 if (IS_ERR(f)) {
  put_unused_fd(fd);
  fd = PTR_ERR(f);
 } else {
  fsnotify_open(f);
  fd_install(fd, f);
 }

putname(tmp);
return fd;

就會(huì)發(fā)現(xiàn)它主要使用了do_filp_open()函數(shù)該函數(shù)在fs/namei.c中,

struct file *do_filp_open(int dfd, struct filename *pathname,
 const struct open_flags *op)

struct nameidata nd;
int flags = op->lookup_flags;
struct file *filp;
filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
if (unlikely(filp == ERR_PTR(-ECHILD)))
 filp = path_openat(dfd, pathname, &nd, op, flags);
if (unlikely(filp == ERR_PTR(-ESTALE)))
 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
return filp;

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

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


* filp_open - open file and return file pointer

* @filename: path to open
* @flags: open flags as per the open(2) second argument
* @m(xù)ode: mode for the new file if O_CREAT is set, else ignored

* This is the helper to open a file from kernelspace if you really
* have to.  But in generally you should not do this, so please move
* along, nothing to see here..

struct file *filp_open(const char *filename, int flags, umode_t mode)

struct filename name = {.name = filename};
return file_open_name(&name, flags, mode);

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。


* file_open_name - open file and return file pointer

* @name: struct filename containing path to open
* @flags: open flags as per the open(2) second argument
* @m(xù)ode: mode for the new file if O_CREAT is set, else ignored

* This is the helper to open a file from kernelspace if you really
* have to.  But in generally you should not do this, so please move
* along, nothing to see here..

struct file *file_open_name(struct filename *name, int flags, umode_t mode)

struct open_flags op;
int err = build_open_flags(flags, mode, &op);
return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);

2. 所有操作函數(shù)

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

功能函數(shù)原型打開文件struct file *filp_open(const char *filename, int flags, int mode)讀文件ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)寫文件ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)關(guān)閉文件int filp_close(struct file *filp, fl_owner_t id)

這些函數(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中,有如下定義:

#define MAKE_M(jìn)M_SEG(s) ((mm_segment_t) { (s) })
#define KERNEL_DS MAKE_M(jìn)M_SEG(0xFFFFFFFF)
#define USER_DS MAKE_M(jìn)M_SEG(PAGE_OFFSET)
#define get_ds() (KERNEL_DS)
#define get_fs() (current->addr_limit)
#define set_fs(x) (current->addr_limit = (x))

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

mm_segment_t fs = get_fs();
set_fs(KERNEL_FS);
//vfs_write();
//vfs_read();
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)核中沒(méi)有這樣的系統(tǒng)調(diào)用我們?nèi)绾尾僮魑募兀?/p>

我們知道read和write在進(jìn)入內(nèi)核態(tài)之后,實(shí)際執(zhí)行的是sys_read和sys_write,但是查看內(nèi)核源代碼,發(fā)現(xiàn)這些操作文件的函數(shù)都沒(méi)有導(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

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

sysopen.c

#include <linux/module.h>
#include <linux/syscalls.h>
#include <linux/file.h>
#include <linux/fcntl.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("yikoulinux");
void test(void)

struct file *file = NULL;
mm_segment_t old_fs;
loff_t  pos;
char buf[64]="yikoulinux";
printk("test()");
file = filp_open("/home/peng/open/test.txt",O_RDWR|O_APPEND|O_CREAT,0644);
if(IS_ERR(file)){
 return ;

old_fs = get_fs();
set_fs(KERNEL_DS);
pos = 0;
vfs_write(file,buf,sizeof(buf),&pos);
pos =0;
vfs_read(file, buf, sizeof(buf), &pos);
printk("buf:%s",buf);

filp_close(file,NULL);
set_fs(old_fs);
return;

static int hello_init(void)

printk("hello_init ");
test();
return 0;

static void hello_exit(void)

printk("hello_exit ");
return;

module_init(hello_init);
module_exit(hello_exit);

編譯:

安裝模塊:

查看操作的文件:

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

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


聲明: 本文由入駐維科號(hào)的作者撰寫,觀點(diǎn)僅代表作者本人,不代表OFweek立場(chǎng)。如有侵權(quán)或其他問(wèn)題,請(qǐng)聯(lián)系舉報(bào)。

發(fā)表評(píng)論

0條評(píng)論,0人參與

請(qǐng)輸入評(píng)論內(nèi)容...

請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字

您提交的評(píng)論過(guò)于頻繁,請(qǐng)輸入驗(yàn)證碼繼續(xù)

  • 看不清,點(diǎn)擊換一張  刷新

暫無(wú)評(píng)論

暫無(wú)評(píng)論

人工智能 獵頭職位 更多
掃碼關(guān)注公眾號(hào)
OFweek人工智能網(wǎng)
獲取更多精彩內(nèi)容
文章糾錯(cuò)
x
*文字標(biāo)題:
*糾錯(cuò)內(nèi)容:
聯(lián)系郵箱:
*驗(yàn) 證 碼:

粵公網(wǎng)安備 44030502002758號(hào)