Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linux: program abort when >&- used #4558

Open
tabraham opened this issue Oct 1, 2024 · 0 comments
Open

linux: program abort when >&- used #4558

tabraham opened this issue Oct 1, 2024 · 0 comments

Comments

@tabraham
Copy link

tabraham commented Oct 1, 2024

On linux, the assumption that file descriptors 0 - 2 belong to stdio
is not correct. They do by default, but they can be closed by the shell at
program launch.

For example, it is possible for someone to use 2>&- on the program command
line, which closes STDERR when running.

This can trigger an abort in libuv due to the assertion fd > STDERR_FILENO in
uv__close.

This can be readily reproduced using the helloworld/main.c example at
#https://docs.libuv.org/en/v1.x/guide/basics.html

#include <stdio.h>
#include <stdlib.h>
#include <uv.h>

int main() {

    uv_loop_t *loop = malloc(sizeof(uv_loop_t));
    uv_loop_init(loop);

    printf("Now quitting.\n");
    uv_run(loop, UV_RUN_DEFAULT);

    uv_loop_close(loop);
    free(loop);
    return 0;

}

The issue occurs because closing STDERR will make file descriptor 2 the
next lowest available file descriptor, which will be consumed by epoll_create1()
in the call trace

uv_loop_init
-> uv__platform_loop_init
-> epoll_create1

Since 2 i now the next lowest unused file descriptor, epoll_create1 will
return 2.

Then,

uv_loop_close
-> uv__close

will fail the assert in uv__close when this file descriptor is passed

There may be other paths for which this can occur, but this appears
to likely be the most common.

tabraham added a commit to tabraham/libuv that referenced this issue Oct 1, 2024
… stdio (libuv#4558)

File descriptors 0 to 2 are used by stdio by default, but are not guaranteed
to be in use by stdio, in which case one or more of them may get used by libuv.

For example, STDERR can be closed by passing 2>&- on the program command line
in the shell. In which case, the file descriptor 2 would be used by libuv

libuv should handle this case without aborting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant