Skip to content

asctime does not check its input #50857

Closed

Description

@AmirHabibi
BPO 6608
Nosy @malemburg, @abalkin, @pitrou, @vstinner, @avassalotti
Files
  • asctime.patch
  • issue6608.diff: Untabified asctime.patch
  • issue6608-p3k.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 = 'https://github.com/abalkin'
    closed_at = <Date 2010-10-01.14:23:55.817>
    created_at = <Date 2009-07-30.23:56:47.278>
    labels = ['extension-modules', 'easy', 'OS-windows', 'type-crash']
    title = 'asctime does not check its input'
    updated_at = <Date 2010-10-01.14:23:55.815>
    user = 'https://bugs.python.org/AmirHabibi'

    bugs.python.org fields:

    activity = <Date 2010-10-01.14:23:55.815>
    actor = 'belopolsky'
    assignee = 'belopolsky'
    closed = True
    closed_date = <Date 2010-10-01.14:23:55.817>
    closer = 'belopolsky'
    components = ['Extension Modules', 'Windows']
    creation = <Date 2009-07-30.23:56:47.278>
    creator = 'AmirHabibi'
    dependencies = []
    files = ['14628', '17468', '19004']
    hgrepos = []
    issue_num = 6608
    keywords = ['patch', 'easy']
    message_count = 22.0
    messages = ['91115', '91120', '91157', '91199', '99235', '106533', '106538', '106958', '107570', '107572', '107596', '107605', '108574', '117196', '117206', '117254', '117312', '117313', '117315', '117348', '117776', '117804']
    nosy_count = 9.0
    nosy_names = ['lemburg', 'belopolsky', 'pitrou', 'vstinner', 'ruseel', 'alexandre.vassalotti', 'srid', 'abbeyj', 'AmirHabibi']
    pr_nums = []
    priority = 'low'
    resolution = 'accepted'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue6608'
    versions = ['Python 3.2']

    Activity

    1. AmirHabibi commented on Jul 30, 2009

      AmirHabibimannequin
      MannequinAuthor

      I use:
      Python 2.6.2
      (r262:71605, Apr 14 2009, 22:40:02)
      [MSC v.1500 32 bit (Intel)] on win32

      import time
      time.asctime((2009, 1, 1, 24, 0, 0, 0, 0, 0))

      the 24 is a wrong parameter but it should'n't crash the engine. This may
      be the side effect of a more serious problem though. An assertion may
      fix it.

    2. added
      type-crashA hard crash of the interpreter, possibly with a core dump
      on Jul 30, 2009
    3. avassalotti commented on Jul 31, 2009

      @avassalotti
      Member

      Confirmed. On Windows, the out-of-range value triggers a debugging
      assertion in the CRT library.

    4. abbeyj commented on Aug 1, 2009

      abbeyjmannequin
      Mannequin

      Since there's no good way to disable the assertion (see bpo-4804),
      checking the validity of the argument beforehand looks like an option.
      The checking that's currently being done in the strftime()
      implementation looks useful but it is not enough. The checking in the
      MS implementation of asctime() is very strict and validates the entire
      date, not just one field at a time. So there's no way to print out
      non-existant dates like (2009, 2, 31, 0, 0, 0, 0, 0, 0) -> 'Mon Feb 31
      00:00:00 2009'.

      I don't know if anybody is relying on that kind of behavior. If not
      then the function could be limited to accept only valid dates.
      Alternatively we could just not call down to asctime() but instead
      provide our own implementation using sprintf/strftime.

    5. abbeyj commented on Aug 2, 2009

      abbeyjmannequin
      Mannequin

      Further investigation shows that MS asctime() doesn't like leap seconds
      and causes an assertion when passing (2008, 12, 31, 23, 59, 60, 2, 366,
      -1) -> 'Wed Dec 31 23:59:60 2008'.

      Given that and since asctime() is such a simple function I think it
      makes more sense to roll our own. Attached is a rough patch which does
      this. It pulls the bounds checking used in strftime() out into its own
      function so it can be used in both places. Overriding ctime() is
      necessary because Windows decided to use " %02d" instead of "%3d" to
      print the day of the month. Without overriding ctime() the output of
      ctime(t) would be different from asctime(localtime(t)) and cause
      test_conversion() to fail. There is a USE_SYSTEM_ASCTIME switch to
      decide whether to use the system asctime() or the internal one.

    6. pitrou commented on Feb 11, 2010

      @pitrou
      Member

      Retrograding to critical after popular python-dev demand.

    7. abalkin commented on May 26, 2010

      @abalkin
      Member

      I have untabified James' patch, ran the tests on the result, but did not otherwise review it.

      There is a not-so-easy-to-find thread on python-dev discussing this issue under subject "Python 2.6.5".

      Here is a relevant quote from Martin v. Löwis:

      """
      That would require that Barry actually *can* judge the issue at hand. In
      the specific case, I would expect that Barry would defer the specifics
      of the Windows issue to Windows experts, and then listen to what they
      say.

      I'm personally split whether the proposed patch is correct (i.e. whether
      asctime really *can* be implemented in a cross-platform manner; any
      definite ruling on that would be welcome). In the past, we had rather
      taken approaches like disabling runtime assertions "locally"; not sure
      whether such approaches would work for asctime as well.

      In any case, I feel that the issue is not security-critical at all.
      People just don't pass out-of-range values to asctime, but instead
      typically pass the result of gmtime/localtime, which will not cause any
      problems.
      """

    8. added
      stdlibStandard Library Python modules in the Lib/ directory
      and removed on May 26, 2010
    9. self-assigned this
      on May 26, 2010
    10. 12 remaining items

    11. avassalotti commented on Jun 11, 2010

      @avassalotti
      Member

      How about checking the preconditions before calling asctime()? If the check fails, then we can raise an exception without crashing.

    12. abalkin commented on Jun 11, 2010

      @abalkin
      Member

      That's what CERT recommends. Their code can be reused as is:

      int validate_tm(struct tm* time) {
        /* 
         * The range of valid values of the tm_sec member is [0, 60] 
         * inclusive (to allow for leap seconds).
         */
        if (time->tm_sec < 0 || time->tm_sec > 60) return 0;
        if (time->tm_min < 0 || time->tm_min >= 60) return 0;
        if (time->tm_hour < 0 || time->tm_hour >= 24) return 0;
        if (time->tm_mday <= 0 || time->tm_mday > 31) return 0;
        if (time->tm_mon < 0 || time->tm_mon >= 12) return 0;
        /* While other years are legit, they may overflow asctime()'s buffer */
        if (time->tm_year < -999 || time->tm_year > 9999) return 0;
        if (time->tm_wday < 0 || time->tm_wday >= 7) return 0;
        if (time->tm_yday < 0 || time->tm_yday >= 366) return 0;
        return 1;
      }
    13. abalkin commented on Jun 25, 2010

      @abalkin
      Member

      I've just noticed that time_strftime already has the range checks for tm structure fields. These checks can be separated in a function and shared with asctime. Marking this as "easy". See also bpo-897625.

    14. ruseel commented on Sep 23, 2010

      ruseelmannequin
      Mannequin

      As alexandre.vassalotti pointed in msg107596, I added precondition check.

      • extracted the range check from time_strftime as "is_valid_tm".

      • time_asctime, time_strftime call "is_valid_tm"

      • testcase for both asctime and strftime (abbeyj's work)

      I splited patch just for unittest files(Lib/test/test_time.py) and (Modules/timemodule.c)

    15. abalkin commented on Sep 23, 2010

      @abalkin
      Member

      The patch looks good to me. Just a few nitpicks: the local convention seems to be no underscores in helper functions such as gettmarg. I would call is_valid_tm, "checktm" instead. Also, predicate-like naming (is_...) suggests a function without side-effects, while is_valid_tm sets an error when it gets an invalid tm. Also, please add missing comments above gettmarg and is_valid_tm. Finally, please start the function body with a "{" on a new line. Thank you for your submission.

    16. ruseel commented on Sep 24, 2010

      ruseelmannequin
      Mannequin

      Thank you for a kind guidance.

      I uploaded patch with

      • function name "checktm"
      • add comment above gettmarg
      • move comment in "checktm" to (above function signature)
      • change comment little bit to mention bpo-6608
      • "{" on new line
    17. abalkin commented on Sep 24, 2010

      @abalkin
      Member

      MunSic,

      Your patch does not apply cleanly to the py3k branch. Please not that new features can only go to py3k. While some may argue that this issue is a bug, I think it is more likely that it will only be accepted for 3.2. In any case, a py3k patch should be the starting point. Could you post a patch agains the py3k branch?

    18. abalkin commented on Sep 24, 2010

      @abalkin
      Member

      Hmm, it looks like bpo-6608-timemodule-2nd.patch is a patch on top of your previous patches. I'll check if I can apply last three patches in order.

    19. abalkin commented on Sep 24, 2010

      @abalkin
      Member

      No, it looks like the starting patch is against 2.7. We need a py3k patch. Meanwhile, I've noticed a couple of grammatical mistakes in comments:

      "strftime(), asctime() does not" -> "strftime() and asctime() do not"
      "fixes bug bpo-897625, bpo-6608" -> "fixes bugs bpo-897625 and bpo-6608"

    20. ruseel commented on Sep 25, 2010

      ruseelmannequin
      Mannequin

      I uploaded a patch against py3k branch.

      Sorry for many "Added file" and "Removed file" mails. I am learning customs Trac now.

    21. ruseel commented on Oct 1, 2010

      ruseelmannequin
      Mannequin

      belopolsky

      I am little worried about my words. I made patch against 2.7-maint branch. But I recognized that patch is not following rules, because of your guidance. Then I uploaded another patch against py3k branch again. and bpo-6608-p3k.patch could be applied to py3k branch cleanly. If there were any boldness in my words, I'm sorry.

      and I'm ready to address any thing left in this issue. :)

    22. abalkin commented on Oct 1, 2010

      @abalkin
      Member

      Committed with minor changes in r85137. Thanks for the patch!

    23. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    OS-windowseasyextension-modulesC modules in the Modules dirtype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions