selinux contexts 安全上下文的临时更改


Androiod 安全上下文的临时更改,命令:

chcon       安全上下文  文件
chcon  -R   安全上下文   目录    (-R是递归更改)

 

举例:

原始信息:

xxx:/system/app # ls -lZ
total 199
...... drwxr-xr-x 4 root root u:object_r:system_file:s0 4096 2009-01-01 08:00 Bluetooth drwxr-xr-x 1 root root u:object_r:system_file:s0 3488 2021-07-06 04:11 Stk drwxr-xr-x 3 root root u:object_r:system_file:s0 4096 2009-01-01 08:00 Traceur .....

运行命令:chcon -R u:object_r:vendor_app_file:s0 Stk/

运行后结果:

130|xxx:/system/app # chcon -R u:object_r:vendor_app_file:s0 Stk/
E6A:/system/app # ls -lZ
total 199......
drwxr-xr-x 3 root root u:object_r:system_file:s0     4096 2009-01-01 08:00 SoterService
drwxr-xr-x 1 root root u:object_r:vendor_app_file:s0 3488 2021-07-06 04:11 Stk
drwxr-xr-x 3 root root u:object_r:system_file:s0     4096 2009-01-01 08:00 Traceur
......

130|xxx:/system/app/Stk # ls -lZ
total 275
-rw-r--r-- 1 root root u:object_r:vendor_app_file:s0 277735 2009-01-01 08:00 Stk.apk drwxr-xr-x 1 root root u:object_r:vendor_app_file:s0 3488 2021-07-06 04:11 oat

恢复命令: restorecon -R Stk/   若不生效则重启手机

 

 

 

 

 

https://www.jianshu.com/p/d10a132df49a

SELinux增强了Linux系统的安全,但是对于初学者来说很容易造成各种各样的错误,经常被搞的莫名其妙不知所措,最后只能祭出大招把SELinux一关了之。笔者也是初学者,也没有搞清楚其中的道理,只是了解点皮毛而已。本文只从chcon命令的使用结合几个例子简单回顾一下,更加深入的知识有待继续学习。

使用selinux contexts

SELinux增强了Linux系统的安全,但是对于初学者来说很容易造成各种各样的错误,经常被搞的莫名其妙不知所措,最后只能祭出大招把SELinux一关了之。笔者也是初学者,也没有搞清楚其中的道理,只是了解点皮毛而已。本文只从chcon命令的使用结合几个例子简单回顾一下,更加深入的知识有待继续学习。



chcon man page

NAME chcon - change security context SYNOPSIS chcon [OPTION]... CONTEXT FILE... chcon [OPTION]... --reference=RFILE FILE... DESCRIPTION Change the security context of each FILE to CONTEXT. -c, --changes like verbose but report only when a change is made -h, --no-dereference affect symbolic links instead of any referenced file (available only on systems with lchown system call) -f, --silent, --quiet suppress most error messages -l, --range set range RANGE in the target security context --reference=RFILE use RFILE’s context instead of using a CONTEXT value -R, --recursive change files and directories recursively  -r, --role set role ROLE in the target security context -t, --type set type TYPE in the target security context -u, --user set user USER in the target security context -v, --verbose output a diagnostic for every file processed --help display this help and exit --version output version information and exit REPORTING BUGS Report bugs to <email@host.com>. SEE ALSO The full documentation for chcon is maintained as a Texinfo manual. If the info and chcon programs are properly installed at your site, the command info chcon should give you access to the complete manual. 

SELinux 上下文 简介

在运行这SELinux的系统上,所有的进程和文件都以标记着一种安全相关的信息,这种信息就是SELinux Context(上下文)。可以使用下面的命令查看文件的上下文:

ls -Z file1
-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1 

在这个例子里面,SELinux提供了一个user (unconfined_u), 一个role (object_r), 一个type (user_home_t), 以及一个level (s0)。这个信息用来进行控制访问。SELinux实现了强制访问控制(MAC)的安全模式。在标准的Linux环境中开启了自由访问控制(Discretionary Access Control,DAC)后,这一机制就会在Linux内核中对所有操作的安全性进行检查。在DAC体系中,访问通过Linux用户ID和组ID进行控制,SELinux策略规则会在DAC规则之后进行检查。也就是说,如果DAC规则已经拒绝访问了,那么SELinux策略规则就不使用了。

有许多命令可以管理文件的SELnux上下文,例如chcon,semanage fcontext和restorecon。

chcon临时的改变

chcon命令可以临时的改变文件的上下文,也就是说如果文件系统relabel一下或者执行一下restorecon命令,上下文的改变就实效了。SELinux策略控制着用户能否修改一个文件的上下文。在适用chcon命令的时候用户要指定所有的或者部分的要改变的SELinux上下文。

改变一个文件或者文件夹的类型

下面这段只说明一下如何改变SELinux中的类型。这个我们假设file1是一个文件。当然,如果file1是一个文件夹的话也是适用的。

查看文件的上下文
~]# ls -Z file1
-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1 改变文件的上下文 ~]# chcon -t samba_share_t file1 ~]# ls -Z file1 -rw-rw-r-- user1 group1 unconfined_u:object_r:samba_share_t:s0 file1 恢复文件的上下文 ~]# restorecon -v file1 restorecon reset file1 context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:user_home_t:s0 

在使用RHEL6中默认的SELinux策略的时候,restore命令会读取/etc/selinux/targeted/contexts/files/文件夹中的文件,来查看文件应该拥有哪个SELinux上下文。

改变一个文件夹及其内容的类型

下面这个例子说明了如何把一个文件夹及其内容的类型改成Apache HTTP服务器使用的类型。当你想让Apache HTTP服务器使用一个不同的root文件夹的时候(默认的是/var/www/html/),这个配置就有派上用场了。

新建文件夹/web,并在其中新建三个文件file{1,2,3}: ~]# mkdir /web ~]# cd /web ~]# touch file{1,2,3} /web/文件夹和里面的文件都具有default_t类型的标签: ~]# ls -dZ /web drwxr-xr-x root root unconfined_u:object_r:default_t:s0 /web ~]# ls -lZ /web -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file1 -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file2 -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file3 使用chcon命令把/web/文件夹及其内容的类型改成httpd_sys_content_t: ~]# chcon -R -t httpd_sys_content_t /web/ 使用restorecon命令来恢复默认的上下文: ~]# restorecon -R -v /web/ restorecon reset /web context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file2 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file3 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file1 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 

参考其他的文件来创建上下文

使用参数--reference来指定其他的文件
chcon -R --reference=RFILE FILE

参考文献

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Security-Enhanced_Linux/sect-Security-Enhanced_Linux-Working_with_SELinux-SELinux_Contexts_Labeling_Files.html
http://wiki.centos.org/HowTos/SELinux
http://www.searchsv.com.cn/showcontent_44629.htm




 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM