/
metadata-verify.c
4436 lines (3553 loc) · 146 KB
/
metadata-verify.c
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* metadata-verify.c: Metadata verfication support
*
* Author:
* Mono Project (http://www.mono-project.com)
*
* Copyright (C) 2005-2008 Novell, Inc. (http://www.novell.com)
*/
#include <mono/metadata/object-internals.h>
#include <mono/metadata/verify.h>
#include <mono/metadata/verify-internals.h>
#include <mono/metadata/opcodes.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/reflection.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/mono-endian.h>
#include <mono/metadata/metadata.h>
#include <mono/metadata/metadata-internals.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/security-manager.h>
#include <mono/metadata/security-core-clr.h>
#include <mono/metadata/cil-coff.h>
#include <mono/metadata/attrdefs.h>
#include <mono/utils/strenc.h>
#include <mono/utils/mono-error-internals.h>
#include <string.h>
//#include <signal.h>
#include <ctype.h>
#ifndef DISABLE_VERIFIER
/*
TODO add fail fast mode
TODO add PE32+ support
TODO verify the entry point RVA and content.
TODO load_section_table and load_data_directories must take PE32+ into account
TODO add section relocation support
TODO verify the relocation table, since we really don't use, no need so far.
TODO do full PECOFF resources verification
TODO verify in the CLI header entry point and resources
TODO implement null token typeref validation
TODO verify table wide invariants for typedef (sorting and uniqueness)
TODO implement proper authenticode data directory validation
TODO verify properties that require multiple tables to be valid
FIXME use subtraction based bounds checking to avoid overflows
FIXME get rid of metadata_streams and other fields from VerifyContext
*/
#ifdef MONO_VERIFIER_DEBUG
#define VERIFIER_DEBUG(code) do { code; } while (0)
#else
#define VERIFIER_DEBUG(code)
#endif
#define INVALID_OFFSET ((guint32)-1)
#define INVALID_ADDRESS 0xffffffff
enum {
STAGE_PE,
STAGE_CLI,
STAGE_TABLES
};
enum {
IMPORT_TABLE_IDX = 1,
RESOURCE_TABLE_IDX = 2,
CERTIFICATE_TABLE_IDX = 4,
RELOCATION_TABLE_IDX = 5,
IAT_IDX = 12,
CLI_HEADER_IDX = 14,
};
enum {
STRINGS_STREAM,
USER_STRINGS_STREAM,
BLOB_STREAM,
GUID_STREAM,
TILDE_STREAM
};
#define INVALID_TABLE (0xFF)
/*format: number of bits, number of tables, tables{n. tables} */
const static unsigned char coded_index_desc[] = {
#define TYPEDEF_OR_REF_DESC (0)
2, /*bits*/
3, /*tables*/
MONO_TABLE_TYPEDEF,
MONO_TABLE_TYPEREF,
MONO_TABLE_TYPESPEC,
#define HAS_CONSTANT_DESC (TYPEDEF_OR_REF_DESC + 5)
2, /*bits*/
3, /*tables*/
MONO_TABLE_FIELD,
MONO_TABLE_PARAM,
MONO_TABLE_PROPERTY,
#define HAS_CATTR_DESC (HAS_CONSTANT_DESC + 5)
5, /*bits*/
20, /*tables*/
MONO_TABLE_METHOD,
MONO_TABLE_FIELD,
MONO_TABLE_TYPEREF,
MONO_TABLE_TYPEDEF,
MONO_TABLE_PARAM,
MONO_TABLE_INTERFACEIMPL,
MONO_TABLE_MEMBERREF,
MONO_TABLE_MODULE,
MONO_TABLE_DECLSECURITY,
MONO_TABLE_PROPERTY,
MONO_TABLE_EVENT,
MONO_TABLE_STANDALONESIG,
MONO_TABLE_MODULEREF,
MONO_TABLE_TYPESPEC,
MONO_TABLE_ASSEMBLY,
MONO_TABLE_ASSEMBLYREF,
MONO_TABLE_FILE,
MONO_TABLE_EXPORTEDTYPE,
MONO_TABLE_MANIFESTRESOURCE,
MONO_TABLE_GENERICPARAM,
#define HAS_FIELD_MARSHAL_DESC (HAS_CATTR_DESC + 22)
1, /*bits*/
2, /*tables*/
MONO_TABLE_FIELD,
MONO_TABLE_PARAM,
#define HAS_DECL_SECURITY_DESC (HAS_FIELD_MARSHAL_DESC + 4)
2, /*bits*/
3, /*tables*/
MONO_TABLE_TYPEDEF,
MONO_TABLE_METHOD,
MONO_TABLE_ASSEMBLY,
#define MEMBERREF_PARENT_DESC (HAS_DECL_SECURITY_DESC + 5)
3, /*bits*/
5, /*tables*/
MONO_TABLE_TYPEDEF,
MONO_TABLE_TYPEREF,
MONO_TABLE_MODULEREF,
MONO_TABLE_METHOD,
MONO_TABLE_TYPESPEC,
#define HAS_SEMANTICS_DESC (MEMBERREF_PARENT_DESC + 7)
1, /*bits*/
2, /*tables*/
MONO_TABLE_EVENT,
MONO_TABLE_PROPERTY,
#define METHODDEF_OR_REF_DESC (HAS_SEMANTICS_DESC + 4)
1, /*bits*/
2, /*tables*/
MONO_TABLE_METHOD,
MONO_TABLE_MEMBERREF,
#define MEMBER_FORWARDED_DESC (METHODDEF_OR_REF_DESC + 4)
1, /*bits*/
2, /*tables*/
MONO_TABLE_FIELD,
MONO_TABLE_METHOD,
#define IMPLEMENTATION_DESC (MEMBER_FORWARDED_DESC + 4)
2, /*bits*/
3, /*tables*/
MONO_TABLE_FILE,
MONO_TABLE_ASSEMBLYREF,
MONO_TABLE_EXPORTEDTYPE,
#define CATTR_TYPE_DESC (IMPLEMENTATION_DESC + 5)
3, /*bits*/
5, /*tables*/
INVALID_TABLE,
INVALID_TABLE,
MONO_TABLE_METHOD,
MONO_TABLE_MEMBERREF,
INVALID_TABLE,
#define RES_SCOPE_DESC (CATTR_TYPE_DESC + 7)
2, /*bits*/
4, /*tables*/
MONO_TABLE_MODULE,
MONO_TABLE_MODULEREF,
MONO_TABLE_ASSEMBLYREF,
MONO_TABLE_TYPEREF,
#define TYPE_OR_METHODDEF_DESC (RES_SCOPE_DESC + 6)
1, /*bits*/
2, /*tables*/
MONO_TABLE_TYPEDEF,
MONO_TABLE_METHOD
};
typedef struct {
guint32 rva;
guint32 size;
guint32 translated_offset;
} DataDirectory;
typedef struct {
guint32 offset;
guint32 size;
} OffsetAndSize;
typedef struct {
guint32 baseRVA;
guint32 baseOffset;
guint32 size;
guint32 rellocationsRVA;
guint16 numberOfRelocations;
} SectionHeader;
typedef struct {
guint32 row_count;
guint32 row_size;
guint32 offset;
} TableInfo;
typedef struct {
const char *data;
guint32 size, token;
GSList *errors;
int valid;
MonoImage *image;
gboolean report_error;
gboolean report_warning;
int stage;
DataDirectory data_directories [16];
guint32 section_count;
SectionHeader *sections;
OffsetAndSize metadata_streams [5]; //offset from begin of the image
} VerifyContext;
#define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
do { \
MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
vinfo->info.status = __status; \
vinfo->info.message = ( __msg); \
vinfo->exception_type = (__exception); \
(__ctx)->errors = g_slist_prepend ((__ctx)->errors, vinfo); \
} while (0)
#define ADD_WARNING(__ctx, __msg) \
do { \
if ((__ctx)->report_warning) { \
ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_WARNING, MONO_EXCEPTION_INVALID_PROGRAM); \
(__ctx)->valid = 0; \
return; \
} \
} while (0)
#define ADD_ERROR_NO_RETURN(__ctx, __msg) \
do { \
if ((__ctx)->report_error) \
ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
(__ctx)->valid = 0; \
} while (0)
#define ADD_ERROR(__ctx, __msg) \
do { \
if ((__ctx)->report_error) \
ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
(__ctx)->valid = 0; \
return; \
} while (0)
#define FAIL(__ctx, __msg) \
do { \
if ((__ctx)->report_error) \
ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
(__ctx)->valid = 0; \
return FALSE; \
} while (0)
#define CHECK_STATE() do { if (!ctx.valid) goto cleanup; } while (0)
#define CHECK_ERROR() do { if (!ctx->valid) return; } while (0)
#define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
#define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
#if SIZEOF_VOID_P == 4
#define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
#else
#define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
#endif
#define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
#define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
static const char *
dword_align (const char *ptr)
{
#if SIZEOF_VOID_P == 8
return (const char *) (((guint64) (ptr + 3)) & ~3);
#else
return (const char *) (((guint32) (ptr + 3)) & ~3);
#endif
}
static void
add_from_mono_error (VerifyContext *ctx, MonoError *error)
{
if (mono_error_ok (error))
return;
ADD_ERROR (ctx, g_strdup (mono_error_get_message (error)));
mono_error_cleanup (error);
}
static guint32
pe_signature_offset (VerifyContext *ctx)
{
return read32 (ctx->data + 0x3c);
}
static guint32
pe_header_offset (VerifyContext *ctx)
{
return read32 (ctx->data + 0x3c) + 4;
}
static gboolean
bounds_check_virtual_address (VerifyContext *ctx, guint32 rva, guint32 size)
{
int i;
if (rva + size < rva) //overflow
return FALSE;
if (ctx->stage > STAGE_PE) {
MonoCLIImageInfo *iinfo = ctx->image->image_info;
const int top = iinfo->cli_section_count;
MonoSectionTable *tables = iinfo->cli_section_tables;
int i;
for (i = 0; i < top; i++) {
guint32 base = tables->st_virtual_address;
guint32 end = base + tables->st_raw_data_size;
if (rva >= base && rva + size <= end)
return TRUE;
/*if ((addr >= tables->st_virtual_address) &&
(addr < tables->st_virtual_address + tables->st_raw_data_size)){
return addr - tables->st_virtual_address + tables->st_raw_data_ptr;
}*/
tables++;
}
return FALSE;
}
if (!ctx->sections)
return FALSE;
for (i = 0; i < ctx->section_count; ++i) {
guint32 base = ctx->sections [i].baseRVA;
guint32 end = ctx->sections [i].baseRVA + ctx->sections [i].size;
if (rva >= base && rva + size <= end)
return TRUE;
}
return FALSE;
}
static gboolean
bounds_check_datadir (DataDirectory *dir, guint32 offset, guint32 size)
{
if (dir->translated_offset > offset)
return FALSE;
if (dir->size < size)
return FALSE;
return offset + size <= dir->translated_offset + dir->size;
}
static gboolean
bounds_check_offset (OffsetAndSize *off, guint32 offset, guint32 size)
{
if (off->offset > offset)
return FALSE;
if (off->size < size)
return FALSE;
return offset + size <= off->offset + off->size;
}
static guint32
translate_rva (VerifyContext *ctx, guint32 rva)
{
int i;
if (ctx->stage > STAGE_PE)
return mono_cli_rva_image_map (ctx->image, rva);
if (!ctx->sections)
return FALSE;
for (i = 0; i < ctx->section_count; ++i) {
guint32 base = ctx->sections [i].baseRVA;
guint32 end = ctx->sections [i].baseRVA + ctx->sections [i].size;
if (rva >= base && rva <= end) {
guint32 res = (rva - base) + ctx->sections [i].baseOffset;
/* double check */
return res >= ctx->size ? INVALID_OFFSET : res;
}
}
return INVALID_OFFSET;
}
static void
verify_msdos_header (VerifyContext *ctx)
{
guint32 lfanew;
if (ctx->size < 128)
ADD_ERROR (ctx, g_strdup ("Not enough space for the MS-DOS header"));
if (ctx->data [0] != 0x4d || ctx->data [1] != 0x5a)
ADD_ERROR (ctx, g_strdup ("Invalid MS-DOS watermark"));
lfanew = pe_signature_offset (ctx);
if (lfanew > ctx->size - 4)
ADD_ERROR (ctx, g_strdup ("MS-DOS lfanew offset points to outside of the file"));
}
static void
verify_pe_header (VerifyContext *ctx)
{
guint32 offset = pe_signature_offset (ctx);
const char *pe_header = ctx->data + offset;
if (pe_header [0] != 'P' || pe_header [1] != 'E' ||pe_header [2] != 0 ||pe_header [3] != 0)
ADD_ERROR (ctx, g_strdup ("Invalid PE header watermark"));
pe_header += 4;
offset += 4;
if (offset > ctx->size - 20)
ADD_ERROR (ctx, g_strdup ("File with truncated pe header"));
if (read16 (pe_header) != 0x14c)
ADD_ERROR (ctx, g_strdup ("Invalid PE header Machine value"));
}
static void
verify_pe_optional_header (VerifyContext *ctx)
{
guint32 offset = pe_header_offset (ctx);
guint32 header_size, file_alignment;
const char *pe_header = ctx->data + offset;
const char *pe_optional_header = pe_header + 20;
header_size = read16 (pe_header + 16);
offset += 20;
if (header_size < 2) /*must be at least 2 or we won't be able to read magic*/
ADD_ERROR (ctx, g_strdup ("Invalid PE optional header size"));
if (offset > ctx->size - header_size || header_size > ctx->size)
ADD_ERROR (ctx, g_strdup ("Invalid PE optional header size"));
if (read16 (pe_optional_header) == 0x10b) {
if (header_size != 224)
ADD_ERROR (ctx, g_strdup_printf ("Invalid optional header size %d", header_size));
/* LAMESPEC MS plays around this value and ignore it during validation
if (read32 (pe_optional_header + 28) != 0x400000)
ADD_ERROR (ctx, g_strdup_printf ("Invalid Image base %x", read32 (pe_optional_header + 28)));*/
if (read32 (pe_optional_header + 32) != 0x2000)
ADD_ERROR (ctx, g_strdup_printf ("Invalid Section Aligmnent %x", read32 (pe_optional_header + 32)));
file_alignment = read32 (pe_optional_header + 36);
if (file_alignment != 0x200 && file_alignment != 0x1000)
ADD_ERROR (ctx, g_strdup_printf ("Invalid file Aligmnent %x", file_alignment));
/* All the junk in the middle is irrelevant, specially for mono. */
if (read32 (pe_optional_header + 92) > 0x10)
ADD_ERROR (ctx, g_strdup_printf ("Too many data directories %x", read32 (pe_optional_header + 92)));
} else {
if (read16 (pe_optional_header) == 0x20B)
ADD_ERROR (ctx, g_strdup ("Metadata verifier doesn't handle PE32+"));
else
ADD_ERROR (ctx, g_strdup_printf ("Invalid optional header magic %d", read16 (pe_optional_header)));
}
}
static void
load_section_table (VerifyContext *ctx)
{
int i;
SectionHeader *sections;
guint32 offset = pe_header_offset (ctx);
const char *ptr = ctx->data + offset;
guint16 num_sections = ctx->section_count = read16 (ptr + 2);
offset += 244;/*FIXME, this constant is different under PE32+*/
ptr += 244;
if (num_sections * 40 > ctx->size - offset)
ADD_ERROR (ctx, g_strdup ("Invalid PE optional header size"));
sections = ctx->sections = g_new0 (SectionHeader, num_sections);
for (i = 0; i < num_sections; ++i) {
sections [i].size = read32 (ptr + 8);
sections [i].baseRVA = read32 (ptr + 12);
sections [i].baseOffset = read32 (ptr + 20);
sections [i].rellocationsRVA = read32 (ptr + 24);
sections [i].numberOfRelocations = read16 (ptr + 32);
ptr += 40;
}
ptr = ctx->data + offset; /*reset it to the beggining*/
for (i = 0; i < num_sections; ++i) {
guint32 raw_size, flags;
if (sections [i].baseOffset == 0)
ADD_ERROR (ctx, g_strdup ("Metadata verifier doesn't handle sections with intialized data only"));
if (sections [i].baseOffset >= ctx->size)
ADD_ERROR (ctx, g_strdup_printf ("Invalid PointerToRawData %x points beyond EOF", sections [i].baseOffset));
if (sections [i].size > ctx->size - sections [i].baseOffset)
ADD_ERROR (ctx, g_strdup ("Invalid VirtualSize points beyond EOF"));
raw_size = read32 (ptr + 16);
if (raw_size < sections [i].size)
ADD_ERROR (ctx, g_strdup ("Metadata verifier doesn't handle sections with SizeOfRawData < VirtualSize"));
if (raw_size > ctx->size - sections [i].baseOffset)
ADD_ERROR (ctx, g_strdup_printf ("Invalid SizeOfRawData %x points beyond EOF", raw_size));
if (sections [i].rellocationsRVA || sections [i].numberOfRelocations)
ADD_ERROR (ctx, g_strdup_printf ("Metadata verifier doesn't handle section relocation"));
flags = read32 (ptr + 36);
/*TODO 0xFE0000E0 is all flags from cil-coff.h OR'd. Make it a less magical number*/
if (flags == 0 || (flags & ~0xFE0000E0) != 0)
ADD_ERROR (ctx, g_strdup_printf ("Invalid section flags %x", flags));
ptr += 40;
}
}
static gboolean
is_valid_data_directory (int i)
{
/*LAMESPEC 4 == certificate 6 == debug, MS uses both*/
return i == 1 || i == 2 || i == 5 || i == 12 || i == 14 || i == 4 || i == 6;
}
static void
load_data_directories (VerifyContext *ctx)
{
guint32 offset = pe_header_offset (ctx) + 116; /*FIXME, this constant is different under PE32+*/
const char *ptr = ctx->data + offset;
int i;
for (i = 0; i < 16; ++i) {
guint32 rva = read32 (ptr);
guint32 size = read32 (ptr + 4);
/*LAMESPEC the authenticode data directory format is different. We don't support CAS, so lets ignore for now.*/
if (i == CERTIFICATE_TABLE_IDX) {
ptr += 8;
continue;
}
if ((rva != 0 || size != 0) && !is_valid_data_directory (i))
ADD_ERROR (ctx, g_strdup_printf ("Invalid data directory %d", i));
if (rva != 0 && !bounds_check_virtual_address (ctx, rva, size))
ADD_ERROR (ctx, g_strdup_printf ("Invalid data directory %d rva/size pair %x/%x", i, rva, size));
ctx->data_directories [i].rva = rva;
ctx->data_directories [i].size = size;
ctx->data_directories [i].translated_offset = translate_rva (ctx, rva);
ptr += 8;
}
}
#define SIZE_OF_MSCOREE (sizeof ("mscoree.dll"))
#define SIZE_OF_CORMAIN (sizeof ("_CorExeMain"))
static void
verify_hint_name_table (VerifyContext *ctx, guint32 import_rva, const char *table_name)
{
const char *ptr;
guint32 hint_table_rva;
import_rva = translate_rva (ctx, import_rva);
g_assert (import_rva != INVALID_OFFSET);
hint_table_rva = read32 (ctx->data + import_rva);
if (!bounds_check_virtual_address (ctx, hint_table_rva, SIZE_OF_CORMAIN + 2))
ADD_ERROR (ctx, g_strdup_printf ("Invalid Hint/Name rva %d for %s", hint_table_rva, table_name));
hint_table_rva = translate_rva (ctx, hint_table_rva);
g_assert (hint_table_rva != INVALID_OFFSET);
ptr = ctx->data + hint_table_rva + 2;
if (memcmp ("_CorExeMain", ptr, SIZE_OF_CORMAIN) && memcmp ("_CorDllMain", ptr, SIZE_OF_CORMAIN)) {
char name[SIZE_OF_CORMAIN];
memcpy (name, ptr, SIZE_OF_CORMAIN);
name [SIZE_OF_CORMAIN - 1] = 0;
ADD_ERROR (ctx, g_strdup_printf ("Invalid Hint / Name: '%s'", name));
}
}
static void
verify_import_table (VerifyContext *ctx)
{
DataDirectory it = ctx->data_directories [IMPORT_TABLE_IDX];
guint32 offset = it.translated_offset;
const char *ptr = ctx->data + offset;
guint32 name_rva, ilt_rva, iat_rva;
g_assert (offset != INVALID_OFFSET);
if (it.size < 40)
ADD_ERROR (ctx, g_strdup_printf ("Import table size %d is smaller than 40", it.size));
ilt_rva = read32 (ptr);
if (ilt_rva && !bounds_check_virtual_address (ctx, ilt_rva, 8))
ADD_ERROR (ctx, g_strdup_printf ("Invalid Import Lookup Table rva %x", ilt_rva));
name_rva = read32 (ptr + 12);
if (name_rva && !bounds_check_virtual_address (ctx, name_rva, SIZE_OF_MSCOREE))
ADD_ERROR (ctx, g_strdup_printf ("Invalid Import Table Name rva %x", name_rva));
iat_rva = read32 (ptr + 16);
if (iat_rva) {
if (!bounds_check_virtual_address (ctx, iat_rva, 8))
ADD_ERROR (ctx, g_strdup_printf ("Invalid Import Address Table rva %x", iat_rva));
if (iat_rva != ctx->data_directories [IAT_IDX].rva)
ADD_ERROR (ctx, g_strdup_printf ("Import Address Table rva %x different from data directory entry %x", read32 (ptr + 16), ctx->data_directories [IAT_IDX].rva));
}
if (name_rva) {
name_rva = translate_rva (ctx, name_rva);
g_assert (name_rva != INVALID_OFFSET);
ptr = ctx->data + name_rva;
if (memcmp ("mscoree.dll", ptr, SIZE_OF_MSCOREE)) {
char name[SIZE_OF_MSCOREE];
memcpy (name, ptr, SIZE_OF_MSCOREE);
name [SIZE_OF_MSCOREE - 1] = 0;
ADD_ERROR (ctx, g_strdup_printf ("Invalid Import Table Name: '%s'", name));
}
}
if (ilt_rva) {
verify_hint_name_table (ctx, ilt_rva, "Import Lookup Table");
CHECK_ERROR ();
}
if (iat_rva)
verify_hint_name_table (ctx, iat_rva, "Import Address Table");
}
static void
verify_resources_table (VerifyContext *ctx)
{
DataDirectory it = ctx->data_directories [RESOURCE_TABLE_IDX];
guint32 offset;
guint16 named_entries, id_entries;
const char *ptr, *root, *end;
if (it.rva == 0)
return;
if (it.size < 16)
ADD_ERROR (ctx, g_strdup_printf ("Resource section is too small, must be at least 16 bytes long but it's %d long", it.size));
offset = it.translated_offset;
root = ptr = ctx->data + offset;
end = root + it.size;
g_assert (offset != INVALID_OFFSET);
named_entries = read16 (ptr + 12);
id_entries = read16 (ptr + 14);
if ((named_entries + id_entries) * 8 + 16 > it.size)
ADD_ERROR (ctx, g_strdup_printf ("Resource section is too small, the number of entries (%d) doesn't fit on it's size %d", named_entries + id_entries, it.size));
/* XXX at least one unmanaged resource is added due to a call to AssemblyBuilder::DefineVersionInfoResource ()
if (named_entries || id_entries)
ADD_ERROR (ctx, g_strdup_printf ("The metadata verifier doesn't support full verification of PECOFF resources"));
*/
}
/*----------nothing from here on can use data_directory---*/
static DataDirectory
get_data_dir (VerifyContext *ctx, int idx)
{
MonoCLIImageInfo *iinfo = ctx->image->image_info;
MonoPEDirEntry *entry= &iinfo->cli_header.datadir.pe_export_table;
DataDirectory res;
entry += idx;
res.rva = entry->rva;
res.size = entry->size;
res.translated_offset = translate_rva (ctx, res.rva);
return res;
}
static void
verify_cli_header (VerifyContext *ctx)
{
DataDirectory it = get_data_dir (ctx, CLI_HEADER_IDX);
guint32 offset;
const char *ptr;
int i;
if (it.rva == 0)
ADD_ERROR (ctx, g_strdup_printf ("CLI header missing"));
if (it.size != 72)
ADD_ERROR (ctx, g_strdup_printf ("Invalid cli header size in data directory %d must be 72", it.size));
offset = it.translated_offset;
ptr = ctx->data + offset;
g_assert (offset != INVALID_OFFSET);
if (read16 (ptr) != 72)
ADD_ERROR (ctx, g_strdup_printf ("Invalid cli header size %d must be 72", read16 (ptr)));
if (!bounds_check_virtual_address (ctx, read32 (ptr + 8), read32 (ptr + 12)))
ADD_ERROR (ctx, g_strdup_printf ("Invalid medatata section rva/size pair %x/%x", read32 (ptr + 8), read32 (ptr + 12)));
if (!read32 (ptr + 8) || !read32 (ptr + 12))
ADD_ERROR (ctx, g_strdup_printf ("Missing medatata section in the CLI header"));
if ((read32 (ptr + 16) & ~0x0001000B) != 0)
ADD_ERROR (ctx, g_strdup_printf ("Invalid CLI header flags"));
ptr += 24;
for (i = 0; i < 6; ++i) {
guint32 rva = read32 (ptr);
guint32 size = read32 (ptr + 4);
if (rva != 0 && !bounds_check_virtual_address (ctx, rva, size))
ADD_ERROR (ctx, g_strdup_printf ("Invalid cli section %i rva/size pair %x/%x", i, rva, size));
ptr += 8;
if (rva && i > 1)
ADD_ERROR (ctx, g_strdup_printf ("Metadata verifier doesn't support cli header section %d", i));
}
}
static guint32
pad4 (guint32 offset)
{
if (offset & 0x3) //pad to the next 4 byte boundary
offset = (offset & ~0x3) + 4;
return offset;
}
static void
verify_metadata_header (VerifyContext *ctx)
{
int i;
DataDirectory it = get_data_dir (ctx, CLI_HEADER_IDX);
guint32 offset, section_count;
const char *ptr;
offset = it.translated_offset;
ptr = ctx->data + offset;
g_assert (offset != INVALID_OFFSET);
//build a directory entry for the metadata root
ptr += 8;
it.rva = read32 (ptr);
ptr += 4;
it.size = read32 (ptr);
it.translated_offset = offset = translate_rva (ctx, it.rva);
ptr = ctx->data + offset;
g_assert (offset != INVALID_OFFSET);
if (it.size < 20)
ADD_ERROR (ctx, g_strdup_printf ("Metadata root section is too small %d (at least 20 bytes required for initial decoding)", it.size));
if (read32 (ptr) != 0x424A5342)
ADD_ERROR (ctx, g_strdup_printf ("Invalid metadata signature, expected 0x424A5342 but got %08x", read32 (ptr)));
offset = pad4 (offset + 16 + read32 (ptr + 12));
if (!bounds_check_datadir (&it, offset, 4))
ADD_ERROR (ctx, g_strdup_printf ("Metadata root section is too small %d (at least %d bytes required for flags decoding)", it.size, offset + 4 - it.translated_offset));
ptr = ctx->data + offset; //move to streams header
section_count = read16 (ptr + 2);
if (section_count < 2)
ADD_ERROR (ctx, g_strdup_printf ("Metadata root section must have at least 2 streams (#~ and #GUID)"));
ptr += 4;
offset += 4;
for (i = 0; i < section_count; ++i) {
guint32 stream_off, stream_size;
int string_size, stream_idx;
if (!bounds_check_datadir (&it, offset, 8))
ADD_ERROR (ctx, g_strdup_printf ("Metadata root section is too small for initial decode of stream header %d, missing %d bytes", i, offset + 9 - it.translated_offset));
stream_off = it.translated_offset + read32 (ptr);
stream_size = read32 (ptr + 4);
if (!bounds_check_datadir (&it, stream_off, stream_size))
ADD_ERROR (ctx, g_strdup_printf ("Invalid stream header %d offset/size pair %x/%x", 0, stream_off, stream_size));
ptr += 8;
offset += 8;
for (string_size = 0; string_size < 32; ++string_size) {
if (!bounds_check_datadir (&it, offset++, 1))
ADD_ERROR (ctx, g_strdup_printf ("Metadata root section is too small to decode stream header %d name", i));
if (!ptr [string_size])
break;
}
if (ptr [string_size])
ADD_ERROR (ctx, g_strdup_printf ("Metadata stream header %d name larger than 32 bytes", i));
if (!strncmp ("#Strings", ptr, 9))
stream_idx = STRINGS_STREAM;
else if (!strncmp ("#US", ptr, 4))
stream_idx = USER_STRINGS_STREAM;
else if (!strncmp ("#Blob", ptr, 6))
stream_idx = BLOB_STREAM;
else if (!strncmp ("#GUID", ptr, 6))
stream_idx = GUID_STREAM;
else if (!strncmp ("#~", ptr, 3))
stream_idx = TILDE_STREAM;
else {
ADD_WARNING (ctx, g_strdup_printf ("Metadata stream header %d invalid name %s", i, ptr));
offset = pad4 (offset);
ptr = ctx->data + offset;
continue;
}
if (ctx->metadata_streams [stream_idx].offset != 0)
ADD_ERROR (ctx, g_strdup_printf ("Duplicated metadata stream header %s", ptr));
ctx->metadata_streams [stream_idx].offset = stream_off;
ctx->metadata_streams [stream_idx].size = stream_size;
offset = pad4 (offset);
ptr = ctx->data + offset;
}
if (!ctx->metadata_streams [TILDE_STREAM].size)
ADD_ERROR (ctx, g_strdup_printf ("Metadata #~ stream missing"));
if (!ctx->metadata_streams [GUID_STREAM].size)
ADD_ERROR (ctx, g_strdup_printf ("Metadata guid stream missing"));
}
static void
verify_tables_schema (VerifyContext *ctx)
{
OffsetAndSize tables_area = ctx->metadata_streams [TILDE_STREAM];
unsigned offset = tables_area.offset;
const char *ptr = ctx->data + offset;
guint64 valid_tables;
guint32 count;
int i;
if (tables_area.size < 24)
ADD_ERROR (ctx, g_strdup_printf ("Table schemata size (%d) too small to for initial decoding (requires 24 bytes)", tables_area.size));
if (ptr [4] != 2 && ptr [4] != 1)
ADD_ERROR (ctx, g_strdup_printf ("Invalid table schemata major version %d, expected 2", ptr [4]));
if (ptr [5] != 0)
ADD_ERROR (ctx, g_strdup_printf ("Invalid table schemata minor version %d, expected 0", ptr [5]));
if ((ptr [6] & ~0x7) != 0)
ADD_ERROR (ctx, g_strdup_printf ("Invalid table schemata heap sizes 0x%02x, only bits 0, 1 and 2 can be set", ((unsigned char *) ptr) [6]));
valid_tables = read64 (ptr + 8);
count = 0;
for (i = 0; i < 64; ++i) {
if (!(valid_tables & ((guint64)1 << i)))
continue;
/*MS Extensions: 0x3 0x5 0x7 0x13 0x16
Unused: 0x1E 0x1F 0x2D-0x3F
We don't care about the MS extensions.*/
if (i == 0x3 || i == 0x5 || i == 0x7 || i == 0x13 || i == 0x16)
ADD_ERROR (ctx, g_strdup_printf ("The metadata verifier doesn't support MS specific table %x", i));
if (i == 0x1E || i == 0x1F || i >= 0x2D)
ADD_ERROR (ctx, g_strdup_printf ("Invalid table %x", i));
++count;
}
if (tables_area.size < 24 + count * 4)
ADD_ERROR (ctx, g_strdup_printf ("Table schemata size (%d) too small to for decoding row counts (requires %d bytes)", tables_area.size, 24 + count * 4));
ptr += 24;
for (i = 0; i < 64; ++i) {
if (valid_tables & ((guint64)1 << i)) {
guint32 row_count = read32 (ptr);
if (row_count > (1 << 24) - 1)
ADD_ERROR (ctx, g_strdup_printf ("Invalid Table %d row count: %d. Mono only supports 16777215 rows", i, row_count));
ptr += 4;
}
}
}
/*----------nothing from here on can use data_directory or metadata_streams ---*/
static guint32
get_col_offset (VerifyContext *ctx, int table, int column)
{
guint32 bitfield = ctx->image->tables [table].size_bitfield;
guint32 offset = 0;
while (column-- > 0)
offset += mono_metadata_table_size (bitfield, column);
return offset;
}
static guint32
get_col_size (VerifyContext *ctx, int table, int column)
{
return mono_metadata_table_size (ctx->image->tables [table].size_bitfield, column);
}
static OffsetAndSize
get_metadata_stream (VerifyContext *ctx, MonoStreamHeader *header)
{
OffsetAndSize res;
res.offset = header->data - ctx->data;
res.size = header->size;
return res;
}
static gboolean
is_valid_string_full_with_image (MonoImage *image, guint32 offset, gboolean allow_empty)
{
guint32 heap_offset = (char*)image->heap_strings.data - image->raw_data;
guint32 heap_size = image->heap_strings.size;
glong length;
const char *data = image->raw_data + heap_offset;
if (offset >= heap_size)
return FALSE;
if (CHECK_ADDP_OVERFLOW_UN (data, offset))
return FALSE;
if (!mono_utf8_validate_and_len_with_bounds (data + offset, heap_size - offset, &length, NULL))
return FALSE;
return allow_empty || length > 0;
}
static gboolean
is_valid_string_full (VerifyContext *ctx, guint32 offset, gboolean allow_empty)
{
return is_valid_string_full_with_image (ctx->image, offset, allow_empty);
}
static gboolean
is_valid_string (VerifyContext *ctx, guint32 offset)
{
return is_valid_string_full (ctx, offset, TRUE);
}
static gboolean
is_valid_non_empty_string (VerifyContext *ctx, guint32 offset)
{
return is_valid_string_full (ctx, offset, FALSE);
}
static gboolean
is_valid_guid (VerifyContext *ctx, guint32 offset)
{
OffsetAndSize guids = get_metadata_stream (ctx, &ctx->image->heap_guid);
return guids.size >= 8 && guids.size - 8 >= offset;
}
static guint32
get_coded_index_token (int token_kind, guint32 coded_token)
{
guint32 bits = coded_index_desc [token_kind];
return coded_token >> bits;
}
static guint32
get_coded_index_table (int kind, guint32 coded_token)
{
guint32 idx, bits = coded_index_desc [kind];
kind += 2;
idx = coded_token & ((1 << bits) - 1);
return coded_index_desc [kind + idx];
}