Bug 1883 - use setproctitle for persistent mux master
Summary: use setproctitle for persistent mux master
Status: CLOSED FIXED
Alias: None
Product: Portable OpenSSH
Classification: Unclassified
Component: ssh (show other bugs)
Version: 5.8p1
Hardware: All All
: P2 enhancement
Assignee: Assigned to nobody
URL:
Keywords:
Depends on:
Blocks: V_5_9 1911
  Show dependency treegraph
 
Reported: 2011-03-28 17:57 AEDT by Bert Wesarg
Modified: 2011-09-06 15:32 AEST (History)
1 user (show)

See Also:


Attachments
implementation (1.98 KB, patch)
2011-03-28 17:57 AEDT, Bert Wesarg
no flags Details | Diff
OpenBSD patch (461 bytes, patch)
2011-05-06 10:48 AEST, Damien Miller
dtucker: ok+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Bert Wesarg 2011-03-28 17:57:58 AEDT
Created attachment 2024 [details]
implementation

Preserving the command line from the invoking ssh command doesn't
make much sense, so use setproctitle() to hide the arguments. And
chdir into /.
Comment 1 Damien Miller 2011-04-12 15:37:26 AEST
Comment on attachment 2024 [details]
implementation

>@@ -230,12 +230,25 @@ main(int ac, char **av)
> 	struct servent *sp;
> 	Forward fwd;
> 
>-	/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
>-	sanitise_stdfd();
>-
> 	__progname = ssh_get_progname(av[0]);
> 	init_rng();
> 
>+#ifndef HAVE_SETPROCTITLE
>+	/* Prepare for later setproctitle emulation */
>+	{
>+		/* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
>+		char **saved_argv = xcalloc(ac + 1, sizeof(*saved_argv));
>+		for (i = 0; i < ac; i++)
>+			saved_argv[i] = xstrdup(av[i]);
>+		saved_argv[i] = NULL;
>+		compat_init_setproctitle(ac, av);
>+		av = saved_argv;

compat_init_setproctitle() should save everything that is necessary. Is it not working for you?

Also, why reorder the sanitise_stdfd() call?
Comment 2 Bert Wesarg 2011-04-12 16:36:14 AEST
(In reply to comment #1)
> 
> compat_init_setproctitle() should save everything that is necessary. Is
> it not working for you?
> 
> Also, why reorder the sanitise_stdfd() call?

I copied from sshd.c which includes the different order of the sanitise_stdfd() to make int consistent. I just put the save_argv code inside the HAVE_SETPROCTITLE guard too, because I don't see a need for this without setproctitle.
Comment 3 Damien Miller 2011-05-06 10:48:42 AEST
Created attachment 2040 [details]
OpenBSD patch

openbsd patch
Comment 4 Bert Wesarg 2011-05-06 15:59:29 AEST
What about the chdir(/)? I think this is mandatory for daemons. I also suggest to change the proctitle after a mux stopped listening. Maybe something like this in client_stop_mux():

setproctitle("[closed mux]");

Thanks.
Comment 5 Damien Miller 2011-05-06 16:10:25 AEST
chdir("/") is required for system daemons so they don't block the unmounting of filesystems by holding directory fds open. This seems to be less important for user background processes, where the user would probably remain logged in anyway. Furthermore, in ssh's case it will require ongoing access to known_hosts over the life of the connection (as the hostkey can legally change during key re-exchange) and to the mux socket itself.

I guess you could make a case for doing chdir(dirname(UserKnownHostsFile)) or chdir to the directory of the mux socket, but that is really a separate issue.

as for doing setproctitle for gracefully-closed mux masters: good idea. I'll add it to the patch.
Comment 6 Damien Miller 2011-05-06 16:16:47 AEST
actually, unconditionally doing setproctitle() in process_mux_stop_listening() is incorrect. It should only be done for forked masters.
Comment 7 Bert Wesarg 2011-05-06 16:28:23 AEST
> chdir("/") is required for system daemons so they don't block the
> unmounting of filesystems by holding directory fds open.

This is exactly my problem, say I do some ssh/scp from a directory on an USB stick or an sshfs mount, than the persistent mux should not prevent unmounting of this, completely unrelated, mount point.
Comment 8 Bert Wesarg 2011-05-06 16:30:43 AEST
> actually, unconditionally doing setproctitle() in
> process_mux_stop_listening() is incorrect. It should only be done for
> forked masters.

Sure, but I proposed doing it in client_stop_mux() and there under the options.control_persist condition.
Comment 9 Damien Miller 2011-05-06 22:04:26 AEST
ok, doing chdir() is a separate issue - please file a bug for that and we can discuss a solution there. I guess it comes down to choosing between the directory containing the mux socket, the user's home directory or the directory containing UserKnownHostsFile.
Comment 10 Bert Wesarg 2011-05-06 22:29:36 AEST
(In reply to comment #9)
> ok, doing chdir() is a separate issue - please file a bug for that and
> we can discuss a solution there. I guess it comes down to choosing
> between the directory containing the mux socket, the user's home
> directory or the directory containing UserKnownHostsFile.

Done: #1902
Comment 11 Damien Miller 2011-06-03 12:12:15 AEST
applied - this will be in openssh-5.9. thanks!
Comment 12 Damien Miller 2011-09-06 15:32:49 AEST
close resolved bugs now that openssh-5.9 has been released