티스토리 뷰

os/Linux

how to generate custom policy

Jacob_baek 2018. 11. 9. 10:15


selinux policy module을 사용하는 방법으로의 접근을 추천한다.


selinux policy module 을 제작하는 방법은 두가지가 있다.

M4+Compiliation 방식과 CIL 방식이다.

CIL(Common Intermediate Language)의 경우 Red Hat Enterprise Linux 7.3부터 정식 지원되고 있다.

(https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/7.3_release_notes/new_features_security)

아래 방식은 M4+Compiliation 이다.


custom policy 를 제작하는 방법


sepolicy generate tool을 이용하여 임의의 policy를 생성할 수 있다.

[root@rhel7 ~]# sepolicy generate -n selinuxtest --init `which hddtemp`
Loaded plugins: auto-update-debuginfo, product-id, subscription-manager
Created the following files:
/root/tmp/selinuxtest.te # Type Enforcement file
/root/tmp/selinuxtest.if # Interface file
/root/tmp/selinuxtest.fc # File Contexts file
/root/tmp/selinuxtest_selinux.spec # Spec file
/root/tmp/selinuxtest.sh # Setup Script


실제 생성된 파일들은 다음과 같다.

[root@rhel7 ~]# ll
total 20
-rw-r--r--. 1 root root  174 Nov  9 09:52 selinuxtest.fc
-rw-r--r--. 1 root root 2161 Nov  9 09:52 selinuxtest.if
-rw-r--r--. 1 root root 1804 Nov  9 09:52 selinuxtest_selinux.spec
-rwxr-x---. 1 root root 1598 Nov  9 09:52 selinuxtest.sh
-rw-r--r--. 1 root root  709 Nov  9 09:52 selinuxtest.te


 File Extension

 Description

 fc

 file contexts file (접근제어할 file에 대한 정의가 되어있다.)

 if

 interface file

 spec

 setup script에 의해 build 수행시 사용되는 spec 파일

 sh

 setup script (실제 pp, rpm 파일들을 만들어 내고 semodule -e로 활성화한다.)

 te

 Type Enforcement file


실제 .sh 파일을 수행하면 아래와 같은 make, manpage 추가, restorecon등의 과정이 수행된다.

make -f /usr/share/selinux/devel/Makefile selinuxtest.pp || exit
/usr/sbin/semodule -i selinuxtest.pp
sepolicy manpage -p . -d selinuxtest_t

/sbin/restorecon -F -R -v /usr/sbin/hddtemp
/sbin/restorecon -F -R -v /usr/lib/systemd/system/hddtemp.service
rpmbuild ~~~~~~ selinuxtest_selinux.spec


.sh를 통해 module build를 수행한다.

[root@rhel7 /]# selinuxtest.sh


.sh를 통해 module build가 완료되면 .pp파일이 생성된다.

[root@rhel7 ~]# ll
total 148
-rw-r--r--. 1 root root   174 Nov  9 09:52 selinuxtest.fc
-rw-r--r--. 1 root root  2161 Nov  9 09:52 selinuxtest.if
-rw-r--r--. 1 root root 98499 Nov  9 09:54 selinuxtest.pp
-rw-r--r--. 1 root root 20301 Nov  9 09:54 selinuxtest_selinux-1.0-1.el7.src.rpm
-rw-r--r--. 1 root root  7023 Nov  9 09:54 selinuxtest_selinux.8
-rw-r--r--. 1 root root  1804 Nov  9 09:52 selinuxtest_selinux.spec
-rwxr-x---. 1 root root  1598 Nov  9 09:52 selinuxtest.sh
-rw-r--r--. 1 root root   709 Nov  9 09:52 selinuxtest.te
drwxr-xr-x. 2 root root    52 Nov  9 09:54 noarch
drwxr-xr-x. 2 root root    72 Nov  9 09:54 tmp


실제 확인해보면 hddtemp에 대한 type이 변경되었음을 확인할 수 있다.

[root@rhel7 /]# ls -Z `which hddtemp`
-rwxr-xr-x. root root system_u:object_r:selinuxtest_exec_t:s0 /usr/sbin/hddtemp



기존 default policy로 복구 하는 방법


현재 enable 한 custom policy module을 disable 하고 기존에 사용되던 policy module을 enable 하는 과정이다.


추가했던 custom policy module 및 해당 module에 의해 selinuxtest_exec_t로 type이 바껴있는 파일의 속성을 확인한다.

[root@rhel7 ~]# ls -laZ /usr/sbin/hddtemp
-rwxr-xr-x. root root system_u:object_r:selinuxtest_exec_t:s0 /usr/sbin/hddtemp


[root@rhel7 ~]# ps -eZ | grep hddtemp
system_u:system_r:unconfined_service_t:s0 390 ? 00:00:00 hddtemp


[root@rhel7 ~]# semodule -l | grep myhdd
myhddtemp    1.0.0


semodule을 이용해서 custom policy module을 disable 하고 기존에 사용하던 모듈을 enable한다.

[root@rhel7 ~]# semodule -d selinuxtest
[root@rhel7 ~]# semodule -e hddtemp


마지막으로 해당 파일을 확인해보면 변경된 type을 확인할 수 있다.

[root@rhel7 ~]# ls -laZ /usr/sbin/hddtemp
-rwxr-xr-x. root root system_u:object_r:unlabeled_t:s0 /usr/sbin/hddtemp


CIL(Common Intermediate Language)를 사용하는 방법은 다음 wiki page를 참고한다.

- https://github.com/SELinuxProject/cil/wiki#language-reference


참고사이트
- https://mgrepl.fedorapeople.org/PolicyCourse/writingSELinuxpolicy_MUNI.pdf

- https://www.redhat.com/files/summit/session-assets/2017/L100049-RH-Summit-2017-Practical-SELinux.pdf

- http://www.billauer.co.il/selinux-policy-module-howto.html

- https://wiki.gentoo.org/wiki/SELinux/Tutorials/Creating_your_own_policy_module_file

- https://github.com/SELinuxProject/cil

'os > Linux' 카테고리의 다른 글

do-release-upgrade doesn't work  (0) 2019.12.29
How to use touchpad gesture at the Ubuntu  (0) 2019.12.25
PTP  (0) 2018.08.21
dnsmasq  (0) 2018.03.02
changing for size of centos root  (0) 2016.10.28
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함