Skip to content

[axlibc] inconsistent mktime behavior with ISO C standard and potential safety issues #312

@WIZeaz

Description

@WIZeaz

The implementation of mktime in arceos/ulib/axlibc/src/mktime.rs is not fully consistent with the ISO C standard and also contains a potential out-of-bounds access issue. Reference for the ISO C standard behavior: https://en.cppreference.com/w/cpp/chrono/c/mktime.

  1. Missing Normalization. The implementation of mktime in arceos/ulib/axlibc/src/mktime.rs does not normalize the input t: *mut ctypes::tm before processing. The ISO C standard permits the tm fields to hold arbitrary values, even values outside their normal ranges. While the ISO standard itself does not define behavior for such cases, POSIX requires mktime to normalize the input to produce a valid result. Another strong reason to fix this problem is that the current implementation can trigger an out-of-bounds memory access. It directly indexes MONTH_DAYS based on (*t).tm_mon, which can exceed 11 when tm_mon >= 12, leading to unsafe memory access.
  2. Missing expected behaviors. According to the ISO C standard, “If the conversion is successful, the time object is modified. All fields of time are updated to fit their proper ranges. time->tm_wday and time->tm_yday are recalculated using information available in other fields.” “A negative value of time->tm_isdst causes mktime to attempt to determine if Daylight Saving Time was in effect.” However, these expected behaviors are currently not implemented.
  3. O(year) time complexity. The current implementation exhibits O(year) time complexity because it computes day offsets year by year. An attacker could supply an excessively large tm_year value, causing the application program to hang or severely degrade performance.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions