Created attachment 1637 [details] Flushed pending output such as motd before execv of shell In do_login (session.c), the routine do_motd() is called that writes to the stdout stream. Apparently, it may have been assumed that stdout was line buffered because it was a pseudo terminal. Even though stdio is indeed a terminal, this may not be the correct assumption. The following sequence of events can occur: 1) Forked child "rexecs" sshd in main() with stdout pointing to a non-tty device (accepted socket). 2) Because the stdout stream is not a terminal, the libc startup code run immediately in the execed process will cause the stdout stream to be fully buffered. 3) After some processing, do_exec_pty (session.c) is called which dup2 the the psedudo tty to fd 1. The stdio buffering mode for the stdout stream is not affected and is still fully buffered even though it is now a terminal. 4) Process calls do_motd and calls fputs to the stdout stream for the motd. The data is now buffered but is not displayed even though it is a terminal. 5) Process execs the shell. Since the stdout stream is now a terminal, the startup code in libc will cause the stdout stream to be line buffered. From this point forward, everything is ok. However, the buffered data from the motd will will lost. Suggest a fflush(NULL) be called before the execv of the shell or alternatively after do_motd writes to stdout. Attached patch does the former.
Additional info: I am using uClibc rather than glibc. Apparently glibc forces the stream to line buffer mode on the first I/O if it is a terminal so when the loginmsg or motd is printed in step 4, even though it is fully buffered before the I/O, it is switched over during the stdio call such as printf or fputs causing the output to be flushed. However, uClibc does not do this which explains the problem reported.
Created attachment 1638 [details] Flushed pending output so motd is not lost
Created attachment 1639 [details] Flushed pending output so motd is not lost
Created attachment 1694 [details] flush just before exec() Does this work? It delays the flush until just before we perform an exec
(In reply to comment #4) > Does this work? It delays the flush until just before we perform an > exec Works fine, thanks!
fix committed, will be in openssh-5.4
With the release of 5.4p1, this bug is now considered closed.