Skip to content

Commit

Permalink
libselinux: introduce reallocarray(3)
Browse files Browse the repository at this point in the history
Introduce reallocarray(3), a realloc(3) wrapper incorporating a
multiplication overflow check.

Add private implementation in case the function is not provided by the
standard C library.

Use in appropriate locations.

Signed-off-by: Christian Göttsche <[email protected]>
Acked-by: James Carter <[email protected]>
  • Loading branch information
cgzones authored and jwcart2 committed Nov 7, 2023
1 parent 3dad44a commit cb8289c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
6 changes: 6 additions & 0 deletions libselinux/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char*d,*s;strlc
override CFLAGS += -DHAVE_STRLCPY
endif

# check for reallocarray(3) availability
H := \#
ifeq (yes,$(shell printf '${H}include <stdlib.h>\nint main(void){reallocarray(NULL, 0, 0);return 0;}' | $(CC) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
override CFLAGS += -DHAVE_REALLOCARRAY
endif

SWIG_CFLAGS += -Wno-error -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-parameter \
-Wno-shadow -Wno-uninitialized -Wno-missing-prototypes -Wno-missing-declarations \
-Wno-deprecated-declarations
Expand Down
2 changes: 1 addition & 1 deletion libselinux/src/get_context_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ static int get_context_user(FILE * fp,
continue;
}
if (security_check_context(usercon_str2) == 0) {
new_reachable = realloc(*reachable, (*nreachable + 2) * sizeof(char *));
new_reachable = reallocarray(*reachable, *nreachable + 2, sizeof(char *));
if (!new_reachable) {
context_free(usercon);
rc = -1;
Expand Down
4 changes: 2 additions & 2 deletions libselinux/src/matchpathcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ static int add_array_elt(char *con)
if (con_array_size) {
while (con_array_used >= con_array_size) {
con_array_size *= 2;
tmp = (char **)realloc(con_array, sizeof(char*) *
con_array_size);
tmp = (char **)reallocarray(con_array, con_array_size,
sizeof(char*));
if (!tmp) {
free_array_elts();
return -1;
Expand Down
14 changes: 14 additions & 0 deletions libselinux/src/selinux_internal.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "selinux_internal.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>


Expand All @@ -16,3 +18,15 @@ size_t strlcpy(char *dest, const char *src, size_t size)
return ret;
}
#endif /* HAVE_STRLCPY */

#ifndef HAVE_REALLOCARRAY
void *reallocarray(void *ptr, size_t nmemb, size_t size)
{
if (size && nmemb > SIZE_MAX / size) {
errno = ENOMEM;
return NULL;
}

return realloc(ptr, nmemb * size);
}
#endif /* HAVE_REALLOCARRAY */
4 changes: 4 additions & 0 deletions libselinux/src/selinux_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,8 @@ extern int has_selinux_config ;
size_t strlcpy(char *dest, const char *src, size_t size);
#endif

#ifndef HAVE_REALLOCARRAY
void *reallocarray(void *ptr, size_t nmemb, size_t size);
#endif

#endif /* SELINUX_INTERNAL_H_ */
3 changes: 1 addition & 2 deletions libselinux/src/selinux_restorecon.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ static int add_exclude(const char *directory, bool who)
return -1;
}

tmp_list = realloc(exclude_lst,
sizeof(struct edir) * (exclude_count + 1));
tmp_list = reallocarray(exclude_lst, exclude_count + 1, sizeof(struct edir));
if (!tmp_list)
goto oom;

Expand Down

0 comments on commit cb8289c

Please sign in to comment.