Blame SOURCES/coolkey-cache-dir-move.patch

ed074c
Index: src/coolkey/machdep.cpp
ed074c
===================================================================
ed074c
RCS file: /cvs/dirsec/coolkey/src/coolkey/machdep.cpp,v
ed074c
retrieving revision 1.4
ed074c
diff -u -r1.4 machdep.cpp
ed074c
--- src/coolkey/machdep.cpp	14 Feb 2007 00:46:28 -0000	1.4
ed074c
+++ src/coolkey/machdep.cpp	15 Aug 2007 01:41:11 -0000
ed074c
@@ -185,12 +185,20 @@
ed074c
 #define MAP_INHERIT 0
ed074c
 #endif
ed074c
 
ed074c
+#ifndef BASEPATH
ed074c
+#ifdef MAC
ed074c
+#define BASEPATH "/var"
ed074c
+#else
ed074c
+#define BASEPATH "/var/cache"
ed074c
+#endif
ed074c
+#endif
ed074c
+
ed074c
 #ifdef FULL_CLEANUP
ed074c
 #define RESERVED_OFFSET 256
ed074c
-#define MEMSEGPATH "/tmp/.pk11ipc"
ed074c
+#define MEMSEGPATH BASEPATH"/coolkey-lock"
ed074c
 #else 
ed074c
 #define RESERVED_OFFSET 0
ed074c
-#define MEMSEGPATH "/tmp/.pk11ipc1"
ed074c
+#define MEMSEGPATH BASEPATH"/coolkey"
ed074c
 #endif
ed074c
 
ed074c
 struct SHMemData {
ed074c
@@ -208,11 +216,6 @@
ed074c
 #ifdef FULL_CLEANUP
ed074c
 	flock(fd,LOCK_EX);
ed074c
 	unsigned long ref = --(*(unsigned long *)addr); 
ed074c
-#ifdef notdef
ed074c
-	if (ref == 0) {
ed074c
-	    unlink(path);
ed074c
-	}
ed074c
-#endif
ed074c
 	flock(fd, LOCK_UN);
ed074c
 #endif
ed074c
 	munmap(addr,size+RESERVED_OFFSET);
ed074c
@@ -225,6 +228,73 @@
ed074c
     }
ed074c
 }
ed074c
 
ed074c
+/*
ed074c
+ * The cache directory is shared and accessible by anyone, make
ed074c
+ * sure the cache file we are opening is really a valid cache file.
ed074c
+ */
ed074c
+int safe_open(char *path, int flags, int mode, int size)
ed074c
+{
ed074c
+    struct stat buf;
ed074c
+    int fd, ret;
ed074c
+
ed074c
+    fd = open (path, flags|O_NOFOLLOW, mode);
ed074c
+
ed074c
+    if (fd < 0) {
ed074c
+	return fd;
ed074c
+    }
ed074c
+
ed074c
+    ret = fstat(fd, &buf;;
ed074c
+    if (ret < 0) {
ed074c
+	close (fd);
ed074c
+	return ret;
ed074c
+    }
ed074c
+
ed074c
+    /* our cache files are pretty specific, make sure we are looking
ed074c
+     * at the correct one */
ed074c
+
ed074c
+    /* first, we should own the file ourselves, don't open a file
ed074c
+     * that someone else wanted us to see. */
ed074c
+    if (buf.st_uid != getuid()) {
ed074c
+	close(fd);
ed074c
+	errno = EACCES;
ed074c
+	return -1;
ed074c
+    }
ed074c
+
ed074c
+    /* next, there should only be one link in this file. Don't
ed074c
+     * use this code to trash another file */
ed074c
+    if (buf.st_nlink != 1) {
ed074c
+	close(fd);
ed074c
+	errno = EMLINK;
ed074c
+	return -1;
ed074c
+    }
ed074c
+
ed074c
+    /* next, This better be a regular file */
ed074c
+    if (!S_ISREG(buf.st_mode)) {
ed074c
+	close(fd);
ed074c
+	errno = EACCES;
ed074c
+	return -1;
ed074c
+    }
ed074c
+
ed074c
+    /* if the permissions don't match, something is wrong */
ed074c
+    if ((buf.st_mode & 03777) != mode) {
ed074c
+	close(fd);
ed074c
+	errno = EACCES;
ed074c
+	return -1;
ed074c
+    }
ed074c
+
ed074c
+    /* finally the file should be the correct size. This 
ed074c
+     * check isn't so much to protect from an attack, as it is to
ed074c
+     * detect a corrupted cache file */
ed074c
+    if (buf.st_size != size) {
ed074c
+	close(fd);
ed074c
+	errno = EACCES;
ed074c
+	return -1;
ed074c
+    }
ed074c
+
ed074c
+    /* OK, the file checked out, ok to continue */
ed074c
+    return fd;
ed074c
+}
ed074c
+
ed074c
 SHMem::SHMem(): shmemData(0) {}
ed074c
 
ed074c
 SHMem *
ed074c
@@ -248,7 +318,7 @@
ed074c
 	return NULL;
ed074c
     }
ed074c
     int mask = umask(0);
ed074c
-    int ret = mkdir (MEMSEGPATH, 0777);
ed074c
+    int ret = mkdir (MEMSEGPATH, 01777);
ed074c
     umask(mask);
ed074c
     if ((ret == -1) && (errno != EEXIST)) {
ed074c
 	delete shmemData;
ed074c
@@ -264,21 +334,16 @@
ed074c
     shmemData->path[sizeof(MEMSEGPATH)-1] = '/';
ed074c
     strcpy(&shmemData->path[sizeof(MEMSEGPATH)],name);
ed074c
 
ed074c
-    int mode = 0777;
ed074c
-    if (strcmp(name,"token_names") != 0) {
ed074c
-	/* each user gets his own uid array */
ed074c
-    	sprintf(uid_str, "-%u",getuid());
ed074c
-    	strcat(shmemData->path,uid_str);
ed074c
-	mode = 0700;
ed074c
-    } 
ed074c
+    sprintf(uid_str, "-%u",getuid());
ed074c
+    strcat(shmemData->path,uid_str);
ed074c
+    int mode = 0600;
ed074c
+
ed074c
     shmemData->fd = open(shmemData->path, 
ed074c
 		O_CREAT|O_RDWR|O_EXCL|O_APPEND|O_EXLOCK, mode);
ed074c
-    if (shmemData->fd  < 0) {
ed074c
-	needInit = false;
ed074c
-	shmemData->fd = open(shmemData->path,O_RDWR|O_EXLOCK, mode);
ed074c
-    }  else {
ed074c
+    if (shmemData->fd >= 0) {
ed074c
 	char *buf;
ed074c
 	int len = size+RESERVED_OFFSET;
ed074c
+	int ret;
ed074c
 
ed074c
 	buf = (char *)calloc(1,len);
ed074c
 	if (!buf) {
ed074c
@@ -289,8 +354,22 @@
ed074c
 	    delete shmemData;
ed074c
 	    return NULL;
ed074c
 	}
ed074c
-	write(shmemData->fd,buf,len);
ed074c
+	ret = write(shmemData->fd,buf,len);
ed074c
+	if (ret != len) {
ed074c
+	    unlink(shmemData->path);
ed074c
+#ifdef FULL_CLEANUP
ed074c
+	    flock(shmemData->fd, LOCK_UN);
ed074c
+#endif
ed074c
+	    delete shmemData;
ed074c
+	    return NULL;
ed074c
+	}
ed074c
+	
ed074c
 	free(buf);
ed074c
+    } else if (errno == EEXIST) {
ed074c
+	needInit = false;
ed074c
+
ed074c
+	shmemData->fd = safe_open(shmemData->path,O_RDWR|O_EXLOCK, mode,
ed074c
+				  size+RESERVED_OFFSET);
ed074c
     }
ed074c
     if (shmemData->fd < 0) {
ed074c
 	delete shmemData;