NEAT-project / neat

A New, Evolutive API and Transport-Layer Architecture for the Internet
https://www.neat-project.org/
BSD 3-Clause "New" or "Revised" License
66 stars 19 forks source link

neat_free_candidate: uses already closed file descriptor #363

Closed bagder closed 5 years ago

bagder commented 7 years ago

candidate->pollable_socket->fd is closed on neat_core.c:537 (and not assigned to anything else)

It is then subsequently wrongly accessed and used on line 550 and onwards.

Coverity CID 1452602

karlgrin commented 6 years ago

@bagder To me it seems that it is enough to assign 'candidate->pollable_socket->fd' to '-1' after 'close(candidate->pollable_socket->fd)'. Or, is it something I am missing here?

bagder commented 6 years ago

I don't understand the logic in this function so I'm not sure.

if (candidate->pollable_socket->fd) { in itself seems wrong since zero is a valid file descriptor, while -1 is typically used for an already-closed descriptor. That should probably rather be:

    if (candidate->pollable_socket->fd != -1) {
        close(candidate->pollable_socket->fd);
        candidate->pollable_socket->fd = -1;
    }

... but if we do that, the check further down that checks if (candidate->pollable_socket->fd == -1) would be totally superfluous as it would then always be true so the two else clauses below that are basically dead code!

karlgrin commented 6 years ago

@bagder I have removed the lines

if (candidate->pollable_socket->fd != -1) {
           close(candidate->pollable_socket->fd);
           candidate->pollable_socket->fd = -1;
    }

From my understanding, we should anyway close the file descriptor by entering the

... else if ( !uv_is_closing((uv_handle_t *) candidate->pollable_socket->handle)) {
   ...
   }

If you agree, we could close this issue.

bagder commented 6 years ago

The new code:

    if (candidate->pollable_socket->fd) {
        close(candidate->pollable_socket->fd);
    }

.. still doesn't check for != -1, but more importantly it doesn't set it to -1 after it closes the file descriptor so it will not match the correct check further down in the same function.