deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 1 | /* $OpenBSD: deattack.c,v 1.32 2015/01/20 23:14:00 deraadt Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3 | * Cryptographic attack detector for ssh - source code |
| 4 | * |
| 5 | * Copyright (c) 1998 CORE SDI S.A., Buenos Aires, Argentina. |
| 6 | * |
| 7 | * All rights reserved. Redistribution and use in source and binary |
| 8 | * forms, with or without modification, are permitted provided that |
| 9 | * this copyright notice is retained. |
| 10 | * |
| 11 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 12 | * WARRANTIES ARE DISCLAIMED. IN NO EVENT SHALL CORE SDI S.A. BE |
| 13 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR |
| 14 | * CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OR MISUSE OF THIS |
| 15 | * SOFTWARE. |
| 16 | * |
| 17 | * Ariel Futoransky <futo@core-sdi.com> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 18 | * <http://www.core-sdi.com> |
| 19 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 20 | |
| 21 | #include "includes.h" |
Ben Lindstrom | 05764b9 | 2002-03-05 01:53:02 +0000 | [diff] [blame] | 22 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 23 | #include <string.h> |
| 24 | #include <stdio.h> |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 26 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 27 | #include "deattack.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 28 | #include "crc32.h" |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 29 | #include "sshbuf.h" |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 30 | #include "misc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 31 | |
Damien Miller | 3c9c1fb | 2006-09-17 06:08:53 +1000 | [diff] [blame] | 32 | /* |
| 33 | * CRC attack detection has a worst-case behaviour that is O(N^3) over |
| 34 | * the number of identical blocks in a packet. This behaviour can be |
| 35 | * exploited to create a limited denial of service attack. |
| 36 | * |
| 37 | * However, because we are dealing with encrypted data, identical |
| 38 | * blocks should only occur every 2^35 maximally-sized packets or so. |
| 39 | * Consequently, we can detect this DoS by looking for identical blocks |
| 40 | * in a packet. |
| 41 | * |
| 42 | * The parameter below determines how many identical blocks we will |
| 43 | * accept in a single packet, trading off between attack detection and |
| 44 | * likelihood of terminating a legitimate connection. A value of 32 |
| 45 | * corresponds to an average of 2^40 messages before an attack is |
| 46 | * misdetected |
| 47 | */ |
| 48 | #define MAX_IDENTICAL 32 |
| 49 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | /* SSH Constants */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 51 | #define SSH_MAXBLOCKS (32 * 1024) |
| 52 | #define SSH_BLOCKSIZE (8) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 53 | |
| 54 | /* Hashing constants */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 55 | #define HASH_MINSIZE (8 * 1024) |
| 56 | #define HASH_ENTRYSIZE (2) |
| 57 | #define HASH_FACTOR(x) ((x)*3/2) |
| 58 | #define HASH_UNUSEDCHAR (0xff) |
| 59 | #define HASH_UNUSED (0xffff) |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 60 | #define HASH_IV (0xfffe) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 61 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 62 | #define HASH_MINBLOCKS (7*SSH_BLOCKSIZE) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 63 | |
| 64 | |
| 65 | /* Hash function (Input keys are cipher results) */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 66 | #define HASH(x) PEEK_U32(x) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 67 | |
Ben Lindstrom | 204e488 | 2001-03-05 06:47:00 +0000 | [diff] [blame] | 68 | #define CMP(a, b) (memcmp(a, b, SSH_BLOCKSIZE)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 70 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 71 | crc_update(u_int32_t *a, u_int32_t b) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 73 | b ^= *a; |
Damien Miller | 2dbbf8e | 2006-03-26 00:11:46 +1100 | [diff] [blame] | 74 | *a = ssh_crc32((u_char *)&b, sizeof(b)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 75 | } |
| 76 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 77 | /* detect if a block is used in a particular pattern */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 78 | static int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 79 | check_crc(const u_char *S, const u_char *buf, u_int32_t len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 81 | u_int32_t crc; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 82 | const u_char *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 83 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 84 | crc = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 85 | for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) { |
| 86 | if (!CMP(S, c)) { |
| 87 | crc_update(&crc, 1); |
| 88 | crc_update(&crc, 0); |
| 89 | } else { |
| 90 | crc_update(&crc, 0); |
| 91 | crc_update(&crc, 0); |
| 92 | } |
| 93 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 94 | return crc == 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 95 | } |
| 96 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 97 | void |
| 98 | deattack_init(struct deattack_ctx *dctx) |
| 99 | { |
| 100 | bzero(dctx, sizeof(*dctx)); |
| 101 | dctx->n = HASH_MINSIZE / HASH_ENTRYSIZE; |
| 102 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 103 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 104 | /* Detect a crc32 compensation attack on a packet */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 105 | int |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 106 | detect_attack(struct deattack_ctx *dctx, const u_char *buf, u_int32_t len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 107 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 108 | u_int32_t i, j, l, same; |
| 109 | u_int16_t *tmp; |
| 110 | const u_char *c, *d; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 111 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 112 | if (len > (SSH_MAXBLOCKS * SSH_BLOCKSIZE) || |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 113 | len % SSH_BLOCKSIZE != 0) |
| 114 | return DEATTACK_ERROR; |
| 115 | for (l = dctx->n; l < HASH_FACTOR(len / SSH_BLOCKSIZE); l = l << 2) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 116 | ; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 117 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 118 | if (dctx->h == NULL) { |
| 119 | if ((dctx->h = calloc(l, HASH_ENTRYSIZE)) == NULL) |
| 120 | return DEATTACK_ERROR; |
| 121 | dctx->n = l; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 122 | } else { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 123 | if (l > dctx->n) { |
| 124 | if ((tmp = reallocarray(dctx->h, l, HASH_ENTRYSIZE)) |
| 125 | == NULL) { |
| 126 | free(dctx->h); |
| 127 | dctx->h = NULL; |
| 128 | return DEATTACK_ERROR; |
| 129 | } |
| 130 | dctx->h = tmp; |
| 131 | dctx->n = l; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 132 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 133 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 134 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 135 | if (len <= HASH_MINBLOCKS) { |
| 136 | for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | for (d = buf; d < c; d += SSH_BLOCKSIZE) { |
| 138 | if (!CMP(c, d)) { |
Damien Miller | 2dbbf8e | 2006-03-26 00:11:46 +1100 | [diff] [blame] | 139 | if ((check_crc(c, buf, len))) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 140 | return DEATTACK_DETECTED; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 141 | else |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 146 | return DEATTACK_OK; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 147 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 148 | memset(dctx->h, HASH_UNUSEDCHAR, dctx->n * HASH_ENTRYSIZE); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 149 | |
Damien Miller | 3c9c1fb | 2006-09-17 06:08:53 +1000 | [diff] [blame] | 150 | for (c = buf, same = j = 0; c < (buf + len); c += SSH_BLOCKSIZE, j++) { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 151 | for (i = HASH(c) & (dctx->n - 1); dctx->h[i] != HASH_UNUSED; |
| 152 | i = (i + 1) & (dctx->n - 1)) { |
| 153 | if (!CMP(c, buf + dctx->h[i] * SSH_BLOCKSIZE)) { |
Damien Miller | 3c9c1fb | 2006-09-17 06:08:53 +1000 | [diff] [blame] | 154 | if (++same > MAX_IDENTICAL) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 155 | return DEATTACK_DOS_DETECTED; |
Damien Miller | 2dbbf8e | 2006-03-26 00:11:46 +1100 | [diff] [blame] | 156 | if (check_crc(c, buf, len)) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 157 | return DEATTACK_DETECTED; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 158 | else |
| 159 | break; |
| 160 | } |
| 161 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 162 | dctx->h[i] = j; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 163 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 164 | return DEATTACK_OK; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 165 | } |