You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/08/12 23:02:47 UTC

svn commit: r803710 - in /commons/sandbox/runtime/trunk/src/main/native: include/acr_shm.h os/unix/shm.c os/win32/shm.c

Author: mturk
Date: Wed Aug 12 21:02:46 2009
New Revision: 803710

URL: http://svn.apache.org/viewvc?rev=803710&view=rev
Log:
Add magic to shared memory for an extra security

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h?rev=803710&r1=803709&r2=803710&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h Wed Aug 12 21:02:46 2009
@@ -32,6 +32,11 @@
  */
 
 /**
+ * Any value will do
+ */
+#define ACR_SHM_MAGIC   0xB2303964
+
+/**
  * Private, platform-specific data struture representing a shared memory
  * segment.
  */

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c?rev=803710&r1=803709&r2=803710&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c Wed Aug 12 21:02:46 2009
@@ -38,6 +38,13 @@
 #define ACR_SHM_OWNER   0
 #define ACR_SHM_CHILD   1
 
+typedef struct memblock_t {
+    acr_uint32_t    magic;       /* Is this our memeory */
+    pid_t           creator;     /* Creator's process ID */
+    acr_size_t      size;
+    acr_size_t      length;
+} memblock_t;
+
 struct acr_shm_t {
     void       *base;           /* base real address */
     void       *usable;         /* base usable address */
@@ -186,6 +193,7 @@
 ACR_DECLARE(int) ACR_ShmCreate(JNIEnv *_E, acr_size_t reqsize,
                                const acr_pchar_t *filename)
 {
+    memblock_t hdr;
     acr_shm_t *shm = NULL;
     struct shmid_ds shmbuf;
     int         file;   /* file where metadata is stored */
@@ -282,9 +290,13 @@
             goto cleanup;
         }
 
-        nbytes = sizeof(reqsize);
+        nbytes = sizeof(memblock_t);
+        hdr.creator = getpid();
+        hdr.magic   = ACR_SHM_MAGIC;
+        hdr.size    = shm->reqsize;
+        hdr.length  = shm->realsize;
         do {
-            rc = write(file,(const void *)&reqsize, nbytes);
+            rc = write(file,(const void *)&hdr, nbytes);
         } while (rc == (acr_size_t)-1 && errno == EINTR);
         if (rc == -1) {
             rc = ACR_GET_OS_ERROR();
@@ -317,6 +329,7 @@
 ACR_DECLARE(int) ACR_ShmAttach(JNIEnv *_E,
                                const acr_pchar_t *filename)
 {
+    memblock_t hdr;
     acr_shm_t *shm = NULL;
     int         file;   /* file where metadata is stored */
     int         rc;
@@ -343,10 +356,10 @@
         goto cleanup;
     }
 
-    nbytes = sizeof(shm->reqsize);
+    nbytes = sizeof(memblock_t);
 
     do {
-        rc = read(file, (void *)&(shm->reqsize), nbytes);
+        rc = read(file, (void *)&hdr, nbytes);
     } while (rc == -1 && errno == EINTR);
     if (rc != nbytes) {
         /* Wrong format */
@@ -358,8 +371,13 @@
     if (rc) {
         goto cleanup;
     }
-
-    shm->shmkey = ftok(filename, 1);
+    if (hdr.magic != ACR_SHM_MAGIC) {
+        /* Not created by us or corrupted */
+        rc = ACR_EBADF;
+        goto cleanup;
+    }
+    shm->reqsize = hdr.size;
+    shm->shmkey  = ftok(filename, 1);
     if (shm->shmkey == (key_t)-1) {
         rc = ACR_GET_OS_ERROR();
         goto cleanup;

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c?rev=803710&r1=803709&r2=803710&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c Wed Aug 12 21:02:46 2009
@@ -27,8 +27,10 @@
 #define ACR_SHM_CHILD   1
 
 typedef struct memblock_t {
-    acr_size_t size;
-    acr_size_t length;
+    acr_uint32_t    magic;       /* Is this our memeory */
+    DWORD           creator;     /* Creator's process ID */
+    acr_size_t      size;
+    acr_size_t      length;
 } memblock_t;
 
 struct acr_shm_t {
@@ -134,6 +136,11 @@
         hfile  = INVALID_HANDLE_VALUE;
         reskey = NULL;
     }
+    else if (_wcsnicmp(filename, L"PAGEFILE:", 9) == 0) {
+        /* Use pagefile instead real file */
+        hfile  = INVALID_HANDLE_VALUE;
+        reskey = res_name_from_filenamew(ACR_DT_SHM, keybuf, filename);
+    }
     /* Name-based shared memory */
     else {
         /* Do file backed, which is not an inherited handle
@@ -193,8 +200,10 @@
     shm->usrmem = (char*)base + sizeof(memblock_t);
     shm->length = reqsize - sizeof(memblock_t);;
 
-    shm->memblk->length = shm->length;
-    shm->memblk->size   = shm->size;
+    shm->memblk->magic   = ACR_SHM_MAGIC;
+    shm->memblk->creator = GetCurrentProcessId();
+    shm->memblk->length  = shm->length;
+    shm->memblk->size    = shm->size;
     if (filename) {
         shm->filename = ACR_StrdupW(_E, THROW_FMARK, filename);
         if (!shm->filename) {