2 * metadata-verify.c: Metadata verfication support
5 * Mono Project (http://www.mono-project.com)
7 * Copyright (C) 2005-2008 Novell, Inc. (http://www.novell.com)
10 #include <mono/metadata/object-internals.h>
11 #include <mono/metadata/verify.h>
12 #include <mono/metadata/verify-internals.h>
13 #include <mono/metadata/opcodes.h>
14 #include <mono/metadata/tabledefs.h>
15 #include <mono/metadata/reflection.h>
16 #include <mono/metadata/debug-helpers.h>
17 #include <mono/metadata/mono-endian.h>
18 #include <mono/metadata/metadata.h>
19 #include <mono/metadata/metadata-internals.h>
20 #include <mono/metadata/class-internals.h>
21 #include <mono/metadata/tokentype.h>
27 TODO add fail fast mode
28 TODO add PE32+ support
29 TODO verify the entry point RVA and content.
38 guint32 data_dir_count;
41 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
43 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
44 vinfo->info.status = __status; \
45 vinfo->info.message = ( __msg); \
46 vinfo->exception_type = (__exception); \
47 (__ctx)->errors = g_slist_prepend ((__ctx)->errors, vinfo); \
51 #define ADD_ERROR(__ctx, __msg) \
53 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
57 #define CHECK_STATE() do { if (!ctx.valid) goto cleanup; } while (0)
60 pe_signature_offset (VerifyContext *ctx)
62 return read32 (ctx->data + 0x3c);
66 verify_msdos_header (VerifyContext *ctx)
69 if (ctx->size < 128) {
70 ADD_ERROR (ctx, g_strdup ("Not enough space for the MS-DOS header"));
73 if (ctx->data [0] != 0x4d || ctx->data [1] != 0x5a)
74 ADD_ERROR (ctx, g_strdup ("Invalid MS-DOS watermark"));
75 lfanew = pe_signature_offset (ctx);
76 if (lfanew > ctx->size - 4)
77 ADD_ERROR (ctx, g_strdup ("MS-DOS lfanew offset points to outside of the file"));
81 verify_pe_header (VerifyContext *ctx)
83 guint32 offset = pe_signature_offset (ctx);
84 const char *pe_header = ctx->data + offset;
85 if (pe_header [0] != 'P' || pe_header [1] != 'E' ||pe_header [2] != 0 ||pe_header [3] != 0)
86 ADD_ERROR (ctx, g_strdup ("Invalid PE header watermark"));
90 if (offset > ctx->size - 20)
91 ADD_ERROR (ctx, g_strdup ("File with truncated pe header"));
92 if (read16 (pe_header) != 0x14c)
93 ADD_ERROR (ctx, g_strdup ("Invalid PE header Machine value"));
98 verify_pe_optional_header (VerifyContext *ctx)
100 guint32 offset = pe_signature_offset (ctx) + 4;
102 const char *pe_header = ctx->data + offset;
103 const char *pe_optional_header = pe_header + 20;
105 header_size = read16 (pe_header + 16);
108 if (header_size < 2) /*must be at least 2 or we won't be able to read magic*/
109 ADD_ERROR (ctx, g_strdup ("Invalid PE optional header size"));
111 if (offset > ctx->size - header_size || header_size > ctx->size) {
112 ADD_ERROR (ctx, g_strdup ("Invalid PE optional header size"));
116 if (read16 (pe_optional_header) == 0x10b) {
117 if (header_size != 224)
118 ADD_ERROR (ctx, g_strdup_printf ("Invalid optional header size %d", header_size));
120 if (read32 (pe_optional_header + 28) != 0x400000)
121 ADD_ERROR (ctx, g_strdup_printf ("Invalid Image base %x", read32 (pe_optional_header + 28)));
122 if (read32 (pe_optional_header + 32) != 0x2000)
123 ADD_ERROR (ctx, g_strdup_printf ("Invalid Section Aligmment %x", read32 (pe_optional_header + 32)));
124 if (read32 (pe_optional_header + 36) != 0x200)
125 ADD_ERROR (ctx, g_strdup_printf ("Invalid file Aligmment %x", read32 (pe_optional_header + 36)));
126 /* All the junk in the middle is irrelevant, specially for mono. */
127 ctx->data_dir_count = read32 (pe_optional_header + 92);
128 if (ctx->data_dir_count > 0x10)
129 ADD_ERROR (ctx, g_strdup_printf ("Too many data directories %x", ctx->data_dir_count));
131 if (read16 (pe_optional_header) == 0x20B)
132 ADD_ERROR (ctx, g_strdup ("Metadata verifier doesn't handle PE32+"));
134 ADD_ERROR (ctx, g_strdup_printf ("Invalid optional header magic %d", read16 (pe_optional_header)));
141 mono_image_verify (const char *data, guint32 size)
144 memset (&ctx, 0, sizeof (VerifyContext));
149 verify_msdos_header (&ctx);
151 verify_pe_header (&ctx);
153 verify_pe_optional_header (&ctx);