Bug 1367216 - nss.io.Socket.import_tcp_socket imports socket as closed
Summary: nss.io.Socket.import_tcp_socket imports socket as closed
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Fedora
Classification: Fedora
Component: python-nss
Version: rawhide
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: ---
Assignee: John Dennis
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
: 1322958 1377414 (view as bug list)
Depends On:
Blocks: 1379863
TreeView+ depends on / blocked
 
Reported: 2016-08-15 21:44 UTC by Patrick Uiterwijk
Modified: 2016-11-19 17:23 UTC (History)
3 users (show)

Fixed In Version: python-nss-1.0.0 python-nss-1.0.0-1.fc25 python-nss-1.0.0-2.fc24
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
: 1379863 (view as bug list)
Environment:
Last Closed: 2016-11-19 17:23:16 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)
trial patch to see if it solves Patrick's problem (4.85 KB, text/plain)
2016-08-16 16:28 UTC, John Dennis
no flags Details

Description Patrick Uiterwijk 2016-08-15 21:44:27 UTC
Description of problem:
After using nss.io.Socket.import_tcp_socket, the moment I try to read from the imported socket, I get a ValueError: I/O operation on closed socket.

Version-Release number of selected component (if applicable):
python-nss-1.0.0-beta1.2.fc24.1

How reproducible:
Consistent

Steps to Reproduce:
1. Run the below code

Actual results:
Traceback (most recent call last):
  File "test.py", line 31, in <module>
    test()
  File "test.py", line 29, in test
    print c1_nss.recv(1024)
ValueError: I/O operation on closed socket


Expected results:
Hello world

Additional info:
This works on the same machine if I downgrade python-nss to 0.16.0.


Code:
import socket
import nss
import nss.ssl
import nss.io

def _tcp_socketpair():
    '''Like socket.socketpair(), but using AF_INET sockets.

    This is necessary because NSS uses getsockname() to create session
    identifiers and it does not support AF_UNIX sockets created by
    socket.socketpair().'''

    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
                           socket.IPPROTO_TCP)
    server.bind(('', 0))
    server.listen(1)
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
                           socket.IPPROTO_TCP)
    client.connect(server.getsockname())
    (server2, _) = server.accept()
    server.close()
    return (client, server2)

def test():
    (c1, s1) = _tcp_socketpair()
    c1_nss = nss.io.Socket.import_tcp_socket(c1.fileno())
    s1.sendall('hello world')
    print c1_nss.recv(1024)

test()

Comment 1 Patrick Uiterwijk 2016-08-15 21:48:42 UTC
*** Bug 1322958 has been marked as a duplicate of this bug. ***

Comment 2 Patrick Uiterwijk 2016-08-15 22:14:02 UTC
It's not just import_tcp_socket that's broken. The nss.io.Socket.new_tcp_pair is also broken.
This reproducer has the same effect:


import nss.io

(c1, s1) = nss.io.Socket.new_tcp_pair()
s1.sendall('hello world')
print c1.recv(1024)

Comment 3 John Dennis 2016-08-16 16:25:19 UTC
Mostly making notes for myself in this comment.

I built a scratch version for Patrick which removed all the socket state tracking code and checks for the socket being open (patch attached: remove_open_for_read.patch) and that solved his problem, but it's probably not the correct solution.

The socket state code was added to address a segfault identified in bug #1229748

It's obvious to me now that trying to track the state of a socket is futile, especially since external fd's can be passed in and manipulated outside of the python binding. The central issue seems to be whether pr_socket should be set to NULL on close and where and when PR Socket resources are cleaned up.

We still need to protect against a segfault so we either need to not set pr_socket to null in the python Socket object when close is called, or we need similar code to SOCKET_CHECK_OPEN that only tests if pr_socket is NULL.

I need to check what happens when PR_Close() is called, is the handle still valid? Who cleans up the resources for the PR Socket? Is there a separate call for that or does that happen when PR_Close() is called? I probably knew the answers at one point but the code in question was written so many years ago I now forget. The answer dictates whether Socket_close() should set pr_socket to NULL or not. There does not seem to be any code in Socket_delloc() that closes the socket and frees PR resources. That looks like an oversight and potential leak.

Comment 4 John Dennis 2016-08-16 16:28:41 UTC
Created attachment 1191319 [details]
trial patch to see if it solves Patrick's problem

trial patch to see if it solves Patrick's problem, it's not a correct solution but has elements of what needs to be adjusted. Mostly here to identify all the code locations that need to be looked at.

Comment 5 Patrick Uiterwijk 2016-09-19 16:11:15 UTC
*** Bug 1377414 has been marked as a duplicate of this bug. ***

Comment 6 Patrick Uiterwijk 2016-09-19 16:13:35 UTC
Could this please be backported to supported Fedora releases?

Comment 7 Fedora Update System 2016-09-22 09:55:17 UTC
python-nss-1.0.0-1.fc25 has been pushed to the Fedora 25 testing repository. If problems still persist, please make note of it in this bug report.
See https://fedoraproject.org/wiki/QA:Updates_Testing for
instructions on how to install test updates.
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2016-189a2448ae

Comment 8 Fedora Update System 2016-09-23 08:01:08 UTC
python-nss-1.0.0-1.fc24 has been pushed to the Fedora 24 testing repository. If problems still persist, please make note of it in this bug report.
See https://fedoraproject.org/wiki/QA:Updates_Testing for
instructions on how to install test updates.
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2016-1801ff25bd

Comment 9 Fedora Update System 2016-09-25 20:56:10 UTC
python-nss-1.0.0-1.fc25 has been pushed to the Fedora 25 stable repository. If problems still persist, please make note of it in this bug report.

Comment 10 John Dennis 2016-09-27 22:47:05 UTC
re comment #6, this bz was filed against rawhide, I'm cloning it into f24 and will update f24. f23 cannot be updated because f23 is still at the 0 major release version and we're not supposed to bump major versions.

Comment 11 Fedora Update System 2016-09-29 02:50:11 UTC
python-nss-1.0.0-2.fc24 has been pushed to the Fedora 24 testing repository. If problems still persist, please make note of it in this bug report.
See https://fedoraproject.org/wiki/QA:Updates_Testing for
instructions on how to install test updates.
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2016-c93fd2726a

Comment 12 Fedora Update System 2016-10-09 13:51:24 UTC
python-nss-1.0.0-2.fc24 has been pushed to the Fedora 24 testing repository. If problems still persist, please make note of it in this bug report.
See https://fedoraproject.org/wiki/QA:Updates_Testing for
instructions on how to install test updates.
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2016-c93fd2726a

Comment 13 Fedora Update System 2016-11-19 17:23:16 UTC
python-nss-1.0.0-2.fc24 has been pushed to the Fedora 24 stable repository. If problems still persist, please make note of it in this bug report.


Note You need to log in before you can comment on or make changes to this bug.