Skip to content

[BUG] lsof silently ignore processes with unbalanced round brackets in COMM #359

@isaac-sec

Description

@isaac-sec

Describe the bug
On Linux, when iterating over processes, lsof silently skips processes that have unbalanced round brackets in their COMM.

To Reproduce
Steps to reproduce the behavior:

  1. Run nc -l -p 5000
  2. Run lsof -iTCP -sTCP:LISTEN -n -P and see output that includes a line similar to this:
nc        430743 user   3u  IPv4 955235      0t0  TCP *:5000 (LISTEN)
  1. Change the process' COMM: sudo gdb -p 430743 -batch -ex 'call (int)prctl(15, "nc (", 0, 0, 0)'
  2. Run lsof -iTCP -sTCP:LISTEN -n -P again
  3. The nc line is gone
  4. Change back the process' COMM: sudo gdb -p 430743 -batch -ex 'call (int)prctl(15, "nc", 0, 0, 0)'
  5. Run lsof -iTCP -sTCP:LISTEN -n -P again
  6. The nc line returns

Expected behavior
An unbalanced round bracket in COMM should not cause a process to disappear from the lsof output.

Program output
N/A

Environment (please complete the following information):

  • Kernel: Linux
  • OS: Fedora
  • lsof Version: 4.99.6
  • Origin: installed by package manager

Additional context
The issue is caused by the /proc/<pid>/stat parsing code that tries to be overly smart and match brackets instead of just looking for the last closing round bracket:

if (ch == ')') {
/*
* Balance parentheses when a closure is encountered. When
* they are balanced, this is the end of the command.
*/
pc--;
if (!pc)
break;
}

Related links

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions