Re: pw keeps setting /etc/group to 0600

From: Jaakko Heinonen <jh_at_FreeBSD.org>
Date: Wed, 21 Nov 2012 17:45:43 +0200
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
Received on Wed Nov 21 2012 - 14:45:55 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:40:32 UTC