Skip to content

[BUG] Wrong variable checked after PyUnicode_DecodeFSDefault - NULL deref (Solaris) #2849

Description

@Fuyugithub

Bug Description

In psutil/arch/sunos/proc.c, after creating py_envval, the code checks py_envname (the previously created variable) instead of py_envval. If PyUnicode_DecodeFSDefault returns NULL for py_envval, the NULL is passed to PyDict_SetItem, causing a segfault.

Location

psutil/arch/sunos/proc.c:214-216

Code

py_envval = PyUnicode_DecodeFSDefault(dm + 1);
if (!py_envname)    // BUG: should check py_envval
    goto error;

Impact

If PyUnicode_DecodeFSDefault fails for the environment value (e.g., invalid encoding), py_envval is NULL, but the check passes because py_envname was set earlier. The NULL py_envval is then passed to PyDict_SetItem(py_envs, py_envname, py_envval), which dereferences it, causing a crash.

Suggested Fix

py_envval = PyUnicode_DecodeFSDefault(dm + 1);
if (!py_envval)     // Fixed: check the correct variable
    goto error;

Environment

  • OS: Solaris / illumos
  • psutil version: current main (commit 7b6a9a6)
  • Affected API: psutil.Process().environ()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions