Skip to content

NODERAWFS open() O_DIRECTORY / opendir() leak file descriptor on error #16452

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

Closed
tiran opened this issue Mar 8, 2022 · 2 comments · Fixed by #16463
Closed

NODERAWFS open() O_DIRECTORY / opendir() leak file descriptor on error #16452

tiran opened this issue Mar 8, 2022 · 2 comments · Fixed by #16463

Comments

@tiran
Copy link
Contributor

tiran commented Mar 8, 2022

open() with O_DIRECTORY as used by opendir() leaks a file descriptor if the filename is not a directory (ENOTDIR).

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.2 (a77b559a8b40b7e89fc8c17e41034128df9543e4)
clang version 14.0.0 (https://github.com/llvm/llvm-project 782c0dd1a1c235afb09a34e7da4a1267ead14765)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /emsdk/upstream/bin

reproducer

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>

#define FNAME "/etc/passwd"
#define LOOP 10000

int main(void) {
    int fd;
    for (int i=0; i < LOOP; i++) {
        fprintf(stderr, "%i\n", i);
        fd = open(FNAME, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
        assert(fd < 0);
        if (errno != ENOTDIR) {
            perror("open O_DIRECTORY failed");
            return 1;
        }
    }
    return 0;
}
$ emcc -s NODERAWFS -o dirfdleak.js dirfdleak.c
$ node dirfdleak.js
...
open O_DIRECTORY failed: No file descriptors available
@sbc100
Copy link
Collaborator

sbc100 commented Mar 8, 2022

Thanks for great repro cases with these bugs! This should be fairly easy fix. I don't have time to look at it right now so labeled as "help wanted".

tiran added a commit to tiran/emscripten that referenced this issue Mar 10, 2022
nfd must be closed when open() O_DIRECTORY fails with ENOTDIR.

Fixes: emscripten-core#16452
Signed-off-by: Christian Heimes <[email protected]>
@tiran
Copy link
Contributor Author

tiran commented Mar 10, 2022

You were right, it was a trivial fix.

tiran added a commit to tiran/emscripten that referenced this issue Mar 10, 2022
nfd must be closed when open() O_DIRECTORY fails with ENOTDIR.

Fixes: emscripten-core#16452
Signed-off-by: Christian Heimes <[email protected]>
sbc100 pushed a commit that referenced this issue Mar 11, 2022
nfd must be closed when open() O_DIRECTORY fails with ENOTDIR.

Fixes: #16452
Signed-off-by: Christian Heimes <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants