Pocketstudio.jp :: Logbook :: Linux :: Fedora Core 3のメモ
 Contributed at:   2004/11/18 (Thu) 11:16

■ /linux/FedoraCore3/ FedoraCore3付属のapache(SELinux)でSuEXECを有効にする方法


 SELinux が有効な状態で apache の SuExEC 機能を有効にする方法です。/var/www/html の DocumentRoot ではなく、一般ユーザが各ユーザ権限で CGI を実行させる方法です。(※selinux-policy-targeted-1.17.30-2.19 が前提です。もしかしたら、リリースが変わったら初期状態の記述が変わっている可能性があります。また、セキュリティ的に緩くなっているとも考えられるので、作業は自己責任でお願いします……)

 まず、selinux-policy-targeted-sources パッケージが調整の為に必要です。
 また、あらかじめ一般ユーザが public_html などでウェブを公開できるように httpd.conf の調整も必要です。

 SuEXEC を有効にするための SELinux 側での編集ファイルは2つです。

  • 『 /etc/selinux/targeted/src/policy/file_contexts/types.fc 』では『 HOME 』の『 ROLE_home_dir_t 』となっているタイプ(という印)を『 httpd_user_content_t 』に書き換えます。CGI が “/home/ユーザ名” 配下(つまり pubilc_html からは見えない所にもファイルがあっても読めるようにするため、と解釈?)を操作できるようにします。
     『HOME_DIR/.+ 』についても同様に変更しても問題ないと思うが、現状で判断つかないので、そのまま残すことにする。
    HOME_DIR               -d      system_u:object_r:httpd_user_content_t
    
    # diff -u types.fc.2004-1117 types.fc --- types.fc.2004-1117 2004-11-02 06:36:22.000000000 +0900 +++ types.fc 2004-11-17 23:59:06.557272440 +0900 @@ -51,7 +51,9 @@ # ROLE expands to each user's role when role != user_r, and to "user" otherwise. # HOME_ROOT -d system_u:object_r:home_root_t -HOME_DIR -d system_u:object_r:ROLE_home_dir_t +#HOME_DIR -d system_u:object_r:httpd_suexec_t +HOME_DIR -d system_u:object_r:httpd_user_content_t +#HOME_DIR -d system_u:object_r:ROLE_home_dir_t HOME_DIR/.+ system_u:object_r:ROLE_home_t /root/.default_contexts -- system_u:object_r:default_context_t
  • 『/etc/selinux/targeted/src/policy/file_contexts/program/apache.fc』では、一般ユーザの公開ディレクトリ(httpd.conf の UserDir で指定されている所。一般的には public_html)に対して【 httpd_sys_script_rw_t 】をラベルとする
    HOME_DIR/((www)|(web)|(public_html))(/.+)? system_u:object_r:httpd_sys_script_rw_t
    
     それから次のコマンドを実行して設定を反映。
    cd /etc/selinux/targeted/src/policy
    make clean
    make reload
    mkee relabel
    

     macros/program/apache_macros.te を見ると httpd_suexec_t というラベルがあるようですが、どうもうまく機能していない様子でした。私の使い方、ポリシーの見方を間違っているかも知れませんが、とりあえず上記の方法で SuEXEC として利用が出来ますので、とりあえず目標は達成したという事にします。。。

     ユーザを新規追加した後など、ラベルが適切に与えられてないと思われる時は、/etc/selinux/targeted/src/policy で make relabel を実行してラベルの再定義をする必要があります(自分はここでハマりました)。
     ラベルを見るには ls のオプションで『 —context 』をつけるか『 -Z 』です。
     実行例:

    # ls -la --context /home/zem2/public_html/
    drwxr-xr-x  zem2     zem2     system_u:object_r:httpd_sys_script_rw_t .
    drwx-----x  zem2     zem2     system_u:object_r:httpd_user_content_t ..
    -rw-r--r--  zem2     zem2     root:object_r:httpd_sys_script_rw_t hello.txt
    -rwx------  zem2     zem2     system_u:object_r:httpd_sys_script_rw_t test.cgi
    
     ”zem2” というアカウントによって作成された “hello.txt” の所有者が “zem2” である事から、SuEXEC の機能によって、test.cgi は “zem2” ユーザ権限で実行されていることが分かります。test.cgi のパーミッションも 700(rwx———)なので、その所有者(この場合は zem2)しか実行できなくなっている=正常に機能しているとわかります。

     また、SuEXEC 自身のエラーによって実行できない場合もあるので、その場合は【 /var/log/httpd/suexec_log 】のチェックも必要になってきます。以下は自爆例。

    [2004-11-17 17:02:20]: uid: (500/zem) gid: (500/500) cmd: test.cgi
    [2004-11-17 17:02:20]: directory is writable by others: (/home/zem/public_html)
    
     /home/zem/public_html のパーミッションが 757(rwxr-xrwx)だったため Internal Server Error。パーミッションは 755(rwxr-xr-x) に変更。他にも、CGI の所有者が /home/アカウント名 の”アカウント名”と一致している必要もあるので、動かない場合はログもあわせて要確認。

     参考までに、テストで使用した test.cgi は以下の通り。

    #!/usr/bin/perl
    
    print "Content-type:text/html\n\n";
    print "<H1>CGI test</h1>";
    open (OUT,">"."hello.txt") or print "error:$!<p>";
    print OUT "hello!\n";
    close (OUT);
    print "<hr>";
    
    open(IN,"<"."./hello.txt") or print "ERR:$!<p>";
    while (<IN>) {
            print;
    }
    
     画面上に【 CGI test 】と表示して、”hello!” を同一ディレクトリ内の hello.txt に書き出す。そして、書き出した内容を画面に表示しています。
     SuEXEC が有効であれば、そのファイル(test.cgi)の所有者の実行権限で新規にファイルが作成されるので、hello.txt の所有者が apache ではなく test.cgi と同一であれば、正常に SuEXEC が機能していると分かります(/var/log/httpd/suexec_log からも正しく実行されているかは参照できます)
    [2004/11/18 追記]

    なお、 /etc/selinux/targeted/src/policy/domains/program/apache.te 内の以下の行も、false から true に変更している。効果の程は FAQ(しかもベータ版ですか) を見てもいまいち不明。とりあえず、現状は変更して使っています。

    bool httpd_unified true;
    
    # Allow httpd cgi support
    bool httpd_enable_cgi true;
    
    # Allow httpd to read home directories
    bool httpd_enable_homedirs true;
    
    # Run SSI execs in system CGI script domain.
    bool httpd_ssi_exec true;
    
    
    参考URL
    Understanding and Customizing the Apache HTTP SELinux Policy (Beta Document) … ベータ版らしい、ということは、Fedora Core3 でも apache に対するポリシー設定はまだ確定していないようですね……。

    [分類 /linux/FedoraCore3 ] 固定リンク

  •  
     書込記録 - Calendar -
    11月(November)
    Sun Mon Tue Wed Thu Fri Sat
     
    18
           
    2004
    11月
     お品書き[ BLOG ] - Weblog -
     過去ログ書庫 - Archives -

      "ポケットスタジオ" Copyright ©1997-2004 Pocketstudio.jp. Some rights reserved.