flag-fingerprinting-canvas-image-data-noise.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. # NOTE: Changes made:
  2. # * Added flag --fingerprinting-canvas-image-data-noise to enable/disable
  3. # Canvas image data fingerprinting deception
  4. # * Removed WebGLDebugRendererInfo disabling in favor of an alternative
  5. # implementation in ungoogled-chromium/disable-webgl-renderer-info.patch
  6. # * Tweak subchannel noise generation to require fewer random number generation
  7. From: csagan5 <32685696+csagan5@users.noreply.github.com>
  8. Date: Sat, 24 Mar 2018 05:18:03 +0100
  9. Subject: Canvas: fingerprinting mitigations for image data and webGL
  10. Disable webGL renderer info and modify the color data returned by ToBlob,
  11. ToDataURL and getImageData so that it will contain randomly manipulated
  12. pixels (maximum 10) that slightly change the color of the R,G,B components
  13. without a visible effect.
  14. Credits to Slaviro (https://github.com/Slaviro) for coming up with a better
  15. approach to change color components.
  16. ---
  17. .../platform/graphics/image_data_buffer.cc | 5 +
  18. .../platform/graphics/static_bitmap_image.cc | 154 +++++++++++++++++++++
  19. .../platform/graphics/static_bitmap_image.h | 2 +
  20. 4 files changed, 163 insertions(+), 2 deletions(-)
  21. --- a/chrome/browser/bromite_flag_entries.h
  22. +++ b/chrome/browser/bromite_flag_entries.h
  23. @@ -16,4 +16,8 @@
  24. flag_descriptions::kMaxConnectionsPerHostName,
  25. flag_descriptions::kMaxConnectionsPerHostDescription,
  26. kOsAll, MULTI_VALUE_TYPE(kMaxConnectionsPerHostChoices)},
  27. + {"fingerprinting-canvas-image-data-noise",
  28. + "Enable Canvas image data fingerprint deception",
  29. + "Slightly modifies at most 10 pixels in Canvas image data extracted via JS APIs. ungoogled-chromium flag, Bromite feature.",
  30. + kOsAll, SINGLE_VALUE_TYPE(switches::kFingerprintingCanvasImageDataNoise)},
  31. #endif // CHROME_BROWSER_BROMITE_FLAG_ENTRIES_H_
  32. --- a/content/browser/renderer_host/render_process_host_impl.cc
  33. +++ b/content/browser/renderer_host/render_process_host_impl.cc
  34. @@ -3423,6 +3423,7 @@ void RenderProcessHostImpl::PropagateBro
  35. switches::kFileUrlPathAlias,
  36. switches::kFingerprintingClientRectsNoise,
  37. switches::kFingerprintingCanvasMeasureTextNoise,
  38. + switches::kFingerprintingCanvasImageDataNoise,
  39. switches::kForceDeviceScaleFactor,
  40. switches::kForceDisplayColorProfile,
  41. switches::kForceGpuMemAvailableMb,
  42. --- a/content/child/runtime_features.cc
  43. +++ b/content/child/runtime_features.cc
  44. @@ -480,6 +480,8 @@ void SetRuntimeFeaturesFromCommandLine(c
  45. switches::kFingerprintingClientRectsNoise, true},
  46. {wrf::EnableFingerprintingCanvasMeasureTextNoise,
  47. switches::kFingerprintingCanvasMeasureTextNoise, true},
  48. + {wrf::EnableFingerprintingCanvasImageDataNoise,
  49. + switches::kFingerprintingCanvasImageDataNoise, true},
  50. };
  51. for (const auto& mapping : switchToFeatureMapping) {
  52. --- a/third_party/blink/public/platform/web_runtime_features.h
  53. +++ b/third_party/blink/public/platform/web_runtime_features.h
  54. @@ -71,6 +71,7 @@ class BLINK_PLATFORM_EXPORT WebRuntimeFe
  55. static void EnableFingerprintingClientRectsNoise(bool);
  56. static void EnableFingerprintingCanvasMeasureTextNoise(bool);
  57. + static void EnableFingerprintingCanvasImageDataNoise(bool);
  58. WebRuntimeFeatures() = delete;
  59. };
  60. --- a/third_party/blink/renderer/modules/canvas/canvas2d/base_rendering_context_2d.cc
  61. +++ b/third_party/blink/renderer/modules/canvas/canvas2d/base_rendering_context_2d.cc
  62. @@ -48,6 +48,7 @@
  63. #include "third_party/blink/renderer/platform/graphics/filters/paint_filter_builder.h"
  64. #include "third_party/blink/renderer/platform/graphics/graphics_context.h"
  65. #include "third_party/blink/renderer/platform/graphics/skia/skia_utils.h"
  66. +#include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h"
  67. #include "third_party/blink/renderer/platform/graphics/stroke_data.h"
  68. #include "third_party/blink/renderer/platform/graphics/video_frame_image_util.h"
  69. #include "third_party/blink/renderer/platform/heap/garbage_collected.h"
  70. @@ -2211,6 +2212,9 @@ ImageData* BaseRenderingContext2D::getIm
  71. snapshot->PaintImageForCurrentFrame().GetSkImageInfo().bounds();
  72. DCHECK(!bounds.intersect(SkIRect::MakeXYWH(sx, sy, sw, sh)));
  73. }
  74. + if (read_pixels_successful && RuntimeEnabledFeatures::FingerprintingCanvasImageDataNoiseEnabled()) {
  75. + StaticBitmapImage::ShuffleSubchannelColorData(image_data_pixmap.addr(), image_data_pixmap.info(), sx, sy);
  76. + }
  77. }
  78. return image_data;
  79. --- a/third_party/blink/renderer/platform/BUILD.gn
  80. +++ b/third_party/blink/renderer/platform/BUILD.gn
  81. @@ -1657,7 +1657,9 @@ component("platform") {
  82. "//third_party/blink/renderer:non_test_config",
  83. ]
  84. - include_dirs = []
  85. + include_dirs = [
  86. + "//third_party/skia/include/private", # For shuffler in graphics/static_bitmap_image.cc
  87. + ]
  88. allow_circular_includes_from = [
  89. "//third_party/blink/renderer/platform/blob",
  90. --- a/third_party/blink/renderer/platform/exported/web_runtime_features.cc
  91. +++ b/third_party/blink/renderer/platform/exported/web_runtime_features.cc
  92. @@ -76,4 +76,8 @@ void WebRuntimeFeatures::EnableFingerpri
  93. RuntimeEnabledFeatures::SetFingerprintingCanvasMeasureTextNoiseEnabled(enable);
  94. }
  95. +void WebRuntimeFeatures::EnableFingerprintingCanvasImageDataNoise(bool enable) {
  96. + RuntimeEnabledFeatures::SetFingerprintingCanvasImageDataNoiseEnabled(enable);
  97. +}
  98. +
  99. } // namespace blink
  100. --- a/third_party/blink/renderer/platform/graphics/image_data_buffer.cc
  101. +++ b/third_party/blink/renderer/platform/graphics/image_data_buffer.cc
  102. @@ -36,6 +36,8 @@
  103. #include "base/compiler_specific.h"
  104. #include "base/memory/ptr_util.h"
  105. +#include "base/rand_util.h"
  106. +#include "base/logging.h"
  107. #include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h"
  108. #include "third_party/blink/renderer/platform/image-encoders/image_encoder.h"
  109. #include "third_party/blink/renderer/platform/runtime_enabled_features.h"
  110. @@ -146,6 +148,11 @@ bool ImageDataBuffer::EncodeImageInterna
  111. const SkPixmap& pixmap) const {
  112. DCHECK(is_valid_);
  113. + if (RuntimeEnabledFeatures::FingerprintingCanvasImageDataNoiseEnabled()) {
  114. + // shuffle subchannel color data within the pixmap
  115. + StaticBitmapImage::ShuffleSubchannelColorData(pixmap_.writable_addr(), pixmap_.info(), 0, 0);
  116. + }
  117. +
  118. if (mime_type == kMimeTypeJpeg) {
  119. SkJpegEncoder::Options options;
  120. options.fQuality = ImageEncoder::ComputeJpegQuality(quality);
  121. --- a/third_party/blink/renderer/platform/graphics/static_bitmap_image.cc
  122. +++ b/third_party/blink/renderer/platform/graphics/static_bitmap_image.cc
  123. @@ -4,6 +4,8 @@
  124. #include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h"
  125. +#include "base/rand_util.h"
  126. +#include "base/logging.h"
  127. #include "base/numerics/checked_math.h"
  128. #include "gpu/command_buffer/client/gles2_interface.h"
  129. #include "third_party/blink/renderer/platform/graphics/accelerated_static_bitmap_image.h"
  130. @@ -11,10 +13,12 @@
  131. #include "third_party/blink/renderer/platform/graphics/image_observer.h"
  132. #include "third_party/blink/renderer/platform/graphics/paint/paint_image.h"
  133. #include "third_party/blink/renderer/platform/graphics/unaccelerated_static_bitmap_image.h"
  134. +#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
  135. #include "third_party/skia/include/core/SkCanvas.h"
  136. #include "third_party/skia/include/core/SkImage.h"
  137. #include "third_party/skia/include/core/SkPaint.h"
  138. #include "third_party/skia/include/core/SkSurface.h"
  139. +#include "third_party/skia/include/private/SkColorData.h"
  140. #include "ui/gfx/geometry/skia_conversions.h"
  141. #include "v8/include/v8.h"
  142. @@ -115,4 +119,154 @@ void StaticBitmapImage::DrawHelper(cc::P
  143. WebCoreClampingModeToSkiaRectConstraint(draw_options.clamping_mode));
  144. }
  145. +// set the component to maximum-delta if it is >= maximum, or add to existing color component (color + delta)
  146. +#define shuffleComponent(color, max, delta) ( (color) >= (max) ? ((max)-(delta)) : ((color)+(delta)) )
  147. +
  148. +#define writable_addr(T, p, stride, x, y) (T*)((const char *)p + y * stride + x * sizeof(T))
  149. +
  150. +void StaticBitmapImage::ShuffleSubchannelColorData(const void *addr, const SkImageInfo& info, int srcX, int srcY) {
  151. + auto w = info.width() - srcX, h = info.height() - srcY;
  152. +
  153. + // skip tiny images; info.width()/height() can also be 0
  154. + if ((w < 8) || (h < 8)) {
  155. + return;
  156. + }
  157. +
  158. + // generate the first random number here
  159. + double shuffleX = base::RandDouble();
  160. +
  161. + // cap maximum pixels to change
  162. + auto pixels = (w + h) / 128;
  163. + if (pixels > 10) {
  164. + pixels = 10;
  165. + } else if (pixels < 2) {
  166. + pixels = 2;
  167. + }
  168. +
  169. + auto colorType = info.colorType();
  170. + auto fRowBytes = info.minRowBytes(); // stride
  171. +
  172. + DLOG(INFO) << "BRM: ShuffleSubchannelColorData() w=" << w << " h=" << h << " colorType=" << colorType << " fRowBytes=" << fRowBytes;
  173. +
  174. + // second random number (for y/height)
  175. + double shuffleY = base::RandDouble();
  176. +
  177. + // calculate random coordinates using bisection
  178. + auto currentW = w, currentH = h;
  179. + for(;pixels >= 0; pixels--) {
  180. + int x = currentW * shuffleX, y = currentH * shuffleY;
  181. +
  182. + // calculate randomisation amounts for each RGB component
  183. + uint8_t shuffleR = base::RandInt(0, 4);
  184. + uint8_t shuffleG = (shuffleR + x) % 4;
  185. + uint8_t shuffleB = (shuffleG + y) % 4;
  186. +
  187. + // manipulate pixel data to slightly change the R, G, B components
  188. + switch (colorType) {
  189. + case kAlpha_8_SkColorType:
  190. + {
  191. + auto *pixel = writable_addr(uint8_t, addr, fRowBytes, x, y);
  192. + auto r = SkColorGetR(*pixel), g = SkColorGetG(*pixel), b = SkColorGetB(*pixel), a = SkColorGetA(*pixel);
  193. +
  194. + r = shuffleComponent(r, UINT8_MAX-1, shuffleR);
  195. + g = shuffleComponent(g, UINT8_MAX-1, shuffleG);
  196. + b = shuffleComponent(b, UINT8_MAX-1, shuffleB);
  197. + // alpha is left unchanged
  198. +
  199. + *pixel = SkColorSetARGB(a, r, g, b);
  200. + }
  201. + break;
  202. + case kGray_8_SkColorType:
  203. + {
  204. + auto *pixel = writable_addr(uint8_t, addr, fRowBytes, x, y);
  205. + *pixel = shuffleComponent(*pixel, UINT8_MAX-1, shuffleB);
  206. + }
  207. + break;
  208. + case kRGB_565_SkColorType:
  209. + {
  210. + auto *pixel = writable_addr(uint16_t, addr, fRowBytes, x, y);
  211. + unsigned r = SkPacked16ToR32(*pixel);
  212. + unsigned g = SkPacked16ToG32(*pixel);
  213. + unsigned b = SkPacked16ToB32(*pixel);
  214. +
  215. + r = shuffleComponent(r, 31, shuffleR);
  216. + g = shuffleComponent(g, 63, shuffleG);
  217. + b = shuffleComponent(b, 31, shuffleB);
  218. +
  219. + unsigned r16 = (r & SK_R16_MASK) << SK_R16_SHIFT;
  220. + unsigned g16 = (g & SK_G16_MASK) << SK_G16_SHIFT;
  221. + unsigned b16 = (b & SK_B16_MASK) << SK_B16_SHIFT;
  222. +
  223. + *pixel = r16 | g16 | b16;
  224. + }
  225. + break;
  226. + case kARGB_4444_SkColorType:
  227. + {
  228. + auto *pixel = writable_addr(uint16_t, addr, fRowBytes, x, y);
  229. + auto a = SkGetPackedA4444(*pixel), r = SkGetPackedR4444(*pixel), g = SkGetPackedG4444(*pixel), b = SkGetPackedB4444(*pixel);
  230. +
  231. + r = shuffleComponent(r, 15, shuffleR);
  232. + g = shuffleComponent(g, 15, shuffleG);
  233. + b = shuffleComponent(b, 15, shuffleB);
  234. + // alpha is left unchanged
  235. +
  236. + unsigned a4 = (a & 0xF) << SK_A4444_SHIFT;
  237. + unsigned r4 = (r & 0xF) << SK_R4444_SHIFT;
  238. + unsigned g4 = (g & 0xF) << SK_G4444_SHIFT;
  239. + unsigned b4 = (b & 0xF) << SK_B4444_SHIFT;
  240. +
  241. + *pixel = r4 | b4 | g4 | a4;
  242. + }
  243. + break;
  244. + case kRGBA_8888_SkColorType:
  245. + {
  246. + auto *pixel = writable_addr(uint32_t, addr, fRowBytes, x, y);
  247. + auto a = SkGetPackedA32(*pixel), r = SkGetPackedR32(*pixel), g = SkGetPackedG32(*pixel), b = SkGetPackedB32(*pixel);
  248. +
  249. + r = shuffleComponent(r, UINT8_MAX-1, shuffleR);
  250. + g = shuffleComponent(g, UINT8_MAX-1, shuffleG);
  251. + b = shuffleComponent(b, UINT8_MAX-1, shuffleB);
  252. + // alpha is left unchanged
  253. +
  254. + *pixel = (a << SK_A32_SHIFT) | (r << SK_R32_SHIFT) |
  255. + (g << SK_G32_SHIFT) | (b << SK_B32_SHIFT);
  256. + }
  257. + break;
  258. + case kBGRA_8888_SkColorType:
  259. + {
  260. + auto *pixel = writable_addr(uint32_t, addr, fRowBytes, x, y);
  261. + auto a = SkGetPackedA32(*pixel), b = SkGetPackedR32(*pixel), g = SkGetPackedG32(*pixel), r = SkGetPackedB32(*pixel);
  262. +
  263. + r = shuffleComponent(r, UINT8_MAX-1, shuffleR);
  264. + g = shuffleComponent(g, UINT8_MAX-1, shuffleG);
  265. + b = shuffleComponent(b, UINT8_MAX-1, shuffleB);
  266. + // alpha is left unchanged
  267. +
  268. + *pixel = (a << SK_BGRA_A32_SHIFT) | (r << SK_BGRA_R32_SHIFT) |
  269. + (g << SK_BGRA_G32_SHIFT) | (b << SK_BGRA_B32_SHIFT);
  270. + }
  271. + break;
  272. + default:
  273. + // the remaining formats are not expected to be used in Chromium
  274. + LOG(WARNING) << "BRM: ShuffleSubchannelColorData(): Ignoring pixel format";
  275. + return;
  276. + }
  277. +
  278. + // keep bisecting or reset current width/height as needed
  279. + if (x == 0) {
  280. + currentW = w;
  281. + } else {
  282. + currentW = x;
  283. + }
  284. + if (y == 0) {
  285. + currentH = h;
  286. + } else {
  287. + currentH = y;
  288. + }
  289. + }
  290. +}
  291. +
  292. +#undef writable_addr
  293. +#undef shuffleComponent
  294. +
  295. } // namespace blink
  296. --- a/third_party/blink/renderer/platform/graphics/static_bitmap_image.h
  297. +++ b/third_party/blink/renderer/platform/graphics/static_bitmap_image.h
  298. @@ -37,6 +37,8 @@ class PLATFORM_EXPORT StaticBitmapImage
  299. StaticBitmapImage(ImageOrientation orientation) : orientation_(orientation) {}
  300. + static void ShuffleSubchannelColorData(const void *addr, const SkImageInfo& info, int srcX, int srcY);
  301. +
  302. bool IsStaticBitmapImage() const override { return true; }
  303. // Methods overridden by all sub-classes
  304. --- a/third_party/blink/renderer/platform/runtime_enabled_features.json5
  305. +++ b/third_party/blink/renderer/platform/runtime_enabled_features.json5
  306. @@ -1893,6 +1893,9 @@
  307. name: "FingerprintingCanvasMeasureTextNoise",
  308. },
  309. {
  310. + name: "FingerprintingCanvasImageDataNoise",
  311. + },
  312. + {
  313. name: "FocuslessSpatialNavigation",
  314. base_feature: "none",
  315. settable_from_internals: true,