Subject: bin/35619: unlink+fopen race in makewhatis
To: None <gnats-admin@netbsd.org, netbsd-bugs@netbsd.org>
From: None <uwe@NetBSD.org>
List: netbsd-bugs
Date: 02/15/2007 23:45:00
>Number:         35619
>Category:       bin
>Synopsis:       unlink+fopen race in makewhatis
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Feb 15 23:45:00 +0000 2007
>Originator:     Valery Ushakov
>Release:        any
>Organization:
>Environment:
>Description:
makewhatis() uses

	(void)unlink(whatisdb);
	if ((out = fopen(whatisdb, "w")) == NULL)
		err(EXIT_FAILURE, "Cannot open `%s'", whatisdb);

to create the whatis database.  If two hosts share the man dir
via NFS and if both run makehwatis, there is a race that might
cause two instances of makewhatis to dump whatis database into
the same file.

Even if we don't consider the race, there's a window in which the
whatis database is incompelte (as makewhatis is still running).

>How-To-Repeat:
A smaller example:

    host1$ (echo A; sleep 10; echo B) > a &
    host2$ (echo 1; sleep 10; echo 2) > a &

There's a window in which you can see a to contain "1\bB\b"

>Fix:
makewhatis should use temp file + rename(2) so that the whatis database
is consistent at all times.