1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-22 10:19:57 +01:00

avoid double-close in unusual dotlock situations

* common/dotlock.c: (dotlock_create_unix) avoid double-close()
 in unusual situations.

--

close(2) says:

 close() should not be retried after an EINTR since this  may
       cause a reused descriptor from another thread to be closed.

Before this patch was applied, if close(fd) failed with EINTR, it
would be closed again in the write_failed: block.

It could also have been closed a second time in the case that
(use_hardlinks_p (h->tname)) evaluated to something other than 0 or 1.

This patch avoids both of those scenarios.

Note that close() could still be called twice on the same file
descriptor if the first close(fd) fails but errno is not EINTR.  I'm
not sure the right thing to do in that scenario.  An alternate
resolution could be to unequivocally set fd to -1 after the first
failed close(fd), avoiding the errno == EINTR test.

Debian-Bug-Id: 773423
This commit is contained in:
Daniel Kahn Gillmor 2014-12-19 17:12:37 -05:00 committed by Werner Koch
parent 351bca9047
commit 628b111fa6

View File

@ -680,7 +680,12 @@ dotlock_create_unix (dotlock_t h, const char *file_to_lock)
if ( write (fd, "\n", 1 ) != 1 ) if ( write (fd, "\n", 1 ) != 1 )
goto write_failed; goto write_failed;
if ( close (fd) ) if ( close (fd) )
goto write_failed; {
if ( errno == EINTR )
fd = -1;
goto write_failed;
}
fd = -1;
/* Check whether we support hard links. */ /* Check whether we support hard links. */
switch (use_hardlinks_p (h->tname)) switch (use_hardlinks_p (h->tname))
@ -718,7 +723,8 @@ dotlock_create_unix (dotlock_t h, const char *file_to_lock)
all_lockfiles = h->next; all_lockfiles = h->next;
UNLOCK_all_lockfiles (); UNLOCK_all_lockfiles ();
my_error_2 (_("error writing to '%s': %s\n"), h->tname, strerror (errno)); my_error_2 (_("error writing to '%s': %s\n"), h->tname, strerror (errno));
close (fd); if ( fd != -1 )
close (fd);
unlink (h->tname); unlink (h->tname);
jnlib_free (h->tname); jnlib_free (h->tname);
jnlib_free (h); jnlib_free (h);