From 4f51a00c2e9dafdf853e6a3d940f26e534c8e6f1 Mon Sep 17 00:00:00 2001 From: Henri Sivonen Date: Mon, 22 Feb 2021 09:18:19 +0000 Subject: [PATCH] Bug 1693534 - Avoid leaking the kernel version to the Web with amdgpu. r=jgilbert Differential Revision: https://phabricator.services.mozilla.com/D105636 --- dom/canvas/ClientWebGLContext.cpp | 5 +- dom/canvas/SanitizeRenderer.h | 56 +++++++++++++++++++++ dom/canvas/gtest/TestSanitizeRenderer.cpp | 59 +++++++++++++++++++++++ dom/canvas/gtest/moz.build | 15 ++++++ dom/canvas/moz.build | 5 ++ 5 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 dom/canvas/SanitizeRenderer.h create mode 100644 dom/canvas/gtest/TestSanitizeRenderer.cpp create mode 100644 dom/canvas/gtest/moz.build diff --git a/dom/canvas/ClientWebGLContext.cpp b/dom/canvas/ClientWebGLContext.cpp index 338b9e000043..b940b69fd7bc 100644 --- a/dom/canvas/ClientWebGLContext.cpp +++ b/dom/canvas/ClientWebGLContext.cpp @@ -11,6 +11,7 @@ #include "HostWebGLContext.h" #include "js/ScalarType.h" // js::Scalar::Type #include "mozilla/dom/Document.h" +#include "mozilla/dom/SanitizeRenderer.h" #include "mozilla/dom/ToJSValue.h" #include "mozilla/dom/WebGLContextEvent.h" #include "mozilla/dom/WorkerCommon.h" @@ -2055,7 +2056,9 @@ void ClientWebGLContext::GetParameter(JSContext* cx, GLenum pname, const auto maybe = GetString(driverEnum); if (maybe) { - retval.set(StringValue(cx, *maybe, rv)); + std::string renderer = *maybe; + mozilla::dom::SanitizeRenderer(renderer); + retval.set(StringValue(cx, renderer, rv)); } return; } diff --git a/dom/canvas/SanitizeRenderer.h b/dom/canvas/SanitizeRenderer.h new file mode 100644 index 000000000000..1e7c57820266 --- /dev/null +++ b/dom/canvas/SanitizeRenderer.h @@ -0,0 +1,56 @@ +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_RendererSanitizer_h +#define mozilla_RendererSanitizer_h + +#include + +namespace mozilla { +namespace dom { + +inline static void SanitizeRenderer(std::string& aRenderer) { + // Remove DRM, kernel, and LLVM versions exposed by amdgpu. + // The string looks like this: + // + // AMD Radeon (TM) GPU model Graphics (GPUgeneration, DRM + // DRMversion, kernelversion, LLVM LLVMversion) + // + // e.g. AMD Radeon (TM) RX 460 Graphics (POLARIS11, + // DRM 3.35.0, 5.4.0-65-generic, LLVM 11.0.0) + // + // OR + // + // AMD Radeon GPU model (GPUgeneration, DRM DRMversion, kernelversion, LLVM + // LLVMversion) + // + // Just in case, let's handle the case without GPUgeneration, i.e. + // + // AMD Radeon GPU model (DRM DRMversion, kernelversion, LLVM LLVMversion) + // + // even though there's no existence proof of this variant. + if (aRenderer.empty()) { + return; + } + if (aRenderer.back() != ')') { + return; + } + auto pos = aRenderer.find(", DRM "); + if (pos != std::string::npos) { + aRenderer.resize(pos); + aRenderer.push_back(')'); + return; + } + pos = aRenderer.find(" (DRM "); + if (pos != std::string::npos) { + aRenderer.resize(pos); + return; + } +} + +}; // namespace dom +}; // namespace mozilla + +#endif // mozilla_RendererSanitizer_h diff --git a/dom/canvas/gtest/TestSanitizeRenderer.cpp b/dom/canvas/gtest/TestSanitizeRenderer.cpp new file mode 100644 index 000000000000..53455d912afd --- /dev/null +++ b/dom/canvas/gtest/TestSanitizeRenderer.cpp @@ -0,0 +1,59 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "gtest/gtest.h" +#include "mozilla/dom/SanitizeRenderer.h" + +TEST(SanitizeRenderer, TestRadeonTM) +{ + std::string renderer( + "AMD Radeon (TM) RX 460 Graphics (POLARIS11, DRM 3.35.0, " + "5.4.0-65-generic, LLVM 11.0.0)"); + std::string expectation("AMD Radeon (TM) RX 460 Graphics (POLARIS11)"); + mozilla::dom::SanitizeRenderer(renderer); + EXPECT_EQ(renderer, expectation); +} + +TEST(SanitizeRenderer, TestRadeon) +{ + std::string renderer( + "AMD Radeon RX 5700 (NAVI10, DRM 3.35.0, 5.4.0-65-generic, LLVM 11.0.0)"); + std::string expectation("AMD Radeon RX 5700 (NAVI10)"); + mozilla::dom::SanitizeRenderer(renderer); + EXPECT_EQ(renderer, expectation); +} + +TEST(SanitizeRenderer, TestRadeonWithoutGeneration) +{ + std::string renderer( + "AMD Radeon RX 5700 (DRM 3.35.0, 5.4.0-65-generic, LLVM 11.0.0)"); + std::string expectation("AMD Radeon RX 5700"); + mozilla::dom::SanitizeRenderer(renderer); + EXPECT_EQ(renderer, expectation); +} + +TEST(SanitizeRenderer, TestVega) +{ + std::string renderer("Radeon RX Vega"); + std::string expectation("Radeon RX Vega"); + mozilla::dom::SanitizeRenderer(renderer); + EXPECT_EQ(renderer, expectation); +} + +TEST(SanitizeRenderer, TestIntel) +{ + std::string renderer("Mesa DRI Intel(R) HD Graphics 4000 (IVB GT2)"); + std::string expectation("Mesa DRI Intel(R) HD Graphics 4000 (IVB GT2)"); + mozilla::dom::SanitizeRenderer(renderer); + EXPECT_EQ(renderer, expectation); +} + +TEST(SanitizeRenderer, TestPipe) +{ + std::string renderer("llvmpipe (LLVM 11.0.0, 256 bits)"); + std::string expectation("llvmpipe (LLVM 11.0.0, 256 bits)"); + mozilla::dom::SanitizeRenderer(renderer); + EXPECT_EQ(renderer, expectation); +} diff --git a/dom/canvas/gtest/moz.build b/dom/canvas/gtest/moz.build new file mode 100644 index 000000000000..1e593a5f82ae --- /dev/null +++ b/dom/canvas/gtest/moz.build @@ -0,0 +1,15 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +LOCAL_INCLUDES += [ + "/dom/canvas", +] + +UNIFIED_SOURCES += [ + "TestSanitizeRenderer.cpp", +] + +FINAL_LIBRARY = "xul-gtest" diff --git a/dom/canvas/moz.build b/dom/canvas/moz.build index b3feac7bce8d..f29d88029295 100644 --- a/dom/canvas/moz.build +++ b/dom/canvas/moz.build @@ -62,6 +62,7 @@ EXPORTS.mozilla.dom += [ "IpdlQueue.h", "OffscreenCanvas.h", "QueueParamTraits.h", + "SanitizeRenderer.h", "TextMetrics.h", "WebGLChild.h", "WebGLCommandQueue.h", @@ -177,6 +178,10 @@ IPDL_SOURCES += [ "PWebGL.ipdl", ] +TEST_DIRS += [ + "gtest", +] + # Suppress warnings from third-party code. if CONFIG["CC_TYPE"] in ("clang", "gcc"): SOURCES["MurmurHash3.cpp"].flags += ["-Wno-implicit-fallthrough"]