Skip to content

Commit 9960ee2

Browse files
committed
handle NULL values returned by HACL* functions
- Handle NULL returned by allocation functions. - Handle NULL returned by copy functions. - Suppress unused impossible return codes.
1 parent 9558d22 commit 9960ee2

File tree

5 files changed

+296
-139
lines changed

5 files changed

+296
-139
lines changed

Modules/blake2module.c

Lines changed: 101 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -395,26 +395,33 @@ new_Blake2Object(PyTypeObject *type)
395395
* 64 bits so we loop in <4gig chunks when needed. */
396396

397397
#if PY_SSIZE_T_MAX > UINT32_MAX
398-
#define HACL_UPDATE_LOOP(update,state,buf,len) \
399-
while (len > UINT32_MAX) { \
400-
update(state, buf, UINT32_MAX); \
401-
len -= UINT32_MAX; \
402-
buf += UINT32_MAX; \
403-
}
398+
# define HACL_UPDATE_LOOP(UPDATE_FUNC, STATE, BUF, LEN) \
399+
do { \
400+
while (LEN > UINT32_MAX) { \
401+
(void)UPDATE_FUNC(STATE, BUF, UINT32_MAX); \
402+
LEN -= UINT32_MAX; \
403+
BUF += UINT32_MAX; \
404+
} \
405+
} while (0)
404406
#else
405-
#define HACL_UPDATE_LOOP(update,state,buf,len)
407+
# define HACL_UPDATE_LOOP(...)
406408
#endif
407409

408-
#define HACL_UPDATE(update,state,buf,len) do { \
409-
/* Note: we explicitly ignore the error code on the basis that it would take >
410-
* 1 billion years to overflow the maximum admissible length for SHA2-256
411-
* (namely, 2^61-1 bytes). */ \
412-
HACL_UPDATE_LOOP(update,state,buf,len) \
413-
/* Cast to uint32_t is safe: len <= UINT32_MAX at this point. */ \
414-
update(state, buf, (uint32_t) len); \
415-
} while (0)
410+
/*
411+
* Note: we explicitly ignore the error code on the basis that it would take
412+
* more than 1 billion years to overflow the maximum admissible length for
413+
* blake2b/2s (2^64 - 1).
414+
*/
415+
#define HACL_UPDATE(UPDATE_FUNC, STATE, BUF, LEN) \
416+
do { \
417+
HACL_UPDATE_LOOP(UPDATE_FUNC, STATE, BUF, LEN); \
418+
assert((Py_ssize_t)(LEN) <= (Py_ssize_t)UINT32_MAX); \
419+
(void)UPDATE_FUNC(STATE, BUF, (uint32_t)LEN); \
420+
} while (0)
416421

417-
static void update(Blake2Object *self, uint8_t *buf, Py_ssize_t len) {
422+
static void
423+
update(Blake2Object *self, uint8_t *buf, Py_ssize_t len)
424+
{
418425
switch (self->impl) {
419426
// These need to be ifdef'd out otherwise it's an unresolved symbol at
420427
// link-time.
@@ -583,21 +590,41 @@ py_blake2b_or_s_new(PyTypeObject *type, PyObject *data, int digest_size,
583590

584591
switch (self->impl) {
585592
#if HACL_CAN_COMPILE_SIMD256
586-
case Blake2b_256:
593+
case Blake2b_256: {
587594
self->blake2b_256_state = Hacl_Hash_Blake2b_Simd256_malloc_with_params_and_key(&params, last_node, key->buf);
595+
if (self->blake2b_256_state == NULL) {
596+
(void)PyErr_NoMemory();
597+
goto error;
598+
}
588599
break;
600+
}
589601
#endif
590602
#if HACL_CAN_COMPILE_SIMD128
591-
case Blake2s_128:
603+
case Blake2s_128: {
592604
self->blake2s_128_state = Hacl_Hash_Blake2s_Simd128_malloc_with_params_and_key(&params, last_node, key->buf);
605+
if (self->blake2s_128_state == NULL) {
606+
(void)PyErr_NoMemory();
607+
goto error;
608+
}
593609
break;
610+
}
594611
#endif
595-
case Blake2b:
612+
case Blake2b: {
596613
self->blake2b_state = Hacl_Hash_Blake2b_malloc_with_params_and_key(&params, last_node, key->buf);
614+
if (self->blake2b_state == NULL) {
615+
(void)PyErr_NoMemory();
616+
goto error;
617+
}
597618
break;
598-
case Blake2s:
619+
}
620+
case Blake2s: {
599621
self->blake2s_state = Hacl_Hash_Blake2s_malloc_with_params_and_key(&params, last_node, key->buf);
622+
if (self->blake2s_state == NULL) {
623+
(void)PyErr_NoMemory();
624+
goto error;
625+
}
600626
break;
627+
}
601628
default:
602629
Py_UNREACHABLE();
603630
}
@@ -610,7 +637,8 @@ py_blake2b_or_s_new(PyTypeObject *type, PyObject *data, int digest_size,
610637
Py_BEGIN_ALLOW_THREADS
611638
update(self, buf.buf, buf.len);
612639
Py_END_ALLOW_THREADS
613-
} else {
640+
}
641+
else {
614642
update(self, buf.buf, buf.len);
615643
}
616644
PyBuffer_Release(&buf);
@@ -688,44 +716,77 @@ py_blake2s_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
688716
return py_blake2b_or_s_new(type, data, digest_size, key, salt, person, fanout, depth, leaf_size, node_offset, node_depth, inner_size, last_node, usedforsecurity);
689717
}
690718

691-
/*[clinic input]
692-
_blake2.blake2b.copy
693-
694-
Return a copy of the hash object.
695-
[clinic start generated code]*/
696-
697-
static PyObject *
698-
_blake2_blake2b_copy_impl(Blake2Object *self)
699-
/*[clinic end generated code: output=622d1c56b91c50d8 input=e383c2d199fd8a2e]*/
719+
static int
720+
blake2_blake2b_copy_locked(Blake2Object *self, Blake2Object *cpy)
700721
{
701-
Blake2Object *cpy;
702-
703-
if ((cpy = new_Blake2Object(Py_TYPE(self))) == NULL)
704-
return NULL;
705-
706-
ENTER_HASHLIB(self);
707722
switch (self->impl) {
708723
#if HACL_CAN_COMPILE_SIMD256
709-
case Blake2b_256:
724+
case Blake2b_256: {
710725
cpy->blake2b_256_state = Hacl_Hash_Blake2b_Simd256_copy(self->blake2b_256_state);
726+
if (cpy->blake2b_256_state == NULL) {
727+
goto error;
728+
}
711729
break;
730+
}
712731
#endif
713732
#if HACL_CAN_COMPILE_SIMD128
714-
case Blake2s_128:
733+
case Blake2s_128: {
715734
cpy->blake2s_128_state = Hacl_Hash_Blake2s_Simd128_copy(self->blake2s_128_state);
735+
if (cpy->blake2s_128_state == NULL) {
736+
goto error;
737+
}
716738
break;
739+
}
717740
#endif
718-
case Blake2b:
741+
case Blake2b: {
719742
cpy->blake2b_state = Hacl_Hash_Blake2b_copy(self->blake2b_state);
743+
if (cpy->blake2b_state == NULL) {
744+
goto error;
745+
}
720746
break;
721-
case Blake2s:
747+
}
748+
case Blake2s: {
722749
cpy->blake2s_state = Hacl_Hash_Blake2s_copy(self->blake2s_state);
750+
if (cpy->blake2s_state == NULL) {
751+
goto error;
752+
}
723753
break;
754+
}
724755
default:
725756
Py_UNREACHABLE();
726757
}
727758
cpy->impl = self->impl;
759+
return 0;
760+
761+
error:
762+
(void)PyErr_NoMemory();
763+
return -1;
764+
}
765+
766+
/*[clinic input]
767+
_blake2.blake2b.copy
768+
769+
Return a copy of the hash object.
770+
[clinic start generated code]*/
771+
772+
static PyObject *
773+
_blake2_blake2b_copy_impl(Blake2Object *self)
774+
/*[clinic end generated code: output=622d1c56b91c50d8 input=e383c2d199fd8a2e]*/
775+
{
776+
Blake2Object *cpy;
777+
778+
if ((cpy = new_Blake2Object(Py_TYPE(self))) == NULL) {
779+
return NULL;
780+
}
781+
782+
int rc;
783+
ENTER_HASHLIB(self);
784+
rc = blake2_blake2b_copy_locked(self, cpy);
728785
LEAVE_HASHLIB(self);
786+
if (rc < 0) {
787+
Py_DECREF(cpy);
788+
return NULL;
789+
}
729790
return (PyObject *)cpy;
730791
}
731792

Modules/md5module.c

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,17 @@ MD5Type_copy_impl(MD5object *self, PyTypeObject *cls)
121121
MD5State *st = PyType_GetModuleState(cls);
122122

123123
MD5object *newobj;
124-
if ((newobj = newMD5object(st))==NULL)
124+
if ((newobj = newMD5object(st)) == NULL) {
125125
return NULL;
126+
}
126127

127128
ENTER_HASHLIB(self);
128129
newobj->hash_state = Hacl_Hash_MD5_copy(self->hash_state);
129130
LEAVE_HASHLIB(self);
131+
if (newobj->hash_state == NULL) {
132+
Py_DECREF(self);
133+
return PyErr_NoMemory();
134+
}
130135
return (PyObject *)newobj;
131136
}
132137

@@ -173,15 +178,23 @@ MD5Type_hexdigest_impl(MD5object *self)
173178
return PyUnicode_FromStringAndSize(digest_hex, sizeof(digest_hex));
174179
}
175180

176-
static void update(Hacl_Hash_MD5_state_t *state, uint8_t *buf, Py_ssize_t len) {
181+
static void
182+
update(Hacl_Hash_MD5_state_t *state, uint8_t *buf, Py_ssize_t len)
183+
{
184+
/*
185+
* Note: we explicitly ignore the error code on the basis that it would
186+
* take more than 1 billion years to overflow the maximum admissible length
187+
* for MD5 (2^61 - 1).
188+
*/
177189
#if PY_SSIZE_T_MAX > UINT32_MAX
178-
while (len > UINT32_MAX) {
179-
Hacl_Hash_MD5_update(state, buf, UINT32_MAX);
180-
len -= UINT32_MAX;
181-
buf += UINT32_MAX;
182-
}
190+
while (len > UINT32_MAX) {
191+
(void)Hacl_Hash_MD5_update(state, buf, UINT32_MAX);
192+
len -= UINT32_MAX;
193+
buf += UINT32_MAX;
194+
}
183195
#endif
184-
Hacl_Hash_MD5_update(state, buf, (uint32_t) len);
196+
assert(len <= (Py_ssize_t)UINT32_MAX);
197+
(void)Hacl_Hash_MD5_update(state, buf, (uint32_t)len);
185198
}
186199

187200
/*[clinic input]
@@ -286,32 +299,36 @@ _md5_md5_impl(PyObject *module, PyObject *string, int usedforsecurity)
286299
MD5object *new;
287300
Py_buffer buf;
288301

289-
if (string)
302+
if (string) {
290303
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
304+
}
291305

292306
MD5State *st = md5_get_state(module);
293307
if ((new = newMD5object(st)) == NULL) {
294-
if (string)
308+
if (string) {
295309
PyBuffer_Release(&buf);
310+
}
296311
return NULL;
297312
}
298313

299314
new->hash_state = Hacl_Hash_MD5_malloc();
300-
301-
if (PyErr_Occurred()) {
315+
if (new->hash_state == NULL) {
302316
Py_DECREF(new);
303-
if (string)
317+
if (string) {
304318
PyBuffer_Release(&buf);
305-
return NULL;
319+
}
320+
return PyErr_NoMemory();
306321
}
322+
307323
if (string) {
308324
if (buf.len >= HASHLIB_GIL_MINSIZE) {
309325
/* We do not initialize self->lock here as this is the constructor
310326
* where it is not yet possible to have concurrent access. */
311327
Py_BEGIN_ALLOW_THREADS
312328
update(new->hash_state, buf.buf, buf.len);
313329
Py_END_ALLOW_THREADS
314-
} else {
330+
}
331+
else {
315332
update(new->hash_state, buf.buf, buf.len);
316333
}
317334
PyBuffer_Release(&buf);

Modules/sha1module.c

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,17 @@ SHA1Type_copy_impl(SHA1object *self, PyTypeObject *cls)
123123
SHA1State *st = _PyType_GetModuleState(cls);
124124

125125
SHA1object *newobj;
126-
if ((newobj = newSHA1object(st)) == NULL)
126+
if ((newobj = newSHA1object(st)) == NULL) {
127127
return NULL;
128+
}
128129

129130
ENTER_HASHLIB(self);
130131
newobj->hash_state = Hacl_Hash_SHA1_copy(self->hash_state);
131132
LEAVE_HASHLIB(self);
133+
if (newobj->hash_state == NULL) {
134+
Py_DECREF(newobj);
135+
return PyErr_NoMemory();
136+
}
132137
return (PyObject *)newobj;
133138
}
134139

@@ -166,15 +171,23 @@ SHA1Type_hexdigest_impl(SHA1object *self)
166171
return _Py_strhex((const char *)digest, SHA1_DIGESTSIZE);
167172
}
168173

169-
static void update(Hacl_Hash_SHA1_state_t *state, uint8_t *buf, Py_ssize_t len) {
174+
static void
175+
update(Hacl_Hash_SHA1_state_t *state, uint8_t *buf, Py_ssize_t len)
176+
{
177+
/*
178+
* Note: we explicitly ignore the error code on the basis that it would
179+
* take more than 1 billion years to overflow the maximum admissible length
180+
* for SHA-1 (2^61 - 1).
181+
*/
170182
#if PY_SSIZE_T_MAX > UINT32_MAX
171-
while (len > UINT32_MAX) {
172-
Hacl_Hash_SHA1_update(state, buf, UINT32_MAX);
173-
len -= UINT32_MAX;
174-
buf += UINT32_MAX;
175-
}
183+
while (len > UINT32_MAX) {
184+
(void)Hacl_Hash_SHA1_update(state, buf, UINT32_MAX);
185+
len -= UINT32_MAX;
186+
buf += UINT32_MAX;
187+
}
176188
#endif
177-
Hacl_Hash_SHA1_update(state, buf, (uint32_t) len);
189+
assert(len <= (Py_ssize_t)UINT32_MAX);
190+
(void)Hacl_Hash_SHA1_update(state, buf, (uint32_t)len);
178191
}
179192

180193
/*[clinic input]
@@ -279,23 +292,26 @@ _sha1_sha1_impl(PyObject *module, PyObject *string, int usedforsecurity)
279292
SHA1object *new;
280293
Py_buffer buf;
281294

282-
if (string)
295+
if (string) {
283296
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
297+
}
284298

285299
SHA1State *st = sha1_get_state(module);
286300
if ((new = newSHA1object(st)) == NULL) {
287-
if (string)
301+
if (string) {
288302
PyBuffer_Release(&buf);
303+
}
289304
return NULL;
290305
}
291306

292307
new->hash_state = Hacl_Hash_SHA1_malloc();
293308

294-
if (PyErr_Occurred()) {
309+
if (new->hash_state == NULL) {
295310
Py_DECREF(new);
296-
if (string)
311+
if (string) {
297312
PyBuffer_Release(&buf);
298-
return NULL;
313+
}
314+
return PyErr_NoMemory();
299315
}
300316
if (string) {
301317
if (buf.len >= HASHLIB_GIL_MINSIZE) {
@@ -304,7 +320,8 @@ _sha1_sha1_impl(PyObject *module, PyObject *string, int usedforsecurity)
304320
Py_BEGIN_ALLOW_THREADS
305321
update(new->hash_state, buf.buf, buf.len);
306322
Py_END_ALLOW_THREADS
307-
} else {
323+
}
324+
else {
308325
update(new->hash_state, buf.buf, buf.len);
309326
}
310327
PyBuffer_Release(&buf);

0 commit comments

Comments
 (0)