Skip to content

Commit 4678c74

Browse files
authored
Merge commit from fork
Fix buffer overflows in model loading code
2 parents d7c0c8e + 2a3c037 commit 4678c74

16 files changed

Lines changed: 361 additions & 168 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ test/regression/*.log
2525
/Testing
2626
_test_solve.out
2727
test/unit/**/*.out
28+
.cache

src/feat/feat.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,8 @@ feat_init(char const *type, cmn_type_t cmn, int32 varnorm,
837837
i = 0;
838838
fcb->out_dim = 0;
839839
fcb->cepsize = 0;
840+
/* NOTE: This looks unsafe but isn't, because wd is allocated
841+
* as the same size as mtype, and strp is a pointer into mtype. */
840842
while (sscanf(strp, "%s%n", wd, &l) == 1) {
841843
strp += l;
842844
if ((i >= fcb->n_stream)

src/lm/lm_trie.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@
88
* are met:
99
*
1010
* 1. Redistributions of source code must retain the above copyright
11-
* notice, this list of conditions and the following disclaimer.
11+
* notice, this list of conditions and the following disclaimer.
1212
*
1313
* 2. Redistributions in binary form must reproduce the above copyright
1414
* notice, this list of conditions and the following disclaimer in
1515
* the documentation and/or other materials provided with the
1616
* distribution.
1717
*
18-
* This work was supported in part by funding from the Defense Advanced
19-
* Research Projects Agency and the National Science Foundation of the
18+
* This work was supported in part by funding from the Defense Advanced
19+
* Research Projects Agency and the National Science Foundation of the
2020
* United States of America, and the CMU Sphinx Speech Consortium.
2121
*
22-
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23-
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22+
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23+
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
2424
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2525
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
2626
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27-
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28-
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29-
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30-
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31-
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3232
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3333
*
3434
* ====================================================================
@@ -59,9 +59,9 @@ static uint32
5959
base_size(uint32 entries, uint32 max_vocab, uint8 remaining_bits)
6060
{
6161
uint8 total_bits = bitarr_required_bits(max_vocab) + remaining_bits;
62-
/* Extra entry for next pointer at the end.
62+
/* Extra entry for next pointer at the end.
6363
* +7 then / 8 to round up bits and convert to bytes
64-
* +sizeof(uint64) so that ReadInt57 etc don't go segfault.
64+
* +sizeof(uint64) so that ReadInt57 etc don't go segfault.
6565
* Note that this waste is O(order), not O(number of ngrams).*/
6666
return ((1 + entries) * total_bits + 7) / 8 + sizeof(uint64);
6767
}
@@ -192,7 +192,7 @@ lm_trie_fix_counts(ngram_raw_t ** raw_ngrams, uint32 * counts,
192192
memcpy(fixed_counts, counts, order * sizeof(*fixed_counts));
193193
for (i = 2; i <= order; i++) {
194194
ngram_raw_t *tmp_ngram;
195-
195+
196196
if (counts[i - 1] <= 0)
197197
continue;
198198

@@ -270,7 +270,7 @@ recursive_insert(lm_trie_t * trie, ngram_raw_t ** raw_ngrams,
270270
(uint32 *) ckd_calloc(order - 1, sizeof(*raw_ngrams_ptr));
271271
for (i = 2; i <= order; ++i) {
272272
ngram_raw_t *tmp_ngram;
273-
273+
274274
if (counts[i - 1] <= 0)
275275
continue;
276276

@@ -517,7 +517,7 @@ lm_trie_build(lm_trie_t * trie, ngram_raw_t ** raw_ngrams, uint32 * counts, uint
517517

518518
lm_trie_fix_counts(raw_ngrams, counts, out_counts, order);
519519
lm_trie_alloc_ngram(trie, out_counts, order);
520-
520+
521521
if (order > 1)
522522
E_INFO("Training quantizer\n");
523523
for (i = 2; i < order; i++) {

src/lm/ngram_model.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ ngram_model_init(ngram_model_t * base,
210210
ngram_funcs_t * funcs,
211211
logmath_t * lmath, int32 n, int32 n_unigram)
212212
{
213+
if (n == 0) {
214+
E_ERROR("Invalid N-Gram order %d\n", n);
215+
return -1;
216+
}
213217
base->refcount = 1;
214218
base->funcs = funcs;
215219
base->n = n;
@@ -279,8 +283,10 @@ ngram_model_free(ngram_model_t * model)
279283
(*model->funcs->free) (model);
280284
if (model->writable) {
281285
/* Free all words. */
282-
for (i = 0; i < model->n_words; ++i) {
283-
ckd_free(model->word_str[i]);
286+
if (model->word_str) {
287+
for (i = 0; i < model->n_words; ++i) {
288+
ckd_free(model->word_str[i]);
289+
}
284290
}
285291
}
286292
else {

0 commit comments

Comments
 (0)