mirror of
https://github.com/libretro/RetroArch.git
synced 2026-11-23 18:49:37 -10:00
Audit and harden the content scanner database stack An audit of the content scanner and everything it calls - tasks/task_database.c, tasks/task_database_cue.c, database_info.c, manual_content_scan.c and the six sources under libretro-db - with fixes for what it turned up and tests that demonstrate them. .rdb files are downloaded from the buildbot, so the reader has to survive whatever is in one, and most of the parser findings are of that shape: end-of-file taken for a fabricated record and looped on forever, an output value freed before it was ever initialised, container nesting with no depth cap, a binary search that read before its own count test and recursed without shrinking, identifier slices handed to strlcpy which strlen()s its source, and an integer accumulator with nothing bounding it. Two file-scope statics in the query compiler were shared across concurrent compiles. In the scanner: a stack-buffer-overflow write building the serial query, a walk one element past the end of the info list, a 256-database ceiling that wrote past the per-database arrays, strlcpy's return value used as a length against a destination half the size of its source, an over-long content directory accepted truncated and so pointed at a different directory, a completion callback dropped entirely in builds without menu support, and an uninitialised flag written into the playlist and read back on the next scan to decide whether to overwrite it. Two archive fixes came out of testing against a real library and are included here: an LZMA property limit that rejected the lc=4 streams 7-Zip routinely writes - and since 7z holds its own header as an LZMA stream, such an archive could not be opened at all - and a lookup for an absent archive member that spun at full CPU instead of returning. The scanner asked each database the same question once per content file, and each was a full cursor walk. A crc index built in one pass answers those in under a microsecond against 10.59 ms before. Serial lookups got the same treatment and the per-database size range now comes out of the same walk rather than two further queries. Indexes cost 8 bytes per record, are capped at a share of free memory, and anything the index cannot serve falls back to the query path. Equivalence was checked rather than assumed, because the scanner takes the first match and a sixth to a quarter of the crcs shipped are duplicates: 1,225 lookups and 126 duplicate groups return the same records in the same order through both paths. The four sample directories now carry 64 checks between them, every case verified to fail without its fix, and sweep.sh runs the lot under ASan, UBSan, LeakSanitizer and TSan. Three of the defects fixed here were introduced by this work and found afterwards, all of them wrong answers with no error reported: index caches keyed by a database's position but not moved when a match promoted it, a teardown reading a list's size after freeing it, and a type check that excluded the binary encoding every shipped database uses for its serial field, which stopped disc-based systems matching entirely. Each has a regression test that fails without its fix. Not covered: the index memory share is a judgement rather than a measurement against a constrained working set.
56 lines
2.2 KiB
C
56 lines
2.2 KiB
C
/* Copyright (C) 2010-2017 The RetroArch team
|
|
*
|
|
* ---------------------------------------------------------------------------------------
|
|
* The following license statement only applies to this file (query.h).
|
|
* ---------------------------------------------------------------------------------------
|
|
*
|
|
* Permission is hereby granted, free of charge,
|
|
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
|
* to deal in the Software without restriction, including without limitation the rights to
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
#ifndef __LIBRETRODB_QUERY_H__
|
|
#define __LIBRETRODB_QUERY_H__
|
|
|
|
#include <retro_common_api.h>
|
|
|
|
#include "libretrodb.h"
|
|
#include "rmsgpack_dom.h"
|
|
|
|
RETRO_BEGIN_DECLS
|
|
|
|
typedef struct libretrodb_query libretrodb_query_t;
|
|
|
|
void libretrodb_query_inc_ref(libretrodb_query_t *q);
|
|
|
|
/* Reset @q's min()/max() accumulator. Called when a cursor walk
|
|
* begins; see the comment on struct query_ctx in query.c. */
|
|
void libretrodb_query_reset_accumulator(libretrodb_query_t *q);
|
|
|
|
void libretrodb_query_dec_ref(libretrodb_query_t *q);
|
|
|
|
int libretrodb_query_filter(libretrodb_query_t *q, struct rmsgpack_dom_value *v);
|
|
|
|
int libretrodb_query_get_filter_fields(libretrodb_query_t *q,
|
|
const char **field_names, uint32_t *field_lens,
|
|
unsigned max_fields);
|
|
|
|
int libretrodb_query_eval_field(libretrodb_query_t *q,
|
|
const char *field_name, uint32_t field_len,
|
|
struct rmsgpack_dom_value *value);
|
|
|
|
RETRO_END_DECLS
|
|
|
|
#endif
|