The file lang/python/examples/howto/export-secret-key.py in the gpgme source code contains this code:
if result is not None:
with open(keyfile, "wb") as f:
f.write(result)
os.chmod(keyfile, 0o600)This is insecure. The problem is that there is a race condition where the private key is written to a file while it is still readable. You can find a proof of concept exploit for such issues here: https://github.com/hannob/fpracer
A more secure pattern would be this:
if result is not None:
old_umask = os.umask(0o077)
with open(keyfile, "wb") as f:
f.write(result)
os.umask(old_umask)This makes sure the file already has secure permissions when it is opened.
This and similar coding patterns appear in these files:
lang/python/doc/src/gpgme-python-howto.org lang/python/doc/src/gpgme-python-howto.tex~ lang/python/examples/howto/export-secret-key.py lang/python/examples/howto/export-secret-keys.py lang/python/examples/howto/temp-homedir-config.py
(sidenote: the .tex~ file is probably there by accident.)