Fix null pointer dereferences when allocation fails#372
Open
aaranmcguire wants to merge 1 commit intoWizardMac:devfrom
Open
Fix null pointer dereferences when allocation fails#372aaranmcguire wants to merge 1 commit intoWizardMac:devfrom
aaranmcguire wants to merge 1 commit intoWizardMac:devfrom
Conversation
Six locations immediately dereference the result of calloc/malloc without checking whether the allocation succeeded. On memory-constrained systems or with crafted files, these are reliable crash bugs. - stata/readstat_dta_read.c: dta_init_variable() — calloc result used unconditionally; adds out_retval parameter and null guard - sas/readstat_sas7bdat_read.c: sas7bdat_parse_subheader_rdc() — malloc for RDC decompression buffer unchecked - sas/readstat_sas7bdat_read.c: readstat_parse_sas7bdat() — both ctx and hinfo calloc results used before any null check - readstat_writer.c: readstat_begin_writing_data() — writer->row malloc unchecked; every subsequent insert call would crash - readstat_writer.c: readstat_string_ref_init() — calloc for strL ref unchecked before dereferencing - sas/readstat_xport_read.c: xport_construct_format() — malloc for format conversion buffer unchecked
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR 1: Fix null pointer dereferences when allocation fails
Summary
Six locations across the parser and writer immediately dereference the result
of
calloc/mallocwithout checking whether the allocation succeeded. Onany memory-constrained system, or with a crafted file large enough to exhaust
address space, these become crash bugs.
Every site below is exercised on normal, well-formed files — the allocations
are not guarded by any prior size check that would prevent them from reaching
the failing path.
1.
stata/readstat_dta_read.c—dta_init_variableVulnerable code
Why it triggers
dta_init_variableis called once for every column in the file(
dta_handle_variables, iteratedctx->var_counttimes). A DTA file with afew thousand columns, parsed on a system with fragmented heap or address-space
limits (e.g. a 32-bit process, or a container with
ulimit -v), will failthe
callocand crash on the very next line.Reproduction
Fix
Call site updated to pass
&retvaland check for NULL return beforedereferencing
ctx->variables[i].2.
sas/readstat_sas7bdat_read.c—sas7bdat_parse_subheader_rdcVulnerable code
Why it triggers
ctx->row_lengthcomes from the SAS7BDAT page header and can be up to 64 KBper row. The RDC decompressor is called for every compressed data page. On
memory-constrained systems, or with a crafted file that declares an extreme
row_length,mallocreturns NULL and the loop crashes on the first write.Reproduction
Fix
3.
sas/readstat_sas7bdat_read.c—readstat_parse_sas7bdatVulnerable code
Why it triggers
These are the very first allocations made when parsing any SAS7BDAT file.
sizeof(sas7bdat_ctx_t)is ~600 bytes. On any system where the allocatorreturns NULL (OOM, address-space exhaustion), the crash happens before a
single byte of the file is read.
Fix
4.
readstat_writer.c—readstat_begin_writing_dataVulnerable code
Why it triggers
writer->rowis used by everyreadstat_insert_*_valuecall. Ifmallocfails here,
writer->rowis NULL. The nextmemset(writer->row, ...)orany insert call crashes.
row_lenis the sum of all variable widths — withmany wide string variables it can reach tens of kilobytes.
Fix
5.
readstat_writer.c—readstat_string_ref_initVulnerable code
Why it triggers
Called for every unique long string in a Stata DTA strL dataset. With many
unique long strings, or on a memory-limited system,
calloccan fail.Fix
6.
sas/readstat_xport_read.c—xport_construct_formatVulnerable code
Why it triggers
src_lenissizeof(namestr.nform)— a compile-time constant (8 bytes),so the allocation is always small. However, the missing null check means any
OOM condition causes
readstat_convertto write through a NULL pointer.Fix