Linux核心原始碼情景分析-存取權限與檔案安全性

來源:互聯網
上載者:User

標籤:

    在Linux核心原始碼情景分析-從路徑名到目標節點,一文中path_walk代碼中,err = permission(inode, MAY_EXEC)當前進程是否可以訪問這個節點,代碼如下:

int permission(struct inode * inode,int mask){if (inode->i_op && inode->i_op->permission) {int retval;lock_kernel();retval = inode->i_op->permission(inode, mask);unlock_kernel();return retval;}return vfs_permission(inode, mask);}
    在ext2_read_inode中,i_op可以設定為ext2_file_inode_operations,ext2_dir_inode_operations,ext2_fast_symlink_inode_operations,page_symlink_inode_operations,均沒有permission指標。所以執行vfs_permission,代碼如下:

int vfs_permission(struct inode * inode,int mask){int mode = inode->i_mode;if ((mask & S_IWOTH) && IS_RDONLY(inode) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))return -EROFS; //如果要求的寫檔案,並且是唯讀系統,而且是常規檔案,目錄,或者連結時,返回-EROFS,表示不能訪問if ((mask & S_IWOTH) && IS_IMMUTABLE(inode)) //Nobody gets write access to an immutable filereturn -EACCES; if (current->fsuid == inode->i_uid)//如果當前進程的fsuid和inode結構的i_uid相等,那麼應該比較mode中S_IRUSR、S_IWUSR、S_IXUSR位mode >>= 6;else if (in_group_p(inode->i_gid))//如果當前進程的fsgid和inode結構的i_gid相等,那麼應該比較mode中S_IRGRP、S_IWGRP、S_IXGRP位mode >>= 3;if (((mode & mask & S_IRWXO) == mask) || capable(CAP_DAC_OVERRIDE))//mask相當於要求,mode是現有的目前使用者可以訪問的許可權,如果相等就返回0。假如不相等,如果當前進程得到了授權,允許其CAP_DAC_OVERRIDE,即可以淩駕於檔案系統的存取權限控制機制DAC之上。return 0;/* read and search access */if ((mask == S_IROTH) ||    (S_ISDIR(inode->i_mode)  && !(mask & ~(S_IROTH | S_IXOTH))))if (capable(CAP_DAC_READ_SEARCH))return 0;return -EACCES;}

     其中mask為:

#define MAY_EXEC 1#define MAY_WRITE 2#define MAY_READ 4
    inode->i_mode為:

    用於對三種不同使用者的存取權限:

#define S_IRWXU 00700#define S_IRUSR 00400#define S_IWUSR 00200#define S_IXUSR 00100#define S_IRWXG 00070#define S_IRGRP 00040#define S_IWGRP 00020#define S_IXGRP 00010#define S_IRWXO 00007#define S_IROTH 00004#define S_IWOTH 00002#define S_IXOTH 00001

    參考這張圖就好理解了。

    


    還有三個標誌位,是一個狀態佔一個位:

#define S_ISUID  0004000#define S_ISGID  0002000#define S_ISVTX  0001000
    現在16位只剩下4位了,表示檔案類型,要為每種檔案類型都分配一個標誌位就不夠了,所以表示檔案的類型的這4位是編碼的。
#define S_IFMT  00170000#define S_IFSOCK 0140000#define S_IFLNK 0120000#define S_IFREG  0100000#define S_IFBLK  0060000#define S_IFDIR  0040000#define S_IFCHR  0020000#define S_IFIFO  0010000


    capable,代碼如下:

static inline int capable(int cap){#if 1 /* ok now */if (cap_raised(current->cap_effective, cap))#elseif (cap_is_fs_cap(cap) ? current->fsuid == 0 : current->euid == 0)#endif{current->flags |= PF_SUPERPRIV;return 1;}return 0;}
#define cap_raised(c, flag)  (cap_t(c) & CAP_TO_MASK(flag))
#define cap_t(x) (x)
#define CAP_TO_MASK(x) (1 << (x))
   

    current->cap_effective是在sys_execve->do_execve->prepare_binprm初始設定的。

int prepare_binprm(struct linux_binprm *bprm){int mode;struct inode * inode = bprm->file->f_dentry->d_inode;mode = inode->i_mode;/* Huh? We had already checked for MAY_EXEC, WTF do we check this? */if (!(mode & 0111))/* with at least _one_ execute bit set */return -EACCES;if (bprm->file->f_op == NULL)return -EACCES;bprm->e_uid = current->euid;bprm->e_gid = current->egid;if(!IS_NOSUID(inode)) {/* Set-uid? */if (mode & S_ISUID)bprm->e_uid = inode->i_uid;/* Set-gid? *//* * If setgid is set but no group execute bit then this * is a candidate for mandatory locking, not a setgid * executable. */if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))bprm->e_gid = inode->i_gid;}/* We don‘t have VFS support for capabilities yet */cap_clear(bprm->cap_inheritable);cap_clear(bprm->cap_permitted);cap_clear(bprm->cap_effective);/*  To support inheritance of root-permissions and suid-root         *  executables under compatibility mode, we raise all three         *  capability sets for the file.         *         *  If only the real uid is 0, we only raise the inheritable         *  and permitted sets of the executable file.         */if (!issecure(SECURE_NOROOT)) {if (bprm->e_uid == 0 || current->uid == 0) {cap_set_full(bprm->cap_inheritable);cap_set_full(bprm->cap_permitted);}if (bprm->e_uid == 0) cap_set_full(bprm->cap_effective);//這裡設定的}memset(bprm->buf,0,BINPRM_BUF_SIZE);return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);}


Linux核心原始碼情景分析-存取權限與檔案安全性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.