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

os.urandom(2500) fails on Solaris 11.3 #70922

Closed
mryan1539 mannequin opened this issue Apr 11, 2016 · 8 comments
Closed

os.urandom(2500) fails on Solaris 11.3 #70922

mryan1539 mannequin opened this issue Apr 11, 2016 · 8 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@mryan1539
Copy link
Mannequin

mryan1539 mannequin commented Apr 11, 2016

BPO 26735
Nosy @rhettinger, @vstinner
Files
  • python3-getrandom.patch
  • urandom_solaris.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 = <Date 2016-04-12.20:40:59.375>
    created_at = <Date 2016-04-11.17:35:08.037>
    labels = ['interpreter-core', 'type-bug']
    title = 'os.urandom(2500) fails on Solaris 11.3'
    updated_at = <Date 2016-04-12.20:40:59.374>
    user = 'https://bugs.python.org/mryan1539'

    bugs.python.org fields:

    activity = <Date 2016-04-12.20:40:59.374>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-04-12.20:40:59.375>
    closer = 'vstinner'
    components = ['Interpreter Core']
    creation = <Date 2016-04-11.17:35:08.037>
    creator = 'mryan1539'
    dependencies = []
    files = ['42433', '42443']
    hgrepos = []
    issue_num = 26735
    keywords = ['patch']
    message_count = 8.0
    messages = ['263191', '263235', '263236', '263259', '263261', '263269', '263274', '263275']
    nosy_count = 4.0
    nosy_names = ['rhettinger', 'vstinner', 'python-dev', 'mryan1539']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue26735'
    versions = ['Python 3.5']

    @mryan1539
    Copy link
    Mannequin Author

    mryan1539 mannequin commented Apr 11, 2016

    On Solaris 11.3 (intel tested, but I assume issue is on SPARC as well),
    I found the following fails:
    import os
    os.urandom(2500)

    The above throws OSError: [Errno 22] Invalid argument.

    It turns out that the Solaris version of getrandom() is limited to returning
    no more than 1024 bytes, per the manpage:
    The getrandom() and getentropy() functions fail if:

    EINVAL The flags are not set to GRND_RANDOM, GRND_NONBLOCK or both,
    or bufsz is <= 0 or > 1024.

    I've attached a possible patch for this issue, against the 3.5.1 source
    tree.

    @mryan1539 mryan1539 mannequin added the type-bug An unexpected behavior, bug, or error label Apr 11, 2016
    @SilentGhost SilentGhost mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Apr 12, 2016
    @vstinner
    Copy link
    Member

    See also the issue bpo-25003 and the changeset 835085cc28cd:

    Issue bpo-25003: On Solaris 11.3 or newer, os.urandom() now uses the getrandom() function instead of the getentropy() function. The getentropy() function is blocking to generate very good quality entropy, os.urandom() doesn't need such high-quality entropy.

    @vstinner
    Copy link
    Member

    I've attached a possible patch for this issue, against the 3.5.1 source
    tree.

    I guess that you are already using Python 3.5.1 which uses getrandom(). You should try to confirm using strace.

    I updated your patch. I replaced "#if defined(sun)" with "#ifdef sun", since "#ifdef sun" looks more common in the Python code base, and I never saw "#if defined(sun)" in the Python code base.

    I also avoided the new len variable, I reused the n variable.

    I don't have Solaris, so I cannot test. I didn't find getrandom() manual page neither, I only found this blog post which doesn't mention the 1024 bytes limitation on Solaris:
    https://blogs.oracle.com/darren/entry/solaris_new_system_calls_getentropy

    @mryan1539
    Copy link
    Mannequin Author

    mryan1539 mannequin commented Apr 12, 2016

    The new patch looks fine; I used __sun__ rather than sun out of habit
    (C standard requires
    system specific macros be in the reserved namespace), but either will work.

    I found the original problem through debugging with GDB, so I know
    getrandom() was being
    called, and the test case I provided (taken from Lib/Random.py) fails
    without the patch,
    and succeeds with it.

    Oddly, the blog post you linked to describes getrandom as:
    "Recent Linux kernels have a getrandom(2) system call that reads
    between 1 and 1024 bytes of randomness"

    But no such limit current exists in the Linux version that I can see;
    however, the Solaris
    version definitely does have that limit:
    https://docs.oracle.com/cd/E53394_01/html/E54765/getrandom-2.html

    On Tue, Apr 12, 2016 at 1:15 AM, STINNER Victor <report@bugs.python.org> wrote:

    STINNER Victor added the comment:

    > I've attached a possible patch for this issue, against the 3.5.1 source
    tree.

    I guess that you are already using Python 3.5.1 which uses getrandom(). You should try to confirm using strace.

    I updated your patch. I replaced "#if defined(sun)" with "#ifdef sun", since "#ifdef sun" looks more common in the Python code base, and I never saw "#if defined(sun)" in the Python code base.

    I also avoided the new len variable, I reused the n variable.

    I don't have Solaris, so I cannot test. I didn't find getrandom() manual page neither, I only found this blog post which doesn't mention the 1024 bytes limitation on Solaris:
    https://blogs.oracle.com/darren/entry/solaris_new_system_calls_getentropy

    ----------
    Added file: http://bugs.python.org/file42443/urandom_solaris.patch


    Python tracker <report@bugs.python.org>
    <http://bugs.python.org/issue26735\>


    @vstinner
    Copy link
    Member

    The new patch looks fine

    Do you mean that it fixes your issue? Can it be applied to Python 3.5 & 3.6?

    @mryan1539
    Copy link
    Mannequin Author

    mryan1539 mannequin commented Apr 12, 2016

    Yes, I've verified that:

    • the issue existed in the default branch as of this morning.
    • the patch applies cleanly against both 3.5 and default, and
      addresses the issue in both branches.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 12, 2016

    New changeset fb7628e8dfef by Victor Stinner in branch '3.5':
    Fix os.urandom() on Solaris 11.3
    https://hg.python.org/cpython/rev/fb7628e8dfef

    @vstinner
    Copy link
    Member

    Yes, I've verified that: (...)

    Cool, thanks for the bug report and for the check.

    It's now fixed. In the meanwhile, you can workaround the issue by limiting yourself calls to os.urandom() to 1024 bytes (and then concatenate the result).

    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant