LoginSignup
1
0

More than 3 years have passed since last update.

SELinuxを有効にした状態でperl(CGI)からsqliteファイルを読み書きする

Last updated at Posted at 2020-07-05

はじめに

SELinuxはずっと無効にしてつかっていましたが、試しに有効にしてみたらCGIからSQLiteのデータベースに書き込めなくなったので、書き込める設定を試行錯誤しました。

前提

環境はCentOS 6(古...)
CGIのスクリプトとSQLiteのデータベースファイルは同じディレクトリにあります。
ディレクトリのパーミッションは

drwx---rwx. 2 root   root   4096 Jul  2 11:35 data

ファイルのパーミッションは

-r-x---r-x. 1 root root  632 Jun 30 04:43 test.cgi
-rw----rw-. 1 root root 2048 Jul  2 11:35 test.db

こんな感じで、test.cgiがCGIスクリプト test.dbがデータベースファイルになります。

ログ

SELinuxでアクセス拒否されると/var/log/audit/audit.log
にメッセージが出力されます。
うまくいってないとこんな感じ

type=AVC msg=audit(1593723989.189:261): avc:  denied  { write } for  pid=1870 comm="test.cgi" name="test.db" dev=dm-0 ino=7149 scontext=unconfined_u:system_r:httpd_sys_script_t:s0 tcontext=unconfined_u:object_r:httpd_sys_script_exec_t:s0 tclass=file
type=SYSCALL msg=audit(1593723989.189:261): arch=c000003e syscall=2 success=no exit=-13 a0=1f51280 a1=42 a2=1a4 a3=7ffe0545f870 items=0 ppid=1365 pid=1870 auid=0 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1 comm="test.cgi" exe="/usr/bin/perl" subj=unconfined_u:system_r:httpd_sys_script_t:s0 key=(null)

とりあえずはtype=AVCの行を見れば良いそうです。
denied{write}とあるので、書き込みが拒否されているのが想像されます。
comm="test.cgi" name="test.db" は test.cgiからtest.dbへのアクセスを示してるのかな・・・

着目したのが
scontext=unconfined_u:system_r:httpd_sys_script_t:s0 tcontext=unconfined_u:object_r:httpd_sys_script_exec_t:s0
です。
scontextのsはsourceのs
tcontextのtはtargetのt
・・・なのだろう。

これは、ファイルのSELinux上での属性(ラベルといいます)を示しているとのことです。
ls -lZコマンドで見ることができます。


[root@localhost data]# ls -lZ
-r-x---r-x. root root unconfined_u:object_r:httpd_sys_script_exec_t:s0 test.cgi
-rw----rw-. root root unconfined_u:object_r:httpd_sys_script_exec_t:s0 test.db

tcontextのとおりですね。
何に変えるべきなのだろう

参考にしたのはこのページ
https://fedoraproject.org/wiki/SELinux/apache

読み書きだと
httpd_sys_script_rw_t
っぽいなあ。
変更はこんなコマンドでできるようです。

chcon -t httpd_sys_script_rw_t test.db

変更を確認

[root@localhost data]# ls -Z
-r-x---r-x. root root unconfined_u:object_r:httpd_sys_script_exec_t:s0 test.cgi
-rw----rw-. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 test.db

これで再度実行すると/var/log/audit/audit.logにこんなメッセージが

type=AVC msg=audit(1593724615.013:262): avc:  denied  { write } for  pid=1877 comm="test.cgi" name="data" dev=dm-0 ino=7025 scontext=unconfined_u:system_r:httpd_sys_script_t:s0 tcontext=unconfined_u:object_r:httpd_sys_script_exec_t:s0 tclass=dir
type=SYSCALL msg=audit(1593724615.013:262): arch=c000003e syscall=2 success=no exit=-13 a0=291229e a1=42 a2=1a4 a3=1a4 items=0 ppid=1367 pid=1877 auid=0 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1 comm="test.cgi" exe="/usr/bin/perl" subj=unconfined_u:system_r:httpd_sys_script_t:s0 key=(null)

name="data" とあるので、フォルダ(dataフォルダ)の権限も同様に設定が必要のようです。

[root@localhost data]# cd ..
[root@localhost cgi-bin]# chcon -t httpd_sys_script_rw_t data

これで書き込めるようになりました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0