This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
Restrict malloc
of uninitialized memory containing checked pointers?
#457
Labels
This issue was copied from checkedc/checkedc#458
Section 2.6 of the Checked C specification states:
I know this is a known issue, but it's not ideal to leave random soundness holes lying around like this and rely on the programmer to notice whenever their code is doing something dangerous and fix it. It would easy to overlook a
malloc
call when porting a large C program to Checked C. Is there any way thatmalloc
of checked pointers could be restricted better?Some potential approaches:
Just remove the bounds-safe declaration for
malloc
, forcing checked regions to usecalloc
instead. That would be disruptive to existing Checked C code, but if we care enough about soundness, maybe the disruption is justified.Place a constraint on the declaration of
malloc
that the type parameterT
cannot contain checked pointers (the same condition the compiler checks for uninitialized stack-allocated variables). That has the advantage of only breaking existing code that is actually unsafe, but it requires a new language feature that probably wouldn't be used for anything exceptmalloc
, which probably isn't worth it.Change the declaration of
malloc
to:void *malloc(size_t size) : itype(_Array_ptr<void>) byte_count(size);
and let the caller do an implicit conversion from
_Array_ptr<void> : byte_count(size)
to_Array_ptr<T> : byte_count(size)
. This takes advantage of the compiler's existing logic that allows such a conversion only ifT
does not contain checked pointers. This would break existing calls tomalloc
until the generic type arguments are removed, unless we leave a dummy_Itype_for_any(T)
on the declaration ofmalloc
, which would buy us compatibility in the short term but be silly in the long term.In all the above approaches, the compatibility issues mainly affect existing Checked C code. I don't think any of the approaches are materially worse for porting existing plain C code than the status quo, under which the user has to modify
malloc
calls to add a type argument.In combination with any of the approaches for restricting traditional
malloc
calls that don't initialize the memory, one thing we could offer programmers to reduce disruption is a function (which I'll callzalloc
for the purpose of this discussion) that is defined incheckedc_extensions.h
and has the same signature asmalloc
(unlikecalloc
, which takes an extra parameter) but zero-initializes the memory. Then programmers would have the option to#define malloc zalloc
, and their Checked C code should work as before.realloc
poses the same soundness problem asmalloc
, and the same options are available for restricting it. Unfortunately, providing a safe alternative torealloc
(calledzrealloc
in this discussion) is more difficult: in order to zero-initialize only the added memory, we need to know the old size of the memory block. The C library doesn't provide a standard way to query the old size, butzrealloc
could probably take it as an extra parameter, since in Checked C, the caller will generally have this information anyway as part of the bounds declaration of the old pointer. So the declaration and implementation ofzrealloc
incheckedc_extensions.h
could look something like this:Note the
_Where
clause: we mustn't allow the user to zero out part of aT
object because that could create an invalid pointer, a struct where a pointer member is inconsistent with its bounds member, etc. The compiler currently doesn't supportsizeof(T)
in a_Where
clause; that's tracked as part of #454.(Side note: For (1), as an alternative to completely removing the bounds-safe declaration for
malloc
, I tried just marking it_Unchecked
, thinking that would disallow calls from checked regions while still providing bounds information to help catch bounds-related mistakes on calls from unchecked regions. But in my tests with the current compiler, marking a function declaration_Unchecked
does not prevent calls from checked regions. Is that a separate bug that I should report?)The text was updated successfully, but these errors were encountered: