[PATCH 2 of 2] Factor out selftest for better test and reviewability

Wald Commits scm-commit at wald.intevation.org
Mon Jun 23 16:43:13 CEST 2014


# HG changeset patch
# User Andre Heinecke <andre.heinecke at intevation.de>
# Date 1403534587 -7200
# Node ID 80d1a80b3e8d5d5818df3977cfb45f56907ddeb9
# Parent  6c090638b2b453d85b14c54d1a6c3eae95acc83c
Factor out selftest for better test and reviewability

diff -r 6c090638b2b4 -r 80d1a80b3e8d common/CMakeLists.txt
--- a/common/CMakeLists.txt	Mon Jun 23 15:29:48 2014 +0200
+++ b/common/CMakeLists.txt	Mon Jun 23 16:43:07 2014 +0200
@@ -15,6 +15,7 @@
    strhelp.c
    util.c
    binverify.c
+   selftest.c
 )
 
 if(WIN32)
diff -r 6c090638b2b4 -r 80d1a80b3e8d common/selftest.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/selftest.c	Mon Jun 23 16:43:07 2014 +0200
@@ -0,0 +1,46 @@
+#include "selftest.h"
+#include "binverify.h"
+#include "strhelp.h"
+#include "logging.h"
+
+bool
+selftest()
+{
+#ifdef WIN32
+  wchar_t wPath[MAX_PATH];
+  char *utf8path = NULL;
+
+  if (!GetModuleFileNameW (NULL, wPath, MAX_PATH - 1)) 
+    {
+      PRINTLASTERROR ("Failed to obtain module file name. Path too long?");
+      return false;
+    }
+
+  /* wPath might not be 0 terminated */
+  wPath[MAX_PATH - 1] = '\0';
+
+  utf8path = wchar_to_utf8 (wPath, wcsnlen(wPath, MAX_PATH));
+
+  if (utf8path == NULL)
+    {
+      ERRORPRINTF ("Failed to convert module path to utf-8");
+      return false;
+    }
+
+  if (!verify_binary (utf8path, strlen(utf8path)) != VerifyValid)
+    {
+      ERRORPRINTF ("Verification of the binary failed");
+      syslog_error_printf ("Integrity check failed.");
+      xfree(utf8path);
+      return false;
+    }
+
+  xfree(utf8path);
+#else
+  if (!verify_binary ("/proc/self/exe", 14) != VerifyValid)
+    {
+      syslog_error_printf ("Integrity check failed.");
+      return false;
+    }
+#endif
+}
diff -r 6c090638b2b4 -r 80d1a80b3e8d common/selftest.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/selftest.h	Mon Jun 23 16:43:07 2014 +0200
@@ -0,0 +1,36 @@
+#ifndef COMMON_SELFTEST_H
+#define COMMON_SELFTEST_H
+/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU GPL (v>=2)
+ * and comes with ABSOLUTELY NO WARRANTY!
+ * See LICENSE.txt for details.
+ */
+
+/** @file self test against manipulation
+ *
+ * The selftest is intended to detect untargeted manipulation or
+ * corruption of the executable. Circumvention of the selftest
+ * by targeted manipulation of the binary can, of course, not
+ * be detected.
+ */
+
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+/** @brief check that the current process is signed by the correct certificate
+ *
+ * Checks that the certificate is signed with a valid signature and the
+ * builtin public certificate.
+ *
+ * @returns true if the selftest is successful. false on error.
+ */
+bool selftest();
+
+#ifdef __cplusplus
+}
+#endif
+#endif // COMMON_SELFTEST_H
diff -r 6c090638b2b4 -r 80d1a80b3e8d ui/main.cpp
--- a/ui/main.cpp	Mon Jun 23 15:29:48 2014 +0200
+++ b/ui/main.cpp	Mon Jun 23 16:43:07 2014 +0200
@@ -7,9 +7,8 @@
  */
 #include "mainwindow.h"
 #include "processhelp.h"
-#include "binverify.h"
 #include "logging.h"
-#include "strhelp.h"
+#include "selftest.h"
 
 #include <QApplication>
 #include <QSystemTrayIcon>
@@ -40,49 +39,12 @@
 int main(int argc, char **argv)
 {
     /* First verify integrity even before calling QApplication*/
-#ifdef Q_OS_WIN
-    {
-      wchar_t wPath[MAX_PATH];
-      char *utf8path = NULL;
-
-      if (!GetModuleFileNameW (NULL, wPath, MAX_PATH - 1)) {
-          PRINTLASTERROR ("Failed to obtain module file name. Path too long?");
-          syslog_error_printf ("Integrity check failed.");
-          return -1;
-      }
-
-      /* wPath might not be 0 terminated */
-      wPath[MAX_PATH - 1] = '\0';
-
-      utf8path = wchar_to_utf8 (wPath, wcsnlen(wPath, MAX_PATH));
-
-      if (utf8path == NULL) {
-          ERRORPRINTF ("Failed to convert module path to utf-8");
-          syslog_error_printf ("Integrity check failed.");
-          return -1;
-      }
-
-      if (!verify_binary (utf8path, strlen(utf8path)) != VerifyValid)
-        {
-          ERRORPRINTF ("Verification of the binary failed");
-          syslog_error_printf ("Integrity check failed.");
-          xfree(utf8path);
-#ifdef RELEASE_BUILD
-          return -1;
-#endif
-        }
-
-      xfree(utf8path);
-    }
-#else
-    if (!verify_binary ("/proc/self/exe", 14) != VerifyValid)
-      {
+    if (!selftest()) {
         syslog_error_printf ("Integrity check failed.");
 #ifdef RELEASE_BUILD
         return -1;
 #endif
-      }
-#endif
+    }
 
     QApplication app (argc, argv);
 


More information about the Trustbridge-commits mailing list