Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 21 Nov 2012 17:45:43 +0200
From:      Jaakko Heinonen <jh@FreeBSD.org>
To:        Mateusz Guzik <mjguzik@gmail.com>, Ryan Stone <rysto32@gmail.com>, FreeBSD Current <freebsd-current@freebsd.org>, bapt@freebsd.org
Subject:   Re: pw keeps setting /etc/group to 0600
Message-ID:  <20121121154542.GA1849@a91-153-116-96.elisa-laajakaista.fi>
In-Reply-To: <20121119222843.GB22292@dft-labs.eu>
References:  <CAFMmRNxDr=%2BpsiazVrJ8e=T4fogiiPv5nEAo%2BnfoD=tPYMehCw@mail.gmail.com> <CAFMmRNxW0FzupbC9w4U5pPZUoqOE%2B3rqFNRUrnsMRGFnO7qPWA@mail.gmail.com> <20121119222843.GB22292@dft-labs.eu>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2012-11-19, Mateusz Guzik wrote:
> First, pw should not fail if other instance is running, it should wait
> instead (think of parallel batch scripts adding some users/groups).
> 
> Second, current code has a race:
> lockfd = open(group_file, O_RDONLY, 0);
> if (lockfd < 0 || fcntl(lockfd, F_SETFD, 1) == -1)
> 	err(1, "%s", group_file);
> if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
> [..]
> gr_copy(pfd, tfd, gr, old_gr); /* copy from groupfile to tempfile */
> [..]
> rename(tempfile,groupfile);

Hmm, could using the O_EXLOCK flag for open() instead of flock() help here?

> Now let's consider threads A and B:
> 
> A: open()
> A: lock();
> A: gr_copy
> B: open()
> 
> Now B has file descriptor to /etc/group that is about to be removed.
> 
> A: rename()
> A: unlock()
> B: lock()
> 
> Now B has a lock on unlinked file.
> 
> B: gr_copy()
> B: rename()
> 
> ... and stores new content losing modifications done by A

-- 
Jaakko



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20121121154542.GA1849>