classification
Title: Multiprocessing UNIX socket connection: client freeze if authkey is an empty byte string
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: miguendes
Priority: normal Keywords: patch

Created on 2021-04-27 11:45 by anon01, last changed 2021-05-13 21:12 by pitrou.

Pull Requests
URL Status Linked Edit
PR 25845 open miguendes, 2021-05-03 10:07
Messages (3)
msg392056 - (view) Author: (anon01) Date: 2021-04-27 11:45
When I run this code (on a UNIX system with temporary directory `/tmp/`):

```
from multiprocessing import connection
import threading
key=b"1"

def run():
    c=connection.Client("/tmp/xxx", authkey=key)
    c.send("data")

l=connection.Listener("/tmp/xxx", authkey=key)
threading.Thread(target=run).start()
c=l.accept()
print("receive", c.recv())
```

it prints out "receive data" normally.

However, in the special case that `key=b""` the doesn't print anything, and can only be interrupted with Ctrl+C.

`key=None` doesn't have that issue.

Note that this issue does happen when the client uses the key `b""` and the server uses the key `None`, but the program works normally if the reverse situation.

Python version: Python 3.9.3.
msg392591 - (view) Author: Miguel Brito (miguendes) * Date: 2021-05-01 12:48
I tried debugging this and from what I can see it's because there's an if that checks if the authkey is not None in the Client constructor:

https://github.com/python/cpython/blob/v3.9.4/Lib/multiprocessing/connection.py#L512

```
    if authkey is not None:
        answer_challenge(c, authkey)
        deliver_challenge(c, authkey)
```

Whereas in the Listener, the check is different:

https://github.com/python/cpython/blob/v3.9.4/Lib/multiprocessing/connection.py#L469

```
        c = self._listener.accept()
        if self._authkey:
            deliver_challenge(c, self._authkey)
            answer_challenge(c, self._authkey)
        return c
```

If I change the Listener to:

```
        if self._authkey is not None:
            deliver_challenge(c, self._authkey)
            answer_challenge(c, self._authkey)
        return c
```

it works.


The docs say:

"""
If authkey is given and not None, it should be a byte string and will be used as the secret key for an HMAC-based authentication challenge. No authentication is done if authkey is None. AuthenticationError is raised if authentication fails. See Authentication keys.
"""


Now the question is, if None is OK because no auth will be done what about empty bytes? Can it be used as secret key? If empty bytes is not accepted shouldn't Listener/Client raise an exception in the constructor?
msg392790 - (view) Author: Miguel Brito (miguendes) * Date: 2021-05-03 10:21
I had a look at the HMAC RFC and apparently empty bytes sequence can be used as secret key.

"The definition of HMAC requires a cryptographic hash function, which
we denote by H, and a secret key K. 

...

The authentication key K can be of any length up to B, the
block length of the hash function."
   
https://tools.ietf.org/html/rfc2104.html#section-2

Assuming that is the case, the fix would be to change the Listener to:

```
        if self._authkey is not None:
            deliver_challenge(c, self._authkey)
            answer_challenge(c, self._authkey)
        return c
```


I created a PR for that, if anyone can review it, I appreciate it.
https://github.com/python/cpython/pull/25845
History
Date User Action Args
2021-05-13 21:12:02pitrousetversions: + Python 3.11, - Python 3.9
2021-05-03 10:21:39miguendessetmessages: + msg392790
2021-05-03 10:07:27miguendessetkeywords: + patch
stage: patch review
pull_requests: + pull_request24528
2021-05-01 12:48:54miguendessetnosy: + miguendes
messages: + msg392591
2021-04-27 11:46:25anon01settype: behavior
2021-04-27 11:45:55anon01setnosy: - anon01
-> (no value)
2021-04-27 11:45:15anon01create