[New-bugs-announce] [issue30297] Recursive starmap causes Segmentation fault

Sebastian Noack report at bugs.python.org
Sun May 7 09:31:52 EDT 2017

New submission from Sebastian Noack:

If I run following code (on Python 3.5.3, Linux) the interpreter crashes with a segfault:

def pbkdf2_bin(data, salt, iterations=1000, keylen=24, hashfunc=None):
    hashfunc = hashfunc or hashlib.sha1
    mac = hmac.new(data, None, hashfunc)
    def _pseudorandom(x, mac=mac):
        h = mac.copy()
        return h.digest()
    buf = []
    for block in range(1, -(-keylen // mac.digest_size) + 1):
        rv = u = _pseudorandom(salt + _pack_int(block))
        for i in range(iterations - 1):
            u = _pseudorandom(u)
            rv = starmap(xor, zip(rv, u))
    return bytes(buf[:keylen])

pbkdf2_bin(b'1234567890', b'1234567890', 200000, 32)

I was able to track it down to the line of buf.extend(rv) which apparently is causing the segfault. Note that rv is a lazy-evaluated starmap. I also get a segfault if I evaluate it by other means (e.g. by passing it to the list constructor). However, if I evaluate it immediately by wrapping the starmap constructor with the list constructor, the code works as expected. But I wasn't able yet, to further isolate the issue. FWIW, the Python 2 version [1] of this code works just fine without forcing immediate evaluation of the starmap.

Note that the code posted, except for the bits I changed in order to make it compatible with Python 3, is under the copyright of Armin Ronacher, who published it under the BSD license.

[1]: https://github.com/mitsuhiko/python-pbkdf2

messages: 293192
nosy: Sebastian.Noack
priority: normal
severity: normal
status: open
title: Recursive starmap causes Segmentation fault
type: crash
versions: Python 3.5

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list