From: Lennart Poettering Date: Tue, 20 Jun 2023 13:00:07 +0000 (+0200) Subject: hostnamed: when parsing day/month of firmware date, force decimal parsing X-Git-Tag: v254-rc1~163^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F28080%2Fhead;p=thirdparty%2Fsystemd.git hostnamed: when parsing day/month of firmware date, force decimal parsing safe_atou() by default determines the base from the prefix 0x, 0b, 0o and for compat with just 0 for octal. This is not what we want here, since the date components are padded with zeroes yet still decimal. Hence force decimal parsing (and while we are at it, prohibit a couple of unexpected decorations). WIthout this we'd fail to parse any the 8th and 9th day of each months, as well aus aug and september of every year, because these look like octal numbers but cannot actually parsed as such. Let's change the testcase to check for a date that exposes this bheaviour. --- diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index 2f63ac4dcf9..9ef45f8e75a 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -307,20 +307,20 @@ static int get_firmware_date(usec_t *ret) { return -EINVAL; unsigned m, d, y; - r = safe_atou(month, &m); + r = safe_atou_full(month, 10 | SAFE_ATO_REFUSE_PLUS_MINUS | SAFE_ATO_REFUSE_LEADING_WHITESPACE, &m); if (r < 0) return r; if (m < 1 || m > 12) return -EINVAL; m -= 1; - r = safe_atou(day, &d); + r = safe_atou_full(day, 10 | SAFE_ATO_REFUSE_PLUS_MINUS | SAFE_ATO_REFUSE_LEADING_WHITESPACE, &d); if (r < 0) return r; if (d < 1 || d > 31) return -EINVAL; - r = safe_atou(year, &y); + r = safe_atou_full(year, 10 | SAFE_ATO_REFUSE_PLUS_MINUS | SAFE_ATO_REFUSE_LEADING_WHITESPACE, &y); if (r < 0) return r; if (y < 1970 || y > (unsigned) INT_MAX) diff --git a/test/units/testsuite-71.sh b/test/units/testsuite-71.sh index 6f99ecadd81..9d84dd89ed3 100755 --- a/test/units/testsuite-71.sh +++ b/test/units/testsuite-71.sh @@ -119,9 +119,9 @@ EOF mount -t tmpfs none /sys/class/dmi/id echo '1' >/sys/class/dmi/id/uevent - echo '01/01/2000' >/sys/class/dmi/id/bios_date + echo '09/08/2000' >/sys/class/dmi/id/bios_date systemctl stop systemd-hostnamed - assert_in '2000-01-01' "$(hostnamectl)" + assert_in '2000-09-08' "$(hostnamectl)" echo '2022' >/sys/class/dmi/id/bios_date systemctl stop systemd-hostnamed