Skip to content

Commit cb8289c

Browse files
cgzonesjwcart2
authored andcommitted
libselinux: introduce reallocarray(3)
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]>
1 parent 3dad44a commit cb8289c

File tree

6 files changed

+28
-5
lines changed

6 files changed

+28
-5
lines changed

libselinux/src/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char*d,*s;strlc
108108
override CFLAGS += -DHAVE_STRLCPY
109109
endif
110110

111+
# check for reallocarray(3) availability
112+
H := \#
113+
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))
114+
override CFLAGS += -DHAVE_REALLOCARRAY
115+
endif
116+
111117
SWIG_CFLAGS += -Wno-error -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-parameter \
112118
-Wno-shadow -Wno-uninitialized -Wno-missing-prototypes -Wno-missing-declarations \
113119
-Wno-deprecated-declarations

libselinux/src/get_context_list.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ static int get_context_user(FILE * fp,
272272
continue;
273273
}
274274
if (security_check_context(usercon_str2) == 0) {
275-
new_reachable = realloc(*reachable, (*nreachable + 2) * sizeof(char *));
275+
new_reachable = reallocarray(*reachable, *nreachable + 2, sizeof(char *));
276276
if (!new_reachable) {
277277
context_free(usercon);
278278
rc = -1;

libselinux/src/matchpathcon.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ static int add_array_elt(char *con)
9696
if (con_array_size) {
9797
while (con_array_used >= con_array_size) {
9898
con_array_size *= 2;
99-
tmp = (char **)realloc(con_array, sizeof(char*) *
100-
con_array_size);
99+
tmp = (char **)reallocarray(con_array, con_array_size,
100+
sizeof(char*));
101101
if (!tmp) {
102102
free_array_elts();
103103
return -1;

libselinux/src/selinux_internal.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "selinux_internal.h"
22

3+
#include <errno.h>
4+
#include <stdlib.h>
35
#include <string.h>
46

57

@@ -16,3 +18,15 @@ size_t strlcpy(char *dest, const char *src, size_t size)
1618
return ret;
1719
}
1820
#endif /* HAVE_STRLCPY */
21+
22+
#ifndef HAVE_REALLOCARRAY
23+
void *reallocarray(void *ptr, size_t nmemb, size_t size)
24+
{
25+
if (size && nmemb > SIZE_MAX / size) {
26+
errno = ENOMEM;
27+
return NULL;
28+
}
29+
30+
return realloc(ptr, nmemb * size);
31+
}
32+
#endif /* HAVE_REALLOCARRAY */

libselinux/src/selinux_internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,8 @@ extern int has_selinux_config ;
9898
size_t strlcpy(char *dest, const char *src, size_t size);
9999
#endif
100100

101+
#ifndef HAVE_REALLOCARRAY
102+
void *reallocarray(void *ptr, size_t nmemb, size_t size);
103+
#endif
104+
101105
#endif /* SELINUX_INTERNAL_H_ */

libselinux/src/selinux_restorecon.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ static int add_exclude(const char *directory, bool who)
175175
return -1;
176176
}
177177

178-
tmp_list = realloc(exclude_lst,
179-
sizeof(struct edir) * (exclude_count + 1));
178+
tmp_list = reallocarray(exclude_lst, exclude_count + 1, sizeof(struct edir));
180179
if (!tmp_list)
181180
goto oom;
182181

0 commit comments

Comments
 (0)