Skip to content

mod_dav: Fix dav_get_timeout_string() overflowing an attacker-control… - #920

Merged
arturobernalg merged 1 commit into
apache:trunkfrom
arturobernalg:fix/httpd-65788-dav-timeout-overflow
Nov 6, 2026
Merged

arturobernalg merged 1 commit into
apache:trunkfrom
arturobernalg:fix/httpd-65788-dav-timeout-overflow

Conversation

@arturobernalg

@arturobernalg arturobernalg commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

dav_get_timeout_string() (modules/dav/main/util.c) parses a "Timeout: Second-" header with atol(), which has no overflow detection. A long enough digit string overflows it, and the later "now + expires" addition overflows time_t too, so the lock ends up stored with a nonsensical (already-expired) timeout instead of the large-but-finite one the client asked for -- a LOCK request that looks successful (200, a valid Lock-Token) silently grants a lock that is already expired.

Confirmed with a real build: a LOCK request with "Timeout: Second-99999999999999999999999999999999" got back "<D:timeout>Second-0</D:timeout>" before the fix (atol() saturates to LONG_MAX on this platform, and now + LONG_MAX overflows time_t to a large negative value), and "<D:timeout>Second-4294967295</D:timeout>" after it -- the RFC 4918 TimeOut maximum, matching the overflow handling the existing code comment already called for ("We need to handle overflow better: timeout will be <= 2^32 - 1") but never implemented.

Fix: parse with apr_strtoi64() (overflow-checked via errno), and clamp to 2^32 - 1 on overflow or on an in-range value above that maximum. A negative value (e.g. "Second--5") clamps to 0. A value with no digits at all (e.g. "Second-notanumber") also clamps to 0, matching what atol() already silently did for that input on trunk -- not Infinite, which would grant more than before instead of less (verified: trunk's actual current behavior for "Second-notanumber" is "Second-0", not "Infinite" as an earlier version of this PR incorrectly claimed).

The RFC 4918 maximum does not always fit a time_t: on platforms where time_t is 32 bits (still the glibc default for 32-bit Linux without _TIME_BITS=64, and standard on other 32-bit/legacy targets), casting 2^32 - 1 to time_t wraps to -1, and "now - 1" reintroduces the exact bug this change fixes, just through the clamp instead of atol(). Added DAV_TIME_T_MAX, computed from sizeof(time_t) rather than assumed, and cap "now + sec" against it regardless of the platform's time_t width. Verified under both a 64-bit and a 32-bit (-m32) build: the 64-bit behavior is unchanged, and the 32-bit cast no longer wraps negative.

Originally reported as BZ 65788 by Eric Covener, citing a finding from GHSL team member @antonio-morales.

PR 65788


  • This PR does not report or address a security vulnerability (see SECURITY.md). The original report is a public Bugzilla issue open since 2022 with no CVE assigned; this fixes a correctness bug (a lock silently granted with the wrong, already-expired timeout) rather than any privilege or data exposure.
  • Code follows the httpd style guide.
  • New log messages (level debug or higher) use an empty APLOGNO() tag; numbers are assigned by a committer at merge time. (No new log messages.)
  • If the change is user-visible, a changes-entries/*.txt file is included, following the template in README.CHANGES.
  • Test cases based on pyhttpd are included in test/modules/dav (test_004_lock_timeout_overflow.py, modeled on the existing DavLockDB/Dav On pattern in test_001_propdb_ns_limit.py in the same directory). Could not run it in this sandbox: its fakeprefix's apxs resolves to the distro-packaged httpd/apxs rather than this trunk build, so compiling the harness's own mod_aptest helper produces a module-magic-number mismatch against the trunk httpd under test ("expected signature 41503234 but saw 41503235") -- a sandbox-specific apxs/module-ABI mismatch unrelated to this fix. Verified instead against a real build and curl, as described above, including the exact strings (Second-4294967295, Second-600, Second-0) the test asserts on.

@arturobernalg
arturobernalg force-pushed the fix/httpd-65788-dav-timeout-overflow branch from c3ed751 to 2b29637 Compare November 1, 2026 18:36
Comment thread changes-entries/65788.txt Outdated
@@ -0,0 +1,5 @@
*) mod_dav: Fix dav_get_timeout_string() using atol(), which has no

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too long and not user-readable, something like "Fix handling of invalid Timeout headers"?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done -- shortened to Fix handling of invalid or overflowing "Timeout:" request headers, which could silently grant a lock with an already-expired timeout instead of the RFC 4918 maximum. in 3d51c56.

Comment thread modules/dav/main/util.c Outdated
** - various utilities, repository-independent
*/

#include <errno.h>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be needed

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, confirmed -- apr_strings.h and apr_lib.h already pull in apr_errno.h, which includes errno.h. Removed in 3d51c56 (compiles clean).

Comment thread modules/dav/main/util.c
apr_int64_t sec;

val += 7;
/* ### We need to handle overflow better:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For trunk it might be worth fixing the code to use apr_time_t (and apr_time_now) properly for timeout values, even if still serialising as time_t in the databases, but otherwise this LGTM for a backportable fix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, that's worth doing but out of scope for this backportable fix -- happy to file a separate BZ/PR for the apr_time_t cleanup if that's useful. Left as-is here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went ahead and did this too - now uses apr_time_now()/apr_time_t for the "now" computation, only narrowing to time_t at the return, so the lock DBs are untouched. Re-tested all 4 cases against a real build, still correct. @notroj mind taking another look when you get a chance?

@arturobernalg
arturobernalg force-pushed the fix/httpd-65788-dav-timeout-overflow branch 2 times, most recently from 3d51c56 to 112a480 Compare November 3, 2026 11:39
…led Timeout value.

dav_get_timeout_string() parses a "Timeout: Second-<N>" header with
atol(), which has no overflow detection. A long enough digit string
overflows it, and the later "now + expires" addition overflows time_t
too, so the lock ends up stored with a nonsensical (already-expired)
timeout instead of the large-but-finite one the client asked for -- a
LOCK request that looks successful (200, a valid Lock-Token) silently
grants a lock that is already expired.

Confirmed with a real build: a LOCK request with
"Timeout: Second-99999999999999999999999999999999" got back
"<D:timeout>Second-0</D:timeout>" before the fix (atol() saturates to
LONG_MAX, and now + LONG_MAX overflows time_t to a large negative
value), and "<D:timeout>Second-4294967295</D:timeout>" after it -- the
RFC 4918 TimeOut maximum, matching the overflow handling the existing
code comment already called for but never implemented.

Fix: parse with apr_strtoi64() (overflow-checked via errno), and clamp
to 2^32 - 1 on overflow or on an in-range value above that maximum. A
negative value (e.g. "Second--5") clamps to 0. A value with no digits
at all (e.g. "Second-notanumber") clamps to 0 too, matching what
atol() already silently did for that input -- not Infinite, which
would grant more than before instead of less.

The RFC 4918 maximum does not always fit a time_t: on platforms where
time_t is 32 bits (still the glibc default for 32-bit Linux without
_TIME_BITS=64, and standard on other 32-bit/legacy targets), casting
2^32 - 1 to time_t wraps to -1, and "now - 1" reintroduces the exact
bug this change fixes, just through the clamp instead of atol(). Added
DAV_TIME_T_MAX, computed from sizeof(time_t) rather than assumed, and
cap "now + sec" against it regardless of the platform's time_t width.
Verified under both a 64-bit and a 32-bit (-m32) build: the 64-bit
behavior is unchanged, and the 32-bit cast no longer wraps negative.

Per review, the current-time source now uses apr_time_t/apr_time_now()
for the internal computation instead of time(NULL), narrowing to
time_t only at the return (locks are still serialized as time_t on
disk, unchanged). Re-verified all four cases (overflow, in-range,
non-numeric, absent header) against a real build after this change.

PR 65788
@arturobernalg
arturobernalg force-pushed the fix/httpd-65788-dav-timeout-overflow branch from 112a480 to b26dec7 Compare November 6, 2026 06:27
@arturobernalg
arturobernalg requested a review from notroj November 6, 2026 06:29

@notroj notroj left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, thanks

@arturobernalg
arturobernalg merged commit f33eb04 into apache:trunk Nov 6, 2026
42 of 43 checks passed
arturobernalg added a commit that referenced this pull request Nov 7, 2026
…eout value. (#960)

dav_get_timeout() parses a "Timeout: Second-<N>" header with atol(),
which has no overflow detection. A long enough digit string overflows
it, and the later "now + expires" addition overflows time_t too, so
the lock ends up stored with a nonsensical (already-expired) timeout
instead of the large-but-finite one the client asked for -- a LOCK
request that looks successful (200, a valid Lock-Token) silently
grants a lock that is already expired.

Fix: parse with apr_strtoi64() (overflow-checked via errno), and clamp
to 2^32 - 1 on overflow or on an in-range value above that maximum. A
negative value clamps to 0, and a value with no digits at all clamps
to 0 too, matching what atol() already silently did for that input.
Also guard against the RFC 4918 maximum itself not fitting a 32-bit
time_t, capping "now + sec" against DAV_TIME_T_MAX.

This backports the trunk fix (f33eb04, PR #920) to 2.4.x. Trunk had
already split this into dav_get_timeout()/dav_get_timeout_string();
2.4.x has no such split, so the fix is applied directly in the single
dav_get_timeout() function instead.

PR 65788
@ylavic

ylavic commented Nov 27, 2026

Copy link
Copy Markdown
Member

@arturobernalg test_dav_004_03 failed once in CI (UBSan job of #1105) with "timeout: Second-1" instead of "Second-0".
Claude thinks it's a clock mismatch this change introduced: dav_get_timeout_string() now computes the expiry from apr_time_sec(apr_time_now()), while dav_lock_get_activelock() still prints the remaining time as lock->timeout - time(NULL). On Linux, time(NULL) and gettimeofday() are not the same clocks (can lag by a few ms), so right after a second boundary a lock expiring "now + 0" prints as Second-1 (and test_dav_004_02 could likewise see Second-601).

I think mod_dav needs one clock for lock timeouts: either back to time(NULL) in dav_get_timeout_string() (keeping the 64-bit arithmetic and the overflow capping), a one-liner, or apr_time_sec(apr_time_now()) in the other places using time(NULL) (fs/lock.c's expiry check, mod_dav.c's DAVLockTimeout, ms_wdv.c and util_lock.c's outputs).
Want me to send either, or will you?

@arturobernalg

Copy link
Copy Markdown
Member Author

Good catch, thanks, that's my change (2.4.x kept time(NULL), so it's trunk only). I opened #1107: back to time(NULL) in dav_get_timeout_string(), keeping the overflow capping, and the tests now allow a second ticking between the LOCK and its response. A real apr_time_t conversion (dav_lock.timeout and the API are time_t) can be a separate PR.

arturobernalg added a commit that referenced this pull request Nov 27, 2026
mod_dav: Use one clock for lock timeouts.

f33eb04 (#920) took "now" for the lock expiry in
dav_get_timeout_string() from apr_time_now(), while the expiry check
and the remaining time in the LOCK response still use time(NULL).  On
Linux time(NULL) can lag gettimeofday() by up to a clock tick, so a
LOCK handled right after a second boundary got an expiry one second
later than asked: a "Second-0" lock reported "Second-1" or was still
held (423).

Go back to time(NULL), as on 2.4.x, keeping the overflow capping.
Let test_dav_004_01/02 accept one second less, since a second may
tick between the LOCK and its response, and add a test locking
repeatedly across second boundaries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants