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

socketmodule.c: _FORTIFY_SOURCE=2 warning in AF_ALG case of getsockaddrarg() #82463

Closed
vstinner opened this issue Sep 26, 2019 · 7 comments
Closed
Labels
3.9 only security fixes stdlib Python modules in the Lib dir

Comments

@vstinner
Copy link
Member

BPO 38282
Nosy @pitrou, @vstinner, @tiran, @pablogsal
PRs
  • bpo-38282: Rewrite getsockaddrarg() helper function #16698
  • [3.8] bpo-38282: Rewrite getsockaddrarg() helper function (GH-16698) #16706
  • bpo-38282: Correctly manage the Bluetooth L2CAP socket structure in FreeBSD #16738
  • 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 2019-10-14.09:51:29.299>
    created_at = <Date 2019-09-26.01:11:45.394>
    labels = ['library', '3.9']
    title = 'socketmodule.c: _FORTIFY_SOURCE=2 warning in AF_ALG case of getsockaddrarg()'
    updated_at = <Date 2019-10-14.09:51:29.296>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2019-10-14.09:51:29.296>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-10-14.09:51:29.299>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2019-09-26.01:11:45.394>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38282
    keywords = ['patch']
    message_count = 7.0
    messages = ['353251', '353561', '354400', '354556', '354559', '354562', '354623']
    nosy_count = 4.0
    nosy_names = ['pitrou', 'vstinner', 'christian.heimes', 'pablogsal']
    pr_nums = ['16698', '16706', '16738']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue38282'
    versions = ['Python 3.9']

    @vstinner
    Copy link
    Member Author

    To test bpo-32375, I just recompiled the master branch of Python twice using these flags:

    • -D_FORTIFY_SOURCE=2 -Og
    • -D_FORTIFY_SOURCE=2 -O3

    I got a few warnings, the same that I get without FORTIFY SOURCE.

    Using -D_FORTIFY_SOURCE=2 -O3, I get one warning, but it's no longer from getpath.c:

    In file included from /usr/include/string.h:494,
    from ./Include/Python.h:30,
    from /home/vstinner/python/master/Modules/socketmodule.c:103:
    In function ‘memset’,
    inlined from ‘getsockaddrarg’ at /home/vstinner/python/master/Modules/socketmodule.c:2331:9,
    inlined from ‘sock_bind’ at /home/vstinner/python/master/Modules/socketmodule.c:3113:10:
    /usr/include/bits/string_fortified.h:71:10: warning: ‘__builtin_memset’ offset [17, 88] from the object at ‘addrbuf’ is out of the bounds of referenced subobject ‘sa’ with type ‘struct sockaddr’ at offset 0 [-Warray-bounds]
    71 | return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest));
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    The warning comes from the code:

        case AF_ALG:
            ...
            struct sockaddr_alg *sa;
            sa = (struct sockaddr_alg *)addr_ret;
            memset(sa, 0, sizeof(*sa));

    called from:

    static PyObject *
    sock_bind(PySocketSockObject *s, PyObject *addro)
    {
        sock_addr_t addrbuf;
        int addrlen;
        int res;
    
        if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen, "bind")) {
            return NULL;
        }

    @vstinner vstinner added 3.9 only security fixes stdlib Python modules in the Lib dir labels Sep 26, 2019
    @vstinner
    Copy link
    Member Author

    Similar when building Python on Fedora 30 (GCC version 9.2.1) with gcc -O3:

    In function ‘getsockaddrarg’,
    inlined from ‘sock_bind’ at /home/vstinner/python/master/Modules/socketmodule.c:3113:10:
    /home/vstinner/python/master/Modules/socketmodule.c:2331:9: warning: ‘memset’ offset [17, 88] from the object at ‘addrbuf’ is out of the bounds of referenced subobject ‘sa’ with type ‘struct sockaddr’ at offset 0 [-Warray-bounds]
    2331 | memset(sa, 0, sizeof(*sa));
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~

    @vstinner
    Copy link
    Member Author

    New changeset d565fb9 by Victor Stinner in branch 'master':
    bpo-38282: Rewrite getsockaddrarg() helper function (GH-16698)
    d565fb9

    @pablogsal
    Copy link
    Member

    The failures on FreeBSD are:

    /usr/home/buildbot/python/3.x.koobs-freebsd10.nondebug/build/Modules/socketmodule.c:1931:50: error: no member named 'bt_l2' in 'union sock_addr'
                struct sockaddr_l2 *addr = &addrbuf->bt_l2;
                                            ~~~~~~~  ^
    /usr/home/buildbot/python/3.x.koobs-freebsd10.nondebug/build/Modules/socketmodule.c:1950:50: error: no member named 'bt_rc' in 'union sock_addr'
                struct sockaddr_rc *addr = &addrbuf->bt_rc;
                                            ~~~~~~~  ^
    /usr/home/buildbot/python/3.x.koobs-freebsd10.nondebug/build/Modules/socketmodule.c:1967:51: error: no member named 'bt_hci' in 'union sock_addr'
                struct sockaddr_hci *addr = &addrbuf->bt_hci;
                                             ~~~~~~~  ^
    3 errors generated.

    This is likely due to the fact that those fields are protected by:

    typedef union sock_addr {
    #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
        struct sockaddr_l2 bt_l2;
        struct sockaddr_rc bt_rc;
        struct sockaddr_sco bt_sco;
        struct sockaddr_hci bt_hci;
    #elif defined(MS_WINDOWS)
    }

    and in those machines HAVE_BLUETOOTH_BLUETOOTH_H is absent.

    @pablogsal
    Copy link
    Member

    PR 16738 fixes the FreeBSD buildbots by correctly handling the Bluetooth L2CAP socket structure in FreeBSD if only bluetooth.h is available. Check out https://man.cx/ng_btsocket(4) for example for more info in the ng_btsocket protocol for FreeBSD.

    @pablogsal
    Copy link
    Member

    New changeset 27b33fb by Pablo Galindo in branch 'master':
    bpo-38282: Correctly manage the Bluetooth L2CAP socket structure in FreeBSD (GH-16738)
    27b33fb

    @vstinner
    Copy link
    Member Author

    I close the issue.

    I planned to backport the change to 3.8, but 3.8.0 final is supposed to be released today. I prefer to avoid putting a last minute regression on some platforms like DragonflyBSD in 3.8. Backporting the change is not strictly required, it's just a compiler warning, it's a false alarm, and it's only when building with _FORTIFY_SOURCE defined.

    @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
    3.9 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants