1

In ssh daemon, select() always returns correctly but after executing following nmap script,

nmap -sV -O -A --script ssh2-enum-algos

select() always returns 1 even though no connection is made to ssh server.

There are several other threads like this but I couldn't find problem with my code.

My code looks like this,

while (1) 
{
    memcpy(tmp_read_set, read_set, sizeof(fd_set));
    retValSS = select(FD_SETSIZE, tmp_read_set, NULL, NULL, NULL);
    switch (retValSS)
    {
        case -1:
            LOG1(CRIT, "select() failed, errno=%d", ipcom_errno);
            goto cleanup;
        case 0: // Timeout....
            break;
        default:
            for(i=0; i<MAX_LISTEN_PORTS; i++)
            {
                if(lst_sock[i] == INVALID_SOCKET)
                    break;

                if(FD_ISSET(lst_sock[i], tmp_read_set))
                {
                    clt_sock = accept(lst_sock[i], NULL, NULL);
                    if (INVALID_SOCKET == clt_sock) /*INVALID_SOCKET is -1*/
                    {
                        LOG1(ERR, "accept() failed, errno=%d", errno);
                        continue;
                    }
                    if(srv_ctx->no_clients >= srv_ctx->max_clients)
                    {
                        LOG0(INFO, "max no of connected clients reached, disconnecting client");
                        close(clt_sock);
                        continue;
                    }
                    else
                    {
                       //some work is being done
                    }
                }
            }
        }
    }

I am getting following error log continuously,

accept() failed, errno = 0

And the strange thing is that errno is 0 which indicates no error. If there is no error, I think then accept() should not fail. Could someone explain in what scenarios accept() can fail.

I would be very thankful if someone could help.

  • Note that you are able to assign `read_set` to `tmp_read_set` directly, without the `memcpy`. – Brian Cain Mar 11 '14 at 13:23
  • What OS are you using? Your use of `INVALID_SOCKET` makes me think WinSock, in which case `errno` is meaningless -- you need to call `WSAGetLastError()` instead. – Chris Dodd Mar 17 '14 at 15:59
  • @ChrisDodd : Thanks Chris for your response. I am using Integrity(RTOS). I don't think there is any issue with errno. I have verified, it is implemented in correct way. I want to highlight one more thing that this occurs only after executing nmap script. Or else there is no issue at all. – Rakesh Gupta Mar 17 '14 at 17:08
  • It should be impossible for `errno` to be 0 after `accept` returns -1, unless your POSIX implementation is broken (possibly by being corrupted due to lack of interprocess memory protection?) – Chris Dodd Mar 17 '14 at 17:42
  • @ChrisDodd : I'll check it again. Your point is very much valid. It may be quiet possible. Thanks again. – Rakesh Gupta Mar 17 '14 at 17:48

1 Answers1

0

What do you expect select() to return? Consider that select() is normally used to wait for multiple file descriptors - if you were connecting two, how would you know which one succeeded/failed based purely on the return value of select? You wouldn't, obviously.

Which is why select() just tells you which file descriptors have changed in some way, and you're supposed to determine independently what that was. In the case of connect(), you should call getsockopt() to retrieve the result of the connection attempt. See this answer where explain how to do a non-blocking connect().

Community
  • 1
  • 1
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
  • Thanks for your response but It doesn't look related to my issue. I am having problem with select() and accept() not the connect(). – Rakesh Gupta Mar 11 '14 at 17:07