From 44b125f77e59be00a263f5f6e40911ff6367e970 Mon Sep 17 00:00:00 2001 From: Soner Tari Date: Mon, 25 Mar 2019 03:39:15 +0300 Subject: [PATCH] Avoid malloc/free for vars of known sizes --- privsep.c | 23 +++-------------------- pxyconn.c | 27 ++++++++++++--------------- 2 files changed, 15 insertions(+), 35 deletions(-) diff --git a/privsep.c b/privsep.c index d657932..6ebd70e 100644 --- a/privsep.c +++ b/privsep.c @@ -583,7 +583,7 @@ privsep_server_handle_req(opts_t *opts, int srvsock) break; } case PRIVSEP_REQ_UPDATE_ATIME: { - userdbkeys_t *arg; + userdbkeys_t arg; if (n != sizeof(char) + sizeof(userdbkeys_t)) { ans[0] = PRIVSEP_ANS_INVALID; @@ -594,24 +594,8 @@ privsep_server_handle_req(opts_t *opts, int srvsock) } return 0; } - - // @attention Do not typecast, but malloc and memcpy - //arg = *(userdbkeys_t**)(&req[1]); - if (!(arg = malloc(n))) { - ans[0] = PRIVSEP_ANS_SYS_ERR; - *((int*)&ans[1]) = errno; - if (sys_sendmsgfd(srvsock, ans, 1 + sizeof(int), - -1) == -1) { - log_err_level_printf(LOG_CRIT, "Sending message failed: %s (%i" - ")\n", strerror(errno), errno); - return -1; - } - return 0; - } - memcpy(arg, req + 1, n - 1); - - if (privsep_server_update_atime(opts, arg) == -1) { - free(arg); + arg = *(userdbkeys_t*)(&req[1]); + if (privsep_server_update_atime(opts, &arg) == -1) { ans[0] = PRIVSEP_ANS_SYS_ERR; *((int*)&ans[1]) = errno; if (sys_sendmsgfd(srvsock, ans, 1 + sizeof(int), @@ -622,7 +606,6 @@ privsep_server_handle_req(opts_t *opts, int srvsock) } return 0; } else { - free(arg); ans[0] = PRIVSEP_ANS_SUCCESS; // @attention Pass -1 as arg 4, otherwise passing 0 opens an stdin (fd 0), causing fd leak if (sys_sendmsgfd(srvsock, ans, 1, -1) == -1) { diff --git a/pxyconn.c b/pxyconn.c index 2649232..c3978d4 100644 --- a/pxyconn.c +++ b/pxyconn.c @@ -391,25 +391,22 @@ pxy_conn_ctx_free(pxy_conn_ctx_t *ctx, int by_requestor) // Update userdb atime if idle time is more than 50% of user timeout, which is expected to reduce update frequency unsigned int idletime = ctx->idletime + (time(NULL) - ctx->ctime); if (idletime > (ctx->opts->user_timeout / 2)) { - struct userdbkeys *keys = malloc(sizeof(userdbkeys_t)); - if (keys) { - // Zero out for NULL termination - memset(keys, 0, sizeof(userdbkeys_t)); - // Leave room for NULL to make sure the strings are always NULL terminated - strncpy(keys->ip, ctx->srchost_str, sizeof(keys->ip) - 1); - strncpy(keys->user, ctx->user, sizeof(keys->user) - 1); - strncpy(keys->ether, ctx->ether, sizeof(keys->ether) - 1); - - if (privsep_client_update_atime(ctx->clisock, keys) == -1) { + userdbkeys_t keys; + // Zero out for NULL termination + memset(&keys, 0, sizeof(userdbkeys_t)); + // Leave room for NULL to make sure the strings are always NULL terminated + strncpy(keys.ip, ctx->srchost_str, sizeof(keys.ip) - 1); + strncpy(keys.user, ctx->user, sizeof(keys.user) - 1); + strncpy(keys.ether, ctx->ether, sizeof(keys.ether) - 1); + + if (privsep_client_update_atime(ctx->clisock, &keys) == -1) { #ifdef DEBUG_PROXY - log_dbg_level_printf(LOG_DBG_MODE_FINEST, "pxy_conn_ctx_free: Error updating user atime: %s, ctx->fd=%d\n", sqlite3_errmsg(ctx->opts->userdb), ctx->fd); + log_dbg_level_printf(LOG_DBG_MODE_FINEST, "pxy_conn_ctx_free: Error updating user atime: %s, ctx->fd=%d\n", sqlite3_errmsg(ctx->opts->userdb), ctx->fd); #endif /* DEBUG_PROXY */ - } else { + } else { #ifdef DEBUG_PROXY - log_dbg_level_printf(LOG_DBG_MODE_FINEST, "pxy_conn_ctx_free: Successfully updated user atime, ctx->fd=%d\n", ctx->fd); + log_dbg_level_printf(LOG_DBG_MODE_FINEST, "pxy_conn_ctx_free: Successfully updated user atime, ctx->fd=%d\n", ctx->fd); #endif /* DEBUG_PROXY */ - } - free(keys); } } else { #ifdef DEBUG_PROXY