Linux/SELinuxメモ書き
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
RIGHT:[[Linux 関係覚え書き]]
以下の文章を参考にする
http://www.techrepublic.com/blog/opensource/practical-sel...
*SELinux のモード確認 [#mb7de81e]
以下の場合は無効になっている.
$ /usr/sbin/getenforce
Disabled
モードには
-Enforcing
(不正アクセスを報告するとともに,攻撃(アクセス)を拒否する)
-Permissive
(不正アクセスを報告するが,アクセスは許可する)
-Disabled
(SELinux 無効)
の 3つがある.
*モードの変更 [#nac03fe0]
いきなり Enforcing モードにするんではなくて,
Permissive モードで試してみる.
モードの変更は
/etc/sysconfig/selinux
の SELINUX オプションを編集して,再起動する.
SELINUX=enforcing
Enforcing モードと Permissive モードを変更するなら,
setenforce コマンドでも可能.
その場合,再起動は必要ない(未確認)
そのため,Permissive モードはトラブルシューティングに便利.
Disable モードから他のモードへ,もしくは
他のモードから Disable モードに変更する場合は,
前記の selinux ファイルを編集して再起動が必要.
Disable モードから他のモードへの場合,
再起動時,ポリシーの再ラベリングを行うためなのか
自動的に再度再起動がかかるようだ
(1回しか試していないんでたまたま再起動がかかっただけかも...
というわけで,再起動の前に
# fixfiles relabel
をしておくべき.
もしくは 以下のコマンドを実行して再起動する.
"touch /.autorelabel; reboot"
Enforcing モードへ
# /usr/sbin/setenforce Enforcing
# /usr/sbin/setenforce 1
Permissive モードへ
# /usr/sbin/setenforce Permissive
# /usr/sbin/setenforce 0
*各プロセスのコンテキスト(ポリシーのようなもの?)を見る [#...
ps コマンドに Z オプションをつける.
# ps auxZ | grep httpd
system_u:system_r:httpd_t:s0 apache 30544 0.0 0.0 305612...
上記は httpd プロセスが以下のポリシーで稼働していることを...
(タイプが注目される項目?,プロセスに与えられるタイプはド...
-タイプ: httpd_t
-ロール: system_r
-ユーザ: system_u
Z オプションは ls, cp, id などでも使える.
*ポリシーを変更する簡単な例 [#x26fc640]
apache のドキュメントルートを /var/www 以下ではなくて
イレギュラーな場所である /srv/www/foo.com/html にする場合.
Apache の設定ファイルで DocumentRoot ディレクティブを変更...
ブラウザでそのページを表示させたときにアクセスエラーとな...
SELinux がアクセスを拒否しているからだ.
SELinux は,Apache プロセスからそのファイルにアクセスが
あってもいいことをまだ知らない.
Apache がアクセスするために必要なファイルのタイプを見極め...
ls コマンドを使って,デフォルトのファイルやディレクトリの...
見てみる.
# ls -lZ /var/www/html/index.html
-rw-r--r--, root root unconfined_u:object_r:httpd_sys_co...
そうすると,おそらく httpd_sys_content_t タイプが必要だと...
もう一つの方法は,semanage コマンド(RHEL 8 では policycor...
このツールは定義されている SELinux のポリシーを表示,変更...
デフォルトではインストールされていない.
# yum install policycoreutils-python
# semanage fcontext -l | grep '/var/www'
/var/www(/.*)? all files system_u:object_r:httpd_sys_co...
で,/srv/www/foo.com/ に新しいコンテキストを適用する.
# semanage fcontext -a -t httpd_sys_content_t '/sys/www(...
(-a は add, -t はタイプ,最後の引数は正規表現可能)
# semanage fcontext -l | grep '/srv/www'
/srv/www(/.*)? all files system_u:object_r:httpd_sys_co...
# restorecon -Rv /srv/www
(既存の /srv/www 以下のファイルの再ラベリングとセキュリ...
これで,ブラウザで件のページを表示できるようになったはず.
注意点として,
コンテキストを適用してから件のディレクトリにコピーしたフ...
おなじコンテキストが適用されるが,
mv したファイルはものとファイルのコンテキストのままである.
そのため,ホームディレクトリなどで作成したファイルを
/srv/www/html に mv したファイルはブラウザで表示できない.
これを解決するためには,restorecon コマンドで再ラベリング...
% echo "my file" > file.html
% ls -Z file.html
-rw-rw-r--. vdanen vdanen unconfined_u:object_r:user_hom...
% mv file.html /srv/www/foo.com/html/
% ls -Z /srv/www/foo.com/file.html
-rw-rw-r--. vdanen vdanen unconfined_u:object_r:user_hom...
# restorecon -v /srv/www/foo.com/html/file.html
restorecon reset /srv/www/foo.com/html/file.html context
unconfined_u:object_r:user_home_t:s0->system_u:object_r:...
*Permissive モードで動かしてみる. [#sfd2ed72]
起動すると /var/log/messages に
May 13 11:36:41 s_local@centos5 setroubleshoot: SELinux ...
May 13 11:36:41 s_local@centos5 setroubleshoot: SELinux ...
といった warning がでる.
トラブルシュートのため,上記に表示されているコマンドを実...
(selaert は RHEL 8 では setroubleshoot-server パッケージ)
-----------------------------
$sealert -l 79632173-cf18-4f5d-a01a-970667967fba
要約:
SELinux is preventing access to files with the label, fi...
詳細説明:
[SELinux is in permissive mode, the operation would have...
permitted due to permissive mode.]
SELinux permission checks on files labeled file_t are be...
the context the SELinux kernel gives to files that do no...
indicates a serious labeling problem. No files on an SEL...
labeled file_t. If you have just added a new disk drive ...
relabel it using the restorecon command. Otherwise you s...
files system.
アクセスを許可:
You can execute the following command as root to relabel...
"touch /.autorelabel; reboot"
追加情報:
ソースコンテキスト system_u:system_r:portmap_t
ターゲットコンテキスト system_u:object_r:file_t
ターゲットオブジェクト /etc/hosts.allow [ file ]
Source portmap
Source Path /sbin/portmap
Port <不明>
Host centos5.example.co.jp
Source RPM Packages portmap-4.0-65.2.2.1
Target RPM Packages setup-2.5.58-7.el5
ポリシー RPM selinux-policy-2.4.6-279.el5_5.2
Selinux 有効化 True
ポリシータイプ targeted
MLS 有効化 True
強制モード Permissive
プラグイン名 file
ホスト名 centos5.example.co.jp
プラットフォーム Linux centos5.example.co.jp
2.6.18-194.32.1.el5 #1 SMP Wed Jan 5 17:53:09 EST
2011 i686 i686
通知カウント 1
First Seen Fri May 13 11:36:41 2011
Last Seen Fri May 13 11:36:41 2011
Local ID 79632173-cf18-4f5d-a01a-97...
行番号
生の監査メッセージ
host=centos5.example.co.jp type=AVC msg=audit(1305254201...
host=centos5.example.co.jp type=SYSCALL msg=audit(130525...
-----------------------------
sealert のメッセージにあるように以下のコマンドを実行して...
"touch /.autorelabel; reboot"
直った?ようだ.
# ps auxZ |grep portmap
user_u:system_r:portmap_t rpc 6265 0.0 0.1...
portmap はコンテキスト user_u:system_r:portmap_t で動いて...
プロセスにつけるラベルをドメインと呼び,上記 portmap プロ...
リソースにつけるラベルをタイプと呼び,上記(/etc/hosts.all...
----
*Role の変更 [#f202ad38]
policycoreutils-newrole パッケージをインストールする.
$ newrole -r user_r -t unconfined_t
------------
*ポリシーの新規作成 [#i01c1584]
http://www.yam-web.net/selinux/createtype.html を参考にす...
selinux-policy-devel パッケージを追加インストールする.
**user01 の $HOME/public_html のページを表示できるように...
モジュールパッケージの作成
# mkdir /root/selinux
# cd /root/selinux
# cp /usr/share/selinux/devel/Makefile .
# vi local.te
module local 1.0;
type my_type01_t;
files_type(my_type01_t)
# vi local.fc → 後で semanage でやった方が賢明かも
/home/user01/public_html(/.*)? gen_context(system_u:obje...
# make
# /usr/sbin/semodule -i local.pp
コンテキストの適用順変更
/etc/selinux/targeted/contexts/files/file_contexts の
/home/user01/public_html(/.*)? system_u:object_r:my_type...
の部分をカットし,
/etc/selinux/targeted/contexts/files/file_contexts.local ...
タイプの適用
# /sbin/restorecon -FR /home/user01/public_html
ポリシーモジュールの作成
この段階ではまだ,messages にアクセスエラーが記録されてい...
(Enforce モードだとアクセス拒否される)
audit2allow(RHEL 8 では policycoreutils-python-utils パッ...
# audit2allow -a -l -r
require {
type var_log_t;
type my_type01_t;
type httpd_t;
class dir { getattr search };
class file { read getattr append };
}
#============= httpd_t ==============
allow httpd_t my_type01_t:dir { getattr search };
allow httpd_t my_type01_t:file { read getattr };
allow httpd_t user_home_t:dir { getattr search };
allow httpd_t user_home_t:file { read getattr };
上記出力を,local.te に追加する.
$ vi local.te
module local 1.0;
type my_type01_t;
files_type(my_type01_t)
require {
type var_log_t;
type my_type01_t;
type httpd_t;
class dir { getattr search };
class file { read getattr append };
}
#============= httpd_t ==============
allow httpd_t my_type01_t:dir { getattr search };
allow httpd_t my_type01_t:file { read getattr };
allow httpd_t user_home_t:dir { getattr search };
allow httpd_t user_home_t:file { read getattr };
# make
# /usr/sbin/semodule -i local.pp
/etc/selinux/targeted/contexts/files/file_contexts の
/home/user01/public_html(/.*)? system_u:object_r:my_type0...
の部分をカットし,
/etc/selinux/targeted/contexts/files/file_contexts.local ...
実際は,わざわざポリシーを追加しなくても
audit2allow 移行の作業で解決するはず.
というか $HOME/public_html や $HOME/www などは
もともと
/etc/selinux/targeted/contexts/files/homedir_template
/etc/selinux/targeted/contexts/files/file_contexts.homed...
にコンテキストが定義されていて
$HOME/public_html を作成した後,ユーザレベルで
$ /sbin/restorecon -v public_html
をやってやれば,
$ ls -lZd public_html
drwxrwxr-x user01 user01 user_u:object_r:httpd_sys_cont...
と,httpd から読み込めるディレクトリ,ファイルとなるよう...
同様のことが,$HOME/.ssh 以下のファイルなどにも言える.
** 簡易的な方法 [#j5298138]
$ grep denied /var/log/audit/audit.log | audit2allow -M ...
でできた localpolicy.te を編集して
$ sudo make -f /usr/share/selinux/devel/Makefile
$ sudo semodule -i localpolicy.pp
*ポリシーモジュールの削除 [#e6b9636a]
# /usr/sbin/semodule -r local
* ポリシーモジュールの無効化 [#oa44ab68]
# /usr/sbin/semodule -d モジュール名
* ポリシーの詳細調査 [#h765b12c]
どんなポリシーが設定されているか詳細を確認するためには
sesearch(RHEL 8 では setools-console パッケージ)を使う
# sesearch -A --source named_t
* Boolean [#g2104a01]
最近の RHEL では Boolean と呼ばれる許可ルールのグループが...
それを on/off するだけで,まとめてポリシーを変更できる
設定確認には
getsebool -a
設定例
setsebool -P Boolean名 on
"-P" は永続的に有効とする
* RHEL9 [#m9256815]
RHEL9 からは
/etc/sysconfig/selinux
の SELINUX オプションを
SELINUX=disable
にして再起動するとたち上がらなくなるという噂.
なので
sudo grubby --update-kernel ALL --args selinux=0
sudo reboot
終了行:
RIGHT:[[Linux 関係覚え書き]]
以下の文章を参考にする
http://www.techrepublic.com/blog/opensource/practical-sel...
*SELinux のモード確認 [#mb7de81e]
以下の場合は無効になっている.
$ /usr/sbin/getenforce
Disabled
モードには
-Enforcing
(不正アクセスを報告するとともに,攻撃(アクセス)を拒否する)
-Permissive
(不正アクセスを報告するが,アクセスは許可する)
-Disabled
(SELinux 無効)
の 3つがある.
*モードの変更 [#nac03fe0]
いきなり Enforcing モードにするんではなくて,
Permissive モードで試してみる.
モードの変更は
/etc/sysconfig/selinux
の SELINUX オプションを編集して,再起動する.
SELINUX=enforcing
Enforcing モードと Permissive モードを変更するなら,
setenforce コマンドでも可能.
その場合,再起動は必要ない(未確認)
そのため,Permissive モードはトラブルシューティングに便利.
Disable モードから他のモードへ,もしくは
他のモードから Disable モードに変更する場合は,
前記の selinux ファイルを編集して再起動が必要.
Disable モードから他のモードへの場合,
再起動時,ポリシーの再ラベリングを行うためなのか
自動的に再度再起動がかかるようだ
(1回しか試していないんでたまたま再起動がかかっただけかも...
というわけで,再起動の前に
# fixfiles relabel
をしておくべき.
もしくは 以下のコマンドを実行して再起動する.
"touch /.autorelabel; reboot"
Enforcing モードへ
# /usr/sbin/setenforce Enforcing
# /usr/sbin/setenforce 1
Permissive モードへ
# /usr/sbin/setenforce Permissive
# /usr/sbin/setenforce 0
*各プロセスのコンテキスト(ポリシーのようなもの?)を見る [#...
ps コマンドに Z オプションをつける.
# ps auxZ | grep httpd
system_u:system_r:httpd_t:s0 apache 30544 0.0 0.0 305612...
上記は httpd プロセスが以下のポリシーで稼働していることを...
(タイプが注目される項目?,プロセスに与えられるタイプはド...
-タイプ: httpd_t
-ロール: system_r
-ユーザ: system_u
Z オプションは ls, cp, id などでも使える.
*ポリシーを変更する簡単な例 [#x26fc640]
apache のドキュメントルートを /var/www 以下ではなくて
イレギュラーな場所である /srv/www/foo.com/html にする場合.
Apache の設定ファイルで DocumentRoot ディレクティブを変更...
ブラウザでそのページを表示させたときにアクセスエラーとな...
SELinux がアクセスを拒否しているからだ.
SELinux は,Apache プロセスからそのファイルにアクセスが
あってもいいことをまだ知らない.
Apache がアクセスするために必要なファイルのタイプを見極め...
ls コマンドを使って,デフォルトのファイルやディレクトリの...
見てみる.
# ls -lZ /var/www/html/index.html
-rw-r--r--, root root unconfined_u:object_r:httpd_sys_co...
そうすると,おそらく httpd_sys_content_t タイプが必要だと...
もう一つの方法は,semanage コマンド(RHEL 8 では policycor...
このツールは定義されている SELinux のポリシーを表示,変更...
デフォルトではインストールされていない.
# yum install policycoreutils-python
# semanage fcontext -l | grep '/var/www'
/var/www(/.*)? all files system_u:object_r:httpd_sys_co...
で,/srv/www/foo.com/ に新しいコンテキストを適用する.
# semanage fcontext -a -t httpd_sys_content_t '/sys/www(...
(-a は add, -t はタイプ,最後の引数は正規表現可能)
# semanage fcontext -l | grep '/srv/www'
/srv/www(/.*)? all files system_u:object_r:httpd_sys_co...
# restorecon -Rv /srv/www
(既存の /srv/www 以下のファイルの再ラベリングとセキュリ...
これで,ブラウザで件のページを表示できるようになったはず.
注意点として,
コンテキストを適用してから件のディレクトリにコピーしたフ...
おなじコンテキストが適用されるが,
mv したファイルはものとファイルのコンテキストのままである.
そのため,ホームディレクトリなどで作成したファイルを
/srv/www/html に mv したファイルはブラウザで表示できない.
これを解決するためには,restorecon コマンドで再ラベリング...
% echo "my file" > file.html
% ls -Z file.html
-rw-rw-r--. vdanen vdanen unconfined_u:object_r:user_hom...
% mv file.html /srv/www/foo.com/html/
% ls -Z /srv/www/foo.com/file.html
-rw-rw-r--. vdanen vdanen unconfined_u:object_r:user_hom...
# restorecon -v /srv/www/foo.com/html/file.html
restorecon reset /srv/www/foo.com/html/file.html context
unconfined_u:object_r:user_home_t:s0->system_u:object_r:...
*Permissive モードで動かしてみる. [#sfd2ed72]
起動すると /var/log/messages に
May 13 11:36:41 s_local@centos5 setroubleshoot: SELinux ...
May 13 11:36:41 s_local@centos5 setroubleshoot: SELinux ...
といった warning がでる.
トラブルシュートのため,上記に表示されているコマンドを実...
(selaert は RHEL 8 では setroubleshoot-server パッケージ)
-----------------------------
$sealert -l 79632173-cf18-4f5d-a01a-970667967fba
要約:
SELinux is preventing access to files with the label, fi...
詳細説明:
[SELinux is in permissive mode, the operation would have...
permitted due to permissive mode.]
SELinux permission checks on files labeled file_t are be...
the context the SELinux kernel gives to files that do no...
indicates a serious labeling problem. No files on an SEL...
labeled file_t. If you have just added a new disk drive ...
relabel it using the restorecon command. Otherwise you s...
files system.
アクセスを許可:
You can execute the following command as root to relabel...
"touch /.autorelabel; reboot"
追加情報:
ソースコンテキスト system_u:system_r:portmap_t
ターゲットコンテキスト system_u:object_r:file_t
ターゲットオブジェクト /etc/hosts.allow [ file ]
Source portmap
Source Path /sbin/portmap
Port <不明>
Host centos5.example.co.jp
Source RPM Packages portmap-4.0-65.2.2.1
Target RPM Packages setup-2.5.58-7.el5
ポリシー RPM selinux-policy-2.4.6-279.el5_5.2
Selinux 有効化 True
ポリシータイプ targeted
MLS 有効化 True
強制モード Permissive
プラグイン名 file
ホスト名 centos5.example.co.jp
プラットフォーム Linux centos5.example.co.jp
2.6.18-194.32.1.el5 #1 SMP Wed Jan 5 17:53:09 EST
2011 i686 i686
通知カウント 1
First Seen Fri May 13 11:36:41 2011
Last Seen Fri May 13 11:36:41 2011
Local ID 79632173-cf18-4f5d-a01a-97...
行番号
生の監査メッセージ
host=centos5.example.co.jp type=AVC msg=audit(1305254201...
host=centos5.example.co.jp type=SYSCALL msg=audit(130525...
-----------------------------
sealert のメッセージにあるように以下のコマンドを実行して...
"touch /.autorelabel; reboot"
直った?ようだ.
# ps auxZ |grep portmap
user_u:system_r:portmap_t rpc 6265 0.0 0.1...
portmap はコンテキスト user_u:system_r:portmap_t で動いて...
プロセスにつけるラベルをドメインと呼び,上記 portmap プロ...
リソースにつけるラベルをタイプと呼び,上記(/etc/hosts.all...
----
*Role の変更 [#f202ad38]
policycoreutils-newrole パッケージをインストールする.
$ newrole -r user_r -t unconfined_t
------------
*ポリシーの新規作成 [#i01c1584]
http://www.yam-web.net/selinux/createtype.html を参考にす...
selinux-policy-devel パッケージを追加インストールする.
**user01 の $HOME/public_html のページを表示できるように...
モジュールパッケージの作成
# mkdir /root/selinux
# cd /root/selinux
# cp /usr/share/selinux/devel/Makefile .
# vi local.te
module local 1.0;
type my_type01_t;
files_type(my_type01_t)
# vi local.fc → 後で semanage でやった方が賢明かも
/home/user01/public_html(/.*)? gen_context(system_u:obje...
# make
# /usr/sbin/semodule -i local.pp
コンテキストの適用順変更
/etc/selinux/targeted/contexts/files/file_contexts の
/home/user01/public_html(/.*)? system_u:object_r:my_type...
の部分をカットし,
/etc/selinux/targeted/contexts/files/file_contexts.local ...
タイプの適用
# /sbin/restorecon -FR /home/user01/public_html
ポリシーモジュールの作成
この段階ではまだ,messages にアクセスエラーが記録されてい...
(Enforce モードだとアクセス拒否される)
audit2allow(RHEL 8 では policycoreutils-python-utils パッ...
# audit2allow -a -l -r
require {
type var_log_t;
type my_type01_t;
type httpd_t;
class dir { getattr search };
class file { read getattr append };
}
#============= httpd_t ==============
allow httpd_t my_type01_t:dir { getattr search };
allow httpd_t my_type01_t:file { read getattr };
allow httpd_t user_home_t:dir { getattr search };
allow httpd_t user_home_t:file { read getattr };
上記出力を,local.te に追加する.
$ vi local.te
module local 1.0;
type my_type01_t;
files_type(my_type01_t)
require {
type var_log_t;
type my_type01_t;
type httpd_t;
class dir { getattr search };
class file { read getattr append };
}
#============= httpd_t ==============
allow httpd_t my_type01_t:dir { getattr search };
allow httpd_t my_type01_t:file { read getattr };
allow httpd_t user_home_t:dir { getattr search };
allow httpd_t user_home_t:file { read getattr };
# make
# /usr/sbin/semodule -i local.pp
/etc/selinux/targeted/contexts/files/file_contexts の
/home/user01/public_html(/.*)? system_u:object_r:my_type0...
の部分をカットし,
/etc/selinux/targeted/contexts/files/file_contexts.local ...
実際は,わざわざポリシーを追加しなくても
audit2allow 移行の作業で解決するはず.
というか $HOME/public_html や $HOME/www などは
もともと
/etc/selinux/targeted/contexts/files/homedir_template
/etc/selinux/targeted/contexts/files/file_contexts.homed...
にコンテキストが定義されていて
$HOME/public_html を作成した後,ユーザレベルで
$ /sbin/restorecon -v public_html
をやってやれば,
$ ls -lZd public_html
drwxrwxr-x user01 user01 user_u:object_r:httpd_sys_cont...
と,httpd から読み込めるディレクトリ,ファイルとなるよう...
同様のことが,$HOME/.ssh 以下のファイルなどにも言える.
** 簡易的な方法 [#j5298138]
$ grep denied /var/log/audit/audit.log | audit2allow -M ...
でできた localpolicy.te を編集して
$ sudo make -f /usr/share/selinux/devel/Makefile
$ sudo semodule -i localpolicy.pp
*ポリシーモジュールの削除 [#e6b9636a]
# /usr/sbin/semodule -r local
* ポリシーモジュールの無効化 [#oa44ab68]
# /usr/sbin/semodule -d モジュール名
* ポリシーの詳細調査 [#h765b12c]
どんなポリシーが設定されているか詳細を確認するためには
sesearch(RHEL 8 では setools-console パッケージ)を使う
# sesearch -A --source named_t
* Boolean [#g2104a01]
最近の RHEL では Boolean と呼ばれる許可ルールのグループが...
それを on/off するだけで,まとめてポリシーを変更できる
設定確認には
getsebool -a
設定例
setsebool -P Boolean名 on
"-P" は永続的に有効とする
* RHEL9 [#m9256815]
RHEL9 からは
/etc/sysconfig/selinux
の SELINUX オプションを
SELINUX=disable
にして再起動するとたち上がらなくなるという噂.
なので
sudo grubby --update-kernel ALL --args selinux=0
sudo reboot
ページ名: