Repository navigation
[security/ise_memory_safety/elf] Constrain the lifetime of ELF bridge accessors - #199
Open
copybara-service[bot] wants to merge 1 commit into
Open
copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
… accessors
- Ref-qualify `SectionData::GetData()`, the `NoteAny`, `NoteGnuBuildId`, and
`SymbolRequirement` accessors, and the section/symbol/hash/dyn table
accessors as `const&` with `ABSL_ATTRIBUTE_LIFETIME_BOUND`, and delete the
`const&&` overloads.
- Document non-NUL-termination of the byte-view accessors.
- Add a single file-level section stating what the guards enforce and where
they stop, rather than repeating a partial version of it at twenty accessors.
Enforced: an initialization taken off a temporary is rejected
(`-Werror,-Wdangling`), including one reached through
`absl::StatusOr::operator->`, which absl annotates.
Not enforced, now written down:
- assignment into an already-declared variable;
- escape from the scope of the named owner;
- laundering a view through an intervening unannotated call;
- reaching into a `std::optional` with `operator->`.
The last of these is specific to this library and was verified against the
compiler rather than reasoned about. Several entry points here return
`absl::StatusOr<std::optional<T>>`, and `std::optional::operator->` is the one
optional accessor that is neither ref-qualified nor lifetime-annotated, so it
breaks the chain. All three of these compile silently and dangle:
const StringTable& st = (*elf.GetSymbolTable())->GetStringTable();
std::string_view f = (*version_table.GetRequirement(i))->GetFile();
const Symbol& sym = (*hash_table.Find(name))->GetSymbol();
Each of the seven affected declarations now carries a one-line pointer to the
file-level note. Closing this needs an upstream annotation on `std::optional`,
not a change here.
Deliberately NOT changed: the wrapper-returning factories (`GetSectionData`,
`GetSectionDataAsStrTab`, `GetSectionDataAsRels`/`Relas`/`Notes`,
`NoteTable::Get`, `SymbolVersionTable::GetRequirement`, ...) do not carry
`ABSL_ATTRIBUTE_LIFETIME_BOUND`, and should not. The objects they return own
their storage -- `SectionData` holds a `std::string`, and `NoteAny`,
`NoteGnuBuildId`, and `SymbolRequirement` hold the Rust-owned object -- so they
do not borrow the `ElfBytes` or `ElfStream` they came from. Annotating them
would bind the result to the wrong object and produce false positives. Likewise
`ElfBytes::Parse()` needs no annotation on its parameter: the Rust side copies
the input.
`NoteAny`, `NoteGnuBuildId`, and `SymbolRequirement` stay zero-copy: their
accessors compute a view over the held object on demand. Those accessors return
`std::string_view`, which carries its own length, so NUL-termination only
matters to a caller who passes `.data()` to a C string API, which is
out-of-contract use. Copying every field into owned `std::string` members to
guarantee it would cost two unconditional heap allocations per note and per
symbol requirement on a path that walks every note and symbol in a binary,
which is not a reasonable price. A caller who genuinely needs a C string can
write `std::string(note.GetName())` at the one call site that needs it.
PiperOrigin-RevId: 975203634
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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.
[security/ise_memory_safety/elf] Constrain the lifetime of ELF bridge accessors
SectionData::GetData(), theNoteAny,NoteGnuBuildId, andSymbolRequirementaccessors, and the section/symbol/hash/dyn tableaccessors as
const&withABSL_ATTRIBUTE_LIFETIME_BOUND, and delete theconst&&overloads.they stop, rather than repeating a partial version of it at twenty accessors.
Enforced: an initialization taken off a temporary is rejected
(
-Werror,-Wdangling), including one reached throughabsl::StatusOr::operator->, which absl annotates.Not enforced, now written down:
std::optionalwithoperator->.The last of these is specific to this library and was verified against the
compiler rather than reasoned about. Several entry points here return
absl::StatusOr<std::optional<T>>, andstd::optional::operator->is the oneoptional accessor that is neither ref-qualified nor lifetime-annotated, so it
breaks the chain. All three of these compile silently and dangle:
Each of the seven affected declarations now carries a one-line pointer to the
file-level note. Closing this needs an upstream annotation on
std::optional,not a change here.
Deliberately NOT changed: the wrapper-returning factories (
GetSectionData,GetSectionDataAsStrTab,GetSectionDataAsRels/Relas/Notes,NoteTable::Get,SymbolVersionTable::GetRequirement, ...) do not carryABSL_ATTRIBUTE_LIFETIME_BOUND, and should not. The objects they return owntheir storage --
SectionDataholds astd::string, andNoteAny,NoteGnuBuildId, andSymbolRequirementhold the Rust-owned object -- so theydo not borrow the
ElfBytesorElfStreamthey came from. Annotating themwould bind the result to the wrong object and produce false positives. Likewise
ElfBytes::Parse()needs no annotation on its parameter: the Rust side copiesthe input.
NoteAny,NoteGnuBuildId, andSymbolRequirementstay zero-copy: theiraccessors compute a view over the held object on demand. Those accessors return
std::string_view, which carries its own length, so NUL-termination onlymatters to a caller who passes
.data()to a C string API, which isout-of-contract use. Copying every field into owned
std::stringmembers toguarantee it would cost two unconditional heap allocations per note and per
symbol requirement on a path that walks every note and symbol in a binary,
which is not a reasonable price. A caller who genuinely needs a C string can
write
std::string(note.GetName())at the one call site that needs it.