Skip to contentSkip to conversation

Fix mmap return checking

PATCH In review Don Slutz 2 participants 8 messages Jul 28, 2014 – Jul 31, 2014

lore
Share & tools
lore

Don Slutz

author
#0 lore raw

cover Fix mmap return checking

On Linux MAP_FAILED is not the same as NULL.

Since lots of other calls check for MAP_FAILED, fix to use MAP_FAILED.

tools/blktap/drivers/tapdisk.c does check for -1 not MAP_FAILED. I did not change since it was not clearly wrong.

Don Slutz (3):
loadpolicy.c: Fix return handling for case of mmap failure libxl_internal.c: Fix return handling for case of mmap failure xenbaked.c: Fix return handling for case of mmap failure

Harry Hart (1):
libxc/xc_linux_osdep.c: Fix return handling for case of mmap failure

 tools/flask/utils/loadpolicy.c | 2 +-
 tools/libxc/xc_linux_osdep.c   | 5 +++++
 tools/libxl/libxl_internal.c   | 2 +-
 tools/xenmon/xenbaked.c        | 2 +-
 4 files changed, 8 insertions(+), 3 deletions(-)
Signature · 1 line
1.8.4
author
#1 lore raw

PATCH 1/4 libxc/xc_linux_osdep.c: Fix return handling for case of mmap failure

From: Harry Hart <[email protected]>

Callers expect NULL on errors not MAP_FAILED.

---
 tools/libxc/xc_linux_osdep.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/libxc/xc_linux_osdep.c +5 -0
index 86bff3e..9745feb 100644
@@ -102,6 +102,11 @@ static void *linux_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_ha
         return NULL;
     }
 
+    if (p == MAP_FAILED) {
+        PERROR ("linux_privcmd_alloc_hypercall_buffer: mmap failed");
+        return NULL;
+    }
+
     /* Do not copy the VMA to child process on fork. Avoid the page being COW
         on hypercall. */
     rc = madvise(p, npages * XC_PAGE_SIZE, MADV_DONTFORK);
Signature · 1 line
1.8.4
#2 lore raw

Re: [PATCH 1/4] libxc/xc_linux_osdep.c: Fix return handling for case of mmap failure

Quoted 15 lines · Don Slutz

From: Harry Hart <[email protected]>

Callers expect NULL on errors not MAP_FAILED.

---
 tools/libxc/xc_linux_osdep.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/libxc/xc_linux_osdep.c +0 -0
index 86bff3e..9745feb 100644

tools/libxc/xc_linux_osdep.c static void *linux_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_ha · line 108

Quoted 7 lines
tools/libxc/xc_linux_osdep.c +4 -0
@@ -102,6 +102,11 @@ static void *linux_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_ha
         return NULL;
     }
 
+    if (p == MAP_FAILED) {
+        PERROR ("linux_privcmd_alloc_hypercall_buffer: mmap failed");
+        return NULL;
+    }

The return NULL just above in the context is the same check as this. Rebasing mistake?

author
#3 lore raw

Re: [PATCH 1/4] libxc/xc_linux_osdep.c: Fix return handling for case of mmap failure

Quoted 19 lines · Ian Campbell
Quoted 18 lines · Don Slutz

From: Harry Hart <[email protected]>

Callers expect NULL on errors not MAP_FAILED.

---
  tools/libxc/xc_linux_osdep.c | 5 +++++
  1 file changed, 5 insertions(+)
tools/libxc/xc_linux_osdep.c +0 -0
index 86bff3e..9745feb 100644
@@ -102,6 +102,11 @@ static void *linux_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_ha
          return NULL;
      }
 
Quoted 9 lines

tools/libxc/xc_linux_osdep.c static void *linux_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_ha · line 108

Quoted 4 lines
tools/libxc/xc_linux_osdep.c +4 -0
+    if (p == MAP_FAILED) {
+        PERROR ("linux_privcmd_alloc_hypercall_buffer: mmap failed");
+        return NULL;
+    }

The return NULL just above in the context is the same check as this. Rebasing mistake?

Yup.

-Don Slutz

author
#4 lore raw

PATCH 2/4 loadpolicy.c: Fix return handling for case of mmap failure

In reply to Don Slutz

mmap() returns MAP_FAILED not NULL.

---
 tools/flask/utils/loadpolicy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/flask/utils/loadpolicy.c +1 -1
index 2edcf97..47b5139 100644
@@ -61,7 +61,7 @@ int main (int argCnt, const char *args[])
 
 #ifdef USE_MMAP
     polMem = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, polFd, 0);
-    if ( !polMem )
+    if ( polMem == MAP_FAILED )
     {
         fprintf(stderr, "Error occurred mapping policy file in memory: %s\n",
                 strerror(errno));
Signature · 1 line
1.8.4
author
#5 lore raw

PATCH 3/4 libxl_internal.c: Fix return handling for case of mmap failure

In reply to Don Slutz

mmap() returns MAP_FAILED not NULL.

---
 tools/libxl/libxl_internal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/libxl_internal.c +1 -1
index 81f8985..b880c89 100644
@@ -246,7 +246,7 @@ int libxl__file_reference_map(libxl__file_reference *f)
 
     ret = -1;
     data = mmap(NULL, st_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-    if (data == NULL)
+    if (data == MAP_FAILED)
         goto out;
 
     f->mapped = 1;
Signature · 1 line
1.8.4
author
#6 lore raw

PATCH 4/4 xenbaked.c: Fix return handling for case of mmap failure

In reply to Don Slutz

mmap() returns MAP_FAILED not NULL.

---
 tools/xenmon/xenbaked.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/xenmon/xenbaked.c +1 -1
index 985f1dd..dc61d14 100644
@@ -674,7 +674,7 @@ static void alloc_qos_data(int ncpu)
         new_qos = (_new_qos_data *) mmap(0, sizeof(_new_qos_data), PROT_READ|PROT_WRITE, 
                                          MAP_SHARED, qos_fd, off);
         off += i;
-        if (new_qos == NULL) {
+        if (new_qos == MAP_FAILED) {
             PERROR("mmap");
             exit(3);
         }
Signature · 1 line
1.8.4
#7 lore raw
In reply to Don Slutz
Quoted 7 lines · Don Slutz

On Linux MAP_FAILED is not the same as NULL.

Since lots of other calls check for MAP_FAILED, fix to use MAP_FAILED.

tools/blktap/drivers/tapdisk.c does check for -1 not MAP_FAILED. I did not change since it was not clearly wrong.

I've acked + applied #2..#4. I replied to #1 which I think is wrong.

Ian.