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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. @@ -3468,6 +3468,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. @@ -493,6 +493,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. @@ -2604,6 +2604,9 @@ ImageData* BaseRenderingContext2D::getIm
  63. snapshot->PaintImageForCurrentFrame().GetSkImageInfo().bounds();
  64. DCHECK(!bounds.intersect(SkIRect::MakeXYWH(sx, sy, sw, sh)));
  65. }
  66. + if (read_pixels_successful && RuntimeEnabledFeatures::FingerprintingCanvasImageDataNoiseEnabled()) {
  67. + StaticBitmapImage::ShuffleSubchannelColorData(image_data_pixmap.addr(), image_data_pixmap.info(), sx, sy);
  68. + }
  69. }
  70. return image_data;
  71. --- a/third_party/blink/renderer/platform/BUILD.gn
  72. +++ b/third_party/blink/renderer/platform/BUILD.gn
  73. @@ -1685,7 +1685,9 @@ component("platform") {
  74. "//third_party/blink/renderer:non_test_config",
  75. ]
  76. - include_dirs = []
  77. + include_dirs = [
  78. + "//third_party/skia/include/private", # For shuffler in graphics/static_bitmap_image.cc
  79. + ]
  80. allow_circular_includes_from = [
  81. "//third_party/blink/renderer/platform/blob",
  82. --- a/third_party/blink/renderer/platform/exported/web_runtime_features.cc
  83. +++ b/third_party/blink/renderer/platform/exported/web_runtime_features.cc
  84. @@ -76,4 +76,8 @@ void WebRuntimeFeatures::EnableFingerpri
  85. RuntimeEnabledFeatures::SetFingerprintingCanvasMeasureTextNoiseEnabled(enable);
  86. }
  87. +void WebRuntimeFeatures::EnableFingerprintingCanvasImageDataNoise(bool enable) {
  88. + RuntimeEnabledFeatures::SetFingerprintingCanvasImageDataNoiseEnabled(enable);
  89. +}
  90. +
  91. } // namespace blink
  92. --- a/third_party/blink/renderer/platform/graphics/image_data_buffer.cc
  93. +++ b/third_party/blink/renderer/platform/graphics/image_data_buffer.cc
  94. @@ -36,6 +36,8 @@
  95. #include "base/compiler_specific.h"
  96. #include "base/memory/ptr_util.h"
  97. +#include "base/rand_util.h"
  98. +#include "base/logging.h"
  99. #include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h"
  100. #include "third_party/blink/renderer/platform/image-encoders/image_encoder.h"
  101. #include "third_party/blink/renderer/platform/runtime_enabled_features.h"
  102. @@ -146,6 +148,11 @@ bool ImageDataBuffer::EncodeImageInterna
  103. const SkPixmap& pixmap) const {
  104. DCHECK(is_valid_);
  105. + if (RuntimeEnabledFeatures::FingerprintingCanvasImageDataNoiseEnabled()) {
  106. + // shuffle subchannel color data within the pixmap
  107. + StaticBitmapImage::ShuffleSubchannelColorData(pixmap_.writable_addr(), pixmap_.info(), 0, 0);
  108. + }
  109. +
  110. if (mime_type == kMimeTypeJpeg) {
  111. SkJpegEncoder::Options options;
  112. options.fQuality = ImageEncoder::ComputeJpegQuality(quality);
  113. --- a/third_party/blink/renderer/platform/graphics/static_bitmap_image.cc
  114. +++ b/third_party/blink/renderer/platform/graphics/static_bitmap_image.cc
  115. @@ -4,6 +4,8 @@
  116. #include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h"
  117. +#include "base/rand_util.h"
  118. +#include "base/logging.h"
  119. #include "base/numerics/checked_math.h"
  120. #include "gpu/command_buffer/client/gles2_interface.h"
  121. #include "third_party/blink/renderer/platform/graphics/accelerated_static_bitmap_image.h"
  122. @@ -11,10 +13,12 @@
  123. #include "third_party/blink/renderer/platform/graphics/image_observer.h"
  124. #include "third_party/blink/renderer/platform/graphics/paint/paint_image.h"
  125. #include "third_party/blink/renderer/platform/graphics/unaccelerated_static_bitmap_image.h"
  126. +#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
  127. #include "third_party/skia/include/core/SkCanvas.h"
  128. #include "third_party/skia/include/core/SkImage.h"
  129. #include "third_party/skia/include/core/SkPaint.h"
  130. #include "third_party/skia/include/core/SkSurface.h"
  131. +#include "third_party/skia/include/private/SkColorData.h"
  132. #include "ui/gfx/geometry/skia_conversions.h"
  133. #include "v8/include/v8.h"
  134. @@ -115,4 +119,154 @@ void StaticBitmapImage::DrawHelper(cc::P
  135. WebCoreClampingModeToSkiaRectConstraint(draw_options.clamping_mode));
  136. }
  137. +// set the component to maximum-delta if it is >= maximum, or add to existing color component (color + delta)
  138. +#define shuffleComponent(color, max, delta) ( (color) >= (max) ? ((max)-(delta)) : ((color)+(delta)) )
  139. +
  140. +#define writable_addr(T, p, stride, x, y) (T*)((const char *)p + y * stride + x * sizeof(T))
  141. +
  142. +void StaticBitmapImage::ShuffleSubchannelColorData(const void *addr, const SkImageInfo& info, int srcX, int srcY) {
  143. + auto w = info.width() - srcX, h = info.height() - srcY;
  144. +
  145. + // skip tiny images; info.width()/height() can also be 0
  146. + if ((w < 8) || (h < 8)) {
  147. + return;
  148. + }
  149. +
  150. + // generate the first random number here
  151. + double shuffleX = base::RandDouble();
  152. +
  153. + // cap maximum pixels to change
  154. + auto pixels = (w + h) / 128;
  155. + if (pixels > 10) {
  156. + pixels = 10;
  157. + } else if (pixels < 2) {
  158. + pixels = 2;
  159. + }
  160. +
  161. + auto colorType = info.colorType();
  162. + auto fRowBytes = info.minRowBytes(); // stride
  163. +
  164. + DLOG(INFO) << "BRM: ShuffleSubchannelColorData() w=" << w << " h=" << h << " colorType=" << colorType << " fRowBytes=" << fRowBytes;
  165. +
  166. + // second random number (for y/height)
  167. + double shuffleY = base::RandDouble();
  168. +
  169. + // calculate random coordinates using bisection
  170. + auto currentW = w, currentH = h;
  171. + for(;pixels >= 0; pixels--) {
  172. + int x = currentW * shuffleX, y = currentH * shuffleY;
  173. +
  174. + // calculate randomisation amounts for each RGB component
  175. + uint8_t shuffleR = base::RandInt(0, 4);
  176. + uint8_t shuffleG = (shuffleR + x) % 4;
  177. + uint8_t shuffleB = (shuffleG + y) % 4;
  178. +
  179. + // manipulate pixel data to slightly change the R, G, B components
  180. + switch (colorType) {
  181. + case kAlpha_8_SkColorType:
  182. + {
  183. + auto *pixel = writable_addr(uint8_t, addr, fRowBytes, x, y);
  184. + auto r = SkColorGetR(*pixel), g = SkColorGetG(*pixel), b = SkColorGetB(*pixel), a = SkColorGetA(*pixel);
  185. +
  186. + r = shuffleComponent(r, UINT8_MAX-1, shuffleR);
  187. + g = shuffleComponent(g, UINT8_MAX-1, shuffleG);
  188. + b = shuffleComponent(b, UINT8_MAX-1, shuffleB);
  189. + // alpha is left unchanged
  190. +
  191. + *pixel = SkColorSetARGB(a, r, g, b);
  192. + }
  193. + break;
  194. + case kGray_8_SkColorType:
  195. + {
  196. + auto *pixel = writable_addr(uint8_t, addr, fRowBytes, x, y);
  197. + *pixel = shuffleComponent(*pixel, UINT8_MAX-1, shuffleB);
  198. + }
  199. + break;
  200. + case kRGB_565_SkColorType:
  201. + {
  202. + auto *pixel = writable_addr(uint16_t, addr, fRowBytes, x, y);
  203. + unsigned r = SkPacked16ToR32(*pixel);
  204. + unsigned g = SkPacked16ToG32(*pixel);
  205. + unsigned b = SkPacked16ToB32(*pixel);
  206. +
  207. + r = shuffleComponent(r, 31, shuffleR);
  208. + g = shuffleComponent(g, 63, shuffleG);
  209. + b = shuffleComponent(b, 31, shuffleB);
  210. +
  211. + unsigned r16 = (r & SK_R16_MASK) << SK_R16_SHIFT;
  212. + unsigned g16 = (g & SK_G16_MASK) << SK_G16_SHIFT;
  213. + unsigned b16 = (b & SK_B16_MASK) << SK_B16_SHIFT;
  214. +
  215. + *pixel = r16 | g16 | b16;
  216. + }
  217. + break;
  218. + case kARGB_4444_SkColorType:
  219. + {
  220. + auto *pixel = writable_addr(uint16_t, addr, fRowBytes, x, y);
  221. + auto a = SkGetPackedA4444(*pixel), r = SkGetPackedR4444(*pixel), g = SkGetPackedG4444(*pixel), b = SkGetPackedB4444(*pixel);
  222. +
  223. + r = shuffleComponent(r, 15, shuffleR);
  224. + g = shuffleComponent(g, 15, shuffleG);
  225. + b = shuffleComponent(b, 15, shuffleB);
  226. + // alpha is left unchanged
  227. +
  228. + unsigned a4 = (a & 0xF) << SK_A4444_SHIFT;
  229. + unsigned r4 = (r & 0xF) << SK_R4444_SHIFT;
  230. + unsigned g4 = (g & 0xF) << SK_G4444_SHIFT;
  231. + unsigned b4 = (b & 0xF) << SK_B4444_SHIFT;
  232. +
  233. + *pixel = r4 | b4 | g4 | a4;
  234. + }
  235. + break;
  236. + case kRGBA_8888_SkColorType:
  237. + {
  238. + auto *pixel = writable_addr(uint32_t, addr, fRowBytes, x, y);
  239. + auto a = SkGetPackedA32(*pixel), r = SkGetPackedR32(*pixel), g = SkGetPackedG32(*pixel), b = SkGetPackedB32(*pixel);
  240. +
  241. + r = shuffleComponent(r, UINT8_MAX-1, shuffleR);
  242. + g = shuffleComponent(g, UINT8_MAX-1, shuffleG);
  243. + b = shuffleComponent(b, UINT8_MAX-1, shuffleB);
  244. + // alpha is left unchanged
  245. +
  246. + *pixel = (a << SK_A32_SHIFT) | (r << SK_R32_SHIFT) |
  247. + (g << SK_G32_SHIFT) | (b << SK_B32_SHIFT);
  248. + }
  249. + break;
  250. + case kBGRA_8888_SkColorType:
  251. + {
  252. + auto *pixel = writable_addr(uint32_t, addr, fRowBytes, x, y);
  253. + auto a = SkGetPackedA32(*pixel), b = SkGetPackedR32(*pixel), g = SkGetPackedG32(*pixel), r = SkGetPackedB32(*pixel);
  254. +
  255. + r = shuffleComponent(r, UINT8_MAX-1, shuffleR);
  256. + g = shuffleComponent(g, UINT8_MAX-1, shuffleG);
  257. + b = shuffleComponent(b, UINT8_MAX-1, shuffleB);
  258. + // alpha is left unchanged
  259. +
  260. + *pixel = (a << SK_BGRA_A32_SHIFT) | (r << SK_BGRA_R32_SHIFT) |
  261. + (g << SK_BGRA_G32_SHIFT) | (b << SK_BGRA_B32_SHIFT);
  262. + }
  263. + break;
  264. + default:
  265. + // the remaining formats are not expected to be used in Chromium
  266. + LOG(WARNING) << "BRM: ShuffleSubchannelColorData(): Ignoring pixel format";
  267. + return;
  268. + }
  269. +
  270. + // keep bisecting or reset current width/height as needed
  271. + if (x == 0) {
  272. + currentW = w;
  273. + } else {
  274. + currentW = x;
  275. + }
  276. + if (y == 0) {
  277. + currentH = h;
  278. + } else {
  279. + currentH = y;
  280. + }
  281. + }
  282. +}
  283. +
  284. +#undef writable_addr
  285. +#undef shuffleComponent
  286. +
  287. } // namespace blink
  288. --- a/third_party/blink/renderer/platform/graphics/static_bitmap_image.h
  289. +++ b/third_party/blink/renderer/platform/graphics/static_bitmap_image.h
  290. @@ -37,6 +37,8 @@ class PLATFORM_EXPORT StaticBitmapImage
  291. StaticBitmapImage(ImageOrientation orientation) : orientation_(orientation) {}
  292. + static void ShuffleSubchannelColorData(const void *addr, const SkImageInfo& info, int srcX, int srcY);
  293. +
  294. bool IsStaticBitmapImage() const override { return true; }
  295. // Methods overridden by all sub-classes
  296. --- a/third_party/blink/renderer/platform/runtime_enabled_features.json5
  297. +++ b/third_party/blink/renderer/platform/runtime_enabled_features.json5
  298. @@ -1730,6 +1730,9 @@
  299. name: "FingerprintingCanvasMeasureTextNoise",
  300. },
  301. {
  302. + name: "FingerprintingCanvasImageDataNoise",
  303. + },
  304. + {
  305. name: "Fledge",
  306. base_feature: "none",
  307. origin_trial_feature_name: "PrivacySandboxAdsAPIs",