From b7c4630b0177b69611d3e1dfec4dae7162342ff1 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Thu, 4 Apr 2019 10:24:53 -0700 Subject: [PATCH 1/3] Memory Update 1. Fixed a couple of dynamic memory type identifiers with typos. 2. Fixed a reference to the heap in a free. These were missed by the compiler becuase in generic builds, the heap and dynamic type are ignored in a macro. --- src/ssh.c | 2 +- src/wolfsftp.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ssh.c b/src/ssh.c index e045d01..834e3e2 100644 --- a/src/ssh.c +++ b/src/ssh.c @@ -1092,7 +1092,7 @@ int wolfSSH_SetUsername(WOLFSSH* ssh, const char* username) if (ret == WS_SUCCESS) { WSTRNCPY(value, username, valueSz + 1); if (ssh->userName != NULL) { - WFREE(ssh->userName, heap, DYNTYPE_STRING); + WFREE(ssh->userName, ssh->ctx->heap, DYNTYPE_STRING); ssh->userName = NULL; } ssh->userName = value; diff --git a/src/wolfsftp.c b/src/wolfsftp.c index 6d4fe3d..9343fa1 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -1368,7 +1368,7 @@ int wolfSSH_SFTP_RecvRMDIR(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz) if (wolfSSH_SFTP_CreateStatus(ssh, type, reqId, res, "English", out, &outSz) != WS_SUCCESS) { - WFREE(out, ssh->ctx->heap, DYNTPE_BUFFER); + WFREE(out, ssh->ctx->heap, DYNTYPE_BUFFER); return WS_FATAL_ERROR; } wolfSSH_SFTP_RecvSetSend(ssh, out, outSz); @@ -5392,7 +5392,7 @@ int wolfSSH_SFTP_SetSTAT(WOLFSSH* ssh, char* dir, WS_SFTP_FILEATRB* atr) ret = WS_FATAL_ERROR; break; } - XFREE(state->data, ssh->ctx->heap, DYNTYPE_BUFER); + XFREE(state->data, ssh->ctx->heap, DYNTYPE_BUFFER); state->data = (byte*)WMALLOC(maxSz, ssh->ctx->heap, DYNTYPE_BUFFER); if (state->data == NULL) { ret = WS_MEMORY_E; From 883b29d031d65696a7724bca481fa09de384df75 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Thu, 4 Apr 2019 11:18:23 -0700 Subject: [PATCH 2/3] Memory Update 1. Removed the memory API. 2. Wired the existing memory calls back into the wolfCrypt memory calls. 3. Updated the include.am and vcxproj files for the deleted source. The wolfSSH memory API is a shallow copy of the wolfCrypt memory API. wolfCrypt's API offers more options including logging and static memory that may be useful for wolfSSH in the future. Since both APIs were available, wolfSSH's was removed as redundant. --- ide/winvs/wolfssh/wolfssh.vcxproj | 1 - src/include.am | 1 - src/memory.c | 109 ------------------------------ wolfssh/include.am | 1 - wolfssh/memory.h | 56 --------------- wolfssh/port.h | 9 ++- 6 files changed, 4 insertions(+), 173 deletions(-) delete mode 100644 src/memory.c delete mode 100644 wolfssh/memory.h diff --git a/ide/winvs/wolfssh/wolfssh.vcxproj b/ide/winvs/wolfssh/wolfssh.vcxproj index 8c8703e..ab5ea5c 100644 --- a/ide/winvs/wolfssh/wolfssh.vcxproj +++ b/ide/winvs/wolfssh/wolfssh.vcxproj @@ -39,7 +39,6 @@ - diff --git a/src/include.am b/src/include.am index 1ecfd96..e080d4a 100644 --- a/src/include.am +++ b/src/include.am @@ -5,7 +5,6 @@ lib_LTLIBRARIES += src/libwolfssh.la src_libwolfssh_la_SOURCES = src/ssh.c \ src/internal.c \ - src/memory.c \ src/log.c \ src/io.c \ src/port.c diff --git a/src/memory.c b/src/memory.c deleted file mode 100644 index 915c4b3..0000000 --- a/src/memory.c +++ /dev/null @@ -1,109 +0,0 @@ -/* memory.c - * - * Copyright (C) 2014-2016 wolfSSL Inc. - * - * This file is part of wolfSSH. - * - * wolfSSH is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * wolfSSH is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with wolfSSH. If not, see . - */ - - -/* - * The memory module contains the interface to the custom memory handling hooks. - */ - - -#ifdef HAVE_CONFIG_H - #include -#endif - -#include - - -#ifdef USE_WOLFSSH_MEMORY - -#include -#include - -/* Set these to default values initially. */ -static wolfSSH_Malloc_cb malloc_function = NULL; -static wolfSSH_Free_cb free_function = NULL; -static wolfSSH_Realloc_cb realloc_function = NULL; - - -/* set wolfSSH allocator callbacks, 0 on success */ -int wolfSSH_SetAllocators(wolfSSH_Malloc_cb mf, - wolfSSH_Free_cb ff, - wolfSSH_Realloc_cb rf) -{ - int res = 0; - - if (mf) - malloc_function = mf; - else - res = WS_BAD_ARGUMENT; - - if (ff) - free_function = ff; - else - res = WS_BAD_ARGUMENT; - - if (rf) - realloc_function = rf; - else - res = WS_BAD_ARGUMENT; - - return res; -} - - -/* our malloc handler */ -void* wolfSSH_Malloc(size_t size) -{ - void* res = 0; - - if (malloc_function) - res = malloc_function(size); - else - res = malloc(size); - - return res; -} - - -/* our free handler */ -void wolfSSH_Free(void *ptr) -{ - if (free_function) - free_function(ptr); - else - free(ptr); -} - - -/* our realloc handler */ -void* wolfSSH_Realloc(void *ptr, size_t size) -{ - void* res = 0; - - if (realloc_function) - res = realloc_function(ptr, size); - else - res = realloc(ptr, size); - - return res; -} - -#endif /* USE_WOLFSSH_MEMORY */ - diff --git a/wolfssh/include.am b/wolfssh/include.am index 4d6b8d9..644f9b4 100644 --- a/wolfssh/include.am +++ b/wolfssh/include.am @@ -8,7 +8,6 @@ nobase_include_HEADERS+= \ wolfssh/ssh.h \ wolfssh/keygen.h \ wolfssh/port.h \ - wolfssh/memory.h \ wolfssh/settings.h \ wolfssh/error.h \ wolfssh/visibility.h \ diff --git a/wolfssh/memory.h b/wolfssh/memory.h deleted file mode 100644 index ca3a270..0000000 --- a/wolfssh/memory.h +++ /dev/null @@ -1,56 +0,0 @@ -/* memory.h - * - * Copyright (C) 2014-2016 wolfSSL Inc. - * - * This file is part of wolfSSH. - * - * wolfSSH is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * wolfSSH is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with wolfSSH. If not, see . - */ - - -/* - * The memory module contains the interface to the custom memory handling hooks. - */ - - -#pragma once - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - - -typedef void *(*wolfSSH_Malloc_cb)(size_t size); -typedef void (*wolfSSH_Free_cb)(void *ptr); -typedef void *(*wolfSSH_Realloc_cb)(void *ptr, size_t size); - - -/* Public set function */ -WOLFSSH_API int wolfSSH_SetAllocators(wolfSSH_Malloc_cb malloc_function, - wolfSSH_Free_cb free_function, - wolfSSH_Realloc_cb realloc_function); - -/* Public in case user app wants to use WMALLOC/WFREE */ -WOLFSSH_API void* wolfSSH_Malloc(size_t size); -WOLFSSH_API void wolfSSH_Free(void *ptr); -WOLFSSH_API void* wolfSSH_Realloc(void *ptr, size_t size); - - -#ifdef __cplusplus -} -#endif - diff --git a/wolfssh/port.h b/wolfssh/port.h index c994790..b84118d 100644 --- a/wolfssh/port.h +++ b/wolfssh/port.h @@ -41,11 +41,10 @@ extern "C" { /* setup memory handling */ #ifndef WMALLOC_USER - #include - - #define WMALLOC(s, h, t) ((void)h, (void)t, wolfSSH_Malloc((s))) - #define WFREE(p, h, t) {void* xp = (p); if ((xp)) wolfSSH_Free((xp));} - #define WREALLOC(p, n, h, t) wolfSSH_Realloc((p), (n)) + #define WMALLOC(s, h, t) XMALLOC(s, h, t) + #define WFREE(p, h, t) XFREE(p, h, t) + #define WREALLOC(p, n, h, t) XREALLOC(p, n, h, t) + #define wolfSSH_SetAllocators wolfSSL_SetAllocators #endif /* WMALLOC_USER */ #ifndef WFGETS From 1d051e1a84e43a7f148f60f503595d218197b456 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Fri, 5 Apr 2019 11:36:25 -0700 Subject: [PATCH 3/3] Memory Update 1. Renumbered the dynamic memory type IDs so they don't conflict with wolfCrypt/wolfSSL. 2. Renumbered the error codes so they don't conflict with wolfCrypt. 3. Fixed a couple of typos when using dynamic memory type IDs that were missed due to default memory handling being used. --- src/internal.c | 2 +- src/wolfscp.c | 2 +- tests/unit.c | 2 +- wolfssh/error.h | 138 ++++++++++++++++++++++----------------------- wolfssh/internal.h | 2 +- wolfssh/port.h | 2 +- 6 files changed, 74 insertions(+), 74 deletions(-) diff --git a/src/internal.c b/src/internal.c index 0e8a0a3..753b7c6 100644 --- a/src/internal.c +++ b/src/internal.c @@ -544,7 +544,7 @@ void SshResourceFree(WOLFSSH* ssh, void* heap) } if (ssh->scpFileBuffer) { ForceZero(ssh->scpFileBuffer, ssh->scpFileBufferSz); - WFREE(ssh->scpFileBuffer, ssh->ctx->heap, DYNTYPE_BUFFFER); + WFREE(ssh->scpFileBuffer, ssh->ctx->heap, DYNTYPE_BUFFER); ssh->scpFileBuffer = NULL; ssh->scpFileBufferSz = 0; } diff --git a/src/wolfscp.c b/src/wolfscp.c index 27fbf79..a7049ca 100644 --- a/src/wolfscp.c +++ b/src/wolfscp.c @@ -171,7 +171,7 @@ int DoScpSink(WOLFSSH* ssh) ssh->scpFileOffset += ssh->scpFileBufferSz; /* shrink and reset recv buffer */ - WFREE(ssh->scpFileBuffer, ssh->ctx->heap, DYNTYPE_BUFFFER); + WFREE(ssh->scpFileBuffer, ssh->ctx->heap, DYNTYPE_BUFFER); ssh->scpFileBuffer = NULL; ssh->scpFileBufferSz = 0; diff --git a/tests/unit.c b/tests/unit.c index 9e6a37f..ac5b40d 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -407,7 +407,7 @@ static int test_Errors(void) /* Check that all errors have a string and it's the same through the two * APIs. Check that the values that are not errors map to the unknown * string. */ - for (i = WS_SUCCESS; i >= WS_LAST_E; i--) { + for (i = WS_FATAL_ERROR; i >= WS_LAST_E; i--) { errStr = wolfSSH_ErrorToName(i); if (j < missingSz && i == missing[j]) { diff --git a/wolfssh/error.h b/wolfssh/error.h index 6e4692e..85ed4de 100644 --- a/wolfssh/error.h +++ b/wolfssh/error.h @@ -37,76 +37,76 @@ extern "C" { /* main public return values */ enum WS_ErrorCodes { - WS_SUCCESS = 0, /* function success */ - WS_FATAL_ERROR = -1, /* general function failure */ - WS_BAD_ARGUMENT = -2, /* bad function argument */ - WS_MEMORY_E = -3, /* memory allocation failure */ - WS_BUFFER_E = -4, /* input/output buffer size error */ - WS_PARSE_E = -5, /* general parsing error */ - WS_NOT_COMPILED = -6, /* feature not compiled in */ - WS_OVERFLOW_E = -7, /* would overflow if continued */ - WS_BAD_USAGE = -8, /* bad example usage */ - WS_SOCKET_ERROR_E = -9, - WS_WANT_READ = -10, - WS_WANT_WRITE = -11, - WS_RECV_OVERFLOW_E = -12, - WS_VERSION_E = -13, /* Peer using wrong version of SSH */ - WS_SEND_OOB_READ_E = -14, - WS_INPUT_CASE_E = -15, - WS_BAD_FILETYPE_E = -16, - WS_UNIMPLEMENTED_E = -17, - WS_RSA_E = -18, - WS_BAD_FILE_E = -19, - WS_INVALID_ALGO_ID = -20, - WS_DECRYPT_E = -21, - WS_ENCRYPT_E = -22, - WS_VERIFY_MAC_E = -23, - WS_CREATE_MAC_E = -24, - WS_RESOURCE_E = -25, /* insufficient resources for new channel */ - WS_INVALID_CHANTYPE = -26, /* invalid channel type */ - WS_INVALID_CHANID = -27, - WS_INVALID_USERNAME = -28, - WS_CRYPTO_FAILED = -29, /* crypto action failed */ - WS_INVALID_STATE_E = -30, - WS_EOF = -31, - WS_INVALID_PRIME_CURVE = -32, - WS_ECC_E = -33, - WS_CHANOPEN_FAILED = -34, - WS_REKEYING = -35, /* Status: rekey in progress */ - WS_CHANNEL_CLOSED = -36, /* Status: channel closed */ - WS_INVALID_PATH_E = -37, - WS_SCP_CMD_E = -38, - WS_SCP_BAD_MSG_E = -39, - WS_SCP_PATH_LEN_E = -40, - WS_SCP_TIMESTAMP_E = -41, - WS_SCP_DIR_STACK_EMPTY_E = -42, - WS_SCP_CONTINUE = -43, - WS_SCP_ABORT = -44, - WS_SCP_ENTER_DIR = -45, - WS_SCP_EXIT_DIR = -46, - WS_SCP_EXIT_DIR_FINAL = -47, - WS_SCP_COMPLETE = -48, /* SCP transfer complete */ - WS_SCP_INIT = -49, /* SCP transfer verified */ - WS_MATCH_KEX_ALGO_E = -50, /* cannot match KEX algo with peer */ - WS_MATCH_KEY_ALGO_E = -51, /* cannot match key algo with peer */ - WS_MATCH_ENC_ALGO_E = -52, /* cannot match encrypt algo with peer */ - WS_MATCH_MAC_ALGO_E = -53, /* cannot match MAC algo with peer */ - WS_PERMISSIONS = -54, - WS_SFTP_COMPLETE = -55, /* SFTP connection established */ - WS_NEXT_ERROR = -56, /* Getting next value/state results in error */ - WS_CHAN_RXD = -57, /* Status that channel data received. */ - WS_INVALID_EXTDATA = -58, /* invalid Channel Extended Data Type */ - WS_CHAN_PENDING = -59, /* peer hasn't confirmed channel open */ - WS_SFTP_BAD_REQ_ID = -60, /* SFTP Bad request ID */ - WS_SFTP_BAD_REQ_TYPE = -61, /* SFTP Bad request ID */ - WS_SFTP_STATUS_NOT_OK = -62, /* SFTP Status not OK */ - WS_SFTP_FILE_DNE = -63, /* SFTP File Does Not Exist */ - WS_SIZE_ONLY = -64, /* Only getting the size of buffer needed */ - WS_CLOSE_FILE_E = -65, /* Unable to close local file */ - WS_PUBKEY_REJECTED_E = -66, /* Server public key rejected */ - WS_EXTDATA = -67, /* Extended Data available to be read */ + WS_SUCCESS = 0, /* function success */ + WS_FATAL_ERROR = -1001, /* general function failure */ + WS_BAD_ARGUMENT = -1002, /* bad function argument */ + WS_MEMORY_E = -1003, /* memory allocation failure */ + WS_BUFFER_E = -1004, /* input/output buffer size error */ + WS_PARSE_E = -1005, /* general parsing error */ + WS_NOT_COMPILED = -1006, /* feature not compiled in */ + WS_OVERFLOW_E = -1007, /* would overflow if continued */ + WS_BAD_USAGE = -1008, /* bad example usage */ + WS_SOCKET_ERROR_E = -1009, + WS_WANT_READ = -1010, + WS_WANT_WRITE = -1011, + WS_RECV_OVERFLOW_E = -1012, + WS_VERSION_E = -1013, /* Peer using wrong version of SSH */ + WS_SEND_OOB_READ_E = -1014, + WS_INPUT_CASE_E = -1015, + WS_BAD_FILETYPE_E = -1016, + WS_UNIMPLEMENTED_E = -1017, + WS_RSA_E = -1018, + WS_BAD_FILE_E = -1019, + WS_INVALID_ALGO_ID = -1020, + WS_DECRYPT_E = -1021, + WS_ENCRYPT_E = -1022, + WS_VERIFY_MAC_E = -1023, + WS_CREATE_MAC_E = -1024, + WS_RESOURCE_E = -1025, /* not enough resources for new channel */ + WS_INVALID_CHANTYPE = -1026, /* invalid channel type */ + WS_INVALID_CHANID = -1027, + WS_INVALID_USERNAME = -1028, + WS_CRYPTO_FAILED = -1029, /* crypto action failed */ + WS_INVALID_STATE_E = -1030, + WS_EOF = -1031, + WS_INVALID_PRIME_CURVE = -1032, + WS_ECC_E = -1033, + WS_CHANOPEN_FAILED = -1034, + WS_REKEYING = -1035, /* Status: rekey in progress */ + WS_CHANNEL_CLOSED = -1036, /* Status: channel closed */ + WS_INVALID_PATH_E = -1037, + WS_SCP_CMD_E = -1038, + WS_SCP_BAD_MSG_E = -1039, + WS_SCP_PATH_LEN_E = -1040, + WS_SCP_TIMESTAMP_E = -1041, + WS_SCP_DIR_STACK_EMPTY_E = -1042, + WS_SCP_CONTINUE = -1043, + WS_SCP_ABORT = -1044, + WS_SCP_ENTER_DIR = -1045, + WS_SCP_EXIT_DIR = -1046, + WS_SCP_EXIT_DIR_FINAL = -1047, + WS_SCP_COMPLETE = -1048, /* SCP transfer complete */ + WS_SCP_INIT = -1049, /* SCP transfer verified */ + WS_MATCH_KEX_ALGO_E = -1050, /* cannot match KEX algo with peer */ + WS_MATCH_KEY_ALGO_E = -1051, /* cannot match key algo with peer */ + WS_MATCH_ENC_ALGO_E = -1052, /* cannot match encrypt algo with peer */ + WS_MATCH_MAC_ALGO_E = -1053, /* cannot match MAC algo with peer */ + WS_PERMISSIONS = -1054, + WS_SFTP_COMPLETE = -1055, /* SFTP connection established */ + WS_NEXT_ERROR = -1056, /* Getting next value/state is error */ + WS_CHAN_RXD = -1057, /* Status that channel data received. */ + WS_INVALID_EXTDATA = -1058, /* invalid Channel Extended Data Type */ + WS_CHAN_PENDING = -1059, /* peer hasn't confirmed channel open */ + WS_SFTP_BAD_REQ_ID = -1060, /* SFTP Bad request ID */ + WS_SFTP_BAD_REQ_TYPE = -1061, /* SFTP Bad request ID */ + WS_SFTP_STATUS_NOT_OK = -1062, /* SFTP Status not OK */ + WS_SFTP_FILE_DNE = -1063, /* SFTP File Does Not Exist */ + WS_SIZE_ONLY = -1064, /* Only getting size of buffer needed */ + WS_CLOSE_FILE_E = -1065, /* Unable to close local file */ + WS_PUBKEY_REJECTED_E = -1066, /* Server public key rejected */ + WS_EXTDATA = -1067, /* Extended Data available to be read */ - WS_LAST_E = -67 /* Update this to indicate last error */ + WS_LAST_E = -1067 /* Update this to indicate last error */ }; diff --git a/wolfssh/internal.h b/wolfssh/internal.h index 1aa2016..8a479c4 100644 --- a/wolfssh/internal.h +++ b/wolfssh/internal.h @@ -658,6 +658,7 @@ enum WS_MessageIds { /* dynamic memory types */ enum WS_DynamicTypes { + DYNTYPE_STRING = 500, DYNTYPE_CTX, DYNTYPE_SSH, DYNTYPE_CHANNEL, @@ -670,7 +671,6 @@ enum WS_DynamicTypes { DYNTYPE_PUBKEY, DYNTYPE_DH, DYNTYPE_RNG, - DYNTYPE_STRING, DYNTYPE_MPINT, DYNTYPE_SCPCTX, DYNTYPE_SCPDIR, diff --git a/wolfssh/port.h b/wolfssh/port.h index b84118d..bd3f4bb 100644 --- a/wolfssh/port.h +++ b/wolfssh/port.h @@ -35,7 +35,7 @@ extern "C" { #endif -#define PORT_DYNTYPE_STRING 12 +#define PORT_DYNTYPE_STRING 500 /* This value needs to stay in sync with the actual value of DYNTYPE_STRING * from internal.h. */