Skip to content

alt_clock_gettime() does not check return value of host_get_clock_service() (macOS) #4804

@zqw86713

Description

@zqw86713

Summary

The implementation of alt_clock_gettime() in clock.cpp does not check the return value of host_get_clock_service() or clock_get_time(). If either of these system calls fail, the function proceeds to use an uninitialized mach_timespec_t, resulting in undefined behavior or potential crashes.

Affected Code

https://github.com/zeromq/libzmq/blob/master/src/clock.cpp

int alt_clock_gettime (int clock_id, timespec *ts)
{
    clock_serv_t cclock;
    mach_timespec_t mts;
    host_get_clock_service (mach_host_self (), clock_id, &cclock);
    clock_get_time (cclock, &mts);
    mach_port_deallocate (mach_task_self (), cclock);
    ts->tv_sec = mts.tv_sec;
    ts->tv_nsec = mts.tv_nsec;
    return 0;
}

Problem

  • host_get_clock_service() and clock_get_time() can fail, returning a non-zero kern_return_t
  • No return code is checked before using the result
  • If either call fails, mts.tv_sec and mts.tv_nsec are undefined
  • This can result in corrupted timestamps or segmentation faults

Expected Behavior

  • Both system calls should be checked for KERN_SUCCESS before using mts
  • If any of them fails, alt_clock_gettime() should return -1 as an error signal, consistent with clock_gettime behavior

Platform

  • macOS < 10.12 (this code path is used as fallback)
  • Affects users relying on alt_clock_gettime() for monotonic time

Impact

Low frequency, but high risk when triggered — undefined time values or potential crash.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions