Introduction to SELINUX Rule Analysis and syntax in android
1. SELINUX is a security mechanism on android. It is a security enhancement system designed for linux by the US National Security Agency and some companies.
We can configure SELINUX policies to customize some permissions for our mobile phone. For example, we can completely make the root user have no permissions as the user.
2. There are two types in android: files and processes.
For these two types, let's take a look at their differences.
On android, after adb shell enters the mobile phone, ps-Z can view the selinux permissions of the current process.
Example:
LABEL USER PID PPID NAMEu:r:init:s0 root 1 0 /initu:r:kernel:s0 root 2 0 kthreadd...u:r:kernel:s0 root 258 2 irq/322-HPH_R Ou:r:logd:s0 logd 259 1 /system/bin/logdu:r:healthd:s0 root 260 1 /sbin/healthdu:r:lmkd:s0 root 261 1 /system/bin/lmkdu:r:servicemanager:s0 system 262 1 /system/bin/servicemanageru:r:vold:s0 root 263 1 /system/bin/voldu:r:surfaceflinger:s0 system 264 1 /system/bin/surfaceflingeru:r:tctd:s0 root 265 1 /system/bin/tctdu:r:rfs_access:s0 system 268 1 /system/bin/rfs_accessu:r:tee:s0 system 271 1 /system/bin/qseecomdu:r:kernel:s0 root 280 2 kworker/3:1Hu:r:kernel:s0 root 290 2 kauditdu:r:rmt_storage:s0 nobody 291 1 /system/bin/rmt_storageu:r:shell:s0 shell 292 1 /system/bin/shu:r:netd:s0 root 295 1 /system/bin/netdu:r:debuggerd:s0 root 296 1 /system/bin/debuggerdu:r:tee:s0 system 297 271 /system/bin/qseecomd
In this example, we can analyze it.
In android, only one user is defined as u. in addition, if it is a process, it will be uniformly defined as r. If it is a file, it will be defined as object_r. the third is the process type. In andorid, more than 100 types are defined. according to my understanding, this is the type of the Process>. The fourth is s0, which is a security level. However, you have not reached the configuration point yet.
In addition, if the file wants to view the relevant SELINUX permissions, You need to execute ls-Z
drwxr-x--x root sdcard_r u:object_r:rootfs:s0 storagedrwx--x--x root root u:object_r:tmpfs:s0 synthesisdr-xr-xr-x root root u:object_r:sysfs:s0 sysdrwxr-xr-x root root u:object_r:system_file:s0 systemdrwxrwxr-x system tctpersist u:object_r:tct_persist_file:s0 tctpersistlrwxrwxrwx root root u:object_r:rootfs:s0 tombstones -> /data/tombstones-rw-r--r-- root root u:object_r:rootfs:s0 ueventd.qcom.rc-rw-r--r-- root root u:object_r:rootfs:s0 ueventd.rc
In this example, combined with the above analysis, we know that object_r is the representative file, u is the only user of android, and rootfs is the type corresponding to this file, s0 is a security level restriction.
3. How to configure selinux
First, follow the Official Google Documents:
The Linux kernel must support selinux first, and The selinux configuration file of android, that is, the content in extern/sepolicy.
Then modify BoardConfig. mk.
Google's nexus sepolicy is supported
Device/lge/mako/sepolicy
First, the folder containing the sepolicy customized by the vendor will be included:
BOARD_SEPOLICY_DIRS
Then add the rule to the sepolicy:
BOARD_SEPOLICY_DIRS
In this case, the compiled image actually has the selinux function.
In fact, if there is no vendor customization, it will also be compiled to external/sepolicy. In this case, all default sepolicies of andriod are used (It defines the domains and types for the AOSP services and apps common to all devices .)
After understanding this, we can see that many vendors also have their own configuration rules under device/***/sepolicy.
4. selinux configuration rules:
First, you must understand the sepolicy structure:
A. App process-> mac_permissions.xml
B. App data file-> seapp_contexts
C. System File-> file_contexts
D. System Properties-> property_contexts
In te files, we generally encounter the following syntax:
Rule_name source_type target_type: class perm_set
Interpretation: Set a rule_name rule for source_type. The rule is to perform the perm_set operation on the class of target_type.
Then there are some special configuration files:
A. external/sepolicy/attributes-> all defined attributes are in this file
B. external/sepolicy/access_vectors-> corresponds to the commands that every class can be executed.
C. external/sepolicy/roles-> only one role is defined in Android. The role name is r, which associates r with attribute domain.
D. external/sepolicy/users-> actually associates the user with the roles and sets the user security level. s0 is the lowest level, which is the default level, and mls_systemHigh is the highest level.
E. external/sepolicy/security_classes-> refers to the class in the above command. In my opinion, the content of this class refers to the module that may be used by the program or system during android running.
F. external/sepolicy/te_macros-> the macros defined by the system are all in the te_macros file.
G. external/sepolicy/***. te-> some configuration files, including various running rules
In addition, selinux has two working modes:
"Permissive": all operations are permitted (I .e., there is no MAC), but logs are recorded if the permission is violated.
"Enforcing": all operations are permitted.
Finally, the type command is as follows:
Type type_id [alias alias_id,] [attribute_id] # associate type_id (alias: alias) to attribute. in this way, attribute can be used to manage different types that contain the same attributes.
The format of the class command is:
Class class_name [inherits common_name] {permission_name ...}
Inherits indicates that the permission defined by common is inherited, and then the permission_name permission is implemented by the user.
Four naming rules are common in te files:
Allow: grant a permission.
Allowaudit: audit indicates to record an operation. By default, SELinux only records the operations that failed the permission check. Allowaudit records the successful permission check operations. Note that allowaudit only allows records. It has nothing to do with granting permissions. Only permission granted
Use the allow statement.
Dontaudit: do not record operations that fail to check permissions.
Neverallow: as mentioned earlier, it is used to check whether there are any allow statements in the security policy file that violate this rule. As shown in Example 5:
Example:
type init, domain;
Associate init with the domain to set the domain to an init attribute.
allow init unlabeled:filesystem mount;
Allows the init type to mount unlabeled filesystem.
allow init fotad:unix_stream_socket { bind create };Allow bind and create operations on unix_stream_socket of the fotad type by the init type
allow appdomain anr_data_file:dir search;allow appdomain anr_data_file:file { open append };First, appdomain is a macro defined in te_macros. Many app Rules use commands similar to app_domain (shell) to add it
1. Allow the app to search for directories of the anr_data_file type
2. Allow the app to open and add an anr_data_file file. In fact, this defines the permission limit for the app to write data to/data/anr/When anr occurs.
neverallow { appdomain -unconfineddomain } kmem_device:chr_file { read write };
The app (except the app with the unconfineddomain attribute) is absolutely not allowed to read and write kmem_device character devices.
neverallow { appdomain -unconfineddomain } self:capability2 *;Apps other than unconfineddomain are absolutely not allowed to perform any operations on capability2 of the self type.
type httpd_user_content_t, file_type, httpdcontent;
Declare An httpd_user_content_t type with file_type and httpdcontent attributes
type httpd_user_content_t; typeattribute httpd_user_content_t file_type, httpdcontent;
Declare An httpd_user_content_t type
Define httpd_user_content_t attributes with file_type and httpdcontent
allow appdomain self:rawip_socket create_socket_perms;
All data types can be set as attributes.
In this example, we allow all content with app attributes to perform the create operation on rawip_socket of the self attribute.
allow {user_t domain} {bin_t file_type sbin_t}:file execute ;Allow user_t and domain attributes to perform executable operations on bin_t, file_type, sbin_t type files.
allow user_t user_t:process signal; allow user_t self:process signal;
The two statements are actually consistent. In fact, self indicates that the type of the target is consistent with that of the initiator.
Therefore, a type or attribute cannot be declared as self.
allow user_t bin_t:file ~{ write setattr ioctl };Allow user_t to perform operations related to write setattr ioctl on bin_t files
type_transition system wifi_data_file:sock_file system_wpa_socket;
When a system-type sock_file access is performed for wifi_data_file, the type is switched to system_wpa_socket by default.
If the following statement is to be executed successfully
Type_transition init_t apache_exec_t: process apache_t;
Declare at least the following three rules:
Allow init_t apache_exec_t: file execute;
Allow init_t apache_t: process transition;
Allow apache_t apache_exec_t: file entrypoint;
The syntax rules of type_transition and type_change are the same. The effects of type_change rules are not effective in the kernel, but dependent on user space applications, such as login or sshd.