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: Open files with O_CLOEXEC to not leak fds to child processes #446

Merged
merged 1 commit into from
Aug 21, 2022

Conversation

ids1024
Copy link
Contributor

@ids1024 ids1024 commented Aug 20, 2022

In general, open() should always be called with O_CLOEXEC, except immediately before an exec* call that will use the fd.

Previously the fd was leaked to a child process, as seen here:

int main() {
    assert(hid_open_path("/dev/hidraw0") > 0);
    char *args[] = {"/bin/sh", "-c", "readlink /proc/self/fd/*", NULL};
    execve("/bin/sh", args, NULL);
}

In some cases this could be a security issue if child processes may be less privileged and shouldn't have access to the file.

In general, `open()` should always be called with `O_CLOEXEC`, except
immediately before an `exec*` call that will use the fd.

Previously the fd was leaked to a child process, as seen here:

```c

int main() {
    assert(hid_open_path("/dev/hidraw0") > 0);
    char *args[] = {"/bin/sh", "-c", "readlink /proc/self/fd/*", NULL};
    execve("/bin/sh", args, NULL);
}
```

In some cases this could be a security issue if child processes may be
less privileged and shouldn't have access to the file.
Copy link
Member

@Youw Youw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Youw Youw merged commit dbd1681 into libusb:master Aug 21, 2022
@libusb libusb locked and limited conversation to collaborators Aug 21, 2022
@mcuee mcuee added the hidraw Related to Linux/hidraw backend label Dec 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
hidraw Related to Linux/hidraw backend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants