2016-11-09 73 views
0

我的目標是修改進程的打開文件描述符的訪問權限。例如,有一個PID已知的進程,其中有兩個文件描述符與標準3打開。一個以只讀權限打開,另一個以只寫權限打開。我想修改文件描述符的權限,從只讀到讀寫。之後,文件描述符可用於寫入其創建的對象。瞭解用於處理文件描述符的linux內核數據結構

我已經編寫了一個內核模塊,使我可以訪問由其PID標識的進程的文件描述符。我搜索了頭文件和論壇,以瞭解linux數據結構如何處理文件描述符,但我仍然感到困惑。我發現,每個進程都有自己的task_struct,其中包含所有打開文件的成員,其中包含一個打開文件描述符的數組。我不知道它是如何與inode鏈接的。

我發現有結構文件的成員,被稱爲f_mode,讓我們的權限,但我無法找到調用它的方法。當我直接訪問它時,它給了我一個unsigned int,但我不知道什麼值映射到什麼?另外,我不確定這是否是存儲訪問權限的數據成員。如果我修改它會改變文件描述符的訪問權限嗎?

的代碼如下:

static int __init mainInit(void){ 

    int pid=13433; 
    struct task_struct * task; 
    struct files_struct * files; 
    struct fdtable * filestable; 
    struct path files_path; 

    //Get Task structure from PID 
    task = pid_task(find_vpid(pid), PIDTYPE_PID); 
    //Get open FIles from the task tstructure 
    files = task->files; 

    filestable=files_fdtable(files); 

    int i=0; 
    char *cwd; 
    char *buf = (char *)kmalloc(GFP_KERNEL,100*sizeof(char)); 

    while(filestable->fd[i] != NULL){ 
     files_path = filestable->fd[i]->f_path; 
     cwd=d_path(&files_path,buf, 100*sizeof(char)); 

     printk(KERN_INFO "Open FD with %d with name %s with access %x\n", i, cwd,filestable->fd[i]->f_mode); 

     //printk(KERN_INFO "FMode read:%x Fmodewrite:%x\n",FMODE_READ,FMODE_WRITE); 


     //Check access mode 
     if(filestable->fd[i]->f_mode==FMODE_READ){ 
      printk(KERN_INFO "File has access FMODE_READ\n"); 
     }else if(filestable->fd[i]->f_mode==FMODE_WRITE){ 
      printk(KERN_INFO "File has access FMODE_WRTIE\n"); 
     } 
     i++; 
    } 
    return 0; 
} 

static void __exit mainExit(void){ 
    printk(KERN_INFO "Goodbye Kernel!. Returning to normal useless world!\n"); 
} 

module_init(mainInit); 
module_exit(mainExit); 

回答

0

的想法是錯誤的,該代碼是錯誤的。什麼是實際目標?爲什麼該文件未開始寫入?如果程序明確打開它以便閱讀,程序如何突然開始寫入文件?這不加上任何東西。

你不能只是放置標誌來讓文件打開。您將不得不重新開放路徑中的所有支票。

static int __init mainInit(void){ 

大部分內核不使用camelCase。

int pid=13433; 

'='周圍缺少空格。 '*' 和 '任務' 之間

struct task_struct * task; 

雜散空間。

struct files_struct * files; 
    struct fdtable * filestable; 

該iditom命名爲'fdt'。

struct path files_path; 

    //Get Task structure from PID 
    task = pid_task(find_vpid(pid), PIDTYPE_PID); 

既不是rcu也不是任務列表,因此這是不安全的。

//Get open FIles from the task tstructure 
    files = task->files; 

任務鎖定未被佔用,因此該威脅是不安全的。

filestable=files_fdtable(files); 

內核本身會告訴你,如果你啓用了調試行是錯誤的。

int i=0; 
    char *cwd; 
    char *buf = (char *)kmalloc(GFP_KERNEL,100*sizeof(char)); 

爲什麼投,爲什麼100,爲什麼包括sizeof(char)?此處硬編碼爲100時也是錯誤的,因爲它會引起容易出錯的重複數字。

while(filestable->fd[i] != NULL){ 

不僅FD表鎖不被佔用,因此遍歷是不安全的,它並沒有他們的方式,你想要的工作。一個進程可以在兩個用戶之間有未使用的fds。

 files_path = filestable->fd[i]->f_path; 
     cwd=d_path(&files_path,buf, 100*sizeof(char)); 
是什麼讓你覺得它是安全的,這裏d_path擺在首位數100

邀請容易出錯的重複?

 printk(KERN_INFO "Open FD with %d with name %s with access %x\n", i, cwd,filestable->fd[i]->f_mode); 

     //printk(KERN_INFO "FMode read:%x Fmodewrite:%x\n",FMODE_READ,FMODE_WRITE); 


     //Check access mode 
     if(filestable->fd[i]->f_mode==FMODE_READ){ 
      printk(KERN_INFO "File has access FMODE_READ\n"); 
     }else if(filestable->fd[i]->f_mode==FMODE_WRITE){ 
      printk(KERN_INFO "File has access FMODE_WRTIE\n"); 
     } 
     i++; 
    } 
    return 0; 
} 

static void __exit mainExit(void){ 
    printk(KERN_INFO "Goodbye Kernel!. Returning to normal useless world!\n"); 
}