summaryrefslogtreecommitdiffstats
path: root/security/sandbox/linux/reporter/SandboxReporterWrappers.cpp
blob: 755e8e858d98961477f7d1d47cc9d915f7fb1748 (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozISandboxReporter.h"
#include "SandboxReporter.h"

#include <time.h>

#include "mozilla/Assertions.h"
#include "mozilla/Components.h"
#include "nsCOMPtr.h"
#include "nsPrintfCString.h"
#include "nsTArray.h"
#include "nsXULAppAPI.h"

using namespace mozilla;

namespace mozilla {

class SandboxReportWrapper final : public mozISandboxReport {
 public:
  NS_DECL_ISUPPORTS
  NS_DECL_MOZISANDBOXREPORT

  explicit SandboxReportWrapper(const SandboxReport& aReport)
      : mReport(aReport) {}

 private:
  ~SandboxReportWrapper() = default;
  SandboxReport mReport;
};

NS_IMPL_ISUPPORTS(SandboxReportWrapper, mozISandboxReport)

/* readonly attribute uint64_t msecAgo; */
NS_IMETHODIMP SandboxReportWrapper::GetMsecAgo(uint64_t* aMsec) {
  struct timespec then = mReport.mTime, now = {0, 0};
  clock_gettime(CLOCK_MONOTONIC_COARSE, &now);

  const uint64_t now_msec = uint64_t(now.tv_sec) * 1000 + now.tv_nsec / 1000000;
  const uint64_t then_msec =
      uint64_t(then.tv_sec) * 1000 + then.tv_nsec / 1000000;
  MOZ_DIAGNOSTIC_ASSERT(now_msec >= then_msec);
  if (now_msec >= then_msec) {
    *aMsec = now_msec - then_msec;
  } else {
    *aMsec = 0;
  }
  return NS_OK;
}

/* readonly attribute int32_t pid; */
NS_IMETHODIMP SandboxReportWrapper::GetPid(int32_t* aPid) {
  *aPid = mReport.mPid;
  return NS_OK;
}

/* readonly attribute int32_t tid; */
NS_IMETHODIMP SandboxReportWrapper::GetTid(int32_t* aTid) {
  *aTid = mReport.mTid;
  return NS_OK;
}

/* readonly attribute ACString procType; */
NS_IMETHODIMP SandboxReportWrapper::GetProcType(nsACString& aProcType) {
  switch (mReport.mProcType) {
    case SandboxReport::ProcType::CONTENT:
      aProcType.AssignLiteral("content");
      return NS_OK;
    case SandboxReport::ProcType::FILE:
      aProcType.AssignLiteral("file");
      return NS_OK;
    case SandboxReport::ProcType::MEDIA_PLUGIN:
      aProcType.AssignLiteral("mediaPlugin");
      return NS_OK;
    case SandboxReport::ProcType::RDD:
      aProcType.AssignLiteral("dataDecoder");
      return NS_OK;
    case SandboxReport::ProcType::SOCKET_PROCESS:
      aProcType.AssignLiteral("socketProcess");
      return NS_OK;
    case SandboxReport::ProcType::UTILITY:
      aProcType.AssignLiteral("utility");
      return NS_OK;
    default:
      MOZ_ASSERT(false);
      return NS_ERROR_UNEXPECTED;
  }
}

/* readonly attribute uint32_t syscall; */
NS_IMETHODIMP SandboxReportWrapper::GetSyscall(uint32_t* aSyscall) {
  *aSyscall = static_cast<uint32_t>(mReport.mSyscall);
  MOZ_ASSERT(static_cast<SandboxReport::ULong>(*aSyscall) == mReport.mSyscall);
  return NS_OK;
}

/* readonly attribute uint32_t numArgs; */
NS_IMETHODIMP SandboxReportWrapper::GetNumArgs(uint32_t* aNumArgs) {
  *aNumArgs = static_cast<uint32_t>(kSandboxSyscallArguments);
  return NS_OK;
}

/* ACString getArg (in uint32_t aIndex); */
NS_IMETHODIMP SandboxReportWrapper::GetArg(uint32_t aIndex,
                                           nsACString& aRetval) {
  if (aIndex >= kSandboxSyscallArguments) {
    return NS_ERROR_INVALID_ARG;
  }
  const auto arg = mReport.mArgs[aIndex];
  nsAutoCString str;
  // Use decimal for smaller numbers (more likely ints) and hex for
  // larger (more likely pointers).  This cutoff is arbitrary.
  if (arg >= 1000000) {
    str.AppendLiteral("0x");
    str.AppendInt(arg, 16);
  } else {
    str.AppendInt(arg, 10);
  }
  aRetval = str;
  return NS_OK;
}

class SandboxReportArray final : public mozISandboxReportArray {
 public:
  NS_DECL_ISUPPORTS
  NS_DECL_MOZISANDBOXREPORTARRAY

  explicit SandboxReportArray(SandboxReporter::Snapshot&& aSnap)
      : mOffset(aSnap.mOffset), mArray(std::move(aSnap.mReports)) {}

 private:
  ~SandboxReportArray() = default;
  uint64_t mOffset;
  nsTArray<SandboxReport> mArray;
};

NS_IMPL_ISUPPORTS(SandboxReportArray, mozISandboxReportArray)

/* readonly attribute uint64_t begin; */
NS_IMETHODIMP SandboxReportArray::GetBegin(uint64_t* aBegin) {
  *aBegin = mOffset;
  return NS_OK;
}

/* readonly attribute uint64_t end; */
NS_IMETHODIMP SandboxReportArray::GetEnd(uint64_t* aEnd) {
  *aEnd = mOffset + mArray.Length();
  return NS_OK;
}

/* mozISandboxReport getElement (in uint64_t aIndex); */
NS_IMETHODIMP SandboxReportArray::GetElement(uint64_t aIndex,
                                             mozISandboxReport** aRetval) {
  uint64_t relIndex = aIndex - mOffset;
  if (relIndex >= mArray.Length()) {
    return NS_ERROR_INVALID_ARG;
  }

  nsCOMPtr<mozISandboxReport> wrapper =
      new SandboxReportWrapper(mArray[relIndex]);
  wrapper.forget(aRetval);
  return NS_OK;
}

class SandboxReporterWrapper final : public mozISandboxReporter {
 public:
  NS_DECL_ISUPPORTS
  NS_DECL_MOZISANDBOXREPORTER

  SandboxReporterWrapper() = default;

 private:
  ~SandboxReporterWrapper() = default;
};

NS_IMPL_ISUPPORTS(SandboxReporterWrapper, mozISandboxReporter)

/* mozISandboxReportArray snapshot(); */
NS_IMETHODIMP SandboxReporterWrapper::Snapshot(
    mozISandboxReportArray** aRetval) {
  if (!XRE_IsParentProcess()) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  nsCOMPtr<mozISandboxReportArray> wrapper =
      new SandboxReportArray(SandboxReporter::Singleton()->GetSnapshot());
  wrapper.forget(aRetval);
  return NS_OK;
}

}  // namespace mozilla

NS_IMPL_COMPONENT_FACTORY(mozISandboxReporter) {
  return MakeAndAddRef<SandboxReporterWrapper>().downcast<nsISupports>();
}