Project

General

Profile

Actions

Bug #81337

open
SZ SZ

mgr: ThreadMonitor::read_thread_stat() returns true with uninitialised values on malformed /proc stat line

Bug #81337: mgr: ThreadMonitor::read_thread_stat() returns true with uninitialised values on malformed /proc stat line

Added by Steven Zuraski 2 months ago. Updated 2 months ago.

Status:
Fix Under Review
Priority:
Normal
Category:
ceph-mgr
Target version:
-
Due date:
% Done:

0%

Source:
Backport:
Regression:
No
Severity:
2 - major
Reviewed:
Affected Versions:
ceph-qa-suite:
Pull request ID:
Tags (freeform):
Merge Commit:
Fixed In:
Released In:
Upkeep Timestamp:

Description

ThreadMonitor::read_thread_stat() (src/mgr/ThreadMonitor.cc:226) parses /proc/<pid>/task/<tid>/stat using std::stringstream >> utime >> stime. There is no stream success check after the extraction. If the parse fails (malformed or unexpectedly short line), utime and stime contain uninitialised / default values and the function still returns true, causing the caller to compute a CPU percentage from garbage data.

bool ThreadMonitor::read_thread_stat(pid_t tid, ...) {
  std::ifstream stat_file(...);
  std::string stat_content(...);
  std::stringstream ss(stat_content);
  // skip fields 1-13
  uint64_t utime, stime;
  ss >> utime >> stime;   // no success check
  // uses utime/stime even if extraction failed
  ...
  return true;            // unconditional true
}

Failure scenario

On a system where the /proc stat line is unexpectedly formatted (e.g. a future kernel adds fields before utime/stime, or the file is transiently short), utime and stime hold uninitialised values. The subsequent CPU-percentage calculation produces garbage metrics silently reported to all mgr modules.

Suggested fix

Check ss.fail() or the boolean result of each >> extraction, and return false on parse failure:

if (!(ss >> utime >> stime))
  return false;

Unit test (illustrative)

TEST(ThreadMonitor, ReadThreadStatReturnsFalseOnMalformedLine) {
  // Feed a deliberately malformed stat line; assert read_thread_stat returns false.
}

SZ Updated by Steven Zuraski 2 months ago Actions #1

  • Status changed from New to Fix Under Review
  • Pull request ID set to 72354
Actions

Also available in: PDF Atom