All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andre Przywara <andre.przywara@arm.com>
To: will.deacon@arm.com, kvm@vger.kernel.org
Cc: marc.zyngier@arm.com, kvmarm@lists.cs.columbia.edu,
	kvm-ppc@vger.kernel.org
Subject: [PATCH 10/14] provide generic read_file() implementation
Date: Thu, 30 Jul 2015 11:52:27 +0100	[thread overview]
Message-ID: <1438253551-2378-11-git-send-email-andre.przywara@arm.com> (raw)
In-Reply-To: <1438253551-2378-1-git-send-email-andre.przywara@arm.com>

In various parts of kvmtool we simply try to read files into memory,
but fail to do so in a safe way. The read(2) syscall can return early
having only parts of the file read, or it may return -1 due to being
interrupted by a signal (in which case we should simply retry).
The ARM code seems to provide the only safe implementation, so take
that as an inspiration to provide a generic read_file() function
usable by every part of kvmtool.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
---
 include/kvm/read-write.h |  2 ++
 util/read-write.c        | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/include/kvm/read-write.h b/include/kvm/read-write.h
index 87eb167..658a653 100644
--- a/include/kvm/read-write.h
+++ b/include/kvm/read-write.h
@@ -12,6 +12,8 @@
 ssize_t xread(int fd, void *buf, size_t count);
 ssize_t xwrite(int fd, const void *buf, size_t count);
 
+ssize_t read_file(int fd, char *buf, size_t max_size);
+
 ssize_t read_in_full(int fd, void *buf, size_t count);
 ssize_t write_in_full(int fd, const void *buf, size_t count);
 
diff --git a/util/read-write.c b/util/read-write.c
index 401afd3..32691a9 100644
--- a/util/read-write.c
+++ b/util/read-write.c
@@ -32,6 +32,27 @@ restart:
 	return nr;
 }
 
+/*
+ * Read in the whole file while not exceeding max_size bytes of the buffer.
+ * Returns -1 (with errno set) in case of an error (ENOMEM if buffer was
+ * too small) or the filesize if the whole file could be read.
+ */
+ssize_t read_file(int fd, char *buf, size_t max_size)
+{
+	ssize_t ret;
+	char dummy;
+
+	errno = 0;
+	ret = read_in_full(fd, buf, max_size);
+
+	/* Probe whether we reached EOF. */
+	if (xread(fd, &dummy, 1) == 0)
+		return ret;
+
+	errno = ENOMEM;
+	return -1;
+}
+
 ssize_t read_in_full(int fd, void *buf, size_t count)
 {
 	ssize_t total = 0;
-- 
2.3.5

WARNING: multiple messages have this Message-ID (diff)
From: Andre Przywara <andre.przywara@arm.com>
To: will.deacon@arm.com, kvm@vger.kernel.org
Cc: marc.zyngier@arm.com, kvmarm@lists.cs.columbia.edu,
	kvm-ppc@vger.kernel.org
Subject: [PATCH 10/14] provide generic read_file() implementation
Date: Thu, 30 Jul 2015 10:52:27 +0000	[thread overview]
Message-ID: <1438253551-2378-11-git-send-email-andre.przywara@arm.com> (raw)
In-Reply-To: <1438253551-2378-1-git-send-email-andre.przywara@arm.com>

In various parts of kvmtool we simply try to read files into memory,
but fail to do so in a safe way. The read(2) syscall can return early
having only parts of the file read, or it may return -1 due to being
interrupted by a signal (in which case we should simply retry).
The ARM code seems to provide the only safe implementation, so take
that as an inspiration to provide a generic read_file() function
usable by every part of kvmtool.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
---
 include/kvm/read-write.h |  2 ++
 util/read-write.c        | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/include/kvm/read-write.h b/include/kvm/read-write.h
index 87eb167..658a653 100644
--- a/include/kvm/read-write.h
+++ b/include/kvm/read-write.h
@@ -12,6 +12,8 @@
 ssize_t xread(int fd, void *buf, size_t count);
 ssize_t xwrite(int fd, const void *buf, size_t count);
 
+ssize_t read_file(int fd, char *buf, size_t max_size);
+
 ssize_t read_in_full(int fd, void *buf, size_t count);
 ssize_t write_in_full(int fd, const void *buf, size_t count);
 
diff --git a/util/read-write.c b/util/read-write.c
index 401afd3..32691a9 100644
--- a/util/read-write.c
+++ b/util/read-write.c
@@ -32,6 +32,27 @@ restart:
 	return nr;
 }
 
+/*
+ * Read in the whole file while not exceeding max_size bytes of the buffer.
+ * Returns -1 (with errno set) in case of an error (ENOMEM if buffer was
+ * too small) or the filesize if the whole file could be read.
+ */
+ssize_t read_file(int fd, char *buf, size_t max_size)
+{
+	ssize_t ret;
+	char dummy;
+
+	errno = 0;
+	ret = read_in_full(fd, buf, max_size);
+
+	/* Probe whether we reached EOF. */
+	if (xread(fd, &dummy, 1) = 0)
+		return ret;
+
+	errno = ENOMEM;
+	return -1;
+}
+
 ssize_t read_in_full(int fd, void *buf, size_t count)
 {
 	ssize_t total = 0;
-- 
2.3.5


  parent reply	other threads:[~2015-07-30 10:52 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-30 10:52 [PATCH 00/14] kvmtool: Refactor kernel image loading to allow pipes Andre Przywara
2015-07-30 10:52 ` Andre Przywara
2015-07-30 10:52 ` [PATCH 01/14] Refactor kernel image loading Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 02/14] arm/powerpc: remove unneeded seeks in kernel loading Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 03/14] x86: allow pipes for bzImage kernel images Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 04/14] x86: support loading flat binary kernel images from a pipe Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 05/14] kvmtool: introduce pseek Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 06/14] MIPS: use pseek() in ELF kernel image loading Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 07/14] MIPS: move ELF headers loading outside of load_elf_binary() Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 08/14] MIPS: remove seeks from load_flat_binary() Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 09/14] arm: move kernel loading into arm/kvm.c Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` Andre Przywara [this message]
2015-07-30 10:52   ` [PATCH 10/14] provide generic read_file() implementation Andre Przywara
2015-07-30 10:52 ` [PATCH 11/14] arm/arm64: use read_file() in kernel and initrd loading Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 12/14] powerpc: " Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 13/14] MIPS: use read wrappers in kernel loading Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 14/14] x86: " Andre Przywara
2015-07-30 10:52   ` Andre Przywara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1438253551-2378-11-git-send-email-andre.przywara@arm.com \
    --to=andre.przywara@arm.com \
    --cc=kvm-ppc@vger.kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.cs.columbia.edu \
    --cc=marc.zyngier@arm.com \
    --cc=will.deacon@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.