天天看點

seandroid政策介紹1

Policy

[編輯]Policy編譯

在SEAndroid中有關policy的相關源檔案都在源碼目錄external/sepolicy中,在Android.mk檔案中描述了相關編譯過程,首先會使用m4預處理器将sepolicy中的所有相關檔案整合成一個源檔案plicy.conf,然後通過checkpolicy 編譯器将policy.conf政策源檔案編譯成sepolicy.24的二進制政策檔案(24為政策版本号)。checkpolicy編譯器的所有源檔案都在external/checkpolicy目錄中,編譯完成的二進制政策檔案會在系統啟動時被加載到核心中在權限檢測時使用,而在系統啟動時為一些系統檔案添加安全上下文則是依據另外兩個檔案file_contexts和seapp_contexts實作。

[編輯]SEAndroid中的安全上下文介紹

SEAndroid的安全上下文共有4個部分組成分别為user、role、type、sensitivity,以u:object_r:system_data_file:s0為例:

user:安全上下文的第一列為user,在SEAndroid中的user隻有一個就是u。

role:第二清單示role,在SEAndroid中的role有兩個,分别為r和object_r。

type:第三列為type,SEAndroid中共定義了139種不同的type。

sensitivity:第四列是專為MLS通路機制所添加的安全上下文的擴充部分,格式為sensitivity[:category list][- sensitivity[:category list]],例如s0 - s15:c0.c1023,其中s0之後的内容可以不需要,冒号後面的内容是category,sensitivity和category組合一起聲明了目前的安全級别,“-”号左右分别辨別了安全級别的最低和最高,這一列的參數将在MLS限制檢查時用到,“15”、“1023”表示了sensitivity和category的最大值,這一參數可在Android.mk中定義。

[編輯]Transition

某個程式被執行時,其相應的程序會處在相應的domain中,但當程式根據需要又執行了另一個程式時,程序就需要根據type transition規則進行domain transition,進而使新程序能順利通路到相關檔案。

[編輯]domain transition

完成domain transition需要滿足5個條件,當程序需要進行domain切換時就會檢測切換前和切換前和切換後兩個domain是否滿足這5個條件。

  • 聲明可執行檔案為相應 domain 的“接入點“檔案

這是通過allow語句顯式聲明主體對客體具有entrypoint權限完成,以下列出了全部SEAndroid中出現的聲明:

以下的第三列部分就是聲明的”接入點“檔案,有了這個聲明後第三列的type就成為了”接入點“檔案。

allow adbd rootfs:file entrypoint;
allow bluetoothd bluetoothd_exec:file { entrypoint read execute };
allow dbusd dbusd_exec:file { entrypoint read execute };
allow debuggerd debuggerd_exec:file { entrypoint read execute };
allow drmserver drmserver_exec:file { entrypoint read execute };
allow gpsd gpsd_exec:file { entrypoint read execute };
allow installd installd_exec:file { entrypoint read execute };
allow keystore keystore_exec:file { entrypoint read execute };
allow mediaserver mediaserver_exec:file { entrypoint read execute };
allow netd netd_exec:file { entrypoint read execute };
allow qemud qemud_exec:file { entrypoint read execute };
allow rild rild_exec:file { entrypoint read execute };
allow servicemanager servicemanager_exec:file { entrypoint read execute };
allow shell shell_exec:file { entrypoint read execute };
allow shell shell_exec:file { entrypoint read execute };
allow surfaceflinger surfaceflinger_exec:file { entrypoint read execute };
allow su su_exec:file { entrypoint read execute };
allow ueventd rootfs:file entrypoint;
allow vold vold_exec:file { entrypoint read execute };
allow wpa wpa_exec:file { entrypoint read execute };
allow zygote zygote_exec:file { entrypoint read execute };
      
  • 許可使用者程序能夠執行“接入點”檔案

通過allow語句顯式聲明需要進行domain切換的主體必須對”接入點“檔案擁有execute權限,以下列出了在SEAndroid中全部相關部分:

allow init bluetoothd_exec:file { getattr open read execute };
allow init dbusd_exec:file { getattr open read execute };
allow init debuggerd_exec:file { getattr open read execute };
allow init drmserver_exec:file { getattr open read execute };
allow init gpsd_exec:file { getattr open read execute };
allow init installd_exec:file { getattr open read execute };
allow init keystore_exec:file { getattr open read execute };
allow init mediaserver_exec:file { getattr open read execute };
allow init netd_exec:file { getattr open read execute };
allow init qemud_exec:file { getattr open read execute };
allow init rild_exec:file { getattr open read execute };
allow init servicemanager_exec:file { getattr open read execute };
allow adbd shell_exec:file { getattr open read execute };
allow init shell_exec:file { getattr open read execute };
allow init surfaceflinger_exec:file { getattr open read execute };
allow shell su_exec:file { getattr open read execute };
allow init vold_exec:file { getattr open read execute };
allow init wpa_exec:file { getattr open read execute };
allow init zygote_exec:file { getattr open read execute };
      
  • 許可兩個 domain 之間的切換

通過allow語句顯式聲明發生切換的兩個domain之間必須擁有transition或dyntransition權限,以下列出了全部SEAndroid中出現部分:

allow init bluetoothd:process transition;
allow init dbusd:process transition;
allow init debuggerd:process transition;
allow init drmserver:process transition;
allow init gpsd:process transition;
allow init installd:process transition;
allow init keystore:process transition;
allow init mediaserver:process transition;
allow init netd:process transition;
allow init qemud:process transition;
allow init rild:process transition;
allow init servicemanager:process transition;
allow adbd shell:process transition;
allow init shell:process transition;
allow init surfaceflinger:process transition;
allow shell su:process transition;
allow init vold:process transition;
allow init wpa:process transition;
allow init zygote:process transition;
allow zygote system:process dyntransition;
allow zygote appdomain:process dyntransition;
      
  • 許可程序的角色和新domain之間的關聯

SEAndroid通過安全上下文中的role實作了“基于角色的通路控制”(RBAC – Role Based Access Control)。程序安全上下文中的role的作用就是限制了該程序能夠轉換到的新的domain。在SEAndroid中隻有兩個role,分别命名為r,object_r。通過使用規則role r types domain來将角色r與屬性domain進行關聯,這樣就能確定所有安全上下文中角色為r的主體可以轉換為在domain屬性中的有的type(多個同一類type可以歸到一個屬性(attribute)中)。

  • 使能 domain 切換

通過type_transition語句聲明來使能domain切換,以下列出了部分type_transition規則,第二列是轉換前的domain,第三列是”接入點“檔案,第四列則是轉換後的domain。

type_transition init bluetoothd_exec:process bluetoothd;
type_transition init dbusd_exec:process dbusd;
type_transition init debuggerd_exec:process debuggerd;
type_transition init drmserver_exec:process drmserver;
type_transition init gpsd_exec:process gpsd;
type_transition gpsd gps_data_file:sock_file gps_socket;
type_transition init installd_exec:process installd;
type_transition init keystore_exec:process keystore;
type_transition init mediaserver_exec:process mediaserver;
type_transition init netd_exec:process netd;
type_transition init qemud_exec:process qemud;
type_transition init rild_exec:process rild;
type_transition init servicemanager_exec:process servicemanager;
type_transition adbd shell_exec:process shell;
type_transition init shell_exec:process shell;
type_transition init surfaceflinger_exec:process surfaceflinger;
type_transition shell su_exec:process su;
type_transition system wifi_data_file:sock_file system_wpa_socket;
type_transition init vold_exec:process vold;
type_transition init wpa_exec:process wpa;
type_transition init zygote_exec:process zygote;
      

[編輯]type transition

type_transition 規則被用在Domain Transition中 或者确定新建立對象的标簽,以重載其預設的、從父目錄(containing directory)所繼承的标簽。通常情況下新建立檔案的标簽和其父目錄的标簽一緻,但是可以使用 type_transition 規則為其指定特定的标簽。

[編輯]标記安全上下文的方式

[編輯]基于政策語句标記

[編輯]預設标記

[編輯]程式請求标記

[編輯]初始SID标記

[編輯]基于不同檔案系統的各類标記機制

[編輯]基于擴充屬性(fs_use_xattr)

[編輯]基于任務(fs_use_task)

[編輯]基于轉換(fs_use_trans)

[編輯]一般方式(genfscon)

[編輯]關于Policy的三個核心源檔案介紹

[編輯]file_contexts

這一檔案主要羅列了啟動時為系統的5個部分添加的安全上下文的内容,這5個部分分别為device(/dev目錄下)、system(/system目錄下)、data(/data目錄下)、efs(/efs目錄下)、catch(/catch目錄下)。安全上下文的添加是以比對左邊的目錄正規表達式實作,這一過程是在系統啟動并運作了initramfs檔案裝載了二進制policy檔案并為rootfs檔案系統添加完安全上下文後進行的。

以下是file_contexts檔案的全部内容:

###########################################
# Root
#
# Nothing required since it is initramfs and implicitly labeled
# by genfscon rootfs in ocontexts.
#
##########################
# Devices
#
/dev(/.*)?              u:object_r:device:s0
/dev/akm8973.*          u:object_r:akm_device:s0
/dev/accelerometer      u:object_r:accelerometer_device:s0
/dev/alarm              u:object_r:alarm_device:s0
/dev/android_adb.*      u:object_r:adb_device:s0
/dev/ashmem             u:object_r:ashmem_device:s0
/dev/audio.*            u:object_r:audio_device:s0
/dev/binder             u:object_r:binder_device:s0
/dev/block(/.*)?        u:object_r:block_device:s0
/dev/block/loop[0-9]*   u:object_r:loop_device:s0
/dev/block/ram[0-9]*    u:object_r:ram_device:s0
/dev/block/mtdblock5    u:object_r:radio_device:s0
/dev/cam                u:object_r:camera_device:s0
/dev/console            u:object_r:console_device:s0
/dev/cpuctl(/.*)?       u:object_r:cpuctl_device:s0
/dev/device-mapper      u:object_r:dm_device:s0
/dev/full               u:object_r:full_device:s0
/dev/graphics(/.*)?     u:object_r:graphics_device:s0
/dev/input(/.*)         u:object_r:input_device:s0
/dev/kmem               u:object_r:kmem_device:s0
/dev/log(/.*)?          u:object_r:log_device:s0
/dev/mem                u:object_r:kmem_device:s0
/dev/modem.*            u:object_r:radio_device:s0
/dev/mtd(/.*)?          u:object_r:mtd_device:s0
/dev/mtd/mtd5           u:object_r:radio_device:s0
/dev/mtd/mtd5ro         u:object_r:radio_device:s0
/dev/mtp_usb            u:object_r:mtp_device:s0
/dev/pn544              u:object_r:nfc_device:s0
/dev/ptmx               u:object_r:ptmx_device:s0
/dev/pvrsrvkm           u:object_r:powervr_device:s0
/dev/qemu_.*            u:object_r:qemu_device:s0
/dev/kmsg               u:object_r:kmsg_device:s0
/dev/null               u:object_r:null_device:s0
/dev/nvhdcp1            u:object_r:video_device:s0
/dev/nvmap              u:object_r:nv_device:s0
/dev/nvhost-.*          u:object_r:nv_device:s0
/dev/random             u:object_r:random_device:s0
/dev/s3c-jpg            u:object_r:camera_device:s0
/dev/s3c-mem            u:object_r:camera_device:s0
/dev/s3c-mfc            u:object_r:graphics_device:s0
/dev/snd(/.*)?          u:object_r:audio_device:s0
/dev/socket             u:object_r:socket_device:s0
/dev/socket/bluetooth   u:object_r:bluetooth_socket:s0
/dev/socket/dbus_bluetooth      u:object_r:bluetooth_socket:s0
/dev/socket/dbus        u:object_r:dbus_socket:s0
/dev/socket/dnsproxyd   u:object_r:dnsproxyd_socket:s0
/dev/socket/installd    u:object_r:installd_socket:s0
/dev/socket/keystore    u:object_r:keystore_socket:s0
/dev/socket/netd        u:object_r:netd_socket:s0
/dev/socket/property_service    u:object_r:property_socket:s0
/dev/socket/qemud       u:object_r:qemud_socket:s0
/dev/socket/rild        u:object_r:rild_socket:s0
/dev/socket/rild-debug  u:object_r:rild_debug_socket:s0
/dev/socket/vold        u:object_r:vold_socket:s0
/dev/socket/wpa_eth[0-9] u:object_r:wpa_socket:s0
/dev/socket/wpa_wlan[0-9] u:object_r:wpa_socket:s0
/dev/socket/zygote      u:object_r:zygote_socket:s0
/dev/spdif_out.*        u:object_r:audio_device:s0
/dev/tegra.*            u:object_r:video_device:s0
/dev/tty[0-9]*          u:object_r:tty_device:s0
/dev/ttyS[0-9]*         u:object_r:serial_device:s0
/dev/uinput             u:object_r:input_device:s0
/dev/urandom            u:object_r:urandom_device:s0
/dev/vcs[0-9a-z]*       u:object_r:vcs_device:s0
/dev/video[0-9]*        u:object_r:video_device:s0
/dev/zero               u:object_r:zero_device:s0
#############################
# System files
#
/system(/.*)?           u:object_r:system_file:s0
/system/bin/ash         u:object_r:shell_exec:s0
/system/bin/mksh        u:object_r:shell_exec:s0
/system/bin/sh          --      u:object_r:shell_exec:s0
/system/bin/app_process u:object_r:zygote_exec:s0
/system/bin/servicemanager      u:object_r:servicemanager_exec:s0
/system/bin/surfaceflinger      u:object_r:surfaceflinger_exec:s0
/system/bin/drmserver   u:object_r:drmserver_exec:s0
/system/bin/vold        u:object_r:vold_exec:s0
/system/bin/netd        u:object_r:netd_exec:s0
/system/bin/rild        u:object_r:rild_exec:s0
/system/bin/mediaserver u:object_r:mediaserver_exec:s0
/system/bin/dbus-daemon u:object_r:dbusd_exec:s0
/system/bin/installd    u:object_r:installd_exec:s0
/system/bin/keystore    u:object_r:keystore_exec:s0
/system/bin/debuggerd   u:object_r:debuggerd_exec:s0
/system/bin/bluetoothd  u:object_r:bluetoothd_exec:s0
/system/bin/wpa_supplicant      u:object_r:wpa_exec:s0
/system/bin/qemud       u:object_r:qemud_exec:s0
/system/xbin/su         u:object_r:su_exec:s0
/system/vendor/bin/gpsd u:object_r:gpsd_exec:s0
#############################
# Data files
#
/data(/.*)?             u:object_r:system_data_file:s0
/data/gps(/.*)?         u:object_r:gps_data_file:s0
/data/dalvik-cache(/.*)? u:object_r:dalvikcache_data_file:s0
/data/anr(/.*)?         u:object_r:anr_data_file:s0
/data/app(/.*)?         u:object_r:apk_data_file:s0
/data/tombstones(/.*)?  u:object_r:tombstone_data_file:s0
/data/local(/.*)?       u:object_r:shell_data_file:s0
# Misc data
/data/misc/bluetoothd(/.*)?     u:object_r:bluetoothd_data_file:s0
/data/misc/bluetooth(/.*)?      u:object_r:bluetooth_data_file:s0
/data/misc/keystore(/.*)?       u:object_r:keystore_data_file:s0
/data/misc/vpn(/.*)?            u:object_r:vpn_data_file:s0
/data/misc/systemkeys(/.*)?     u:object_r:systemkeys_data_file:s0
/data/misc/wifi(/.*)?           u:object_r:wifi_data_file:s0
# App sandboxes
/data/data/.*           u:object_r:app_data_file:s0
#############################
# efs files
#
/efs(/.*)?              u:object_r:efs_file:s0
#############################
# Cache files
#
/cache(/.*)?            u:object_r:cache_file:s0
#############################
# sysfs files
#
/sys/qemu_trace/process_name    --      u:object_r:sysfs_writable:s0
      

[編輯]seapp_contexts

[編輯]policy.conf

policy.conf檔案是在系統編譯時期由m4預處理器将external/sepolicy目錄下相關的10個檔案整合而成的一個政策源檔案,這一源檔案集合了全部的安全政策規則。參與整合的10個檔案如下所示:

  • policy_capabilities

這一部分的内容列舉了目前policy可實作的能力,全部内容如下:

# Enable new networking controls.
policycap network_peer_controls;
# Enable open permission check.
policycap open_perms;
      
  • initial_sids

初始sid提供了一種特殊的預設标記行為,初始sid适用于兩種環境:在系統初始化政策還沒有載入前的時間,以及當客體的安全上下文無效或安全上下文丢失時使用,initial_sids檔案列出所有的初始sid,第二列的内容為這些sid的名字。 檔案内容如下所示:

# FLASK

#
# Define initial security identifiers
#

sid kernel
sid security
sid unlabeled
sid fs
sid file
sid file_labels
sid init
sid any_socket
sid port
sid netif
sid netmsg
sid node
sid igmp_packet
sid icmp_socket
sid tcp_socket
sid sysctl_modprobe
sid sysctl
sid sysctl_fs
sid sysctl_kernel
sid sysctl_net
sid sysctl_net_unix
sid sysctl_vm
sid sysctl_dev
sid kmod
sid policy
sid scmp_packet
sid devnull

# FLASK
      
  • ocontexts

ocontexts檔案中包含了對于初始sid的安全上下文配置設定, 以及針對不同的檔案系統使用四種不同文法對其安全上下文進行的标記,以下是該檔案的全部内容。

sid kernel u:r:kernel:s0
sid security u:object_r:kernel:s0
sid unlabeled u:object_r:unlabeled:s0
sid fs u:object_r:labeledfs:s0
sid file u:object_r:unlabeled:s0
sid file_labels u:object_r:unlabeled:s0
sid init u:object_r:unlabeled:s0
sid any_socket u:object_r:unlabeled:s0
sid port u:object_r:port:s0
sid netif u:object_r:netif:s0
sid netmsg u:object_r:unlabeled:s0
sid node u:object_r:node:s0
sid igmp_packet u:object_r:unlabeled:s0
sid icmp_socket u:object_r:unlabeled:s0
sid tcp_socket u:object_r:unlabeled:s0
sid sysctl_modprobe u:object_r:unlabeled:s0
sid sysctl u:object_r:proc:s0
sid sysctl_fs u:object_r:unlabeled:s0
sid sysctl_kernel u:object_r:unlabeled:s0
sid sysctl_net u:object_r:unlabeled:s0
sid sysctl_net_unix u:object_r:unlabeled:s0
sid sysctl_vm u:object_r:unlabeled:s0
sid sysctl_dev u:object_r:unlabeled:s0
sid kmod u:object_r:unlabeled:s0
sid policy u:object_r:unlabeled:s0
sid scmp_packet u:object_r:unlabeled:s0
sid devnull u:object_r:null_device:s0

# Label inodes via getxattr.
fs_use_xattr yaffs2 u:object_r:labeledfs:s0;
fs_use_xattr jffs2 u:object_r:labeledfs:s0;
fs_use_xattr ext2 u:object_r:labeledfs:s0;
fs_use_xattr ext3 u:object_r:labeledfs:s0;
fs_use_xattr ext4 u:object_r:labeledfs:s0;
fs_use_xattr xfs u:object_r:labeledfs:s0;
fs_use_xattr btrfs u:object_r:labeledfs:s0;

# Label inodes from task label.
fs_use_task pipefs u:object_r:pipefs:s0;
fs_use_task sockfs u:object_r:sockfs:s0;

# Label inodes from combination of task label and fs label.
# Define type_transition rules if you want per-domain types.
fs_use_trans devpts u:object_r:devpts:s0;
fs_use_trans tmpfs u:object_r:tmpfs:s0;
fs_use_trans devtmpfs u:object_r:device:s0;
fs_use_trans shm u:object_r:shm:s0;
fs_use_trans mqueue u:object_r:mqueue:s0;

# Label inodes with the fs label.
genfscon rootfs / u:object_r:rootfs:s0
# proc labeling can be further refined (longest matching prefix).
genfscon proc / u:object_r:proc:s0
# selinuxfs booleans can be individually labeled.
genfscon selinuxfs / u:object_r:selinuxfs:s0
genfscon cgroup / u:object_r:cgroup:s0
# sysfs labels can be set by userspace.
genfscon sysfs / u:object_r:sysfs:s0
genfscon inotifyfs / u:object_r:inotify:s0
genfscon vfat / u:object_r:sdcard:s0
genfscon debugfs / u:object_r:debugfs:s0
genfscon fuse / u:object_r:sdcard:s0

# portcon statements go here, e.g.
# portcon tcp 80 u:object_r:http_port:s0
      
  • security_classes

系統中檔案/資源的數量有數百萬甚至書千萬之多,但是類型固定。為了便于定義Object的通路權限,把系統中所有資源劃分為若幹類(class),可以給同一類資源的所有執行個體定義一組相同的通路權限。SEAndroid中共定義了83個class。

  • access_vectors
  • attributes

在SEAndroid中将一組具有共性的type集合歸類為一個屬性(attribute),SEAndroid中共有17中屬性。使用 attribute 可以在開發時有效地減少類似規則的數目,并且可以“一勞永逸”地針對某種 type 指定它對某類 attribute 的能力。在allow語句中既可以直接使用type也可以使用attribute進行規則聲明。

  • users
  • roles

SEAndroid中的roles檔案非常簡單,檔案内容如下: 它定義了角色r和關聯了角色r與屬性domain。

role r;
role r types domain;
      
  • te
  • mls
  • 轉自:http://en.wikipedia.org/wiki/User:Blueswhen

繼續閱讀