Commit 7cfaffb1 authored by Brian White's avatar Brian White Committed by Commit Bot

Fix InspectConstructionArguments with random min & max.

If the min/max parameters are really fubar, such as which might come
from a malicious IPC message, min & max can be limited only to then
be swapped and become invalid.  Do the swap first.

Also, remove duplicate bucket-limit condition and set the defined
maximum to be the lower of the two.

Bug: 905226
Change-Id: If73e97e43e93704f93456da6af0708422fa145a0
Reviewed-on: https://chromium-review.googlesource.com/c/1335731
Commit-Queue: Brian White <bcwhite@chromium.org>
Reviewed-by: default avatarAlexei Svitkine <asvitkine@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608398}
parent 38d62c24
...@@ -93,7 +93,7 @@ typedef HistogramBase::Count Count; ...@@ -93,7 +93,7 @@ typedef HistogramBase::Count Count;
typedef HistogramBase::Sample Sample; typedef HistogramBase::Sample Sample;
// static // static
const uint32_t Histogram::kBucketCount_MAX = 16384u; const uint32_t Histogram::kBucketCount_MAX = 10002u;
class Histogram::Factory { class Histogram::Factory {
public: public:
...@@ -523,27 +523,37 @@ bool Histogram::InspectConstructionArguments(StringPiece name, ...@@ -523,27 +523,37 @@ bool Histogram::InspectConstructionArguments(StringPiece name,
Sample* minimum, Sample* minimum,
Sample* maximum, Sample* maximum,
uint32_t* bucket_count) { uint32_t* bucket_count) {
bool check_okay = true;
// Checks below must be done after any min/max swap.
if (*minimum > *maximum) {
check_okay = false;
std::swap(*minimum, *maximum);
}
// Defensive code for backward compatibility. // Defensive code for backward compatibility.
if (*minimum < 1) { if (*minimum < 1) {
DVLOG(1) << "Histogram: " << name << " has bad minimum: " << *minimum; DVLOG(1) << "Histogram: " << name << " has bad minimum: " << *minimum;
*minimum = 1; *minimum = 1;
if (*maximum < 1)
*maximum = 1;
} }
if (*maximum >= kSampleType_MAX) { if (*maximum >= kSampleType_MAX) {
DVLOG(1) << "Histogram: " << name << " has bad maximum: " << *maximum; DVLOG(1) << "Histogram: " << name << " has bad maximum: " << *maximum;
*maximum = kSampleType_MAX - 1; *maximum = kSampleType_MAX - 1;
} }
if (*bucket_count >= kBucketCount_MAX) { if (*bucket_count >= kBucketCount_MAX) {
check_okay = false;
DVLOG(1) << "Histogram: " << name << " has bad bucket_count: " DVLOG(1) << "Histogram: " << name << " has bad bucket_count: "
<< *bucket_count; << *bucket_count;
*bucket_count = kBucketCount_MAX - 1; *bucket_count = kBucketCount_MAX - 1;
} }
if (*bucket_count > 1002) {
bool check_okay = true; UmaHistogramSparse("Histogram.TooManyBuckets.1000",
static_cast<Sample>(HashMetricName(name)));
if (*minimum > *maximum) {
check_okay = false;
std::swap(*minimum, *maximum);
} }
// Ensure parameters are sane.
if (*maximum == *minimum) { if (*maximum == *minimum) {
check_okay = false; check_okay = false;
*maximum = *minimum + 1; *maximum = *minimum + 1;
...@@ -552,13 +562,6 @@ bool Histogram::InspectConstructionArguments(StringPiece name, ...@@ -552,13 +562,6 @@ bool Histogram::InspectConstructionArguments(StringPiece name,
check_okay = false; check_okay = false;
*bucket_count = 3; *bucket_count = 3;
} }
// Very high bucket counts are wasteful. Use a sparse histogram instead.
// Value of 10002 equals a user-supplied value of 10k + 2 overflow buckets.
constexpr uint32_t kMaxBucketCount = 10002;
if (*bucket_count > kMaxBucketCount) {
check_okay = false;
*bucket_count = kMaxBucketCount;
}
if (*bucket_count > static_cast<uint32_t>(*maximum - *minimum + 2)) { if (*bucket_count > static_cast<uint32_t>(*maximum - *minimum + 2)) {
check_okay = false; check_okay = false;
*bucket_count = static_cast<uint32_t>(*maximum - *minimum + 2); *bucket_count = static_cast<uint32_t>(*maximum - *minimum + 2);
......
...@@ -37820,6 +37820,17 @@ uploading your change for review. ...@@ -37820,6 +37820,17 @@ uploading your change for review.
</summary> </summary>
</histogram> </histogram>
<histogram name="Histogram.TooManyBuckets.1000" enum="HistogramNameHash"
expires_after="M73">
<owner>asvitkine@chromium.org</owner>
<owner>bcwhite@chromium.org</owner>
<summary>
The hash codes of histograms that were found to request more than 1000
buckets. These would be DCHECK exceptions in debug builds if the limit is
lowered so are being logged before that change.
</summary>
</histogram>
<histogram name="History.AttemptedToFixProfileError"> <histogram name="History.AttemptedToFixProfileError">
<owner>brettw@chromium.org</owner> <owner>brettw@chromium.org</owner>
<summary> <summary>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment