summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>2019-08-05 00:41:49 +1200
committerAndreas Schneider <asn@cryptomilk.org>2022-08-26 07:59:32 +0000
commitb7b4d6da5fa81635e71c5e5e84dbdd13e7915b4b (patch)
tree7aef236d840896bb8530352653492742dafaec7f /python
parent333e1efa27f1d99bbfc69d94d3bf47e7b99c1e40 (diff)
downloadsamba-b7b4d6da5fa81635e71c5e5e84dbdd13e7915b4b.tar.gz
samba-b7b4d6da5fa81635e71c5e5e84dbdd13e7915b4b.tar.bz2
samba-b7b4d6da5fa81635e71c5e5e84dbdd13e7915b4b.zip
pyglue: generate_random_bytes/str accept positive numbers only
We aren't yet able to generate negative numbers of random bytes. Instead a request for -n bytes is implicitly converted into one for SIZE_MAX - n bytes, which is typically very large. Memory exhaustion seems a likely outcome. With this patch callers will see a ValueError. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andreas Schneider <asn@samba.org>
Diffstat (limited to 'python')
-rw-r--r--python/pyglue.c20
1 files changed, 16 insertions, 4 deletions
diff --git a/python/pyglue.c b/python/pyglue.c
index 5ee2b68b8ad..969b35145de 100644
--- a/python/pyglue.c
+++ b/python/pyglue.c
@@ -37,9 +37,15 @@ static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
int len;
PyObject *ret;
char *retstr;
- if (!PyArg_ParseTuple(args, "i", &len))
+ if (!PyArg_ParseTuple(args, "i", &len)) {
return NULL;
-
+ }
+ if (len < 0) {
+ PyErr_Format(PyExc_ValueError,
+ "random string length should be positive, not %d",
+ len);
+ return NULL;
+ }
retstr = generate_random_str(NULL, len);
ret = PyUnicode_FromString(retstr);
talloc_free(retstr);
@@ -97,9 +103,15 @@ static PyObject *py_generate_random_bytes(PyObject *self, PyObject *args)
PyObject *ret;
uint8_t *bytes = NULL;
- if (!PyArg_ParseTuple(args, "i", &len))
+ if (!PyArg_ParseTuple(args, "i", &len)) {
return NULL;
-
+ }
+ if (len < 0) {
+ PyErr_Format(PyExc_ValueError,
+ "random bytes length should be positive, not %d",
+ len);
+ return NULL;
+ }
bytes = talloc_zero_size(NULL, len);
if (bytes == NULL) {
PyErr_NoMemory();