Create a rudimentary symbol scanning script
authorNeil Horman <nhorman@openssl.org>
Mon, 23 Oct 2023 16:47:13 +0000 (12:47 -0400)
committerTomas Mraz <tomas@openssl.org>
Wed, 1 Nov 2023 15:32:26 +0000 (16:32 +0100)
We would like to be able to log and audit the symbols we use in openssl
so that we might catch when a new platform symbols is referecned

Add such a script (just on unix platforms for now) that gathers the used
symbols not belonging to libcrypto or libssl, and compare it to a prior
known set of used symbols.  Error out if a new symbol is found

Add this script to the ci workflow in CI to capture newly
introduced platform symbols

Fixes #22330

Reviewed-by: Hugo Landau <hlandau@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22478)

.github/workflows/run-checker-ci.yml
.github/workflows/windows.yml
.github/workflows/windows_comp.yml
util/checkplatformsyms.pl [new file with mode: 0755]
util/platform_symbols/unix-symbols.txt [new file with mode: 0644]
util/platform_symbols/windows-symbols.txt [new file with mode: 0644]

index 2b7ca84d86e7694506608c5250e2672cd4d3a6eb..0c961f8ef12f724851b9398fb90606726e4b11f8 100644 (file)
@@ -54,5 +54,7 @@ jobs:
       run: |
         cat /proc/cpuinfo
         ./util/opensslwrap.sh version -c
+    - name: Check platform symbol usage
+      run: ./util/checkplatformsyms.pl ./util/platform_symbols/unix-symbols.txt ./libcrypto.so ./libssl.so
     - name: make test
       run: make test HARNESS_JOBS=${HARNESS_JOBS:-4}
index 8f68450a5f9e1d166bbb4acf8c9d4bf2351f1547..0d0f24c299b8b24edfca37f8d079b2949d549f10 100644 (file)
@@ -58,6 +58,9 @@ jobs:
         7z.exe x coreinfo/Coreinfo.zip
         ./Coreinfo64.exe -accepteula -f
         ./apps/openssl.exe version -c
+    - name: Check platform symbol usage
+      working-directory: _build
+      run: perl ../util/checkplatformsyms.pl ../util/platform_symbols/windows-symbols.txt libcrypto-3-x64.dll ./libssl-3-x64.dll
     - name: test
       working-directory: _build
       run: nmake test VERBOSE_FAILURE=yes TESTS=-test_fuzz* HARNESS_JOBS=4
index abcc0ad0190b5b984e1edeb502b5d9a3011190b5..a75451669a1d4db1f856ed54af2fb3af6123c955 100644 (file)
@@ -53,6 +53,9 @@ jobs:
         7z.exe x coreinfo/Coreinfo.zip
         ./Coreinfo64.exe -accepteula -f
         ./apps/openssl.exe version -c
+    - name: Check platform symbol usage
+      run: |
+        perl ./util/checkplatformsyms.pl ./util/platform_symbols/windows-symbols.txt libcrypto-3-x64.dll ./libssl-3-x64.dll
     - name: test
       working-directory: _build
       run: |
diff --git a/util/checkplatformsyms.pl b/util/checkplatformsyms.pl
new file mode 100755 (executable)
index 0000000..742dd8e
--- /dev/null
@@ -0,0 +1,84 @@
+#! /usr/bin/env perl
+# Copyright 2006-2023 The OpenSSL Project Authors. All Rights Reserved.
+#
+# Licensed under the Apache License 2.0 (the "License").  You may not use
+# this file except in compliance with the License.  You can obtain a copy
+# in the file LICENSE in the source distribution or at
+# https://www.openssl.org/source/license.html
+
+use warnings;
+use strict;
+use Config;
+
+my $expectedsyms=$ARGV[0];
+
+shift(@ARGV);
+
+my $objlist;
+my $objfilelist = join(" ", @ARGV);
+my $expsyms;
+my $exps;
+my $OBJFH;
+my $cmd;
+
+if ($Config{osname} eq "MSWin32") {
+        my $currentdll = "";
+        $cmd = "dumpbin /imports " . $objfilelist;
+        my @symlist;
+        open $expsyms, '<', $expectedsyms or die;
+        {
+            local $/;
+            $exps=<$expsyms>;
+        }
+        close($expsyms);
+        open($OBJFH, "$cmd|") or die "Cannot open process: $!";
+        while (<$OBJFH>)
+        {
+            chomp;
+            my $dllfile = $_;
+            $dllfile =~ s/( +)(.*)(\.dll)(.*)/DLLFILE \2/;
+            if (index($dllfile, "DLLFILE") >= 0) {
+                $currentdll = substr($dllfile, 8);
+                $currentdll =~ s/^\s+|s+$//g;
+            }
+            # filter imports from our own library
+            if ("$currentdll" ne "libcrypto-3-x64") {
+                my $line = $_;
+                $line =~ s/                          [0-9a-fA-F]{1,2} /SYMBOL /;
+                if (index($line, "SYMBOL") != -1) {
+                    $line =~ s/.*SYMBOL //;
+                    push(@symlist, $line);
+                }
+            }
+        }
+        foreach (@symlist) {
+            if (index($exps, $_) < 0) {
+                print "Symbol $_ not in the allowed platform symbols list\n";
+                exit 1;
+            }
+        }
+        exit 0;
+    }
+else {
+        $cmd = "objdump -t " . $objfilelist . " | grep UND | grep -v \@OPENSSL";
+        $cmd = $cmd . " | awk '{print \$NF}' |";
+        $cmd = $cmd . " sed -e\"s/@.*\$//\" | sort | uniq";
+
+        open $expsyms, '<', $expectedsyms or die;
+        {
+            local $/;
+            $exps=<$expsyms>;
+        }
+        close($expsyms);
+
+        open($OBJFH, "$cmd|") or die "Cannot open process: $!";
+        while (<$OBJFH>)
+        {
+                if (index($exps, $_) < 0) {
+                    print "Symbol $_ not in the allowed platform symbols list\n";
+                    exit 1;
+                }
+        }
+        close($OBJFH);
+        exit 0;
+    }
diff --git a/util/platform_symbols/unix-symbols.txt b/util/platform_symbols/unix-symbols.txt
new file mode 100644 (file)
index 0000000..2b2fe53
--- /dev/null
@@ -0,0 +1,156 @@
+abort
+accept
+bcmp
+bind
+calloc
+chmod
+clearerr
+clock_gettime
+close
+closedir
+closelog
+connect
+__ctype_b_loc
+__ctype_tolower_loc
+__cxa_atexit
+__cxa_finalize
+dladdr
+dlclose
+dlerror
+dlopen
+dlsym
+__errno_location
+fclose
+fdopen
+feof
+ferror
+fflush
+fgets
+fileno
+fopen
+fopen64
+fprintf
+fputc
+fputs
+fread
+free
+freeaddrinfo
+fseek
+fstat
+ftell
+fwrite
+gai_strerror
+getaddrinfo
+getauxval
+getcontext
+getentropy
+getenv
+gethostbyname
+getnameinfo
+getpeername
+getpid
+getsockname
+getsockopt
+gettimeofday
+__gmon_start__
+gmtime_r
+gmtime
+ioctl
+__isoc99_sscanf
+_ITM_deregisterTMCloneTable
+_ITM_registerTMCloneTable
+listen
+_longjmp
+lseek
+madvise
+makecontext
+malloc
+memchr
+memcmp
+memcpy
+memmove
+memset
+mktime
+mlock
+mmap
+mprotect
+munmap
+opendir
+openlog
+poll
+pthread_attr_destroy
+pthread_attr_init
+pthread_attr_setdetachstate
+pthread_cond_broadcast
+pthread_cond_destroy
+pthread_cond_init
+pthread_cond_signal
+pthread_cond_timedwait
+pthread_cond_wait
+pthread_create
+pthread_exit
+pthread_getspecific
+pthread_join
+pthread_key_create
+pthread_key_delete
+pthread_mutex_destroy
+pthread_mutex_init
+pthread_mutex_lock
+pthread_mutex_trylock
+pthread_mutex_unlock
+pthread_once
+pthread_rwlock_destroy
+pthread_rwlock_init
+pthread_rwlock_rdlock
+pthread_rwlock_unlock
+pthread_rwlock_wrlock
+pthread_self
+pthread_setspecific
+qsort
+readdir
+realloc
+recvfrom
+recvmmsg
+secure_getenv
+select
+sendmmsg
+sendto
+setbuf
+setcontext
+_setjmp
+setsockopt
+shmat
+shmdt
+shmget
+shutdown
+sigaction
+sleep
+socket
+sprintf
+stderr
+stdin
+stdout
+strcat
+strchr
+strcmp
+strcpy
+strcspn
+strdup
+strlen
+strncmp
+strncpy
+strrchr
+strspn
+strstr
+strtol
+strtoul
+syscall
+sysconf
+syslog
+tcgetattr
+tcsetattr
+__timezone
+uname
+usleep
+vfprintf
+__xpg_strerror_r
diff --git a/util/platform_symbols/windows-symbols.txt b/util/platform_symbols/windows-symbols.txt
new file mode 100644 (file)
index 0000000..097741b
--- /dev/null
@@ -0,0 +1,231 @@
+CryptGenRandom
+RegisterEventSourceW
+ReportEventW
+CryptAcquireContextW
+CryptReleaseContext
+DeregisterEventSource
+CertFindCertificateInStore
+CertFreeCertificateContext
+CertOpenSystemStoreW
+CertCloseStore
+GetUserObjectInformationW
+GetProcessWindowStation
+MessageBoxW
+GetCurrentProcess
+ReadConsoleW
+ReadConsoleA
+SetConsoleMode
+GetConsoleMode
+CreateSemaphoreA
+GetExitCodeThread
+WaitForSingleObject
+ReleaseSemaphore
+GetCurrentProcessId
+TryEnterCriticalSection
+LeaveCriticalSection
+EnterCriticalSection
+InitializeCriticalSection
+SystemTimeToFileTime
+GetSystemTime
+TlsFree
+TlsSetValue
+TlsGetValue
+GetSystemTimeAsFileTime
+RtlCaptureContext
+RtlLookupFunctionEntry
+UnhandledExceptionFilter
+SetUnhandledExceptionFilter
+IsProcessorFeaturePresent
+IsDebuggerPresent
+GetStartupInfoW
+QueryPerformanceCounter
+InitializeSListHead
+DeleteCriticalSection
+TerminateProcess
+TlsAlloc
+GetCurrentThreadId
+AcquireSRWLockShared
+AcquireSRWLockExclusive
+RtlVirtualUnwind
+ConvertFiberToThread
+ConvertThreadToFiberEx
+SwitchToFiber
+DeleteFiber
+CreateFiberEx
+GetSystemDirectoryA
+FreeLibrary
+GetProcAddress
+LoadLibraryA
+FormatMessageA
+GetLastError
+SetLastError
+CloseHandle
+LoadLibraryW
+GetEnvironmentVariableW
+GetStdHandle
+GetFileType
+WriteFile
+GetModuleHandleW
+MultiByteToWideChar
+WideCharToMultiByte
+GetACP
+GetModuleHandleExW
+GetSystemInfo
+VirtualAlloc
+VirtualProtect
+VirtualFree
+VirtualLock
+FindClose
+FindFirstFileW
+FindNextFileW
+Sleep
+InitializeSRWLock
+ReleaseSRWLockExclusive
+ReleaseSRWLockShared
+__current_exception
+__C_specific_handler
+wcsstr
+__current_exception_context
+strstr
+strchr
+memmove
+strrchr
+memcmp
+memset
+memcpy
+memchr
+__std_type_info_destroy_list
+__stdio_common_vsprintf
+__stdio_common_vfprintf
+__stdio_common_vsscanf
+ftell
+fseek
+fread
+_fileno
+__stdio_common_vswprintf
+_wfopen
+fopen
+setvbuf
+fflush
+ferror
+feof
+clearerr
+setbuf
+fclose
+fputs
+__acrt_iob_func
+__stdio_common_vsprintf_s
+fwrite
+fgets
+_setmode
+strtoul
+atoi
+strtol
+tolower
+strspn
+strcspn
+strncpy
+strncmp
+strcmp
+strcat_s
+isspace
+_strdup
+isdigit
+strncpy_s
+strcpy_s
+_gmtime64_s
+__timezone
+_mktime64
+_time64
+qsort
+malloc
+realloc
+calloc
+free
+terminate
+signal
+_initialize_narrow_environment
+_beginthreadex
+_endthreadex
+_register_onexit_function
+strerror_s
+_execute_onexit_table
+raise
+_crt_atexit
+_exit
+_crt_at_quick_exit
+_errno
+_cexit
+_initterm_e
+_configure_narrow_argv
+_initialize_onexit_table
+_initterm
+_seh_filter_dll
+_chmod
+_stat64i32
+_fstat64i32
+getenv
+GetStartupInfoW
+RtlLookupFunctionEntry
+RtlVirtualUnwind
+UnhandledExceptionFilter
+GetSystemTime
+SystemTimeToFileTime
+CloseHandle
+InitializeCriticalSection
+EnterCriticalSection
+LeaveCriticalSection
+TryEnterCriticalSection
+DeleteCriticalSection
+ReleaseSemaphore
+WaitForSingleObject
+GetCurrentThreadId
+SetUnhandledExceptionFilter
+GetExitCodeThread
+CreateSemaphoreA
+SetLastError
+GetLastError
+GetCurrentProcess
+TerminateProcess
+IsProcessorFeaturePresent
+QueryPerformanceCounter
+RtlCaptureContext
+GetCurrentProcessId
+GetSystemTimeAsFileTime
+DisableThreadLibraryCalls
+InitializeSListHead
+IsDebuggerPresent
+GetModuleHandleW
+memcpy
+memset
+__current_exception_context
+strchr
+memcmp
+memchr
+strstr
+memmove
+__std_type_info_destroy_list
+__current_exception
+__C_specific_handler
+_errno
+_endthreadex
+terminate
+_initterm
+_initterm_e
+_seh_filter_dll
+_configure_narrow_argv
+_initialize_narrow_environment
+_initialize_onexit_table
+_register_onexit_function
+_execute_onexit_table
+_crt_atexit
+_crt_at_quick_exit
+_cexit
+_beginthreadex
+_time64
+strncmp
+strcmp
+qsort
+_stat64i32
+atoi
+__stdio_common_vsprintf