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

kbx: Fix deadlock in gpgsm on Windows due to a sharing violation.

* kbx/keybox-init.c (keybox_lock) [W32]: Use _keybox_close_file
instead of fclose so that a close is done if the file is opened by
another handle.
* kbx/keybox-search.c (keybox_search): Remember the last offset and
use that in NEXT search mode if we had to re-open the file.
--

GnuPG-bug-id: 4505
Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2019-05-14 19:05:58 +02:00
parent 3a28706cfd
commit 6f72aa8214
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
3 changed files with 42 additions and 14 deletions

View File

@ -289,18 +289,14 @@ keybox_lock (KEYBOX_HANDLE hd, int yes)
if (!kb->is_locked) if (!kb->is_locked)
{ {
#ifdef HAVE_W32_SYSTEM #ifdef HAVE_W32_SYSTEM
/* Under Windows we need to close the file before we try /* Under Windows we need to close the file before we try
* to lock it. This is because another process might have * to lock it. This is because another process might have
* taken the lock and is using keybox_file_rename to * taken the lock and is using keybox_file_rename to
* rename the base file. How if our dotlock_take below is * rename the base file. Now if our dotlock_take below is
* waiting for the lock but we have the base file still * waiting for the lock but we have the base file still
* open, keybox_file_rename will never succeed as we are * open, keybox_file_rename will never succeed as we are
* in a deadlock. */ * in a deadlock. */
if (hd->fp) _keybox_close_file (hd);
{
fclose (hd->fp);
hd->fp = NULL;
}
#endif /*HAVE_W32_SYSTEM*/ #endif /*HAVE_W32_SYSTEM*/
if (dotlock_take (kb->lockhd, -1)) if (dotlock_take (kb->lockhd, -1))
{ {

View File

@ -844,16 +844,21 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc,
KEYBOXBLOB blob = NULL; KEYBOXBLOB blob = NULL;
struct sn_array_s *sn_array = NULL; struct sn_array_s *sn_array = NULL;
int pk_no, uid_no; int pk_no, uid_no;
off_t lastfoundoff;
if (!hd) if (!hd)
return gpg_error (GPG_ERR_INV_VALUE); return gpg_error (GPG_ERR_INV_VALUE);
/* clear last found result */ /* Clear last found result but reord the offset of the last found
* blob which we may need later. */
if (hd->found.blob) if (hd->found.blob)
{ {
lastfoundoff = _keybox_get_blob_fileoffset (hd->found.blob);
_keybox_release_blob (hd->found.blob); _keybox_release_blob (hd->found.blob);
hd->found.blob = NULL; hd->found.blob = NULL;
} }
else
lastfoundoff = 0;
if (hd->error) if (hd->error)
return hd->error; /* still in error state */ return hd->error; /* still in error state */
@ -872,6 +877,7 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc,
case KEYDB_SEARCH_MODE_FIRST: case KEYDB_SEARCH_MODE_FIRST:
/* always restart the search in this mode */ /* always restart the search in this mode */
keybox_search_reset (hd); keybox_search_reset (hd);
lastfoundoff = 0;
break; break;
default: default:
break; break;
@ -896,6 +902,32 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc,
xfree (sn_array); xfree (sn_array);
return rc; return rc;
} }
/* log_debug ("%s: re-opened file\n", __func__); */
if (ndesc && desc[0].mode == KEYDB_SEARCH_MODE_NEXT && lastfoundoff)
{
/* Search mode is next and the last search operation
* returned a blob which also was not the first one. We now
* need to skip over that blob and hope that the file has
* not changed. */
if (fseeko (hd->fp, lastfoundoff, SEEK_SET))
{
rc = gpg_error_from_syserror ();
log_debug ("%s: seeking to last found offset failed: %s\n",
__func__, gpg_strerror (rc));
xfree (sn_array);
return gpg_error (GPG_ERR_NOTHING_FOUND);
}
/* log_debug ("%s: re-opened file and sought to last offset\n", */
/* __func__); */
rc = _keybox_read_blob (NULL, hd->fp, NULL);
if (rc)
{
log_debug ("%s: skipping last found blob failed: %s\n",
__func__, gpg_strerror (rc));
xfree (sn_array);
return gpg_error (GPG_ERR_NOTHING_FOUND);
}
}
} }
/* Kludge: We need to convert an SN given as hexstring to its binary /* Kludge: We need to convert an SN given as hexstring to its binary

View File

@ -423,7 +423,7 @@ keybox_update_keyblock (KEYBOX_HANDLE hd, const void *image, size_t imagelen)
if (off == (off_t)-1) if (off == (off_t)-1)
return gpg_error (GPG_ERR_GENERAL); return gpg_error (GPG_ERR_GENERAL);
/* Close this the file so that we do no mess up the position for a /* Close the file so that we do no mess up the position for a
next search. */ next search. */
_keybox_close_file (hd); _keybox_close_file (hd);