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

Determine elf arch by e_machine and bits #2177

Merged
merged 5 commits into from
Apr 9, 2023

Conversation

Xeonacid
Copy link
Contributor

@Xeonacid Xeonacid commented Mar 30, 2023

Pwntools Pull Request

Thanks for contributing to Pwntools! Take a moment to look at CONTRIBUTING.md to make sure you're familiar with Pwntools development.

In context module, riscv32 and riscv64 need to be assigned arch separately.
In elf module, riscv32 and riscv64 share the same e_machine, EM_RISCV, and we used e_machine to determine arch directly, which does not work for RISC-V (and MIPS).
Change to determining elf arch by e_machine and bits.

Separate 32/64 for riscv and mips.

Testing

Pull Requests that introduce new code should try to add doctests for that code. See TESTING.md for more information.

Target Branch

Depending on what the PR is for, it needs to target a different branch.

You can always change the branch after you create the PR if it's against the wrong branch.

Branch Type of PR
dev New features, and enhancements
dev Documentation fixes and new tests
stable Bug fixes that affect the current stable branch
beta Bug fixes that affect the current beta branch, but not stable
dev Bug fixes for code that has never been released

Changelog

After creating your Pull Request, please add and push a commit that updates the changelog for the appropriate branch.
You can look at the existing changelog for examples of how to do this.

@Xeonacid Xeonacid changed the base branch from dev to stable March 30, 2023 13:24
@Xeonacid Xeonacid changed the base branch from stable to dev March 30, 2023 13:25
@Xeonacid Xeonacid changed the base branch from dev to stable March 30, 2023 13:28
@Xeonacid
Copy link
Contributor Author

CHANGELOG.md says This changelog only includes added major features and changes. Bugfixes and minor changes are omitted.. But CI fails if I don't add changelog. Should I do this?

Copy link
Member

@Arusekk Arusekk left a comment

Choose a reason for hiding this comment

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

Great stuff! I really like what you have done here. One minor thing though.

pwnlib/elf/plt.py Outdated Show resolved Hide resolved
@Arusekk
Copy link
Member

Arusekk commented Mar 31, 2023

Please see why the ROP tests suddenly fail (I think this is something about how your changes influence GOT/PLT resolution).

@Arusekk Arusekk enabled auto-merge (squash) March 31, 2023 10:04
auto-merge was automatically disabled March 31, 2023 13:08

Head branch was pushed to by a user without write access

@Xeonacid
Copy link
Contributor Author

Xeonacid commented Mar 31, 2023

Please see why the ROP tests suddenly fail (I think this is something about how your changes influence GOT/PLT resolution).

Unfortunately, I can't reproduce any of the failures on my machine. Could you try a reproduction please?

@peace-maker
Copy link
Member

Cool stuff, used it already during hack-a-sat. I have a follow up patch ready which fixes shellcode assembly and debugging.
All other architectures are named without the 32 suffix like mips/mips64, powerpc/powerpc64, and sparc/sparc64. Is it intentional to break this pattern in riscv32/riscv64?

@Xeonacid
Copy link
Contributor Author

Xeonacid commented Apr 6, 2023

All other architectures are named without the 32 suffix like mips/mips64, powerpc/powerpc64, and sparc/sparc64. Is it intentional to break this pattern in riscv32/riscv64?

The name is picked from gcc target triple or uname -m. Nobody calls the 32bit variant riscv but riscv32.
Those arches which don't have a -32 postfix should be a historical reason when there is no 64bit variant I think. Newer arches like riscv and loongarch have the postfix.

@Arusekk
Copy link
Member

Arusekk commented Apr 7, 2023

Alright, this is definitely a new feature, not a bugfix, so it should go in dev.
RISC-V support in Unicorn appeared in 2.0.0, which does not support py2 because nobody checked it (see unicorn-engine/unicorn#1822).

@Arusekk Arusekk changed the base branch from stable to dev April 7, 2023 22:28
Xeonacid and others added 3 commits April 8, 2023 11:09
Separate 32/64 for riscv and mips
Trim some trailing space
Co-authored-by: Arusekk <arek_koz@o2.pl>
@Arusekk
Copy link
Member

Arusekk commented Apr 8, 2023

Also, is rv64 ISA really different from rv32? Or should they just be both treated as riscv by pwntools code? I think they can have common shellcodes in the future etc, and bits is used anyway in all places to distinguish architectures.

@Xeonacid
Copy link
Contributor Author

Xeonacid commented Apr 8, 2023

I think they can have common shellcodes in the future etc

They may share the same shellcode for some commands, but I don't think all would work.
For example, riscv32 use lw/sw (32 bit) for loading/storing, while riscv64 is likely to use ld/sd (64 bit).

Comment on lines -407 to +408
'riscv': little_32,
'riscv32': little_32,
'riscv64': little_64,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, is rv64 ISA really different from rv32? Or should they just be both treated as riscv by pwntools code? I think they can have common shellcodes in the future etc, and bits is used anyway in all places to distinguish architectures.

And what should we do here? If hard-coded riscv as 32 or 64, ppl have to set context.bits manually to work for the other one.

@Arusekk
Copy link
Member

Arusekk commented Apr 8, 2023 via email

@Xeonacid
Copy link
Contributor Author

Xeonacid commented Apr 8, 2023

Just as they need to do anyway with e.g. mips endianness. The recommended way to set everything automatically is using context.binary anyway.

Or we can add aliases rv32/rv64/riscv32/riscv64 to set both architecture and bits.

Alright. Then could you introduce a right approach for adding aliases? Seems to need some refactoring.

@Arusekk Arusekk merged commit fbf2727 into Gallopsled:dev Apr 9, 2023
@Xeonacid Xeonacid deleted the fix-elf-arch branch April 9, 2023 03:41
peace-maker added a commit to peace-maker/pwntools that referenced this pull request Apr 10, 2023
They are broken since the riscv architecture was split into riscv32
and riscv64 internally in Gallopsled#2177.
Arusekk pushed a commit that referenced this pull request Apr 11, 2023
* Fix debugging and assembly for RISCV

They are broken since the riscv architecture was split into riscv32
and riscv64 internally in #2177.

* Always look for riscv32. riscv64 or riscv toolchains

* Look for riscv64 for riscv64 first
gogo2464 pushed a commit to gogo2464/pwntools that referenced this pull request Sep 10, 2023
* Determine elf arch by e_machine and bits

Separate 32/64 for riscv and mips
Trim some trailing space

* Fix missing comma

Co-authored-by: Arusekk <arek_koz@o2.pl>

* Add CHANGELOG

* unicorn: Use fixed version

* Cosmetic enhancements

---------

Co-authored-by: Arusekk <arek_koz@o2.pl>
gogo2464 pushed a commit to gogo2464/pwntools that referenced this pull request Sep 10, 2023
* Fix debugging and assembly for RISCV

They are broken since the riscv architecture was split into riscv32
and riscv64 internally in Gallopsled#2177.

* Always look for riscv32. riscv64 or riscv toolchains

* Look for riscv64 for riscv64 first
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.

3 participants