From 6adc48fa2d165fb7522351375ea9e1c0ed01d714 Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Thu, 7 Mar 2002 20:11:49 +0000 Subject: [PATCH] updates. --- CHANGES | 6 ++ TODO-1.0 | 8 --- apps/silcd/serverconfig.c | 7 +- lib/silccore/silcargument.c | 59 +++++++++-------- lib/silccore/silcauth.c | 66 +++++++++++-------- lib/silccore/silcchannel.c | 124 ++++++++++++++++++++---------------- lib/silccore/silccommand.c | 72 +++++++++++++++------ lib/silccore/silcid.c | 35 +++++++--- lib/silccore/silcidcache.c | 25 ++++++++ lib/silccore/silcnotify.c | 43 +++++++++---- lib/silccore/silcpacket.c | 4 ++ lib/silccore/silcprivate.c | 23 ++++--- lib/silcutil/silcbuffer.h | 33 ++++++++-- 13 files changed, 329 insertions(+), 176 deletions(-) diff --git a/CHANGES b/CHANGES index 7b9d3ec8..78b61345 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,12 @@ Thu Mar 7 19:21:22 EET 2002 Pekka Riikonen * Added more error printing to logs in server code. Affected files silcd/server.c and silcd/protocol.c. + * Fixed -S option parsing in Irssi SILC Client. Affected file + irssi/src/silc/core/silc-core.c. + + * Added silc_buffer_alloc_size function. Affected file is + lib/silcutil/silcbuffer.h. + Tue Mar 5 14:37:27 EET 2002 Pekka Riikonen * Changed all silc_[hash|hmac|cipher|pkcs]_default tables to diff --git a/TODO-1.0 b/TODO-1.0 index 601712ea..cc751428 100644 --- a/TODO-1.0 +++ b/TODO-1.0 @@ -90,14 +90,6 @@ least could be done. SILC_BUFFER_LEN macro can do the same. These would save totally 8 bytes of memory per buffer. - Add also perhaps function silc_buffer_alloc_size that would - effectively do: - - return silc_buffer_pull_tail(silc_buffer_alloc(size), - size); - - to not require the user to give the pull_tail anymore. - o Scheduler can be optimized for FD tasks by changing the fd_queue to SilcHashTable instead of using linked list. We need to do one-to-one mapping of FD to task and hash table is more efficient diff --git a/apps/silcd/serverconfig.c b/apps/silcd/serverconfig.c index 09fde607..e0f02915 100644 --- a/apps/silcd/serverconfig.c +++ b/apps/silcd/serverconfig.c @@ -1293,7 +1293,7 @@ bool silc_server_config_register_ciphers(SilcServer server) int i; for (i = 0; silc_default_ciphers[i].name; i++) if (!strcmp(silc_default_ciphers[i].name, cipher->name)) { - silc_cipher_register(&silc_default_ciphers[i]); + silc_cipher_register((SilcCipherObject *)&silc_default_ciphers[i]); break; } if (!silc_cipher_is_supported(cipher->name)) { @@ -1394,7 +1394,7 @@ bool silc_server_config_register_hashfuncs(SilcServer server) int i; for (i = 0; silc_default_hash[i].name; i++) if (!strcmp(silc_default_hash[i].name, hash->name)) { - silc_hash_register(&silc_default_hash[i]); + silc_hash_register((SilcHashObject *)&silc_default_hash[i]); break; } if (!silc_hash_is_supported(hash->name)) { @@ -1478,6 +1478,7 @@ bool silc_server_config_register_hmacs(SilcServer server) silc_server_stop(server); exit(1); } + /* Register the HMAC */ memset(&hmac_obj, 0, sizeof(hmac_obj)); hmac_obj.name = hmac->name; @@ -1506,7 +1507,7 @@ bool silc_server_config_register_pkcs(SilcServer server) int i; for (i = 0; silc_default_pkcs[i].name; i++) if (!strcmp(silc_default_pkcs[i].name, pkcs->name)) { - silc_pkcs_register(&silc_default_pkcs[i]); + silc_pkcs_register((SilcPKCSObject *)&silc_default_pkcs[i]); break; } if (!silc_pkcs_is_supported(pkcs->name)) { diff --git a/lib/silccore/silcargument.c b/lib/silccore/silcargument.c index efd2cafd..dd470a37 100644 --- a/lib/silccore/silcargument.c +++ b/lib/silccore/silcargument.c @@ -43,7 +43,7 @@ SilcArgumentPayload silc_argument_payload_parse(const unsigned char *payload, SilcUInt32 argc) { SilcBufferStruct buffer; - SilcArgumentPayload new; + SilcArgumentPayload newp; SilcUInt16 p_len = 0; unsigned char arg_num = 0; unsigned char arg_type = 0; @@ -53,10 +53,18 @@ SilcArgumentPayload silc_argument_payload_parse(const unsigned char *payload, SILC_LOG_DEBUG(("Parsing argument payload")); silc_buffer_set(&buffer, (unsigned char *)payload, payload_len); - new = silc_calloc(1, sizeof(*new)); - new->argv = silc_calloc(argc, sizeof(unsigned char *)); - new->argv_lens = silc_calloc(argc, sizeof(SilcUInt32)); - new->argv_types = silc_calloc(argc, sizeof(SilcUInt32)); + newp = silc_calloc(1, sizeof(*newp)); + if (!newp) + return NULL; + newp->argv = silc_calloc(argc, sizeof(unsigned char *)); + if (!newp->argv) + goto err; + newp->argv_lens = silc_calloc(argc, sizeof(SilcUInt32)); + if (!newp->argv_lens) + goto err; + newp->argv_types = silc_calloc(argc, sizeof(SilcUInt32)); + if (!newp->argv_types) + goto err; /* Get arguments */ arg_num = 1; @@ -68,8 +76,8 @@ SilcArgumentPayload silc_argument_payload_parse(const unsigned char *payload, if (ret == -1) goto err; - new->argv_lens[i] = p_len; - new->argv_types[i] = arg_type; + newp->argv_lens[i] = p_len; + newp->argv_types[i] = arg_type; if (p_len > buffer.len - 3) break; @@ -77,7 +85,7 @@ SilcArgumentPayload silc_argument_payload_parse(const unsigned char *payload, /* Get argument data */ silc_buffer_pull(&buffer, 3); ret = silc_buffer_unformat(&buffer, - SILC_STR_UI_XNSTRING_ALLOC(&new->argv[i], + SILC_STR_UI_XNSTRING_ALLOC(&newp->argv[i], p_len), SILC_STR_END); if (ret == -1) @@ -90,27 +98,22 @@ SilcArgumentPayload silc_argument_payload_parse(const unsigned char *payload, if (buffer.len != 0) goto err; - new->argc = argc; - new->pos = 0; + newp->argc = argc; + newp->pos = 0; silc_buffer_push(&buffer, pull_len); - return new; + return newp; err: - if (i) { - int k; + if (i) + for (ret = 0; ret < i; ret++) + silc_free(newp->argv[ret]); - for (k = 0; k < i; k++) - silc_free(new->argv[k]); - } - - silc_free(new->argv); - silc_free(new->argv_lens); - silc_free(new->argv_types); - - if (new) - silc_free(new); + silc_free(newp->argv); + silc_free(newp->argv_lens); + silc_free(newp->argv_types); + silc_free(newp); return NULL; } @@ -132,8 +135,9 @@ SilcBuffer silc_argument_payload_encode(SilcUInt32 argc, for (i = 0; i < argc; i++) len += 3 + argv_lens[i]; - buffer = silc_buffer_alloc(len); - silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer)); + buffer = silc_buffer_alloc_size(len); + if (!buffer) + return NULL; /* Put arguments */ for (i = 0; i < argc; i++) { @@ -165,8 +169,9 @@ SilcBuffer silc_argument_payload_encode_payload(SilcArgumentPayload payload) for (i = 0; i < payload->argc; i++) len += 3 + payload->argv_lens[i]; - buffer = silc_buffer_alloc(len); - silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer)); + buffer = silc_buffer_alloc_size(len); + if (!buffer) + return NULL; /* Put arguments */ for (i = 0; i < payload->argc; i++) { diff --git a/lib/silccore/silcauth.c b/lib/silccore/silcauth.c index 23029640..b3fb4558 100644 --- a/lib/silccore/silcauth.c +++ b/lib/silccore/silcauth.c @@ -44,40 +44,42 @@ SilcAuthPayload silc_auth_payload_parse(const unsigned char *data, SilcUInt32 data_len) { SilcBufferStruct buffer; - SilcAuthPayload new; + SilcAuthPayload newp; int ret; SILC_LOG_DEBUG(("Parsing Authentication Payload")); silc_buffer_set(&buffer, (unsigned char *)data, data_len); - new = silc_calloc(1, sizeof(*new)); + newp = silc_calloc(1, sizeof(*newp)); + if (!newp) + return NULL; /* Parse the payload */ ret = silc_buffer_unformat(&buffer, - SILC_STR_UI_SHORT(&new->len), - SILC_STR_UI_SHORT(&new->auth_method), - SILC_STR_UI16_NSTRING_ALLOC(&new->random_data, - &new->random_len), - SILC_STR_UI16_NSTRING_ALLOC(&new->auth_data, - &new->auth_len), + SILC_STR_UI_SHORT(&newp->len), + SILC_STR_UI_SHORT(&newp->auth_method), + SILC_STR_UI16_NSTRING_ALLOC(&newp->random_data, + &newp->random_len), + SILC_STR_UI16_NSTRING_ALLOC(&newp->auth_data, + &newp->auth_len), SILC_STR_END); if (ret == -1) { - silc_free(new); + silc_free(newp); return NULL; } - if (new->len != buffer.len) { - silc_auth_payload_free(new); + if (newp->len != buffer.len) { + silc_auth_payload_free(newp); return NULL; } /* If password authentication, random data must not be set */ - if (new->auth_method == SILC_AUTH_PASSWORD && new->random_len) { - silc_auth_payload_free(new); + if (newp->auth_method == SILC_AUTH_PASSWORD && newp->random_len) { + silc_auth_payload_free(newp); return NULL; } - return new; + return newp; } /* Encodes authentication payload into buffer and returns it */ @@ -94,8 +96,9 @@ SilcBuffer silc_auth_payload_encode(SilcAuthMethod method, SILC_LOG_DEBUG(("Encoding Authentication Payload")); len = 2 + 2 + 2 + random_len + 2 + auth_len; - buffer = silc_buffer_alloc(len); - silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer)); + buffer = silc_buffer_alloc_size(len); + if (!buffer) + return NULL; silc_buffer_format(buffer, SILC_STR_UI_SHORT(len), SILC_STR_UI_SHORT(method), @@ -173,8 +176,12 @@ silc_auth_public_key_encode_data(SilcPublicKey public_key, } id_len = silc_id_get_len(id, type); - buf = silc_buffer_alloc(random_len + id_len + pk_len); - silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf)); + buf = silc_buffer_alloc_size(random_len + id_len + pk_len); + if (!buf) { + silc_free(pk); + silc_free(id_data); + return NULL; + } silc_buffer_format(buf, SILC_STR_UI_XNSTRING(random, random_len), SILC_STR_UI_XNSTRING(id_data, id_len), @@ -182,6 +189,8 @@ silc_auth_public_key_encode_data(SilcPublicKey public_key, SILC_STR_END); ret = silc_memdup(buf->data, buf->len); + if (!ret) + return NULL; if (ret_len) *ret_len = buf->len; @@ -420,26 +429,28 @@ silc_key_agreement_payload_parse(const unsigned char *payload, SilcUInt32 payload_len) { SilcBufferStruct buffer; - SilcKeyAgreementPayload new; + SilcKeyAgreementPayload newp; int ret; SILC_LOG_DEBUG(("Parsing Key Agreement Payload")); silc_buffer_set(&buffer, (unsigned char *)payload, payload_len); - new = silc_calloc(1, sizeof(*new)); + newp = silc_calloc(1, sizeof(*newp)); + if (!newp) + return NULL; /* Parse the payload */ ret = silc_buffer_unformat(&buffer, - SILC_STR_UI16_NSTRING_ALLOC(&new->hostname, - &new->hostname_len), - SILC_STR_UI_INT(&new->port), + SILC_STR_UI16_NSTRING_ALLOC(&newp->hostname, + &newp->hostname_len), + SILC_STR_UI_INT(&newp->port), SILC_STR_END); if (ret == -1) { - silc_free(new); + silc_free(newp); return NULL; } - return new; + return newp; } /* Encodes the Key Agreement protocol and returns the encoded buffer */ @@ -452,8 +463,9 @@ SilcBuffer silc_key_agreement_payload_encode(const char *hostname, SILC_LOG_DEBUG(("Encoding Key Agreement Payload")); - buffer = silc_buffer_alloc(2 + len + 4); - silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer)); + buffer = silc_buffer_alloc_size(2 + len + 4); + if (!buffer) + return NULL; silc_buffer_format(buffer, SILC_STR_UI_SHORT(len), SILC_STR_UI_XNSTRING(hostname, len), diff --git a/lib/silccore/silcchannel.c b/lib/silccore/silcchannel.c index ae6ac4be..8fcc4c53 100644 --- a/lib/silccore/silcchannel.c +++ b/lib/silccore/silcchannel.c @@ -46,35 +46,37 @@ SilcChannelPayload silc_channel_payload_parse(const unsigned char *payload, SilcUInt32 payload_len) { SilcBufferStruct buffer; - SilcChannelPayload new; + SilcChannelPayload newp; int ret; SILC_LOG_DEBUG(("Parsing channel payload")); silc_buffer_set(&buffer, (unsigned char *)payload, payload_len); - new = silc_calloc(1, sizeof(*new)); + newp = silc_calloc(1, sizeof(*newp)); + if (!newp) + return NULL; /* Parse the Channel Payload. Ignore the padding. */ ret = silc_buffer_unformat(&buffer, - SILC_STR_UI16_NSTRING_ALLOC(&new->channel_name, - &new->name_len), - SILC_STR_UI16_NSTRING_ALLOC(&new->channel_id, - &new->id_len), - SILC_STR_UI_INT(&new->mode), + SILC_STR_UI16_NSTRING_ALLOC(&newp->channel_name, + &newp->name_len), + SILC_STR_UI16_NSTRING_ALLOC(&newp->channel_id, + &newp->id_len), + SILC_STR_UI_INT(&newp->mode), SILC_STR_END); if (ret == -1) goto err; - if ((new->name_len < 1 || new->name_len > buffer.len) || - (new->id_len < 1 || new->id_len > buffer.len)) { + if ((newp->name_len < 1 || newp->name_len > buffer.len) || + (newp->id_len < 1 || newp->id_len > buffer.len)) { SILC_LOG_ERROR(("Incorrect channel payload in packet, packet dropped")); goto err; } - return new; + return newp; err: - silc_channel_payload_free(new); + silc_channel_payload_free(newp); return NULL; } @@ -85,7 +87,7 @@ SilcDList silc_channel_payload_parse_list(const unsigned char *payload, { SilcBufferStruct buffer; SilcDList list; - SilcChannelPayload new; + SilcChannelPayload newp; int len, ret; SILC_LOG_DEBUG(("Parsing channel payload list")); @@ -94,29 +96,31 @@ SilcDList silc_channel_payload_parse_list(const unsigned char *payload, list = silc_dlist_init(); while (buffer.len) { - new = silc_calloc(1, sizeof(*new)); + newp = silc_calloc(1, sizeof(*newp)); + if (!newp) + goto err; ret = silc_buffer_unformat(&buffer, - SILC_STR_UI16_NSTRING_ALLOC(&new->channel_name, - &new->name_len), - SILC_STR_UI16_NSTRING_ALLOC(&new->channel_id, - &new->id_len), - SILC_STR_UI_INT(&new->mode), + SILC_STR_UI16_NSTRING_ALLOC(&newp->channel_name, + &newp->name_len), + SILC_STR_UI16_NSTRING_ALLOC(&newp->channel_id, + &newp->id_len), + SILC_STR_UI_INT(&newp->mode), SILC_STR_END); if (ret == -1) goto err; - if ((new->name_len < 1 || new->name_len > buffer.len) || - (new->id_len < 1 || new->id_len > buffer.len)) { + if ((newp->name_len < 1 || newp->name_len > buffer.len) || + (newp->id_len < 1 || newp->id_len > buffer.len)) { SILC_LOG_ERROR(("Incorrect channel payload in packet, packet dropped")); goto err; } - len = 2 + new->name_len + 2 + new->id_len + 4; + len = 2 + newp->name_len + 2 + newp->id_len + 4; if (buffer.len < len) break; silc_buffer_pull(&buffer, len); - silc_dlist_add(list, new); + silc_dlist_add(list, newp); } return list; @@ -138,8 +142,10 @@ SilcBuffer silc_channel_payload_encode(const unsigned char *channel_name, SILC_LOG_DEBUG(("Encoding message payload")); - buffer = silc_buffer_alloc(2 + channel_name_len + 2 + channel_id_len + 4); - silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer)); + buffer = silc_buffer_alloc_size(2 + channel_name_len + 2 + + channel_id_len + 4); + if (!buffer) + return NULL; /* Encode the Channel Payload */ silc_buffer_format(buffer, @@ -260,10 +266,13 @@ bool silc_channel_message_payload_decrypt(unsigned char *data, /* Allocate destination decryption buffer since we do not want to modify the original data buffer, since we might want to call this function many times for same payload. */ - if (hmac) + if (hmac) { dst = silc_calloc(data_len - iv_len, sizeof(*dst)); - else + if (!dst) + return FALSE; + } else { dst = data; + } /* Decrypt the channel message */ silc_cipher_decrypt(cipher, data, dst, data_len - iv_len, iv); @@ -304,7 +313,7 @@ silc_channel_message_payload_parse(unsigned char *payload, SilcHmac hmac) { SilcBufferStruct buffer; - SilcChannelMessagePayload new; + SilcChannelMessagePayload newp; int ret; SilcUInt32 iv_len, mac_len; @@ -321,30 +330,32 @@ silc_channel_message_payload_parse(unsigned char *payload, iv_len = silc_cipher_get_block_len(cipher); mac_len = silc_hmac_len(hmac); - new = silc_calloc(1, sizeof(*new)); + newp = silc_calloc(1, sizeof(*newp)); + if (!newp) + return NULL; /* Parse the Channel Message Payload. Ignore the padding. */ ret = silc_buffer_unformat(&buffer, - SILC_STR_UI_SHORT(&new->flags), - SILC_STR_UI16_NSTRING_ALLOC(&new->data, - &new->data_len), + SILC_STR_UI_SHORT(&newp->flags), + SILC_STR_UI16_NSTRING_ALLOC(&newp->data, + &newp->data_len), SILC_STR_UI16_NSTRING(NULL, NULL), - SILC_STR_UI_XNSTRING(&new->mac, mac_len), - SILC_STR_UI_XNSTRING(&new->iv, iv_len), + SILC_STR_UI_XNSTRING(&newp->mac, mac_len), + SILC_STR_UI_XNSTRING(&newp->iv, iv_len), SILC_STR_END); if (ret == -1) goto err; - if (new->data_len > buffer.len) { + if (newp->data_len > buffer.len) { SILC_LOG_ERROR(("Incorrect channel message payload in packet, " "packet dropped")); goto err; } - return new; + return newp; err: - silc_channel_message_payload_free(new); + silc_channel_message_payload_free(newp); return NULL; } @@ -378,6 +389,8 @@ SilcBuffer silc_channel_message_payload_encode(SilcUInt16 flags, /* Allocate channel payload buffer */ len += pad_len + iv_len; buffer = silc_buffer_alloc(len); + if (!buffer) + return NULL; /* Generate padding */ for (i = 0; i < pad_len; i++) pad[i] = silc_rng_global_get_byte(); @@ -482,40 +495,43 @@ silc_channel_key_payload_parse(const unsigned char *payload, SilcUInt32 payload_len) { SilcBufferStruct buffer; - SilcChannelKeyPayload new; + SilcChannelKeyPayload newp; int ret; SILC_LOG_DEBUG(("Parsing channel key payload")); silc_buffer_set(&buffer, (unsigned char *)payload, payload_len); - new = silc_calloc(1, sizeof(*new)); + newp = silc_calloc(1, sizeof(*newp)); + if (!newp) + return NULL; /* Parse the Channel Key Payload */ ret = silc_buffer_unformat(&buffer, - SILC_STR_UI16_NSTRING_ALLOC(&new->id, &new->id_len), - SILC_STR_UI16_NSTRING_ALLOC(&new->cipher, - &new->cipher_len), - SILC_STR_UI16_NSTRING_ALLOC(&new->key, &new->key_len), + SILC_STR_UI16_NSTRING_ALLOC(&newp->id, &newp->id_len), + SILC_STR_UI16_NSTRING_ALLOC(&newp->cipher, + &newp->cipher_len), + SILC_STR_UI16_NSTRING_ALLOC(&newp->key, + &newp->key_len), SILC_STR_END); if (ret == -1) goto err; - if (new->id_len < 1 || new->key_len < 1 || new->cipher_len < 1) { + if (newp->id_len < 1 || newp->key_len < 1 || newp->cipher_len < 1) { SILC_LOG_ERROR(("Incorrect channel key payload in packet")); goto err; } - return new; + return newp; err: - if (new->id) - silc_free(new->id); - if (new->cipher) - silc_free(new->cipher); - if (new->key) - silc_free(new->key); - silc_free(new); + if (newp->id) + silc_free(newp->id); + if (newp->cipher) + silc_free(newp->cipher); + if (newp->key) + silc_free(newp->key); + silc_free(newp); return NULL; } @@ -537,9 +553,9 @@ SilcBuffer silc_channel_key_payload_encode(SilcUInt16 id_len, /* Allocate channel payload buffer. Length is 2 + id + 2 + key + 2 + cipher */ len = 2 + id_len + 2 + key_len + 2 + cipher_len; - buffer = silc_buffer_alloc(len); - - silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer)); + buffer = silc_buffer_alloc_size(len); + if (!buffer) + return NULL; /* Encode the Channel Payload */ silc_buffer_format(buffer, diff --git a/lib/silccore/silccommand.c b/lib/silccore/silccommand.c index 50d96200..aaa190c2 100644 --- a/lib/silccore/silccommand.c +++ b/lib/silccore/silccommand.c @@ -45,7 +45,7 @@ SilcCommandPayload silc_command_payload_parse(const unsigned char *payload, SilcUInt32 payload_len) { SilcBufferStruct buffer; - SilcCommandPayload new; + SilcCommandPayload newp; unsigned char args_num; SilcUInt16 p_len; int ret; @@ -53,42 +53,44 @@ SilcCommandPayload silc_command_payload_parse(const unsigned char *payload, SILC_LOG_DEBUG(("Parsing command payload")); silc_buffer_set(&buffer, (unsigned char *)payload, payload_len); - new = silc_calloc(1, sizeof(*new)); + newp = silc_calloc(1, sizeof(*newp)); + if (!newp) + return NULL; /* Parse the Command Payload */ ret = silc_buffer_unformat(&buffer, SILC_STR_UI_SHORT(&p_len), - SILC_STR_UI_CHAR(&new->cmd), + SILC_STR_UI_CHAR(&newp->cmd), SILC_STR_UI_CHAR(&args_num), - SILC_STR_UI_SHORT(&new->ident), + SILC_STR_UI_SHORT(&newp->ident), SILC_STR_END); if (ret == -1) { - silc_free(new); + silc_free(newp); return NULL; } if (p_len != buffer.len) { SILC_LOG_ERROR(("Incorrect command payload in packet, packet dropped")); - silc_free(new); + silc_free(newp); return NULL; } - if (new->cmd == 0) { - silc_free(new); + if (newp->cmd == 0) { + silc_free(newp); return NULL; } silc_buffer_pull(&buffer, SILC_COMMAND_PAYLOAD_LEN); if (args_num) { - new->args = silc_argument_payload_parse(buffer.data, buffer.len, args_num); - if (!new->args) { - silc_free(new); + newp->args = silc_argument_payload_parse(buffer.data, buffer.len, args_num); + if (!newp->args) { + silc_free(newp); return NULL; } } silc_buffer_push(&buffer, SILC_COMMAND_PAYLOAD_LEN); - return new; + return newp; } /* Encodes Command Payload returning it to SilcBuffer. */ @@ -112,8 +114,9 @@ SilcBuffer silc_command_payload_encode(SilcCommand cmd, } len += SILC_COMMAND_PAYLOAD_LEN; - buffer = silc_buffer_alloc(len); - silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer)); + buffer = silc_buffer_alloc_size(len); + if (!buffer) + return NULL; /* Create Command payload */ silc_buffer_format(buffer, @@ -155,8 +158,12 @@ SilcBuffer silc_command_payload_encode_payload(SilcCommandPayload payload) } len += SILC_COMMAND_PAYLOAD_LEN; - buffer = silc_buffer_alloc(len); - silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer)); + buffer = silc_buffer_alloc_size(len); + if (!buffer) { + if (args) + silc_buffer_free(args); + return NULL; + } /* Create Command payload */ silc_buffer_format(buffer, @@ -212,13 +219,19 @@ SilcBuffer silc_command_payload_encode_vap(SilcCommand cmd, unsigned char *x; SilcUInt32 x_len; SilcUInt32 x_type; - SilcBuffer buffer; + SilcBuffer buffer = NULL; int i, k = 0; if (argc) { argv = silc_calloc(argc, sizeof(unsigned char *)); + if (!argv) + return NULL; argv_lens = silc_calloc(argc, sizeof(SilcUInt32)); + if (!argv_lens) + return NULL; argv_types = silc_calloc(argc, sizeof(SilcUInt32)); + if (!argv_types) + return NULL; for (i = 0, k = 0; i < argc; i++) { x_type = va_arg(ap, SilcUInt32); @@ -229,6 +242,8 @@ SilcBuffer silc_command_payload_encode_vap(SilcCommand cmd, continue; argv[k] = silc_memdup(x, x_len); + if (!argv[k]) + goto out; argv_lens[k] = x_len; argv_types[k] = x_type; k++; @@ -238,6 +253,7 @@ SilcBuffer silc_command_payload_encode_vap(SilcCommand cmd, buffer = silc_command_payload_encode(cmd, k, argv, argv_lens, argv_types, ident); + out: for (i = 0; i < k; i++) silc_free(argv[i]); silc_free(argv); @@ -280,16 +296,33 @@ silc_command_reply_payload_encode_vap(SilcCommand cmd, unsigned char *x; SilcUInt32 x_len; SilcUInt32 x_type; - SilcBuffer buffer; + SilcBuffer buffer = NULL; int i, k; argc++; argv = silc_calloc(argc, sizeof(unsigned char *)); + if (!argv) + return NULL; argv_lens = silc_calloc(argc, sizeof(SilcUInt32)); + if (!argv_lens) { + silc_free(argv); + return NULL; + } argv_types = silc_calloc(argc, sizeof(SilcUInt32)); + if (!argv_types) { + silc_free(argv_lens); + silc_free(argv); + return NULL; + } SILC_PUT16_MSB(status, status_data); argv[0] = silc_memdup(status_data, sizeof(status_data)); + if (!argv[0]) { + silc_free(argv_types); + silc_free(argv_lens); + silc_free(argv); + return NULL; + } argv_lens[0] = sizeof(status_data); argv_types[0] = 1; @@ -302,6 +335,8 @@ silc_command_reply_payload_encode_vap(SilcCommand cmd, continue; argv[k] = silc_memdup(x, x_len); + if (!argv[k]) + goto out; argv_lens[k] = x_len; argv_types[k] = x_type; k++; @@ -310,6 +345,7 @@ silc_command_reply_payload_encode_vap(SilcCommand cmd, buffer = silc_command_payload_encode(cmd, k, argv, argv_lens, argv_types, ident); + out: for (i = 0; i < k; i++) silc_free(argv[i]); silc_free(argv); diff --git a/lib/silccore/silcid.c b/lib/silccore/silcid.c index 655601ef..87040063 100644 --- a/lib/silccore/silcid.c +++ b/lib/silccore/silcid.c @@ -45,38 +45,40 @@ SilcIDPayload silc_id_payload_parse(const unsigned char *payload, SilcUInt32 payload_len) { SilcBufferStruct buffer; - SilcIDPayload new; + SilcIDPayload newp; int ret; SILC_LOG_DEBUG(("Parsing ID payload")); silc_buffer_set(&buffer, (unsigned char *)payload, payload_len); - new = silc_calloc(1, sizeof(*new)); + newp = silc_calloc(1, sizeof(*newp)); + if (!newp) + return NULL; ret = silc_buffer_unformat(&buffer, - SILC_STR_UI_SHORT(&new->type), - SILC_STR_UI_SHORT(&new->len), + SILC_STR_UI_SHORT(&newp->type), + SILC_STR_UI_SHORT(&newp->len), SILC_STR_END); if (ret == -1) goto err; silc_buffer_pull(&buffer, 4); - if (new->len > buffer.len) + if (newp->len > buffer.len) goto err; ret = silc_buffer_unformat(&buffer, - SILC_STR_UI_XNSTRING_ALLOC(&new->id, new->len), + SILC_STR_UI_XNSTRING_ALLOC(&newp->id, newp->len), SILC_STR_END); if (ret == -1) goto err; silc_buffer_push(&buffer, 4); - return new; + return newp; err: - silc_free(new); + silc_free(newp); return NULL; } @@ -143,8 +145,9 @@ SilcBuffer silc_id_payload_encode_data(const unsigned char *id, type == SILC_ID_CLIENT ? "Client" : type == SILC_ID_SERVER ? "Server" : "Channel")); - buffer = silc_buffer_alloc(4 + id_len); - silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer)); + buffer = silc_buffer_alloc_size(4 + id_len); + if (!buffer) + return NULL; silc_buffer_format(buffer, SILC_STR_UI_SHORT(type), SILC_STR_UI_SHORT(id_len), @@ -209,6 +212,8 @@ unsigned char *silc_id_id2str(const void *id, SilcIdType type) case SILC_ID_SERVER: server_id = (SilcServerID *)id; ret_id = silc_calloc(id_len, sizeof(unsigned char)); + if (!ret_id) + return NULL; memcpy(ret_id, server_id->ip.data, server_id->ip.data_len); SILC_PUT16_MSB(server_id->port, &ret_id[4]); SILC_PUT16_MSB(server_id->rnd, &ret_id[6]); @@ -217,6 +222,8 @@ unsigned char *silc_id_id2str(const void *id, SilcIdType type) case SILC_ID_CLIENT: client_id = (SilcClientID *)id; ret_id = silc_calloc(id_len, sizeof(unsigned char)); + if (!ret_id) + return NULL; memcpy(ret_id, client_id->ip.data, client_id->ip.data_len); ret_id[4] = client_id->rnd; memcpy(&ret_id[5], client_id->hash, CLIENTID_HASH_LEN); @@ -225,6 +232,8 @@ unsigned char *silc_id_id2str(const void *id, SilcIdType type) case SILC_ID_CHANNEL: channel_id = (SilcChannelID *)id; ret_id = silc_calloc(id_len, sizeof(unsigned char)); + if (!ret_id) + return NULL; memcpy(ret_id, channel_id->ip.data, channel_id->ip.data_len); SILC_PUT16_MSB(channel_id->port, &ret_id[4]); SILC_PUT16_MSB(channel_id->rnd, &ret_id[6]); @@ -250,6 +259,8 @@ void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, SilcIdType type return NULL; server_id = silc_calloc(1, sizeof(*server_id)); + if (!server_id) + return NULL; memcpy(server_id->ip.data, id, (id_len > ID_SERVER_LEN_PART + 4 ? 16 : 4)); server_id->ip.data_len = (id_len > ID_SERVER_LEN_PART + 4 ? 16 : 4); @@ -267,6 +278,8 @@ void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, SilcIdType type return NULL; client_id = silc_calloc(1, sizeof(*client_id)); + if (!client_id) + return NULL; memcpy(client_id->ip.data, id, (id_len > ID_CLIENT_LEN_PART + 4 ? 16 : 4)); client_id->ip.data_len = (id_len > ID_CLIENT_LEN_PART + 4 ? 16 : 4); @@ -284,6 +297,8 @@ void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, SilcIdType type return NULL; channel_id = silc_calloc(1, sizeof(*channel_id)); + if (!channel_id) + return NULL; memcpy(channel_id->ip.data, id, (id_len > ID_CHANNEL_LEN_PART + 4 ? 16 : 4)); channel_id->ip.data_len = (id_len > ID_CHANNEL_LEN_PART + 4 ? 16 : 4); diff --git a/lib/silccore/silcidcache.c b/lib/silccore/silcidcache.c index a28c6833..65ef1338 100644 --- a/lib/silccore/silcidcache.c +++ b/lib/silccore/silcidcache.c @@ -106,6 +106,8 @@ SilcIDCache silc_idcache_alloc(SilcUInt32 count, SilcIdType id_type, SILC_LOG_DEBUG(("Allocating new cache")); cache = silc_calloc(1, sizeof(*cache)); + if (!cache) + return NULL; cache->id_table = silc_hash_table_alloc(count, silc_hash_id, (void *)(SilcUInt32)id_type, silc_hash_id_compare, @@ -120,6 +122,17 @@ SilcIDCache silc_idcache_alloc(SilcUInt32 count, SilcIdType id_type, cache->destructor = destructor; cache->type = id_type; + if (!cache->id_table || !cache->name_table || !cache->context_table) { + if (cache->id_table) + silc_hash_table_free(cache->id_table); + if (cache->name_table) + silc_hash_table_free(cache->name_table); + if (cache->context_table) + silc_hash_table_free(cache->context_table); + silc_free(cache); + return NULL; + } + return cache; } @@ -150,6 +163,8 @@ bool silc_idcache_add(SilcIDCache cache, char *name, void *id, /* Allocate new cache entry */ c = silc_calloc(1, sizeof(*c)); + if (!c) + return FALSE; c->id = id; c->name = name; c->expire = expire; @@ -380,6 +395,8 @@ bool silc_idcache_get_all(SilcIDCache cache, SilcIDCacheList *ret) return TRUE; list = silc_idcache_list_alloc(); + if (!list) + return FALSE; silc_hash_table_foreach(cache->id_table, silc_idcache_get_all_foreach, list); if (silc_idcache_list_count(list) == 0) { @@ -400,6 +417,8 @@ bool silc_idcache_find_by_id(SilcIDCache cache, void *id, SilcIDCacheList list; list = silc_idcache_list_alloc(); + if (!list) + return FALSE; if (!ret) return TRUE; @@ -458,6 +477,8 @@ bool silc_idcache_find_by_name(SilcIDCache cache, char *name, SilcIDCacheList list; list = silc_idcache_list_alloc(); + if (!list) + return FALSE; if (!ret) return TRUE; @@ -493,6 +514,8 @@ static SilcIDCacheList silc_idcache_list_alloc() SilcIDCacheList list; list = silc_calloc(1, sizeof(*list)); + if (!list) + return FALSE; return list; } @@ -529,6 +552,8 @@ static void silc_idcache_list_add(SilcIDCacheList list, SilcIDCacheEntry cache) i = list->cache_dyn_count; list->cache_dyn = silc_realloc(list->cache_dyn, sizeof(*list->cache_dyn) * (i + 5)); + if (!list->cache_dyn) + return; /* NULL the reallocated area */ for (k = i; k < (i + 5); k++) diff --git a/lib/silccore/silcnotify.c b/lib/silccore/silcnotify.c index 44552d56..549f7e0a 100644 --- a/lib/silccore/silcnotify.c +++ b/lib/silccore/silcnotify.c @@ -40,19 +40,21 @@ SilcNotifyPayload silc_notify_payload_parse(const unsigned char *payload, SilcUInt32 payload_len) { SilcBufferStruct buffer; - SilcNotifyPayload new; + SilcNotifyPayload newp; SilcUInt16 len; int ret; SILC_LOG_DEBUG(("Parsing Notify payload")); silc_buffer_set(&buffer, (unsigned char *)payload, payload_len); - new = silc_calloc(1, sizeof(*new)); + newp = silc_calloc(1, sizeof(*newp)); + if (!newp) + return NULL; ret = silc_buffer_unformat(&buffer, - SILC_STR_UI_SHORT(&new->type), + SILC_STR_UI_SHORT(&newp->type), SILC_STR_UI_SHORT(&len), - SILC_STR_UI_CHAR(&new->argc), + SILC_STR_UI_CHAR(&newp->argc), SILC_STR_END); if (ret == -1) goto err; @@ -60,17 +62,17 @@ SilcNotifyPayload silc_notify_payload_parse(const unsigned char *payload, if (len > buffer.len) goto err; - if (new->argc) { + if (newp->argc) { silc_buffer_pull(&buffer, 5); - new->args = silc_argument_payload_parse(buffer.data, buffer.len, - new->argc); + newp->args = silc_argument_payload_parse(buffer.data, buffer.len, + newp->argc); silc_buffer_push(&buffer, 5); } - return new; + return newp; err: - silc_free(new); + silc_free(newp); return NULL; } @@ -91,8 +93,19 @@ SilcBuffer silc_notify_payload_encode(SilcNotifyType type, SilcUInt32 argc, if (argc) { argv = silc_calloc(argc, sizeof(unsigned char *)); + if (!argv) + return NULL; argv_lens = silc_calloc(argc, sizeof(SilcUInt32)); + if (!argv_lens) { + silc_free(argv); + return NULL; + } argv_types = silc_calloc(argc, sizeof(SilcUInt32)); + if (!argv_types) { + silc_free(argv_lens); + silc_free(argv); + return NULL; + } for (i = 0, k = 0; i < argc; i++) { x = va_arg(ap, unsigned char *); @@ -102,6 +115,8 @@ SilcBuffer silc_notify_payload_encode(SilcNotifyType type, SilcUInt32 argc, continue; argv[k] = silc_memdup(x, x_len); + if (!argv[k]) + return NULL; argv_lens[k] = x_len; argv_types[k] = i + 1; k++; @@ -118,8 +133,9 @@ SilcBuffer silc_notify_payload_encode(SilcNotifyType type, SilcUInt32 argc, } len += 5; - buffer = silc_buffer_alloc(len); - silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer)); + buffer = silc_buffer_alloc_size(len); + if (!buffer) + return NULL; silc_buffer_format(buffer, SILC_STR_UI_SHORT(type), SILC_STR_UI_SHORT(len), @@ -148,8 +164,9 @@ SilcBuffer silc_notify_payload_encode_args(SilcNotifyType type, int len; len = 5 + (args ? args->len : 0); - buffer = silc_buffer_alloc(len); - silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer)); + buffer = silc_buffer_alloc_size(len); + if (!buffer) + return NULL; silc_buffer_format(buffer, SILC_STR_UI_SHORT(type), SILC_STR_UI_SHORT(len), diff --git a/lib/silccore/silcpacket.c b/lib/silccore/silcpacket.c index 7f72922b..c8e17e60 100644 --- a/lib/silccore/silcpacket.c +++ b/lib/silccore/silcpacket.c @@ -377,6 +377,8 @@ bool silc_packet_receive_process(SilcSocketConnection sock, SILC_UNSET_INBUF_PENDING(sock); parse_ctx = silc_calloc(1, sizeof(*parse_ctx)); + if (!parse_ctx) + return FALSE; parse_ctx->packet = silc_packet_context_alloc(); parse_ctx->packet->buffer = silc_buffer_alloc(paddedlen + mac_len); parse_ctx->packet->type = sock->inbuf->data[3]; @@ -746,6 +748,8 @@ SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx, SilcPacketContext *silc_packet_context_alloc(void) { SilcPacketContext *ctx = silc_calloc(1, sizeof(*ctx)); + if (!ctx) + return NULL; ctx->users++; return ctx; } diff --git a/lib/silccore/silcprivate.c b/lib/silccore/silcprivate.c index 26a06429..fa9bfa9e 100644 --- a/lib/silccore/silcprivate.c +++ b/lib/silccore/silcprivate.c @@ -48,7 +48,7 @@ silc_private_message_payload_parse(unsigned char *payload, SilcCipher cipher) { SilcBufferStruct buffer; - SilcPrivateMessagePayload new; + SilcPrivateMessagePayload newp; int ret; SILC_LOG_DEBUG(("Parsing private message payload")); @@ -60,29 +60,31 @@ silc_private_message_payload_parse(unsigned char *payload, silc_cipher_decrypt(cipher, buffer.data, buffer.data, buffer.len, cipher->iv); - new = silc_calloc(1, sizeof(*new)); + newp = silc_calloc(1, sizeof(*newp)); + if (!newp) + return NULL; /* Parse the Private Message Payload. Ignore the padding. */ ret = silc_buffer_unformat(&buffer, - SILC_STR_UI_SHORT(&new->flags), - SILC_STR_UI16_NSTRING_ALLOC(&new->message, - &new->message_len), + SILC_STR_UI_SHORT(&newp->flags), + SILC_STR_UI16_NSTRING_ALLOC(&newp->message, + &newp->message_len), SILC_STR_END); if (ret == -1) { SILC_LOG_DEBUG(("Incorrect private message payload")); goto err; } - if ((new->message_len < 1 || new->message_len > buffer.len)) { + if ((newp->message_len < 1 || newp->message_len > buffer.len)) { SILC_LOG_DEBUG(("Incorrect private message payload in packet, " "packet dropped")); goto err; } - return new; + return newp; err: - silc_private_message_payload_free(new); + silc_private_message_payload_free(newp); return NULL; } @@ -114,10 +116,11 @@ SilcBuffer silc_private_message_payload_encode(SilcUInt16 flags, } /* Allocate private message payload buffer */ - buffer = silc_buffer_alloc(len); + buffer = silc_buffer_alloc_size(len); + if (!buffer) + return NULL; /* Encode the Channel Message Payload */ - silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer)); silc_buffer_format(buffer, SILC_STR_UI_SHORT(flags), SILC_STR_UI_SHORT(data_len), diff --git a/lib/silcutil/silcbuffer.h b/lib/silcutil/silcbuffer.h index 8d39ec81..b495296e 100644 --- a/lib/silcutil/silcbuffer.h +++ b/lib/silcutil/silcbuffer.h @@ -134,9 +134,13 @@ SilcBuffer silc_buffer_alloc(SilcUInt32 len) /* Allocate new SilcBuffer */ sb = (SilcBuffer)silc_calloc(1, sizeof(*sb)); + if (!sb) + return NULL; /* Allocate the actual data area */ sb->head = (unsigned char *)silc_calloc(len, sizeof(*sb->head)); + if (!sb->head) + return NULL; /* Set pointers to the new buffer */ sb->truelen = len; @@ -366,6 +370,20 @@ unsigned char *silc_buffer_put_tail(SilcBuffer sb, return (unsigned char *)memcpy(sb->tail, data, len); } +/* Allocates `len' bytes size buffer and moves the tail area automaticlly + `len' bytes so that the buffer is ready to use without calling the + silc_buffer_pull_tail. */ + +static inline +SilcBuffer silc_buffer_alloc_size(SilcUInt32 len) +{ + SilcBuffer sb = silc_buffer_alloc(len); + if (!sb) + return NULL; + silc_buffer_pull_tail(sb, len); + return sb; +} + /* Clears and initialiazes the buffer to the state as if it was just allocated by silc_buffer_alloc. */ @@ -387,8 +405,9 @@ SilcBuffer silc_buffer_copy(SilcBuffer sb) { SilcBuffer sb_new; - sb_new = silc_buffer_alloc(sb->len); - silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new)); + sb_new = silc_buffer_alloc_size(sb->len); + if (!sb_new) + return NULL; silc_buffer_put(sb_new, sb->data, sb->len); return sb_new; @@ -403,8 +422,9 @@ SilcBuffer silc_buffer_clone(SilcBuffer sb) { SilcBuffer sb_new; - sb_new = silc_buffer_alloc(sb->truelen); - silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new)); + sb_new = silc_buffer_alloc_size(sb->truelen); + if (!sb_new) + return NULL; silc_buffer_put(sb_new, sb->head, sb->truelen); sb_new->data = sb_new->head + (sb->data - sb->head); sb_new->tail = sb_new->data + sb->len; @@ -428,8 +448,9 @@ SilcBuffer silc_buffer_realloc(SilcBuffer sb, SilcUInt32 newsize) if (newsize <= sb->truelen) return sb; - sb_new = silc_buffer_alloc(newsize); - silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new)); + sb_new = silc_buffer_alloc_size(newsize); + if (!sb_new) + return NULL; silc_buffer_put(sb_new, sb->head, sb->truelen); sb_new->data = sb_new->head + (sb->data - sb->head); sb_new->tail = sb_new->data + sb->len; -- 2.24.0