Skip to content

pathbuf: reserve room for the separator in pathbuf_append_path_len - #318

Merged
hyperrealm merged 1 commit into
hyperrealm:masterfrom
affan-arch:pathbuf-separator-capacity
Nov 29, 2026
Merged

hyperrealm merged 1 commit into
hyperrealm:masterfrom
affan-arch:pathbuf-separator-capacity

Conversation

@affan-arch

Copy link
Copy Markdown
Contributor

pathbuf_append_path_len() reserves the byte for the '/' separator only on the branch that doesn't write one, so an append onto a non-empty buffer whose length + len + 1 is already exactly the 128-byte-rounded capacity puts the terminating NUL one byte past the allocation; example4's include_func() feeds it the directory part of the @include path and the names readdir() returns for that directory, so a config file with @include "<120 characters>/*.cfg" and a 7-character file name in that directory gives asan a heap-buffer-overflow WRITE of size 1 at pathbuf.c:74 under config_read_file().

pathbuf_append_path_len() asks pathbuf_ensure_capacity() for len + 1
bytes when it is not going to insert a '/' and for only len when it is,
which is the wrong way round. ensure_capacity() reserves
length + len + 1 and rounds that up to a 128-byte block, so when a
separator is written and length + len + 1 is already exactly the
capacity, nothing is reallocated and the terminating NUL lands one byte
past the allocation.

Both appends in example4.c's include_func() are fed from outside the
program: the directory part of the @include path in the configuration
file, and the names readdir() returns for that directory. A config file
containing

  @include "<120 characters>/*.cfg"

together with a 7-character file name in that directory makes
config_read_file() abort under -fsanitize=address with a
heap-buffer-overflow WRITE of size 1 at pathbuf.c:74.

Reserve the byte on the branch that writes it. lib/strbuf.c, which this
helper is modelled on, already counts it this way.
@hyperrealm
hyperrealm merged commit 3375200 into hyperrealm:master Nov 29, 2026
5 checks passed
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.

2 participants