This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: GNU GCC's f77


On Mon, Nov 13, 2000 at 10:00:19PM +0100, Toon Moene wrote:

> Do you know why the FreeBSD developers consider tempnam a problem ?
> 
> Is it a generic problem with tempnam's specification, or is the specific
> implementation on FreeBSD problematic ?

It is a generic problem with tempnam's specification.  tempnam does
not create a scratch file, it just finds a name which is not in use.
In between the time it determines the name is not in use, and the time
you get around to creating the file, someone else may have created
another file of the same name, and you'll clobber their work.

Worse, someone else may have linked that name to /etc/passwd, and if
you're running with privileges, you'll clobber /etc/passwd.  This is
not a theoretical problem.  A large number of "local root compromise"
security holes exploit exactly this race condition.

mkstemp is lower-level and harder to use, but it creates the file
atomically, so you can be sure you are not clobbering anything.

> Yesterday I tried to come up with a patch to let libf2c use mkstemp
> instead of tempnam if available.  Unfortunately, during testing of that
> patch (which worked) I noticed that mkstemp doesn't create temporary
> files in $TMPDIR which tempnam does.  Because this is an important
> feature of allocating SCRATCH files in Fortran (as users can determine
> on which filesystem they will appear) I hesitate to switch to mkstemp.

You could use a wrapper routine which looked up $TMPDIR and made
mkstemp's template argument reference that directory.  This wrapper
routine could also unlink the file before it returns, which would get
you as near as possible to the VMS scratch-file semantics you said you
wanted earlier: the file is invisible to other processes and
automatically destroyed when closed or the program finishes.

int
f2c_tmpfile(void)
{
  char tmpl[PATH_MAX];
  char *env;
  int fd, len;

  env = getenv("TMPDIR");
  if (!env) env = getenv("TEMP");
  if (!env) env = "/tmp";

  len = strlen(env);
  if (len > PATH_MAX - sizeof "/f2c.XXXXXX") {
    error("%s is too long", env);
    return -1;
  }

  strcpy(tmpl, env);
  strcat(tmpl, "/f2c.XXXXXX");

  fd = mkstemp(tmpl);
  if (fd == -1) {
    error("mkstemp: %s", strerror(errno));
    return -1;
  }

  unlink(tmpl);
  return fd;
}

zw

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]