From 612562bc1ea38f1708b044e7a079c47a05b1291d Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Thu, 5 May 2022 20:54:11 +0200 Subject: [PATCH] Fix: index entry label not being zero-terminated with corrupt input --- ChangeLog | 1 + src/index.c | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3f93455..701c097 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ +2022-05-05: Fix: index entry label not being zero-terminated with corrupt input 2022-05-03: Fix boundary checking error in markup search, that could cause buffer over-read with corrupt input 2022-05-02: Fix typo in macro name 2022-04-27: Fix undefined behavior when passing null to strdup diff --git a/src/index.c b/src/index.c index ca83675..109231c 100644 --- a/src/index.c +++ b/src/index.c @@ -29,11 +29,11 @@ /** @brief Read index entry label from buffer pointing at index record data - @param[in,out] output Output string + @param[in,out] output Output buffer (INDX_LABEL_SIZEMAX + 1 bytes) @param[in,out] buf MOBIBuffer structure, offset pointing at index entry label @param[in] length Number of bytes to be read @param[in] has_ligatures Decode ligatures if true - @return Size of read label + @return Length of output string (without null terminator), on error buf->error set to MOBI_RET status */ size_t mobi_indx_get_label(unsigned char *output, MOBIBuffer *buf, const size_t length, const size_t has_ligatures) { if (!output) { @@ -248,9 +248,9 @@ uint16_t mobi_ordt_lookup(const MOBIOrdt *ordt, const uint16_t offset) { @param[in] ordt MOBIOrdt structure (ORDT data and metadata) @param[in,out] buf MOBIBuffer structure with input string - @param[in,out] output Output buffer (INDX_LABEL_SIZEMAX bytes) + @param[in,out] output Output buffer (INDX_LABEL_SIZEMAX + 1 bytes) @param[in] length Length of input string contained in buf - @return Number of bytes read + @return Length of output string (without null terminator) */ size_t mobi_getstring_ordt(const MOBIOrdt *ordt, MOBIBuffer *buf, unsigned char *output, size_t length) { size_t i = 0; @@ -362,12 +362,16 @@ static MOBI_RET mobi_parse_index_entry(MOBIIndx *indx, const MOBIIdxt idxt, cons debug_print("Label length too long: %zu\n", label_length); return MOBI_DATA_CORRUPT; } - char text[INDX_LABEL_SIZEMAX]; + char text[INDX_LABEL_SIZEMAX + 1]; /* FIXME: what is ORDT1 for? */ if (ordt->ordt2) { label_length = mobi_getstring_ordt(ordt, buf, (unsigned char*) text, label_length); } else { label_length = mobi_indx_get_label((unsigned char*) text, buf, label_length, indx->ligt_entries_count); + if (buf->error != MOBI_SUCCESS) { + debug_print("Buffer error reading label: %d\n", buf->error); + return MOBI_DATA_CORRUPT; + } } indx->entries[entry_number].label = malloc(label_length + 1); if (indx->entries[entry_number].label == NULL) {