[PATCH] Fix a /tmp race in the linux code

Andi Kleen andi at firstfloor.org
Sun Mar 2 16:26:28 PST 2008


/tmp races are bad for you. Use mkstemp instead of a predictable name for 
the debugging file.

-Andi

diff -u openjdk/hotspot/src/os/linux/vm/os_linux.cpp-o openjdk/hotspot/src/os/linux/vm/os_linux.cpp
--- openjdk/hotspot/src/os/linux/vm/os_linux.cpp-o	2007-05-24 09:30:53.000000000 +0200
+++ openjdk/hotspot/src/os/linux/vm/os_linux.cpp	2007-06-24 16:56:57.000000000 +0200
@@ -2185,15 +2196,12 @@
     return;
   }
 
-  char buf[40];
   int num = Atomic::add(1, &cnt);
 
-  sprintf(buf, "/tmp/hs-vm-%d-%d", os::current_process_id(), num);
-  unlink(buf);
- 
-  int fd = open(buf, O_CREAT | O_RDWR, S_IRWXU);
+  int fd = mkstemp("/tmp/hs-vm-XXXXXX");
 
   if (fd != -1) {
+    fchmod(fd, S_IRWXU);
     off_t rv = lseek(fd, size-2, SEEK_SET);
     if (rv != (off_t)-1) {
       if (write(fd, "", 1) == 1) {
@@ -2203,7 +2211,6 @@
       }
     }
     close(fd);
-    unlink(buf);
   }
 }
 



More information about the hotspot-dev mailing list