Skip to content

Daemonization code doesn't disconnect controlling terminal #141

Closed

Description

@xaizek

This looks like a bug to me:

sshfs/sshfs.c

Lines 1032 to 1076 in eac4207

pid = fork();
if (pid == -1) {
perror("failed to fork");
close(sockpair[1]);
return -1;
} else if (pid == 0) {
int devnull;
devnull = open("/dev/null", O_WRONLY);
if (dup2(sockpair[1], 0) == -1 || dup2(sockpair[1], 1) == -1) {
perror("failed to redirect input/output");
_exit(1);
}
if (!sshfs.foreground && devnull != -1)
dup2(devnull, 2);
close(devnull);
close(sockpair[0]);
close(sockpair[1]);
switch (fork()) {
case -1:
perror("failed to fork");
_exit(1);
case 0:
break;
default:
_exit(0);
}
chdir("/");
if (sshfs.password_stdin) {
int sfd;
setsid();
sfd = open(ptyname, O_RDWR);
if (sfd == -1) {
perror(ptyname);
_exit(1);
}
close(sfd);
close(sshfs.ptyslavefd);
close(sshfs.ptyfd);
}

Why isn't setsid() called after the first fork()? By leaving it open daemonized process can still get at least SIGINT, which wasn't intended for it.

How to reproduce:

  1. mkdir ~/sshfs-tst
  2. vim
  3. (in vim) :!sshfs server: ~/tst
  4. (in vim) :!read and hit CTRL-C

~/tst gets unmounted, which shouldn't happen.

Moving setsid() should be enough to fix this.

Activity

  1. Nikratio commented on Aug 11, 2018

    @Nikratio
    Contributor

    Thanks for the report! Yes, setsid() should come right after the first fork (at least according to Kerrisk, 2010). Could you provide a patch/pull request?

  2. Nikratio commented on Aug 11, 2018

    @Nikratio
    Contributor

    (note that I haven't looked at the actual code. Please make sure that reading the password from stdin still works after this change)

  3. xaizek commented on Aug 11, 2018

    @xaizek
    Author

    No, sorry, building seems to require too much of additional software, I tried to install some, but then it wanted more and more...

  4. jose1711 commented on Aug 11, 2018

    @jose1711

    i moved setsid() right after fork like this

     	pid = fork();
    +	setsid();
    
    

    it works fine in vim but sshfs is unable to read password from stdin (when ssh key is not in place):

    ~ sshfs server: /tmp/mtpoint
    read: Connection reset by peer
    
  5. xaizek commented on Aug 11, 2018

    @xaizek
    Author

    Thanks @jose1711. Realized that I can test this on older version which doesn't require installing anything, but has the same issue. Apparently reading input doesn't work unless final process is a session leader. It seems to work if setsid() is just moved out above if-statement, just to call it unconditionally.

  6. jose1711 commented on Aug 11, 2018

    @jose1711

    @xaizek can you please fork and make a testing branch? i am trying to move setsid() places but sshfs still does not ask for the password. thank you

  7. xaizek commented on Aug 11, 2018

    @xaizek
    Author

    Just make it look something like this:

    chdir("/"); 
    setsid(); 
    
    if (sshfs.password_stdin) { 
    	int sfd; 
    
    	sfd = open(ptyname, O_RDWR); 
    

    sshfs.password_stdin is set on -o password_stdin, so I do:

    echo pass | sshfs -o password_stdin server: /tmp/mtpoint
    

    If I do sshfs server: /tmp/mtpoint, I do get password request but via some pinentry program with and without changes to the source code.

    Do you get password prompt if setsid() is left where it is?

  8. jose1711 commented on Aug 11, 2018

    @jose1711

    with setsid() right below chdir("/"); the behaviour is as follows:

    sshfs server: /tmp/mtpoint
    read: Connection reset by peer
    

    There's no prompt for password or console or in pinentry program.
    Sending password via stdin works

    echo pass | sshfs -o password_stdin server: /tmp/mtpoint
    

    With original sourcecode i am getting prompted on console and password is accepted.

    sshfs server: /tmp/mtpoint
    user@server's password: 
    # mounted
    
  9. xaizek commented on Aug 11, 2018

    @xaizek
    Author

    From man ssh:

    SSH_ASKPASS
    
    If ssh needs a passphrase, it will read the passphrase from the current terminal
    if it was run from a terminal.  If ssh does not have a terminal associated with
    it but DISPLAY and SSH_ASKPASS are set, it will execute the program specified by
    SSH_ASKPASS and open an X11 window to read the passphrase. [...]
    

    That's probably why I get pinentry.

    Since it's ssh process which needs a terminal, it's the one that should detach from it afterwards, but it doesn't have such option. Maybe there is no fix for this issue then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions