0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ping で linux kernel の capabilities の実験を成功させるためのメモ

Posted at

数年前の書籍で linux kernel の capabilities(7) の実験で ping を使ったサンプルが載ってたんですが、手元の centos stream 8 で試したらそのとおりにならなくて調べたメモです。

結論

  • net.ipv4.ping_group_range でping可能なグループID範囲を設定していて、この範囲が広いと CAP_NET_RAWsetuid 無しで実行できるようになってる。
  • 少なくとも手元の centos stream 8 や ubuntu 20 では 0 2147483647 となっているため、capabilities なしでも一般ユーザで ping できる。
  • もし CAP_NET_RAWsetuidが必要な状況を再現したければ sudo sysctl -w net.ipv4.ping_group_range="1 0" する。
[centos stream 8 のデフォルト]
$ uname -r
4.18.0-448.el8.x86_64

$ sysctl net.ipv4.ping_group_range
net.ipv4.ping_group_range = 0   2147483647

[ubuntu 20.04 LTS のデフォルト]
$ uname -r
5.4.0-144-generic

$ sysctl net.ipv4.ping_group_range
net.ipv4.ping_group_range = 0   2147483647

実験例

実際に centos stream 8 上で試した例です。

## (1) 現在の net.ipv4.ping_group_range を確認
$ sysctl net.ipv4.ping_group_range
net.ipv4.ping_group_range = 0   2147483647

## (2) kernel document でも記載している通り ICMP_PROTO を作れなくする "1 0" に変更
$ sudo sysctl -w net.ipv4.ping_group_range="1 0"
net.ipv4.ping_group_range = 1 0

## (3) /bin/ping を ./myping にコピーし、root所有にして setuid もする。
$ cp /bin/ping ./myping
$ sudo chown root ./myping
$ sudo chmod +s ./myping
$ ./myping 10.0.2.2
PING 10.0.2.2 (10.0.2.2) 56(84) bytes of data.
64 bytes from 10.0.2.2: icmp_seq=1 ttl=64 time=0.157 ms
...
(root +s なら動く模様)

## (4) ./myping を一般ユーザ権限に戻す。
$ cp /bin/ping ./myping
$ ./myping 10.0.2.2
ping: socket: Operation not permitted

## (5) setcap(8) で CAP_NET_RAW を許可 -> ping 成功
$ sudo setcap cap_net_raw=ep ./myping
$ getcap ./myping
./myping cap_net_raw=ep
$ ./myping 10.0.2.2
PING 10.0.2.2 (10.0.2.2) 56(84) bytes of data.
64 bytes from 10.0.2.2: icmp_seq=1 ttl=64 time=0.094 ms
(...)
(-> 一般ユーザ権限で動作していることを ps euf で別セッションから確認済み)

## (6) net.ipv4.ping_group_range の設定値を復元
$ sudo sysctl -w net.ipv4.ping_group_range="0   2147483647"
net.ipv4.ping_group_range = 0   2147483647

参考資料

linux kernel の capabilities(7) について:

ping の capabilities について:

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?