Bug 11636

Summary: switch_root: fix to zap initramfs's sub-directories for the mount point of new root file system
Product: Busybox Reporter: abo junghichi <abo_junghichi>
Component: OtherAssignee: unassigned
Status: NEW ---    
Severity: enhancement CC: busybox-cvs
Priority: P5    
Version: 1.30.x   
Target Milestone: ---   
Hardware: All   
OS: Linux   
Host: Target:
Build:

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