procfs の hidepid オプション

LinuxカーネルChangelog を斜め読みしていたら hidepid オプションなるものが出ていたのを知りました。 ps や top 等のコマンドを使うと他ユーザのプロセス名やその他の情報を procfs (/proc) を通していい感じに参照できますが、procfsの仕組みとしてこの参照を制限する仕組みのようです


本エントリは下記を参考に書いたものになります

環境

でざっくりと検証した内容を記しておきます

hidepid=1 を有効にする

sudo mount -oremount,hidepid=1 proc
[vagrant@local ~]$ ps auxf
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
vagrant  11877  0.1  0.1 108296  1924 pts/1    Ss   14:56   0:00 -bash
vagrant  11937  0.0  0.1 110188  1064 pts/1    R+   14:58   0:00  \_ ps auxf
vagrant   2492  0.0  0.1 108296  1904 pts/0    Ss+  12:30   0:00 -bash

vagrantユーザ (uid:500, gid:500) 以外のプロセスが参照できなくなりました


/proc/ のディレクトリを確認することはできますが、参照できません (EPERM)

[vagrant@local ~]$ ls -hal /proc
total 4.0K
dr-xr-xr-x  105 root    root       0 Oct  7 15:18 .
dr-xr-xr-x   23 root    root    4.0K Oct  7 15:18 ..
dr-xr-xr-x.   7 root    root       0 Oct 10 14:57 1
dr-xr-xr-x.   7 root    root       0 Oct 10 14:57 10
dr-xr-xr-x.   7 root    root       0 Oct 10 14:57 1009
dr-xr-xr-x.   7 root    root       0 Oct 10 14:57 1028
dr-xr-xr-x.   7 root    root       0 Oct 10 14:57 11
dr-xr-xr-x.   7 root    root       0 Oct 10 14:57 1106
dr-xr-xr-x.   7 postfix postfix    0 Oct 10 14:57 1114
dr-xr-xr-x.   7 root    root       0 Oct 10 14:57 1116
dr-xr-xr-x.   7 root    root       0 Oct 10 14:57 1129
dr-xr-xr-x.   7 root    root       0 Oct 10 14:57 1131
dr-xr-xr-x.   7 root    root       0 Oct 10 14:57 1133
dr-xr-xr-x.   7 root    root       0 Oct 10 14:57 1135
[vagrant@local ~]$ ls -hal /proc/1
ls: cannot open directory /proc/1: Operation not permitted

hidepid=2 を有効にする

sudo mount -oremount,hidepid=2 proc
[vagrant@local ~]$ ps auxf
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
vagrant  11877  0.1  0.1 108296  1924 pts/1    Ss   14:56   0:00 -bash
vagrant  11942  0.0  0.1 110188  1056 pts/1    R+   15:00   0:00  \_ ps auxf
vagrant   2492  0.0  0.1 108296  1904 pts/0    Ss+  12:30   0:00 -bash

hidepid=1 の時と同様に vagrantユーザ (uid:500, gid:500) 以外のプロセスは参照できません



/proc/ のディレクトリ を見ると、vagrantユーザのプロセスだけが確認できます。inode が vagrant:vagrant になっていますね。他ユーザの pid は表示されません。

[vagrant@local ~]$ ls -hal /proc/
total 4.0K
dr-xr-xr-x  105 root    root       0 Oct  7 15:18 .
dr-xr-xr-x   23 root    root    4.0K Oct  7 15:18 ..
dr-xr-xr-x.   7 vagrant vagrant    0 Oct 10 15:00 11877
dr-xr-xr-x.   7 vagrant vagrant    0 Oct 10 15:01 11948
dr-xr-xr-x.   7 vagrant vagrant    0 Oct 10 15:00 2492

gid オプションを足す

gid オプションを追加すると、指定した gid を持つユーザは制限が解除されます ( 全部見える )

sudo mount -oremount,hidepid=2,gid=500 proc
[vagrant@local ~]$ ps auxf
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         2  0.0  0.0      0     0 ?        S    12:27   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    12:27   0:00  \_ [migration/0]
root         4  0.0  0.0      0     0 ?        S    12:27   0:04  \_ [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S    12:27   0:00  \_ [migration/0]
root         6  0.0  0.0      0     0 ?        S    12:27   0:00  \_ [watchdog/0]
root         7  0.0  0.0      0     0 ?        S    12:27   0:00  \_ [migration/1]
root         8  0.0  0.0      0     0 ?        S    12:27   0:00  \_ [migration/1]
root         9  0.0  0.0      0     0 ?        S    12:27   0:04  \_ [ksoftirqd/1]
root        10  0.0  0.0      0     0 ?        S    12:27   0:00  \_ [watchdog/1]
root        11  0.0  0.0      0     0 ?        S    12:27   0:02  \_ [events/0]
root        12  0.0  0.0      0     0 ?        S    12:27   0:05  \_ [events/1]
root        13  0.0  0.0      0     0 ?        S    12:27   0:00  \_ [cgroup]
root        14  0.0  0.0      0     0 ?        S    12:27   0:00  \_ [khelper]
root        15  0.0  0.0      0     0 ?        S    12:27   0:00  \_ [netns]
root        16  0.0  0.0      0     0 ?        S    12:27   0:00  \_ [async/mgr]

...

Nagios、Zabbix(よく知らない) のような監視エージェントや munin などのリソースモニタリングのプロセスには特別なgid を割り当てて、 gidオプションを設定しておくと良いでしょう。

感想

grsecurity patch でも同様の機能を提供していましたが、カーネルにパッチを当てずにつかえるとなると利用しやすいですね