Fix safechoot unmount ordering. (#6708)

This commit is contained in:
Chris Gunn 2023-11-09 12:17:51 -08:00 коммит произвёл GitHub
Родитель ed7226875b
Коммит 0fce2cd03d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 9 добавлений и 15 удалений

Просмотреть файл

@ -8,7 +8,6 @@ import (
"os"
"os/signal"
"path/filepath"
"sort"
"sync"
"time"
@ -265,19 +264,11 @@ func (c *Chroot) Initialize(tarPath string, extraDirectories []string, extraMoun
}
}
// Mount with the original unsorted order. Assumes the order of mounts is important.
err = c.createMountPoints(allMountPoints)
// Sort the mount points by target directory
// This way nested mounts will be correctly unraveled:
// e.g.: /dev/pts is unmounted and then /dev is.
//
// Sort now before checking err so that `unmountAndRemove` can be called from Initialize.
// Assign to `c.mountPoints` now since `Initialize` will call `unmountAndRemove` if an error occurs.
c.mountPoints = allMountPoints
sort.Slice(c.mountPoints, func(i, j int) bool {
return c.mountPoints[i].target > c.mountPoints[j].target
})
// Mount with the original unsorted order. Assumes the order of mounts is important.
err = c.createMountPoints()
if err != nil {
logger.Log.Warn("Error creating mountpoints for chroot")
return
@ -530,7 +521,10 @@ func (c *Chroot) unmountAndRemove(leaveOnDisk, lazyUnmount bool) (err error) {
unmountFlags = unmountFlagsLazy
}
for _, mountPoint := range c.mountPoints {
// Unmount in the reverse order of mounting to ensure that any nested mounts are unraveled in the correct order.
for i := len(c.mountPoints) - 1; i >= 0; i-- {
mountPoint := c.mountPoints[i]
fullPath := filepath.Join(c.rootDir, mountPoint.target)
var exists bool
@ -632,8 +626,8 @@ func (c *Chroot) restoreRoot(originalRoot, originalWd *os.File) {
}
// createMountPoints will create a provided list of mount points
func (c *Chroot) createMountPoints(allMountPoints []*MountPoint) (err error) {
for _, mountPoint := range allMountPoints {
func (c *Chroot) createMountPoints() (err error) {
for _, mountPoint := range c.mountPoints {
fullPath := filepath.Join(c.rootDir, mountPoint.target)
logger.Log.Debugf("Mounting: source: (%s), target: (%s), fstype: (%s), flags: (%#x), data: (%s)",
mountPoint.source, fullPath, mountPoint.fstype, mountPoint.flags, mountPoint.data)