-
Type:
Problem report
-
Resolution: Unresolved
-
Priority:
Trivial
-
None
-
Affects Version/s: 7.0.32
-
Component/s: None
-
None
Affected: zabbix_sender, libs/zbxjson. Checked in the source of release/7.0
Other branches were not checked.
Steps to reproduce
The bug is found by code reading, not observed at runtime. Suggested reproduction:
1. Prepare an input file with enough items that the resulting JSON request is larger than
ZBX_JSON_STAT_BUF_LEN (4096 bytes), e.g. ~200 lines:
<host> <key> <value>
2. In the configuration file, specify two or more addresses in ServerActive. The servers may be
dummy listeners:
ServerActive=127.0.0.1:10051,127.0.0.2:10051
3. Run the sender with this configuration under valgrind (or an ASan/MSan build):
valgrind zabbix_sender -c zabbix_sender.conf -i data.txt
The data is cloned only for the second and further destinations. With a single address in
ServerActive the clone is not used.
Result
zbx_json_clone() (src/libs/zbxjson/json.c) copies the buffer with
memcpy(dst->buffer, src->buffer, src->buffer_size). buffer_size does not include the terminating
'\0' (see the +1 /'\0'/ in zbx_json_addstring() and similar functions), and dst->buffer comes from
zbx_malloc(), which does not zero memory. So dst->buffer[buffer_size] is uninitialized.
The buffer is only cloned when the source JSON no longer fits in the static buffer
(src->buffer != src->buf_stat), i.e. when it is larger than 4096 bytes. In
perform_data_sending() (src/zabbix_sender/zabbix_sender.c) the clone is made for every destination
except the first. It is then used as a C string:
- connect_callback() returns json->buffer, which goes to zbx_tcp_send(), where the length is
taken with strlen(); - the same buffer is passed to zabbix_log() with "%s".
Possible effects, from most to least likely:
- the request to the second and further destinations has garbage after the JSON, so the server
rejects it and the data for that destination is lost, while the first destination works; - heap contents of the sender process are sent to the server (information disclosure);
- strlen() runs past the end of the heap block and the process crashes;
- garbage in the debug log.
The bug can stay unnoticed for a long time, because fresh heap memory is usually zero-filled and a
'\0' is often found by chance.
Expected
The clone must be a valid NUL-terminated copy of the source. See the attached patch:
- memcpy(dst->buffer, src->buffer, src->buffer_size);
+ memcpy(dst->buffer, src->buffer, src->buffer_size + 1/'\0'/);
With the patch, valgrind reports no uninitialized reads for the steps above, and all destinations
receive an identical, valid JSON.