hugsy / gef

GEF (GDB Enhanced Features) - a modern experience for GDB with advanced debugging capabilities for exploit devs & reverse engineers on Linux
https://hugsy.github.io/gef
MIT License
6.87k stars 726 forks source link

[Bug] pyenv site_packages_dir assertion fails when setting multiple python versions in pyenv #1048

Closed Apeng7364 closed 8 months ago

Apeng7364 commented 8 months ago

GEF+GDB version

GEF: (Standalone)
Blob Hash(/home/apeng/.gef-.py): 3300b1c858cbd6765e9877e18f98fa30c03c4fdf
SHA256(/home/apeng/.gef-.py): acb7142bb4ecab5f5a867fcb583fc58e12d7f24829b8a9fdcf596f8e6eb3c18a
GDB: 14.0.50.20230907-git
GDB-Python: 3.11

Operating System

Ubuntu 23.10

Describe the issue you encountered

When setting multiple global versions by pyenv, gef will throw an AssertionError.

The Pyenv site packages path is set inappropriately, and should not just get the output of pyenv version-name as the python version of pyenv. And the python directory name in site_packages is also set inappropriately when python version >= 3.10.

        pyenv = which("pyenv")
        pyenv_root = gef_pystring(subprocess.check_output([pyenv, "root"]).strip())
        pyenv_version = gef_pystring(subprocess.check_output([pyenv, "version-name"]).strip())
        site_packages_dir = pathlib.Path(pyenv_root) / f"versions/{pyenv_version}/lib/python{pyenv_version[:3]}/site-packages"
        assert site_packages_dir.is_dir()
        site.addsitedir(str(site_packages_dir.absolute()))

There is a possible solution:

        pyenv = which("pyenv")
        pyenv_root = gef_pystring(subprocess.check_output([pyenv, "root"]).strip())
        pyenv_version_first = gef_pystring(subprocess.check_output([pyenv, "version-name"]).split(b":")[0].strip())
        pyenv_version = ".".join(pyenv_version_first.split(".")[:2])
        site_packages_dir = pathlib.Path(pyenv_root) / f"versions/{pyenv_version_first}/lib/python{pyenv_version}/site-packages"
        assert site_packages_dir.is_dir()
        site.addsitedir(str(site_packages_dir.absolute()))

Do you read the docs and look at previously closed issues/PRs for similar cases?

Yes

Architecture impacted

Describe your issue. Without a proper reproduction step-by-step, your issue will be ignored.

Set multiple pyenv global versions:

pyenv global 3.12.1 2.7.18

gef script will throw an AssertionError

$ gdb
GNU gdb (Ubuntu 14.0.50.20230907-0ubuntu1) 14.0.50.20230907-git
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
Traceback (most recent call last):
  File "/home/apeng/.gef-.py", line 11303, in <module>
    assert site_packages_dir.is_dir()
AssertionError

(gdb) 

Minimalist test case

No response

Additional context?

No response

timfel commented 8 months ago

The issue isn't even multiple versions of pyenv. The code does pyenv_version[:3], which for versions >= 3.9 is just always wrong.

Grazfather commented 8 months ago

Yeah that is sloppy version parsing, will have to fix.

hugsy commented 8 months ago

pyenv is a user setup issue, not gdb related. Thinking more into it, I don't think we should get involved at all into making gef compatible for it.

In my opinion this should be done from a separate script that'd be source-d before gef is. Happy to do the PR (and docs) for it. Thoughts cc @Grazfather ?

Grazfather commented 8 months ago

It might be a use case we want support, but I don't use it personally (for GDB) and I think it adds a lot of complexity. And I can't even help test it, so I'd rather not worry about it.

Grazfather commented 8 months ago

A gef-extras script to do it would be nice!