Dear Maintainer,
mozilla/certdata2pem.py derives an output filename from each certificate's
CKA_LABEL. It sanitises the label (replacing '/' with '_') *before* decoding
backslash escape sequences, so an escaped path separator in a label survives
sanitisation and is turned back into a real '/' afterwards. The result is
used
directly as the path passed to open(), allowing the written file to escape
the
intended output directory.
This is an input-validation / defense-in-depth issue, not a remote
vulnerability: certdata.txt is normally the trusted Mozilla NSS trust store.
It becomes relevant wherever certdata2pem.py is run on an
attacker-influenced
certdata.txt (e.g. a derivative rebuilding the bundle from a vendor/mirror
blob,
a pipeline fetching certdata over a weak channel, or a developer running the
tool on an untrusted file). I'm filing it as a hardening fix and am not
requesting an embargo; a CVE is at your discretion.
Root cause
----------
mozilla/certdata2pem.py, in the CKO_CERTIFICATE loop (around lines 131-159):
bname = obj['CKA_LABEL'][1:-1].replace('/', '_')\
.replace(' ', '_')\
.replace('(', '=')\
.replace(')', '=')\
.replace(',', '_')
...
bname = bname.encode('utf-8').decode('unicode_escape').encode('latin-1')
...
fname = bname + b'.crt'
...
f = open(fname, 'w')
The .replace('/', '_') runs first; the .decode('unicode_escape') runs
second.
A label containing an octal (\057) or hex (\x2f) escape for '/', plus '\056'
for '.', is unaffected by the replace and is revived into '../...' by the
decode. os.path.exists()-based dedup (the "_2.crt" rename) means the
primitive
is file *creation* at an attacker-chosen relative path (not overwrite of an
existing file); the content is a PEM wrapper around attacker-controlled DER.
As the tool typically runs as root during packaging/build, a file can be
created outside the output directory as root.
Reproduction
------------
A self-contained, sandboxed PoC is attached as
ca-certificates-traversal-poc.zip
(repro.sh, malicious-certdata.txt, and the patch).
It crafts a minimal certdata.txt with one trusted CKO_CERTIFICATE whose
CKA_LABEL carries the payload ..\057escaped\057pwned and runs the
unmodified
certdata2pem.py from a throwaway working directory. Observed result: the
script
prints
Found duplicate certificate name b'../escaped/pwned', renaming.
and writes the PEM to ../escaped/pwned_2.crt — i.e. one directory *above*
the
working directory, outside it. (The "_2" suffix is the existence-check
dedup;
with a non-colliding target name the file is created directly.)
Fix
---
Re-neutralise separators after the unicode_escape decode and collapse to a
single path component before open(). Verified against the PoC: the same
malicious input is then contained to the working directory (file becomes
.._escaped_pwned.crt in the cwd; nothing escapes).
--- a/mozilla/certdata2pem.py
+++ b/mozilla/certdata2pem.py
@@ -143,6 +143,11 @@
# then encode the byte-sequence as an escaped string that
# can be passed to open() and os.path.exists()
bname =
bname.encode('utf-8').decode('unicode_escape').encode('latin-1')
+ # SECURITY: the decode above can revive path separators that
the
+ # earlier str.replace() stripped (e.g. an octal- or
hex-escaped '/').
+ # Re-neutralise and collapse to a single path component before
open().
+ bname = bname.replace(b'/', b'_').replace(b'\\', b'_')
+ bname = os.path.basename(bname)
else:
# Python 2
# Convert the unicode string back to its original byte form
(The Python 2 branch has the same ordering issue; the same two lines applied
after its decode would cover it, if that branch is still considered
supported.)
Note on scope
-------------
The identical sanitise-before-decode pattern appears in several downstream
copies of certdata2pem.py (e.g. ptxdist, cerbero, m2crypto,
maemo-ca-certificates),
so a fix here is worth flagging to those projects as well.
Thanks for maintaining this package.