1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "Fuzzyfox.h"
#include "mozilla/Logging.h"
#include "mozilla/Preferences.h"
#include "mozilla/SchedulerGroup.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPrefs_privacy.h"
#include "mozilla/TimeStamp.h"
#include "nsComponentManagerUtils.h"
#include "nsIPrefBranch.h"
#include "nsServiceManagerUtils.h"
#include "prrng.h"
#include "prtime.h"
// For usleep/Sleep & QueryPerformanceFrequency
#ifdef XP_WIN
# include <windows.h>
#else
# include <unistd.h>
#endif
using namespace mozilla;
static LazyLogModule sFuzzyfoxLog("Fuzzyfox");
#define US_TO_NS(x) ((x)*1000)
#define NS_TO_US(x) ((x) / 1000)
#ifdef LOG
# undef LOG
#endif
#define LOG(level, args) MOZ_LOG(sFuzzyfoxLog, mozilla::LogLevel::level, args)
#define FUZZYFOX_ENABLED_PREF "privacy.fuzzyfox.enabled"
#define FUZZYFOX_ENABLED_PREF_DEFAULT false
#define FUZZYFOX_CLOCKGRAIN_PREF "privacy.fuzzyfox.clockgrainus"
static bool sFuzzyfoxInitializing;
NS_IMPL_ISUPPORTS_INHERITED(Fuzzyfox, Runnable, nsIObserver)
/* static */
void Fuzzyfox::Start() {
MOZ_ASSERT(NS_IsMainThread());
RefPtr<Fuzzyfox> r = new Fuzzyfox();
SchedulerGroup::Dispatch(TaskCategory::Other, r.forget());
}
Fuzzyfox::Fuzzyfox()
: Runnable("Fuzzyfox"),
mSanityCheck(false),
mStartTime(0),
mDuration(PickDuration()),
mTickType(eUptick) {
MOZ_ASSERT(NS_IsMainThread());
// [[ I originally ran this after observing profile-after-change, but
// it turned out that this contructor was getting called _after_ that
// event had already fired. ]]
bool fuzzyfoxEnabled = Preferences::GetBool(FUZZYFOX_ENABLED_PREF,
FUZZYFOX_ENABLED_PREF_DEFAULT);
LOG(Info, ("PT(%p) Created Fuzzyfox, FuzzyFox is now %s \n", this,
(fuzzyfoxEnabled ? "initializing" : "disabled")));
sFuzzyfoxInitializing = fuzzyfoxEnabled;
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
if (!prefs) {
// Sometimes the call to the pref service fails. If that happens, we
// don't add the observers. The user will have to restart to have
// preference changes take effect.
return;
}
prefs->AddObserver(FUZZYFOX_ENABLED_PREF, this, false);
prefs->AddObserver(FUZZYFOX_CLOCKGRAIN_PREF, this, false);
}
Fuzzyfox::~Fuzzyfox() = default;
/*
* Fuzzyfox::Run is the core of FuzzyFox. Every so often we pop into this
* method, and pick a new point in the future to hold time constant until. If we
* have not reached the _previous_ point in time we had picked, we sleep until
* we do so. Then we round the current time downwards to a configurable grain
* value, fix it in place so time does not advance, and let execution continue.
*/
NS_IMETHODIMP
Fuzzyfox::Observe(nsISupports* aObject, const char* aTopic,
const char16_t* aMessage) {
if (!strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic)) {
NS_ConvertUTF16toUTF8 pref(aMessage);
if (pref.EqualsLiteral(FUZZYFOX_ENABLED_PREF)) {
bool fuzzyfoxEnabled = Preferences::GetBool(
FUZZYFOX_ENABLED_PREF, FUZZYFOX_ENABLED_PREF_DEFAULT);
LOG(Info, ("PT(%p) Observed a pref change, FuzzyFox is now %s \n", this,
(fuzzyfoxEnabled ? "initializing" : "disabled")));
sFuzzyfoxInitializing = fuzzyfoxEnabled;
if (sFuzzyfoxInitializing) {
// Queue a runnable
nsCOMPtr<nsIRunnable> r = this;
SchedulerGroup::Dispatch(TaskCategory::Other, r.forget());
} else {
mStartTime = 0;
mTickType = eUptick;
mSanityCheck = false;
TimeStamp::SetFuzzyfoxEnabled(false);
}
}
}
return NS_OK;
}
#define DISPATCH_AND_RETURN() \
nsCOMPtr<nsIRunnable> r = this; \
SchedulerGroup::Dispatch(TaskCategory::Other, r.forget()); \
return NS_OK
NS_IMETHODIMP
Fuzzyfox::Run() {
MOZ_ASSERT(NS_IsMainThread());
if (!sFuzzyfoxInitializing && !TimeStamp::GetFuzzyfoxEnabled()) {
LOG(Info, ("[FuzzyfoxEvent] PT(%p) Fuzzyfox is shut down, doing nothing \n",
this));
return NS_OK;
}
if (sFuzzyfoxInitializing) {
// This is the first time we are running afer enabling FuzzyFox. We need
// to prevent time from going backwards, so for the first run we round the
// time up to the next grain.
mStartTime = CeilToGrain(ActualTime());
MOZ_ASSERT(mStartTime != 0);
TimeStamp newTimeStamp = CeilToGrain(TimeStamp::NowUnfuzzed());
Fuzzyfox::UpdateClocks(mStartTime, newTimeStamp);
mSanityCheck = true;
LOG(Info, ("[FuzzyfoxEvent] PT(%p) Going to start Fuzzyfox, queuing up the "
"job \n",
this));
// Now that we've updated the timestamps, we can let everyone know Fuzzyfox
// is ready to use
TimeStamp::SetFuzzyfoxEnabled(true);
sFuzzyfoxInitializing = false;
DISPATCH_AND_RETURN();
}
// We need to check how long its been since we ran
uint64_t endTime = ActualTime();
uint64_t remaining = 0;
// Pick the amount to sleep
if (endTime < mStartTime) {
// This can only happen if we just enabled FuzzyFox, rounded up, and then
// re-ran the runnable before we advanced to the next grain. If that
// happens, then repeat the current time. We use mSanityCheck just to be
// sure (and will eventually remove it.)
MOZ_ASSERT(mSanityCheck);
LOG(Warning, ("[FuzzyfoxEvent] Unusual!! PT(%p) endTime < mStartTime "
"mStartTime %" PRIu64 " endTime %" PRIu64 " \n",
this, mStartTime, endTime));
mSanityCheck = true;
DISPATCH_AND_RETURN();
}
uint64_t actualRunDuration = endTime - mStartTime;
LOG(Verbose,
("[FuzzyfoxEvent] PT(%p) mDuration: %" PRIu32 " endTime: %" PRIu64
" mStartTime: %" PRIu64 " actualRunDuration: %" PRIu64 " \n",
this, mDuration, endTime, mStartTime, actualRunDuration));
if (actualRunDuration > mDuration) {
// We ran over our budget!
uint64_t over = actualRunDuration - mDuration;
LOG(Debug, ("[FuzzyfoxEvent] PT(%p) Overran budget of %" PRIu32
" by %" PRIu64 " \n",
this, mDuration, over));
uint64_t nextDuration = PickDuration();
while (over > nextDuration) {
over -= nextDuration;
nextDuration = PickDuration();
mTickType = mTickType == eUptick ? eDowntick : eUptick;
}
remaining = nextDuration - over;
} else {
// Didn't go over budget
remaining = mDuration - actualRunDuration;
LOG(Debug, ("[FuzzyfoxEvent] PT(%p) Finishing budget of %" PRIu32
" with %" PRIu64 " \n",
this, mDuration, remaining));
}
mSanityCheck = false;
// Sleep for now
#ifdef XP_WIN
Sleep(remaining);
#else
usleep(remaining);
#endif
/*
* Update clocks (and fire pending events etc)
*
* Note: Anytime we round the current time to the grain, and then round the
* 'real' time to the grain, we are introducing the risk that we split the
* grain. That is, the time advances enough after the first rounding that the
* second rounding causes us to move to a different grain.
*
* In theory, such an occurance breaks the security of FuzzyFox, and if an
* attacker can influence the event to occur reliably, and then measure
* against it they can attack FuzzyFox. But such an attack is so difficult
* that it will never be acheived until you read this comment in a future
* Academic Publication that demonstrates it. And at that point the paper
* would surely never be accepted into any _respectable_ journal unless the
* authors had also presented a solution for the issue that was usable and
* incorporated into Firefox!
*/
uint64_t newTime = FloorToGrain(ActualTime());
TimeStamp newTimeStamp = FloorToGrain(TimeStamp::NowUnfuzzed());
UpdateClocks(newTime, newTimeStamp);
// Reset values
mTickType = mTickType == eUptick ? eDowntick : eUptick;
mStartTime = ActualTime();
mDuration = PickDuration();
LOG(Verbose, ("[FuzzyfoxEvent] PT(%p) For next time mDuration: %" PRIu32
" mStartTime: %" PRIu64 " \n",
this, mDuration, mStartTime));
DISPATCH_AND_RETURN();
}
/*
* ActualTime returns the unfuzzed/unrounded time in microseconds since the
* epoch
*/
uint64_t Fuzzyfox::ActualTime() { return PR_Now(); }
/*
* Calculate a duration we will wait until we allow time to advance again
*/
uint64_t Fuzzyfox::PickDuration() {
// TODO: Bug 1484298 - use a real RNG
long int rval = rand();
// Avoid divide by zero errors and overflow errors
uint32_t duration =
std::max((uint32_t)1, StaticPrefs::privacy_fuzzyfox_clockgrainus());
duration = duration >= (UINT32_MAX / 2) ? (UINT32_MAX / 2) : duration;
// We want uniform distribution from 1->duration*2
// so that the mean is duration
return 1 + (rval % (duration * 2));
}
/*
* Update the TimeStamp's class value for the current (constant) time and
* dispatch the new (constant) timestamp so observers can register to receive it
* to update their own time code.
*/
void Fuzzyfox::UpdateClocks(uint64_t aNewTime, TimeStamp aNewTimeStamp) {
// newTime is the new canonical time for this scope!
#ifndef XP_WIN
LOG(Debug, ("[Time] New time is %" PRIu64 " (compare to %" PRIu64
") and timestamp is %" PRIu64 " (compare to %" PRIu64 ")\n",
aNewTime, ActualTime(), aNewTimeStamp.mValue.mTimeStamp,
TimeStamp::NowUnfuzzed().mValue.mTimeStamp));
#else
LOG(Debug, ("[Time] New time is %" PRIu64 " (compare to %" PRIu64 ") \n",
aNewTime, ActualTime()));
#endif
// Fire notifications
if (MOZ_UNLIKELY(!mObs)) {
mObs = services::GetObserverService();
if (NS_WARN_IF(!mObs)) {
return;
}
}
// Event firings on occur on downticks and have no data
if (mTickType == eDowntick) {
mObs->NotifyObservers(nullptr, FUZZYFOX_FIREOUTBOUND_OBSERVER_TOPIC,
nullptr);
}
if (!mTimeUpdateWrapper) {
mTimeUpdateWrapper = do_CreateInstance(NS_SUPPORTS_PRINT64_CONTRACTID);
if (NS_WARN_IF(!mTimeUpdateWrapper)) {
return;
}
}
mTimeUpdateWrapper->SetData(aNewTime);
// Clocks get the new official (frozen) time. This happens on all ticks
mObs->NotifyObservers(mTimeUpdateWrapper, FUZZYFOX_UPDATECLOCK_OBSERVER_TOPIC,
nullptr);
// Update the timestamp's canonicaltimes
TimeStamp::UpdateFuzzyTime(aNewTime);
TimeStamp::UpdateFuzzyTimeStamp(aNewTimeStamp);
}
/*
* FloorToGrain accepts a timestamp in microsecond precision
* and returns it in microseconds, rounded down to the nearest
* ClockGrain value.
*/
uint64_t Fuzzyfox::FloorToGrain(uint64_t aValue) {
return aValue - (aValue % StaticPrefs::privacy_fuzzyfox_clockgrainus());
}
/*
* FloorToGrain accepts a timestamp and returns it, rounded down
* to the nearest ClockGrain value.
*/
TimeStamp Fuzzyfox::FloorToGrain(TimeStamp aValue) {
#ifdef XP_WIN
// grain is in us
uint64_t grain = StaticPrefs::privacy_fuzzyfox_clockgrainus();
// GTC and QPS are stored in |mt| and need to be converted to
uint64_t GTC = mt2ms(aValue.mValue.mGTC) * 1000;
uint64_t QPC = mt2ms(aValue.mValue.mQPC) * 1000;
return TimeStamp(TimeStampValue(ms2mt((GTC - (GTC % grain)) / 1000),
ms2mt((QPC - (QPC % grain)) / 1000),
aValue.mValue.mHasQPC, true));
#else
return TimeStamp(TimeStampValue(
true, US_TO_NS(FloorToGrain(NS_TO_US(aValue.mValue.mTimeStamp)))));
#endif
}
/*
* CeilToGrain accepts a timestamp in microsecond precision
* and returns it in microseconds, rounded up to the nearest
* ClockGrain value.
*/
uint64_t Fuzzyfox::CeilToGrain(uint64_t aValue) {
return (aValue / StaticPrefs::privacy_fuzzyfox_clockgrainus()) *
StaticPrefs::privacy_fuzzyfox_clockgrainus();
}
/*
* CeilToGrain accepts a timestamp and returns it, rounded up
* to the nearest ClockGrain value.
*/
TimeStamp Fuzzyfox::CeilToGrain(TimeStamp aValue) {
#ifdef XP_WIN
// grain is in us
uint64_t grain = StaticPrefs::privacy_fuzzyfox_clockgrainus();
// GTC and QPS are stored in |mt| and need to be converted
uint64_t GTC = mt2ms(aValue.mValue.mGTC) * 1000;
uint64_t QPC = mt2ms(aValue.mValue.mQPC) * 1000;
return TimeStamp(TimeStampValue(ms2mt(((GTC / grain) * grain) / 1000),
ms2mt(((QPC / grain) * grain) / 1000),
aValue.mValue.mHasQPC, true));
#else
return TimeStamp(TimeStampValue(
true, US_TO_NS(CeilToGrain(NS_TO_US(aValue.mValue.mTimeStamp)))));
#endif
}
|