summarylogtreecommitdiffstats
path: root/0001-switch_root-verify-initramfs-by-f_type-not-devno.patch
blob: 56191a30e7607e090fde4eeabe575c8b22915ee1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
From 751c39383adaf5ff5a860516238d524b0e20f835 Mon Sep 17 00:00:00 2001
From: Dave Reisner <dreisner@archlinux.org>
Date: Wed, 2 Apr 2014 10:41:30 -0400
Subject: [PATCH] switch_root: verify initramfs by f_type, not devno

As of linux 3.14, the initramfs device will have both major and
minor 0, causing our paranoia check to fail. Make this version agnostic
by checking the filesystem type, rather than a device number.

[adopted from master for backport into 2.24.x branch]

Signed-off-by: Dave Reisner <dreisner@archlinux.org>
---
 sys-utils/switch_root.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/sys-utils/switch_root.c b/sys-utils/switch_root.c
index f26f7da..40e222d 100644
--- a/sys-utils/switch_root.c
+++ b/sys-utils/switch_root.c
@@ -23,6 +23,7 @@
 #include <sys/mount.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/statfs.h>
 #include <sys/param.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -33,6 +34,8 @@
 #include <ctype.h>
 #include <dirent.h>
 
+#include <linux/magic.h>
+
 #include "c.h"
 #include "nls.h"
 #include "closestream.h"
@@ -174,12 +177,12 @@ static int switchroot(const char *newroot)
 	if (cfd >= 0) {
 		pid = fork();
 		if (pid <= 0) {
-			if (fstat(cfd, &sb) == 0) {
-				if (sb.st_dev == makedev(0, 1))
-					recursiveRemove(cfd);
-				else
-					warn(_("old root filesystem is not an initramfs"));
-			}
+			struct statfs stfs;
+			if (fstatfs(cfd, &stfs) == 0 &&
+			    (stfs.f_type == RAMFS_MAGIC || stfs.f_type == TMPFS_MAGIC))
+				recursiveRemove(cfd);
+			else
+				warn(_("old root filesystem is not an initramfs"));
 
 			if (pid == 0)
 				exit(EXIT_SUCCESS);
-- 
1.9.1