config: heap corruption when a vhost inherits route/group entries and more are added
Description
ocserv crashes with heap corruption (realloc(): invalid next size, SIGABRT) when a named virtual host inherits multi-line configuration entries (e.g. route, select-group) from the default vhost and additional entries of the same kind are added in the vhost section.
Affected: 1.5.0 and 1.6.0 (code unchanged between the two).
Mechanism
When a named vhost has no auth of its own (or otherwise inherits static config), cfg_copy_from_default() rebuilds the vhost's config from the protobuf snapshot. The repeated arrays (routes, group_list, ...) are allocated exactly to the number of inherited elements.
The insertion helpers, however, assume the arrays were allocated with DEFAULT_CONFIG_ENTRIES (96) capacity:
_add_multi_line_val()(src/config.c:2643 in 1.6.0) only reallocates when*num >= _max - 1(i.e. 95). If the array was allocated by the protobuf path with, say, 5 elements, writes at indices 5..94 go out of bounds.READ_MULTI_BRACKET_LINE(src/config.c:115 in 1.6.0) has no reallocation at all: it writesvarname[num]/varname2[num]as long asnum < DEFAULT_CONFIG_ENTRIES, again assuming 96 slots exist.
Out-of-bounds writes corrupt the talloc headers of the adjacent chunk; the crash surfaces later as realloc(): invalid next size / free(): invalid next size, or as a segfault, depending on layout.
Reproducer
With a config where the default section and a vhost together hold enough route entries to cross the actually-allocated capacity (with an inherited base the threshold is far below 96; from a fresh default section it is 96 total: 95 entries OK, 96 crash):
`# default section route = 10.0.0.0/8 ... (repeat)
[vhost:example.com] route = 192.168.1.0/24 ... (repeat) `
$ ocserv -t -c ocserv.conf realloc(): invalid next size Aborted (core dumped) # exit 134
The same corruption is reachable via select-group/config-per-group paths that go through READ_MULTI_BRACKET_LINE.
Suggested fix
Reallocate to *num + 2 on every insertion in both _add_multi_line_val() and READ_MULTI_BRACKET_LINE, instead of assuming DEFAULT_CONFIG_ENTRIES capacity for arrays that may have been allocated smaller by the protobuf inheritance path. We carry such a patch locally and it survives both reproducer variants and production configs.