| 1 |
/* |
| 2 |
* Copyright 2006 The Android Open Source Project |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/core/SkStream.h" |
| 9 |
#include "include/core/SkString.h" |
| 10 |
#include "include/core/SkTypes.h" |
| 11 |
#include "include/private/base/SkTemplates.h" |
| 12 |
#include "include/private/base/SkTo.h" |
| 13 |
#include "src/xml/SkXMLParser.h" |
| •14 |
#include "src/base/SkRandom.h" |
| 15 |
#include "src/base/SkTime.h" |
| 16 |
#include "src/base/SkUtils.h" |
| 17 |
|
| 18 |
#include <expat.h> |
| 19 |
|
| 20 |
#include <vector> |
| 21 |
|
| 22 |
static char const* const gErrorStrings[] = { |
| 23 |
"empty or missing file ", |
| 24 |
"unknown element ", |
| 25 |
"unknown attribute name ", |
| 26 |
"error in attribute value ", |
| 27 |
"duplicate ID ", |
| 28 |
"unknown error " |
| 29 |
}; |
| 30 |
|
| 31 |
SkXMLParserError::SkXMLParserError() : fCode(kNoError), fLineNumber(-1), |
| 32 |
fNativeCode(-1) |
| 33 |
{ |
| 34 |
reset(); |
| 35 |
} |
| 36 |
|
| 37 |
SkXMLParserError::~SkXMLParserError() |
| 38 |
{ |
| 39 |
// need a virtual destructor for our subclasses |
| 40 |
} |
| 41 |
|
| 42 |
void SkXMLParserError::getErrorString(SkString* str) const |
| 43 |
{ |
| 44 |
SkASSERT(str); |
| 45 |
SkString temp; |
| 46 |
if (fCode != kNoError) { |
| 47 |
if ((unsigned)fCode < std::size(gErrorStrings)) |
| 48 |
temp.set(gErrorStrings[fCode - 1]); |
| 49 |
temp.append(fNoun); |
| 50 |
} else |
| 51 |
SkXMLParser::GetNativeErrorString(fNativeCode, &temp); |
| 52 |
str->append(temp); |
| 53 |
} |
| 54 |
|
| 55 |
void SkXMLParserError::reset() { |
| 56 |
fCode = kNoError; |
| 57 |
fLineNumber = -1; |
| 58 |
fNativeCode = -1; |
| 59 |
} |
| 60 |
|
| 61 |
//////////////// |
| 62 |
|
| 63 |
namespace { |
| 64 |
|
| •65 |
// Return a random number based on the clock to be consistent |
| 66 |
// across platforms yet hard to guess and not leaking information. |
| 67 |
static uint32_t get_hash_salt() { |
| 68 |
static const uint32_t s_salt = []() { |
| 69 |
double nsecs = SkTime::GetNSecs(); |
| 70 |
uint64_t timeVal = sk_bit_cast<uint64_t>(nsecs); |
| 71 |
uint32_t seed = static_cast<uint32_t>(timeVal ^ timeVal >> 32); |
| 72 |
|
| 73 |
SkRandom rand(seed); |
| 74 |
uint32_t salt = rand.nextU(); |
| 75 |
while (salt == 0) [[unlikely]] { |
| 76 |
// Expat hash seed must be non-zero or the default cPRNG will be used. |
| 77 |
salt = rand.nextU(); |
| 78 |
} |
| 79 |
return salt; |
| 80 |
}(); |
| 81 |
return s_salt; |
| 82 |
} |
| 83 |
|
| 84 |
const XML_Memory_Handling_Suite sk_XML_alloc = { |
| 85 |
sk_malloc_throw, |
| 86 |
sk_realloc_throw, |
| 87 |
sk_free |
| 88 |
}; |
| 89 |
|
| 90 |
struct ParsingContext { |
| 91 |
ParsingContext(SkXMLParser* parser) |
| 92 |
: fParser(parser) |
| 93 |
, fXMLParser(XML_ParserCreate_MM(nullptr, &sk_XML_alloc, nullptr)) { } |
| 94 |
|
| 95 |
void flushText() { |
| 96 |
if (!fBufferedText.empty()) { |
| 97 |
fParser->text(fBufferedText.data(), SkTo<int>(fBufferedText.size())); |
| 98 |
fBufferedText.clear(); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void appendText(const char* txt, size_t len) { |
| 103 |
fBufferedText.insert(fBufferedText.end(), txt, &txt[len]); |
| 104 |
} |
| 105 |
|
| 106 |
SkXMLParser* fParser; |
| 107 |
SkAutoTCallVProc<std::remove_pointer_t<XML_Parser>, XML_ParserFree> fXMLParser; |
| 108 |
|
| 109 |
private: |
| 110 |
std::vector<char> fBufferedText; |
| 111 |
}; |
| 112 |
|
| 113 |
#define HANDLER_CONTEXT(arg, name) ParsingContext* name = static_cast<ParsingContext*>(arg) |
| 114 |
|
| 115 |
void XMLCALL start_element_handler(void *data, const char* tag, const char** attributes) { |
| 116 |
HANDLER_CONTEXT(data, ctx); |
| 117 |
ctx->flushText(); |
| 118 |
|
| 119 |
ctx->fParser->startElement(tag); |
| 120 |
|
| 121 |
for (size_t i = 0; attributes[i]; i += 2) { |
| 122 |
ctx->fParser->addAttribute(attributes[i], attributes[i + 1]); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
void XMLCALL end_element_handler(void* data, const char* tag) { |
| 127 |
HANDLER_CONTEXT(data, ctx); |
| 128 |
ctx->flushText(); |
| 129 |
|
| 130 |
ctx->fParser->endElement(tag); |
| 131 |
} |
| 132 |
|
| 133 |
void XMLCALL text_handler(void *data, const char* txt, int len) { |
| 134 |
HANDLER_CONTEXT(data, ctx); |
| 135 |
|
| 136 |
ctx->appendText(txt, SkTo<size_t>(len)); |
| 137 |
} |
| 138 |
|
| 139 |
void XMLCALL entity_decl_handler(void *data, |
| 140 |
const XML_Char *entityName, |
| 141 |
int is_parameter_entity, |
| 142 |
const XML_Char *value, |
| 143 |
int value_length, |
| 144 |
const XML_Char *base, |
| 145 |
const XML_Char *systemId, |
| 146 |
const XML_Char *publicId, |
| 147 |
const XML_Char *notationName) { |
| 148 |
HANDLER_CONTEXT(data, ctx); |
| 149 |
|
| 150 |
SkDEBUGF("'%s' entity declaration found, stopping processing", entityName); |
| 151 |
XML_StopParser(ctx->fXMLParser, XML_FALSE); |
| 152 |
} |
| 153 |
|
| 154 |
} // anonymous namespace |
| 155 |
|
| 156 |
SkXMLParser::SkXMLParser(SkXMLParserError* parserError) : fParser(nullptr), fError(parserError) |
| 157 |
{ |
| 158 |
} |
| 159 |
|
| 160 |
SkXMLParser::~SkXMLParser() |
| 161 |
{ |
| 162 |
} |
| 163 |
|
| 164 |
bool SkXMLParser::parse(SkStream& docStream) |
| 165 |
{ |
| 166 |
ParsingContext ctx(this); |
| 167 |
if (!ctx.fXMLParser) { |
| 168 |
SkDEBUGF("could not create XML parser\n"); |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| •172 |
// Expat calls to rand_s if no salt is set, which is not allowed on Windows. |
| 173 |
// https://crbug.com/40088338 |
| 174 |
// Setting the salt helps prevent DOS with a known hash sequence. Generating a |
| 175 |
// random salt once per process oursevles ensures the salt is non-deterministic |
| 176 |
// and unguessable, while preventing calls to rand_s inside sandboxed processes |
| 177 |
// on Windows. |
| 178 |
XML_SetHashSalt(ctx.fXMLParser, get_hash_salt()); |
| 179 |
|
| 180 |
XML_SetUserData(ctx.fXMLParser, &ctx); |
| 181 |
XML_SetElementHandler(ctx.fXMLParser, start_element_handler, end_element_handler); |
| 182 |
XML_SetCharacterDataHandler(ctx.fXMLParser, text_handler); |
| 183 |
|
| 184 |
// Disable entity processing, to inhibit internal entity expansion. See expat CVE-2013-0340. |
| 185 |
XML_SetEntityDeclHandler(ctx.fXMLParser, entity_decl_handler); |
| 186 |
|
| 187 |
XML_Status status = XML_STATUS_OK; |
| 188 |
if (docStream.getMemoryBase() && docStream.hasLength()) { |
| 189 |
const char* base = reinterpret_cast<const char*>(docStream.getMemoryBase()); |
| 190 |
status = XML_Parse(ctx.fXMLParser, |
| 191 |
base + docStream.getPosition(), |
| 192 |
docStream.getLength() - docStream.getPosition(), |
| 193 |
true); |
| 194 |
} else { |
| 195 |
static constexpr int kBufferSize = 4096; |
| 196 |
bool done = false; |
| 197 |
do { |
| 198 |
void* buffer = XML_GetBuffer(ctx.fXMLParser, kBufferSize); |
| 199 |
if (!buffer) { |
| 200 |
SkDEBUGF("could not buffer enough to continue\n"); |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
size_t len = docStream.read(buffer, kBufferSize); |
| 205 |
done = docStream.isAtEnd(); |
| 206 |
status = XML_ParseBuffer(ctx.fXMLParser, SkToS32(len), done); |
| 207 |
if (XML_STATUS_ERROR == status) { |
| 208 |
break; |
| 209 |
} |
| 210 |
} while (!done); |
| 211 |
} |
| 212 |
if (XML_STATUS_ERROR == status) { |
| 213 |
#if defined(SK_DEBUG) |
| 214 |
XML_Error error = XML_GetErrorCode(ctx.fXMLParser); |
| 215 |
int line = XML_GetCurrentLineNumber(ctx.fXMLParser); |
| 216 |
int column = XML_GetCurrentColumnNumber(ctx.fXMLParser); |
| 217 |
const XML_LChar* errorString = XML_ErrorString(error); |
| 218 |
SkDEBUGF("parse error @%d:%d: %d (%s).\n", line, column, error, errorString); |
| 219 |
#endif |
| 220 |
return false; |
| 221 |
} |
| 222 |
|
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
bool SkXMLParser::parse(const char doc[], size_t len) |
| 227 |
{ |
| 228 |
SkMemoryStream docStream(doc, len); |
| 229 |
return this->parse(docStream); |
| 230 |
} |
| 231 |
|
| 232 |
void SkXMLParser::GetNativeErrorString(int error, SkString* str) |
| 233 |
{ |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
bool SkXMLParser::startElement(const char elem[]) |
| 238 |
{ |
| 239 |
return this->onStartElement(elem); |
| 240 |
} |
| 241 |
|
| 242 |
bool SkXMLParser::addAttribute(const char name[], const char value[]) |
| 243 |
{ |
| 244 |
return this->onAddAttribute(name, value); |
| 245 |
} |
| 246 |
|
| 247 |
bool SkXMLParser::endElement(const char elem[]) |
| 248 |
{ |
| 249 |
return this->onEndElement(elem); |
| 250 |
} |
| 251 |
|
| 252 |
bool SkXMLParser::text(const char text[], int len) |
| 253 |
{ |
| 254 |
return this->onText(text, len); |
| 255 |
} |
| 256 |
|
| 257 |
//////////////////////////////////////////////////////////////////////////////// |
| 258 |
|
| 259 |
bool SkXMLParser::onStartElement(const char elem[]) {return false; } |
| 260 |
bool SkXMLParser::onAddAttribute(const char name[], const char value[]) {return false; } |
| 261 |
bool SkXMLParser::onEndElement(const char elem[]) { return false; } |
| 262 |
bool SkXMLParser::onText(const char text[], int len) {return false; } |