changeset 634:80d1a80b3e8d

Factor out selftest for better test and reviewability
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 23 Jun 2014 16:43:07 +0200
parents 6c090638b2b4
children ed1887be5170
files common/CMakeLists.txt common/selftest.c common/selftest.h ui/main.cpp
diffstat 4 files changed, 86 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- /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
+}
--- /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
--- 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);
 

http://wald.intevation.org/projects/trustbridge/