LoginSignup
15
13

More than 5 years have passed since last update.

dockerでシステムコールを制御する

Last updated at Posted at 2016-03-27

dockerで使うシステムコールを選びたい

今僕はSlackで使えるPerl製evalbotを作っています。
Perlには危険な関数が多くて、エスケープ処理しようとしても厳しいし...
そこでDockerコンテナ内で使えるシステムコールを制御することにしました🍣

やり方

ここに完璧なやり方が載ってる。
まずカーネルがseccompに対応しているか調べる。

$ cat /boot/config-`uname -r` | grep CONFIG_SECCOMP=

CONFIG_SECCOMP=yと表示されれば対応してるということになります。次に、

Note: seccomp profiles require seccomp 2.2.1 and are only available starting with Debian 9 "Stretch", Ubuntu 15.10 "Wily", and Fedora 22. To use this feature on Ubuntu 14.04, Debian Wheezy, or Debian Jessie, you must download the latest static Docker Linux binary. This feature is currently not available on other distributions.

「seccomp 2.2.1 が必要で、Debian 9 "Stretch"、Ubuntu 15.10 "Wily"、そして Fedora 22で使えるようになるけど、最新のDockerのバイナリをダウンロードしたらUbuntu 14.04, Debian Wheezy, Debian Jessieでも使えるよん」とのことなので、リンク先から自分に合ったものをダウンロードしてくる。
uname -aくらいで確認。

Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt20-1+deb8u4 (2016-02-29) x86_64 GNU/Linux

なので僕の場合だと、https://get.docker.com/builds/Linux/x86_64/docker-latest からダウンロードした。

静的バイナリのdockerを起動する。

ダウンロードしたdockerにchmod +x dockerをして、docker daemon &でデーモンを起動。

呼び出したいシステムコールに合わせてjsonを作成

例えばこんな感じ

example.json
{
    "defaultAction": "SCMP_ACT_ALLOW",
    "architectures": [
        "SCMP_ARCH_X86_64",
        "SCMP_ARCH_X86",
        "SCMP_ARCH_X32"
    ],
    "syscalls": [
        {
            "name": "chown",
            "action": "SCMP_ACT_ERRNO",
        }
    ]
}

この場合だと基本許可するけど、chownだけは呼べないよ!ということになる。
SCMP_ACT_ALLOWで許可、SCMP_ACT_ERRNOで拒否。
{},
{},

[],
[],
といったように最後のブラケットにコンマがついてるとエラーになるので注意しましょう。

実行

あとは
--security-opt seccomp=/path/to/example.jsonといったようにdocker runのオプションとして実行すれば可能です!!

テストしてみたい場合はこのように実行。

$ docker run --rm -it --security-opt seccomp=/path/to/example.json debian:jessie

参考

15
13
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
15
13