Fedora Core 3에서 apache 사용시에... selinux..

유니1944의 이미지

페도라 코어3에서 selinux를 활성화시킨 상태로

apache를 구동시키고 있습니다

가끔 어떤 php들은 파일업로드 때문에 혹은

설치 스크립트를 실행시키기 위해서

(제로보드나 태터툴즈... 기타 많은 php sciprt들이..)

특정디렉토리를 777 또는 707로 잡아야할 일이 생깁니다

이럴 경우 chmod로 777또는 707로 설정을 잡아줘도

selinux에 의해 제지를 당하는거 같습니다

(파일이 올라가지 않습니다) 어떻게 해야됩니까?

단순하게 selinux를 꺼버리기는 싫고..

chcon에서 file context를 어떻게 잘 정해주면

될꺼같기도 한대.. -_ -

atie의 이미지

베타 문서가 있습니다.

----
I paint objects as I think them, not as I see them.
atie's minipage

atie의 이미지

SELinux와 아파치 HTTP의 디렉토리 접근에 대한 이해를 돕기 위해서, 위의 베타 문서 중 "3. 간단한 설정" 편을 번역해 보았습니다.

길게 썼지만, ls -aZ로 확인하고, chcon으로 보안 식별자를 변경하고, 에러가 나면 /var/log/messages를 본다는 것이 요약입니다. (ls -aZ는 SELinux가 활성화되어 있어야만 동작합니다.)

Quote:

아주 간단한 하나의 웹사이트-html파일은 /var/www/html에 있지만, CGI 스크립트, 가상 호스팅, 그리고 다수의 웹 관리자는 없는 그런 웹 사이트-를 가지고 있다고 가정하자. CGI등의 고급 주제는 "6. 가상 호스팅 , CGI 스크립트 그리고 suEXEC"에서 다루어 질 것이다.

보안 목표는 아파치 HTTP는 오직 정적인 웹 내용물을 읽기만 할 뿐이며, 내용물을 작성하거나, 데이타베이스 소켓에 접속하거나, 사용자의 홈 디렉토리를 읽거나 하는 등의 다른 것들을 하지 않도록 확인하는 것이다.

기본 설정으로, 페도라 SELinux 정책은 아파치 HTTP가 CGI 스크립트를 수행하고, 사용자의 홈 디렉토리 내용을 직접 읽는 것을 허용한다. 이러한 조건을 비활성화가 하려면, system-config-securitylevel을 실행하고, SELinux 탭을 누른후, Apache 밑에, "Allow httpd CGI support" 그리고 "Allow httpd to read home directories"를 지정하지 않는다. 다른 방법으로는, 터미널에서 다음의 명령을 실행해도 된다.

setsebool httpd_enable_cgi false
setsebool httpd_enable_homedirs false

SELinux 와 boolean들에 대해서는 "8.1 간단한 사용자 맞춤 설정"에서 논의될 것이다.

"2. 시작하기"에서, 아파치 HTTP 프로세스는 httpd_t로 꼬리표가 붙었고 그래서 SELinux 제한이 적용되었음을 언급하였다. SELinux에서는, 시스템의 모든 대상, 즉 일반적인 파일, 네트워크 포트, 프로세스, 파일 디스크립터 등이 그것과 연관된 보안 식별을 가진다. 여기에 간단한 웹사이트의 파일에 대한 식별이 있다.

ls -aZ /var/www/
drwxr-xr-x  root     root     system_u:object_r:httpd_sys_content_t .
drwxr-xr-x  root     root     system_u:object_r:var_t          ..
drwxr-xr-x  root     root     system_u:object_r:httpd_sys_script_exec_t cgi-bin
drwxr-xr-x  root     root     system_u:object_r:httpd_sys_content_t error
drwxr-xr-x  root     root     system_u:object_r:httpd_sys_content_t html
drwxr-xr-x  root     root     system_u:object_r:httpd_sys_content_t icons
ls -aZ /var/www/error
drwxr-xr-x  root     root     system_u:object_r:httpd_sys_content_t .
drwxr-xr-x  root     root     system_u:object_r:httpd_sys_content_t ..
[...]
drwxr-xr-x  root     root     system_u:object_r:httpd_sys_content_t include
-rw-r--r--  root     root     system_u:object_r:httpd_sys_content_t noindex.php
-rw-r--r--  root     root     system_u:object_r:httpd_sys_content_t README

다시 한번, 식별의 세번째 요소인, httpd_sys_content_t 타입이 중요한 부분이다. 보안 정책은 httpd_t(아파치 HTTP)가 이 타입으로 된 파일과 디렉토리를 읽는 것을 허용하며, 이는 etc_t와 같은 다른 중요한 타입들도 마찬가지다.

지금 시점에서, http://localhost를 웹 브라우저를 띄어, 기본 페도라 코어나 레드햇 엔터프라이즈 리눅스 페이지가 보이는지 점검해보자. 다음으로, /var/www/html 안에 hello.html 같은 새로운 파일을 만드는 것을 시도해 보고 그것은 웹 브라우저에서 볼 수 있는지 확인해보자.

$ echo "Hello world" > /var/www/html/hello.html
$ ls -aZ /var/www/html/hello.html
-rw-r--r--  root  root  system_u:object_r:httpd_sys_content_t hello.html

/var/www/html에 새로운 파일이 만들어질 때, 기본으로 httpd_sys_content_t 타입이 상위(부모) 디렉토리로부터 상속됨을 유념하자. 이 방법으로 아파치 HTTP (httpd_t로 수행중인)가 새 파일을 읽을 수 있다.

system-config-securitylevel을 사용해 아파치 HTTP가 홈 디렉토리를 읽을 수 있도록 설정된 SELinux boolean을 커 놓았을 때, 이는 httpd_t domain이 user_home_dir_t를 읽을 수 있는 권한이 없도록 한다. user_home_dir_t는 사용자의 홈 디렉토리에 지정된 타입이다. 아파치 HTTP는 상위의 디렉토리(사용자 홈 디렉토리를 의미)를 사용할 수 없으므로, 아파치 HTTP는 사용자 홈 디렉토리에 있는 웹 내용물을 포함한 어떤 파일도 사용할 수 없게된다.

ls -aZ /home/walters
drwx------  walters  walters  root:object_r:user_home_dir_t    .
drwxr-xr-x  root     root     system_u:object_r:home_root_t    ..
-rw-r--r--  walters  walters  user_u:object_r:user_home_t      anaconda-ks.cfg
-rwxr-xr-x  walters  walters  user_u:object_r:user_home_t      anaconda.log
...

아마 갖게 될지도 모르는 한가지 문제는 mv와 cp 명령의 차이점이다. 새로운 내용물(about.html)을 웹 사이트에 추가한다고 가정하자. about.html을 홈 디렉토리에서 작성을 한 후,

sudo mv /home/walters/about.html /var/www/html

로 나중에 그것을 웹 디렉토리로 옮기면, 그 화일에 연관된 식별을 기본적으로 저장하게 되는 것이 문제다. cp는, 새로운 파일을 생성하므로, 부모(웹) 디렉토리의 식별을 상속한다. 예를 들어, 위에 언급된 mv의 결과는 이것이다.

ls -aZ /var/www/html/
drwxr-xr-x  root     root     system_u:object_r:httpd_sys_content_t .
drwxr-xr-x  root     root     system_u:object_r:httpd_sys_content_t ..
-rw-r--r--  root     root     system_u:object_r:httpd_sys_content_t index.php
-rw-r--r--  walters  walters  system_u:object_r:user_home_t about.html

아파치 HTTP를 수행하고, 웹 브라우저에서 about.html을 열려고 시도하면, "금지된(Forbidden)" 에러가 난다. 그리고, /var/log/messages를 보면 아래와 비슷한 메세지를 볼 것이다.

Oct 19 17:54:59 hostname kernel: audit(1098222899.827:0): avc:  \
denied  { getattr } for  pid=19029 exe=/usr/sbin/httpd \
path=/var/www/html/about.html dev=dm-0 ino=373900 \
scontext=root:system_r:httpd_t tcontext=user_u:object_r:user_home_t \
tclass=file

이 메세지가 말하는 것은 httpd_t가 user_home_t 타입을 가지는 파일을 접근할 수 없었다는 것이다. /home/walters의 목록에서 본 것처럼, user_home_t는 그 홈 디렉토리의 안의 많은 파일에 사용된다. 기본 보안 정책은 httpd_t가 user_home_t 타입에 대한 어떠한 접근도 허용하지 않는다.

파일의 식별을 바꾸어 아파치 HTTP가 그 파일을 읽도록 하려면, chcon을 사용하여 할 수 있다.

chcon -t httpd_sys_content_t /var/www/html/about.html

화일의 보안 식별자가 제대로 되었는지 확인하자.

ls -aZ /var/www/html/
drwxr-xr-x  root     root     system_u:object_r:httpd_sys_content_t .
drwxr-xr-x  root     root     system_u:object_r:httpd_sys_content_t ..
-rw-r--r--  root     root     system_u:object_r:httpd_sys_content_t index.php
-rw-r--r--  walters  walters  system_u:object_r:httpd_sys_content_t about.html

chcon은 유닉스 명령 chmod와 비슷하다. 예를 들어, 한 디렉토리와 그 안의 모든 파일들의 타입을 재귀적(디렉토리 하위의 모든 것에 대해)으로 변경한다면, 다음과 같이 한다. -R 스위치는 재귀적으로 작동하고, -h 선택은 symlinks 따르지 말도록 (거의 모든 경우에 원해지는) 하는 것을 뜻한다.

chcon -R -h -t httpd_sys_content_t /path/to/directory

파일의 꼬리표를 변경할 수 있는 다른 명령은 restorecon이다. 이 명령은 파일의 기본 꼬리표로 복귀하고자 할 때 유용하다. 예를 들어,

restorecon -v -R /var/www/

수행해서 /var/www/ 디렉토리의 모든 파일 꼬리표를 재설정할 수 있다. 내부적으로, restorecon은 /etc/selinux/targeted/contexts/files/file_contexts 파일을 읽으며, 그 파일은 파일 경로를 보안 식별자에 대응한 정규식의 집합을 가지고 있다.

----
I paint objects as I think them, not as I see them.
atie's minipage

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.