From f4d93cd5c36712a7aacfd52b2ee8c8e8aec72dd9 Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Sun, 8 Mar 2009 10:54:38 -0400 Subject: [PATCH] Use thread-safe libgcrypt and libgpg-error functions. Only call the new _gpg_strerror() which is a wrapper for gpg_strerror_r(). The string is g_strdup()'d and freed when the thread terminates. --- src/commands.c | 38 +++++++++++++++++++------------------- src/pinentry.c | 4 ++-- src/pwmd.c | 16 ++++++++-------- src/pwmd_error.c | 16 ++++++++++++++-- src/pwmd_error.h | 5 ++++- 5 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/commands.c b/src/commands.c index 8ae82abd..7dcce9e1 100644 --- a/src/commands.c +++ b/src/commands.c @@ -84,7 +84,7 @@ static gboolean encrypt_xml(gcry_cipher_hd_t gh, void *outbuf, gsize outsize, gpg_error_t rc; if ((rc = gcry_cipher_encrypt(gh, outbuf, outsize, inbuf, insize))) { - log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); return FALSE; } @@ -97,7 +97,7 @@ static gpg_error_t decrypt_xml(gcry_cipher_hd_t gh, void *outbuf, gsize outsize, gpg_error_t rc; if ((rc = gcry_cipher_decrypt(gh, outbuf, outsize, inbuf, insize))) - log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); return rc; } @@ -499,7 +499,7 @@ static int open_command(assuan_context_t ctx, char *line) if ((rc = gcry_cipher_open(&client->gh, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, 0))) { g_strfreev(req); - log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); cleanup_client(client); return send_error(ctx, rc); } @@ -814,7 +814,7 @@ gpg_error_t do_xml_encrypt(struct client_s *client, gcry_cipher_hd_t gh, if ((rc = gcry_cipher_setkey(gh, tkey, gcrykeysize))) { memset(tkey, 0, sizeof(tkey)); gcry_free(inbuf); - log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); return rc; } @@ -850,14 +850,14 @@ gpg_error_t do_xml_encrypt(struct client_s *client, gcry_cipher_hd_t gh, if ((rc = gcry_cipher_setiv(gh, file_header.iv, sizeof(file_header.iv)))) { gcry_free(inbuf); - log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); return rc; } if (encrypt_xml(gh, inbuf, insize, NULL, 0) == FALSE) { gcry_free(inbuf); - log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); return rc; } @@ -868,19 +868,19 @@ gpg_error_t do_xml_encrypt(struct client_s *client, gcry_cipher_hd_t gh, if ((rc = gcry_cipher_setiv(gh, file_header.iv, sizeof(file_header.iv)))) { gcry_free(inbuf); - log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); return rc; } if ((rc = gcry_cipher_setkey(gh, shakey, gcrykeysize))) { gcry_free(inbuf); - log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); return rc; } if (encrypt_xml(gh, inbuf, insize, NULL, 0) == FALSE) { gcry_free(inbuf); - log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); return rc; } @@ -2739,10 +2739,10 @@ gpg_error_t try_xml_decrypt(assuan_context_t ctx, gint fd, struct stat st, if ((rc = gcry_cipher_setiv(gh, iv, gcryblocksize))) { if (!ctx) { gcry_cipher_close(gh); - warnx("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + warnx("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); } else - log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); gcry_free(inbuf); gcry_free(iv); @@ -2752,10 +2752,10 @@ gpg_error_t try_xml_decrypt(assuan_context_t ctx, gint fd, struct stat st, if ((rc = gcry_cipher_setkey(gh, key, gcrykeysize))) { if (!ctx) { gcry_cipher_close(gh); - warnx("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + warnx("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); } else - log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); gcry_free(inbuf); gcry_free(iv); @@ -2794,10 +2794,10 @@ gpg_error_t try_xml_decrypt(assuan_context_t ctx, gint fd, struct stat st, if ((rc = gcry_cipher_setkey(gh, tkey, gcrykeysize))) { if (!ctx) { gcry_cipher_close(gh); - warnx("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + warnx("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); } else - log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); memset(tkey, 0, sizeof(tkey)); gcry_free(inbuf); @@ -2824,10 +2824,10 @@ gpg_error_t try_xml_decrypt(assuan_context_t ctx, gint fd, struct stat st, if ((rc = gcry_cipher_setiv(gh, iv, gcryblocksize))) { if (!ctx) { gcry_cipher_close(gh); - warnx("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + warnx("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); } else - log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); gcry_free(inbuf); gcry_free(iv); @@ -2839,10 +2839,10 @@ gpg_error_t try_xml_decrypt(assuan_context_t ctx, gint fd, struct stat st, if (rc) { if (!ctx) { gcry_cipher_close(gh); - warnx("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + warnx("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); } else - log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(rc)); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc)); gcry_free(inbuf); gcry_free(iv); diff --git a/src/pinentry.c b/src/pinentry.c index ae82629c..adde24bd 100644 --- a/src/pinentry.c +++ b/src/pinentry.c @@ -675,14 +675,14 @@ pth_event_t pinentry_iterate(struct client_s *cl, pth_event_t ev) pk.error = GPG_ERR_TIMEOUT; pk.error = gpg_err_code_from_syserror(); - log_write("%s", gpg_strerror(pk.error)); + log_write("%s", _gpg_strerror(pk.error)); } else if (len == 0) { if (cl->pinentry->status == PINENTRY_TIMEOUT) pk.error = GPG_ERR_TIMEOUT; pk.error = GPG_ERR_EOF; - log_write("%s", gpg_strerror(pk.error)); + log_write("%s", _gpg_strerror(pk.error)); } else log_write(N_("pth_read(): short byte count")); diff --git a/src/pwmd.c b/src/pwmd.c index 465faf45..fd35c103 100644 --- a/src/pwmd.c +++ b/src/pwmd.c @@ -111,7 +111,7 @@ gpg_error_t send_syserror(assuan_context_t ctx, gint e) { gpg_error_t n = gpg_error_from_errno(e); - return assuan_process_done(ctx, assuan_set_error(ctx, n, gpg_strerror(n))); + return assuan_process_done(ctx, assuan_set_error(ctx, n, _gpg_strerror(n))); } gpg_error_t send_error(assuan_context_t ctx, gpg_error_t e) @@ -295,7 +295,7 @@ static assuan_context_t new_connection(gint fd) return ctx; fail: - log_write("%s", gpg_strerror(rc)); + log_write("%s", _gpg_strerror(rc)); return NULL; } @@ -452,7 +452,7 @@ static void *client_msg_thread(void *data) if (rc) { pth_mutex_release(&cn_mutex); - log_write("%s", gpg_strerror(rc)); + log_write("%s", _gpg_strerror(rc)); break; } } @@ -547,7 +547,7 @@ static void *client_thread(void *data) rc = send_status(cl->ctx, STATUS_CACHE); if (rc) { - log_write("%s", gpg_strerror(rc)); + log_write("%s", _gpg_strerror(rc)); goto fail; } @@ -577,11 +577,11 @@ static void *client_thread(void *data) if (gpg_err_code(rc) == GPG_ERR_EOF) goto done; - log_write("assuan_process_next(): %s", gpg_strerror(rc)); + log_write("assuan_process_next(): %s", _gpg_strerror(rc)); rc = send_error(cl->ctx, gpg_err_make(PWMD_ERR_SOURCE, rc)); if (rc) { - log_write("assuan_process_done(): %s", gpg_strerror(rc)); + log_write("assuan_process_done(): %s", _gpg_strerror(rc)); goto done; } } @@ -1083,7 +1083,7 @@ done: if (rc) { memset(shakey, 0, sizeof(shakey)); - warnx("%s", gpg_strerror(rc)); + warnx("%s", _gpg_strerror(rc)); return FALSE; } @@ -1300,7 +1300,7 @@ again: rc = pinentry_getpin(pin, &result); if (rc) { - warnx("%s: %s", filename, gpg_strerror(rc)); + warnx("%s: %s", filename, _gpg_strerror(rc)); cleanup_pinentry(pin); gcry_free(key); gcry_free(md5file); diff --git a/src/pwmd_error.c b/src/pwmd_error.c index 9c62c697..a9ff5bb1 100644 --- a/src/pwmd_error.c +++ b/src/pwmd_error.c @@ -19,6 +19,8 @@ #include #include +#include +#include #ifdef HAVE_CONFIG_H #include @@ -29,7 +31,7 @@ #include "pwmd_error.h" -const char *pwmd_strerror(gpg_error_t e) +const gchar *pwmd_strerror(gpg_error_t e) { gpg_err_code_t code = gpg_err_code(e); @@ -51,5 +53,15 @@ const char *pwmd_strerror(gpg_error_t e) } } - return gpg_strerror(e); + return _gpg_strerror(e); +} + +gchar *_gpg_strerror(gpg_error_t rc) +{ + gchar ebuf[ERROR_BUFLEN], *ep; + + gpg_strerror_r(rc, ebuf, sizeof(ebuf)); + ep = g_strdup(ebuf); + pth_cleanup_push(g_free, ep); + return ep; } diff --git a/src/pwmd_error.h b/src/pwmd_error.h index a80b99d3..a84335ac 100644 --- a/src/pwmd_error.h +++ b/src/pwmd_error.h @@ -21,6 +21,8 @@ #include +#define ERROR_BUFLEN 255 + #define PWMD_ERR_SOURCE GPG_ERR_SOURCE_USER_1 #define EPWMD_BADKEY GPG_ERR_INV_PASSPHRASE @@ -41,6 +43,7 @@ #define EPWMD_FILE_MODIFIED GPG_ERR_USER_6 #define EPWMD_MAX GPG_ERR_USER_7 -const char *pwmd_strerror(gpg_error_t pwmd_errno); +const gchar *pwmd_strerror(gpg_error_t e); +gchar *_gpg_strerror(gpg_error_t rc); #endif -- 2.11.4.GIT