pwntools icon indicating copy to clipboard operation
pwntools copied to clipboard

`context.bits` will not automatically switch when setting `context.arch` twice.

Open RocketMaDev opened this issue 1 year ago • 3 comments

I was using IPython to do some quick poc work that time, first I switched context.arch to 'amd64' and assembled cdqe and then I suddenly would like to try what if I assemble cdqe in 32-bit mode. So I switched context.arch back to 'i386' and wanted to assemble, only to find an error: AttributeError: Invalid arch/bits combination: i386/64

Pwntools may can not handle context.arch correctly, it could change context.bits accordingly when first setting it. However, when setting context.arch the second time, context.bits left unchanged. I guess this bug is related to self._tls?

poc:

屏幕截图_20241111_003054

one more poc:

from pwn import *
print(f"{context.arch}/{context.bits}")
context.arch = 'amd64'
print(f"{context.arch}/{context.bits}")
context.arch = 'i386'
print(f"{context.arch}/{context.bits}")

RocketMaDev avatar Nov 10 '24 16:11 RocketMaDev

BTW, the document has a wrong source code reference. When clicking on property arch [source], it jumps to file /pwnlib/context.py, which don't exist. The correct file should be pwnlib/context/__init__.py.

RocketMaDev avatar Nov 10 '24 17:11 RocketMaDev

This is actually a feature to keep manual context changes instead of silently overwriting values using those smart attributes like arch. arch changes bits and endian too, so the idea is to keep the old bits value if it was changed before:

>>> context.bits = 24
>>> context.arch = 'amd64'
>>> context.bits
24

We could special case setting bits and endian through arch and not protect them from changes through arch again? Sounds like that's what most users want. We should only retain the old value if it was set explicitly like in the example above.

peace-maker avatar Dec 08 '24 18:12 peace-maker

That's right. Consider adding an "explicit flag", set it only when user explicitly set context.bits by method. When switching context.arch, set property directly, so that the flag won't be modified.

RocketMaDev avatar Dec 08 '24 19:12 RocketMaDev