From 50dbe8314b1796d05e12c1a8a9b9c8b3242d8c5a Mon Sep 17 00:00:00 2001 From: Darren Tucker Date: Fri, 5 Nov 2004 20:41:24 +1100 Subject: [PATCH] - djm@cvs.openbsd.org 2004/10/29 23:56:17 [bufaux.c bufaux.h buffer.c buffer.h] introduce a new buffer API that returns an error rather than fatal()ing when presented with bad data; ok markus@ --- ChangeLog | 6 +- bufaux.c | 221 ++++++++++++++++++++++++++++++++++++++++++------------ bufaux.h | 12 ++- buffer.c | 50 +++++++++--- buffer.h | 6 +- 5 files changed, 234 insertions(+), 61 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3847553dc..f991fe7eb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -54,6 +54,10 @@ - djm@cvs.openbsd.org 2004/10/29 22:53:56 [clientloop.c misc.h readpass.c ssh-agent.c] factor out common permission-asking code to separate function; ok markus@ + - djm@cvs.openbsd.org 2004/10/29 23:56:17 + [bufaux.c bufaux.h buffer.c buffer.h] + introduce a new buffer API that returns an error rather than fatal()ing + when presented with bad data; ok markus@ 20041102 - (dtucker) [configure.ac includes.h] Bug #947: Fix compile error on HP-UX @@ -1833,4 +1837,4 @@ - (djm) Trim deprecated options from INSTALL. Mention UsePAM - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu -$Id: ChangeLog,v 1.3576 2004/11/05 09:38:03 dtucker Exp $ +$Id: ChangeLog,v 1.3577 2004/11/05 09:41:24 dtucker Exp $ diff --git a/bufaux.c b/bufaux.c index bf148316d..cbe77d5ae 100644 --- a/bufaux.c +++ b/bufaux.c @@ -37,7 +37,7 @@ */ #include "includes.h" -RCSID("$OpenBSD: bufaux.c,v 1.32 2004/02/23 15:12:46 markus Exp $"); +RCSID("$OpenBSD: bufaux.c,v 1.33 2004/10/29 23:56:17 djm Exp $"); #include #include "bufaux.h" @@ -49,8 +49,8 @@ RCSID("$OpenBSD: bufaux.c,v 1.32 2004/02/23 15:12:46 markus Exp $"); * Stores an BIGNUM in the buffer with a 2-byte msb first bit count, followed * by (bits+7)/8 bytes of binary data, msb first. */ -void -buffer_put_bignum(Buffer *buffer, const BIGNUM *value) +int +buffer_put_bignum_ret(Buffer *buffer, const BIGNUM *value) { int bits = BN_num_bits(value); int bin_size = (bits + 7) / 8; @@ -60,9 +60,11 @@ buffer_put_bignum(Buffer *buffer, const BIGNUM *value) /* Get the value of in binary */ oi = BN_bn2bin(value, buf); - if (oi != bin_size) - fatal("buffer_put_bignum: BN_bn2bin() failed: oi %d != bin_size %d", + if (oi != bin_size) { + error("buffer_put_bignum_ret: BN_bn2bin() failed: oi %d != bin_size %d", oi, bin_size); + return (-1); + } /* Store the number of bits in the buffer in two bytes, msb first. */ PUT_16BIT(msg, bits); @@ -72,36 +74,63 @@ buffer_put_bignum(Buffer *buffer, const BIGNUM *value) memset(buf, 0, bin_size); xfree(buf); + + return (0); +} + +void +buffer_put_bignum(Buffer *buffer, const BIGNUM *value) +{ + if (buffer_put_bignum_ret(buffer, value) == -1) + fatal("buffer_put_bignum: buffer error"); } /* * Retrieves an BIGNUM from the buffer. */ -void -buffer_get_bignum(Buffer *buffer, BIGNUM *value) +int +buffer_get_bignum_ret(Buffer *buffer, BIGNUM *value) { u_int bits, bytes; u_char buf[2], *bin; /* Get the number for bits. */ - buffer_get(buffer, (char *) buf, 2); + if (buffer_get_ret(buffer, (char *) buf, 2) == -1) { + error("buffer_get_bignum_ret: invalid length"); + return (-1); + } bits = GET_16BIT(buf); /* Compute the number of binary bytes that follow. */ bytes = (bits + 7) / 8; - if (bytes > 8 * 1024) - fatal("buffer_get_bignum: cannot handle BN of size %d", bytes); - if (buffer_len(buffer) < bytes) - fatal("buffer_get_bignum: input buffer too small"); + if (bytes > 8 * 1024) { + error("buffer_get_bignum_ret: cannot handle BN of size %d", bytes); + return (-1); + } + if (buffer_len(buffer) < bytes) { + error("buffer_get_bignum_ret: input buffer too small"); + return (-1); + } bin = buffer_ptr(buffer); BN_bin2bn(bin, bytes, value); - buffer_consume(buffer, bytes); + if (buffer_consume_ret(buffer, bytes) == -1) { + error("buffer_get_bignum_ret: buffer_consume failed"); + return (-1); + } + return (0); +} + +void +buffer_get_bignum(Buffer *buffer, BIGNUM *value) +{ + if (buffer_get_bignum_ret(buffer, value) == -1) + fatal("buffer_get_bignum: buffer error"); } /* * Stores an BIGNUM in the buffer in SSH2 format. */ -void -buffer_put_bignum2(Buffer *buffer, const BIGNUM *value) +int +buffer_put_bignum2_ret(Buffer *buffer, const BIGNUM *value) { u_int bytes; u_char *buf; @@ -110,69 +139,140 @@ buffer_put_bignum2(Buffer *buffer, const BIGNUM *value) if (BN_is_zero(value)) { buffer_put_int(buffer, 0); - return; + return 0; + } + if (value->neg) { + error("buffer_put_bignum2_ret: negative numbers not supported"); + return (-1); } - if (value->neg) - fatal("buffer_put_bignum2: negative numbers not supported"); bytes = BN_num_bytes(value) + 1; /* extra padding byte */ - if (bytes < 2) - fatal("buffer_put_bignum2: BN too small"); + if (bytes < 2) { + error("buffer_put_bignum2_ret: BN too small"); + return (-1); + } buf = xmalloc(bytes); buf[0] = '\0'; /* Get the value of in binary */ oi = BN_bn2bin(value, buf+1); - if (oi != bytes-1) - fatal("buffer_put_bignum2: BN_bn2bin() failed: " + if (oi != bytes-1) { + error("buffer_put_bignum2_ret: BN_bn2bin() failed: " "oi %d != bin_size %d", oi, bytes); + xfree(buf); + return (-1); + } hasnohigh = (buf[1] & 0x80) ? 0 : 1; buffer_put_string(buffer, buf+hasnohigh, bytes-hasnohigh); memset(buf, 0, bytes); xfree(buf); + return (0); +} + +void +buffer_put_bignum2(Buffer *buffer, const BIGNUM *value) +{ + if (buffer_put_bignum2_ret(buffer, value) == -1) + fatal("buffer_put_bignum2: buffer error"); +} + +int +buffer_get_bignum2_ret(Buffer *buffer, BIGNUM *value) +{ + u_int len; + u_char *bin; + + if ((bin = buffer_get_string_ret(buffer, &len)) == NULL) { + error("buffer_get_bignum2_ret: invalid bignum"); + return (-1); + } + + if (len > 0 && (bin[0] & 0x80)) { + error("buffer_get_bignum2_ret: negative numbers not supported"); + return (-1); + } + if (len > 8 * 1024) { + error("buffer_get_bignum2_ret: cannot handle BN of size %d", len); + return (-1); + } + BN_bin2bn(bin, len, value); + xfree(bin); + return (0); } void buffer_get_bignum2(Buffer *buffer, BIGNUM *value) { - u_int len; - u_char *bin = buffer_get_string(buffer, &len); - - if (len > 0 && (bin[0] & 0x80)) - fatal("buffer_get_bignum2: negative numbers not supported"); - if (len > 8 * 1024) - fatal("buffer_get_bignum2: cannot handle BN of size %d", len); - BN_bin2bn(bin, len, value); - xfree(bin); + if (buffer_get_bignum2_ret(buffer, value) == -1) + fatal("buffer_get_bignum2: buffer error"); } /* * Returns integers from the buffer (msb first). */ -u_short -buffer_get_short(Buffer *buffer) +int +buffer_get_short_ret(u_short *ret, Buffer *buffer) { u_char buf[2]; - buffer_get(buffer, (char *) buf, 2); - return GET_16BIT(buf); + if (buffer_get_ret(buffer, (char *) buf, 2) == -1) + return (-1); + *ret = GET_16BIT(buf); + return (0); +} + +u_short +buffer_get_short(Buffer *buffer) +{ + u_short ret; + + if (buffer_get_short_ret(&ret, buffer) == -1) + fatal("buffer_get_short: buffer error"); + + return (ret); +} + +int +buffer_get_int_ret(u_int *ret, Buffer *buffer) +{ + u_char buf[4]; + + if (buffer_get_ret(buffer, (char *) buf, 4) == -1) + return (-1); + *ret = GET_32BIT(buf); + return (0); } u_int buffer_get_int(Buffer *buffer) { - u_char buf[4]; + u_int ret; - buffer_get(buffer, (char *) buf, 4); - return GET_32BIT(buf); + if (buffer_get_int_ret(&ret, buffer) == -1) + fatal("buffer_get_int: buffer error"); + + return (ret); +} + +int +buffer_get_int64_ret(u_int64_t *ret, Buffer *buffer) +{ + u_char buf[8]; + + if (buffer_get_ret(buffer, (char *) buf, 8) == -1) + return (-1); + *ret = GET_64BIT(buf); + return (0); } u_int64_t buffer_get_int64(Buffer *buffer) { - u_char buf[8]; + u_int64_t ret; - buffer_get(buffer, (char *) buf, 8); - return GET_64BIT(buf); + if (buffer_get_int64_ret(&ret, buffer) == -1) + fatal("buffer_get_int: buffer error"); + + return (ret); } /* @@ -214,25 +314,41 @@ buffer_put_int64(Buffer *buffer, u_int64_t value) * to the returned string, and is not counted in length. */ void * -buffer_get_string(Buffer *buffer, u_int *length_ptr) +buffer_get_string_ret(Buffer *buffer, u_int *length_ptr) { u_char *value; u_int len; /* Get the length. */ len = buffer_get_int(buffer); - if (len > 256 * 1024) - fatal("buffer_get_string: bad string length %u", len); + if (len > 256 * 1024) { + error("buffer_get_string_ret: bad string length %u", len); + return (NULL); + } /* Allocate space for the string. Add one byte for a null character. */ value = xmalloc(len + 1); /* Get the string. */ - buffer_get(buffer, value, len); + if (buffer_get_ret(buffer, value, len) == -1) { + error("buffer_get_string_ret: buffer_get failed"); + xfree(value); + return (NULL); + } /* Append a null character to make processing easier. */ value[len] = 0; /* Optionally return the length of the string. */ if (length_ptr) *length_ptr = len; - return value; + return (value); +} + +void * +buffer_get_string(Buffer *buffer, u_int *length_ptr) +{ + void *ret; + + if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL) + fatal("buffer_get_string: buffer error"); + return (ret); } /* @@ -255,12 +371,23 @@ buffer_put_cstring(Buffer *buffer, const char *s) /* * Returns a character from the buffer (0 - 255). */ +int +buffer_get_char_ret(char *ret, Buffer *buffer) +{ + if (buffer_get_ret(buffer, ret, 1) == -1) { + error("buffer_get_char_ret: buffer_get_ret failed"); + return (-1); + } + return (0); +} + int buffer_get_char(Buffer *buffer) { char ch; - buffer_get(buffer, &ch, 1); + if (buffer_get_char_ret(&ch, buffer) == -1) + fatal("buffer_get_char: buffer error"); return (u_char) ch; } diff --git a/bufaux.h b/bufaux.h index 61c72e353..e30911ddc 100644 --- a/bufaux.h +++ b/bufaux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bufaux.h,v 1.19 2003/11/10 16:23:41 jakob Exp $ */ +/* $OpenBSD: bufaux.h,v 1.20 2004/10/29 23:56:17 djm Exp $ */ /* * Author: Tatu Ylonen @@ -42,4 +42,14 @@ void buffer_put_cstring(Buffer *, const char *); #define buffer_skip_string(b) \ do { u_int l = buffer_get_int(b); buffer_consume(b, l); } while(0) +int buffer_put_bignum_ret(Buffer *, const BIGNUM *); +int buffer_get_bignum_ret(Buffer *, BIGNUM *); +int buffer_put_bignum2_ret(Buffer *, const BIGNUM *); +int buffer_get_bignum2_ret(Buffer *, BIGNUM *); +int buffer_get_short_ret(u_short *, Buffer *); +int buffer_get_int_ret(u_int *, Buffer *); +int buffer_get_int64_ret(u_int64_t *, Buffer *); +void *buffer_get_string_ret(Buffer *, u_int *); +int buffer_get_char_ret(char *, Buffer *); + #endif /* BUFAUX_H */ diff --git a/buffer.c b/buffer.c index 9217cb269..1a25004ba 100644 --- a/buffer.c +++ b/buffer.c @@ -12,7 +12,7 @@ */ #include "includes.h" -RCSID("$OpenBSD: buffer.c,v 1.21 2003/11/21 11:57:03 djm Exp $"); +RCSID("$OpenBSD: buffer.c,v 1.22 2004/10/29 23:56:17 djm Exp $"); #include "xmalloc.h" #include "buffer.h" @@ -126,34 +126,62 @@ buffer_len(Buffer *buffer) /* Gets data from the beginning of the buffer. */ +int +buffer_get_ret(Buffer *buffer, void *buf, u_int len) +{ + if (len > buffer->end - buffer->offset) { + error("buffer_get_ret: trying to get more bytes %d than in buffer %d", + len, buffer->end - buffer->offset); + return (-1); + } + memcpy(buf, buffer->buf + buffer->offset, len); + buffer->offset += len; + return (0); +} + void buffer_get(Buffer *buffer, void *buf, u_int len) { - if (len > buffer->end - buffer->offset) - fatal("buffer_get: trying to get more bytes %d than in buffer %d", - len, buffer->end - buffer->offset); - memcpy(buf, buffer->buf + buffer->offset, len); - buffer->offset += len; + if (buffer_get_ret(buffer, buf, len) == -1) + fatal("buffer_get: buffer error"); } /* Consumes the given number of bytes from the beginning of the buffer. */ +int +buffer_consume_ret(Buffer *buffer, u_int bytes) +{ + if (bytes > buffer->end - buffer->offset) { + error("buffer_consume_ret: trying to get more bytes than in buffer"); + return (-1); + } + buffer->offset += bytes; + return (0); +} + void buffer_consume(Buffer *buffer, u_int bytes) { - if (bytes > buffer->end - buffer->offset) - fatal("buffer_consume: trying to get more bytes than in buffer"); - buffer->offset += bytes; + if (buffer_consume_ret(buffer, bytes) == -1) + fatal("buffer_consume: buffer error"); } /* Consumes the given number of bytes from the end of the buffer. */ +int +buffer_consume_end_ret(Buffer *buffer, u_int bytes) +{ + if (bytes > buffer->end - buffer->offset) + return (-1); + buffer->end -= bytes; + return (0); +} + void buffer_consume_end(Buffer *buffer, u_int bytes) { - if (bytes > buffer->end - buffer->offset) + if (buffer_consume_end_ret(buffer, bytes) == -1) fatal("buffer_consume_end: trying to get more bytes than in buffer"); - buffer->end -= bytes; } /* Returns a pointer to the first used byte in the buffer. */ diff --git a/buffer.h b/buffer.h index 5e4c41244..9c09d4f43 100644 --- a/buffer.h +++ b/buffer.h @@ -1,4 +1,4 @@ -/* $OpenBSD: buffer.h,v 1.11 2002/03/04 17:27:39 stevesk Exp $ */ +/* $OpenBSD: buffer.h,v 1.12 2004/10/29 23:56:17 djm Exp $ */ /* * Author: Tatu Ylonen @@ -40,4 +40,8 @@ void buffer_consume_end(Buffer *, u_int); void buffer_dump(Buffer *); +int buffer_get_ret(Buffer *, void *, u_int); +int buffer_consume_ret(Buffer *, u_int); +int buffer_consume_end_ret(Buffer *, u_int); + #endif /* BUFFER_H */