Linuxプロセスの書き込み権限をフォルダに制限する

Linuxプロセスの書き込み権限をフォルダに制限する

プロセス(およびすべての潜在的なサブプロセス)が私のユーザープロファイルに基づいてファイルシステムを読み取ることができるようにしたいのですが、そのプロセスの書き込み権限を事前に選択されたフォルダのセット(おそらく1つのフォルダ)に制限したいと思います。 )。

chrootアクションが広すぎるようです。プロセスをファイルシステムの特定の部分に制限すると、/binフォルダなどをマウントする必要性がはるかに簡単になります。私のプロセスは、私が起動する一般的なプロセスのようにファイルシステムの内容を読み取ることができるはずです。

Dockerコンテナを使用してボリュームをマウントすることはできますが、これは過度のようです。ドッカーのインストール、イメージの作成、コンテナの起動などの作業が必要です。

似たようなことをする方法はありますか? :

restricted-exec --read-all --write-to /a/particular/path --write-to /another/particular/path my-executable -- --option-to-the-executable

一部unveilただし、呼び出しプロセスによって制御され、書き込みアクセスにのみ使用されます。

ベストアンサー1

を使用することもできます。これには、systemdなどのファイルシステムへのアクセスを制御するためのさまざまな設定があります。ReadOnlyPathsReadWritePaths

systemd-run以下は、書き込み権限のみを使用してコマンドを実行する簡単な例です/var/lib。この例はtouch複数のディレクトリにファイルを作成するために使用されますが、書き込み可能なパスのみが成功します。

root@ubuntu:~# systemd-run --wait -p ProtectSystem=strict -p ProtectHome=read-only -p ReadWritePaths=/var/lib bash -c
'touch /etc/myfile /var/lib/myfile /var/cache/myfile /root/myfile /home/ubuntu/myfile'
Running as unit: run-u1008.service
Finished with result: exit-code
Main processes terminated with: code=exited/status=1
Service runtime: 44ms
root@ubuntu:~# ls -l {/etc/,/var/lib/,/var/cache/,/root/,/home/ubuntu/}myfile
ls: cannot access '/etc/myfile': No such file or directory
ls: cannot access '/var/cache/myfile': No such file or directory
ls: cannot access '/root/myfile': No such file or directory
ls: cannot access '/home/ubuntu/myfile': No such file or directory
-rw-r--r-- 1 root root 0 Mar  3 23:17 /var/lib/myfile

編集する

オプションを使用してユーザーとしてコマンドを実行できます-p User。残念ながら、私の提案を使用するにはrootアクセス権が必要です。いいえファイルシステム保護の使用--user(基準:systemd-run --userこのような制限が実装されていないのはなぜですかProtectSystem)。

ここに別の簡単な例があります。今回は、コマンドがubuntuユーザーとして実行されます。

root@ubuntu:~# install -o ubuntu -g ubuntu -d /tmp/{test,test2}
root@ubuntu:~# systemd-run --wait -p User=ubuntu -p ProtectSystem=strict -p ProtectHome=read-only -p ReadWritePaths=/tmp/test bash -c 'touch /tmp/test/myfile /tmp/test2/myfile /home/ubuntu/myfile'
Running as unit: run-u1043.service
Finished with result: exit-code
Main processes terminated with: code=exited/status=1
Service runtime: 55ms
root@ubuntu:~# ls -l /tmp/{test,test2}/myfile /home/ubuntu/myfile
ls: cannot access '/tmp/test2/myfile': No such file or directory
ls: cannot access '/home/ubuntu/myfile': No such file or directory
-rw-r--r-- 1 ubuntu ubuntu 0 Mar  5 17:37 /tmp/test/myfile

保護に興味がある場合は、systemdの機能を/tmp検討してください。PrivateTmpこれはサンドボックスプロセスで使用できるシステム設定の1つにすぎません。

おすすめ記事