From 8bcd65193c51be8ac94b9b7691253aeecb446265 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Wed, 1 Apr 2009 19:54:41 +0000 Subject: [PATCH] Added memory error checks for option functions. git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@322 7dcaeef0-15fb-0310-b436-a5af3365683c --- libssh/options.c | 27 ++++++++++++++++++++++++--- libssh/server.c | 12 +++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/libssh/options.c b/libssh/options.c index afef761e..81e4bd8a 100644 --- a/libssh/options.c +++ b/libssh/options.c @@ -50,7 +50,13 @@ */ SSH_OPTIONS *ssh_options_new(void) { - SSH_OPTIONS *option=malloc(sizeof(SSH_OPTIONS)); + SSH_OPTIONS *option; + + option = malloc(sizeof(SSH_OPTIONS)); + if (options == NULL) { + return NULL; + } + memset(option,0,sizeof(SSH_OPTIONS)); option->port=22; /* set the default port */ option->fd=-1; @@ -82,8 +88,14 @@ void ssh_options_set_port(SSH_OPTIONS *opt, unsigned int port){ * \see ssh_session_connect() */ SSH_OPTIONS *ssh_options_copy(SSH_OPTIONS *opt){ - SSH_OPTIONS *ret=ssh_options_new(); + SSH_OPTIONS *ret; int i; + + ret = ssh_options_new(); + if (ret == NULL) { + return NULL; + } + ret->fd=opt->fd; ret->port=opt->port; if(opt->username) @@ -331,6 +343,9 @@ int ssh_options_default_username(SSH_OPTIONS *opt){ DWORD Size = 0; GetUserName(NULL, &Size); //Get Size user = malloc(Size); + if (user == NULL) { + return -1; + } if (GetUserName(user, &Size)){ opt->username=user; return 0; @@ -460,7 +475,7 @@ int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv){ char *cipher=NULL; char *localaddr=NULL; char *identity=NULL; - char **save=malloc(argc * sizeof(char *)); + char **save = NULL; int current=0; #ifdef HAVE_SSH1 int ssh1=1; @@ -471,6 +486,12 @@ int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv){ int saveoptind=optind; /* need to save 'em */ int saveopterr=opterr; + + save = malloc(argc * sizeof(char *)); + if (save == NULL) { + return -1; + } + opterr=0; /* shut up getopt */ while(cont && ((i=getopt(argc,argv,"c:i:Cl:p:vb:rd12"))!=-1)){ diff --git a/libssh/server.c b/libssh/server.c index bb80ead8..4cdf84b1 100644 --- a/libssh/server.c +++ b/libssh/server.c @@ -170,10 +170,20 @@ SSH_SESSION *ssh_bind_accept(SSH_BIND *ssh_bind){ session=ssh_new(); session->server=1; session->version=2; + session->options = ssh_options_copy(ssh_bind->options); + if (session->options == NULL) { + ssh_set_error(ssh_bind, SSH_FATAL, "No space left"); + if (dsa) + private_key_free(dsa); + if (rsa) + private_key_free(rsa); + ssh_cleanup(session); + return NULL; + } + ssh_socket_free(session->socket); session->socket=ssh_socket_new(session); ssh_socket_set_fd(session->socket,fd); - session->options=ssh_options_copy(ssh_bind->options); session->dsa_key=dsa; session->rsa_key=rsa; return session;