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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* 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 <driverblocklist.hxx>
#include <algorithm>
#include <string_view>
#include <sal/log.hxx>
#include <utility>
#ifdef _WIN32
#if !defined WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#endif
namespace DriverBlocklist
{
static OperatingSystem getOperatingSystem(std::string_view rString)
{
if (rString == "all")
return DRIVER_OS_ALL;
else if (rString == "7")
return DRIVER_OS_WINDOWS_7;
else if (rString == "8")
return DRIVER_OS_WINDOWS_8;
else if (rString == "8_1")
return DRIVER_OS_WINDOWS_8_1;
else if (rString == "10")
return DRIVER_OS_WINDOWS_10;
else if (rString == "windows")
return DRIVER_OS_WINDOWS_ALL;
else if (rString == "linux")
return DRIVER_OS_LINUX;
else if (rString == "osx_10_5")
return DRIVER_OS_OSX_10_5;
else if (rString == "osx_10_6")
return DRIVER_OS_OSX_10_6;
else if (rString == "osx_10_7")
return DRIVER_OS_OSX_10_7;
else if (rString == "osx_10_8")
return DRIVER_OS_OSX_10_8;
else if (rString == "osx")
return DRIVER_OS_OSX_ALL;
else if (rString == "android")
return DRIVER_OS_ANDROID;
return DRIVER_OS_UNKNOWN;
}
static VersionComparisonOp getComparison(std::string_view rString)
{
if (rString == "less")
{
return DRIVER_LESS_THAN;
}
else if (rString == "less_equal")
{
return DRIVER_LESS_THAN_OR_EQUAL;
}
else if (rString == "greater")
{
return DRIVER_GREATER_THAN;
}
else if (rString == "greater_equal")
{
return DRIVER_GREATER_THAN_OR_EQUAL;
}
else if (rString == "equal")
{
return DRIVER_EQUAL;
}
else if (rString == "not_equal")
{
return DRIVER_NOT_EQUAL;
}
else if (rString == "between_exclusive")
{
return DRIVER_BETWEEN_EXCLUSIVE;
}
else if (rString == "between_inclusive")
{
return DRIVER_BETWEEN_INCLUSIVE;
}
else if (rString == "between_inclusive_start")
{
return DRIVER_BETWEEN_INCLUSIVE_START;
}
throw InvalidFileException();
}
static OUString GetVendorId(std::string_view rString)
{
if (rString == "all")
{
return "";
}
else if (rString == "intel")
{
return "0x8086";
}
else if (rString == "nvidia")
{
return "0x10de";
}
else if (rString == "amd")
{
return "0x1002";
}
else if (rString == "microsoft")
{
return "0x1414";
}
else
{
// Allow having simply the hex number as such there, too.
return OStringToOUString(rString, RTL_TEXTENCODING_UTF8);
}
}
OUString GetVendorId(DeviceVendor id)
{
assert(id >= 0 && id < DeviceVendorMax);
switch (id)
{
case VendorAll:
return "";
case VendorIntel:
return "0x8086";
case VendorNVIDIA:
return "0x10de";
case VendorAMD:
return "0x1002";
case VendorMicrosoft:
return "0x1414";
}
abort();
}
DeviceVendor GetVendorFromId(uint32_t id)
{
switch (id)
{
case 0x8086:
return VendorIntel;
case 0x10de:
return VendorNVIDIA;
case 0x1002:
return VendorAMD;
case 0x1414:
return VendorMicrosoft;
default:
return VendorAll;
}
}
std::string_view GetVendorNameFromId(uint32_t id)
{
switch (id)
{
case 0x8086:
return "Intel";
case 0x10de:
return "Nvidia";
case 0x1002:
return "AMD";
case 0x1414:
return "Microsoft";
default:
return "?";
}
}
Parser::Parser(OUString aURL, std::vector<DriverInfo>& rDriverList, VersionType versionType)
: meBlockType(BlockType::UNKNOWN)
, mrDriverList(rDriverList)
, maURL(std::move(aURL))
, mVersionType(versionType)
{
}
bool Parser::parse()
{
try
{
xmlreader::XmlReader aReader(maURL);
handleContent(aReader);
}
catch (...)
{
mrDriverList.clear();
return false;
}
return true;
}
// This allows us to pad driver version 'substrings' with 0s, this
// effectively allows us to treat the version numbers as 'decimals'. This is
// a little strange but this method seems to do the right thing for all
// different vendor's driver strings. i.e. .98 will become 9800, which is
// larger than .978 which would become 9780.
static void PadDriverDecimal(char* aString)
{
for (int i = 0; i < 4; i++)
{
if (!aString[i])
{
for (int c = i; c < 4; c++)
{
aString[c] = '0';
}
break;
}
}
aString[4] = 0;
}
// All destination string storage needs to have at least 5 bytes available.
static bool SplitDriverVersion(const char* aSource, char* aAStr, char* aBStr, char* aCStr,
char* aDStr, VersionType versionType)
{
// sscanf doesn't do what we want here to we parse this manually.
int len = strlen(aSource);
char* dest[4] = { aAStr, aBStr, aCStr, aDStr };
unsigned destIdx = 0;
unsigned destPos = 0;
for (int i = 0; i < len; i++)
{
if (destIdx >= SAL_N_ELEMENTS(dest))
{
// Invalid format found. Ensure we don't access dest beyond bounds.
return false;
}
if (aSource[i] == '.')
{
dest[destIdx++][destPos] = 0;
destPos = 0;
continue;
}
if (destPos > 3)
{
// Ignore more than 4 chars. Ensure we never access dest[destIdx]
// beyond its bounds.
continue;
}
dest[destIdx][destPos++] = aSource[i];
}
// Add last terminator.
dest[destIdx][destPos] = 0;
// Vulkan version numbers have only 3 fields.
if (versionType == VersionType::Vulkan && destIdx == SAL_N_ELEMENTS(dest) - 2)
dest[++destIdx][0] = '\0';
if (destIdx != SAL_N_ELEMENTS(dest) - 1)
{
return false;
}
return true;
}
static bool ParseDriverVersion(std::u16string_view aVersion, uint64_t& rNumericVersion,
VersionType versionType)
{
rNumericVersion = 0;
int a, b, c, d;
char aStr[8], bStr[8], cStr[8], dStr[8];
/* honestly, why do I even bother */
OString aOVersion = OUStringToOString(aVersion, RTL_TEXTENCODING_UTF8);
if (!SplitDriverVersion(aOVersion.getStr(), aStr, bStr, cStr, dStr, versionType))
return false;
if (versionType == VersionType::OpenGL)
{
PadDriverDecimal(bStr);
PadDriverDecimal(cStr);
PadDriverDecimal(dStr);
}
a = atoi(aStr);
b = atoi(bStr);
c = atoi(cStr);
d = atoi(dStr);
if (versionType == VersionType::Vulkan)
assert(d == 0);
if (a < 0 || a > 0xffff)
return false;
if (b < 0 || b > 0xffff)
return false;
if (c < 0 || c > 0xffff)
return false;
if (d < 0 || d > 0xffff)
return false;
rNumericVersion = GFX_DRIVER_VERSION(a, b, c, d);
return true;
}
uint64_t Parser::getVersion(std::string_view rString)
{
OUString aString = OStringToOUString(rString, RTL_TEXTENCODING_UTF8);
uint64_t nVersion;
bool bResult = ParseDriverVersion(aString, nVersion, mVersionType);
if (!bResult)
{
throw InvalidFileException();
}
return nVersion;
}
void Parser::handleDevices(DriverInfo& rDriver, xmlreader::XmlReader& rReader)
{
int nLevel = 1;
bool bInMsg = false;
while (true)
{
xmlreader::Span name;
int nsId;
xmlreader::XmlReader::Result res
= rReader.nextItem(xmlreader::XmlReader::Text::Normalized, &name, &nsId);
if (res == xmlreader::XmlReader::Result::Begin)
{
++nLevel;
if (nLevel > 2)
throw InvalidFileException();
if (name == "msg")
{
bInMsg = true;
}
else if (name == "device")
{
int nsIdDeveice;
while (rReader.nextAttribute(&nsIdDeveice, &name))
{
if (name == "id")
{
name = rReader.getAttributeValue(false);
OString aDeviceId(name.begin, name.length);
rDriver.maDevices.push_back(
OStringToOUString(aDeviceId, RTL_TEXTENCODING_UTF8));
}
}
}
else
throw InvalidFileException();
}
else if (res == xmlreader::XmlReader::Result::End)
{
--nLevel;
bInMsg = false;
if (!nLevel)
break;
}
else if (res == xmlreader::XmlReader::Result::Text)
{
if (bInMsg)
{
OString sMsg(name.begin, name.length);
rDriver.maMsg = OStringToOUString(sMsg, RTL_TEXTENCODING_UTF8);
}
}
}
}
void Parser::handleEntry(DriverInfo& rDriver, xmlreader::XmlReader& rReader)
{
if (meBlockType == BlockType::ALLOWLIST)
{
rDriver.mbAllowlisted = true;
}
else if (meBlockType == BlockType::DENYLIST)
{
rDriver.mbAllowlisted = false;
}
else if (meBlockType == BlockType::UNKNOWN)
{
throw InvalidFileException();
}
xmlreader::Span name;
int nsId;
while (rReader.nextAttribute(&nsId, &name))
{
if (name == "os")
{
name = rReader.getAttributeValue(false);
OString sOS(name.begin, name.length);
rDriver.meOperatingSystem = getOperatingSystem(sOS);
}
else if (name == "vendor")
{
name = rReader.getAttributeValue(false);
OString sVendor(name.begin, name.length);
rDriver.maAdapterVendor = GetVendorId(sVendor);
}
else if (name == "compare")
{
name = rReader.getAttributeValue(false);
OString sCompare(name.begin, name.length);
rDriver.meComparisonOp = getComparison(sCompare);
}
else if (name == "version")
{
name = rReader.getAttributeValue(false);
OString sVersion(name.begin, name.length);
rDriver.mnDriverVersion = getVersion(sVersion);
}
else if (name == "minVersion")
{
name = rReader.getAttributeValue(false);
OString sMinVersion(name.begin, name.length);
rDriver.mnDriverVersion = getVersion(sMinVersion);
}
else if (name == "maxVersion")
{
name = rReader.getAttributeValue(false);
OString sMaxVersion(name.begin, name.length);
rDriver.mnDriverVersionMax = getVersion(sMaxVersion);
}
else
{
OString aAttrName(name.begin, name.length);
SAL_WARN("vcl.driver", "unsupported attribute: " << aAttrName);
}
}
handleDevices(rDriver, rReader);
}
void Parser::handleList(xmlreader::XmlReader& rReader)
{
xmlreader::Span name;
int nsId;
while (true)
{
xmlreader::XmlReader::Result res
= rReader.nextItem(xmlreader::XmlReader::Text::NONE, &name, &nsId);
if (res == xmlreader::XmlReader::Result::Begin)
{
if (name == "entry")
{
DriverInfo aDriver;
handleEntry(aDriver, rReader);
mrDriverList.push_back(aDriver);
}
else if (name == "entryRange")
{
DriverInfo aDriver;
handleEntry(aDriver, rReader);
mrDriverList.push_back(aDriver);
}
else
{
throw InvalidFileException();
}
}
else if (res == xmlreader::XmlReader::Result::End)
{
break;
}
}
}
void Parser::handleContent(xmlreader::XmlReader& rReader)
{
while (true)
{
xmlreader::Span name;
int nsId;
xmlreader::XmlReader::Result res
= rReader.nextItem(xmlreader::XmlReader::Text::NONE, &name, &nsId);
if (res == xmlreader::XmlReader::Result::Begin)
{
if (name == "allowlist")
{
meBlockType = BlockType::ALLOWLIST;
handleList(rReader);
}
else if (name == "denylist")
{
meBlockType = BlockType::DENYLIST;
handleList(rReader);
}
else if (name == "root")
{
}
else
{
throw InvalidFileException();
}
}
else if (res == xmlreader::XmlReader::Result::End)
{
if (name == "allowlist" || name == "denylist")
{
meBlockType = BlockType::UNKNOWN;
}
}
else if (res == xmlreader::XmlReader::Result::Done)
{
break;
}
}
}
static OperatingSystem getOperatingSystem()
{
#ifdef _WIN32
// OS version in 16.16 major/minor form
// based on http://msdn.microsoft.com/en-us/library/ms724834(VS.85).aspx
switch (DriverBlocklist::GetWindowsVersion())
{
case 0x00060001:
return DRIVER_OS_WINDOWS_7;
case 0x00060002:
return DRIVER_OS_WINDOWS_8;
case 0x00060003:
return DRIVER_OS_WINDOWS_8_1;
case 0x000A0000: // Major 10 Minor 0
return DRIVER_OS_WINDOWS_10;
default:
return DRIVER_OS_UNKNOWN;
}
#elif defined LINUX
return DRIVER_OS_LINUX;
#else
return DRIVER_OS_UNKNOWN;
#endif
}
namespace
{
struct compareIgnoreAsciiCase
{
explicit compareIgnoreAsciiCase(OUString aString)
: maString(std::move(aString))
{
}
bool operator()(std::u16string_view rCompare)
{
return maString.equalsIgnoreAsciiCase(rCompare);
}
private:
OUString maString;
};
}
const uint64_t allDriverVersions = ~(uint64_t(0));
DriverInfo::DriverInfo()
: meOperatingSystem(DRIVER_OS_UNKNOWN)
, maAdapterVendor(GetVendorId(VendorAll))
, mbAllowlisted(false)
, meComparisonOp(DRIVER_COMPARISON_IGNORED)
, mnDriverVersion(0)
, mnDriverVersionMax(0)
{
}
DriverInfo::DriverInfo(OperatingSystem os, OUString vendor, VersionComparisonOp op,
uint64_t driverVersion, bool bAllowlisted,
const char* suggestedVersion /* = nullptr */)
: meOperatingSystem(os)
, maAdapterVendor(std::move(vendor))
, mbAllowlisted(bAllowlisted)
, meComparisonOp(op)
, mnDriverVersion(driverVersion)
, mnDriverVersionMax(0)
{
if (suggestedVersion)
maSuggestedVersion
= OStringToOUString(std::string_view(suggestedVersion), RTL_TEXTENCODING_UTF8);
}
bool FindBlocklistedDeviceInList(std::vector<DriverInfo>& aDeviceInfos, VersionType versionType,
std::u16string_view sDriverVersion,
std::u16string_view sAdapterVendorID,
OUString const& sAdapterDeviceID, OperatingSystem system,
const OUString& blocklistURL)
{
uint64_t driverVersion;
ParseDriverVersion(sDriverVersion, driverVersion, versionType);
bool match = false;
for (std::vector<DriverInfo>::size_type i = 0; i < aDeviceInfos.size(); i++)
{
bool osMatch = false;
if (aDeviceInfos[i].meOperatingSystem == DRIVER_OS_ALL)
osMatch = true;
else if (aDeviceInfos[i].meOperatingSystem == system)
osMatch = true;
else if (aDeviceInfos[i].meOperatingSystem == DRIVER_OS_WINDOWS_ALL
&& system >= DRIVER_OS_WINDOWS_FIRST && system <= DRIVER_OS_WINDOWS_LAST)
osMatch = true;
else if (aDeviceInfos[i].meOperatingSystem == DRIVER_OS_OSX_ALL
&& system >= DRIVER_OS_OSX_FIRST && system <= DRIVER_OS_OSX_LAST)
osMatch = true;
if (!osMatch)
{
continue;
}
if (!aDeviceInfos[i].maAdapterVendor.equalsIgnoreAsciiCase(GetVendorId(VendorAll))
&& !aDeviceInfos[i].maAdapterVendor.equalsIgnoreAsciiCase(sAdapterVendorID))
{
continue;
}
if (std::none_of(aDeviceInfos[i].maDevices.begin(), aDeviceInfos[i].maDevices.end(),
compareIgnoreAsciiCase("all"))
&& std::none_of(aDeviceInfos[i].maDevices.begin(), aDeviceInfos[i].maDevices.end(),
compareIgnoreAsciiCase(sAdapterDeviceID)))
{
continue;
}
switch (aDeviceInfos[i].meComparisonOp)
{
case DRIVER_LESS_THAN:
match = driverVersion < aDeviceInfos[i].mnDriverVersion;
break;
case DRIVER_LESS_THAN_OR_EQUAL:
match = driverVersion <= aDeviceInfos[i].mnDriverVersion;
break;
case DRIVER_GREATER_THAN:
match = driverVersion > aDeviceInfos[i].mnDriverVersion;
break;
case DRIVER_GREATER_THAN_OR_EQUAL:
match = driverVersion >= aDeviceInfos[i].mnDriverVersion;
break;
case DRIVER_EQUAL:
match = driverVersion == aDeviceInfos[i].mnDriverVersion;
break;
case DRIVER_NOT_EQUAL:
match = driverVersion != aDeviceInfos[i].mnDriverVersion;
break;
case DRIVER_BETWEEN_EXCLUSIVE:
match = driverVersion > aDeviceInfos[i].mnDriverVersion
&& driverVersion < aDeviceInfos[i].mnDriverVersionMax;
break;
case DRIVER_BETWEEN_INCLUSIVE:
match = driverVersion >= aDeviceInfos[i].mnDriverVersion
&& driverVersion <= aDeviceInfos[i].mnDriverVersionMax;
break;
case DRIVER_BETWEEN_INCLUSIVE_START:
match = driverVersion >= aDeviceInfos[i].mnDriverVersion
&& driverVersion < aDeviceInfos[i].mnDriverVersionMax;
break;
case DRIVER_COMPARISON_IGNORED:
// We don't have a comparison op, so we match everything.
match = true;
break;
default:
SAL_WARN("vcl.driver", "Bogus op in " << blocklistURL);
break;
}
if (match || aDeviceInfos[i].mnDriverVersion == allDriverVersions)
{
// white listed drivers
if (aDeviceInfos[i].mbAllowlisted)
{
SAL_INFO("vcl.driver", "allowlisted driver");
return false;
}
match = true;
if (!aDeviceInfos[i].maSuggestedVersion.isEmpty())
{
SAL_WARN("vcl.driver", "use : " << aDeviceInfos[i].maSuggestedVersion);
}
break;
}
}
SAL_INFO("vcl.driver", (match ? "denylisted" : "not denylisted") << " in " << blocklistURL);
return match;
}
bool IsDeviceBlocked(const OUString& blocklistURL, VersionType versionType,
std::u16string_view driverVersion, std::u16string_view vendorId,
const OUString& deviceId)
{
std::vector<DriverInfo> driverList;
Parser parser(blocklistURL, driverList, versionType);
if (!parser.parse())
{
SAL_WARN("vcl.driver", "error parsing denylist " << blocklistURL);
return false;
}
return FindBlocklistedDeviceInList(driverList, versionType, driverVersion, vendorId, deviceId,
getOperatingSystem(), blocklistURL);
}
#ifdef _WIN32
int32_t GetWindowsVersion()
{
static int32_t winVersion = []() {
// GetVersion(Ex) and VersionHelpers (based on VerifyVersionInfo) API are
// subject to manifest-based behavior since Windows 8.1, so give wrong results.
// Another approach would be to use NetWkstaGetInfo, but that has some small
// reported delays (some milliseconds), and might get slower in domains with
// poor network connections.
// So go with a solution described at https://msdn.microsoft.com/en-us/library/ms724429
HINSTANCE hLibrary = LoadLibraryW(L"kernel32.dll");
if (hLibrary != nullptr)
{
wchar_t szPath[MAX_PATH];
DWORD dwCount = GetModuleFileNameW(hLibrary, szPath, SAL_N_ELEMENTS(szPath));
FreeLibrary(hLibrary);
if (dwCount != 0 && dwCount < SAL_N_ELEMENTS(szPath))
{
dwCount = GetFileVersionInfoSizeW(szPath, nullptr);
if (dwCount != 0)
{
std::unique_ptr<char[]> ver(new char[dwCount]);
if (GetFileVersionInfoW(szPath, 0, dwCount, ver.get()) != FALSE)
{
void* pBlock = nullptr;
UINT dwBlockSz = 0;
if (VerQueryValueW(ver.get(), L"\\", &pBlock, &dwBlockSz) != FALSE
&& dwBlockSz >= sizeof(VS_FIXEDFILEINFO))
{
VS_FIXEDFILEINFO* vinfo = static_cast<VS_FIXEDFILEINFO*>(pBlock);
return int32_t(vinfo->dwProductVersionMS);
}
}
}
}
}
return 0;
}();
return winVersion;
}
#endif
} // namespace
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|