Skip to content

[security/ise_memory_safety/elf] Constrain the lifetime of ELF bridge accessors - #199

Open
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_975203634
Open

copybara-service[bot] wants to merge 1 commit into
mainfrom
test_975203634

Conversation

@copybara-service

Copy link
Copy Markdown

[security/ise_memory_safety/elf] Constrain the lifetime of ELF bridge 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.

… 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
@google-cla

google-cla Bot commented Oct 22, 2026

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants