1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "sandboxLogging.h"
8 
9 #include "base/strings/utf_string_conversions.h"
10 #include "sandbox/win/src/sandbox_policy.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/StackWalk.h"
13 
14 namespace mozilla {
15 namespace sandboxing {
16 
17 static LogFunction sLogFunction = nullptr;
18 
19 void
ProvideLogFunction(LogFunction aLogFunction)20 ProvideLogFunction(LogFunction aLogFunction)
21 {
22   sLogFunction = aLogFunction;
23 }
24 
25 static void
LogBlocked(const char * aFunctionName,const char * aContext,const void * aFirstFramePC)26 LogBlocked(const char* aFunctionName, const char* aContext, const void* aFirstFramePC)
27 {
28   if (sLogFunction) {
29     sLogFunction("BLOCKED", aFunctionName, aContext,
30                  /* aShouldLogStackTrace */ true, aFirstFramePC);
31   }
32 }
33 
34 MOZ_NEVER_INLINE void
LogBlocked(const char * aFunctionName,const char * aContext)35 LogBlocked(const char* aFunctionName, const char* aContext)
36 {
37   if (sLogFunction) {
38     LogBlocked(aFunctionName, aContext, CallerPC());
39   }
40 }
41 
42 MOZ_NEVER_INLINE void
LogBlocked(const char * aFunctionName,const wchar_t * aContext)43 LogBlocked(const char* aFunctionName, const wchar_t* aContext)
44 {
45   if (sLogFunction) {
46     LogBlocked(aFunctionName, base::WideToUTF8(aContext).c_str(), CallerPC());
47   }
48 }
49 
50 MOZ_NEVER_INLINE void
LogBlocked(const char * aFunctionName,const wchar_t * aContext,uint16_t aLengthInBytes)51 LogBlocked(const char* aFunctionName, const wchar_t* aContext,
52            uint16_t aLengthInBytes)
53 {
54   if (sLogFunction) {
55     LogBlocked(aFunctionName,
56                base::WideToUTF8(std::wstring(aContext, aLengthInBytes / sizeof(wchar_t))).c_str(),
57                CallerPC());
58   }
59 }
60 
61 void
LogAllowed(const char * aFunctionName,const char * aContext)62 LogAllowed(const char* aFunctionName, const char* aContext)
63 {
64   if (sLogFunction) {
65     sLogFunction("Broker ALLOWED", aFunctionName, aContext,
66                  /* aShouldLogStackTrace */ false, nullptr);
67   }
68 }
69 
70 void
LogAllowed(const char * aFunctionName,const wchar_t * aContext)71 LogAllowed(const char* aFunctionName, const wchar_t* aContext)
72 {
73   if (sLogFunction) {
74     LogAllowed(aFunctionName, base::WideToUTF8(aContext).c_str());
75   }
76 }
77 
78 void
LogAllowed(const char * aFunctionName,const wchar_t * aContext,uint16_t aLengthInBytes)79 LogAllowed(const char* aFunctionName, const wchar_t* aContext,
80            uint16_t aLengthInBytes)
81 {
82   if (sLogFunction) {
83     LogAllowed(aFunctionName,
84                base::WideToUTF8(std::wstring(aContext, aLengthInBytes / sizeof(wchar_t))).c_str());
85   }
86 }
87 
88 } // sandboxing
89 } // mozilla
90