Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssl.SSLSocket.write may fail on non-blocking sockets #52487

Closed
cbay mannequin opened this issue Mar 26, 2010 · 27 comments
Closed

ssl.SSLSocket.write may fail on non-blocking sockets #52487

cbay mannequin opened this issue Mar 26, 2010 · 27 comments
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@cbay
Copy link
Mannequin

cbay mannequin commented Mar 26, 2010

BPO 8240
Nosy @jcea, @pitrou, @vstinner, @giampaolo, @benjaminp, @bitdancer, @bdarnell
Files
  • _ssl.c.patch
  • test_ssl.py.patch
  • test_ssl.py.patch.v2
  • ssl_mode.patch: implements SSLSocket.get_mode/set_mode, made on trunk 44327.
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2013-05-25.11:09:59.718>
    created_at = <Date 2010-03-26.15:30:33.547>
    labels = ['extension-modules', 'type-feature']
    title = 'ssl.SSLSocket.write may fail on non-blocking sockets'
    updated_at = <Date 2013-05-25.11:09:59.717>
    user = 'https://bugs.python.org/cbay'

    bugs.python.org fields:

    activity = <Date 2013-05-25.11:09:59.717>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-05-25.11:09:59.718>
    closer = 'pitrou'
    components = ['Extension Modules']
    creation = <Date 2010-03-26.15:30:33.547>
    creator = 'cbay'
    dependencies = []
    files = ['16668', '16669', '16670', '16800']
    hgrepos = []
    issue_num = 8240
    keywords = ['patch']
    message_count = 27.0
    messages = ['101753', '101754', '101757', '101761', '101762', '101763', '101765', '101767', '101768', '101769', '101770', '101826', '101944', '101946', '101986', '102535', '102541', '102563', '102746', '109637', '138119', '168547', '189763', '189789', '189952', '189954', '189955']
    nosy_count = 11.0
    nosy_names = ['jcea', 'janssen', 'pitrou', 'vstinner', 'giampaolo.rodola', 'benjamin.peterson', 'r.david.murray', 'cbay', 'python-dev', 'Ben.Darnell', 'Ken.Giusti']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue8240'
    versions = ['Python 3.4']

    @cbay
    Copy link
    Mannequin Author

    cbay mannequin commented Mar 26, 2010

    ssl.SSLSocket.write on non-blocking sockets will fail with:

    _ssl.c:1217: error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry

    on a write retry, if the buffer address has changed between the initial call and the retry (when the initial call returned 0 bytes written, which means you should try again later).

    From OpenSSL docs (http://www.openssl.org/docs/ssl/SSL_CTX_set_mode.html):

    SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
    Make it possible to retry SSL_write() with changed buffer location (the buffer contents must stay the same). This is not the default to avoid the misconception that non-blocking SSL_write() behaves like non-blocking write().

    Attached patch fixes the problem (tested on Python 2.6.5, 2.7 trunk) by calling SSL_CTX_set_mode with SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER. It's a single line patch.

    @cbay cbay mannequin added the stdlib Python modules in the Lib dir label Mar 26, 2010
    @cbay
    Copy link
    Mannequin Author

    cbay mannequin commented Mar 26, 2010

    The following test case exhibits the bug, but I'm not sure it will fail every time as it depends on 2 things:

    • your connection speed (I guess)
    • I used the following trick to have 2 identical strings with a different id (memory address):
    data = (('xx'[0] + 'xx'[1:])*10000, ('xx'[0] + 'xx'[1:])*10000)

    I'm not sure it will work all the time though.

    @bitdancer bitdancer added extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error and removed stdlib Python modules in the Lib dir labels Mar 26, 2010
    @vstinner
    Copy link
    Member

    If I understood correctly, the patch only concerns non blocking socket if SSL_write() returns 0? If SSL_write() returns a non zero value, can you use: ssl_socket.send(data[count:])?

    About the string identifier trick, you should add an assertion to ensure that identifiers are differents. Example:
    --------

    a = 'x' * 20000
    # create a copy with a different memory address
    b = a[0:] + a[1:]
    assert (a == b) and (a is not b)
    data = a, b

    See also issue bpo-8222: enabling SSL_MODE_AUTO_RETRY on SSL sockets.

    @cbay
    Copy link
    Mannequin Author

    cbay mannequin commented Mar 26, 2010

    You're right about the assert, I've just uploaded a new patch.

    In non-blocking mode, ssl_socket.send(data) will return either 0 (which means nothing was sent, you'll have to try again), or len(data) when everything was sent. It can't return anything inbetween. This is because SSL_MODE_ENABLE_PARTIAL_WRITE is not enabled.

    In my opinion, SSL_MODE_ENABLE_PARTIAL_WRITE should probably be enabled, although I don't know if it would have any consequence on existing code. Note that _ssl.c header has:

    XXX should partial writes be enabled, SSL_MODE_ENABLE_PARTIAL_WRITE?

    However, it's totally unrelated to our bug. Issue bpo-8222 is also unrelated since SSL_MODE_AUTO_RETRY only applies to blocking sockets.

    By the way, this bug was triaged "test needed". Am I missing anything? This is my first reported bug, I'm not sure about the process.

    @cbay
    Copy link
    Mannequin Author

    cbay mannequin commented Mar 26, 2010

    I forgot to talk about the conditions in which I stumbled upon that bug. I use a cStringIO.StringIO as a send buffer. When the socket is ready to send data, I call ssl_socket.send(send_buffer.getvalue()).

    Unfortunately, two consecutive calls to send_buffer.getvalue() may return a different object (i.e. a string with a different memory address).

    @bitdancer
    Copy link
    Member

    "test needed" is in reference to your assertion that you weren't sure your test would fail reliably. A test that fails some times and passes some times is...suboptimal when dealing with a buildbot testing infrastructure :)

    @pitrou
    Copy link
    Member

    pitrou commented Mar 26, 2010

    Since this error seems to be aimed at warning about potential programming errors, I'm not sure it should be silenced. The obvious fix should be to pass the same argument every time (until the data finally gets written).

    @cbay
    Copy link
    Mannequin Author

    cbay mannequin commented Mar 26, 2010

    r.david.murray: ah, sure :) However, I'm not sure a test case is absolutely required for this issue for two reasons:

    • the fix is trivial: it's a one-liner that enables a SSL mode that explicitely authorizes SSL_write to be called a second time with a a different memory pointer than the first time. Since memory pointers are opaque to Python programmers anyway, I doubt it could break code (unless you'd expect the failure, of course :) )

    • tests about SSL in non-blocking mode are almost inexistant, I think. The only one I could find tests the handshake. See issue bpo-3890 for instance. Probably because writing tests in non-blocking mode isn't easy.

    However, my test may be correct, I'm just not sure it will pass everywhere :)

    @cbay
    Copy link
    Mannequin Author

    cbay mannequin commented Mar 26, 2010

    pitrou: that's debatable, since the Python programmer has no control over memory pointers. As I said, I have a cStringIO buffer, and two consecutive calls to buffer.getvalue() yield different objects. What can I do about it? I think it's a rather sane scenario, and I don't feel I'm doing anything wrong.

    If you think the programmer should be alerted about it, however, then we should at least say it explicitely in the documentation and probably return an explicit Python error. I had to google quite a bit before finding out what this error meant:

    error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry

    @pitrou
    Copy link
    Member

    pitrou commented Mar 26, 2010

    pitrou: that's debatable, since the Python programmer has no control
    over memory pointers.

    No, but he has control over whether he always uses the same object, or
    generates a new argument everytime.

    As I said, I have a cStringIO buffer, and two consecutive calls to
    buffer.getvalue() yield different objects. What can I do about it? I
    think it's a rather sane scenario, and I don't feel I'm doing anything
    wrong.

    Hmm, indeed. What you can do, very simply, is cache the getvalue()
    result once you have generated it.

    If you think the programmer should be alerted about it, however, then
    we should at least say it explicitely in the documentation and
    probably return an explicit Python error. I had to google quite a bit
    before finding out what this error meant:

    error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry

    Indeed, this is cryptic.

    By the way, I've found a thread explaining this in greater detail:
    http://readlist.com/lists/openssl.org/openssl-users/0/1794.html

    Basically, even when SSL_write() says the write must be retried, it does
    process and buffer some of your data, so that if you retry with
    different data, some junk will be written out on the SSL socket.

    @cbay
    Copy link
    Mannequin Author

    cbay mannequin commented Mar 26, 2010

    Switching to a documentation issue is fine to me. Indeed I can just cache the result of StringIO.getvalue(), although it feels a bit crude.

    I won't be able to create a documentation patch since English is not my primary language. While you're at it, the doc says about SSLSocket.write:

    Returns the number of bytes written.

    It actually either returns 0 or len(data), at least as long as we don't have SSL partial writes. That's a different behaviour from regular sockets, and I had to look in _ssl.c to figure out why I never had values inbetween.

    @vstinner
    Copy link
    Member

    ..., the doc says about SSLSocket.write:

    Returns the number of bytes written.

    It actually either returns 0 or len(data), at least as long as we don't
    have SSL partial writes. That's a different behaviour from regular
    sockets, and I had to look in _ssl.c to figure out why I never had values
    inbetween.

    You should open a new issue for this point.

    @cbay
    Copy link
    Mannequin Author

    cbay mannequin commented Mar 30, 2010

    Hmm, indeed. What you can do, very simply, is cache the getvalue()
    result once you have generated it.

    After some thoughts, it's not really an option: my cStringIO.StringIO buffer is, well a buffer. To append data to the buffer, I call buffer.write(). When I've got a chance to send data over the socket (remember, it's async, so I don't really know when it's going to happen), I call buffer.getvalue().

    If socket.write() returns zero byte written, I'll have to wait until I get another chance to send my buffer. But in the meantime, some more data might get appended to the buffer, and the string returned by getvalue() will be different from the first call (and thus, I can't really cache it).

    I could find some tricks (like using multiple buffers), but it would be ugly.

    @pitrou
    Copy link
    Member

    pitrou commented Mar 30, 2010

    If socket.write() returns zero byte written, I'll have to wait until I
    get another chance to send my buffer. But in the meantime, some more
    data might get appended to the buffer, and the string returned by
    getvalue() will be different from the first call (and thus, I can't
    really cache it).

    I could find some tricks (like using multiple buffers), but it would
    be ugly.

    Right. I think we should somehow support your use case, but I'm not sure
    whether it should be the default.

    @cbay
    Copy link
    Mannequin Author

    cbay mannequin commented Mar 31, 2010

    I had a look at how M2Crypto and pyOpenSSL handled this:

    • M2Crypto has wrappers around SSL_set_mode that let you set the modes you want. From their changelog [1], it was required to be able to operate with Twisted. By default, though, they only set SSL_MODE_AUTO_RETRY.

    • pyOpenSSL enables everything by default, and there's no set_mode wrapper. Here is the relevant code:

    /* Some initialization that's required to operate smoothly in Python */
    SSL_CTX_set_mode(self->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE |
    SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
    SSL_MODE_AUTO_RETRY);

    I don't see any other possible alternative. I'm not sure which one is better. Implementing a set_mode wrapper with no mode set by default has no compatibility issues, although we'd still have that 'bad write retry' OpenSSL error.

    On the other hand, setting SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER by default is easy but we lose some security (and, possibly, some compatibility problems, although I doubt anyone relies on the 'bad write retry' error).

    What do you think? I'd be ready to write the patch for the set_mode wrapper if you want.

    @cbay
    Copy link
    Mannequin Author

    cbay mannequin commented Apr 7, 2010

    Here is a patch that implements SSLSocket.get_mode/set_mode, with the SSL_MODE_ENABLE_PARTIAL_WRITE and SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER constants defined in the ssl module.

    The patch contains a test case and documentation. It's made against trunk 44327 and also applies nicely with --fuzz=3 on a 2.6.5.

    There are no compatibility issues as no specific mode is set by default. It's up to the application to call SSLSocket.set_mode before use. I've tested my own use case with a set_mode(SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER), it works nicely.

    @pitrou
    Copy link
    Member

    pitrou commented Apr 7, 2010

    The patch adds a new feature, which makes it unsuitable for 2.6. I guess it could be applied to the 2.7 trunk, although a beta is being released and I'm not sure new features are really welcome afterwards. This one is really small and non-controversial, though, so I'd advocate accepting it.

    The patch itself looks good.

    @benjaminp
    Copy link
    Contributor

    Wouldn't it be nicer if mode was a property?

    @pitrou
    Copy link
    Member

    pitrou commented Apr 9, 2010

    Wouldn't it be nicer if mode was a property?

    Good point. I guess it would indeed...

    @pitrou
    Copy link
    Member

    pitrou commented Jul 8, 2010

    Patch should probably be rewritten to add a mode property on the new SSLContext object instead.

    @pitrou
    Copy link
    Member

    pitrou commented Jun 10, 2011

    See bpo-12197 for a related request.

    @bdarnell
    Copy link
    Mannequin

    bdarnell mannequin commented Aug 19, 2012

    Related pypy issue: https://bugs.pypy.org/issue1238

    @pitrou
    Copy link
    Member

    pitrou commented May 21, 2013

    I'm thinking that perhaps we should simply enable
    SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER by default. Ben, what do you think? Does the current behaviour allow to catch bugs?

    @pitrou pitrou added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels May 21, 2013
    @bdarnell
    Copy link
    Mannequin

    bdarnell mannequin commented May 22, 2013

    I vote for enabling SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER by default. It can catch mistakes (i.e. failure to check the return value of send) in Python just as easily as in C, but I don't think those mistakes are common enough to be worth the headache of this error. The false positive rate of this error is higher in Python than in C because we don't have direct control over memory and pointers.

    As for partial writes, I'm not sure if it's backwards compatible to turn them on by default, but it might be nice if the option were exposed. Partial writes may have less benefit in Python than in C since we'd have to reallocate and copy a string instead of just moving a pointer.

    @pitrou
    Copy link
    Member

    pitrou commented May 25, 2013

    As for partial writes, I'm not sure if it's backwards compatible to
    turn them on by default, but it might be nice if the option were
    exposed. Partial writes may have less benefit in Python than in C
    since we'd have to reallocate and copy a string instead of just moving
    a pointer.

    You can slice a memoryview() to avoid a copy. But I'm not sure of the point of partial writes here: can't you just send slices that are small enough (e.g. 4KB each)?

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented May 25, 2013

    New changeset 60310223d075 by Antoine Pitrou in branch 'default':
    Issue bpo-8240: Set the SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER flag on SSL sockets.
    http://hg.python.org/cpython/rev/60310223d075

    @pitrou
    Copy link
    Member

    pitrou commented May 25, 2013

    Ok, I should have fixed the original issue. If you want to see an option to enable partial writes, please open a separate issue.

    @pitrou pitrou closed this as completed May 25, 2013
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    extension-modules C modules in the Modules dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants