time_rz: fix heap buffer overflow vulnerability 85/266285/1 accepted/tizen_6.5_base accepted/tizen_6.5_base_tool tizen_6.5_base accepted/tizen/6.5/base/20230714.002515 accepted/tizen/6.5/base/tool/20211110.101245 submit/tizen_6.5_base/20211110.081345
authorPádraig Brady <P@draigBrady.com>
Mon, 24 Apr 2017 08:43:36 +0000 (01:43 -0700)
committerDongkyun Son <dongkyun.s@samsung.com>
Wed, 10 Nov 2021 07:59:44 +0000 (07:59 +0000)
commit14a9ad9acdb2b2f63095a84328969acc4e4db81b
treedcdb03b4f0d94b9e8218a9435bcbf6c00effdc79
parente6f4943b3ff4e25340c980174b8dc5342e2c0ae8
time_rz: fix heap buffer overflow vulnerability

This issue has been assigned CVE-2017-7476 and was
detected with American Fuzzy Lop 2.41b run on the
coreutils date(1) program with ASAN enabled.

  ERROR: AddressSanitizer: heap-buffer-overflow on address 0x...
  WRITE of size 8 at 0x60d00000cff8 thread T0
  #1 0x443020 in extend_abbrs lib/time_rz.c:88
  #2 0x443356 in save_abbr lib/time_rz.c:155
  #3 0x44393f in localtime_rz lib/time_rz.c:290
  #4 0x41e4fe in parse_datetime2 lib/parse-datetime.y:1798

A minimized reproducer is the following 120 byte TZ value,
which goes beyond the value of ABBR_SIZE_MIN (119) on x86_64.
Extend the aa...b portion to overwrite more of the heap.

  date -d $(printf 'TZ="aaa%020daaaaaab%089d"')

localtime_rz and mktime_z were affected since commit 4bc76593.
parse_datetime was affected since commit 4e6e16b3f.

* lib/time_rz.c (save_abbr): Rearrange the calculation determining
whether there is enough buffer space available.  The rearrangement
ensures we're only dealing with positive numbers, thus avoiding
the problematic promotion of signed to unsigned causing an invalid
comparison when zone_copy is more than ABBR_SIZE_MIN bytes beyond
the start of the buffer.
* tests/test-parse-datetime.c (main): Add a test case written by
Paul Eggert, which overwrites enough of the heap so that
standard glibc will fail with "free(): invalid pointer"
without the patch applied.
Reported and analyzed at https://bugzilla.redhat.com/1444774

(cherry-pick from gnulib commit 94e01571507835ff59dd8ce2a0b56a4b566965a4)

Change-Id: Ie44025ff673705a339f6cbead646a47173bcbcbd
(cherry picked from commit 32d6ed7e700bd5ecd78847eaec545eb1ff65bba5)
lib/time_rz.c