Joa*_*ner 13 linux security selinux apparmor capabilities
我想以一种无法创建或打开任何要写入的文件的方式在 Linux 上运行命令。它应该仍然能够正常读取文件(因此空的 chroot 不是一个选项),并且仍然能够写入已经打开的文件(尤其是 stdout)。
如果仍然可以将文件写入某些目录(即当前目录),则可以加分。
我正在寻找一个进程本地的解决方案,即不涉及为整个系统配置 AppArmor 或 SELinux 之类的东西,也不涉及 root 权限。不过,这可能涉及安装他们的内核模块。
我正在研究功能,如果有创建文件的功能,这些功能会很好也很容易。ulimit 是另一种方便的方法,如果它涵盖了这个用例。
如何创建一个空的 chroot,然后在 chroot 中将主文件系统绑定挂载为只读?
创建只读绑定安装可能应该是这样的:
mount --bind /foo/ /path/to/chroot/
mount -o remount,ro /path/to/chroot/
Run Code Online (Sandbox Code Playgroud)
您可以绑定挂载您希望 jail 也具有写入权限的其他目录。如果您需要绑定挂载特殊目录(/dev/、/proc/、/sys/),请小心,按原样挂载它们可能不安全。
似乎适合这项工作的工具是fseccomp基于sync-ignoringBastian Blank 的 f 代码,我想出了这个相对较小的文件,它导致所有子文件都无法打开文件进行写入:
/*
* Copyright (C) 2013 Joachim Breitner <mail@joachim-breitner.de>
*
* Based on code Copyright (C) 2013 Bastian Blank <waldi@debian.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _GNU_SOURCE 1
#include <errno.h>
#include <fcntl.h>
#include <seccomp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define filter_rule_add(action, syscall, count, ...) \
if (seccomp_rule_add(filter, action, syscall, count, ##__VA_ARGS__)) abort();
static int filter_init(void)
{
scmp_filter_ctx filter;
if (!(filter = seccomp_init(SCMP_ACT_ALLOW))) abort();
if (seccomp_attr_set(filter, SCMP_FLTATR_CTL_NNP, 1)) abort();
filter_rule_add(SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1, SCMP_A1(SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY));
filter_rule_add(SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1, SCMP_A1(SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR));
return seccomp_load(filter);
}
int main(__attribute__((unused)) int argc, char *argv[])
{
if (argc <= 1)
{
fprintf(stderr, "usage: %s COMMAND [ARG]...\n", argv[0]);
return 2;
}
if (filter_init())
{
fprintf(stderr, "%s: can't initialize seccomp filter\n", argv[0]);
return 1;
}
execvp(argv[1], &argv[1]);
if (errno == ENOENT)
{
fprintf(stderr, "%s: command not found: %s\n", argv[0], argv[1]);
return 127;
}
fprintf(stderr, "%s: failed to execute: %s: %s\n", argv[0], argv[1], strerror(errno));
return 1;
}
Run Code Online (Sandbox Code Playgroud)
在这里你可以看到仍然可以读取文件:
[jojo@kirk:1] Wed, der 06.03.2013 um 12:58 Uhr Keep Smiling :-)
> ls test
ls: cannot access test: No such file or directory
> echo foo > test
bash: test: Permission denied
> ls test
ls: cannot access test: No such file or directory
> touch test
touch: cannot touch 'test': Permission denied
> head -n 1 no-writes.c # reading still works
/*
Run Code Online (Sandbox Code Playgroud)
除了打开文件之外,它不会阻止删除文件、移动文件或其他文件操作,但可以添加。
无需编写 C 代码即可启用此功能的工具是syscall_limiter。