Skip to content

Commit 50105de

Browse files
committed
Fix failure with rw bind mount of a ro fuse
As reported in [1], in a case where read-only fuse (sshfs) mount is used as a volume without specifying ro flag, the kernel fails to remount it (when adding various flags such as nosuid and nodev), returning EPERM. Here's the relevant strace line: > [pid 333966] mount("/tmp/bats-run-PRVfWc/runc.RbNv8g/bundle/mnt", "/proc/self/fd/7", 0xc0001e9164, MS_NOSUID|MS_NODEV|MS_REMOUNT|MS_BIND|MS_REC, NULL) = -1 EPERM (Operation not permitted) I was not able to reproduce it with other read-only mounts as the source (tried tmpfs, read-only bind mount, and an ext2 mount), so somehow this might be specific to fuse. The fix is to check whether the source has RDONLY flag, and retry the remount with this flag added. A test case (which was kind of hard to write) is added, and it fails without the fix. Note that rootless user need to be able to ssh to rootless@localhost in order to sshfs to work -- amend setup scripts to make it work, and skip the test if the setup is not working. [1] containers/podman#12205 Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 20e9288 commit 50105de

File tree

6 files changed

+71
-7
lines changed

6 files changed

+71
-7
lines changed

.cirrus.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ task:
9999
# Work around dnf mirror failures by retrying a few times.
100100
for i in $(seq 0 2); do
101101
sleep $i
102-
yum install -y -q gcc git iptables jq glibc-static libseccomp-devel make criu && break
102+
yum install -y -q gcc git iptables jq glibc-static libseccomp-devel make criu fuse-sshfs && break
103103
done
104104
[ $? -eq 0 ] # fail if yum failed
105105
# install Go
@@ -113,6 +113,12 @@ task:
113113
cd -
114114
# Add a user for rootless tests
115115
useradd -u2000 -m -d/home/rootless -s/bin/bash rootless
116+
# Allow root and rootless itself to execute `ssh rootless@localhost` in tests/rootless.sh
117+
ssh-keygen -t ecdsa -N "" -f /root/rootless.key
118+
mkdir -m 0700 -p /home/rootless/.ssh
119+
cp /root/rootless.key /home/rootless/.ssh/id_ecdsa
120+
cat /root/rootless.key.pub >> /home/rootless/.ssh/authorized_keys
121+
chown -R rootless.rootless /home/rootless
116122
# set PATH
117123
echo 'export PATH=/usr/local/go/bin:/usr/local/bin:$PATH' >> /root/.bashrc
118124
# Setup ssh localhost for terminal emulation (script -e did not work)

.github/workflows/test.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ jobs:
4646
curl -fSsl $REPO/Release.key | sudo apt-key add -
4747
echo "deb $REPO/ /" | sudo tee /etc/apt/sources.list.d/criu.list
4848
sudo apt update
49-
sudo apt install libseccomp-dev criu
49+
sudo apt install libseccomp-dev criu sshfs
5050
5151
- name: install deps (criu ${{ matrix.criu }})
5252
if: matrix.criu != ''
5353
run: |
5454
sudo apt -q update
55-
sudo apt -q install libseccomp-dev \
55+
sudo apt -q install libseccomp-dev sshfs \
5656
libcap-dev libnet1-dev libnl-3-dev \
5757
libprotobuf-c-dev libprotobuf-dev protobuf-c-compiler protobuf-compiler
5858
git clone https://github.com/checkpoint-restore/criu.git ~/criu
@@ -81,9 +81,10 @@ jobs:
8181
if: matrix.rootless == 'rootless'
8282
run: |
8383
sudo useradd -u2000 -m -d/home/rootless -s/bin/bash rootless
84-
# Allow root to execute `ssh rootless@localhost` in tests/rootless.sh
84+
# Allow root and rootless itself to execute `ssh rootless@localhost` in tests/rootless.sh
8585
ssh-keygen -t ecdsa -N "" -f $HOME/rootless.key
8686
sudo mkdir -m 0700 -p /home/rootless/.ssh
87+
sudo cp $HOME/rootless.key /home/rootless/.ssh/id_ecdsa
8788
sudo cp $HOME/rootless.key.pub /home/rootless/.ssh/authorized_keys
8889
sudo chown -R rootless.rootless /home/rootless
8990

Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ RUN KEYFILE=/usr/share/keyrings/criu-repo-keyring.gpg; \
3131
kmod \
3232
pkg-config \
3333
python3-minimal \
34+
sshfs \
3435
sudo \
3536
uidmap \
3637
&& apt-get clean \

Vagrantfile.fedora

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Vagrant.configure("2") do |config|
2727
cat << EOF | dnf -y --exclude=kernel,kernel-core,systemd,systemd-* shell && break
2828
config install_weak_deps false
2929
update
30-
install iptables gcc make golang-go glibc-static libseccomp-devel bats jq git-core criu
30+
install iptables gcc make golang-go glibc-static libseccomp-devel bats jq git-core criu fuse-sshfs
3131
ts run
3232
EOF
3333
done
@@ -36,9 +36,10 @@ EOF
3636
# Add a user for rootless tests
3737
useradd -u2000 -m -d/home/rootless -s/bin/bash rootless
3838
39-
# Allow root to execute `ssh rootless@localhost` in tests/rootless.sh
39+
# Allow root and rootless itself to execute `ssh rootless@localhost` in tests/rootless.sh
4040
ssh-keygen -t ecdsa -N "" -f /root/rootless.key
4141
mkdir -m 0700 -p /home/rootless/.ssh
42+
cp /root/rootless.key /home/rootless/.ssh/id_ecdsa
4243
cat /root/rootless.key.pub >> /home/rootless/.ssh/authorized_keys
4344
chown -R rootless.rootless /home/rootless
4445

libcontainer/rootfs_linux.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,22 @@ func remount(m *configs.Mount, rootfs string, mountFd *int) error {
10651065
}
10661066

10671067
return utils.WithProcfd(rootfs, m.Destination, func(procfd string) error {
1068-
return mount(source, m.Destination, procfd, m.Device, uintptr(m.Flags|unix.MS_REMOUNT), "")
1068+
flags := uintptr(m.Flags | unix.MS_REMOUNT)
1069+
err := mount(source, m.Destination, procfd, m.Device, flags, "")
1070+
if err == nil {
1071+
return nil
1072+
}
1073+
// Check if the source has ro flag...
1074+
var s unix.Statfs_t
1075+
if err := unix.Statfs(source, &s); err != nil {
1076+
return &os.PathError{Op: "statfs", Path: source, Err: err}
1077+
}
1078+
if s.Flags&unix.MS_RDONLY != unix.MS_RDONLY {
1079+
return err
1080+
}
1081+
// ... and retry the mount with ro flag set.
1082+
flags |= unix.MS_RDONLY
1083+
return mount(source, m.Destination, procfd, m.Device, flags, "")
10691084
})
10701085
}
10711086

tests/integration/mounts_sshfs.bats

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bats
2+
3+
load helpers
4+
5+
function setup() {
6+
# Create a ro fuse-sshfs mount; skip the test if it's not working.
7+
local sshfs="sshfs
8+
-o UserKnownHostsFile=/dev/null
9+
-o StrictHostKeyChecking=no
10+
-o PasswordAuthentication=no"
11+
12+
DIR="$BATS_RUN_TMPDIR/fuse-sshfs"
13+
mkdir -p "$DIR"
14+
15+
if ! $sshfs -o ro rootless@localhost: "$DIR"; then
16+
skip "test requires working sshfs mounts"
17+
fi
18+
19+
setup_hello
20+
}
21+
22+
function teardown() {
23+
# New distros (Fedora 35) do not have fusermount installed
24+
# as a dependency of fuse-sshfs, and good ol' umount works.
25+
fusermount -u "$DIR" || umount "$DIR"
26+
27+
teardown_bundle
28+
}
29+
30+
@test "runc run [rw bind mount of a ro fuse sshfs mount]" {
31+
update_config ' .mounts += [{
32+
type: "bind",
33+
source: "'"$DIR"'",
34+
destination: "/mnt",
35+
options: ["rw", "rprivate", "nosuid", "nodev", "rbind"]
36+
}]'
37+
38+
runc run test_busybox
39+
[ "$status" -eq 0 ]
40+
}

0 commit comments

Comments
 (0)