[PATCH 2/6 v7] libsemanage: Add overflow checks
James Carter <[email protected]>
| Newsgroups | org.kernel.vger.selinux |
|---|---|
| Message-ID | <[email protected]> |
Using "*= 2" is a common idiom to grow a buffer, but it is possible for this multiplication to overflow. Use the __builtin_mul_overflow() family of functions that will do the multiplication while checking for overflow. Acked-by: Stephen Smalley <[email protected]> Signed-off-by: James Carter <[email protected]> --- v7: No changes libsemanage/src/direct_api.c | 7 +++++-- libsemanage/src/genhomedircon.c | 11 +++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/libsemanage/src/direct_api.c b/libsemanage/src/direct_api.c index 9572dbf0..b87b87e7 100644 --- a/libsemanage/src/direct_api.c +++ b/libsemanage/src/direct_api.c @@ -588,8 +588,11 @@ static int read_from_pipe_to_data(semanage_handle_t *sh, size_t initial_len, data_read_len += read_len; if (data_read_len == max_len) { char *tmp; - - max_len *= 2; + if (__builtin_mul_overflow(max_len, 2, &max_len)) { + ERR(sh, "Overflow"); + free(data_read); + return -1; + } tmp = realloc(data_read, max_len); if (tmp == NULL) { ERR(sh, "Failed to realloc, out of memory."); diff --git a/libsemanage/src/genhomedircon.c b/libsemanage/src/genhomedircon.c index ce7ea167..f04fc127 100644 --- a/libsemanage/src/genhomedircon.c +++ b/libsemanage/src/genhomedircon.c @@ -1090,9 +1090,9 @@ retry: goto cleanup; retval = getpwnam_r(name, &pwstorage, rbuf, rbuflen, &pwent); - if (retval == ERANGE && rbuflen < LONG_MAX / 2) { + if (retval == ERANGE && + !__builtin_smull_overflow(rbuflen, 2, &rbuflen)) { free(rbuf); - rbuflen *= 2; goto retry; } if (retval != 0 || pwent == NULL) { @@ -1171,11 +1171,10 @@ static int get_group_users(genhomedircon_settings_t *s, &group)) != 0 && errno == ERANGE) { char *new_grbuf; - grbuflen *= 2; - if (grbuflen < 0) - /* the member list could exceed 2Gb on a system with a 32-bit CPU (where - * sizeof(long) = 4) - if this ever happened, the loop would become infinite. */ + if (__builtin_smull_overflow(grbuflen, 2, &grbuflen)) { + ERR(s->h_semanage, "Overflow"); goto cleanup; + } new_grbuf = realloc(grbuf, grbuflen); if (new_grbuf == NULL) goto cleanup; -- 2.55.0