# HG changeset patch # User Matt Johnston # Date 1424786493 -28800 # Node ID 2b4fd440399d097d378eb74bb4c758a1318b89e0 # Parent d0e6dd5af46e00ea432d1b20336dd3ca3f0fc10e Free memory before exiting. Based on patch from Thorsten Horstmann. Client side is not complete. diff -r d0e6dd5af46e -r 2b4fd440399d common-session.c --- a/common-session.c Tue Feb 24 20:53:32 2015 +0800 +++ b/common-session.c Tue Feb 24 22:01:33 2015 +0800 @@ -240,6 +240,15 @@ /* Not reached */ } +static void cleanup_buf(buffer **buf) { + if (!*buf) { + return; + } + buf_burn(*buf); + buf_free(*buf); + *buf = NULL; +} + /* clean up a session on exit */ void session_cleanup() { @@ -256,19 +265,31 @@ } chancleanup(); - - /* Cleaning up keys must happen after other cleanup - functions which might queue packets */ - if (ses.session_id) { - buf_burn(ses.session_id); - buf_free(ses.session_id); - ses.session_id = NULL; + + /* Most dropbear functions are unsafe to run after this point */ +#ifdef DROPBEAR_CLEANUP + /* listeners call cleanup functions, this should occur before + other session state is freed. */ + remove_all_listeners(); + + while (!isempty(&ses.writequeue)) { + buf_free(dequeue(&ses.writequeue)); } - if (ses.hash) { - buf_burn(ses.hash); - buf_free(ses.hash); - ses.hash = NULL; - } + + m_free(ses.remoteident); + m_free(ses.authstate.pw_dir); + m_free(ses.authstate.pw_name); + m_free(ses.authstate.pw_shell); + m_free(ses.authstate.pw_passwd); + m_free(ses.authstate.username); +#endif + + cleanup_buf(&ses.session_id); + cleanup_buf(&ses.hash); + cleanup_buf(&ses.payload); + cleanup_buf(&ses.readbuf); + cleanup_buf(&ses.writepayload); + m_burn(ses.keys, sizeof(struct key_context)); m_free(ses.keys); diff -r d0e6dd5af46e -r 2b4fd440399d listener.c --- a/listener.c Tue Feb 24 20:53:32 2015 +0800 +++ b/listener.c Tue Feb 24 22:01:33 2015 +0800 @@ -161,5 +161,14 @@ } ses.listeners[listener->index] = NULL; m_free(listener); +} +void remove_all_listeners(void) { + unsigned int i; + for (i = 0; i < ses.listensize; i++) { + if (ses.listeners[i]) { + remove_listener(ses.listeners[i]); + } + } + m_free(ses.listeners); } diff -r d0e6dd5af46e -r 2b4fd440399d listener.h --- a/listener.h Tue Feb 24 20:53:32 2015 +0800 +++ b/listener.h Tue Feb 24 22:01:33 2015 +0800 @@ -60,4 +60,6 @@ void remove_listener(struct Listener* listener); +void remove_all_listeners(void); + #endif /* DROPBEAR_LISTENER_H */ diff -r d0e6dd5af46e -r 2b4fd440399d svr-chansession.c --- a/svr-chansession.c Tue Feb 24 20:53:32 2015 +0800 +++ b/svr-chansession.c Tue Feb 24 22:01:33 2015 +0800 @@ -787,9 +787,11 @@ TRACE(("back to normal sigchld")) /* Revert to normal sigchld handling */ + /* if (signal(SIGCHLD, SIG_DFL) == SIG_ERR) { dropbear_exit("signal() error"); } + */ /* redirect stdin/stdout/stderr */ close(chansess->master); @@ -1005,9 +1007,11 @@ sa_chld.sa_handler = sesssigchild_handler; sa_chld.sa_flags = SA_NOCLDSTOP; sigemptyset(&sa_chld.sa_mask); + /* if (sigaction(SIGCHLD, &sa_chld, NULL) < 0) { dropbear_exit("signal() error"); } + */ } diff -r d0e6dd5af46e -r 2b4fd440399d svr-session.c --- a/svr-session.c Tue Feb 24 20:53:32 2015 +0800 +++ b/svr-session.c Tue Feb 24 22:01:33 2015 +0800 @@ -78,10 +78,13 @@ }; static void -svr_session_cleanup(void) -{ +svr_session_cleanup(void) { /* free potential public key options */ svr_pubkey_options_cleanup(); + + m_free(svr_ses.addrstring); + m_free(svr_ses.childpids); + m_free(svr_ses.remotehost); } static void @@ -150,6 +153,7 @@ void svr_dropbear_exit(int exitcode, const char* format, va_list param) { char fmtbuf[300]; + int i; if (!sessinitdone) { /* before session init */ @@ -183,6 +187,15 @@ session_cleanup(); } + if (svr_opts.hostkey) { + sign_key_free(svr_opts.hostkey); + svr_opts.hostkey = NULL; + } + for (i = 0; i < DROPBEAR_MAX_PORTS; i++) { + m_free(svr_opts.addresses[i]); + m_free(svr_opts.ports[i]); + } + exit(exitcode); } diff -r d0e6dd5af46e -r 2b4fd440399d sysoptions.h --- a/sysoptions.h Tue Feb 24 20:53:32 2015 +0800 +++ b/sysoptions.h Tue Feb 24 22:01:33 2015 +0800 @@ -256,6 +256,9 @@ #define DROPBEAR_LISTEN_BACKLOG MAX_CHANNELS #endif +/* free memory before exiting */ +#define DROPBEAR_CLEANUP + /* Use this string since some implementations might special-case it */ #define DROPBEAR_KEEPALIVE_STRING "keepalive@openssh.com"