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 
12 namespace mozilla {
13 namespace sandboxing {
14 
15 static LogFunction sLogFunction = nullptr;
16 
17 void
ProvideLogFunction(LogFunction aLogFunction)18 ProvideLogFunction(LogFunction aLogFunction)
19 {
20   sLogFunction = aLogFunction;
21 }
22 
23 void
LogBlocked(const char * aFunctionName,const char * aContext,uint32_t aFramesToSkip)24 LogBlocked(const char* aFunctionName, const char* aContext, uint32_t aFramesToSkip)
25 {
26   if (sLogFunction) {
27     sLogFunction("BLOCKED", aFunctionName, aContext,
28                  /* aShouldLogStackTrace */ true, aFramesToSkip);
29   }
30 }
31 
32 void
LogBlocked(const char * aFunctionName,const wchar_t * aContext)33 LogBlocked(const char* aFunctionName, const wchar_t* aContext)
34 {
35   if (sLogFunction) {
36     // Skip an extra frame to allow for this function.
37     LogBlocked(aFunctionName, base::WideToUTF8(aContext).c_str(),
38                /* aFramesToSkip */ 3);
39   }
40 }
41 
42 void
LogBlocked(const char * aFunctionName,const wchar_t * aContext,uint16_t aLengthInBytes)43 LogBlocked(const char* aFunctionName, const wchar_t* aContext,
44            uint16_t aLengthInBytes)
45 {
46   if (sLogFunction) {
47     // Skip an extra frame to allow for this function.
48     LogBlocked(aFunctionName,
49                base::WideToUTF8(std::wstring(aContext, aLengthInBytes / sizeof(wchar_t))).c_str(),
50                /* aFramesToSkip */ 3);
51   }
52 }
53 
54 void
LogAllowed(const char * aFunctionName,const char * aContext)55 LogAllowed(const char* aFunctionName, const char* aContext)
56 {
57   if (sLogFunction) {
58     sLogFunction("Broker ALLOWED", aFunctionName, aContext,
59                  /* aShouldLogStackTrace */ false, /* aFramesToSkip */ 0);
60   }
61 }
62 
63 void
LogAllowed(const char * aFunctionName,const wchar_t * aContext)64 LogAllowed(const char* aFunctionName, const wchar_t* aContext)
65 {
66   if (sLogFunction) {
67     LogAllowed(aFunctionName, base::WideToUTF8(aContext).c_str());
68   }
69 }
70 
71 void
LogAllowed(const char * aFunctionName,const wchar_t * aContext,uint16_t aLengthInBytes)72 LogAllowed(const char* aFunctionName, const wchar_t* aContext,
73            uint16_t aLengthInBytes)
74 {
75   if (sLogFunction) {
76     LogAllowed(aFunctionName,
77                base::WideToUTF8(std::wstring(aContext, aLengthInBytes / sizeof(wchar_t))).c_str());
78   }
79 }
80 
81 } // sandboxing
82 } // mozilla
83