[Fdo-trac] [fdo-trac] #1026: Security hardening sweep
FDO
trac_fdo at osgeo.org
Fri Nov 13 06:51:51 PST 2026
#1026: Security hardening sweep
---------------------+----------------------
Reporter: jng | Owner: jng
Type: task | Status: closed
Priority: major | Milestone: 4.2.0
Component: FDO API | Version:
Severity: 3 | Resolution: fixed
Keywords: | External ID:
---------------------+----------------------
Changes (by jng):
* resolution: => fixed
* status: assigned => closed
Comment:
In [changeset:"8437" 8437]:
{{{#!CommitTicketReference repository="" revision="8437"
#1026: Fix the buffer overflows and input-validation gaps a security sweep
found
A code-reading sweep of FDO - core, utilities and every provider that
builds
on Linux, the vendored SQLite 3.3.13 included, and nothing else from
Thirdparty - turned up 48 places where a value from an untrusted boundary
reaches a sink unchecked. This is the fix for all of them. None of the
changes
moves the provider-facing ABI: no shipped class gains, loses or reorders a
member, so the surface the SDK installs keeps its shape.
Parsers (filter/expression lexer, WKT lexer, constraint lexer)
FdoLex::getword() documented its maxCharacters parameter and then ignored
it,
copying the token into the caller's 4000-wchar stack buffer with no bound.
A
filter is client text - MapGuide's SELECTFEATURES passes the FILTER
parameter
straight to FdoFilter::Parse() - so a long enough identifier wrote past
that
buffer. getword() now refuses a token that does not fit
(PARSE_5_STRINGTOOLONG), getdigits() takes an end pointer so a caller that
has
already written a sign or a radix character is bounded correctly, and the
dotted-name and quoted-identifier paths append through
AppendIdentifierPart(),
which checks the capacity before concatenating: a name like a.a.a...,
assembled from several tokens into the same buffer, could otherwise grow
past
it however each individual part was bounded.
The heap buffer those paths take from get_string() is owned by a scope
guard
now, so a rejected token cannot leak it, and get_string() clears the
caller's
pointer when it frees the buffer itself - the callers' own error branch
was a
double free as soon as it became reachable.
The same treatment is applied to the WKT lexer in
Geometry/Parse/LexFgft.cpp,
which had the unbounded copy against a 256-wchar buffer
(FDO_27_BUFFERLENGTHERROR there), and to the constraint lexer in
Utilities/Common/Src/Parse/Lex.cpp. Its bit and hex string literals are
sized
from the declared length and the length is tested before the store; the
test
came after it, so an over-long literal was written first and rejected
second.
XML reader
FdoXmlReader is FDO's only Xerces construction site, and it resolved
external
general entities. The external-DTD switches it did set
(fgXercesLoadExternalDTD, and the schema and doctype flags) only cover the
external subset: a document that declares <!ENTITY xxe SYSTEM
"file:///...">
in its own internal subset is well formed, and everything FDO parses - a
capabilities document, a schema, a spatial context file - can be remote.
The
reader sets fgXercesDisableDefaultEntityResolution now, which rejects such
a
document. The flag was chosen after reproducing the entity resolution
against
a real Xerces build: with the flags the code already had, an entity
pointing
at /etc/hostname came back as that file's contents.
Geometry
The FGF and WKB parsers read counts out of the stream and used them to
size
arrays and bound copies. Fgf/Util.cpp rejects a negative curve-segment
position count and validates the accumulated ordinate count in 64 bits
against
the bytes that are left; Fgf/MultiPoint.cpp does the same for
numDims*numPositions instead of letting the product wrap;
Fgf/LineString.cpp
checks the declared position count against the remaining stream before
handing
the array to callers; Fgf/GeometryFactory.cpp gained CheckWkbLength() and
ReadWkbCount(), so no WKB branch reads a count or copies ordinates past
the
end. CoordinateGroup::invertAxis() swaps whole coordinates only, and
SpatialGeometryConverter's 4-entry ordinate table is bounds checked
(FDO_17_UNSUPPORTEDDIMENSIONALITY) because the index comes from an FGF
header.
Common and the expression engine
FdoStringP::IsNumber() copied the string into a 1000-byte buffer with
strcpy,
with the length test that guarded it commented out; it scans the string in
place now. FileStream's wide-to-narrow mode buffer was sized from the file
name rather than from the mode, and both conversions are snprintf.
TOSTRING
and TODATE wrote past their fixed 500-entry format arrays, the latter's
token
counter also being reused across calls without being reset; both refuse a
format specification that needs more than 500 tokens.
RDBMS providers: SQL injection through a filter
A function's name reached the generated SQL verbatim -
ProcessFunctionName()
appended expr.GetName() - and the filter grammar takes that name from an
identifier, which may be delimited, so "x) OR 1=1 #"(1) = 1 was emitted as
SQL. FdoRdbmsFilterProcessor validates the name first
(ValidateSqlFunctionName()), which the SQL Server Spatial and PostGIS
processors call as well, and an aggregate's first string argument is
emitted
as a bare keyword only when it is ALL or DISTINCT - anything else goes
through
ProcessStringValue() as a quoted literal. MySQL and MariaDB also treat a
backslash as escaping the closing quote, so the MySQL processor escapes
backslashes before the quote is doubled.
A datastore name reached CREATE DATABASE and DROP DATABASE unescaped. It
is
delimited as a MySQL identifier with any embedded backtick doubled, from
the
raw owner name: GetDbName() is already ANSI-quoted by GetSQLObjectName(),
so
nesting it inside backticks would make the quotes part of the database
name
instead of delimiters. The metadata reader that looks a schema up by
datastore
name escapes it with the provider's own FormatSQLVal() rather than quoting
it
inline, and FdoRdbmsPropBindHelper's memset() had its arguments in the
wrong
order, which only worked because the struct happened to be value-
initialised.
The legacy King providers had no validation at all: filter identifiers,
function names, ORDER BY identifiers and ApplySchema's table and column
names
are concatenated into SQL text. They go through KgOraIsSqlIdentifier()
now,
which accepts a plain identifier, a dotted one, or the same with each part
double-quoted and holding no quote of its own.
File providers
SDF: a string's own length field was used as an allocation size and a read
length with no bound against the record (SDFPROVIDER_110), and a
property's
offset and end offset from the record header became a read position and a
length with no check either - positioning is PositionAtValue() now,
SDFPROVIDER_111. The fixed-width readers compared m_pos against m_len -
sizeof(T) in unsigned arithmetic, which underflows for a record shorter
than
the field, so a short record passed the test. The R-tree checked its
record
size with an assert that release builds compile out (CheckBlobSize()
throws
now), and the compare handler read one offset past its array and used the
result as a length - the array has a slot for the record size now.
SHP: the shape objects overlay the record buffer and trust the part and
point
counts read out of it, and a MultiPatch record additionally needs room for
the
part-type array the overlay injects, which the buffer did not have.
ValidateShapeRecord() checks each shape type's declared counts and its
part
indices against the record before the shape object is built, and the read
buffer takes the extra part-type room. The DBF side rejects a column
layout
that does not fit the record, copies a field out of the record before
zero-terminating it, bounds the width-driven sprintf in SetData(), and
checks
the whitespace trim's bound before dereferencing it.
SQLite: AppendSQuoted() and AppendDQuoted() wrapped a value in quotes
without
escaping it, and their output goes to sqlite3_exec(), so an embedded quote
ended the literal; AppendQuoted() doubles it. Wkb2Fgf() and Fgf2Wkb() took
neither the input length nor the output capacity; both do now and return 0
for
a geometry that is malformed or does not fit. GetFgfExtents() ignored the
length it was given and now stops at the end of the stream instead of
walking
the buffer. SQLiteTable::Drop() formatted the table name into char[128].
The vendored SQLite 3.3.13 had its page-size validation removed so that it
could use a 51200-byte page, which is not a power of two: the page size
taken
from the file then sized the page structure and every cell offset read
from
it, while the pager allocated each page with its own size. lockBtree()
requires the file's page size to match the pager's (the new
sqlite3pager_pagesize()) and to be a multiple of 8, and findCell() range
checks the stored cell offset instead of masking it, which cannot work for
a
page size that is not a power of two.
Transport (WMS and WFS)
TLS peer verification was disabled for every request, credentials followed
redirects to any host, and the endpoint a remote capabilities document
advertises was fetched as given - as was an absolute schemaLocation in a
DescribeFeatureType response. Verification is on now
(FDO_OWS_INSECURE_TLS=1
is the opt-out for a self-signed endpoint), CURLOPT_UNRESTRICTED_AUTH is
off,
and FdoOwsDelegate::IsSameOrigin() confines both the advertised endpoint
and
the schema imports to the origin the connection was configured for.
Other providers
OGR built two statements with sprintf() into char[512] from caller-
supplied
names, and the GDAL provider's string-conversion macros allocate their
buffer
on the stack from the input length; the first two are std::string now and
the
macros refuse an input longer than FDORFP_MAX_CONVERSION_LENGTH
(GRFP_115).
The ArcSDE connection copied its connection properties into fixed members
with
sde_strcpy() and no bound; they go through CacheConnectionProperty()
(ARCSDE_CONNECTION_PROPERTY_TOO_LONG).
Tests
Three cases cover the safeguards, and each of them fails without its
change:
* XmlTest::testExternalEntityNotResolved - a document whose internal
subset
declares an external general entity is rejected by FdoXmlReader. Without
the
reader change the entity is resolved and the referenced file's contents
appear in the parsed content.
* FilterParseTest::testLongTokens - a quoted identifier one character
short of
the lexer's 4000-character buffer still parses; an identifier, quoted
identifier, dotted name or string literal past it is refused rather than
overflowing.
* CommonTest::testStringBoundaries - IsNumber() on a 3000-character number
and
on the same with a trailing non-digit, plus the length-related
conversions
on a 2000-character string.
Adding them turned up three defects in the harness, fixed here:
ParseFilter()'s message buffer was a fixed 512 bytes holding two copies of
the
filter; the lexer's string reader freed its buffer without clearing the
caller's pointer, a double free once a rejected token made the caller's
cleanup reachable; and the SQLite provider escaped a string DEFAULT value
itself as well as through the quoting helper, so it was stored double-
escaped.
Verified on ubuntu22 amd64 against the dbs/docker-compose.yml fixtures,
Debug
(ASan/LSan) and Release, each suite run from its build directory with
LC_CTYPE
and NLSPATH set as run_fdo_tests.sh does, and the SHP TestData reset from
the
sources:
* FDO Core OK (133), SDF OK (122), SQLite OK (96), OGR OK (58), SHP OK
(138)
and MySQL OK (249) in both configurations, with no LeakSanitizer record
of
either kind in the Debug run.
* The run_fdo_tests.sh run in Debug is green for the rest as well: GDAL OK
(68), Oracle OK (42), PostGIS OK (141), SQL Server Spatial OK (290),
ODBC
SQL Server OK (87) and ODBC Access OK (35), none of them with a leak
record
either.
Fixes #1026
}}}
--
Ticket URL: <https://trac.osgeo.org/fdo/ticket/1026#comment:2>
FDO <http://fdo.osgeo.org/>
Feature Data Objects
More information about the fdo-trac
mailing list