[PATCH 1/2] agent: Use npth_clock_gettime for S2K calibration.

Damien Goutte-Gattat dgouttegattat at incenp.org
Thu Jul 13 16:24:38 CEST 2017


* agent/protect.c (struct calibrate_time_s): Make that struct
an alias for timespec on non-W32 systems.
(calibrate_gettime): Use npth_clock_gettime instead of times.
(calibrate_elapsed_time): Adapt computation of elapsed time
accordingly.
* agent/Makefile.am: Link gpg-protect-tool against nPth.
--

It has been reported that the current method of measuring
elapsed times could result in the agent being stuck in an
infinite loop if the Linux kernel we are running on has been
configured with VIRT_CPU_ACCOUNTING_GEN=y.

Contrary to times(), clock_gettime() seems unaffected by
whatever happens in the kernel under that configuration.

We use the nPth wrapper for clock_gettime, which will provide
a fallback implementation on systems without clock_gettime.

GnuPG-bug-id: 3276
Signed-off-by: Damien Goutte-Gattat <dgouttegattat at incenp.org>
---
 agent/Makefile.am |  6 +++---
 agent/protect.c   | 22 ++++++++++++----------
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/agent/Makefile.am b/agent/Makefile.am
index ce29462..ea6db2b 100644
--- a/agent/Makefile.am
+++ b/agent/Makefile.am
@@ -77,10 +77,10 @@ gpg_protect_tool_SOURCES = \
 	protect-tool.c \
 	protect.c cvt-openpgp.c
 
-gpg_protect_tool_CFLAGS = $(AM_CFLAGS) $(LIBASSUAN_CFLAGS) \
+gpg_protect_tool_CFLAGS = $(AM_CFLAGS) $(LIBASSUAN_CFLAGS) $(NPTH_CFLAGS) \
 	$(INCICONV)
 gpg_protect_tool_LDADD = $(common_libs) $(LIBGCRYPT_LIBS) $(LIBASSUAN_LIBS) \
-         $(GPG_ERROR_LIBS) $(LIBINTL) $(NETLIBS) $(LIBICONV)
+         $(NPTH_LIBS) $(GPG_ERROR_LIBS) $(LIBINTL) $(NETLIBS) $(LIBICONV)
 
 gpg_preset_passphrase_SOURCES = \
 	preset-passphrase.c
@@ -103,7 +103,7 @@ $(PROGRAMS): $(common_libs) $(commonpth_libs) $(pwquery_libs)
 TESTS = t-protect
 
 t_common_ldadd = $(common_libs)  $(LIBGCRYPT_LIBS) $(GPG_ERROR_LIBS) \
-	          $(LIBINTL) $(LIBICONV) $(NETLIBS)
+	          $(LIBINTL) $(LIBICONV) $(NETLIBS) $(NPTH_LIBS)
 
 t_protect_SOURCES = t-protect.c protect.c
 t_protect_LDADD = $(t_common_ldadd)
diff --git a/agent/protect.c b/agent/protect.c
index 18b44f1..42fb479 100644
--- a/agent/protect.c
+++ b/agent/protect.c
@@ -33,7 +33,7 @@
 # endif
 # include <windows.h>
 #else
-# include <sys/times.h>
+# include <npth.h>
 #endif
 
 #include "agent.h"
@@ -71,14 +71,14 @@ static const struct {
 
 
 /* A helper object for time measurement.  */
+#ifdef HAVE_W32_SYSTEM
 struct calibrate_time_s
 {
-#ifdef HAVE_W32_SYSTEM
   FILETIME creation_time, exit_time, kernel_time, user_time;
+};
 #else
-  clock_t ticks;
+ #define calibrate_time_s timespec
 #endif
-};
 
 
 static int
@@ -105,10 +105,7 @@ calibrate_get_time (struct calibrate_time_s *data)
                    &data->kernel_time, &data->user_time);
 # endif
 #else
-  struct tms tmp;
-
-  times (&tmp);
-  data->ticks = tmp.tms_utime;
+  npth_clock_gettime(data);
 #endif
 }
 
@@ -134,8 +131,13 @@ calibrate_elapsed_time (struct calibrate_time_s *starttime)
     return (unsigned long)((t2 - t1)/10000);
   }
 #else
-  return (unsigned long)((((double) (stoptime.ticks - starttime->ticks))
-                          /CLOCKS_PER_SEC)*10000000);
+  {
+    struct calibrate_time_s difftime;
+
+    npth_timersub(&stoptime, starttime, &difftime);
+
+    return difftime.tv_sec * 1000 + difftime.tv_nsec / 1000000.0;
+  }
 #endif
 }
 
-- 
2.9.0




More information about the Gnupg-devel mailing list