"Fossies" - the Fresh Open Source Software Archive

Member "tor-0.4.1.6/src/test/fuzz/fuzzing_common.c" (10 Jun 2019, 5023 Bytes) of package /linux/misc/tor-0.4.1.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "fuzzing_common.c": 0.4.1.6_vs_0.4.2.4-rc.

    1 /* Copyright (c) 2016-2019, The Tor Project, Inc. */
    2 /* See LICENSE for licensing information */
    3 #define CRYPTO_ED25519_PRIVATE
    4 #include "orconfig.h"
    5 #include "core/or/or.h"
    6 #include "app/main/subsysmgr.h"
    7 #include "lib/err/backtrace.h"
    8 #include "app/config/config.h"
    9 #include "test/fuzz/fuzzing.h"
   10 #include "lib/compress/compress.h"
   11 #include "lib/crypt_ops/crypto_ed25519.h"
   12 #include "lib/crypt_ops/crypto_init.h"
   13 #include "lib/version/torversion.h"
   14 
   15 static or_options_t *mock_options = NULL;
   16 static const or_options_t *
   17 mock_get_options(void)
   18 {
   19   return mock_options;
   20 }
   21 
   22 static int
   23 mock_crypto_pk_public_checksig__nocheck(const crypto_pk_t *env, char *to,
   24                                         size_t tolen,
   25                                         const char *from, size_t fromlen)
   26 {
   27   tor_assert(env && to && from);
   28   (void)fromlen;
   29   /* We could look at from[0..fromlen-1] ... */
   30   tor_assert(tolen >= crypto_pk_keysize(env));
   31   size_t siglen = MIN(20, crypto_pk_keysize(env));
   32   memset(to, 0x01, siglen);
   33   return (int)siglen;
   34 }
   35 
   36 static int
   37 mock_crypto_pk_public_checksig_digest__nocheck(crypto_pk_t *env,
   38                                                const char *data,
   39                                                size_t datalen,
   40                                                const char *sig,
   41                                                size_t siglen)
   42 {
   43   tor_assert(env && data && sig);
   44   (void)datalen;
   45   (void)siglen;
   46   /* We could look at data[..] and sig[..] */
   47   return 0;
   48 }
   49 
   50 static int
   51 mock_ed25519_checksig__nocheck(const ed25519_signature_t *signature,
   52                       const uint8_t *msg, size_t len,
   53                       const ed25519_public_key_t *pubkey)
   54 {
   55   tor_assert(signature && msg && pubkey);
   56   /* We could look at msg[0..len-1] ... */
   57   (void)len;
   58   return 0;
   59 }
   60 
   61 static int
   62 mock_ed25519_checksig_batch__nocheck(int *okay_out,
   63                                      const ed25519_checkable_t *checkable,
   64                                      int n_checkable)
   65 {
   66   tor_assert(checkable);
   67   int i;
   68   for (i = 0; i < n_checkable; ++i) {
   69     /* We could look at messages and signatures XXX */
   70     tor_assert(checkable[i].pubkey);
   71     tor_assert(checkable[i].msg);
   72     if (okay_out)
   73       okay_out[i] = 1;
   74   }
   75   return 0;
   76 }
   77 
   78 static int
   79 mock_ed25519_impl_spot_check__nocheck(void)
   80 {
   81   return 0;
   82 }
   83 
   84 void
   85 disable_signature_checking(void)
   86 {
   87   MOCK(crypto_pk_public_checksig,
   88        mock_crypto_pk_public_checksig__nocheck);
   89   MOCK(crypto_pk_public_checksig_digest,
   90        mock_crypto_pk_public_checksig_digest__nocheck);
   91   MOCK(ed25519_checksig, mock_ed25519_checksig__nocheck);
   92   MOCK(ed25519_checksig_batch, mock_ed25519_checksig_batch__nocheck);
   93   MOCK(ed25519_impl_spot_check, mock_ed25519_impl_spot_check__nocheck);
   94 }
   95 
   96 static void
   97 global_init(void)
   98 {
   99   subsystems_init_upto(SUBSYS_LEVEL_LIBS);
  100   flush_log_messages_from_startup();
  101 
  102   tor_compress_init();
  103 
  104   if (crypto_global_init(0, NULL, NULL) < 0)
  105     abort();
  106 
  107   {
  108     struct sipkey sipkey = { 1337, 7331 };
  109     siphash_unset_global_key();
  110     siphash_set_global_key(&sipkey);
  111   }
  112 
  113   /* set up the options. */
  114   mock_options = tor_malloc_zero(sizeof(or_options_t));
  115   MOCK(get_options, mock_get_options);
  116 
  117   /* Make BUG() and nonfatal asserts crash */
  118   tor_set_failed_assertion_callback(abort);
  119 
  120   /* Make protocol warnings handled correctly. */
  121   init_protocol_warning_severity_level();
  122 }
  123 
  124 #ifdef LLVM_FUZZ
  125 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
  126 int
  127 LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
  128 {
  129   static int initialized = 0;
  130   if (!initialized) {
  131     global_init();
  132     if (fuzz_init() < 0)
  133       abort();
  134     initialized = 1;
  135   }
  136 
  137   return fuzz_main(Data, Size);
  138 }
  139 
  140 #else /* !(defined(LLVM_FUZZ)) */
  141 
  142 int
  143 main(int argc, char **argv)
  144 {
  145   size_t size;
  146 
  147   global_init();
  148 
  149   /* Disable logging by default to speed up fuzzing. */
  150   int loglevel = LOG_ERR;
  151 
  152   for (int i = 1; i < argc; ++i) {
  153     if (!strcmp(argv[i], "--warn")) {
  154       loglevel = LOG_WARN;
  155     } else if (!strcmp(argv[i], "--notice")) {
  156       loglevel = LOG_NOTICE;
  157     } else if (!strcmp(argv[i], "--info")) {
  158       loglevel = LOG_INFO;
  159     } else if (!strcmp(argv[i], "--debug")) {
  160       loglevel = LOG_DEBUG;
  161     }
  162   }
  163 
  164   {
  165     log_severity_list_t s;
  166     memset(&s, 0, sizeof(s));
  167     set_log_severity_config(loglevel, LOG_ERR, &s);
  168     /* ALWAYS log bug warnings. */
  169     s.masks[LOG_WARN-LOG_ERR] |= LD_BUG;
  170     add_stream_log(&s, "", fileno(stdout));
  171   }
  172 
  173   if (fuzz_init() < 0)
  174     abort();
  175 
  176 #ifdef __AFL_HAVE_MANUAL_CONTROL
  177   /* Tell AFL to pause and fork here - ignored if not using AFL */
  178   __AFL_INIT();
  179 #endif
  180 
  181 #define MAX_FUZZ_SIZE (128*1024)
  182   char *input = read_file_to_str_until_eof(0, MAX_FUZZ_SIZE, &size);
  183   tor_assert(input);
  184   char *raw = tor_memdup(input, size); /* Because input is nul-terminated */
  185   tor_free(input);
  186   fuzz_main((const uint8_t*)raw, size);
  187   tor_free(raw);
  188 
  189   if (fuzz_cleanup() < 0)
  190     abort();
  191 
  192   tor_free(mock_options);
  193   UNMOCK(get_options);
  194   return 0;
  195 }
  196 
  197 #endif /* defined(LLVM_FUZZ) */