OOBW/R due to incompatible format conversion (sandbox escape)
Categories
(Core :: Graphics, defect)
Tracking
()
People
(Reporter: oskarlindberg348, Assigned: lsalzman)
References
Details
(4 keywords, Whiteboard: [client-bounty-form] [adv-main147+] [adv-esr140.7+] [adv-esr115.32+])
Attachments
(1 external file)
Writeup follows
Background
In order to print, the content uses the PRemotePrintJob actor,
this actor uses cairo as its backend for rendering the content to print.
This actor gets drawing events and uses PrintTranslator to draw.
Vulnerability
On some drawing operations, a pattern is specified (such as RecordedStrokeRect), this is handled by DrawTargetCairo::StrokeRect->DrawTargetCairo::DrawPattern, which attempts to convert the general pattern onto a cairo pattern by calling:
void DrawTargetCairo::DrawPattern(const Pattern& aPattern,
const StrokeOptions& aStrokeOptions,
const DrawOptions& aOptions,
DrawPatternType aDrawType,
bool aPathBoundsClip) {
...
cairo_pattern_t* pat =
GfxPatternToCairoPattern(aPattern, aOptions.mAlpha, GetTransform());
...
static cairo_pattern_t* GfxPatternToCairoPattern(const Pattern& aPattern,
Float aAlpha,
const Matrix& aTransform) {
...
switch (aPattern.GetType()) {
...
case PatternType::SURFACE: {
const SurfacePattern& pattern =
static_cast<const SurfacePattern&>(aPattern);
cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(
pattern.mSurface, false, pattern.mSamplingRect);
if (!surf) return nullptr;
GetCairoSurfaceForSourceSurface will check if the surface is a compatible with cairo, and if it's not, will create a new cairo surface, and copy the surface from it:
cairo_surface_t* surf = CreateSubImageForData(map.mData, subimage,
map.mStride, data->GetFormat()); // [1]
if (!surf || cairo_surface_status(surf)) {
if (surf && (cairo_surface_status(surf) == CAIRO_STATUS_INVALID_STRIDE)) {
// If we failed because of an invalid stride then copy into
// a new surface with a stride that cairo chooses. No need to
// set user data since we're not dependent on the original
// data.
cairo_surface_t* result = CopyToImageSurface(
map.mData, subimage, map.mStride, data->GetFormat()); // [2]
data->Unmap();
return result;
}
data->Unmap();
return nullptr;
}
- try to create a cairo image which wrapps the original data.
- in the case we failed doing so, due to invalid stride, create a new surface from scratch and copy the data from the old surface.
The main issue resides in CopyToImageSurface:
tatic cairo_surface_t* CopyToImageSurface(unsigned char* aData,
const IntRect& aRect,
int32_t aStride,
SurfaceFormat aFormat) {
MOZ_ASSERT(aData);
auto aRectWidth = aRect.Width();
auto aRectHeight = aRect.Height();
cairo_surface_t* surf = cairo_image_surface_create(
GfxFormatToCairoFormat(aFormat), aRectWidth, aRectHeight); // [1]
// In certain scenarios, requesting larger than 8k image fails. Bug 803568
// covers the details of how to run into it, but the full detailed
// investigation hasn't been done to determine the underlying cause. We
// will just handle the failure to allocate the surface to avoid a crash.
if (cairo_surface_status(surf)) {
gfxWarning() << "Invalid surface DTC " << cairo_surface_status(surf);
return nullptr;
}
unsigned char* surfData = cairo_image_surface_get_data(surf);
int surfStride = cairo_image_surface_get_stride(surf);
int32_t pixelWidth = BytesPerPixel(aFormat); // [2]
unsigned char* source = aData + aRect.Y() * aStride + aRect.X() * pixelWidth;
MOZ_ASSERT(aStride >= aRectWidth * pixelWidth);
for (int32_t y = 0; y < aRectHeight; ++y) {
memcpy(surfData + y * surfStride, source + y * aStride,
aRectWidth * pixelWidth); // [3]
}
cairo_surface_mark_dirty(surf);
return surf;
}
- We allocate a new surface, however, we need to convert the old surface format to cairo format by
GfxFormatToCairoFormat:
static inline cairo_format_t GfxFormatToCairoFormat(SurfaceFormat format) {
switch (format) {
case SurfaceFormat::A8R8G8B8_UINT32:
return CAIRO_FORMAT_ARGB32;
case SurfaceFormat::X8R8G8B8_UINT32:
return CAIRO_FORMAT_RGB24;
case SurfaceFormat::A8:
return CAIRO_FORMAT_A8;
case SurfaceFormat::R5G6B5_UINT16:
return CAIRO_FORMAT_RGB16_565;
default:
gfxCriticalError() << "Unknown image format " << (int)format;
return CAIRO_FORMAT_ARGB32;
}
}
As we can see, the default format is CAIRO_FORMAT_ARGB32, whose size is 4 bytes per pixel.
2. bpp is calculated based on the old format, which might be greater then 4 bytes (as in the case of HSV)
3. data is being copied according to the bpp calculated in 2, which might be larger then the actually allocated cairo surface backing store.
Asan report
READ of size 12288 at 0x51f0000d8e8f thread T0
#0 0x5c75c849170a in __asan_memcpy /builds/worker/fetches/llvm-project/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:63:3
#1 0x7c174c33bee7 in mozilla::gfx::CopyToImageSurface(unsigned char*, mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> const&, int, mozilla::gfx::SurfaceFormat) /home/user/Downloads/firefox/gfx/2d/DrawTargetCairo.cpp:226:5
#2 0x7c174c32c9fe in mozilla::gfx::GetCairoSurfaceForSourceSurface(mozilla::gfx::SourceSurface*, bool, mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> const&) /home/user/Downloads/firefox/gfx/2d/DrawTargetCairo.cpp:372:33
#3 0x7c174c32fdcc in mozilla::gfx::GfxPatternToCairoPattern(mozilla::gfx::Pattern const&, float, mozilla::gfx::BaseMatrix<float> const&) /home/user/Downloads/firefox/gfx/2d/DrawTargetCairo.cpp:463:31
#4 0x7c174c32f1a5 in mozilla::gfx::DrawTargetCairo::DrawPattern(mozilla::gfx::Pattern const&, mozilla::gfx::StrokeOptions const&, mozilla::gfx::DrawOptions const&, mozilla::gfx::DrawTargetCairo::DrawPatternType, bool) /home/user/Downloads/firefox/gfx/2d/DrawTargetCairo.cpp:1046:7
#5 0x7c174c334508 in mozilla::gfx::DrawTargetCairo::StrokeRect(mozilla::gfx::RectTyped<mozilla::gfx::UnknownUnits, float> const&, mozilla::gfx::Pattern const&, mozilla::gfx::StrokeOptions const&, mozilla::gfx::DrawOptions const&) /home/user/Downloads/firefox/gfx/2d/DrawTargetCairo.cpp:1294:3
#6 0x7c174c44b0f4 in nsDeviceContext::CreateRenderingContextCommon(bool) /home/user/Downloads/firefox/gfx/src/nsDeviceContext.cpp:150:9
#7 0x7c174c44ac7f in nsDeviceContext::CreateRenderingContext() /home/user/Downloads/firefox/gfx/src/nsDeviceContext.cpp:110:10
#8 0x7c1756c0a9df in mozilla::layout::PrintTranslator::CreateDrawTarget(mozilla::gfx::ReferencePtr, mozilla::gfx::IntSizeTyped<mozilla::gfx::UnknownUnits> const&, mozilla::gfx::SurfaceFormat) /home/user/Downloads/firefox/layout/printing/PrintTranslator.cpp:78:51
#9 0x7c174c2a9f60 in mozilla::gfx::RecordedDrawTargetCreation::PlayEvent(mozilla::gfx::Translator*) const /home/user/Downloads/firefox/gfx/2d/RecordedEventImpl.h:2183:20
#10 0x7c174c4050f2 in std::function<bool (mozilla::gfx::RecordedEvent*)>::operator()(mozilla::gfx::RecordedEvent*) const /home/user/.mozbuild/sysroot-x86_64-linux-gnu/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/std_function.h:622:14
#11 0x7c174c4050f2 in bool mozilla::gfx::RecordedEvent::DoWithEvent<mozilla::gfx::EventStream>(mozilla::gfx::EventStream&, mozilla::gfx::RecordedEvent::EventType, std::function<bool (mozilla::gfx::RecordedEvent*)> const&) /home/user/Downloads/firefox/gfx/2d/RecordedEventImpl.h:4723:5
#12 0x7c1756c0a4af in mozilla::layout::PrintTranslator::TranslateRecording(mozilla::layout::PRFileDescStream&) /home/user/Downloads/firefox/layout/printing/PrintTranslator.cpp:54:20
A read violation happens, a write will follow.
Implicaitons
Out of bounds write in the browser process on firefox latest
Reproduction
- Apply the following patch:
diff --git a/gfx/src/nsDeviceContext.cpp b/gfx/src/nsDeviceContext.cpp
index 055ebbabac41..876ed74ec8ce 100644
--- a/gfx/src/nsDeviceContext.cpp
+++ b/gfx/src/nsDeviceContext.cpp
@@ -10,11 +10,16 @@
#include "gfxPoint.h" // for gfxSize
#include "gfxTextRun.h" // for gfxFontGroup
#include "mozilla/LookAndFeel.h"
+#include "mozilla/gfx/2D.h"
+#include "mozilla/gfx/DrawEventRecorder.h"
#include "mozilla/gfx/PathHelpers.h"
+#include "mozilla/gfx/Point.h"
#include "mozilla/gfx/PrintTarget.h"
#include "mozilla/ProfilerMarkers.h"
#include "mozilla/StaticPrefs_layout.h"
-#include "mozilla/Try.h" // for MOZ_TRY
+#include "mozilla/Try.h" // for MOZ_TRY
+#include "mozilla/gfx/Rect.h"
+#include "mozilla/layout/printing/DrawEventRecorder.h"
#include "mozilla/widget/Screen.h" // for Screen
#include "nsDebug.h" // for NS_ASSERTION, etc
#include "nsFontMetrics.h" // for nsFontMetrics
@@ -127,6 +132,18 @@ UniquePtr<gfxContext> nsDeviceContext::CreateRenderingContextCommon(
RefPtr<DrawEventRecorder> recorder;
mDeviceContextSpec->GetDrawEventRecorder(getter_AddRefs(recorder));
dt = mPrintTarget->MakeDrawTarget(gfx::IntSize(mWidth, mHeight), recorder);
+
+ auto data_surf_size = mozilla::gfx::IntSize(0x100, 1);
+ auto data_surf_format = SurfaceFormat::HSV;
+ auto data_surf =
+ Factory::CreateDataSourceSurface(data_surf_size, data_surf_format, true)
+ .take();
+
+ auto pattern =
+ SurfacePattern(data_surf, ExtendMode::REPEAT, Matrix(),
+ SamplingFilter::GOOD, IntRect(0, 0, 0x400, 1));
+ const auto rect = Rect(0, 0, 0x100, 0x100);
+ dt->StrokeRect(rect, pattern);
}
if (!dt || !dt->IsValid()) {
- Host the triggering index.html (this just prints the page, save the result as pdf to trigger rendering, this is not a necessity for the bug. its for debugging reasons only)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Print Example</title>
<style>
/* This CSS rule hides the print button when the user is printing the page. */
@media print {
.print-button {
display: none;
}
}
</style>
<script>
window.print();
</script>
</head>
<body>
<header>
<h1>This is the content you can print</h1>
<p>This is a paragraph that will be included in the printout.</p>
</header>
<main>
<p>You can also control what gets printed using CSS.</p>
<p>
For example, elements with the class `print-button` will be hidden in
the print preview.
</p>
</main>
<footer>
<p>© 2025 Print Example</p>
</footer>
<button class="print-button" onclick="window.print()">
Print this Page
</button>
</body>
</html>
The vulnerability was found by manual auditing.
Updated•11 months ago
|
| Assignee | ||
Comment 3•11 months ago
|
||
Updated•11 months ago
|
Updated•11 months ago
|
| Assignee | ||
Comment 4•11 months ago
|
||
Comment on attachment 9531586 [details]
(secure)
Security Approval Request
- How easily could an exploit be constructed based on the patch?:
- Do comments in the patch, the check-in comment, or tests included in the patch paint a bulls-eye on the security problem?: No
- Which branches (beta, release, and/or ESR) are affected by this flaw, and do the release status flags reflect this affected/unaffected state correctly?: all
- If not all supported branches, which bug introduced the flaw?: None
- Do you have backports for the affected branches?: Yes
- If not, how different, hard to create, and risky will they be?:
- How likely is this patch to cause regressions; how much testing does it need?: Unlikely
- Is the patch ready to land after security approval is given?: Yes
- Is Android affected?: Yes
Updated•11 months ago
|
Updated•11 months ago
|
Comment 5•11 months ago
|
||
Comment on attachment 9531586 [details]
(secure)
Approved to land and request uplift
| Assignee | ||
Comment 6•11 months ago
|
||
Comment on attachment 9531586 [details]
(secure)
Beta/Release Uplift Approval Request
- User impact if declined/Reason for urgency: Potential memory bounds violation in printing.
- Is this code covered by automated tests?: Yes
- Has the fix been verified in Nightly?: Yes
- Needs manual test from QE?: No
- If yes, steps to reproduce:
- List of other uplifts needed: None
- Risk to taking this patch: Low
- Why is the change risky/not risky? (and alternatives if risky): It just converts a silent warning about an unexpected case leading to the sec issue into more visible errors that can be safely addressed if they arise.
- String changes made/needed:
- Is Android affected?: Yes
ESR Uplift Approval Request
- If this is not a sec:{high,crit} bug, please state case for ESR consideration:
- User impact if declined: Potential memory bounds violation in printing.
- Fix Landed on Version: 148
- Risk to taking this patch: Low
- Why is the change risky/not risky? (and alternatives if risky):
Comment 8•11 months ago
|
||
Comment 9•11 months ago
|
||
Comment on attachment 9531586 [details]
(secure)
Approved for 147.0b3, 140.7esr, and 115.32esr.
Comment 10•11 months ago
|
||
| uplift | ||
Comment 11•11 months ago
|
||
| uplift | ||
Updated•11 months ago
|
Comment 12•11 months ago
|
||
| uplift | ||
Updated•11 months ago
|
Comment 13•11 months ago
|
||
I'm not sure what you mean by this bit of the original comment:
- Host the triggering index.html (this just prints the page, save the result as pdf to trigger rendering, this is not a necessity for the bug. its for debugging reasons only)
The testcase calls window.print() -- does the print preview itself trigger the bug? Or does the user have to actually interact with that dialog to print or save to PDF to trigger the bug? I'm not sure what "not a necessity" means -- that you could have done the same with no user interaction?
Comment 14•11 months ago
|
||
Lee, do we need a cairo_surface_status() for example here: https://searchfox.org/firefox-main/source/gfx/2d/DrawTargetCairo.cpp#270
It's not obvious to me if the "nil" surface is handled correctly.
| Reporter | ||
Comment 15•11 months ago
|
||
The bug is triggered when the ProcessPage ipc is sent to the parent, this does not require user interaction.
this calls PrintPage->TranslateRecording which triggers the bug.
| Reporter | ||
Comment 16•11 months ago
|
||
(In reply to Daniel Veditz [:dveditz] from comment #13)
I'll attempt and create a poc which does not require any interaction.
| Assignee | ||
Comment 17•11 months ago
|
||
(In reply to Simon Friedberger [:simonf] from comment #14)
Lee, do we need a cairo_surface_status() for example here: https://searchfox.org/firefox-main/source/gfx/2d/DrawTargetCairo.cpp#270
It's not obvious to me if the "nil" surface is handled correctly.
It's a Cairo error surface, not a nullptr, so it should be fine.
Updated•10 months ago
|
Updated•10 months ago
|
Updated•10 months ago
|
Updated•5 months ago
|
Description
•