Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIMD-0189: SBPF stricter ELF headers #189

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Lichtso
Copy link
Contributor

@Lichtso Lichtso commented Oct 23, 2024

No description provided.

@@ -0,0 +1,131 @@
---
simd: '0187'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update the SIMD number (and PR title) to match the PR number, per https://github.com/solana-foundation/solana-improvement-documents/blob/main/proposals/0001-simd-process.md#draft

@Lichtso Lichtso changed the title SIMD-0187: SBPF stricter ELF headers SIMD-0189: SBPF stricter ELF headers Oct 31, 2024
@Lichtso Lichtso force-pushed the sbpf-stricter-elf-headers branch from eb17010 to 51bec42 Compare October 31, 2024 21:38
- `p_memsz` must fit in 32 bits / be less than `1 << 32`
- `p_align` is ignored

If any check fails `ElfParserError::InvalidProgramHeader` must be thrown.
Copy link

@topointon-jump topointon-jump Nov 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind specifying where in the program lifecycle this will be thrown? (i.e. will these new checks be enforced at deployment time? when loading into the program cache for execution? both?)

What is the plan for currently deployed programs?

Copy link

@topointon-jump topointon-jump Nov 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is this SIMD associated with an SBPF version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is associated with a SBPF version (see title), though the specific one is not decided yet.
And these checks will all happen at load time (deployment or feature set change), not at runtime.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we specify in the SIMD where the checks will be performed, and what happens if they fail?

Comment on lines +72 to +73
| stack | PT_GNU_STACK | PF_R, PF_W | 2 << 32 |
| heap | PT_LOAD | PF_R, PF_W | 3 << 32 |

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we allow these headers? These regions are configured independently of the ELF. Do we allow these headers just so that we can reduce LLVM modifications? Just curious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is so that the memory regions are correctly reflected in the ELF header. This makes it easier for tooling to detect issues (such as overlap in section allocation) and could simplify the validator implementation in the future.

Also, with the upcoming CPI syscall restrictions (necessary for direct mapping) we will limit pointers provided by the user to come from the stack and heap. Thus their layout will be further solidified.

Copy link
Contributor

@deanmlittle deanmlittle Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all programs make use of stack/heap/symbols. A noop program, for example, should be just 128 bytes (ELF header + 1 PT_LOAD PF_X + 1 exit insn). Enforcing 5 headers, 4 of which aren't even used, would bloat it to 352 bytes - even larger than the current mainnet-beta minimum of 336 bytes.

On the other hand, it's great to be able to handle for memory layouts that diverge from the default of a specific version without having to bump versions should the need arise. As such, I think it would be better not to strictly enforce these headers, but to instead opt for a solution like:

  1. First header is always PT_LOAD PF_X. This is the only mandatory header.
  2. For anything with .rodata, add a PT_LOAD PF_R header.
  3. For anything with symbols, add PT_NONE 0x00.
  4. For anything with non-standard stack layout for its version, add PT_GNU_STACK PF_R | PF_W
  5. For anything with non-standard heap layout for its version, add PT_LOAD PF_R | PF_W

This solution would make the minimum program use 1 program header, the average program use 3 program headers, and if we ever had memory layout issues, they could be solvable for the small price of 1 or 2 extra program headers.


If any check fails `ElfParserError::InvalidProgramHeader` must be thrown.

### Dynamic symbol table
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you choose the dynamic symbol table entry SHT_DYNAMIC and not the symbol table elf entry SHT_SYMTAB?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

5 participants