Bug 11636 - switch_root: fix to zap initramfs's sub-directories for the mount point of new root file system
Summary: switch_root: fix to zap initramfs's sub-directories for the mount point of ne...
Status: NEW
Alias: None
Product: Busybox
Classification: Unclassified
Component: Other (show other bugs)
Version: 1.30.x
Hardware: All Linux
: P5 enhancement
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-01-24 15:34 UTC by abo junghichi
Modified: 2019-01-24 15:34 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description abo junghichi 2019-01-24 15:34:03 UTC
As Rob seid in the comment of util-linux/switch_root.c, current imprementation doesn't rmdir() sub-directory for the mount point of new root-file-system.
This patch fixes this by zapping old root-file-system after remounting new root-file-system to "/".
Since current process of "/" still points old root-file-system until chroot() is initiated, we can still access and zap ititramfs after mount point of new root-file-system is moved from sub-directory of initramfs.

diff -Naur busybox-1.30.0.org/util-linux/switch_root.c busybox-1.30.0.fix/util-linux/switch_root.c
--- busybox-1.30.0.org/util-linux/switch_root.c	2018-12-31 00:14:20.000000000 +0900
+++ busybox-1.30.0.fix/util-linux/switch_root.c	2019-01-25 00:20:57.078879499 +0900
@@ -257,14 +257,14 @@
 	}
 
 	if (!dry_run) {
-		// Zap everything out of rootdev
-		delete_contents("/", rootdev);
-
 		// Overmount / with newdir and chroot into it
 		if (mount(".", "/", NULL, MS_MOVE, NULL)) {
 			// For example, fails when newroot is not a mountpoint
 			bb_perror_msg_and_die("error moving root");
 		}
+
+		// Zap everything out of rootdev
+		delete_contents("/", rootdev);
 	}
 	xchroot(".");
 	// The chdir is needed to recalculate "." and ".." links