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

Uninitialized variable may be used in PyUnicode_DecodeUTF7Stateful() #49639

Closed
gvanrossum opened this issue Feb 27, 2009 · 9 comments
Closed

Comments

@gvanrossum
Copy link
Member

BPO 5389
Nosy @malemburg, @gvanrossum, @loewis, @birkenfeld, @pitrou

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 2009-03-05.21:47:49.622>
created_at = <Date 2009-02-27.22:13:12.876>
labels = []
title = 'Uninitialized variable may be used in PyUnicode_DecodeUTF7Stateful()'
updated_at = <Date 2009-03-05.21:47:49.621>
user = 'https://github.com/gvanrossum'

bugs.python.org fields:

activity = <Date 2009-03-05.21:47:49.621>
actor = 'gvanrossum'
assignee = 'none'
closed = True
closed_date = <Date 2009-03-05.21:47:49.622>
closer = 'gvanrossum'
components = []
creation = <Date 2009-02-27.22:13:12.876>
creator = 'gvanrossum'
dependencies = []
files = []
hgrepos = []
issue_num = 5389
keywords = []
message_count = 9.0
messages = ['82881', '82897', '82901', '83007', '83041', '83186', '83207', '83219', '83222']
nosy_count = 5.0
nosy_names = ['lemburg', 'gvanrossum', 'loewis', 'georg.brandl', 'pitrou']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue5389'
versions = ['Python 2.5']

@gvanrossum
Copy link
Member Author

[Found by a Googler who prefers to remain anonymous]

This might be easier to trigger on a 64-bit:

PyObject *PyUnicode_DecodeUTF7Stateful(...)
{
    ...
    Py_ssize_t startinpos;
    ...
    while (s < e) {
    ...
      utf7Error:
        outpos = p-PyUnicode_AS_UNICODE(unicode);
        endinpos = s-starts;
        if (unicode_decode_call_errorhandler(
                errors, &errorHandler,
                "utf7", errmsg,
                starts, size, &startinpos, &endinpos, &exc, &s,
                &unicode, &outpos, &p))
        ...
    }
    ...
}

The lack of initialization of startinpos will lead to the likelihood of
the value being >= INT_MAX with a 64-bit value, leading to the
subsequent assert [somewhere in unicode_decode_call_errorhandler()]. In
theory the assert could trigger in 32-bit if the uninitialized value
happened to get set to INT_MAX.

The other similar variable also probably need to be initialized.
Furthermore, the function PyUnicode_DecodeUTF8Stateful also has the same
uninitialized variables.

@birkenfeld
Copy link
Member

I can't see at the moment how the unicode_decode_call_errorhandler call
can be made without startinpos being previously set to some value.
Antoine, maybe you can verify?

@pitrou
Copy link
Member

pitrou commented Feb 28, 2009

Hmm, I know nothing about UTF7...

Anyway, looking at the code, the utf7Error code path can be called from
the following places (trunk line numbers):

  • line 1595, and startinpos was set three lines before
  • a bunch of places in the "if (inShift) { ... }" chunk between lines
    1537 and 1578; inShift would have had previously been set to 1 and
    that's at line 1587, a couple of lines after setting startinpos

So it seems things are fine, but perhaps I'm missing something.

@malemburg
Copy link
Member

The UTF-7 codec implementation has a few problems (one of them is that
it is hardly being used, so bugs only get detected very slowly).

bpo-4426 has a patch with cleaned up and more standards compliant
implementation. Perhaps that also fixes the problem with uninitialized
variables.

@gvanrossum
Copy link
Member Author

It looks like it was fixed in 2.6 by adding an assignment to startinpos
to this block:

       else if (SPECIAL(ch,0,0)) {
           startinpos = s-starts;     /* <---------- This was added */
           errmsg = "unexpected special character";
           s++;
           goto utf7Error;
       }

Are we going to release another 2.5, ever?

@birkenfeld
Copy link
Member

Only with security fixes IIRC. Letting Martin decide.

@birkenfeld birkenfeld assigned loewis and unassigned pitrou Mar 5, 2009
@gvanrossum
Copy link
Member Author

Well, this one is technically a security fix, though I have no idea how
it could be exploited unless you offer your users a facility to execute
arbitrary Python code.

@loewis
Copy link
Mannequin

loewis mannequin commented Mar 5, 2009

I agree it is technically a security fix, so somebody please feel free
to commit it. I will make another 2.5 release when enough of these have
accumulated, or something urgent happens, or somebody wants to see a
release really badly :-)

@loewis loewis mannequin removed their assignment Mar 5, 2009
@gvanrossum
Copy link
Member Author

OK, submitted.

@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
None yet
Projects
None yet
Development

No branches or pull requests

4 participants