From d3a73e46e8f839531b91d787199277195823881a Mon Sep 17 00:00:00 2001 From: LODIBIDO Date: Fri, 4 Dec 2026 15:27:39 +0100 Subject: [PATCH] fix(libcairo): fault context pointers that Cairo dereferences Co-Authored-By: Claude Sonnet 5.5 --- core/libs/prx/libcairo/Context.cpp | 22 ++++- .../tests/GuestCairoContextGuestPointers.cpp | 92 +++++++++++++------ .../fix-cairo-context-guest-pointers.md | 2 +- 3 files changed, 85 insertions(+), 31 deletions(-) diff --git a/core/libs/prx/libcairo/Context.cpp b/core/libs/prx/libcairo/Context.cpp index d0e53b8066bf..67c763cc00f1 100644 --- a/core/libs/prx/libcairo/Context.cpp +++ b/core/libs/prx/libcairo/Context.cpp @@ -43,7 +43,7 @@ T ReadGuest(const T* source, const char* caller) { const bool ok = GuestCopyIn_nid_no_patch(&value, source, sizeof(T)); errno = savedErrno; if (!ok) - NotImplemented_nid_no_patch(caller); + GuestFault_nid_no_patch(caller); return value; } @@ -53,7 +53,7 @@ void WriteGuest(T* destination, const T& value, const char* caller) { const bool ok = GuestCopyOut_nid_no_patch(destination, &value, sizeof(T)); errno = savedErrno; if (!ok) - NotImplemented_nid_no_patch(caller); + GuestFault_nid_no_patch(caller); } void RequireWritable(void* destination, std::size_t bytes, const char* caller) { @@ -61,7 +61,7 @@ void RequireWritable(void* destination, std::size_t bytes, const char* caller) { const bool ok = GuestWritable_nid_no_patch(destination, bytes); errno = savedErrno; if (!ok) - NotImplemented_nid_no_patch(caller); + GuestFault_nid_no_patch(caller); } void RequireReadable(const double* source, std::size_t count, const char* caller) { @@ -78,6 +78,10 @@ void RequireReadable(const double* source, std::size_t count, const char* caller NotImplemented_nid_no_patch(caller); } +bool InError(CairoContext* context) { + return context && aps5_cairo_status(Backend(context)) != CAIRO_STATUS_SUCCESS; +} + template void WriteExtents(CairoContext* context, double* x1, double* y1, double* x2, double* y2, Fn call, const char* caller) { for (double* bound : {x1, y1, x2, y2}) @@ -97,6 +101,8 @@ void WriteExtents(CairoContext* context, double* x1, double* y1, double* x2, dou template void TransformCoordinates(CairoContext* context, double* x, double* y, Fn transform, const char* caller) { + if (InError(context)) + return; if (x == y) { double value = ReadGuest(x, caller); RequireWritable(x, sizeof(value), caller); @@ -155,11 +161,15 @@ void APS5_VABI cairo_rotate(CairoContext* context, double radians) { } void APS5_VABI cairo_transform(CairoContext* context, const CairoMatrix* matrix) { + if (InError(context)) + return; const cairo_matrix_t backend = std::bit_cast(ReadGuest(matrix, __func__)); aps5_cairo_transform(Backend(context), &backend); } void APS5_VABI cairo_set_matrix(CairoContext* context, const CairoMatrix* matrix) { + if (InError(context)) + return; const cairo_matrix_t backend = std::bit_cast(ReadGuest(matrix, __func__)); aps5_cairo_set_matrix(Backend(context), &backend); } @@ -240,6 +250,8 @@ double APS5_VABI cairo_get_miter_limit(CairoContext* context) { } void APS5_VABI cairo_set_dash(CairoContext* context, const double* dashes, int numDashes, double offset) { + if (InError(context)) + return; if (numDashes > 0) { if (!dashes) NotImplemented_nid_no_patch(__func__); @@ -249,6 +261,8 @@ void APS5_VABI cairo_set_dash(CairoContext* context, const double* dashes, int n } void APS5_VABI cairo_get_dash(CairoContext* context, double* dashes, double* offset) { + if (InError(context)) + return; const int count = aps5_cairo_get_dash_count(Backend(context)); std::vector hostDashes(dashes && count > 0 ? static_cast(count) : 0); if (!hostDashes.empty()) @@ -265,7 +279,7 @@ void APS5_VABI cairo_get_dash(CairoContext* context, double* dashes, double* off const bool ok = GuestCopyOut_nid_no_patch(dashes, hostDashes.data(), hostDashes.size() * sizeof(double)); errno = savedErrno; if (!ok) - NotImplemented_nid_no_patch(__func__); + GuestFault_nid_no_patch(__func__); } if (offset) WriteGuest(offset, hostOffset, __func__); diff --git a/core/libs/tests/GuestCairoContextGuestPointers.cpp b/core/libs/tests/GuestCairoContextGuestPointers.cpp index 48fc6df090e6..a896bb6301bb 100644 --- a/core/libs/tests/GuestCairoContextGuestPointers.cpp +++ b/core/libs/tests/GuestCairoContextGuestPointers.cpp @@ -1,3 +1,4 @@ +#include "prx/libc/include/General.hpp" #include "prx/libcairo/include/Path.hpp" #include @@ -100,6 +101,13 @@ void ExpectThrowsPreservingErrno(Fn&& fn, int line) { #define EXPECT_REJECTED(...) ExpectThrowsPreservingErrno([&] { __VA_ARGS__; }, __LINE__) +#define EXPECT_FAULT(...) \ + do { \ + const auto calls = NotImplementedCalls_nid_no_patch(); \ + EXPECT_REJECTED(__VA_ARGS__); \ + CHECK(NotImplementedCalls_nid_no_patch() == calls); \ + } while (false) + CairoContext* CreateContext() { CairoSurface* surface = cairo_image_surface_create(0, 16, 16); CHECK(surface != nullptr); @@ -193,6 +201,37 @@ void UnreadableDashTail() { cairo_destroy(context); } +void ContextInError() { + CairoContext* context = CreateContext(); + const double negative = -1.0; + cairo_set_dash(context, &negative, 1, 0.0); + CHECK(aps5_cairo_status(reinterpret_cast(context)) == CAIRO_STATUS_INVALID_DASH); + auto* badMatrix = reinterpret_cast(std::uintptr_t{0x10}); + auto* bad = reinterpret_cast(std::uintptr_t{0x10}); + errno = 71; + cairo_set_matrix(context, badMatrix); + cairo_transform(context, badMatrix); + cairo_user_to_device(context, bad, bad); + cairo_user_to_device_distance(context, bad, nullptr); + cairo_device_to_user(context, nullptr, bad); + cairo_device_to_user_distance(context, nullptr, nullptr); + cairo_get_dash(context, bad, bad); + cairo_set_dash(context, bad, 2, 0.0); + cairo_set_dash(context, nullptr, 2, 0.0); + CHECK(errno == 71); + double offset = 99.0; + cairo_get_dash(context, nullptr, &offset); + CHECK(offset == 99.0); + CairoMatrix matrix{}; + cairo_get_matrix(context, &matrix); + CHECK(Close(matrix.xx, 1.0) && Close(matrix.yy, 1.0)); + EXPECT_FAULT(cairo_get_matrix(context, badMatrix)); + EXPECT_FAULT(cairo_get_current_point(context, bad, nullptr)); + EXPECT_FAULT(cairo_path_extents(context, bad, nullptr, nullptr, nullptr)); + EXPECT_FAULT(cairo_clip_extents(context, nullptr, nullptr, nullptr, bad)); + cairo_destroy(context); +} + } // namespace int main() { @@ -211,13 +250,13 @@ int main() { auto* badMatrixOut = reinterpret_cast(std::uintptr_t{0x10}); auto* badDouble = reinterpret_cast(std::uintptr_t{0x10}); - EXPECT_REJECTED(cairo_set_matrix(context, nullptr)); - EXPECT_REJECTED(cairo_set_matrix(context, badMatrixIn)); - EXPECT_REJECTED(cairo_transform(context, nullptr)); - EXPECT_REJECTED(cairo_transform(context, badMatrixIn)); - EXPECT_REJECTED(cairo_get_matrix(context, nullptr)); - EXPECT_REJECTED(cairo_get_matrix(context, badMatrixOut)); - EXPECT_REJECTED(cairo_get_matrix(context, roMatrix)); + EXPECT_FAULT(cairo_set_matrix(context, nullptr)); + EXPECT_FAULT(cairo_set_matrix(context, badMatrixIn)); + EXPECT_FAULT(cairo_transform(context, nullptr)); + EXPECT_FAULT(cairo_transform(context, badMatrixIn)); + EXPECT_FAULT(cairo_get_matrix(context, nullptr)); + EXPECT_FAULT(cairo_get_matrix(context, badMatrixOut)); + EXPECT_FAULT(cairo_get_matrix(context, roMatrix)); const CairoMatrix scale{2.0, 0.0, 0.0, 3.0, 4.0, -5.0}; cairo_set_matrix(context, &scale); @@ -251,17 +290,17 @@ int main() { for (CoordinateTransform transform : coordinateTransforms) { double first = 7.0; double second = 11.0; - EXPECT_REJECTED(transform(context, nullptr, &second)); + EXPECT_FAULT(transform(context, nullptr, &second)); CHECK(Close(second, 11.0)); - EXPECT_REJECTED(transform(context, &first, nullptr)); + EXPECT_FAULT(transform(context, &first, nullptr)); CHECK(Close(first, 7.0)); - EXPECT_REJECTED(transform(context, badDouble, &second)); + EXPECT_FAULT(transform(context, badDouble, &second)); CHECK(Close(second, 11.0)); - EXPECT_REJECTED(transform(context, &first, badDouble)); + EXPECT_FAULT(transform(context, &first, badDouble)); CHECK(Close(first, 7.0)); - EXPECT_REJECTED(transform(context, roDoubles, &second)); + EXPECT_FAULT(transform(context, roDoubles, &second)); CHECK(Close(second, 11.0)); - EXPECT_REJECTED(transform(context, &first, roDoubles)); + EXPECT_FAULT(transform(context, &first, roDoubles)); CHECK(Close(first, 7.0)); } @@ -287,10 +326,10 @@ int main() { std::array outDashes{99.0, 99.0}; double outOffset = 99.0; - EXPECT_REJECTED(cairo_get_dash(context, badDouble, &outOffset)); - EXPECT_REJECTED(cairo_get_dash(context, roDoubles, &outOffset)); + EXPECT_FAULT(cairo_get_dash(context, badDouble, &outOffset)); + EXPECT_FAULT(cairo_get_dash(context, roDoubles, &outOffset)); CHECK(Close(outOffset, 99.0)); - EXPECT_REJECTED(cairo_get_dash(context, outDashes.data(), roDoubles)); + EXPECT_FAULT(cairo_get_dash(context, outDashes.data(), roDoubles)); CHECK(Close(outDashes[0], 99.0) && Close(outDashes[1], 99.0)); cairo_get_dash(context, outDashes.data(), &outOffset); CHECK(Close(outDashes[0], 4.0) && Close(outDashes[1], 2.0) && Close(outOffset, 1.5)); @@ -302,8 +341,8 @@ int main() { double px = 77.0; double py = 88.0; - EXPECT_REJECTED(cairo_get_current_point(context, &px, roDoubles)); - EXPECT_REJECTED(cairo_get_current_point(context, badDouble, &py)); + EXPECT_FAULT(cairo_get_current_point(context, &px, roDoubles)); + EXPECT_FAULT(cairo_get_current_point(context, badDouble, &py)); CHECK(Close(px, 77.0) && Close(py, 88.0)); cairo_get_current_point(context, &px, nullptr); CHECK(Close(px, 30.0)); @@ -314,14 +353,14 @@ int main() { double y1 = 92.0; double x2 = 93.0; double y2 = 94.0; - EXPECT_REJECTED(cairo_path_extents(context, &x1, &y1, &x2, roDoubles)); - EXPECT_REJECTED(cairo_path_extents(nullptr, &x1, nullptr, nullptr, roDoubles)); - EXPECT_REJECTED(cairo_fill_extents(context, &x1, &y1, &x2, badDouble)); - EXPECT_REJECTED(cairo_fill_extents(nullptr, &x1, nullptr, nullptr, roDoubles)); - EXPECT_REJECTED(cairo_stroke_extents(context, &x1, &y1, &x2, roDoubles)); - EXPECT_REJECTED(cairo_stroke_extents(nullptr, &x1, nullptr, nullptr, badDouble)); - EXPECT_REJECTED(cairo_clip_extents(context, &x1, &y1, &x2, roDoubles)); - EXPECT_REJECTED(cairo_clip_extents(nullptr, &x1, nullptr, nullptr, roDoubles)); + EXPECT_FAULT(cairo_path_extents(context, &x1, &y1, &x2, roDoubles)); + EXPECT_FAULT(cairo_path_extents(nullptr, &x1, nullptr, nullptr, roDoubles)); + EXPECT_FAULT(cairo_fill_extents(context, &x1, &y1, &x2, badDouble)); + EXPECT_FAULT(cairo_fill_extents(nullptr, &x1, nullptr, nullptr, roDoubles)); + EXPECT_FAULT(cairo_stroke_extents(context, &x1, &y1, &x2, roDoubles)); + EXPECT_FAULT(cairo_stroke_extents(nullptr, &x1, nullptr, nullptr, badDouble)); + EXPECT_FAULT(cairo_clip_extents(context, &x1, &y1, &x2, roDoubles)); + EXPECT_FAULT(cairo_clip_extents(nullptr, &x1, nullptr, nullptr, roDoubles)); CHECK(Close(x1, 91.0) && Close(y1, 92.0) && Close(x2, 93.0) && Close(y2, 94.0)); cairo_path_extents(context, &x1, &y1, &x2, &y2); @@ -336,5 +375,6 @@ int main() { LongDashes(); InvalidDashFromCairo(); UnreadableDashTail(); + ContextInError(); std::puts("guest Cairo context guest pointer tests passed"); } diff --git a/docs/dev/debt/changes/fix-cairo-context-guest-pointers.md b/docs/dev/debt/changes/fix-cairo-context-guest-pointers.md index 7e767d4b145c..fb8ef795c051 100644 --- a/docs/dev/debt/changes/fix-cairo-context-guest-pointers.md +++ b/docs/dev/debt/changes/fix-cairo-context-guest-pointers.md @@ -2,4 +2,4 @@ ## add Functional -- [Context guest pointer checks](../../../core/libs/prx/libcairo/Context.cpp) (libcairo) - `cairo_transform`, `cairo_set_matrix`, `cairo_get_matrix`, `cairo_user_to_device`, `cairo_user_to_device_distance`, `cairo_device_to_user`, `cairo_device_to_user_distance`, `cairo_set_dash`, `cairo_get_dash`, `cairo_get_current_point`, `cairo_path_extents`, `cairo_fill_extents`, `cairo_stroke_extents`, and `cairo_clip_extents` validate and transfer non-null guest matrices, coordinate pairs, dash arrays, current-point coordinates, and extent outputs through `GuestCopyIn_nid_no_patch`, `GuestWritable_nid_no_patch`, and `GuestCopyOut_nid_no_patch` while preserving `errno`, rejecting invalid or unwritable pointers via `NotImplemented_nid_no_patch(__func__)` before writing partial outputs. The coordinate transforms preserve aliased input/output pointers by using the same local value for both arguments. Cairo still reads a checked dash array in place, so unmapping it during the call faults the host; console behavior on faulted pointers is unverified. +- [Context guest pointer checks](../../../core/libs/prx/libcairo/Context.cpp) (libcairo) - console behaviour on faulted pointers is unmeasured. Cairo 1.18.0 dereferences them, so `GuestFault_nid_no_patch` covers: an unreadable `matrix` (`cairo_transform`, `cairo_set_matrix`), an unwritable one (`cairo_get_matrix`), an inaccessible coordinate (the four coordinate transforms), an unwritable non-null `dashes` or inaccessible non-null `offset` (`cairo_get_dash`), and an unwritable non-null output (`cairo_get_current_point`, the four extents calls), before partial outputs. On a context in error the transforms, `cairo_set_matrix`, `cairo_set_dash` and `cairo_get_dash` return first, as Cairo does. Unreadable `cairo_set_dash` arrays still call `NotImplemented_nid_no_patch`, as Cairo can stop before the unreadable element; Cairo reads a checked array in place, so unmapping it during the call faults the host. Coordinate transforms keep aliased pointers aliased.