Project

General

Profile

Actions

Bug #81020

open
SZ SZ

mgr: PerfCounterInstance::get_latest_data_avg() calls avg_buffer.back() with no empty() guard (UB on empty/scalar buffer)

Bug #81020: mgr: PerfCounterInstance::get_latest_data_avg() calls avg_buffer.back() with no empty() guard (UB on empty/scalar buffer)

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:
3 - minor
Reviewed:
Affected Versions:
ceph-qa-suite:
Pull request ID:
Tags (freeform):
Merge Commit:
Fixed In:
Released In:
Upkeep Timestamp:

Description

PerfCounterInstance::get_latest_data_avg() (src/mgr/PerfCounterInstance.h:67) returns avg_buffer.back() with no !avg_buffer.empty() check -- the same class of UB as get_latest_data(). It is reachable two ways: (1) an avg-type instance before its first push_avg(), and (2) any SCALAR (non-PERFCOUNTER_LONGRUNAVG) instance, whose avg_buffer is default-constructed with capacity 0 and is always empty -- so calling get_latest_data_avg() on a scalar instance is always UB.

const AvgDataPoint& get_latest_data_avg() const
{
  return avg_buffer.back();   // line 69 - no empty() guard; UB on empty/zero-cap
}

The constructor only sizes one of the two buffers:

PerfCounterInstance(enum perfcounter_type_d type)
{
  if (type & PERFCOUNTER_LONGRUNAVG)
    avg_buffer = boost::circular_buffer<AvgDataPoint>(20);
  else
    buffer = boost::circular_buffer<DataPoint>(20);   // avg_buffer stays capacity 0
}

Failure scenario

Construct PerfCounterInstance(PERFCOUNTER_U64) (scalar; avg_buffer capacity 0) and call get_latest_data_avg() -> avg_buffer.back() on a zero-capacity/empty buffer -> UB.

Suggested fix

Guard with if (avg_buffer.empty()); consider asserting the instance is a LONGRUNAVG type before serving avg data.

Unit test (illustrative)

TEST(PerfCounterInstance, LatestAvgOnScalarIsGuarded) {
  PerfCounterInstance pci(PERFCOUNTER_U64);   // scalar: avg_buffer capacity 0
  ASSERT_TRUE(pci.get_data_avg().empty());
  // Current code: pci.get_latest_data_avg() is UB here.
}

SZ Updated by Steven Zuraski 2 months ago Actions #1

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

Also available in: PDF Atom