pw keeps setting /etc/group to 0600

Jaakko Heinonen jh at FreeBSD.org
Wed Nov 21 15:45:55 UTC 2012


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


More information about the freebsd-current mailing list