From 8210ab33c192fa21420e5873c5557dacd3fc072f Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Tue, 26 Jul 2022 09:41:49 +0300 Subject: [PATCH 1/5] gh-95041: fail syslog.syslog in case inner call to syslog.openlog fails --- Modules/syslogmodule.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index 27b176aeb6dba7..478efd448c0eb9 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -191,8 +191,11 @@ syslog_syslog(PyObject * self, PyObject * args) */ if ((openargs = PyTuple_New(0))) { PyObject *openlog_ret = syslog_openlog(self, openargs, NULL); - Py_XDECREF(openlog_ret); Py_DECREF(openargs); + Py_XDECREF(openlog_ret); + if (openlog_ret == NULL) { + return NULL; + } } } From c95b55c1ba73310972845b3f304f4f8e256d2ae6 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Tue, 26 Jul 2022 10:10:44 +0300 Subject: [PATCH 2/5] return null if `PyTuple_New()` fails on syslog.syslog --- Modules/syslogmodule.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index 478efd448c0eb9..59f5a59d88ae17 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -196,6 +196,8 @@ syslog_syslog(PyObject * self, PyObject * args) if (openlog_ret == NULL) { return NULL; } + } else { + return NULL; } } From 98133481dcd2c9da6739fedcea00ca173b6621aa Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Tue, 26 Jul 2022 10:11:22 +0300 Subject: [PATCH 3/5] avoid using a pointer to freed memory on syslog.syslog --- Modules/syslogmodule.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index 59f5a59d88ae17..804fbf2cfdd376 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -192,8 +192,9 @@ syslog_syslog(PyObject * self, PyObject * args) if ((openargs = PyTuple_New(0))) { PyObject *openlog_ret = syslog_openlog(self, openargs, NULL); Py_DECREF(openargs); - Py_XDECREF(openlog_ret); - if (openlog_ret == NULL) { + if (openlog_ret != NULL) { + Py_DECREF(openlog_ret); + } else { return NULL; } } else { From b507a03c63064885f3c1d3e447577f3788595492 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Tue, 26 Jul 2022 10:36:46 +0300 Subject: [PATCH 4/5] syslog.syslog: nicer error handling --- Modules/syslogmodule.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index 804fbf2cfdd376..e9d182d859eff7 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -192,11 +192,10 @@ syslog_syslog(PyObject * self, PyObject * args) if ((openargs = PyTuple_New(0))) { PyObject *openlog_ret = syslog_openlog(self, openargs, NULL); Py_DECREF(openargs); - if (openlog_ret != NULL) { - Py_DECREF(openlog_ret); - } else { + if (openlog_ret == NULL) { return NULL; } + Py_DECREF(openlog_ret); } else { return NULL; } From 5a097a6d62282cc96a1a4687453bc8ca1305d9e1 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Tue, 26 Jul 2022 10:57:30 +0300 Subject: [PATCH 5/5] Update Modules/syslogmodule.c Co-authored-by: Erlend Egeberg Aasland --- Modules/syslogmodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index e9d182d859eff7..c409fe968f8894 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -196,7 +196,8 @@ syslog_syslog(PyObject * self, PyObject * args) return NULL; } Py_DECREF(openlog_ret); - } else { + } + else { return NULL; } }