File: EventLogger.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (408 lines) | stat: -rw-r--r-- 17,896 bytes parent folder | download | duplicates (7)
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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

namespace System.Runtime.Diagnostics
{
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Globalization;
    using System.Runtime.Interop;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Runtime.Versioning;
    using System.Security;
    using System.Security.Permissions;
    using System.Security.Principal;
    using System.Text;
    using System.Diagnostics.CodeAnalysis;

    sealed class EventLogger
    {
        [Fx.Tag.SecurityNote(Miscellaneous = "RequiresReview - In PT log no more than 5 events.")]
        const int MaxEventLogsInPT = 5;

        [SecurityCritical]
        static int logCountForPT;
        static bool canLogEvent = true;

        DiagnosticTraceBase diagnosticTrace;        
        [Fx.Tag.SecurityNote(Critical = "Protect the string that defines the event source name.",
            Safe = "It demands UnmanagedCode=true so PT cannot call.")]
        [SecurityCritical]
        string eventLogSourceName;
        bool isInPartialTrust;

        EventLogger()
        {
            this.isInPartialTrust = IsInPartialTrust();
        }

        [Obsolete("For System.Runtime.dll use only. Call FxTrace.EventLog instead")]
        public EventLogger(string eventLogSourceName, DiagnosticTraceBase diagnosticTrace)
        {
            try
            {
                this.diagnosticTrace = diagnosticTrace;
                //set diagnostics trace prior to calling SafeSetLogSourceName
                if (canLogEvent)
                {
                    SafeSetLogSourceName(eventLogSourceName);
                }
            }
            catch (SecurityException)
            {
                // running in PT, do not try to log events anymore
                canLogEvent = false;
                // not throwing exception on purpose
            }
        }       

        [Fx.Tag.SecurityNote(Critical = "Unsafe method to create event logger (sets the event source name).")]
        [SecurityCritical]
        public static EventLogger UnsafeCreateEventLogger(string eventLogSourceName, DiagnosticTraceBase diagnosticTrace)
        {
            EventLogger logger = new EventLogger();
            logger.SetLogSourceName(eventLogSourceName, diagnosticTrace);
            return logger;
        }       

        [Fx.Tag.SecurityNote(Critical = "Logs event to the event log and asserts Unmanaged code.")]
        [SecurityCritical]
        public void UnsafeLogEvent(TraceEventType type, ushort eventLogCategory, uint eventId, bool shouldTrace, params string[] values)
        {
            if (logCountForPT < MaxEventLogsInPT)
            {
                try
                {
                    // Vista introduces a new limitation:  a much smaller max
                    // event log entry size that we need to track.  All strings cannot
                    // exceed 31839 characters in length when totalled together.
                    // Choose a max length of 25600 characters (25k) to allow for
                    // buffer since this max length may be reduced without warning.
                    const int MaxEventLogEntryLength = 25600;

                    int eventLogEntryLength = 0;

                    string[] logValues = new string[values.Length + 2];
                    for (int i = 0; i < values.Length; ++i)
                    {
                        string stringValue = values[i];
                        if (!string.IsNullOrEmpty(stringValue))
                        {
                            stringValue = NormalizeEventLogParameter(stringValue);
                        }
                        else
                        {
                            stringValue = String.Empty;
                        }

                        logValues[i] = stringValue;
                        eventLogEntryLength += stringValue.Length + 1;
                    }

                    string normalizedProcessName = NormalizeEventLogParameter(UnsafeGetProcessName());
                    logValues[logValues.Length - 2] = normalizedProcessName;
                    eventLogEntryLength += (normalizedProcessName.Length + 1);

                    string invariantProcessId = UnsafeGetProcessId().ToString(CultureInfo.InvariantCulture);
                    logValues[logValues.Length - 1] = invariantProcessId;
                    eventLogEntryLength += (invariantProcessId.Length + 1);

                    // If current event log entry length is greater than max length
                    // need to truncate to max length.  This probably means that we
                    // have a very long exception and stack trace in our parameter
                    // strings.  Truncate each string by MaxEventLogEntryLength
                    // divided by number of strings in the entry.
                    // Truncation algorithm is overly aggressive by design to 
                    // simplify the code change due to Product Cycle timing.
                    if (eventLogEntryLength > MaxEventLogEntryLength)
                    {
                        // logValues.Length is always > 0 (minimum value = 2)
                        // Subtract one to insure string ends in '\0'
                        int truncationLength = (MaxEventLogEntryLength / logValues.Length) - 1;

                        for (int i = 0; i < logValues.Length; i++)
                        {
                            if (logValues[i].Length > truncationLength)
                            {
                                logValues[i] = logValues[i].Substring(0, truncationLength);
                            }
                        }
                    }

                    SecurityIdentifier sid = WindowsIdentity.GetCurrent().User;
                    byte[] sidBA = new byte[sid.BinaryLength];
                    sid.GetBinaryForm(sidBA, 0);
                    IntPtr[] stringRoots = new IntPtr[logValues.Length];
                    GCHandle stringsRootHandle = new GCHandle(); 
                    GCHandle[] stringHandles = null;

                    try
                    {
                        stringsRootHandle = GCHandle.Alloc(stringRoots, GCHandleType.Pinned);
                        stringHandles = new GCHandle[logValues.Length];
                        for (int strIndex = 0; strIndex < logValues.Length; strIndex++)
                        {
                            stringHandles[strIndex] = GCHandle.Alloc(logValues[strIndex], GCHandleType.Pinned);
                            stringRoots[strIndex] = stringHandles[strIndex].AddrOfPinnedObject();
                        }
                        UnsafeWriteEventLog(type, eventLogCategory, eventId, logValues, sidBA, stringsRootHandle);
                    }
                    finally
                    {
                        if (stringsRootHandle.AddrOfPinnedObject() != IntPtr.Zero)
                        {
                            stringsRootHandle.Free();
                        }
                        if (stringHandles != null)
                        {
                            foreach (GCHandle gcHandle in stringHandles)
                            {
                                if (gcHandle != null)
                                {
                                    gcHandle.Free();
                                }
                            }
                        }
                    }
                    
                    if (shouldTrace && this.diagnosticTrace != null && this.diagnosticTrace.IsEnabled())
                    {
                        const int RequiredValueCount = 4;
                        Dictionary<string, string> eventValues = new Dictionary<string, string>(logValues.Length + RequiredValueCount);
                        eventValues["CategoryID.Name"] = "EventLogCategory";
                        eventValues["CategoryID.Value"] = eventLogCategory.ToString(CultureInfo.InvariantCulture);
                        eventValues["InstanceID.Name"] = "EventId";
                        eventValues["InstanceID.Value"] = eventId.ToString(CultureInfo.InvariantCulture);
                        for (int i = 0; i < values.Length; ++i)
                        {
                            eventValues.Add("Value" + i.ToString(CultureInfo.InvariantCulture), values[i] == null ? string.Empty : DiagnosticTraceBase.XmlEncode(values[i]));
                        }

                        this.diagnosticTrace.TraceEventLogEvent(type, new DictionaryTraceRecord((eventValues)));
                    }
                }
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }
                    // If not fatal, just eat the exception
                }
                // In PT, we only limit 5 event logging per session
                if (this.isInPartialTrust)
                {
                    logCountForPT++;
                }
            }
        }

        public void LogEvent(TraceEventType type, ushort eventLogCategory, uint eventId, bool shouldTrace, params string[] values)
        {
            if (canLogEvent)
            {
                try
                {
                    SafeLogEvent(type, eventLogCategory, eventId, shouldTrace, values);
                }
                catch (SecurityException ex)
                {
                    // running in PT, do not try to log events anymore
                    canLogEvent = false;

                    // not throwing exception on purpose
                    if (shouldTrace)
                    {
                        Fx.Exception.TraceHandledException(ex, TraceEventType.Information);
                    }
                }
            }
        }

        public void LogEvent(TraceEventType type, ushort eventLogCategory, uint eventId, params string[] values)
        {
            this.LogEvent(type, eventLogCategory, eventId, true, values);
        }

        // Converts incompatible serverity enumeration TraceEventType into EventLogEntryType
        static EventLogEntryType EventLogEntryTypeFromEventType(TraceEventType type)
        {
            EventLogEntryType retval = EventLogEntryType.Information;
            switch (type)
            {
                case TraceEventType.Critical:
                case TraceEventType.Error:
                    retval = EventLogEntryType.Error;
                    break;
                case TraceEventType.Warning:
                    retval = EventLogEntryType.Warning;
                    break;
            }
            return retval;
        }

        [Fx.Tag.SecurityNote(Critical = "Logs event to the event log by calling unsafe method.",
           Safe = "Demands the same permission that is asserted by the unsafe method.")]
        [SecuritySafeCritical]
        [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
        void SafeLogEvent(TraceEventType type, ushort eventLogCategory, uint eventId, bool shouldTrace, params string[] values)
        {
            UnsafeLogEvent(type, eventLogCategory, eventId, shouldTrace, values);
        }

        [Fx.Tag.SecurityNote(Critical = "Protect the string that defines the event source name.",
            Safe = "It demands UnmanagedCode=true so PT cannot call.")]
        [SecuritySafeCritical]
        [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
        void SafeSetLogSourceName(string eventLogSourceName)
        {
            this.eventLogSourceName = eventLogSourceName;
        }

        [Fx.Tag.SecurityNote(Critical = "Sets event source name.")]
        [SecurityCritical]
        void SetLogSourceName(string eventLogSourceName, DiagnosticTraceBase diagnosticTrace)
        {
            this.eventLogSourceName = eventLogSourceName;
            this.diagnosticTrace = diagnosticTrace;
        }

        [Fx.Tag.SecurityNote(Critical = "Satisfies a LinkDemand for 'PermissionSetAttribute' on type 'Process' when calling method GetCurrentProcess", 
            Safe = "Does not leak any resource")]
        [SecuritySafeCritical]
        [SuppressMessage(FxCop.Category.Security, FxCop.Rule.DoNotIndirectlyExposeMethodsWithLinkDemands,
                Justification = "SecuritySafeCritical method, Does not expose critical resources returned by methods with Link Demands")]
        bool IsInPartialTrust()
        {
            bool retval = false;
            try
            {
                using (Process process = Process.GetCurrentProcess())
                {
                    retval = string.IsNullOrEmpty(process.ProcessName);
                }
            }
            catch (SecurityException)
            {
                // we are just testing, ignore exception
                retval = true;
            }

            return retval;
        }

        [SecurityCritical]
        [Fx.Tag.SecurityNote(Critical = "Accesses security critical code RegisterEventSource and ReportEvent")]
        [SecurityPermission(SecurityAction.Assert, UnmanagedCode = true)]
        [ResourceConsumption(ResourceScope.Machine)]
        [SuppressMessage(FxCop.Category.Security, FxCop.Rule.SecureAsserts)]
        void UnsafeWriteEventLog(TraceEventType type, ushort eventLogCategory, uint eventId, string[] logValues, byte[] sidBA, GCHandle stringsRootHandle)
        {
            using (SafeEventLogWriteHandle handle = SafeEventLogWriteHandle.RegisterEventSource(null, this.eventLogSourceName))
            {
                if (handle != null)
                {
                    HandleRef data = new HandleRef(handle, stringsRootHandle.AddrOfPinnedObject());
                    UnsafeNativeMethods.ReportEvent(
                         handle,
                         (ushort)EventLogEntryTypeFromEventType(type),
                         eventLogCategory,
                         eventId,
                         sidBA,
                         (ushort)logValues.Length,
                         0,
                         data,
                         null);
                }
            }
        }

        [Fx.Tag.SecurityNote(Critical = "Satisfies a LinkDemand for 'PermissionSetAttribute' on type 'Process' when calling method GetCurrentProcess",
            Safe = "Does not leak any resource")]
        [SecurityCritical]
        [SecurityPermission(SecurityAction.Assert, UnmanagedCode = true)]
        [MethodImpl(MethodImplOptions.NoInlining)]
        [SuppressMessage(FxCop.Category.Security, FxCop.Rule.SecureAsserts)]
        [SuppressMessage(FxCop.Category.Security, FxCop.Rule.DoNotIndirectlyExposeMethodsWithLinkDemands,
                Justification = "SecurityCritical method, Does not expose critical resources returned by methods with Link Demands")]
        string UnsafeGetProcessName()
        {
            string retval = null;
            using (Process process = Process.GetCurrentProcess())
            {
                retval = process.ProcessName;
            }
            return retval;
        }

        [Fx.Tag.SecurityNote(Critical = "Satisfies a LinkDemand for 'PermissionSetAttribute' on type 'Process' when calling method GetCurrentProcess",
            Safe = "Does not leak any resource")]
        [SecurityCritical]
        [SecurityPermission(SecurityAction.Assert, UnmanagedCode = true)]
        [MethodImpl(MethodImplOptions.NoInlining)]
        [SuppressMessage(FxCop.Category.Security, FxCop.Rule.SecureAsserts)]
        [SuppressMessage(FxCop.Category.Security, FxCop.Rule.DoNotIndirectlyExposeMethodsWithLinkDemands,
                Justification = "SecurityCritical method, Does not expose critical resources returned by methods with Link Demands")]
        int UnsafeGetProcessId()
        {
            int retval = -1;
            using (Process process = Process.GetCurrentProcess())
            {
                retval = process.Id;
            }
            return retval;
        }
        
        internal static string NormalizeEventLogParameter(string eventLogParameter)
        {
            if (eventLogParameter.IndexOf('%') < 0)
            {
                return eventLogParameter;
            }

            StringBuilder parameterBuilder = null;
            int len = eventLogParameter.Length;
            for (int i = 0; i < len; ++i)
            {
                char c = eventLogParameter[i];

                // Not '%'
                if (c != '%')
                {
                    if (parameterBuilder != null) parameterBuilder.Append(c);
                    continue;
                }

                // Last char
                if ((i + 1) >= len)
                {
                    if (parameterBuilder != null) parameterBuilder.Append(c);
                    continue;
                }

                // Next char is not number
                if (eventLogParameter[i + 1] < '0' || eventLogParameter[i + 1] > '9')
                {
                    if (parameterBuilder != null) parameterBuilder.Append(c);
                    continue;
                }

                // initialize str builder
                if (parameterBuilder == null)
                {
                    parameterBuilder = new StringBuilder(len + 2);
                    for (int j = 0; j < i; ++j)
                    {
                        parameterBuilder.Append(eventLogParameter[j]);
                    }
                }
                parameterBuilder.Append(c);
                parameterBuilder.Append(' ');
            }

            return parameterBuilder != null ? parameterBuilder.ToString() : eventLogParameter;
        }
    }
}