components/coolkey/patches/02-machdep.cpp.patch
author Stephen Gaul Jr <steve.gaul@oracle.com>
Tue, 12 Jul 2016 17:34:11 -0700
changeset 6401 8e624b116c1d
permissions -rw-r--r--
PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices 22017764 Add Coolkey v1.1.0 to Userland consolidation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6401
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     1
Upstream fixes already included in the latest updates to coolkey v1.1.0
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     2
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     3
Addresses cache directory, memory leak and compiler issues.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     4
Added MAP_FILE definiton for Solaris.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     5
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     6
--- ORIGINAL/./src/coolkey/machdep.cpp	2016-06-24 16:07:19.527102431 -0400
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     7
+++ ././src/coolkey/machdep.cpp	2016-06-27 13:36:57.607064368 -0400
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     8
@@ -33,8 +33,18 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     9
 #include <sys/stat.h>
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    10
 #include <sys/mman.h>
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    11
 #include <pthread.h>
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    12
+#include <string.h>
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    13
+#include <stdlib.h>
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    14
 #endif
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    15
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    16
+bool OSLock::needThread = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    17
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    18
+// Solaris specific - MAP_FILE needs to be defined
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    19
+#ifndef	MAP_FILE
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    20
+#define MAP_FILE	0
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    21
+#endif
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    22
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    23
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    24
 #ifdef _WIN32
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    25
 //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    26
 // Windows functions to grab a named shared memory segment of a specific size,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    27
@@ -121,6 +131,10 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    28
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    29
 OSLock::OSLock(bool exceptionAllowed)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    30
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    31
+    if (!needThread) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    32
+	lockData = NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    33
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    34
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    35
     lockData = new OSLockData;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    36
     if (lockData) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    37
 	InitializeCriticalSection(&lockData->mutex);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    38
@@ -185,12 +199,20 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    39
 #define MAP_INHERIT 0
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    40
 #endif
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    41
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    42
+#ifndef BASEPATH
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    43
+#ifdef MAC
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    44
+#define BASEPATH "/var"
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    45
+#else
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    46
+#define BASEPATH "/var/cache"
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    47
+#endif
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    48
+#endif
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    49
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    50
 #ifdef FULL_CLEANUP
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    51
 #define RESERVED_OFFSET 256
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    52
-#define MEMSEGPATH "/tmp/.pk11ipc"
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    53
+#define MEMSEGPATH BASEPATH"/coolkey-lock"
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    54
 #else 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    55
 #define RESERVED_OFFSET 0
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    56
-#define MEMSEGPATH "/tmp/.pk11ipc1"
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    57
+#define MEMSEGPATH BASEPATH"/coolkey"
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    58
 #endif
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    59
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    60
 struct SHMemData {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    61
@@ -208,11 +230,6 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    62
 #ifdef FULL_CLEANUP
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    63
 	flock(fd,LOCK_EX);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    64
 	unsigned long ref = --(*(unsigned long *)addr); 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    65
-#ifdef notdef
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    66
-	if (ref == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    67
-	    unlink(path);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    68
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    69
-#endif
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    70
 	flock(fd, LOCK_UN);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    71
 #endif
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    72
 	munmap(addr,size+RESERVED_OFFSET);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    73
@@ -225,6 +242,73 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    74
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    75
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    76
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    77
+/*
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    78
+ * The cache directory is shared and accessible by anyone, make
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    79
+ * sure the cache file we are opening is really a valid cache file.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    80
+ */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    81
+int safe_open(char *path, int flags, int mode, int size)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    82
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    83
+    struct stat buf;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    84
+    int fd, ret;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    85
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    86
+    fd = open (path, flags|O_NOFOLLOW, mode);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    87
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    88
+    if (fd < 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    89
+	return fd;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    90
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    91
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    92
+    ret = fstat(fd, &buf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    93
+    if (ret < 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    94
+	close (fd);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    95
+	return ret;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    96
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    97
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    98
+    /* our cache files are pretty specific, make sure we are looking
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    99
+     * at the correct one */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   100
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   101
+    /* first, we should own the file ourselves, don't open a file
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   102
+     * that someone else wanted us to see. */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   103
+    if (buf.st_uid != getuid()) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   104
+	close(fd);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   105
+	errno = EACCES;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   106
+	return -1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   107
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   108
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   109
+    /* next, there should only be one link in this file. Don't
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   110
+     * use this code to trash another file */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   111
+    if (buf.st_nlink != 1) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   112
+	close(fd);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   113
+	errno = EMLINK;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   114
+	return -1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   115
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   116
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   117
+    /* next, This better be a regular file */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   118
+    if (!S_ISREG(buf.st_mode)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   119
+	close(fd);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   120
+	errno = EACCES;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   121
+	return -1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   122
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   123
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   124
+    /* if the permissions don't match, something is wrong */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   125
+    if ((buf.st_mode & 03777) != mode) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   126
+	close(fd);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   127
+	errno = EACCES;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   128
+	return -1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   129
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   130
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   131
+    /* finally the file should be the correct size. This 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   132
+     * check isn't so much to protect from an attack, as it is to
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   133
+     * detect a corrupted cache file */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   134
+    if (buf.st_size != size) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   135
+	close(fd);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   136
+	errno = EACCES;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   137
+	return -1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   138
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   139
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   140
+    /* OK, the file checked out, ok to continue */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   141
+    return fd;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   142
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   143
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   144
 SHMem::SHMem(): shmemData(0) {}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   145
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   146
 SHMem *
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   147
@@ -248,7 +332,7 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   148
 	return NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   149
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   150
     int mask = umask(0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   151
-    int ret = mkdir (MEMSEGPATH, 0777);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   152
+    int ret = mkdir (MEMSEGPATH, 01777);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   153
     umask(mask);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   154
     if ((ret == -1) && (errno != EEXIST)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   155
 	delete shmemData;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   156
@@ -264,21 +348,16 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   157
     shmemData->path[sizeof(MEMSEGPATH)-1] = '/';
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   158
     strcpy(&shmemData->path[sizeof(MEMSEGPATH)],name);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   159
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   160
-    int mode = 0777;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   161
-    if (strcmp(name,"token_names") != 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   162
-	/* each user gets his own uid array */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   163
-    	sprintf(uid_str, "-%u",getuid());
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   164
-    	strcat(shmemData->path,uid_str);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   165
-	mode = 0700;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   166
-    } 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   167
+    sprintf(uid_str, "-%u",getuid());
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   168
+    strcat(shmemData->path,uid_str);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   169
+    int mode = 0600;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   170
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   171
     shmemData->fd = open(shmemData->path, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   172
 		O_CREAT|O_RDWR|O_EXCL|O_APPEND|O_EXLOCK, mode);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   173
-    if (shmemData->fd  < 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   174
-	needInit = false;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   175
-	shmemData->fd = open(shmemData->path,O_RDWR|O_EXLOCK, mode);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   176
-    }  else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   177
+    if (shmemData->fd >= 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   178
 	char *buf;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   179
 	int len = size+RESERVED_OFFSET;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   180
+	int ret;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   181
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   182
 	buf = (char *)calloc(1,len);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   183
 	if (!buf) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   184
@@ -289,8 +368,23 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   185
 	    delete shmemData;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   186
 	    return NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   187
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   188
-	write(shmemData->fd,buf,len);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   189
+	ret = write(shmemData->fd,buf,len);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   190
+	if (ret != len) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   191
+	    unlink(shmemData->path);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   192
+#ifdef FULL_CLEANUP
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   193
+	    flock(shmemData->fd, LOCK_UN);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   194
+#endif
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   195
+	    free(buf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   196
+	    delete shmemData;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   197
+	    return NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   198
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   199
+	
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   200
 	free(buf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   201
+    } else if (errno == EEXIST) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   202
+	needInit = false;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   203
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   204
+	shmemData->fd = safe_open(shmemData->path,O_RDWR|O_EXLOCK, mode,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   205
+				  size+RESERVED_OFFSET);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   206
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   207
     if (shmemData->fd < 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   208
 	delete shmemData;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   209
@@ -358,6 +452,9 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   210
     int rc;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   211
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   212
     lockData = NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   213
+    if (!needThread) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   214
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   215
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   216
 #ifdef MAC
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   217
     if (!OSLock_attr_init) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   218
 	rc = pthread_mutexattr_init(&OSLock_attr);