Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cherry-picked to v0.20.2] alpine: avoid wiping out writable host mounts under /home, etc. #2234

Merged
merged 1 commit into from
Mar 11, 2024

Conversation

AkihiroSuda
Copy link
Member

@AkihiroSuda AkihiroSuda commented Mar 8, 2024

A host directory could be wiped out when all the following conditions are met:

  • The directory is mounted to Lima via virtiofs or 9p (reverse-sshfs is not affected)
  • The mount is writable
  • The mount point in the guest is under one of: /etc /home /root /usr/local /var/lib
  • The guest OS is Alpine Linux

Fix #2221
Fix rancher-sandbox/rancher-desktop#6582

@jandubois
Copy link
Member

  • The directory is mounted to Lima via virtiofs

I have not tested it, but I suspect the same will be true for 9p. The important factor is that the directory is mounted before cloud-init is running, which is true for 9p and virtiofs, but not for reverse-sshfs.

@AkihiroSuda AkihiroSuda marked this pull request as ready for review March 8, 2024 10:11
jandubois added a commit to rancher-sandbox/rancher-desktop-lima that referenced this pull request Mar 8, 2024
Copy link
Member

@jandubois jandubois left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some code that has become redundant with this change and should be removed.

@AkihiroSuda AkihiroSuda mentioned this pull request Mar 9, 2024
@AkihiroSuda AkihiroSuda marked this pull request as draft March 9, 2024 08:29
@AkihiroSuda AkihiroSuda force-pushed the fix-rancher-desktop-6582 branch 2 times, most recently from d4dc678 to f0b317a Compare March 9, 2024 08:47
@AkihiroSuda AkihiroSuda marked this pull request as ready for review March 9, 2024 08:48
@AkihiroSuda AkihiroSuda marked this pull request as draft March 9, 2024 08:52
@AkihiroSuda
Copy link
Member Author

DNM, since fstab is gone, the PR has to be worked out again from scratch

@AkihiroSuda AkihiroSuda force-pushed the fix-rancher-desktop-6582 branch 4 times, most recently from 798f6f0 to 784061d Compare March 9, 2024 10:05
@AkihiroSuda AkihiroSuda marked this pull request as ready for review March 9, 2024 10:20
@AkihiroSuda
Copy link
Member Author

Confirmed that the issue affects 9p as well

@AkihiroSuda AkihiroSuda force-pushed the fix-rancher-desktop-6582 branch 2 times, most recently from 80e8d9e to 99c93b2 Compare March 9, 2024 11:43
@AkihiroSuda
Copy link
Member Author

Added a test

@AkihiroSuda AkihiroSuda force-pushed the fix-rancher-desktop-6582 branch 4 times, most recently from cf056f3 to 994df39 Compare March 9, 2024 12:44
@AkihiroSuda AkihiroSuda force-pushed the fix-rancher-desktop-6582 branch 4 times, most recently from 8caa477 to a8c8bd7 Compare March 9, 2024 18:42
jandubois
jandubois previously approved these changes Mar 11, 2024
Copy link
Member

@jandubois jandubois left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM and worked in my local tests.

I don't think the mounting code will work with mount points that include backslashes, due to the way we have to do the quoting, but I think that is acceptable; who is using backslashes in directory names?

[ "${MNTTYPE}" = "ext4" ] && continue
[ "${MNTTYPE}" = "tmpfs" ] && continue
MNTOPTS="$(echo "${LINE}" | awk '{print $4}')"
echo "mount -t \"${MNTTYPE}\" -o \"${MNTOPTS}\" \"${MNTDEV}\" \"${MNTPNT}\"" >>/mnt.sh
Copy link
Member

@jandubois jandubois Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine to merge as-is. But if you want to support double-quotes and backslashes in mountpoints, then you could to

Suggested change
echo "mount -t \"${MNTTYPE}\" -o \"${MNTOPTS}\" \"${MNTDEV}\" \"${MNTPNT}\"" >>/mnt.sh
MNTPNT=${MNTPNT//\\/\\\\}
MNTPNT=${MNTPNT//\"/\\\"}
echo "mount -t \"${MNTTYPE}\" -o \"${MNTOPTS}\" \"${MNTDEV}\" \"${MNTPNT}\"" >>/mnt.sh

I've tested it with

$ MNTPNT=$'foo\\bar"baz\nfoo\\bar"baz'
$ echo "$MNTPNT"
foo\bar"baz
foo\bar"baz
$ MNTPNT=${MNTPNT//\\/\\\\}
$ MNTPNT=${MNTPNT//\"/\\\"}
$ echo "$MNTPNT"
foo\\bar\"baz
foo\\bar\"baz

If you do that, then you should probably also update the test case to use backslashes and quotes in the test directory name.

Just realized that lima-init.sh in alpine-lima doesn't support backslashes and quotes in mountpoint either right now: https://github.com/rancher-sandbox/alpine-lima/blob/5a55b61512a6e7b4affb8a9779d179f559b0ac18/lima-init.sh#L65-L66

So the tests would fail unless we fix this in alpine-lima as-well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, applied

A host directory could be wiped out when all the following conditions are met:
- The directory is mounted to Lima via virtiofs or 9p (reverse-sshfs is not affected)
- The mount is writable
- The mount point in the guest is under one of: /etc /home /root /usr/local /var/lib
- The guest OS is Alpine Linux

Fix issue 2221
Fix rancher-sandbox/rancher-desktop issue 6582

Co-authored-by: Jan Dubois <[email protected]>
Signed-off-by: Akihiro Suda <[email protected]>
@AkihiroSuda AkihiroSuda merged commit e9423da into lima-vm:master Mar 11, 2024
25 checks passed
@AkihiroSuda AkihiroSuda changed the title alpine: avoid wiping out writable host mounts under /home, etc. [Cherry-picked to v0.20.2] alpine: avoid wiping out writable host mounts under /home, etc. Mar 11, 2024
@AkihiroSuda
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dataloss when using virtiofsd on Linux Lost files on host when canceling limactl start
2 participants