我可以限制某个进程对某些文件的访问吗?

发布于 2024-10-08 19:20:30 字数 210 浏览 0 评论 0原文

是否可以在 Linux 中启动一个进程,并限制其对某些文件/目录的访问?例如:

$ start-process --enable-dir=./sandbox --exec="some-script.sh"

some-script.sh 将无法在 ./sandbox 之外执行任何操作。

Is it possible to start a process in Linux, and restrict its access to certain files/directories? For example:

$ start-process --enable-dir=./sandbox --exec="some-script.sh"

some-script.sh won't be able to do anything outside of ./sandbox.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷离° 2024-10-15 19:20:30

您可以使用 chroot 设置进程树的根目录。然而,这意味着该进程的所有依赖项必须在其新根中可用。

有许多软件包可以帮助您根据需要设置 chroot 环境。 Google 是你的朋友;)


关于构建 chroot 环境的一些提示

在为某些程序或守护程序构建 chroot 时,你必须为要 chroot 的程序拥有一个完整的环境 。这意味着您必须在目录中提供最小系统。它可能包含:

  • 一个 shell 和一些 shell 实用程序,或者 busybox 的变体。 (如果您不打算部署一个静态可执行文件,这也包含下一步)。
  • Libc 和其他依赖的共享库。
    • 您需要使用 lddobjdump 检查共享库依赖关系。出现的每个库都必须位于您的私有根目录中。对于您需要的每个可执行文件和库,此步骤可能会重复多次。请注意,某些在运行时使用 dlopen 显式链接的库需要单独进行检查。
  • 根据您计划chroot 最小的/dev 树。
    • 如果您计划 chroot 守护进程,则很可能需要 /dev 中的一些最小文件,例如 randomzero。您可以使用 mknod 命令创建它们。请参阅 mknod 文档,如下以及有关设备应具有哪些主要/次要编号的 Linux 文档 .
  • 另外,取决于您计划chroot是一个最小的/etc。其中需要的文件有:
    • 最小的密码和影子(不是您的系统密码/影子)。
    • 包含 / 的最小 mtab
    • 最小(同样,不是您的系统组文件)。

你必须从某个地方开始,所以最好从你的程序的先决条件开始。有关详细信息,请参阅您的文档

You can use chroot to set the root directory of your process tree. This means however, that all dependencies of that process must be available in it's new root.

There are a number of packages that can help you setup chroot-environments for your needs. Google is your friend ;)


Some pointers on building a chroot environment

When builing a chroot for some program or daemon you have to have a complete environment for the program you want to chroot. This means you have to provide a minimum system in a directory. That might contain:

  • A shell and some shell utilities, or a variant of busybox. (this encompasses the next step too, if you aren't planning on deploying one single static executable that is).
  • Libc and other dependent shared libraries.
    • You need to check shared library dependencies using ldd or objdump. Every library that appears has to be in your private root directory. This step might be repeated several times for every executable and library you need. Note that some libraries, which are linked explicitly at runtime using dlopen need to be checked separately.
  • Depending on what you plan to chroot a minimal /dev tree.
    • If you plan to chroot a daemon process this may well be needing some minimal files in /dev such as random or zero. You can create those with the mknod command. Please refer to the mknod documentation, as well as the linux documentation on which major/minor numbers which device should have.
  • Also depending on what you plan to chroot is a minimal /etc. Files needed therein are:
    • A minimal passwd and shadow (not your system passwd/shadow).
    • A minimal mtab containing /.
    • A minimal group (again, not your system group file).

You have to start somewhere, so it's best to start with the prerequisites for you program. Refer to your documentation for specifics.

╰沐子 2024-10-15 19:20:30

通常,您希望chroot进程,以便它只能访问目录及其子目录,并且只能执行一些定义的命令。

请参阅如何 chroot

Typically you want to chroot the process, so that it can only access a directory and its sub-directories, and only execute some defined commands.

See How to chroot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文