unlink safe before close?
Dan Bernstein
bernsten at phoenix.Princeton.EDU
Mon May 8 07:21:37 AEST 1989
In article <755 at unify.UUCP> rk at unify.UUCP (Ron Kuris) writes:
[is it nonportable to unlink an fd and still use it?]
According to various UNIX standards and according to the internal
workings of the ``standard'' kernel routines (see, e.g., Bach),
it is perfectly safe. A file is removed when it is fully unlink()ed
*and* fully close()d; link() and unlink() do not affect file behavior
after an open().
> The intent is to create a temporary file that noone else
> can conflict with, and is automatically removed when the program
> terminates (either normally or abnormally):
Almost, but not quite. Before the creat(), the only way that someone
else can conflict with your operations is by guessing your filename;
this is reasonably easy to deal with. After the unlink(), no other
process (other than later children) can do anything to the file,
*as long as it has no remaining links*. That last assertion is the
tricky part; if someone makes an extra link to your file between
creat() and unlink(), then the file will still have the new link
and won't disappear after your close(). You can detect this situation,
but curing it is very difficult: there is no way to find the new link,
let alone to remove it if its directory is unwritable! A sufficiently
determined user can start filling the filesystem with your old files
in this manner.
Although creat()-unlink() is the closest we have right know to a
real fdopen(char *directory), it does not exactly do the job from
a security viewpoint. (By the way, fdopen() must take an argument
to determine the appropriate filesystem; I think a directory should
be specified and require write permission, so that users can't
fill up filesystems with fdopen() that they can't fill up otherwise.)
---Dan Bernstein, bernsten at phoenix.princeton.edu
More information about the Comp.unix.wizards
mailing list