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

NULL pointer deref on error path in _ssl debughelpers.c #84260

Closed
ariccio mannequin opened this issue Mar 26, 2020 · 2 comments
Closed

NULL pointer deref on error path in _ssl debughelpers.c #84260

ariccio mannequin opened this issue Mar 26, 2020 · 2 comments
Assignees
Labels
3.9 only security fixes topic-SSL

Comments

@ariccio
Copy link
Mannequin

ariccio mannequin commented Mar 26, 2020

BPO 40079
Nosy @tiran, @ariccio

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/tiran'
closed_at = <Date 2021-04-17.19:40:27.881>
created_at = <Date 2020-03-26.21:56:53.776>
labels = ['expert-SSL', '3.9']
title = 'NULL pointer deref on error path in _ssl debughelpers.c'
updated_at = <Date 2021-04-17.19:40:27.880>
user = 'https://github.com/ariccio'

bugs.python.org fields:

activity = <Date 2021-04-17.19:40:27.880>
actor = 'christian.heimes'
assignee = 'christian.heimes'
closed = True
closed_date = <Date 2021-04-17.19:40:27.881>
closer = 'christian.heimes'
components = ['SSL']
creation = <Date 2020-03-26.21:56:53.776>
creator = 'Alexander Riccio'
dependencies = []
files = []
hgrepos = []
issue_num = 40079
keywords = []
message_count = 2.0
messages = ['365114', '391310']
nosy_count = 2.0
nosy_names = ['christian.heimes', 'Alexander Riccio']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue40079'
versions = ['Python 3.9']

@ariccio
Copy link
Mannequin Author

ariccio mannequin commented Mar 26, 2020

At line 138 in debughelpers.c, ssl_obj, which was set to NULL on line 122, is dereferenced.

I think the original intent was to actually bubble the error up through the ssl object.

Full function:

static void
_PySSL_keylog_callback(const SSL *ssl, const char *line)
{
    PyGILState_STATE threadstate;
    PySSLSocket *ssl_obj = NULL;  /* ssl._SSLSocket, borrowed ref */
    int res, e;
    static PyThread_type_lock *lock = NULL;
    threadstate = PyGILState_Ensure();
    /* Allocate a static lock to synchronize writes to keylog file.
     * The lock is neither released on exit nor on fork(). The lock is
     * also shared between all SSLContexts although contexts may write to
     * their own files. IMHO that's good enough for a non-performance
     * critical debug helper.
     */
    if (lock == NULL) {
        lock = PyThread_allocate_lock();
        if (lock == NULL) {
            PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
            PyErr_Fetch(&ssl_obj->exc_type, &ssl_obj->exc_value,
                        &ssl_obj->exc_tb);
            return;
        }
    }

    ssl_obj = (PySSLSocket *)SSL_get_app_data(ssl);
    assert(PySSLSocket_Check(ssl_obj));
    if (ssl_obj->ctx->keylog_bio == NULL) {
        return;
    }

    PySSL_BEGIN_ALLOW_THREADS
    PyThread_acquire_lock(lock, 1);
    res = BIO_printf(ssl_obj->ctx->keylog_bio, "%s\n", line);
    e = errno;
    (void)BIO_flush(ssl_obj->ctx->keylog_bio);
    PyThread_release_lock(lock);
    PySSL_END_ALLOW_THREADS

    if (res == -1) {
        errno = e;
        PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
                                             ssl_obj->ctx->keylog_filename);
        PyErr_Fetch(&ssl_obj->exc_type, &ssl_obj->exc_value, &ssl_obj->exc_tb);
    }
    PyGILState_Release(threadstate);
}

@ariccio ariccio mannequin added the 3.9 only security fixes label Mar 26, 2020
@ariccio ariccio mannequin assigned tiran Mar 26, 2020
@ariccio ariccio mannequin added topic-SSL 3.9 only security fixes labels Mar 26, 2020
@ariccio ariccio mannequin assigned tiran Mar 26, 2020
@ariccio ariccio mannequin added the topic-SSL label Mar 26, 2020
@tiran
Copy link
Member

tiran commented Apr 17, 2021

The issue has been fixed by fbf94af in on 2020-Jun-21.

@tiran tiran closed this as completed Apr 17, 2021
@tiran tiran closed this as completed Apr 17, 2021
@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 topic-SSL
Projects
None yet
Development

No branches or pull requests

1 participant