[PATCH 14/14] x86: use read wrappers in kernel loading

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Replace the unsafe read-loops in the x86 kernel image loading
functions with our safe read_file() and read_in_full() wrappers.
This should fix random fails in kernel image loading, especially
from pipes and sockets.

Signed-off-by: Andre Przywara <andre.przywara@xxxxxxx>
---
 x86/kvm.c | 40 +++++++++++++++++++---------------------
 1 file changed, 19 insertions(+), 21 deletions(-)

diff --git a/x86/kvm.c b/x86/kvm.c
index 9817953..8cf4ec6 100644
--- a/x86/kvm.c
+++ b/x86/kvm.c
@@ -9,6 +9,7 @@
 
 #include <asm/bootparam.h>
 #include <linux/kvm.h>
+#include <linux/kernel.h>
 
 #include <sys/types.h>
 #include <sys/ioctl.h>
@@ -209,15 +210,14 @@ static inline void *guest_real_to_host(struct kvm *kvm, u16 selector, u16 offset
 static bool load_flat_binary(struct kvm *kvm, int fd_kernel, void *buf, int len)
 {
 	void *p;
-	int nr;
 
 	p = guest_real_to_host(kvm, BOOT_LOADER_SELECTOR, BOOT_LOADER_IP);
 
 	memcpy(p, buf, len);
 	p += len;
 
-	while ((nr = read(fd_kernel, p, 65536)) > 0)
-		p += nr;
+	if (read_file(fd_kernel, p, kvm->cfg.ram_size) < 0)
+		die_perror("read");
 
 	kvm->arch.boot_selector	= BOOT_LOADER_SELECTOR;
 	kvm->arch.boot_ip	= BOOT_LOADER_IP;
@@ -232,11 +232,9 @@ static bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd,
 			 const char *kernel_cmdline, struct boot_params *boot)
 {
 	struct boot_params *kern_boot;
-	unsigned long setup_sects;
 	size_t cmdline_size;
-	ssize_t setup_size;
+	ssize_t file_size;
 	void *p;
-	int nr;
 	u16 vidmode;
 
 	/*
@@ -250,25 +248,26 @@ static bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd,
 	if (boot->hdr.version < BOOT_PROTOCOL_REQUIRED)
 		die("Too old kernel");
 
+	/* read real-mode setup.bin to boot loader address */
+	p = guest_real_to_host(kvm, BOOT_LOADER_SELECTOR, BOOT_LOADER_IP);
 	if (!boot->hdr.setup_sects)
 		boot->hdr.setup_sects = BZ_DEFAULT_SETUP_SECTS;
-	setup_sects = boot->hdr.setup_sects + 1;
-
-	setup_size = setup_sects << 9;
-	p = guest_real_to_host(kvm, BOOT_LOADER_SELECTOR, BOOT_LOADER_IP);
+	file_size = (boot->hdr.setup_sects + 1) << 9;
 
-	/* copy setup.bin to mem */
+	/* copy in the part already read earlier from the file */
 	memcpy(p, boot, sizeof(struct boot_params));
 	p += sizeof(struct boot_params);
-	setup_size -= sizeof(struct boot_params);
-	if (read(fd_kernel, p, setup_size) != setup_size)
-		die_perror("read");
+	file_size -= sizeof(struct boot_params);
 
-	/* copy vmlinux.bin to BZ_KERNEL_START*/
-	p = guest_flat_to_host(kvm, BZ_KERNEL_START);
+	if (read_in_full(fd_kernel, p, file_size) != file_size)
+		die_perror("kernel setup read");
 
-	while ((nr = read(fd_kernel, p, 65536)) > 0)
-		p += nr;
+	/* read actual kernel image (vmlinux.bin) to BZ_KERNEL_START */
+	p = guest_flat_to_host(kvm, BZ_KERNEL_START);
+	file_size = read_file(fd_kernel, p,
+			      kvm->cfg.ram_size - BZ_KERNEL_START);
+	if (file_size < 0)
+		die_perror("kernel read");
 
 	p = guest_flat_to_host(kvm, BOOT_CMDLINE_OFFSET);
 	if (kernel_cmdline) {
@@ -319,8 +318,7 @@ static bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd,
 		}
 
 		p = guest_flat_to_host(kvm, addr);
-		nr = read(fd_initrd, p, initrd_stat.st_size);
-		if (nr != initrd_stat.st_size)
+		if (read_in_full(fd_initrd, p, initrd_stat.st_size) < 0)
 			die("Failed to read initrd");
 
 		kern_boot->hdr.ramdisk_image	= addr;
@@ -343,7 +341,7 @@ bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd,
 {
 	struct boot_params boot;
 
-	if (read(fd_kernel, &boot, sizeof(boot)) != sizeof(boot))
+	if (read_in_full(fd_kernel, &boot, sizeof(boot)) != sizeof(boot))
 		return false;
 
 	if (load_bzimage(kvm, fd_kernel, fd_initrd, kernel_cmdline, &boot))
-- 
2.3.5

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux