2012-02-02 80 views
-1

我已經寫了模塊讀/寫在/ proc文件和工作正常,但要使用權限與它時,我給下面顯示的權限,它給了我錯誤(基本上我希望每個人都可以讀取文件,但只有根可以寫在裏面)。struct task_struct成員?

int my_permission(struct inode *inode, int op) 
{ 
if(op == 4||(op == 2 && current->euid = 0)) //euid is not a member of task_struct 
return 0; 
return -EACCES; 
} 
const struct inode_operations my_iops = { 
.permission = my_permission, 
}; 

但它給我的錯誤如下:

/home/karan/practice/procf/testproc1.c: In function ‘my_permission’: 
/home/karan/practice/procf/testproc1.c:50:32: error: ‘struct task_struct’ has no member named ‘euid’ 

我認爲在task_struct中指向用戶ID其他成員。我對解決方案以及task_struct成員字段的描述感興趣。

問候 卡蘭

+0

你看過http://kernel.org/doc/htmldocs/ – 2012-02-02 19:00:55

+0

@Shiplu我想要一些關於task_struct的具體內容,沒有別的.........雖然鏈接很好。 – karan421 2012-02-02 19:08:55

+0

Google「linux lxr」,找到'struct task_struct'的定義,看看你有哪些字段。 – ugoren 2012-02-02 19:37:18

回答

1

見在include/linux/cred.h:

 
    #define current_euid()   (current_cred_xxx(euid)) 
    #define current_cred_xxx(xxx)     \ 
    ({            \ 
      current_cred()->xxx;     \ 
    }) 

等current_euid()倒在current_cred():

 
    /** 
    * current_cred - Access the current task's subjective credentials 
    * 
    * Access the subjective credentials of the current task. RCU-safe, 
    * since nobody else can modify it. 
    */ 
    #define current_cred() \ 
      rcu_dereference_protected(current->cred, 1) 

所以對於你的問題,要做到有效的UID比較,看看/fs/exec.c:

 
    if (current_euid() == current_uid() && current_egid() == current_gid()) 
    bprm->cred->euid = current_euid(); 
      bprm->cred->euid = inode->i_uid 

與程序對比:

 
    if (current_euid() == 0) 

這意味着即使用戶未被登錄爲UID = 0,它也會被有效處理爲UID == 0?聽起來很危險

1
if(op == 4||(op == 2 && current->euid = 0)) 
  1. 您確定要分配0到current->euid?也許你打算比較一下。
  2. current在哪裏定義?我假設它的struct task_struct,但如果是這樣,是euid一個指針?或者是current指向struct task_struct?如果沒有euid也不current的指針只是.

類進行主題的更換->,但:

const struct inode_operations my_iops = { 
.permission = my_permission, 
}; 

是,這將使程序員維護你的代碼之類的話要殺死你在你的睡眠中。爲了便於閱讀,請使用typedef。不要聲明全局變量;如果你這樣做,不要在函數之後聲明它們。在聲明具有部分賦值的結構時,在聲明之後它的可讀性更高。

我知道它是一個測試,或者是一個不完整的代碼,或者沒有人需要維護的東西。但是你可能想在幾年內閱讀這些內容,當你這樣做時,你會花費兩倍的時間去尋找你正在尋找的解決方案。而且你在SO上張貼這樣的話,那裏有一些像我這樣的納粹風格的人,在看到這樣的聲明後,今晚會有困難。

+1

與[this](http://kerneltrap.org/node/1584)驚人的相似,試圖在Linux內核中創建一個後門。 – ugoren 2012-02-02 19:34:48

+0

@whitelion好吧我可以理解會有一些編寫代碼的標準....但是這個代碼是我從「Linux內核編程指南」中拿出來的,並且在將來我會嘗試給你的建議(以便你可以睡覺(只是開玩笑))....但最新的解決方案..... – karan421 2012-02-02 19:58:03

+0

@karan你試過改變current-> euid current.euid? – whitelionV 2012-02-02 20:17:18

相關問題