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
|
/* -*- 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 https://mozilla.org/MPL/2.0/. */
#include <iostream>
#include "FuzzingInterface.h"
#include "nsComponentManagerUtils.h"
#include "nsCOMPtr.h"
#include "nsIZipReader.h"
#include "nsNetUtil.h"
#include "nsNetCID.h"
#include "nsPrintfCString.h"
#include "nsString.h"
#include "mozilla/Span.h"
#include "mozilla/Unused.h"
#include "nsIInputStream.h"
#include "nsIStringEnumerator.h"
enum FuzzMethodType {
eTest = 0,
eGetEntry,
eHasEntry,
eFindEntries,
eInputStream,
eOpenInner,
eLastMethod,
};
static NS_DEFINE_CID(kZipReaderCID, NS_ZIPREADER_CID);
template <typename T>
T jar_get_num(char** buf, size_t* size) {
if (sizeof(T) > *size) {
return 0;
}
T* iptr = reinterpret_cast<T*>(*buf);
*buf += sizeof(T);
*size -= sizeof(T);
return *iptr;
}
nsAutoCString jar_get_string(char** buf, size_t* size) {
uint8_t len = jar_get_num<uint8_t>(buf, size);
if (len > *size) {
len = static_cast<uint8_t>(*size);
}
nsAutoCString str(*buf, len);
*buf += len;
*size -= len;
return str;
}
nsresult FuzzEntries(char** buf, size_t* size, nsIZipReader* aReader,
const nsACString& aName) {
uint8_t iters = jar_get_num<uint8_t>(buf, size);
nsresult rv;
for (uint8_t i = 0; i < iters; ++i) {
nsAutoCString out;
uint64_t written;
nsCOMPtr<nsIZipEntry> entry;
nsCOMPtr<nsIInputStream> stream;
switch (jar_get_num<uint8_t>(buf, size) % eLastMethod) {
case eTest: {
rv = aReader->Test(aName);
NS_ENSURE_SUCCESS(rv, rv);
break;
}
case eGetEntry: {
rv = aReader->GetEntry(aName, getter_AddRefs(entry));
NS_ENSURE_SUCCESS(rv, rv);
break;
}
case eHasEntry: {
bool has = false;
rv = aReader->HasEntry(aName, &has);
NS_ENSURE_SUCCESS(rv, rv);
break;
}
case eInputStream:
rv = aReader->GetInputStream(aName, getter_AddRefs(stream));
NS_ENSURE_SUCCESS(rv, rv);
if (NS_FAILED(rv)) {
break;
}
rv = NS_ReadInputStreamToString(stream, out, -1, &written);
NS_ENSURE_SUCCESS(rv, rv);
break;
default:
break;
}
}
return NS_OK;
}
nsresult FuzzReader(char** buf, size_t* size, nsIZipReader* aReader) {
nsresult rv;
nsAutoCString name;
nsCOMPtr<nsIZipEntry> entry;
bool has = false;
nsAutoCString pattern;
nsCOMPtr<nsIUTF8StringEnumerator> enumerator;
nsCOMPtr<nsIInputStream> stream;
bool hasMore;
nsAutoCString out;
uint64_t written;
nsCOMPtr<nsIZipReader> newReader = do_CreateInstance(kZipReaderCID, &rv);
switch (jar_get_num<uint8_t>(buf, size) % eLastMethod) {
case eTest:
rv = aReader->Test(""_ns);
NS_ENSURE_SUCCESS(rv, rv);
break;
case eGetEntry:
name = jar_get_string(buf, size);
rv = aReader->GetEntry(name, getter_AddRefs(entry));
NS_ENSURE_SUCCESS(rv, rv);
break;
case eHasEntry:
name = jar_get_string(buf, size);
rv = aReader->HasEntry(name, &has);
NS_ENSURE_SUCCESS(rv, rv);
break;
case eFindEntries:
pattern = jar_get_string(buf, size);
rv = aReader->FindEntries(pattern, getter_AddRefs(enumerator));
NS_ENSURE_SUCCESS(rv, rv);
while (NS_SUCCEEDED(enumerator->HasMore(&hasMore)) && hasMore) {
if (NS_FAILED(enumerator->GetNext(name))) {
break;
}
rv = FuzzEntries(buf, size, aReader, name);
NS_ENSURE_SUCCESS(rv, rv);
}
break;
case eInputStream:
name = jar_get_string(buf, size);
rv = aReader->GetInputStream(name, getter_AddRefs(stream));
NS_ENSURE_SUCCESS(rv, rv);
rv = NS_ReadInputStreamToString(stream, out, -1, &written);
NS_ENSURE_SUCCESS(rv, rv);
break;
case eOpenInner:
name = jar_get_string(buf, size);
rv = newReader->OpenInner(aReader, name);
NS_ENSURE_SUCCESS(rv, rv);
rv = FuzzReader(buf, size, newReader);
NS_ENSURE_SUCCESS(rv, rv);
break;
default:
break;
}
return rv;
}
static int FuzzingRunJARParser(const uint8_t* data, size_t size) {
char* buf = (char*)data;
nsresult rv;
nsCOMPtr<nsIURI> uri;
nsAutoCString jardata = jar_get_string(&buf, &size);
nsCOMPtr<nsIZipReader> reader = do_CreateInstance(kZipReaderCID, &rv);
rv = reader->OpenMemory((void*)jardata.get(), jardata.Length());
NS_ENSURE_SUCCESS(rv, 0);
#if 0
// For easily exporting the last test case that triggered a crash.
FILE * f = fopen("/tmp/input.jar", "wb");
fwrite((void*)jardata.get(), 1, jardata.Length(), f);
fclose(f);
#endif
uint8_t iters = jar_get_num<uint8_t>(&buf, &size);
for (uint8_t i = 0; i < iters; ++i) {
rv = FuzzReader(&buf, &size, reader);
NS_ENSURE_SUCCESS(rv, 0);
}
return 0;
}
MOZ_FUZZING_INTERFACE_RAW(nullptr, FuzzingRunJARParser, JARParser);
|