Public bug reported:

When several firejail sandboxes start at the same moment and
/run/firejail/firejail.ro.dir is not mounted yet, each of them bind-
mounts that directory on itself. /run is a shared mount, so the mounts
stack.

Every later firejail start reads the mount table several times, so every
start gets slower, for every sandbox on the machine, until the mounts
are removed or the machine reboots.

Upstream fixed this in April 2024: commit e53b6d66b, "modif: populate
/run/firejail while holding flock", pull request
https://github.com/netblue30/firejail/pull/6307, first released in
0.9.74. The commit message describes the same symptom. Ubuntu 24.04
ships 0.9.72-2ubuntu3, which does not have it.

Affected version

- Ubuntu 24.04.5 LTS (noble), firejail 0.9.72-2ubuntu3, arm64. This is where I 
saw it and reproduced it.
- Launchpad lists 0.9.72 for plucky, questing and resolute too. I have not 
tested those and have not read their patches.

What happened on a real machine

A server starts about twenty firejail sandboxes together at boot. After
one boot, /run/firejail/firejail.ro.dir was mounted 10,835 times. Each
firejail start then took more than 30 seconds; strace showed the time
going into reading the mount table. Sandboxed services timed out for 13
hours until the cause was found. "umount -R
/run/firejail/firejail.ro.dir" took 80 seconds and brought a start back
to 0.06 seconds.

Why it happens (from reading the source)

In 0.9.72, main() calls preproc_build_firejail_dir() and only afterwards
opens and locks /run/firejail/firejail-run.lock. Inside it,
fs_remount(RUN_RO_DIR, MOUNT_READONLY, 0) checks whether the directory
is already read-only and, if not, bind-mounts it on itself. Two
processes that both pass the check before either has mounted will both
mount.

In current upstream the same remount is in
preproc_build_firejail_dir_locked(), called between
preproc_lock_firejail_dir() and preproc_unlock_firejail_dir().
fs_remount_simple() is identical in 0.9.72 and in upstream master; the
change is the lock around it.

Later upstream commits touch the same lock and may matter for a
backport: 53bc65897 (improve flock handling), f4b8c6dbb (possible
deadlock with flock and SIGTSTP, #6750), 7471a24b8 (non-blocking flock
calls, #6761).

How to reproduce

The attached repro.sh works inside a private mount namespace, so the
host's mount table is not touched. Each round removes every mount of the
directory, releases N sandboxes at the same instant, and counts the
mounts.

 sudo unshare -m --propagation private bash repro.sh <user> 60 5

The race is intermittent. On a busy 4-core arm64 machine, one round of
five stacked mounts:

 firejail version 0.9.72
 round 1: 60 parallel starts from zero mounts -> 1 mounts of 
/run/firejail/firejail.ro.dir
 round 2: 60 parallel starts from zero mounts -> 3 mounts of 
/run/firejail/firejail.ro.dir
 round 3: 60 parallel starts from zero mounts -> 1 mounts of 
/run/firejail/firejail.ro.dir
 round 4: 60 parallel starts from zero mounts -> 1 mounts of 
/run/firejail/firejail.ro.dir
 round 5: 60 parallel starts from zero mounts -> 1 mounts of 
/run/firejail/firejail.ro.dir
 now one mount is left in place and 60 more sandboxes start together:
 with one mount already there -> 1 mounts

A version of the script that started the sandboxes in a plain loop,
without releasing them together, did not reproduce in seven rounds of 20
and 40 starts.

Once one mount is in place, a further wave of parallel starts leaves the
count at 1. That is why the problem appears at boot and then persists.

Expected: 1 mount after every round. Seen: sometimes more than 1, and on
the real machine 10,835.

Workaround

- Start one sandbox alone at boot, before anything starts many together.
- To recover: sudo umount -R /run/firejail/firejail.ro.dir. Running sandboxes 
are not affected.
The reproducer, a boot unit and a repair script are at 
https://github.com/fernandogzp/firejail-mount-pileup

Request
Please consider backporting upstream PR #6307 to firejail in noble, or updating 
the package to a release that has it.

** Affects: firejail (Ubuntu)
     Importance: Undecided
         Status: New

** Attachment added: "repro.sh"
   https://bugs.launchpad.net/bugs/2185802/+attachment/6023841/+files/repro.sh

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2185802

Title:
  firejail 0.9.72: parallel starts stack mounts on
  /run/firejail/firejail.ro.dir (fixed upstream in 0.9.74, PR #6307)

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/firejail/+bug/2185802/+subscriptions


-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to