2009-12-11
■[SELinux][Linux][検証] SELinux を Ruby から操作してみる。 
Fedora に「libselinux-ruby」というパッケージがあります。
これを使うことで、SELinux を Ruby からある程度操作することが可能になる。
と、いうことで試してみました。
未熟者なので言葉などおかしいところがあったら是非教えてやってください。。
環境
インストール方法
インストールはいたって簡単。以下のコマンドを実行するだけです。
# yum -y install libselinux-ruby
中身はこれしかありません。man もありません。
# rpm -ql libselinux-ruby
man がない!
と、いうことで既に libselinux-ruby を使用しているソフトウェアを参考にします。
そのソフトウェアは「puppet」です。過去記事はこちら
関連しそうなソースを見ていくと、
libselinux に用意されているものはそれなりに使えるようです。
この辺りにあるものは一通り使えるはずです…。
使い方
ライブラリロード
libselinux-ruby を利用するためには、selinux.so をロードします。
これで、libselinux-ruby を利用することが可能になります。
require 'selinux'
(深く考えず)使ってみる
selinux.so をロードすると、Selinux という名前のモジュール(?)が作られます。
実際に使ってみました。
SELinux が有効であれば、enable と表示をし、ラベルの表示と設定を行います。
SELinux が無効な場合は、disabled と表示します。
#!/usr/bin/ruby require "selinux" if Selinux.is_selinux_enabled == 1 then puts "enable" # check security context puts "/root/.bash_history security context is " + Selinux.getfilecon("/root/.bash_history")[1] puts "/var/log/messages security context is " + Selinux.getfilecon("/var/log/messages")[1] puts "/root/work/README security context is " + Selinux.getfilecon("/root/work/README")[1] # set security context Selinux.setfilecon("/root/work/README", "system_u:object_r:httpd_sys_content_t:s0") puts "/root/work/README security context is " + Selinux.getfilecon("/root/work/README")[1] else puts "disabled" end
実行結果
# sestatus SELinux status: enabled SELinuxfs mount: /selinux Current mode: enforcing Mode from config file: enforcing Policy version: 24 Policy from config file: targeted # # ./selinux.rb enabled /root/.bash_history security context is system_u:object_r:admin_home_t:s0 /var/log/messages security context is system_u:object_r:var_log_t:s0 /root/work/README security context is system_u:object_r:admin_home_t:s0 /root/work/README security context is system_u:object_r:httpd_sys_content_t:s0
終わり
とりあえず、使えることは分かりました。
引き続き、調査を行ってみることにします。
コメントを書く
トラックバック - http://d.hatena.ne.jp/ishikawa84g/20091211/1260459603
リンク元
- 68 http://www.google.co.jp/search?q=mcstrans&lr=lang_ja&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:ja:official&client=firefox-a
- 43 http://d.hatena.ne.jp/bonvivant/20090514/1242231536
- 43 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja:official&hs=EDc&q=mcstrans&btnG=検索&lr=lang_ja&aq=f&oq=
- 19 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rlz=1T4GGLL_jaJP314JP315&q=avc:++denied
- 15 http://twitter.com/ishikawa84g
- 13 http://www.google.co.jp/search?hl=ja&q=mcstrans&btnG=検索&lr=&aq=f&oq=
- 10 http://www.google.co.jp/search?q=mcstrans&ie=utf-8&oe=utf-8&aq=t&rls=org.vinelinux:ja:official&client=firefox-a
- 9 http://www.google.co.jp/search?hl=ja&q=mcstrans&sourceid=navclient-ff&rlz=1B3GGGL_jaJP322JP322&ie=UTF-8
- 9 http://www.google.com/search?hl=ja&lr=lang_ja&ie=UTF-8&oe=UTF-8&q=/usr/libexec/gam_server&num=50
- 8 http://www.google.co.jp/search?client=firefox-a&rls=org.mozilla:ja:official&channel=s&hl=ja&source=hp&q=gam_server&lr=&btnG=Google+検索