Coverage Report

Created: 2025-07-11 06:19

/src/hpn-ssh/regress/misc/fuzz-harness/kex_fuzz.cc
Line
Count
Source (jump to first uncovered line)
1
// libfuzzer driver for key exchange fuzzing.
2
3
4
#include <sys/types.h>
5
#include <stdio.h>
6
#include <stdint.h>
7
#include <stdlib.h>
8
#include <string.h>
9
10
extern "C" {
11
12
#include "includes.h"
13
#include "ssherr.h"
14
#include "ssh_api.h"
15
#include "sshbuf.h"
16
#include "packet.h"
17
#include "myproposal.h"
18
#include "xmalloc.h"
19
#include "authfile.h"
20
#include "log.h"
21
22
#include "fixed-keys.h"
23
24
// Define if you want to generate traces.
25
/* #define STANDALONE 1 */
26
27
static int prepare_key(struct shared_state *st, int keytype, int bits);
28
29
struct shared_state {
30
  size_t nkeys;
31
  struct sshkey **privkeys, **pubkeys;
32
};
33
34
struct test_state {
35
  struct sshbuf *smsgs, *cmsgs; /* output, for standalone mode */
36
  struct sshbuf *sin, *cin; /* input; setup per-test in do_kex_with_key */
37
  struct sshbuf *s_template, *c_template; /* main copy of input */
38
};
39
40
static int
41
do_send_and_receive(struct ssh *from, struct ssh *to,
42
    struct sshbuf *store, int clobber, size_t *n)
43
22.5k
{
44
22.5k
  u_char type;
45
22.5k
  size_t len;
46
22.5k
  const u_char *buf;
47
22.5k
  int r;
48
49
38.0k
  for (*n = 0;; (*n)++) {
50
38.0k
    if ((r = ssh_packet_next(from, &type)) != 0) {
51
15.0k
      debug_fr(r, "ssh_packet_next");
52
15.0k
      return r;
53
15.0k
    }
54
23.0k
    if (type != 0)
55
0
      return 0;
56
23.0k
    buf = ssh_output_ptr(from, &len);
57
23.0k
    debug_f("%zu%s", len, clobber ? " ignore" : "");
58
23.0k
    if (len == 0)
59
7.50k
      return 0;
60
15.5k
    if ((r = ssh_output_consume(from, len)) != 0) {
61
0
      debug_fr(r, "ssh_output_consume");
62
0
      return r;
63
0
    }
64
15.5k
    if (store != NULL && (r = sshbuf_put(store, buf, len)) != 0) {
65
0
      debug_fr(r, "sshbuf_put");
66
0
      return r;
67
0
    }
68
15.5k
    if (!clobber && (r = ssh_input_append(to, buf, len)) != 0) {
69
0
      debug_fr(r, "ssh_input_append");
70
0
      return r;
71
0
    }
72
15.5k
  }
73
22.5k
}
74
75
static int
76
run_kex(struct test_state *ts, struct ssh *client, struct ssh *server)
77
15.0k
{
78
15.0k
  int r = 0;
79
15.0k
  size_t cn, sn;
80
81
  /* If fuzzing, replace server/client input */
82
15.0k
  if (ts->sin != NULL) {
83
7.50k
    if ((r = ssh_input_append(server, sshbuf_ptr(ts->sin),
84
7.50k
        sshbuf_len(ts->sin))) != 0) {
85
0
      error_fr(r, "ssh_input_append");
86
0
      return r;
87
0
    }
88
7.50k
    sshbuf_reset(ts->sin);
89
7.50k
  }
90
15.0k
  if (ts->cin != NULL) {
91
7.50k
    if ((r = ssh_input_append(client, sshbuf_ptr(ts->cin),
92
7.50k
        sshbuf_len(ts->cin))) != 0) {
93
0
      error_fr(r, "ssh_input_append");
94
0
      return r;
95
0
    }
96
7.50k
    sshbuf_reset(ts->cin);
97
7.50k
  }
98
15.0k
  while (!server->kex->done || !client->kex->done) {
99
15.0k
    cn = sn = 0;
100
15.0k
    debug_f("S:");
101
15.0k
    if ((r = do_send_and_receive(server, client,
102
15.0k
        ts->smsgs, ts->cin != NULL, &sn)) != 0) {
103
7.50k
      debug_fr(r, "S->C");
104
7.50k
      break;
105
7.50k
    }
106
7.50k
    debug_f("C:");
107
7.50k
    if ((r = do_send_and_receive(client, server,
108
7.50k
        ts->cmsgs, ts->sin != NULL, &cn)) != 0) {
109
7.50k
      debug_fr(r, "C->S");
110
7.50k
      break;
111
7.50k
    }
112
0
    if (cn == 0 && sn == 0) {
113
0
      debug_f("kex stalled");
114
0
      r = SSH_ERR_PROTOCOL_ERROR;
115
0
      break;
116
0
    }
117
0
  }
118
15.0k
  debug_fr(r, "done");
119
15.0k
  return r;
120
15.0k
}
121
122
static void
123
store_key(struct shared_state *st, struct sshkey *pubkey,
124
    struct sshkey *privkey)
125
3
{
126
3
  if (st == NULL || pubkey->type < 0 || pubkey->type > INT_MAX ||
127
3
      privkey->type != pubkey->type ||
128
3
      ((size_t)pubkey->type < st->nkeys &&
129
3
       st->pubkeys[pubkey->type] != NULL))
130
0
    abort();
131
3
  if ((size_t)pubkey->type >= st->nkeys) {
132
3
    st->pubkeys = (struct sshkey **)xrecallocarray(st->pubkeys,
133
3
        st->nkeys, pubkey->type + 1, sizeof(*st->pubkeys));
134
3
    st->privkeys = (struct sshkey **)xrecallocarray(st->privkeys,
135
3
        st->nkeys, privkey->type + 1, sizeof(*st->privkeys));
136
3
    st->nkeys = privkey->type + 1;
137
3
  }
138
3
  debug_f("store %s at %d", sshkey_ssh_name(pubkey), pubkey->type);
139
3
  st->pubkeys[pubkey->type] = pubkey;
140
3
  st->privkeys[privkey->type] = privkey;
141
3
}
142
143
static int
144
prepare_keys(struct shared_state *st)
145
1
{
146
1
  if (prepare_key(st, KEY_RSA, 2048) != 0 ||
147
1
      prepare_key(st, KEY_ECDSA, 256) != 0 ||
148
1
      prepare_key(st, KEY_ED25519, 256) != 0) {
149
0
    error_f("key prepare failed");
150
0
    return -1;
151
0
  }
152
1
  return 0;
153
1
}
154
155
static struct sshkey *
156
get_pubkey(struct shared_state *st, int keytype)
157
15.0k
{
158
15.0k
  if (st == NULL || keytype < 0 || (size_t)keytype >= st->nkeys ||
159
15.0k
      st->pubkeys == NULL || st->pubkeys[keytype] == NULL)
160
0
    abort();
161
15.0k
  return st->pubkeys[keytype];
162
15.0k
}
163
164
static struct sshkey *
165
get_privkey(struct shared_state *st, int keytype)
166
15.0k
{
167
15.0k
  if (st == NULL || keytype < 0 || (size_t)keytype >= st->nkeys ||
168
15.0k
      st->privkeys == NULL || st->privkeys[keytype] == NULL)
169
0
    abort();
170
15.0k
  return st->privkeys[keytype];
171
15.0k
}
172
173
static int
174
do_kex_with_key(struct shared_state *st, struct test_state *ts,
175
    const char *kex, int keytype)
176
15.0k
{
177
15.0k
  struct ssh *client = NULL, *server = NULL;
178
15.0k
  struct sshkey *privkey = NULL, *pubkey = NULL;
179
15.0k
  struct sshbuf *state = NULL;
180
15.0k
  struct kex_params kex_params;
181
15.0k
  const char *ccp, *proposal[PROPOSAL_MAX] = { KEX_CLIENT };
182
15.0k
  char *myproposal[PROPOSAL_MAX] = {0}, *keyname = NULL;
183
15.0k
  int i, r;
184
185
15.0k
  ts->cin = ts->sin = NULL;
186
15.0k
  if (ts->c_template != NULL &&
187
15.0k
      (ts->cin = sshbuf_fromb(ts->c_template)) == NULL)
188
0
    abort();
189
15.0k
  if (ts->s_template != NULL &&
190
15.0k
      (ts->sin = sshbuf_fromb(ts->s_template)) == NULL)
191
0
    abort();
192
193
15.0k
  pubkey = get_pubkey(st, keytype);
194
15.0k
  privkey = get_privkey(st, keytype);
195
15.0k
  keyname = xstrdup(sshkey_ssh_name(privkey));
196
15.0k
  if (ts->cin != NULL) {
197
7.50k
    debug_f("%s %s clobber client %zu", kex, keyname,
198
7.50k
        sshbuf_len(ts->cin));
199
7.50k
  } else if (ts->sin != NULL) {
200
7.50k
    debug_f("%s %s clobber server %zu", kex, keyname,
201
7.50k
        sshbuf_len(ts->sin));
202
7.50k
  } else
203
0
    debug_f("%s %s noclobber", kex, keyname);
204
205
165k
  for (i = 0; i < PROPOSAL_MAX; i++) {
206
150k
    ccp = proposal[i];
207
150k
#ifdef CIPHER_NONE_AVAIL
208
150k
    if (i == PROPOSAL_ENC_ALGS_CTOS || i == PROPOSAL_ENC_ALGS_STOC)
209
30.0k
      ccp = "none";
210
150k
#endif
211
150k
    if (i == PROPOSAL_SERVER_HOST_KEY_ALGS)
212
15.0k
      ccp = keyname;
213
135k
    else if (i == PROPOSAL_KEX_ALGS && kex != NULL)
214
15.0k
      ccp = kex;
215
150k
    if ((myproposal[i] = strdup(ccp)) == NULL) {
216
0
      error_f("strdup prop %d", i);
217
0
      goto fail;
218
0
    }
219
150k
  }
220
15.0k
  memcpy(kex_params.proposal, myproposal, sizeof(myproposal));
221
15.0k
  if ((r = ssh_init(&client, 0, &kex_params)) != 0) {
222
0
    error_fr(r, "init client");
223
0
    goto fail;
224
0
  }
225
15.0k
  if ((r = ssh_init(&server, 1, &kex_params)) != 0) {
226
0
    error_fr(r, "init server");
227
0
    goto fail;
228
0
  }
229
15.0k
  if ((r = ssh_add_hostkey(server, privkey)) != 0 ||
230
15.0k
      (r = ssh_add_hostkey(client, pubkey)) != 0) {
231
0
    error_fr(r, "add hostkeys");
232
0
    goto fail;
233
0
  }
234
15.0k
  if ((r = run_kex(ts, client, server)) != 0) {
235
15.0k
    error_fr(r, "kex");
236
15.0k
    goto fail;
237
15.0k
  }
238
  /* XXX rekex, set_state, etc */
239
15.0k
 fail:
240
165k
  for (i = 0; i < PROPOSAL_MAX; i++)
241
150k
    free(myproposal[i]);
242
15.0k
  sshbuf_free(ts->sin);
243
15.0k
  sshbuf_free(ts->cin);
244
15.0k
  sshbuf_free(state);
245
15.0k
  ssh_free(client);
246
15.0k
  ssh_free(server);
247
15.0k
  free(keyname);
248
15.0k
  return r;
249
15.0k
}
250
251
static int
252
prepare_key(struct shared_state *st, int kt, int bits)
253
3
{
254
3
  const char *pubstr = NULL;
255
3
  const char *privstr = NULL;
256
3
  char *tmp, *cp;
257
3
  struct sshkey *privkey = NULL, *pubkey = NULL;
258
3
  struct sshbuf *b = NULL;
259
3
  int r;
260
261
3
  switch (kt) {
262
1
  case KEY_RSA:
263
1
    pubstr = PUB_RSA;
264
1
    privstr = PRIV_RSA;
265
1
    break;
266
1
  case KEY_ECDSA:
267
1
    pubstr = PUB_ECDSA;
268
1
    privstr = PRIV_ECDSA;
269
1
    break;
270
1
  case KEY_ED25519:
271
1
    pubstr = PUB_ED25519;
272
1
    privstr = PRIV_ED25519;
273
1
    break;
274
0
  default:
275
0
    abort();
276
3
  }
277
3
  if ((b = sshbuf_from(privstr, strlen(privstr))) == NULL)
278
0
    abort();
279
3
  if ((r = sshkey_parse_private_fileblob(b, "", &privkey, NULL)) != 0) {
280
0
    error_fr(r, "priv %d", kt);
281
0
    abort();
282
0
  }
283
3
  sshbuf_free(b);
284
3
  tmp = cp = xstrdup(pubstr);
285
3
  if ((pubkey = sshkey_new(KEY_UNSPEC)) == NULL)
286
0
    abort();
287
3
  if ((r = sshkey_read(pubkey, &cp)) != 0) {
288
0
    error_fr(r, "pub %d", kt);
289
0
    abort();
290
0
  }
291
3
  free(tmp);
292
293
3
  store_key(st, pubkey, privkey);
294
3
  return 0;
295
3
}
296
297
#if defined(STANDALONE)
298
299
#if 0 /* use this if generating new keys to embed above */
300
static int
301
prepare_key(struct shared_state *st, int keytype, int bits)
302
{
303
  struct sshkey *privkey = NULL, *pubkey = NULL;
304
  int r;
305
306
  if ((r = sshkey_generate(keytype, bits, &privkey)) != 0) {
307
    error_fr(r, "generate");
308
    abort();
309
  }
310
  if ((r = sshkey_from_private(privkey, &pubkey)) != 0) {
311
    error_fr(r, "make pubkey");
312
    abort();
313
  }
314
  store_key(st, pubkey, privkey);
315
  return 0;
316
}
317
#endif
318
319
int main(void)
320
{
321
  static struct shared_state *st;
322
  struct test_state *ts;
323
  const int keytypes[] = { KEY_RSA, KEY_ECDSA, KEY_ED25519, -1 };
324
  static const char * const kextypes[] = {
325
    "sntrup761x25519-sha512@openssh.com",
326
    "curve25519-sha256@libssh.org",
327
    "ecdh-sha2-nistp256",
328
    "diffie-hellman-group1-sha1",
329
    "diffie-hellman-group-exchange-sha1",
330
    NULL,
331
  };
332
  int i, j;
333
  char *path;
334
  FILE *f;
335
336
  log_init("kex_fuzz", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 1);
337
338
  if (st == NULL) {
339
    st = (struct shared_state *)xcalloc(1, sizeof(*st));
340
    prepare_keys(st);
341
  }
342
  /* Run each kex method for each key and save client/server packets */
343
  for (i = 0; keytypes[i] != -1; i++) {
344
    for (j = 0; kextypes[j] != NULL; j++) {
345
      ts = (struct test_state *)xcalloc(1, sizeof(*ts));
346
      ts->smsgs = sshbuf_new();
347
      ts->cmsgs = sshbuf_new();
348
      do_kex_with_key(st, ts, kextypes[j], keytypes[i]);
349
      xasprintf(&path, "S2C-%s-%s",
350
          kextypes[j], sshkey_type(st->pubkeys[keytypes[i]]));
351
      debug_f("%s", path);
352
      if ((f = fopen(path, "wb+")) == NULL)
353
        abort();
354
      if (fwrite(sshbuf_ptr(ts->smsgs), 1,
355
          sshbuf_len(ts->smsgs), f) != sshbuf_len(ts->smsgs))
356
        abort();
357
      fclose(f);
358
      free(path);
359
      //sshbuf_dump(ts->smsgs, stderr);
360
      xasprintf(&path, "C2S-%s-%s",
361
          kextypes[j], sshkey_type(st->pubkeys[keytypes[i]]));
362
      debug_f("%s", path);
363
      if ((f = fopen(path, "wb+")) == NULL)
364
        abort();
365
      if (fwrite(sshbuf_ptr(ts->cmsgs), 1,
366
          sshbuf_len(ts->cmsgs), f) != sshbuf_len(ts->cmsgs))
367
        abort();
368
      fclose(f);
369
      free(path);
370
      //sshbuf_dump(ts->cmsgs, stderr);
371
      sshbuf_free(ts->smsgs);
372
      sshbuf_free(ts->cmsgs);
373
      free(ts);
374
    }
375
  }
376
  for (i = 0; keytypes[i] != -1; i++) {
377
    xasprintf(&path, "%s.priv",
378
        sshkey_type(st->privkeys[keytypes[i]]));
379
    debug_f("%s", path);
380
    if (sshkey_save_private(st->privkeys[keytypes[i]], path,
381
        "", "", SSHKEY_PRIVATE_OPENSSH, NULL, 0) != 0)
382
      abort();
383
    free(path);
384
    xasprintf(&path, "%s.pub",
385
        sshkey_type(st->pubkeys[keytypes[i]]));
386
    debug_f("%s", path);
387
    if (sshkey_save_public(st->pubkeys[keytypes[i]], path, "") != 0)
388
      abort();
389
    free(path);
390
  }
391
}
392
#else /* !STANDALONE */
393
static void
394
do_kex(struct shared_state *st, struct test_state *ts, const char *kex)
395
5.00k
{
396
5.00k
  do_kex_with_key(st, ts, kex, KEY_RSA);
397
5.00k
  do_kex_with_key(st, ts, kex, KEY_ECDSA);
398
5.00k
  do_kex_with_key(st, ts, kex, KEY_ED25519);
399
5.00k
}
400
401
static void
402
kex_tests(struct shared_state *st, struct test_state *ts)
403
1.00k
{
404
1.00k
  do_kex(st, ts, "sntrup761x25519-sha512@openssh.com");
405
1.00k
  do_kex(st, ts, "curve25519-sha256@libssh.org");
406
1.00k
  do_kex(st, ts, "ecdh-sha2-nistp256");
407
1.00k
  do_kex(st, ts, "diffie-hellman-group1-sha1");
408
1.00k
  do_kex(st, ts, "diffie-hellman-group-exchange-sha1");
409
1.00k
}
410
411
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
412
500
{
413
500
  static struct shared_state *st;
414
500
  struct test_state *ts;
415
500
  u_char crbuf[SSH_MAX_PRE_BANNER_LINES * 4];
416
500
  u_char zbuf[4096] = {0};
417
500
  static LogLevel loglevel = SYSLOG_LEVEL_INFO;
418
419
500
  if (st == NULL) {
420
1
    if (getenv("DEBUG") != NULL || getenv("KEX_FUZZ_DEBUG") != NULL)
421
0
      loglevel = SYSLOG_LEVEL_DEBUG3;
422
1
    log_init("kex_fuzz",
423
1
        loglevel, SYSLOG_FACILITY_AUTH, 1);
424
1
    st = (struct shared_state *)xcalloc(1, sizeof(*st));
425
1
    prepare_keys(st);
426
1
  }
427
428
  /* Ensure that we can complete (fail) banner exchange at least */
429
500
  memset(crbuf, '\n', sizeof(crbuf));
430
431
500
  ts = (struct test_state *)xcalloc(1, sizeof(*ts));
432
500
  if ((ts->s_template = sshbuf_new()) == NULL ||
433
500
      sshbuf_put(ts->s_template, data, size) != 0 ||
434
500
      sshbuf_put(ts->s_template, crbuf, sizeof(crbuf)) != 0 ||
435
500
      sshbuf_put(ts->s_template, zbuf, sizeof(zbuf)) != 0)
436
0
    abort();
437
500
  kex_tests(st, ts);
438
500
  sshbuf_free(ts->s_template);
439
500
  free(ts);
440
441
500
  ts = (struct test_state *)xcalloc(1, sizeof(*ts));
442
500
  if ((ts->c_template = sshbuf_new()) == NULL ||
443
500
      sshbuf_put(ts->c_template, data, size) != 0 ||
444
500
      sshbuf_put(ts->c_template, crbuf, sizeof(crbuf)) != 0 ||
445
500
      sshbuf_put(ts->c_template, zbuf, sizeof(zbuf)) != 0)
446
0
    abort();
447
500
  kex_tests(st, ts);
448
500
  sshbuf_free(ts->c_template);
449
500
  free(ts);
450
451
500
  return 0;
452
500
}
453
#endif /* STANDALONE */
454
} /* extern "C" */