Description
Summary
A DXF-format drawing with particular (not necessarily malformed!) properties may cause an out-of-bounds memory access when imported using import()
.
Vulnerable versions
- OpenSCAD Linux (commit eedf370)
- OpenSCAD Windows x64 (2021.01)
Steps to reproduce
Two Proof-of-concept files are provided. oob_dxfdata_505
is larger, but more reliably reproduces the fault. oob_dxfdata_k5
is my attempt at a minimal working example, but only reliably crashes the linux build from the latest commit on the main
branch (i.e. it does not crash in the windows release). Both illustrate the same crash, so I expect the _min
example to be more helpful in testing.
oob_dxfdata_505.zip, the larger messier p-o-c
oob_dxfdata_k5.zip, the smaller p-o-c
- Unzip one of the provided proof of concept files
- Open the
.scad
file in OpenSCAD - Render with F6
- Observe the application crashing
Alternatively, for headless operation:
- Unzip one of the provided proof of concept files
openscad --export-format stl -o /dev/null oob_dxfdata_k5.scad
- Observe the segmentation fault
Screenshot
Cause
I haven't quite been able to wrap my head around the DXF parser yet, but the OOB access occurs in src/dxfdata.cc
on one of the lines 470, 475, 505, or 510 depending on the input file.
It appears to be related to the fact that if multiple line segments share points in common, they are merged into contiguous paths. As a part of this, The ADD_LINE macro manipulates the grid.data<>
and lines<>
structures. By creating lines either in the ENTITIES
section of the DXF file or outside of it, an attacker is able to ensure that the grid.data
entry created on line 108 points to an index in lines
which never becomes valid.
On line 470 (and the others listed above), this value (k
) is used as an index into lines
, out of bounds.
Impact
It appears that the out-of-bounds data can only be read, and not written. An out-of-bounds read does not expose a security vulnerability on its own, but can be used to bypass automatic security features such as stack canaries and pointer encryption.
Proposed mitigation
The algorithm employed in DxfData::DxfData
needs to be revised to prevent this type of aliasing. Without more insight into the DXF format and how it is used by OpenSCAD, I cannot say for sure where the actual flaw lies.
It seems likely that the ADD_LINE
macro on lines 108-109 is the culprit, since it inserts values that will be used for indexing even though the values are not yet valid indices.