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

syslog logging handler fails with address in unix abstract namespace #70990

Open
xdegaye mannequin opened this issue Apr 19, 2016 · 14 comments
Open

syslog logging handler fails with address in unix abstract namespace #70990

xdegaye mannequin opened this issue Apr 19, 2016 · 14 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@xdegaye
Copy link
Mannequin

xdegaye mannequin commented Apr 19, 2016

BPO 26803
Nosy @vsajip, @vstinner, @xdegaye
Files
  • unix_abstract_namespace.patch
  • unix_abstract_namespace_2.patch: address in abstract namespace as a string with file system encoding
  • unix_abstract_namespace_3.patch
  • 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 = None
    created_at = <Date 2016-04-19.07:35:16.032>
    labels = ['type-bug', 'library']
    title = 'syslog logging handler fails with address in unix abstract namespace'
    updated_at = <Date 2016-04-21.11:42:16.373>
    user = 'https://github.com/xdegaye'

    bugs.python.org fields:

    activity = <Date 2016-04-21.11:42:16.373>
    actor = 'xdegaye'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2016-04-19.07:35:16.032>
    creator = 'xdegaye'
    dependencies = []
    files = ['42513', '42542', '42543']
    hgrepos = []
    issue_num = 26803
    keywords = ['patch']
    message_count = 14.0
    messages = ['263715', '263745', '263802', '263810', '263817', '263828', '263837', '263838', '263839', '263840', '263841', '263857', '263858', '263913']
    nosy_count = 3.0
    nosy_names = ['vinay.sajip', 'vstinner', 'xdegaye']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue26803'
    versions = ['Python 3.6']

    @xdegaye
    Copy link
    Mannequin Author

    xdegaye mannequin commented Apr 19, 2016

    The traceback:

        Traceback (most recent call last):
          File "Lib/logging/handlers.py", line 917, in emit
            self.socket.sendto(msg, self.address)
        TypeError: getsockaddrarg: AF_INET address must be tuple, not bytes

    The attached patch reproduces the issue with a test case and fixes it.

    @xdegaye xdegaye mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 19, 2016
    @vstinner
    Copy link
    Member

    The following change looks good to me:

    -        if isinstance(address, str):
    +        if isinstance(address, (str, bytes)):
                 self.unixsocket = True
                 self._connect_unixsocket(address)

    But I don't understand the testcase. Is an address starting with a NULL character a special address? How does it the bytes address case?

    + # override the definition in the base class
    + self.address = '\x00python_logging_test'

    "syslog logging handler fails with address in unix abstract namespace"

    What is the unix abstract namespace?

    @xdegaye
    Copy link
    Mannequin Author

    xdegaye mannequin commented Apr 20, 2016

    A unix abstract socket address is defined here:
    http://man7.org/linux/man-pages/man7/unix.7.html
    as:

    "Traditionally, UNIX domain sockets can be either unnamed, or bound to
    a filesystem pathname (marked as being of type socket). Linux also
    supports an abstract namespace which is independent of the
    filesystem."

    and:

    "abstract: an abstract socket address is distinguished (from a
    pathname socket) by the fact that sun_path[0] is a null byte ('\0').
    ..."

    It is also documented in the Python socket module documentation at
    https://docs.python.org/3/library/socket.html
    and tested in the TestLinuxAbstractNamespace test of
    Lib/test/test_socket.py.

    @vstinner
    Copy link
    Member

    Ok. And what is the link between abstract namespace and the fact that the syslog handler gets bytes? Is it tested by your change?

    @xdegaye
    Copy link
    Mannequin Author

    xdegaye mannequin commented Apr 20, 2016

    The SysLogHandlerTest class instantiates a 'server_class' that eventually calls the
    server_bind() method of the TCPServer class of the socketserver module.

    This server_bind() method executes the statements:

        self.socket.bind(self.server_address)
        self.server_address = self.socket.getsockname()

    and when self.server_address is a string and contains a null byte, then
    getsockname() returns a bytes object.

    So finally, self.server in SysLogHandlerTest is a bytes object in this case,
    hence the needed to fix Lib/logging/handlers.py.

    @vstinner
    Copy link
    Member

    Xavier de Gaye added the comment:

    when self.server_address is a string and contains a null byte, then
    getsockname() returns a bytes object.

    Ah? It sounds strange to me that the type of getsockname() depends on
    the NULL byte. I would also expect a Unicode name for the abstract
    namespace. What do you think?

    @xdegaye
    Copy link
    Mannequin Author

    xdegaye mannequin commented Apr 20, 2016

    makesockaddr() in socketmodule.c calls PyBytes_FromStringAndSize() when the
    first byte is a null byte.

    My opinion is not worth much in this matter :). The socket documentation does
    say that AF_UNIX addresses are "represented as a string, using the file system
    encoding", so the implementation looks wrong. However it is possible that
    PyUnicode_DecodeFSDefault() fails for some file system encodings when the
    encoded address contains null bytes ?

    @vstinner
    Copy link
    Member

    However it is possible that
    PyUnicode_DecodeFSDefault() fails for some file system encodings when the
    encoded address contains null bytes ?

    No, it's not possible. Undecode bytes are escaped as surrogate characters using the surrogateescape error handler. See the PEP-393.

    @xdegaye
    Copy link
    Mannequin Author

    xdegaye mannequin commented Apr 20, 2016

    Then it seems that makesockaddr() should be fixed instead of the logging
    handler.

    @vstinner
    Copy link
    Member

    Then it seems that makesockaddr() should be fixed instead of the logging handler.

    Yes. That's why I didn't understand the issue at the beginning :-) Would you like to work on fixing the _socket module?

    It may be worth to keep your syslog unit test. It's up to you.

    @xdegaye
    Copy link
    Mannequin Author

    xdegaye mannequin commented Apr 20, 2016

    I will give it a try.

    @xdegaye
    Copy link
    Mannequin Author

    xdegaye mannequin commented Apr 20, 2016

    Here is the patch.

    As a reference, issue bpo-8373 with changeset 1f23bb74f4bc changed the AF_UNIX
    socket addresses encoding from UTF-8 to the file system encoding and the
    'surrogateescape' error handler (PEP-383).

    @xdegaye
    Copy link
    Mannequin Author

    xdegaye mannequin commented Apr 20, 2016

    This patch corrects the non-ascii quote character in the Misc/NEWS diff.

    @xdegaye
    Copy link
    Mannequin Author

    xdegaye mannequin commented Apr 21, 2016

    In makesockaddr(), the current implementation does not do any decoding of AF_UNIX addresses in the abstract namespace in the struct sockaddr_un to bytes direction, i.e. system to python direction, but does encode a string or bytes object to struct sockaddr_un in the reverse direction, in getsockaddrarg(). This is not correct.

    So the patch does more than fixing the type of the addresses as it also fixes this in a non backward compatible way. Maybe this should be indicated in Misc/NEWS.

    @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
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    Status: No status
    Development

    No branches or pull requests

    1 participant