/etc/apparmor.d/ に保存され、プロファイルには各プログラムが使うことができるリソースに対するアクセス制御規則のリストが含まれています。プロファイルは apparmor_parser コマンドによってコンパイルされてカーネルに読み込まれます。各プロファイルは enforce または complain モードで読み込みます。enforce モードではポリシーの遵守を強制され、ポリシー違反の試行を報告されます。これに対して、complain モードではポリシーの遵守を強制されませんが、ポリシー違反で拒否されうるシステムコールを記録されます。
#apt install apparmor apparmor-profiles apparmor-utils[...] #perl -pi -e 's,GRUB_CMDLINE_LINUX="(.*)"$,GRUB_CMDLINE_LINUX="$1 apparmor=1 security=apparmor",' /etc/default/grub#update-grub
aa-status を使えば AppArmor の有効化を素早く確認できます。
#aa-statusapparmor module is loaded. 44 profiles are loaded. 9 profiles are in enforce mode. /usr/bin/lxc-start /usr/lib/chromium-browser/chromium-browser//browser_java [...] 35 profiles are in complain mode. /sbin/klogd [...] 3 processes have profiles defined. 1 processes are in enforce mode. /usr/sbin/libvirtd (1295) 2 processes are in complain mode. /usr/sbin/avahi-daemon (941) /usr/sbin/avahi-daemon (1000) 0 processes are unconfined but have a profile defined.
aa-enforce または aa-complain を呼び出します。さらにプロファイルを完全に無効化するには、aa-disable を使うか aa-audit を使ってプロファイルを audit モード (承認されたシステムコールもログ記録するモード) に切り替えます。
#aa-enforce /usr/sbin/avahi-daemonSetting /usr/sbin/avahi-daemon to enforce mode.#aa-complain /etc/apparmor.d/usr.bin.lxc-startSetting /etc/apparmor.d/usr.bin.lxc-start to complain mode.
aa-unconfined コマンドが用意されています。aa-unconfined コマンドは関連するプロファイルが存在せず開かれたネットワークソケットを公開しているプログラムを表示します。aa-unconfined に --paranoid オプションを付けて実行すれば、少なくとも 1 つ以上のアクティブなネットワーク接続を持つアクセス制限を設けていないプロセスがすべて表示されます。
#aa-unconfined801 /sbin/dhclient not confined 890 /sbin/rpcbind not confined 899 /sbin/rpc.statd not confined 929 /usr/sbin/sshd not confined 941 /usr/sbin/avahi-daemon confined by '/usr/sbin/avahi-daemon (complain)' 988 /usr/sbin/minissdpd not confined 1276 /usr/sbin/exim4 not confined 1485 /usr/lib/erlang/erts-6.2/bin/epmd not confined 1751 /usr/lib/erlang/erts-6.2/bin/beam.smp not confined 19592 /usr/lib/dleyna-renderer/dleyna-renderer-service not confined
/sbin/dhclient 用のプロファイルを作成しようとしています。プロファイルを作成するためには aa-genprof dhclient を使います。aa-genprof はユーザに別のウィンドウから対象のアプリケーションを開始することを求め、その後ユーザにシステムログ内の AppArmor イベントをスキャンしてこれらのログをアクセス規則に変換するために aa-genprof に戻ることを求めます。ログ記録されたそれぞれのイベントについて、aa-genprof は 1 つ以上の規則を提案します。ユーザは提案された規則を受け入れるかまたはさまざまな方法で規則を編集することが可能です。
#aa-genprof dhclientWriting updated profile for /sbin/dhclient. Setting /sbin/dhclient to complain mode. Before you begin, you may wish to check if a profile already exists for the application you wish to confine. See the following wiki page for more information: http://wiki.apparmor.net/index.php/Profiles Please start the application to be profiled in another window and exercise its functionality now. Once completed, select the "Scan" option below in order to scan the system logs for AppArmor events. For each AppArmor event, you will be given the opportunity to choose whether the access should be allowed or denied. Profiling: /sbin/dhclient [(S)can system log for AppArmor events] / (F)inish Reading log entries from /var/log/audit/audit.log. Profile: /sbin/dhclientExecute: /usr/lib/NetworkManager/nm-dhcp-helper Severity: unknown (I)nherit / (C)hild / (P)rofile / (N)amed / (U)nconfined / (X) ix On / (D)eny / Abo(r)t / (F)inish
PShould AppArmor sanitise the environment when switching profiles? Sanitising environment is more secure, but some applications depend on the presence of LD_PRELOAD or LD_LIBRARY_PATH. (Y)es / [(N)o]YWriting updated profile for /usr/lib/NetworkManager/nm-dhcp-helper. Complain-mode changes: WARN: unknown capability: CAP_net_raw Profile: /sbin/dhclientCapability: net_raw Severity: unknown [(A)llow] / (D)eny / (I)gnore / Audi(t) / Abo(r)t / (F)inish
AAdding capability net_raw to profile. Profile: /sbin/dhclientPath: /etc/nsswitch.conf Mode: r Severity: unknown 1 - #include <abstractions/apache2-common> 2 - #include <abstractions/libvirt-qemu> 3 - #include <abstractions/nameservice> 4 - #include <abstractions/totem> [5 - /etc/nsswitch.conf] [(A)llow] / (D)eny / (I)gnore / (G)lob / Glob with (E)xtension / (N)ew / Abo(r)t / (F)inish / (M)ore
3Profile: /sbin/dhclient Path: /etc/nsswitch.conf Mode: r Severity: unknown 1 - #include <abstractions/apache2-common> 2 - #include <abstractions/libvirt-qemu> [3 - #include <abstractions/nameservice>] 4 - #include <abstractions/totem> 5 - /etc/nsswitch.conf [(A)llow] / (D)eny / (I)gnore / (G)lob / Glob with (E)xtension / (N)ew / Abo(r)t / (F)inish / (M)oreAAdding #include <abstractions/nameservice> to profile. Profile: /sbin/dhclient Path: /proc/7252/net/dev Mode: r Severity: 6 1 - /proc/7252/net/dev [2 - /proc/*/net/dev] [(A)llow] / (D)eny / (I)gnore / (G)lob / Glob with (E)xtension / (N)ew / Abo(r)t / (F)inish / (M)oreAAdding /proc/*/net/dev r to profile [...] Profile: /sbin/dhclientPath: /run/dhclient-eth0.pid Mode: w Severity: unknown [1 - /run/dhclient-eth0.pid] [(A)llow] / (D)eny / (I)gnore / (G)lob / Glob with (E)xtension / (N)ew / Abo(r)t / (F)inish / (M)ore
NEnter new path: /run/dhclient*.pid Profile: /sbin/dhclient Path: /run/dhclient-eth0.pid Mode: w Severity: unknown 1 - /run/dhclient-eth0.pid [2 - /run/dhclient*.pid] [(A)llow] / (D)eny / (I)gnore / (G)lob / Glob with (E)xtension / (N)ew / Abo(r)t / (F)inish / (M)oreAAdding /run/dhclient*.pid w to profile [...] Profile: /usr/lib/NetworkManager/nm-dhcp-helperPath: /proc/filesystems Mode: r Severity: 6 [1 - /proc/filesystems] [(A)llow] / (D)eny / (I)gnore / (G)lob / Glob with (E)xtension / (N)ew / Abo(r)t / (F)inish / (M)ore
AAdding /proc/filesystems r to profile = Changed Local Profiles = The following local profiles were changed. Would you like to save them? [1 - /sbin/dhclient] 2 - /usr/lib/NetworkManager/nm-dhcp-helper (S)ave Changes / Save Selec(t)ed Profile / [(V)iew Changes] / View Changes b/w (C)lean profiles / Abo(r)tSWriting updated profile for /sbin/dhclient. Writing updated profile for /usr/lib/NetworkManager/nm-dhcp-helper. Profiling: /sbin/dhclient [(S)can system log for AppArmor events] / (F)inishFSetting /sbin/dhclient to enforce mode. Setting /usr/lib/NetworkManager/nm-dhcp-helper to enforce mode. Reloaded AppArmor profiles in enforce mode. Please consider contributing your new profile! See the following wiki page for more information: http://wiki.apparmor.net/index.php/Profiles Finished generating profile for /sbin/dhclient.
aa-genprof プログラムはユーザが入力した操作文字を再表示しないという点に注意してください。しかし、上に挙げた例では説明を明確に行うために、ユーザが入力した操作文字をその直後に表記しています。
最初に別のプログラムである nm-dhcp-helper の実行イベントが検出されています。この場合、ユーザに対していくつかの選択肢が示されます。ユーザは dhclient のプロファイルを適用して nm-dhcp-helper を実行したり (「Inherit」を選んだ場合)、専用のプロファイルを適用して nm-dhcp-helper を実行したり (「Profile」と「Named」を選んだ場合。両者の違いは任意のプロファイル名を使用する可能性があるか否かです)、dhclient のサブプロファイルを適用して nm-dhcp-helper を実行したり (「Child」を選んだ場合)、プロファイルを適用せずに nm-dhcp-helper を実行したり (「Unconfined」を選んだ場合)、nm-dhcp-helper の実行を拒否したり (「Deny」を選んだ場合) することも可能です。
ここでまだ存在しない専用プロファイルを適用して nm-dhcp-helper を実行することを選んだ場合、 aa-genprof ツールは不足しているプロファイルを作成し、さらにこのプロファイルに対する規則を提案します。この点に注意してください。
| |
カーネルレベルでは root ユーザの特権が「capability」に分割されます。dhclient からのシステムコールが特定の「capability」を要求する場合、AppArmor はプロファイルの中でプログラムがその「capability」を使うことを許可されているかを確認します。
| |
ここで dhclient は /etc/nsswitch.conf を読み込もうとしています。aa-genprof は複数の「abstraction」を介して /etc/nsswitch.conf の読み込みが可能になることを検出し、代替選択肢として該当する「abstraction」を挙げています。abstraction とは通常同時に使われる複数のリソースをまとめた一連のアクセス規則を再利用できる形で提供するものです。今回の場合、/etc/nsswitch.conf ファイルは一般に C 言語ライブラリのネームサービスに関連する関数を通じてアクセスされます。ここでは最初に「3」を入力して「#include <abstractions/nameservice>」を選択し、その後に「A」を入力して/etc/nsswitch.conf の読み込みを許可しています。
| |
ここで dhclient は /run/dhclient-eth0.pid ファイルを作成しようとしています。/run/dhclient-eth0.pid だけの作成を許可した場合、ユーザが eth0 以外のネットワークインターフェースに対して dhclient を実行する際に dhclient が動作しなくなるでしょう。このため、ここでは「New」を選択してファイル名をより一般的な「/run/dhclient*.pid」に変更した後、「Allow」を選択してこの規則を適用しています。
| |
このアクセス要求に対するアクセス規則は dhclient 専用のプロファイルではなく、 /usr/lib/NetworkManager/nm-dhcp-helper 専用のプロファイルを適用して nm-dhcp-helper を実行することを許可した際に作成された新しいプロファイルに組み込まれる点に注意してください。
ログ記録されたすべてのイベントを検討した後、 aa-genprof プログラムは実行中に作成されたすべてのプロファイルを保存することを提案します。今回の場合、2 つのプロファイルを「Save」を使って一度に保存した後 (1 つずつ保存することも可能です)、「Finish」でプログラムを終了しています。
|
aa-genprof は aa-logprof の洗練されたラッパーに過ぎません。すなわち aa-genprof は空のプロファイルを作成し、complain モードでそのプロファイルを読み込み、aa-logprof を実行しているだけです。aa-logprof はログ記録されたプロファイル違反に基づいてプロファイルを更新するツールです。このため、たった今作成したプロファイルを改良するために aa-genprof をもう一度実行することが可能です。
/etc/apparmor.d/sbin.dhclient は完全なプロファイルに近いものとなるでしょう。
# Last Modified: Tue Sep 8 21:40:02 2015
#include <tunables/global>
/sbin/dhclient {
#include <abstractions/base>
#include <abstractions/nameservice>
capability net_bind_service,
capability net_raw,
/bin/dash r,
/etc/dhcp/* r,
/etc/dhcp/dhclient-enter-hooks.d/* r,
/etc/dhcp/dhclient-exit-hooks.d/* r,
/etc/resolv.conf.* w,
/etc/samba/dhcp.conf.* w,
/proc/*/net/dev r,
/proc/filesystems r,
/run/dhclient*.pid w,
/sbin/dhclient mr,
/sbin/dhclient-script rCx,
/usr/lib/NetworkManager/nm-dhcp-helper Px,
/var/lib/NetworkManager/* r,
/var/lib/NetworkManager/*.lease rw,
/var/lib/dhcp/*.leases rw,
profile /sbin/dhclient-script flags=(complain) {
#include <abstractions/base>
#include <abstractions/bash>
/bin/dash rix,
/etc/dhcp/dhclient-enter-hooks.d/* r,
/etc/dhcp/dhclient-exit-hooks.d/* r,
/sbin/dhclient-script r,
}
}