unix: some bugs found by audit

Đang mở
#5,460 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
15/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c, linux, macos

Hướng nghiên cứu

This is a tracker for 24 separate bugs across src/unix, so pick one self-contained item rather than the whole list, such as the one-line fd2 fix in src/unix/process.c:725-733 or the EOF check in src/unix/fs.c:1376-1392. Read the cited lines and the listed trigger, then reproduce it with a small test program. Done means the trigger no longer reproduces and the existing unix tests still pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Bugs found in src/unix

Audit of the Unix backend on branch v1.x (as of f08538cc).
Line numbers refer to that revision.

Legend:

  • Severity: High (memory corruption, hang, wrong data, crash), Medium (leak,
    wrong error code, wrong result in a specific setup), Low (minor, performance).
  • Status: Reproduced = triggered with a test program; Confirmed = verified
    by reading the code and its callers.

High severity

1. uv_udp_try_send2: sendmmsg batching loop skips messages and misreports what was sent
  • File: src/unix/udp.c:1433-1449 (uv__udp_sendmsgv)
  • Status: Confirmed
  • The inner loop already advances i by n while filling m[], and then
    i += r advances it again after sendmmsg().
    for (n = 0; i < count && n < ARRAY_SIZE(m); i++, n++) ...
    r = sendmmsg(fd, m, n, 0);
    nsent += r;
    i += r;
    
  • Trigger: count = 30, the socket buffer is nearly full, and sendmmsg sends
    only 5 of the first 20 messages. i becomes 25, so messages 5–24 are never
    tried and 25–29 are sent. The function returns nsent = 10, so the caller
    thinks messages 0–9 went out. With full batches it also stops early
    (r = 20 makes i = 40).
  • Fix: set i = i - n + r instead of i += r.
2. uv_spawn (posix_spawn path): scratch-fd search can pick an fd that is still in use
  • File: src/unix/process.c:725-733
  • Status: Confirmed
  • After a collision the loop sets fd2 = 0, and the loop's fd2++ turns it into 1,
    so child_fds[0] is never checked against the new use_fd.
  • Trigger: child_fds = {4, 3, 1} (stderr inherited from fd 1, i.e. 2>&1).
    For target fd 2 the search tries 3 (taken by child_fds[1]), then picks 4,
    which is child_fds[0]. adddup2(1, 4) overwrites the child's fd 4 before
    it is dup'd onto fd 0, so stdin silently ends up as the parent's fd 1.
  • Affects Linux and macOS, which use posix_spawn by default.
  • Fix: fd2 = -1.
3. uv_spawn: stdio pipe fds closed twice when opening a stream fails
  • File: src/unix/process.c:1232-1252
  • Status: Confirmed
  • On failure at index i, uv__process_close_stream() closes the streams for
    j < i, which closes pipes[j][0] through uv__stream_close. pipes[j][0]
    is not reset to -1, so the error: loop closes the same fd number again.
  • Trigger: stdio[0] is UV_CREATE_PIPE with a fresh pipe, and stdio[1] is
    UV_CREATE_PIPE with a pipe that is already open (uv__stream_open returns
    UV_EBUSY). In a multithreaded program, the second close can close an fd
    another thread just opened, such as a threadpool file operation.
4. uv_fs_copyfile: infinite loop if the source is shorter than its st_size
  • File: src/unix/fs.c:1376-1392
  • Status: Confirmed
  • The loop runs until bytes_to_send (taken from fstat) reaches 0, but
    uv_fs_sendfile returns 0 at EOF, and that case is not handled.
  • Trigger: copying a file while another process truncates it, or copying a
    sysfs file (st_size 4096 but only a few bytes of content). The threadpool
    thread, or a synchronous caller, spins forever.
  • Fix: break out of the loop when bytes_written == 0.
5. uv_pipe_connect2: no guard against a connect that is already in progress
  • File: src/unix/pipe.c:269-358
  • Status: Reproduced
  • Unlike uv__tcp_connect (which returns UV_EALREADY), the pipe version
    overwrites handle->connect_req and calls uv__req_init again.
  • Trigger: call uv_pipe_connect2(&r1, ...) and then uv_pipe_connect2(&r2, ...)
    on the same handle. r1's callback never runs, and its active request is never
    released, so uv_run(UV_RUN_DEFAULT) never returns, even after the handle is
    closed.
6. uv_pipe_connect2 on a handle that already has an fd leaves it unreadable and unwritable
  • File: src/unix/pipe.c:301-345
  • Status: Reproduced
  • UV_HANDLE_READABLE | UV_HANDLE_WRITABLE are set only when the call creates
    the socket (new_sock).
  • Trigger 1: connect fails with ENOENT, then a retry to a valid path succeeds.
    The handle reports readable=0 writable=0, uv_read_start returns
    UV_ENOTCONN, and uv_write returns UV_EPIPE.
  • Trigger 2: uv_pipe_bind followed by uv_pipe_connect fails the same way.
7. uv_signal_start after uv_signal_start_oneshot: the handle stays one-shot
  • File: src/unix/signal.c:424-425, src/unix/signal.c:540-577
  • Status: Confirmed
  • UV_SIGNAL_ONE_SHOT is set on start but never cleared, not even in
    uv__signal_stop.
  • Trigger: uv_signal_start_oneshot(h, cb, SIGUSR1), let it fire (or call
    stop), then uv_signal_start(h, cb, SIGUSR1). The handle still stops itself
    after the first signal.
  • Related: the short-circuit at line 396 (signum == handle->signum) ignores a
    switch between one-shot and regular mode on a running handle.
8. macOS fsevents: uv__fsevents_init error path leaves freed memory reachable
  • File: src/unix/fsevents.c:822-858
  • Status: Confirmed
  • When uv__cf_loop_signal fails (ENOMEM), cf_cb has already gone through
    uv_async_init (so it is in the loop's handle and async queues), and the
    handle is already in state->fsevent_handles. The error path only frees
    cf_cb and destroys cf_mutex. The loop queues and the CF thread then
    point at freed memory.
  • fail_cf_mutex_init has the same problem: it frees an async handle that has
    already been initialized.
9. macOS uv_set_process_title: NULL function-pointer call after a failed lookup
  • File: src/unix/darwin-proctitle.c:104, :173-180, :209-213
  • Status: Confirmed
  • ls.CFRelease is set before the LaunchServices symbol lookups that can still
    fail. On failure, ls.CFRelease stays non-NULL, so the next
    uv__set_process_title skips the lookup and calls
    ls.LSSetApplicationLaunchServicesServerConnectionStatus, which is NULL.
  • Trigger: a macOS version that lacks one of the private symbols. The first
    call fails silently, because proctitle.c ignores the return value, and the
    second call crashes.
10. Linux uv_cpu_info (arm64): every CPU gets the first model name
  • File: src/unix/linux.c:1906-1907
  • Status: Reproduced
  • model++ runs after found is set, so model points one past the slot that
    matched. Slot 1 stays empty and matches every name (strlen("") == 0), so
    every later model resolves to slot 2, which holds the first model's name.
  • Trigger: big.LITTLE systems (for example Cortex-A55 + A76) report all cores
    as Cortex-A55. The strncmp prefix match also confuses names like
    "Cortex-A78" and "Cortex-A78AE".

Medium severity

11. uv_fs_sendfile to an O_APPEND fd fails with EBADF instead of falling back
  • File: src/unix/fs.c:1050-1051
  • Status: Reproduced
  • copy_file_range returns EBADF for an O_APPEND output fd, but only EINVAL
    and ENOSYS trigger the sendfile and read/write fallbacks, both of which
    would work.
12. cgroup v2 CPU quota: the tightest ancestor limit is computed but never returned
  • File: src/unix/linux.c:2426-2442
  • Status: Confirmed
  • min_quota is computed but never written back, so *quota ends up as the
    limit of the highest ancestor that has one.
  • Trigger: /a/cpu.max allows 4 CPUs, /a/b/cpu.max allows 2, and the
    process is in /a/b. uv_available_parallelism() returns 4.
13. cgroup v1 CPU controller path is parsed incorrectly
  • File: src/unix/linux.c:2449-2457
  • Status: Confirmed
  • For a line like 4:cpu,cpuacct:/docker/abc, skipping ":cpu," leaves
    cpuacct:/docker/abc as the path. The file is never found, and the code
    returns UV_EIO.
  • A plain N:cpu:/path line is not matched at all. As a result, CPU quotas
    never apply on cgroup v1 hosts (for example docker --cpus=2).
14. cgroup path buffers are 256 bytes and silently truncate
  • File: src/unix/linux.c:2367, :2462
  • Status: Confirmed
  • char path[256] is too short for deep Kubernetes/systemd cgroup paths.
    snprintf truncates the path, the open fails, and the CPU limit is ignored.
    The memory-limit code uses 4097-byte buffers.
15. uv_socketpair checks the wrong flag (BSDs)
  • File: src/unix/tcp.c:647
  • Status: Confirmed
  • The check is flags & UV_FS_O_NONBLOCK but should be flags & SOCK_NONBLOCK.
    On FreeBSD, NetBSD and OpenBSD O_NONBLOCK == 4, so for SOCK_SEQPACKET (5)
    or SOCK_RDM (4) the early return skips uv__nonblock, and the fds come
    back blocking.
16. uv__stream_open sets handle flags before steps that can still fail
  • File: src/unix/stream.c:411-421
  • Status: Confirmed
  • If nodelay, keepalive or SO_OOBINLINE fails, the handle keeps
    READABLE|WRITABLE with io_watcher.fd == -1. A later uv_read_start
    reaches uv__io_start(fd=-1), which asserts in debug builds and writes
    loop->watchers[-1] in release builds.
  • Trigger: on macOS, uv_tcp_nodelay(client, 1) before uv_accept, and the
    peer sends RST before the accept is processed.
17. uv_spawn can return a positive value
  • File: src/unix/process.c:841, :889-892
  • Status: Confirmed
  • err starts at -1. If every PATH entry is at least PATH_MAX long, no
    spawn is attempted, -1 is returned, and the caller returns
    UV__ERR(-1) == 1. Callers that check r < 0 treat this as success.
  • Fix: start with err = ENOENT.
18. uv__fsevents_close returns a positive errno and leaks on failure
  • File: src/unix/fsevents.c:880-882
  • Status: Confirmed
  • err is already a negative UV code, so UV__ERR(err) makes it positive, and
    uv_fs_event_stop returns that positive value. The handle has also already
    been removed from the list, but cf_cb, cf_mutex and realpath are not
    cleaned up.
19. illumos/Solaris: a failed first uv_fs_event_start disables fs events for the loop
  • File: src/unix/sunos.c:507-539
  • Status: Confirmed
  • loop->fs_fd is set before uv__fs_event_rearm. If that fails, for example
    with ENOENT, the port watcher is never started, and later starts see
    first_run == 0, so they never start it either. No events are delivered
    on that loop again.
20. macOS tty (select fallback): uv__tty_close compares the wrong fd
  • File: src/unix/tty.c:357-366 vs :303/:319
  • Status: Confirmed (code reading)
  • orig_termios_fd is set from uv__stream_fd(), which is the real tty fd, but
    close compares it with io_watcher.fd, which is the socketpair fake_fd. The
    terminal is not restored, and uv_tty_reset_mode() later runs tcsetattr
    on a closed or reused fd.
21. AIX AHAFS: memory leak on every directory event, plus a possible NULL dereference
  • File: src/unix/aix.c:672, :682-683, :739-749
  • Status: Confirmed
  • dir_filename = uv__strdup(...) runs on every event without freeing the
    previous value.
  • After AHAFS_MODDIR_UNMOUNT sets it to NULL, uv__strscpy(fname, NULL, ...)
    dereferences NULL.
22. AIX uv_fs_event_start: leaks, and no check for an already-active handle
  • File: src/unix/aix.c:451-484, :601-603, :770-802
  • Status: Confirmed
  • uv__is_ahafs_mounted leaks p on every error path.
  • uv__setup_ahafs leaks the fd when write() fails.
  • A second start on an active handle overwrites the watcher and the path
    without cleaning up the old ones.
23. z/OS: closing an fd whose number equals the message-queue slot disables fs events
  • File: src/unix/os390-syscalls.c:363, :243
  • Status: Confirmed
  • The slot items[size-1] is reserved for the message queue, but
    epoll_file_close and EPOLL_CTL_DEL only check fd < size. Closing an
    unrelated fd numbered size-1 clears the message-queue entry.
24. QNX uv_resident_set_memory checks for failure the wrong way
  • File: src/unix/qnx.c:134-143
  • Status: Confirmed
  • uv__open_cloexec returns -errno, not -1. The code then calls devctl and
    uv__close on a negative fd, which trips an assert in debug builds and
    returns the wrong error in release builds.
25. QNX uv_exepath ignores a realpath failure
  • File: src/unix/qnx.c:71-73
  • Status: Confirmed
  • If realpath fails, path is uninitialized. strlcpy copies stack garbage,
    possibly reading past the buffer, and the function still returns 0.
26. kqueue uv_fs_event_start leaks the fd and path when uv__io_init_start fails
  • File: src/unix/kqueue.c:620-629
  • Status: Confirmed (needs OOM)
27. kqueue EVFILT_USER async: placeholder fd leaks when kevent registration fails
  • File: src/unix/async.c:249-265
  • Status: Confirmed
28. Linux uv_fs_event_start leaks the inotify watch when malloc fails
  • File: src/unix/linux.c:2772
  • Status: Confirmed
  • inotify_rm_watch is not called, so the watch keeps counting against the
    per-user inotify quota.
29. macOS uv_thread_setname / uv_thread_getname return the wrong error code
  • File: src/unix/thread.c:907-909, :954-955
  • Status: Confirmed
  • pthread_setname_np and pthread_getname_np return the error number
    directly, but the code returns UV__ERR(errno), which can be stale or 0.
30. uv_pipe_bind2 with namelen == 0 on Linux reads *name and binds a bogus name
  • File: src/unix/pipe.c:126
  • Status: Reproduced
  • uv_pipe_bind2(&p, "foo", 0, 0) takes the file-path branch and binds an
    abstract name of 108 NUL bytes instead of autobinding. The bind is not
    repeatable, and close calls unlink("").

Low severity

31. Wrong kernel-version constant disables io_uring close on 5.15.90+
  • File: src/unix/linux.c:901
  • Status: Confirmed
  • 0x050A00 is 5.10.0, not 5.16.0, so the 5.15.90+ LTS range always falls back to
    the threadpool for close. This costs performance only.
32. uv_cond_timedwait / uv_thread_create_ex integer overflow
  • File: src/unix/thread.c:858, :164
  • Status: Confirmed
  • timeout += uv__hrtime() wraps for very large timeouts such as UINT64_MAX,
    which causes an immediate UV_ETIMEDOUT.
  • Rounding the stack size wraps to 0 near SIZE_MAX, which silently gives the
    minimum stack size instead of an error.
33. EAI_SYSTEM with errno == 0 maps to success
  • File: src/unix/getaddrinfo.c:87
  • Status: Plausible
  • The callback can get status == 0 with res == NULL.
34. macOS uv_tty_init error path restores flags on a closed fd
  • File: src/unix/tty.c:219-229
  • Status: Confirmed
  • The caller's original fd is left O_NONBLOCK.
35. IPC read: data is lost when control-message handling fails
  • File: src/unix/stream.c:1122-1127
  • Status: Confirmed (needs ENOMEM)
  • The nread payload bytes already received are dropped, read_cb gets the
    error, and the stream keeps reading, which corrupts framing.
36. vm_deallocate gets an element count instead of a byte size
  • File: src/unix/darwin.c:227, :245
  • Status: Confirmed
  • The size passed should be msg_type * sizeof(integer_t). This only leaks on
    machines with more than about 256 CPUs.
37. DragonFly uv_cpu_info reads uninitialized heap
  • File: src/unix/freebsd.c:201-202, :249-269
  • Status: Confirmed
  • kern.cp_time returns one aggregate row, but the code reads one row per CPU.
Minor and other notes
  • src/unix/aix.c:88-89: uv__platform_loop_init returns a literal -1
    instead of UV__ERR(errno).
  • src/unix/aix.c:1251: uv_interface_addresses always returns UV_ENOSYS
    on a system error.
  • src/unix/ibmi.c:234: uv_get_free_memory returns total memory.
  • src/unix/fsevents.c:646-650: the fail_thread_create path never
    CFReleases signal_source.
  • src/unix/hurd.c:48-57: uv_exepath with *size == 1 reads an
    uninitialized buffer.
  • src/unix/sunos.c:869: uv_interface_addresses dereferences
    ifa_netmask without a NULL check.
  • src/unix/linux.c: uv__inotify_fork returns early on a restart error,
    leaking the remaining path copies.
  • src/unix/fs.c: uv__fs_readlink silently truncates when len == maxlen.
Ngôn ngữ chính
C
Star
27.4k
Fork
4k
Merge trung bình
4 ngày 14 giờ
Pull request đã merge (30 ngày)
23

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của libuv/libuv

Tất cả issue của libuv/libuv

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.