From 998300562e14f9d07293ec41e1aecca5930d5e6d Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 21 Jan 2013 19:07:56 +0100 Subject: Add error message for OOM on buffer alloc --- src/crypto.c | 2 +- src/fastd.c | 10 +++++----- src/fastd.h | 4 ++-- src/handshake.c | 6 +++--- src/method_aes128_gcm.c | 6 +++--- src/method_xsalsa20_poly1305.c | 6 +++--- src/protocol_ec25519_fhmqvc.c | 2 +- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/crypto.c b/src/crypto.c index e46f56d..05e7bcd 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -46,7 +46,7 @@ static fastd_crypto_aes128ctr_context_t* aes128ctr_init(fastd_context_t *ctx) { static fastd_crypto_aes128ctr_state_t* aes128ctr_set_key(fastd_context_t *ctx, const fastd_crypto_aes128ctr_context_t *cctx, const fastd_block128_t *key) { fastd_crypto_aes128ctr_state_t *cstate = malloc(sizeof(fastd_crypto_aes128ctr_state_t)); - cstate->d = fastd_buffer_alloc(crypto_stream_aes128ctr_BEFORENMBYTES, 0, 0); + cstate->d = fastd_buffer_alloc(ctx, crypto_stream_aes128ctr_BEFORENMBYTES, 0, 0); crypto_stream_aes128ctr_beforenm(cstate->d.data, key->b); return cstate; diff --git a/src/fastd.c b/src/fastd.c index c593ee5..e0351c6 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -536,7 +536,7 @@ void fastd_handle_receive(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer fastd_peer_t *dest_peer; for (dest_peer = ctx->peers; dest_peer; dest_peer = dest_peer->next) { if (dest_peer != peer && fastd_peer_is_established(dest_peer)) { - fastd_buffer_t send_buffer = fastd_buffer_alloc(buffer.len, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx)); + fastd_buffer_t send_buffer = fastd_buffer_alloc(ctx, buffer.len, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx)); memcpy(send_buffer.data, buffer.data, buffer.len); ctx->conf->protocol->send(ctx, dest_peer, send_buffer); } @@ -708,7 +708,7 @@ static void handle_tasks(fastd_context_t *ctx) { case TASK_KEEPALIVE: pr_debug(ctx, "sending keepalive to %P", task->peer); - ctx->conf->protocol->send(ctx, task->peer, fastd_buffer_alloc(0, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx))); + ctx->conf->protocol->send(ctx, task->peer, fastd_buffer_alloc(ctx, 0, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx))); break; default: @@ -721,7 +721,7 @@ static void handle_tasks(fastd_context_t *ctx) { static void handle_tun(fastd_context_t *ctx) { size_t max_len = fastd_max_packet_size(ctx); - fastd_buffer_t buffer = fastd_buffer_alloc(max_len, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx)); + fastd_buffer_t buffer = fastd_buffer_alloc(ctx, max_len, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx)); ssize_t len = read(ctx->tunfd, buffer.data, max_len); if (len < 0) { @@ -758,7 +758,7 @@ static void handle_tun(fastd_context_t *ctx) { if (peer == NULL) { for (peer = ctx->peers; peer; peer = peer->next) { if (fastd_peer_is_established(peer)) { - fastd_buffer_t send_buffer = fastd_buffer_alloc(len, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx)); + fastd_buffer_t send_buffer = fastd_buffer_alloc(ctx, len, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx)); memcpy(send_buffer.data, buffer.data, len); ctx->conf->protocol->send(ctx, peer, send_buffer); } @@ -770,7 +770,7 @@ static void handle_tun(fastd_context_t *ctx) { static void handle_socket(fastd_context_t *ctx, fastd_socket_t *sock) { size_t max_len = PACKET_TYPE_LEN + methods_max_packet_size(ctx); - fastd_buffer_t buffer = fastd_buffer_alloc(max_len, methods_min_decrypt_head_space(ctx), methods_min_decrypt_tail_space(ctx)); + fastd_buffer_t buffer = fastd_buffer_alloc(ctx, max_len, methods_min_decrypt_head_space(ctx), methods_min_decrypt_tail_space(ctx)); uint8_t *packet_type; fastd_peer_address_t recvaddr; diff --git a/src/fastd.h b/src/fastd.h index 2dd0996..9223ffe 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -363,11 +363,11 @@ static inline size_t alignto(size_t l, size_t a) { return ((l+a-1)/a)*a; } -static inline fastd_buffer_t fastd_buffer_alloc(size_t len, size_t head_space, size_t tail_space) { +static inline fastd_buffer_t fastd_buffer_alloc(const fastd_context_t *ctx, size_t len, size_t head_space, size_t tail_space) { size_t base_len = head_space+len+tail_space; void *ptr; if (posix_memalign(&ptr, 16, base_len)) - return (fastd_buffer_t){ .base = NULL, .base_len = 0, .data = NULL, .len = 0 }; + exit_errno(ctx, "posix_memalign"); return (fastd_buffer_t){ .base = ptr, .base_len = base_len, .data = ptr+head_space, .len = len }; } diff --git a/src/handshake.c b/src/handshake.c index ad19f8a..9273972 100644 --- a/src/handshake.c +++ b/src/handshake.c @@ -92,7 +92,7 @@ fastd_buffer_t fastd_handshake_new_init(fastd_context_t *ctx, size_t tail_space) size_t method_list_len; uint8_t *method_list = create_method_list(ctx, &method_list_len); - fastd_buffer_t buffer = fastd_buffer_alloc(sizeof(fastd_packet_t), 0, + fastd_buffer_t buffer = fastd_buffer_alloc(ctx, sizeof(fastd_packet_t), 0, 2*5 + /* handshake type, mode */ 6 + /* MTU */ 4+version_len + /* version name */ @@ -143,7 +143,7 @@ fastd_buffer_t fastd_handshake_new_reply(fastd_context_t *ctx, const fastd_hands extra_size = 6 + /* MTU */ 4+version_len; /* version name */ - fastd_buffer_t buffer = fastd_buffer_alloc(sizeof(fastd_packet_t), 0, + fastd_buffer_t buffer = fastd_buffer_alloc(ctx, sizeof(fastd_packet_t), 0, 2*5 + /* handshake type, reply code */ 4+method_len + /* method name */ extra_size + @@ -294,7 +294,7 @@ void fastd_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fa send_reply: if (reply_code) { - fastd_buffer_t reply_buffer = fastd_buffer_alloc(sizeof(fastd_packet_t), 0, 3*5 /* enough space for handshake type, reply code and error detail */); + fastd_buffer_t reply_buffer = fastd_buffer_alloc(ctx, sizeof(fastd_packet_t), 0, 3*5 /* enough space for handshake type, reply code and error detail */); fastd_packet_t *reply = reply_buffer.data; reply->rsv1 = 0; diff --git a/src/method_aes128_gcm.c b/src/method_aes128_gcm.c index dd93540..7dfabef 100644 --- a/src/method_aes128_gcm.c +++ b/src/method_aes128_gcm.c @@ -169,7 +169,7 @@ static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho memset(in.data, 0, sizeof(fastd_block128_t)); size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len; - *out = fastd_buffer_alloc(in.len, alignto(NONCEBYTES, 16), sizeof(fastd_block128_t)+tail_len); + *out = fastd_buffer_alloc(ctx, in.len, alignto(NONCEBYTES, 16), sizeof(fastd_block128_t)+tail_len); if (tail_len) memset(in.data+in.len, 0, tail_len); @@ -241,7 +241,7 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho fastd_buffer_push_head(&in, NONCEBYTES); size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len; - *out = fastd_buffer_alloc(in.len, 0, tail_len); + *out = fastd_buffer_alloc(ctx, in.len, 0, tail_len); int n_blocks = (in.len+sizeof(fastd_block128_t)-1)/sizeof(fastd_block128_t); @@ -282,7 +282,7 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho else if (age == 0 || session->receive_reorder_seen & (1 << (age-1))) { pr_debug(ctx, "dropping duplicate packet from %P (age %u)", peer, (unsigned)age); fastd_buffer_free(*out); - *out = fastd_buffer_alloc(0, 0, 0); + *out = fastd_buffer_alloc(ctx, 0, 0, 0); } else { pr_debug(ctx, "accepting reordered packet from %P (age %u)", peer, (unsigned)age); diff --git a/src/method_xsalsa20_poly1305.c b/src/method_xsalsa20_poly1305.c index 3e51ad9..655f61b 100644 --- a/src/method_xsalsa20_poly1305.c +++ b/src/method_xsalsa20_poly1305.c @@ -140,7 +140,7 @@ static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho fastd_buffer_pull_head(&in, crypto_secretbox_xsalsa20poly1305_ZEROBYTES); memset(in.data, 0, crypto_secretbox_xsalsa20poly1305_ZEROBYTES); - *out = fastd_buffer_alloc(in.len, 0, 0); + *out = fastd_buffer_alloc(ctx, in.len, 0, 0); uint8_t nonce[crypto_secretbox_xsalsa20poly1305_NONCEBYTES]; memcpy(nonce, session->send_nonce, NONCEBYTES); @@ -184,7 +184,7 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho fastd_buffer_pull_head(&in, crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES-NONCEBYTES); memset(in.data, 0, crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES); - *out = fastd_buffer_alloc(in.len, 0, 0); + *out = fastd_buffer_alloc(ctx, in.len, 0, 0); if (crypto_secretbox_xsalsa20poly1305_open(out->data, in.data, in.len, nonce, session->key) != 0) { fastd_buffer_free(*out); @@ -206,7 +206,7 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho else if (age == 0 || session->receive_reorder_seen & (1 << (age-1))) { pr_debug(ctx, "dropping duplicate packet from %P (age %u)", peer, (unsigned)age); fastd_buffer_free(*out); - *out = fastd_buffer_alloc(crypto_secretbox_xsalsa20poly1305_ZEROBYTES, 0, 0); + *out = fastd_buffer_alloc(ctx, crypto_secretbox_xsalsa20poly1305_ZEROBYTES, 0, 0); } else { pr_debug(ctx, "accepting reordered packet from %P (age %u)", peer, (unsigned)age); diff --git a/src/protocol_ec25519_fhmqvc.c b/src/protocol_ec25519_fhmqvc.c index 459c986..07fc887 100644 --- a/src/protocol_ec25519_fhmqvc.c +++ b/src/protocol_ec25519_fhmqvc.c @@ -767,7 +767,7 @@ static void protocol_send(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer } static void send_empty(fastd_context_t *ctx, fastd_peer_t *peer, protocol_session_t *session) { - session_send(ctx, peer, fastd_buffer_alloc(0, alignto(session->method->min_encrypt_head_space(ctx), 8), session->method->min_encrypt_tail_space(ctx)), session); + session_send(ctx, peer, fastd_buffer_alloc(ctx, 0, alignto(session->method->min_encrypt_head_space(ctx), 8), session->method->min_encrypt_tail_space(ctx)), session); } static void protocol_init_peer_state(fastd_context_t *ctx, fastd_peer_t *peer) { -- cgit v1.2.3