Firejail

来自 Arch Linux 中文维基

Firejail 是一个易于使用的 SUID 沙盒程序,它通过使用 Linux 命名空间、seccomp-bpf 和 Linux 功能来限制不受信任的应用程序的运行环境以降低安全漏洞被利用的风险。

警告: 运行不受信任的代码永远不安全,沙盒并不能挽救这个局面。

安装[编辑 | 编辑源代码]

安装 firejail 或者 firejail-gitAUR 包. 还有可以与 Firejail 一起使用的 GUI 应用程序, firetools.

注意: 有关 Arch Linux 内核中的 user_namespaces(7) 支持的信息,请参阅 Security#Sandboxing applications. Firejail即使被禁用也可以使用
警告: 虽然上游正在逐步采用白名单(参见/etc/firejail/firefox.profile),但大多数提供的配置文件仍然严重依赖黑名单。这意味着,任何没有被配置文件明确禁止的东西都会被应用程序访问。例如,如果你在/mnt/btrfs中有 btrfs 快照,沙盒里的程序可能被禁止访问$HOME/.ssh,但仍能访问/mnt/btrfs/@some-snapshot/$HOME/.ssh。请确保检查您的配置文件。见#测试配置文件

配置[编辑 | 编辑源代码]

大多数用户不需要任何自定义配置,并且可以继续 #使用方法.

Firejail 使用配置文件为其中执行的每个应用程序设置安全保护 - 您可以在 /etc/firejail/application.profile 中找到默认配置文件. 如果您需要为未包含的应用程序自定义配置文件,或者希望修改默认值, 您可以在 ~/.config/firejail/ 目录中放置新规则或默认值副本. 一个应用程序可能有多个自定义配置文件,并且您可以在多个应用程序之间共享同一个配置文件.

如果 firejail 没有特定应用程序的配置文件,它会使用其限制性的系统范围默认配置文件。如果没有事先创建自定义且限制较少的配置文件,这可能会导致应用程序无法按预期运行。

参阅 firejail-profile(5)

使用方法[编辑 | 编辑源代码]

要使用 firejail 对该应用程序的默认保护(默认配置文件)执行应用程序,请执行以下命令 :

$ firejail <应用程序名称>

一次性添加到默认配置文件可以作为命令行选项添加(参见 firejail(1))。 例如,要执行带有 seccomp 保护的 okular,请执行以下命令 :

$ firejail --seccomp okular

您可以为单个程序定义多个非默认配置文件。 创建配置文件后,您可以通过执行来使用它 :

$ firejail --profile=/absolute/path/to/profile <program name>

默认配置使用 Firejail[编辑 | 编辑源代码]

默认情况下将 Firejail 用于具有配置文件的所有应用程序 , 使用 sudo 运行 firecfg 工具:

$ sudo firecfg

这将在/usr/local/bin中创建指向/usr/bin/firejail,用于 Firejail 有默认或自定配置文件的程序的符号链接。请注意,firecfg(1) 只与/etc/firejail/firecfg.config中列出的程序建立符号链接。某些命令行界面的程序是不存在的,例如 tarcurlgit。这些程序需要手动建立符号链接。请参阅 Profiles not in firecfg #2507 了解为什么不包括这些程序。firecfg 还将当前用户加入 Firejail 用户访问数据库,并检查/usr/share/applications/*.desktop文件是否包含相应可执行文件的完整路径,然后删除完整路径并将其复制到~/.local/share/applications/。这确保了/usr/local/bin中的符号链接将被使用,从而防止 Firejail 被绕过。如果您的系统中没有安装 sudo,请以 root 身份执行:

# firecfg

以及以一般用户执行:

$ firecfg --fix

以修复 .desktop 文件

在某些情况下,您可能需要手动修改~/.local/share/applications/中 ".desktop " 文件的Exec=行,以直接调用 Firejail。

提示:可使用 pacman hook来在 pacman 操作中自动运行firecfg
/etc/pacman.d/hooks/firejail.hook
[Trigger]
Type = Path
Operation = Install
Operation = Upgrade
Operation = Remove
Target = usr/bin/*
Target = usr/local/bin/*
Target = usr/share/applications/*.desktop

[Action]
Description = Configure symlinks in /usr/local/bin based on firecfg.config...
When = PostTransaction
Depends = firejail
Exec = /bin/sh -c 'firecfg >/dev/null 2>&1'

要手动链接各个应用程序,请执行以下操作:

# ln -s /usr/bin/firejail /usr/local/bin/application
注意:
  • /usr/local/bin must be set before /usr/bin and /bin in the PATH environment variable.
  • To run a symbolic program with custom Firejail setting, simple prefix firejail as seen in #使用方法.
  • For a daemon, you will need to overwrite the systemd unit file for that daemon to call firejail, see systemd#Editing provided units.
  • Symbolic links to gzip and xz interfere with makepkg's ability to preload libfakeroot.so. See BBS#230913.

Use with hardened_malloc[编辑 | 编辑源代码]

hardened_mallocAUR is a hardened implementation of glibc's malloc() allocator, originally written for Android but extended for use on the desktop. While not integrated into glibc yet, it can be used selectively with LD_PRELOAD. The proper way to launch an application within firejail using hardened_malloc is demonstrated below. To make it permanent, you would need to create your own entry in /usr/local/bin for the desired application.

$ firejail --env=LD_PRELOAD='/usr/lib/libhardened_malloc.so' /usr/bin/firefox

Alternatively, add the following to a custom profile:

env LD_PRELOAD='/usr/lib/libhardened_malloc.so'

The various environment variables and settings that can be used to tune hardened_malloc can be found on its github page.

Enable AppArmor support[编辑 | 编辑源代码]

Since 0.9.60-1, Firejail has supported more direct integration with AppArmor through a generic AppArmor profile. During installation, the profile, firejail-default, is placed in /etc/apparmor.d directory, and needs to be loaded into the kernel by running the following command as root:

# apparmor_parser -r /etc/apparmor.d/firejail-default

See firejail(1) § APPARMOR.

Local customizations of the apparmor profile are supported by editing the file /etc/apparmor.d/local/firejail-local

AppArmor is already enabled for a large number of Firejail profiles. There are several ways to enable AppArmor confinement on top of a Firejail security profile:

  • Pass the --apparmor flag to Firejail in the command line, e.g. $ firejail --apparmor firefox
  • Use a custom profile and add the apparmor command.
  • Enable Apparmor globally in /etc/firejail/globals.local and disable as needed through the use of ignore apparmor in /etc/firejail/<ProgramName>.local.

Note that enabling AppArmor by above methods always means that /etc/apparmor.d/firejail-default is used. If you rather want to use a specific AppArmor profile for an application, you have to use the above mentioned ignore apparmor command. However, that is not recommended, as using both Firejail and AppArmor for the same applications often creates problems.

Verifying Firejail is being used[编辑 | 编辑源代码]

$ firejail --list

A more comprehensive output is produced by

$ firejail --tree

Creating custom profiles[编辑 | 编辑源代码]

Whitelists and blacklists[编辑 | 编辑源代码]

Blacklists are heavily used in various /etc/firejail/*.inc files which are included in most profiles. Blacklists are permissive:

  • Deny access to a directory or file and permit everything else: blacklist <directory/file>
  • Disable/undo/ignore blacklisting a directory or file already blacklisted, e.g., in an *.inc file: noblacklist <directory/file>

The order in which they appear in a profile is important: noblacklist directives must be added above blacklist directives.

Whitelists block everything what is not explicitly whitelisted. They should not be used in profiles for applications that need access to random locations (e.g., text editors, image viewers/editors).

  • Allow access to a directory or file and forbid everything else: whitelist <directory/file>
  • Disable/undo/ignore whitelisting a directory or file already whitelisted, e.g., in an *.inc file: nowhitelist <directory/file>

The order in which they appear in a profile is important: nowhitelist directives must be added above whitelist directives.

Whitelisting is always done before blacklisting. As mentioned, a whitelist directive blacklists everything else. A blacklist directive is therefore a fallback if there are no whitelist directives or if a whitelist directive is too permissive.

(no)blacklist and (no)whitelist directives are often used in combination. Example: /etc/firejail/disable-programs.inc (which is included in all profiles) contains the directive:

blacklist ${HOME}/.mozilla

in order to block access to that directory for all applications sandboxed by Firejail. /etc/firejail/firefox.profile must disable this directive and must add a whitelist directive to allow access to that directory (as the Firefox profile is a whitelisted profile):

noblacklist ${HOME}/.mozilla
whitelist ${HOME}/.mozilla

Profile writing[编辑 | 编辑源代码]

The basic process is:

  1. Copy /usr/share/doc/firejail/profile.template to /etc/firejail/ or ~/.config/firejail/ and rename it to ProfileName.profile where ProfileName should match the name of the executable to be sandboxed
  2. Change the line include PROFILE.local to include ProfileName.local
  3. Gradually comment/uncomment the various options while checking at each stage that the application runs inside the new sandbox. Do not change the order of the sections in that template.
  4. Detailed explanations of the possible options for a Firejail profile can be found in the firejail-profile(5) man page
  5. Test the profile for security holes, see #Testing profiles

If you want to create a whitelisted profile (i.e. a profile which contains whitelist directives) you can build a whitelist of permitted locations by executing

$ firejail --build application

Keep in mind that a whitelisted profile is problematic for applications that need to access random locations (like text editors or file managers).

注意:
  • The idea is to be as restrictive as possible, while still maintaining usability. This may involve sacrificing potentially dangerous functionality and a change in cavalier work habits.
  • By default, seccomp filters work on a blacklist (which can be found in /usr/share/doc/firejail/syscalls.txt). It is possible to use seccomp.keep to build a custom whitelist of filters for an application. [1]. A convenient way to automate these steps is to execute /usr/lib/firejail/syscalls.sh. If the application is still broken because of missing syscalls, you should follow the instructions at the bottom of /usr/share/doc/firejail/syscalls.txt.

Persistent local customisation[编辑 | 编辑源代码]

The standard profile layout includes the capability to make persistent local customisations through the inclusion of .local files[2]. Basically, each officially supported profile contains the lines include ProgramName.local and include globals.local. These *.local files might be located in /etc/firejail or in ~/.config/firejail. Since the order of precedence is determined by which is read first, this makes for a very powerful way of making local customisations. For example, with reference this firejail question, to globally enable Apparmor and disable Internet connectivity, one could simply create/edit /etc/firejail/globals.local to include the lines

# enable Apparmor and disable Internet globally
net none
apparmor

Then, to allow, for example, "curl" to connect to the internet, yet still maintain its apparmor confinement, one would create/edit /etc/firejail/curl.local to include the lines.

# enable internet for curl
ignore net

Since curl.local is read before globals.local, ignore net overrides net none, and, as a bonus, the above changes would be persistent across future updates.

Testing profiles[编辑 | 编辑源代码]

In order to test and audit a Firejail profile you may find the following to be useful:

  1. firejail --debug $Program > $PathToOutputFile Gives a detailed breakdown of the sandbox
  2. firejail --debug-blacklists $Program and firejail --debug-whitelists $Program show the blacklisted and whitelisted directories and files for the current profile.
  3. firejail --debug-caps gives a list of caps supported by the current Firejail software build. This is useful when building a caps whitelist.
  4. firejail --help for a full list of --debug options
  5. firemon PID monitors the running process. See firemon --help for details
  6. Executing sudo jailcheck tests running sandboxes. See the jailcheck(1) man page for details.
  7. checksec may also be useful in testing which standard security features are being used

Firejail with Xorg[编辑 | 编辑源代码]

本文或本章节的事实准确性存在争议。

原因: Why does sandboxing X11 require DNS?(在 Talk:Firejail 中讨论)


On Xorg any program can listen to all keyboard input and record all screens. The purpose of sandboxing X11 is to restrict this behavior, which is especially problematic for complex programs working with potentially malicious input like browsers.

Xephyr and Xpra allow you to sandbox Xorg. Although Xpra provides full clipboard support, it is recommended to use Xephyr due to the very notable and permanent lag with nested X11 sessions.

For a complete setup with (not ideal) clipboard support (clipboard is still always shared), see Sakaki's Gentoo guide, especially the section about the clipboard and automatic rescaling.

Alternatively, if clipboard support is not needed but windows need to be managed, install a standalone window manager such as Openbox.

xephyr-screen WidthxHeight can be set in /etc/firejail/firejail.config where Width and Height are in pixels and based on your screen resolution.

To open the sandbox:

$ firejail --x11 --net=device openbox

device is your active network interface, which is needed to ensure that DNS works. Then right click and select your applications to run.

注意: If you use Unbound, dnsmasq, Pdnsd or any other local resolver on 127.0.0.1, you would leave --net=device out of the command as DNS should work automatically.

See the Firejail Wordpress site for a simpler guide.

According to the guide:

The sandbox replaces the regular X11 server with Xpra or Xephyr server. This prevents X11 keyboard loggers and screenshot utilities from accessing the main X11 server.

Note that the statement:

The only way to disable the abstract socket @/tmp/.X11-unix/X0 is by using a network namespace. If for any reasons you cannot use a network namespace, the abstract socket will still be visible inside the sandbox. Hackers can attach keylogger and screenshot programs to this socket.

is incorrect, xserverrc can be edited to -nolisten local, which disables the abstract sockets of X11 and helps isolate it.

Sandboxing a browser[编辑 | 编辑源代码]

Openbox can be configured to start a certain browser at startup. program.profile is the respective profile contained in /etc/firejail, and --startup "command" is the command line used to start the program. For example, to start Chromium in the sandbox:

$ firejail --x11 --profile=/etc/firejail/chromium.profile openbox --startup "chromium"

Tips and tricks[编辑 | 编辑源代码]

Hardening Firejail[编辑 | 编辑源代码]

The security risk of Firejail being a SUID executable can be mitigated by adding the line

force-nonewprivs yes

to /etc/firejail/firejail.config. However, this can break specific applications. On Arch Linux, VirtualBox doesn't start anymore. With the linux-hardened kernel Wireshark and Chromium-based browsers are also affected.

Further hardening measures include creating a special firejail group with adding the user to that group and changing the file mode for the firejail executable. For details see here.

Paths containing spaces[编辑 | 编辑源代码]

If you need to reference, whitelist, or blacklist a directory within a custom profile, such as with palemoonAUR, you must do so using the absolute path, without encapsulation or escapes:

/home/user/.moonchild productions

Private mode[编辑 | 编辑源代码]

Firejail also includes a one time private mode, in which no mounts are made in the chroots to your home directory. In doing this, you can execute applications without performing any changes to disk. For example, to execute okular in private mode, do the following:

$ firejail --seccomp --private okular

Experimental improved tools[编辑 | 编辑源代码]

Some of the Firejail developers recognized issues with the tools it ships with and made their own, improved versions of them.

警告: As stated in some of the projects descriptions, these tools might be experimental.

Troubleshooting[编辑 | 编辑源代码]

Firejail can be hard to debug. The symptoms of a misconfigured or otherwise unfitting setup range from random segmentation faults and hangs in the applications to simple error messages.

Some applications are harder to sandbox than others. For example web browsers and Electron applications tend to need more troubleshooting than others since there is much that can go wrong. It is crucial to check the FAQ and open issues first, since debugging can take quite some time.

提示:Also see the upstream wiki, especially its page on debugging Firejail.

Remove Firejail symbolic links[编辑 | 编辑源代码]

To remove Firejail created symbolic links (e.g. reset to default):

# firecfg --clean

If you do not want to use Firejail for a specific application (e.g., because you prefer to rather confine it with AppArmor), you have to manually remove the related symbolic link:

# rm /usr/local/bin/application

As a subsequent execution of firecfg would re-add the removed symlinks, the respective applications should be commented in /etc/firejail/firecfg.config.

Verify if any leftovers of Desktop entries are still overruled by Firejail.

PulseAudio[编辑 | 编辑源代码]

注意: Using PulseAudio version 9.0 or later should fix this issue.

If Firejail causes PulseAudio issues with sandboxed applications [3], the following command may be used:

$ firecfg --fix-sound

This commands creates a custom ~/.config/pulse/client.conf file for the current user with enable-shm = no and possible other workarounds.

hidepid[编辑 | 编辑源代码]

If the system uses the hidepid 内核参数, Firemon can only be run as root. This, among other things, will cause problems with the Firetools GUI incorrectly reporting "Capabilities", "Protocols" and the status of "Seccomp"[4].

Proprietary Nvidia drivers[编辑 | 编辑源代码]

Some users report problems when using Firejail and proprietary graphic drivers from NVIDIA (e.g. [5], [6] or [7]). This can often be solved by disabling the noroot Firejail option in the application's profile file.

--net options and Linux kernel >=4.20.0[编辑 | 编辑源代码]

There is a bug on firejail 0.5.96 with linux >= 4.20.0, see [8] and [9]

Example error message:

$ firejail --noprofile --net=eth0 ls
Parent pid 8521, child pid 8522
Error send: arp.c:182 arp_check: Invalid argument
Error: proc 8521 cannot sync with peer: unexpected EOF
Peer 8522 unexpectedly exited with status 1

Warning: Cannot confine the application using AppArmor[编辑 | 编辑源代码]

本文或本章节可能需要合并到#Enable AppArmor support

附注: The requirement to enable AppArmor should be made explicit in the previous text. And note that Manjaro references are irrelevant.(在 Talk:Firejail 中讨论)

For some applications (e.g. Firefox [10]) starting with Firejail may result in warnings like:

Warning: Cannot confine the application using AppArmor.
Maybe firejail-default AppArmor profile is not loaded into the kernel.
As root, run "aa-enforce firejail-default" to load it.

When running the suggested command you might see:

ERROR: Cache read/write disabled: interface file missing. (Kernel needs AppArmor 2.4 compatibility patch.)

This means that AppArmor is not enabled as a kernel parameter, so you have to set it according to AppArmor#Installation.

/usr/bin/patch: **** Can't open patch file[编辑 | 编辑源代码]

This means the PKGBUILD uses patch with the -i argument so a whitelist for $SRCDEST in /etc/makepkg.conf is needed.

Create the override patch.local with the value of your $SRCDEST:

whitelist /path/to/makepkg/sources

Changing the PKGBUILD to use stdin also works:

patch -p1 < ../file.patch

Graphical applications hang on start with AMDGPU[编辑 | 编辑源代码]

Some graphical applications, e.g. Firefox and mpv, will hang on start when using AMDGPU with Mesa >= 19.3.4. See [11]. The issue is fixed upstream, so firejail-gitAUR should work. Alternatively, for all affected applications, add seccomp !kcmp to their profiles in etc/firejail. If they already have a seccomp statement, you can concatenate them as a comma-separated list, eg seccomp !chroot,!kcmp.

Daemonizing/backgrounded processes hang[编辑 | 编辑源代码]

There is a known issue that prevents processes from daemonizing. There is currently no solution to this except not using Firejail to sandbox the affected application. Because it is a bug within Firejail, no configuration can solve this issue. Fortunately the applications mentioned in the issue usually do not have a large attack surface, so the risks of running them without a sandbox are comparatively low.

See also[编辑 | 编辑源代码]