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

David Holmes David.Holmes at Sun.COM
Sun Mar 2 21:11:24 PST 2008


Andi,

Where is the race given the temp filename is unique?

David Holmes

Andi Kleen wrote:
> /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