DPDK patches and discussions
 help / color / mirror / Atom feed
From: Michal Jastrzebski <michalx.k.jastrzebski@intel.com>
To: bruce.richardson@intel.com
Cc: dev@dpdk.org, michalx.kobylinski@intel.com,
	sergio.gonzalez.monroy@intel.com, david.marchand@6wind.com,
	thomas.monjalon@6wind.com
Subject: [dpdk-dev] [PATCH v2] eal: fix check number of bytes from read function
Date: Fri, 22 Jul 2016 16:33:50 +0200	[thread overview]
Message-ID: <1469198030-8664-1-git-send-email-michalx.k.jastrzebski@intel.com> (raw)
In-Reply-To: <1469024689-1076-1-git-send-email-michalx.k.jastrzebski@intel.com>

v2:
-moved close(fd) just after read.
-when read() from fd we expect 8 bytes, so PFN_MASK_SIZE macro 
was introduced instead sizeof(uint64_t).
-removed errno print when read returns less than 8 bytes

In rte_mem_virt2phy: Value returned from a function and indicating the
number of bytes was ignored. This could cause a wrong pfn (page frame
number) mask read from pagemap file.
When read returns less than the number of sizeof(uint64_t) bytes,
function rte_mem_virt2phy returns error.

Coverity issue: 13212
Fixes: 40b966a211ab ("ivshmem: library changes for mmaping using
ivshmem").

Signed-off-by: Michal Jastrzebski <michalx.k.jastrzebski@intel.com>
---
 lib/librte_eal/linuxapp/eal/eal_memory.c |   19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/lib/librte_eal/linuxapp/eal/eal_memory.c b/lib/librte_eal/linuxapp/eal/eal_memory.c
index 42a29fa..e20a38c 100644
--- a/lib/librte_eal/linuxapp/eal/eal_memory.c
+++ b/lib/librte_eal/linuxapp/eal/eal_memory.c
@@ -99,6 +99,8 @@
 #include "eal_filesystem.h"
 #include "eal_hugepages.h"
 
+#define PFN_MASK_SIZE	8
+
 #ifdef RTE_LIBRTE_XEN_DOM0
 int rte_xen_dom0_supported(void)
 {
@@ -158,7 +160,7 @@ rte_mem_lock_page(const void *virt)
 phys_addr_t
 rte_mem_virt2phy(const void *virtaddr)
 {
-	int fd;
+	int fd, retval;
 	uint64_t page, physaddr;
 	unsigned long virt_pfn;
 	int page_size;
@@ -209,10 +211,19 @@ rte_mem_virt2phy(const void *virtaddr)
 		close(fd);
 		return RTE_BAD_PHYS_ADDR;
 	}
-	if (read(fd, &page, sizeof(uint64_t)) < 0) {
+
+	retval = read(fd, &page, sizeof(uint64_t));
+	close(fd);
+	if (retval < 0) {
 		RTE_LOG(ERR, EAL, "%s(): cannot read /proc/self/pagemap: %s\n",
 				__func__, strerror(errno));
-		close(fd);
+
+		return RTE_BAD_PHYS_ADDR;
+	} else if (retval != PFN_MASK_SIZE) {
+		RTE_LOG(ERR, EAL, "%s(): read %d bytes from /proc/self/pagemap "
+				"but expected %d:\n",
+				__func__, retval, PFN_MASK_SIZE);
+
 		return RTE_BAD_PHYS_ADDR;
 	}
 
@@ -222,7 +233,7 @@ rte_mem_virt2phy(const void *virtaddr)
 	 */
 	physaddr = ((page & 0x7fffffffffffffULL) * page_size)
 		+ ((unsigned long)virtaddr % page_size);
-	close(fd);
+
 	return physaddr;
 }
 
-- 
1.7.9.5

  parent reply	other threads:[~2016-07-22 14:34 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-20 14:24 [dpdk-dev] [PATCH] " Michal Jastrzebski
2016-07-21 14:35 ` Thomas Monjalon
2016-07-21 20:50   ` Jastrzebski, MichalX K
2016-07-21 23:09     ` Thomas Monjalon
2016-07-21 14:37 ` Sergio Gonzalez Monroy
2016-07-22 14:38   ` Jastrzebski, MichalX K
2016-07-22 14:33 ` Michal Jastrzebski [this message]
2016-07-22 15:24   ` [dpdk-dev] [PATCH v2] " Thomas Monjalon
2016-07-22 16:02     ` Sergio Gonzalez Monroy
2016-07-22 16:22       ` Thomas Monjalon
2016-07-22 16:23     ` Jastrzebski, MichalX K

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=1469198030-8664-1-git-send-email-michalx.k.jastrzebski@intel.com \
    --to=michalx.k.jastrzebski@intel.com \
    --cc=bruce.richardson@intel.com \
    --cc=david.marchand@6wind.com \
    --cc=dev@dpdk.org \
    --cc=michalx.kobylinski@intel.com \
    --cc=sergio.gonzalez.monroy@intel.com \
    --cc=thomas.monjalon@6wind.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).