Linux3.0.0內核中客體(如文件/目錄)相關的DAC安全數據結構(傳統9Bit模塊、ACL模式)


第1章           FCB數據結構(文件控制塊)


1.  目錄項

    相當於FCB次部,包括兩個內容: 即文件名和inode編號, 不同的文件名可能對應同一個inode編號,  這樣便對應同一個FCB主部, 即一個文件可以有多個名字.

 2.  inode

    相當於FCB主部, 包括文件主、共享說明、地址信息等, 稱爲inode, inode與文件具有一對一的關係. 在UNIX文件系統中, 有一個固定的區域, 用於保存所有文件的inode.每個inode有一個唯一的編號,稱爲i_number。

inode定義在include/linux/fs.h中,具體如下:

struct inode {
      /* RCU path lookup touches following: */
      umode_t                     i_mode;
      uid_t                    i_uid;
      gid_t                    i_gid;
      const struct inode_operations  *i_op;
      struct super_block      *i_sb;
 
      spinlock_t            i_lock;   /* i_blocks, i_bytes, maybe i_size */
      unsigned int        i_flags;
      unsigned long             i_state;
#ifdef CONFIG_SECURITY
      void                     *i_security;
#endif
      struct mutex        i_mutex;
 
 
      unsigned long             dirtied_when;      /* jiffies of first dirtying */
 
      struct hlist_node i_hash;
      struct list_head    i_wb_list;     /* backing dev IO list */
      struct list_head    i_lru;             /* inode LRU list */
      struct list_head    i_sb_list;
      union {
             struct list_head    i_dentry;
             struct rcu_head          i_rcu;
      };
      unsigned long             i_ino;
      atomic_t              i_count;
      unsigned int        i_nlink;
      dev_t                   i_rdev;
      unsigned int        i_blkbits;
      u64               i_version;
      loff_t                   i_size;
#ifdef __NEED_I_SIZE_ORDERED
      seqcount_t           i_size_seqcount;
#endif
      struct timespec           i_atime;
      struct timespec           i_mtime;
      struct timespec           i_ctime;
      blkcnt_t        i_blocks;
      unsigned short          i_bytes;
      struct rw_semaphore  i_alloc_sem;
      const struct file_operations      *i_fop;   /* former ->i_op->default_file_ops */
      struct file_lock    *i_flock;
      struct address_space  *i_mapping;
      struct address_space  i_data;
#ifdef CONFIG_QUOTA
      struct dquot         *i_dquot[MAXQUOTAS];
#endif
      struct list_head    i_devices;
      union {
             struct pipe_inode_info      *i_pipe;
             struct block_device    *i_bdev;
             struct cdev          *i_cdev;
      };
 
      __u32                  i_generation;
 
#ifdef CONFIG_FSNOTIFY
      __u32                  i_fsnotify_mask; /* all events this inode cares about */
      struct hlist_head  i_fsnotify_marks;
#endif
 
#ifdef CONFIG_IMA
      atomic_t              i_readcount; /* struct files open RO */
#endif
      atomic_t              i_writecount;
#ifdef CONFIG_FS_POSIX_ACL
      struct posix_acl   *i_acl;
      struct posix_acl   *i_default_acl;
#endif
      void                     *i_private; /* fs or device private pointer */
};


第2章           UGO與ACL的數據結構


其中,i_mode爲UGO權限(User, Group, Other),其類型umode_t在include/linux/types.h中定義:

typedef unsigned short          umode_t;


可見i_mode本質上就是一個16bit的空間,只有右邊10bit被使用。UGO訪問控制方式將文件的權限用三組3位的bit描述,即9bit,並且在最前面加上一位作爲文件的類型標誌。表示是文件還是目錄,每類用戶佔3位,讀、寫、執行權限各用1位描述,具有權限時,該位就設置爲1。讀、寫、執行權限分別用r、w、x三個字符表示。

i_acl和i_default_acl爲ACL數據結構的指針,分別代表當前ACL和默認ACL,默認ACL只有目錄具有,當目錄中新建文件或目錄時,新建文件會沿用父目錄的默認ACL。

posix_acl等數據結構定義在/include/linux/posix_acl.h文件中


struct posix_acl_entry {
      short                    e_tag;
      unsigned short            e_perm;
      unsigned int        e_id;
};
 
struct posix_acl {
      atomic_t              a_refcount;
      unsigned int        a_count;
      struct posix_acl_entry       a_entries[0];
};

每一個 ACL 實體由三部分組成: e_tag 、 e_id 、 e_perm 。 e_tag 表示 ACL 實體的標誌,如 user:tux:rwx 中 user 就是一個 e_tag 。 e_id 是 ACL 實體限制的用戶或組 id, 如 user:tux:rwx中的 tux, 在某些實體中這個項可以爲空。 e_perm 說明的是具體的訪問權限,主要有 rwx 三種,這和傳統的 u/g/o 模式是一致的。在 Posix 標準中規定一共有 6 種 e_tag ,分別是 ACL_USER_OBJ, ACL_USER,ACL_GROUP_OBJ, ,ACL_GROUP, ACL_MASK, ACL_OTHER 。 ACL_USER_OBJ 是文件屬主的 ACL 實體,ACL_GROUP_OBJ 是文件屬組的 ACL 實體, ACL_MASK 是掩碼 ACL 實體, ACL_OTHER 是其他用戶的 ACL 實體。這四種 ( 個 )ACL 實體的 id 都爲空,其他類型的 ACL 實體的 e_id 都不能爲空。

 

第3章           相關操作


3.1           include/linux/posix_acl.c中的相關函數


posix_acl_init(struct posix_acl *acl, int count)

初始化一個ACL

 

struct posix_acl *posix_acl_alloc(int count, gfp_t flags)

分配一個ACL的空間

 

struct posix_acl *posix_acl_clone(const struct posix_acl *acl, gfp_tflags)

複製一個ACL

 

Int posix_acl_valid(const struct posix_acl *acl)

檢查一個ACL的合法性

 

Int posix_acl_equiv_mode(const struct posix_acl *acl, mode_t*mode_p)

比較一個ACL與一個UGO權限

 

struct posix_acl *posix_acl_from_mode(mode_t mode, gfp_t flags)

從UGO權限創建一個ACL

 

posix_acl_permission(struct inode *inode, const struct posix_acl*acl, int want)

檢查當前進程是否有訪問inode的want權限

 

3.2           include/fs/ext4/acl.c中的相關函數


static struct posix_acl *ext4_acl_from_disk(const void *value,size_t size)

從磁盤中取出ACL

 

static void *ext4_acl_to_disk(const struct posix_acl *acl, size_t*size)

將ACL存入磁盤

 

static struct posix_acl *ext4_get_acl(struct inode *inode, int type)

從inode中取出ACL

 

static int ext4_set_acl(handle_t *handle, struct inode *inode, inttype, struct posix_acl *acl)

將ACL存入inode

 

Int ext4_check_acl(struct inode *inode, int mask, unsigned intflags)

檢查當前進程是否有訪問inode的mask權限,內部調用了posix_acl_permission函數。

 


第4章           參考資料


在Linux 內核源碼中找到了相關的數據結構:

/usr/src/linux-3.0/fs/ext4/acl.h

/usr/src/linux-3.0/fs/ext4/acl.c

/usr/src/linux-3.0/include/linux/posix_acl.h

/usr/src/linux-3.0/fs/posix_acl.c

 

發佈了48 篇原創文章 · 獲贊 47 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章