개요

cp와 mv 명령어는 SELinux 에서 security context 관련 동작이 다르기 때문에 예상하지 않은 오류를 만날수 있다. 두 명령어의 차이점에 대해서 정리해 본다

 

mv

mv 명령어는 original file 의 security context 를 유지한다. 그래서 mv 를 사용할 경우 의도하지 않게 SELinux 의 access denied 메시지를 접할수 있다. 예로 다음과 같이 외부에서

미리 컴파일 된 mod_jk.so 를 받은 후에 작업 결과를 비교해 보자. 

 

  1. root 로 login
  2. wget "https://lesstif.com/download/attachments/12943367/mod_jk_1.2.37-x86_64.tar.gz?version=1&modificationDate=1382662783654&api=v2"
  3. tar zxvf mod_jk_*.tar.gz - 현재 폴더에 mod_jk.so 가 압축이 해제 된다.
  4. mod_jk.so 의 context 조회하면 아래와 같이 admin_homt_t 이다

    root@localhost:> ls -lZ mod_jk.so 
    -rwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 mod_jk.so
    BASH
  5. mv mod_jk.so /etc/httpd/modules
  6. mod_jk.so 의 context 조회

    root@localhost:> ls -lZ mod_jk.so 
    -rwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 mod_jk.so
    BASH

 

위와 같이 mv 를 사용하면 현재 file 의 context 가 유지되어 의도하지 않게 access denied 가 발생할 수 있다.

  1. modules 밑에 있으면 httpd_modules_t 를 가져야 하는데 admin_home_t 라 httpd 가 읽을수 없다.
  2. 위 문제를 해결하려면 restorecon 으로 default context 를 설정하거나 chcon 으로 해당 경로에 맞는 context 를 설정한다.

    root@local:> restorecon /etc/httpd/modules/mod_jk.so 
    root@local:>ls -lZ /etc/httpd/modules/mod_jk.so 
      -rwxr-xr-x. root root system_u:object_r:httpd_modules_t:s0 /etc/httpd/modules/mod_jk.so
    BASH
  3. file context 를 조회하려면 semanage 명령어를 사용한다.

     root@local:> semanage fcontext -l|grep etc\/httpd\/modules
    /etc/httpd/modules                                 all files          system_u:object_r:httpd_modules_t:s0 
    BASH

 

cp

cp 는 복사되는 directory 의 default security context 를 따른다. 위의 mod_jk.so 를 cp 로 /etc/httpd/modules 에 복사하는 경우 

/etc/httpd/modules 의 default security context 인 httpd_modules_t 가 설정되므로 사용에 문제가 없다.

mv와 다르게 cp 의 option 에는 복사되는 파일의 security context 를 지정할 수 있는 -Z 옵션이 있다.

~]$ touch file1
~]$ cp -Z system_u:object_r:samba_share_t:s0 file1 file2
~]$ ls -Z file1 file2
  -rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1 -rw-rw-r-- user1 group1 system_u:object_r:samba_share_t:s0 file2
~]$ rm file1 file2
BASH

 

Ref