[Devel] [PATCH 06/10] e4defrag2: validate block device at the beginning

Dmitry Monakhov dmonakhov at openvz.org
Thu Feb 25 02:54:29 PST 2016


Signed-off-by: Dmitry Monakhov <dmonakhov at openvz.org>
---
 misc/e4defrag2.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/misc/e4defrag2.c b/misc/e4defrag2.c
index 3699a28..f1db897 100644
--- a/misc/e4defrag2.c
+++ b/misc/e4defrag2.c
@@ -2348,16 +2348,32 @@ static void close_device(char *device_name, ext2_filsys fs)
 		com_err(device_name, retval, "while closing the filesystem");
 }
 
-static void open_device(char *device_name, ext2_filsys *fs)
+static void open_device(char *device_name, ext2_filsys *fs, dev_t devno)
 {
+	struct stat64 st;
 	int retval;
 	int flag = EXT2_FLAG_FORCE | EXT2_FLAG_64BITS;
 
+	retval = stat64(device_name, &st);
+	if (retval < 0) {
+		com_err(device_name, retval, "while opening filesystem image");
+		exit(1);
+	}
+	if (!S_ISBLK(st.st_mode)) {
+		com_err(device_name, retval, "while opening filesystem, block device expected");
+		exit(1);
+	}
+	if (st.st_rdev != devno) {
+		com_err(device_name, retval, "while opening filesystem, wrong block device");
+		exit(1);
+	}
+
 	retval = ext2fs_open(device_name, flag, 0, 0, unix_io_manager, fs);
 	if (retval) {
 		com_err(device_name, retval, "while opening filesystem");
 		exit(1);
 	}
+      
 	(*fs)->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
 }
 
@@ -2470,7 +2486,6 @@ int main(int argc, char *argv[])
 	current_uid = getuid();
 	device_name = argv[optind];
 	root_dir = argv[optind + 1];
-	open_device(device_name, &dfx.fs);
 	dfx.root_fd = open(root_dir, O_RDONLY|O_DIRECTORY);
 	if (dfx.root_fd < 0) {
 		fprintf(stderr, "%s: can not open directory at:%s, errno:%d\n",
@@ -2483,7 +2498,7 @@ int main(int argc, char *argv[])
 			program_name, root_dir);
 		exit(1);
 	}
-
+	open_device(device_name, &dfx.fs, dfx.root_st.st_dev);
 	dfx.blocksize_bits = ul_log2(dfx.root_st.st_blksize);
 	if (cluster_size < dfx.root_st.st_blksize) {
 		fprintf(stderr, "%s: Defragmentation cluster can not be less than fs"
-- 
1.8.3.1



More information about the Devel mailing list