[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
creating temporary files in a secure way
From: |
Bruno Haible |
Subject: |
creating temporary files in a secure way |
Date: |
Mon, 10 Jan 2005 22:49:55 +0100 |
User-agent: |
KMail/1.5 |
Hi Alexandre,
Followup to a discussion from 2004-10-26: I think it's worth describing how
to portably create a temporary file. If this had existed in the autoconf
documentation, Debian bug #278283 (affecting autopoint and gettextize)
would not have existed. I don't see another canonical place where this
could be documented. Can you please put this in for me?
2005-01-09 Bruno Haible <address@hidden>
* doc/autoconf.texi (Limitations of Usual Tools): Add a paragraph
about mktemp.
*** autoconf/doc/autoconf.texi.bak 2004-10-12 13:50:46.000000000 +0200
--- autoconf/doc/autoconf.texi 2004-10-26 20:30:58.000000000 +0200
***************
*** 11581,11586 ****
--- 11581,11624 ----
older versions are not thread-safe either).
+ @item @command{mktemp}
+ @c -------------------
+ @prindex @command{mktemp}
+ @cindex Creating temporary files
+ The command @command{mktemp} exists only on a few systems. A portable
+ way to create a temporary file name into which it is safe to write,
+ protecting against symlink attacks, is to create a temporary directory
+ with mode 700 and use a file inside this directory.
+
+ Here is sample code to securely create a temporary directory:
+
+ @example
+ # Use the environment variable TMPDIR, falling back to /tmp. This allows
+ # users to specify a different temporary directory, for example, if their
+ # /tmp is filled up or too small.
+ : address@hidden/address@hidden
+ @{
+ # Use the mktemp program if available. If not available, hide the error
+ # message.
+ tmp=`(umask 077 && mktemp -d -q "$TMPDIR/fooXXXXXX") 2>/dev/null` &&
+ test -n "$tmp" && test -d "$tmp"
+ @} ||
+ @{
+ # Use a simple mkdir command. It is guaranteed to fail if the directory
+ # already exists. $RANDOM is bash specific and expands to empty in shells
+ # other than bash, ksh and zsh. Its use does not increase security;
+ # rather, it minimizes the probability of failure in a very cluttered /tmp
+ # directory.
+ tmp=$TMPDIR/foo$$-$RANDOM
+ (umask 077 && mkdir "$tmp")
+ @} ||
+ @{
+ echo "$0: cannot create a temporary directory in $TMPDIR" >&2
+ @{ (exit 1); exit 1; @}
+ @}
+ @end example
+
+
@item @command{mv}
@c ---------------
@prindex @command{mv}
Bruno
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- creating temporary files in a secure way,
Bruno Haible <=