From 5029a85267865f39195d057cffa977c39e8a1268 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Thu, 12 Apr 2018 14:46:59 +0100 Subject: [PATCH 1/7] lib/ldb: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power --- lib/ldb/pyldb.c | 10 +++++----- lib/ldb/pyldb_util.c | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ldb/pyldb.c b/lib/ldb/pyldb.c index 67ecafbc420..110ec8e60ad 100644 --- a/lib/ldb/pyldb.c +++ b/lib/ldb/pyldb.c @@ -1078,7 +1078,7 @@ static const char **PyList_AsStrList(TALLOC_CTX *mem_ctx, PyObject *list, const char *str = NULL; Py_ssize_t size; PyObject *item = PyList_GetItem(list, i); - if (!PyStr_Check(item)) { + if (!(PyStr_Check(item) || PyUnicode_Check(item))) { PyErr_Format(PyExc_TypeError, "%s should be strings", paramname); talloc_free(ret); return NULL; @@ -2861,7 +2861,7 @@ static struct ldb_message_element *PyObject_AsMessageElement( me->name = talloc_strdup(me, attr_name); me->flags = flags; - if (PyBytes_Check(set_obj) || PyStr_Check(set_obj)) { + if (PyBytes_Check(set_obj) || PyUnicode_Check(set_obj)) { me->num_values = 1; me->values = talloc_array(me, struct ldb_val, me->num_values); if (PyBytes_Check(set_obj)) { @@ -2897,7 +2897,7 @@ static struct ldb_message_element *PyObject_AsMessageElement( return NULL; } msg = _msg; - } else if (PyStr_Check(obj)) { + } else if (PyUnicode_Check(obj)) { msg = PyStr_AsUTF8AndSize(obj, &size); if (msg == NULL) { talloc_free(me); @@ -3069,7 +3069,7 @@ static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyOb if (py_elements != NULL) { Py_ssize_t i; - if (PyBytes_Check(py_elements) || PyStr_Check(py_elements)) { + if (PyBytes_Check(py_elements) || PyUnicode_Check(py_elements)) { char *_msg = NULL; el->num_values = 1; el->values = talloc_array(el, struct ldb_val, 1); @@ -3110,7 +3110,7 @@ static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyOb char *_msg = NULL; result = PyBytes_AsStringAndSize(item, &_msg, &size); msg = _msg; - } else if (PyStr_Check(item)) { + } else if (PyUnicode_Check(item)) { msg = PyStr_AsUTF8AndSize(item, &size); result = (msg == NULL) ? -1 : 0; } else { diff --git a/lib/ldb/pyldb_util.c b/lib/ldb/pyldb_util.c index 3bda1dbc2a8..46ee4034122 100644 --- a/lib/ldb/pyldb_util.c +++ b/lib/ldb/pyldb_util.c @@ -70,7 +70,7 @@ bool pyldb_Object_AsDn(TALLOC_CTX *mem_ctx, PyObject *object, struct ldb_dn *odn; PyTypeObject *PyLdb_Dn_Type; - if (ldb_ctx != NULL && PyStr_Check(object)) { + if (ldb_ctx != NULL && (PyStr_Check(object) || PyUnicode_Check(object))) { odn = ldb_dn_new(mem_ctx, ldb_ctx, PyStr_AsUTF8(object)); *dn = odn; return true; From 494edcb9b3a3626439ebe5025da2c61ffd70fa56 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 13 Apr 2018 17:17:20 +0100 Subject: [PATCH 2/7] lib/tevent: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power --- lib/tevent/pytevent.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tevent/pytevent.c b/lib/tevent/pytevent.c index 10d8a22a8cf..369ec6e02c8 100644 --- a/lib/tevent/pytevent.c +++ b/lib/tevent/pytevent.c @@ -188,7 +188,7 @@ static PyObject *py_register_backend(PyObject *self, PyObject *args) return NULL; } - if (!PyStr_Check(name)) { + if (!(PyStr_Check(name) || PyUnicode_Check(name))) { PyErr_SetNone(PyExc_TypeError); Py_DECREF(name); return NULL; From eb07436ebcb12fab32b8b30c66c69a124b3f9df6 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 13 Apr 2018 17:32:15 +0100 Subject: [PATCH 3/7] libcli/nbt: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power --- libcli/nbt/pynbt.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libcli/nbt/pynbt.c b/libcli/nbt/pynbt.c index 254c98a4b89..032561a4bd8 100644 --- a/libcli/nbt/pynbt.c +++ b/libcli/nbt/pynbt.c @@ -57,7 +57,7 @@ static PyObject *py_nbt_node_init(PyTypeObject *self, PyObject *args, PyObject * static bool PyObject_AsDestinationTuple(PyObject *obj, const char **dest_addr, uint16_t *dest_port) { - if (PyStr_Check(obj)) { + if (PyStr_Check(obj) || PyUnicode_Check(obj)) { *dest_addr = PyStr_AsString(obj); *dest_port = NBT_NAME_SERVICE_PORT; return true; @@ -69,7 +69,7 @@ static bool PyObject_AsDestinationTuple(PyObject *obj, const char **dest_addr, u return false; } - if (!PyStr_Check(PyTuple_GetItem(obj, 0))) { + if (!(PyStr_Check(PyTuple_GetItem(obj, 0)) || PyUnicode_Check(PyTuple_GetItem(obj, 0)))) { PyErr_SetString(PyExc_TypeError, "Destination tuple first element not string"); return false; } @@ -111,7 +111,7 @@ static bool PyObject_AsNBTName(PyObject *obj, struct nbt_name_socket *name_socke } } - if (PyStr_Check(obj)) { + if (PyStr_Check(obj) || PyUnicode_Check(obj)) { /* FIXME: Parse string to be able to interpret things like RHONWYN<02> ? */ name->name = PyStr_AsString(obj); name->scope = NULL; From 121de8372c25342baf84951264d0ccafa376e884 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 13 Apr 2018 17:33:10 +0100 Subject: [PATCH 4/7] s4/auth: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power --- source4/auth/pyauth.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source4/auth/pyauth.c b/source4/auth/pyauth.c index 4cb12f882bc..fc22637564d 100644 --- a/source4/auth/pyauth.c +++ b/source4/auth/pyauth.c @@ -184,7 +184,7 @@ static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list, const char *value; Py_ssize_t size; PyObject *item = PyList_GetItem(list, i); - if (!PyStr_Check(item)) { + if (!(PyStr_Check(item) || PyUnicode_Check(item))) { PyErr_Format(PyExc_TypeError, "%s should be strings", paramname); return NULL; } From 503fb0bcd26b2d167cfca5132eaa80532fa53664 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 13 Apr 2018 17:33:47 +0100 Subject: [PATCH 5/7] s4/dsdb: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power --- source4/dsdb/pydsdb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source4/dsdb/pydsdb.c b/source4/dsdb/pydsdb.c index 8d84a16dd18..d9177bbd1d7 100644 --- a/source4/dsdb/pydsdb.c +++ b/source4/dsdb/pydsdb.c @@ -580,7 +580,7 @@ static PyObject *py_dsdb_DsReplicaAttribute(PyObject *self, PyObject *args) for (i = 0; i < el->num_values; i++) { PyObject *item = PyList_GetItem(el_list, i); - if (!PyStr_Check(item)) { + if (!(PyStr_Check(item) || PyUnicode_Check(item))) { PyErr_Format(PyExc_TypeError, "ldif_elements should be strings"); talloc_free(tmp_ctx); return NULL; From 6d3e96deb6c15c5f51049e10b708a1da701ea752 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 13 Apr 2018 17:34:19 +0100 Subject: [PATCH 6/7] s4/librpc: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power --- source4/librpc/rpc/pyrpc.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/source4/librpc/rpc/pyrpc.c b/source4/librpc/rpc/pyrpc.c index 8b817b8b46d..e86ea0e94aa 100644 --- a/source4/librpc/rpc/pyrpc.c +++ b/source4/librpc/rpc/pyrpc.c @@ -50,28 +50,32 @@ static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *sy { ZERO_STRUCTP(syntax_id); - if (PyStr_Check(object)) { + if (PyStr_Check(object) || PyUnicode_Check(object)) { return PyString_AsGUID(object, &syntax_id->uuid); } else if (PyTuple_Check(object)) { + PyObject *item = NULL; if (PyTuple_Size(object) < 1 || PyTuple_Size(object) > 2) { PyErr_SetString(PyExc_ValueError, "Syntax ID tuple has invalid size"); return false; } - if (!PyStr_Check(PyTuple_GetItem(object, 0))) { + item = PyTuple_GetItem(object, 0); + if (!(PyStr_Check(item) || PyUnicode_Check(item))) { PyErr_SetString(PyExc_ValueError, "Expected GUID as first element in tuple"); return false; } - if (!PyString_AsGUID(PyTuple_GetItem(object, 0), &syntax_id->uuid)) + if (!PyString_AsGUID(item, &syntax_id->uuid)) { return false; + } - if (!PyInt_Check(PyTuple_GetItem(object, 1))) { + item = PyTuple_GetItem(object, 1); + if (!PyInt_Check(item)) { PyErr_SetString(PyExc_ValueError, "Expected version as second element in tuple"); return false; } - syntax_id->if_version = PyInt_AsLong(PyTuple_GetItem(object, 1)); + syntax_id->if_version = PyInt_AsLong(item); return true; } From 74aaa7125fa2cc2102957b81687fd8b8e5da6085 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 13 Apr 2018 17:34:40 +0100 Subject: [PATCH 7/7] s4/param: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power --- source4/param/pyparam.c | 2 +- source4/param/pyparam_util.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source4/param/pyparam.c b/source4/param/pyparam.c index 18d7017b9c6..e7e908fcac3 100644 --- a/source4/param/pyparam.c +++ b/source4/param/pyparam.c @@ -456,7 +456,7 @@ static Py_ssize_t py_lp_ctx_len(PyObject *self) static PyObject *py_lp_ctx_getitem(PyObject *self, PyObject *name) { struct loadparm_service *service; - if (!PyStr_Check(name)) { + if (!(PyStr_Check(name) || PyUnicode_Check(name))) { PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported"); return NULL; } diff --git a/source4/param/pyparam_util.c b/source4/param/pyparam_util.c index 512a8b1cdb7..917caf809e5 100644 --- a/source4/param/pyparam_util.c +++ b/source4/param/pyparam_util.c @@ -34,7 +34,7 @@ _PUBLIC_ struct loadparm_context *lpcfg_from_py_object(TALLOC_CTX *mem_ctx, PyOb PyTypeObject *lp_type; bool is_lpobj; - if (PyStr_Check(py_obj)) { + if (PyStr_Check(py_obj) || PyUnicode_Check(py_obj)) { lp_ctx = loadparm_init_global(false); if (lp_ctx == NULL) { return NULL;