Skip to contentSkip to conversation

libselinux, libsepol: Add CFLAGS and LDFLAGS to Makefile checks

PATCH In review James Carter 3 participants 4 messages Mar 13, 2024 – Mar 20, 2024 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libselinux, libsepol: Add CFLAGS and LDFLAGS to Makefile checks

In libselinux there is an availability check for strlcpy() and in both libselinux and libsepol there are availability checks for reallocarray() in the src Makfiles. CFLAGS and LDFLAGS are needed for cross-compiling, but, unfortunately, the default CFLAGS cause all of these availability checks to fail to compile because of compilationerrors (rather than just the function not being available).

Add CFLAGS and LDFLAGS to the availibility checks, update the checks so that a compilation error will only happen if the function being checked for is not available, and make checks for the same function the same in both libselinux and libsepol.

---
 libselinux/src/Makefile | 4 ++--
 libsepol/src/Makefile   | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
libselinux/src/Makefile +2 -2
index d3b981fc..41cfbdca 100644
@@ -104,13 +104,13 @@ override CFLAGS += -I../include -D_GNU_SOURCE $(DISABLE_FLAGS) $(PCRE_CFLAGS)
 
 # check for strlcpy(3) availability
 H := \#
-ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char*d,*s;strlcpy(d, s, 0);return 0;}' | $(CC) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
+ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char d[2];const char *s="a";return (size_t)strlcpy(d,s,sizeof(d))>=sizeof(d);}' | $(CC) $(CFLAGS) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
 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))
+ifeq (yes,$(shell printf '${H}include <stdlib.h>\nint main(void){return reallocarray(NULL,0,0)==NULL;}' | $(CC) $(CFLAGS) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
 override CFLAGS += -DHAVE_REALLOCARRAY
 endif
 
libsepol/src/Makefile +1 -1
index 16b9bd5e..7b0e8446 100644
@@ -31,7 +31,7 @@ endif
 
 # check for reallocarray(3) availability
 H := \#
-ifeq (yes,$(shell printf '${H}define _GNU_SOURCE\n${H}include <stdlib.h>\nint main(void){void*p=reallocarray(NULL, 1, sizeof(char));return 0;}' | $(CC) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
+ifeq (yes,$(shell printf '${H}include <stdlib.h>\nint main(void){return reallocarray(NULL,0,0)==NULL;}' | $(CC) $(CFLAGS) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
 override CFLAGS += -DHAVE_REALLOCARRAY
 endif
 
Signature · 1 line
2.44.0
#1 lore raw
Quoted 45 lines · James Carter

In libselinux there is an availability check for strlcpy() and in both libselinux and libsepol there are availability checks for reallocarray() in the src Makfiles. CFLAGS and LDFLAGS are needed for cross-compiling, but, unfortunately, the default CFLAGS cause all of these availability checks to fail to compile because of compilationerrors (rather than just the function not being available).

Add CFLAGS and LDFLAGS to the availibility checks, update the checks so that a compilation error will only happen if the function being checked for is not available, and make checks for the same function the same in both libselinux and libsepol.

---
  libselinux/src/Makefile | 4 ++--
  libsepol/src/Makefile   | 2 +-
  2 files changed, 3 insertions(+), 3 deletions(-)
libselinux/src/Makefile +2 -2
index d3b981fc..41cfbdca 100644
@@ -104,13 +104,13 @@ override CFLAGS += -I../include -D_GNU_SOURCE $(DISABLE_FLAGS) $(PCRE_CFLAGS)
 
  # check for strlcpy(3) availability
  H := \#
-ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char*d,*s;strlcpy(d, s, 0);return 0;}' | $(CC) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
+ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char d[2];const char *s="a";return (size_t)strlcpy(d,s,sizeof(d))>=sizeof(d);}' | $(CC) $(CFLAGS) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
  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))
+ifeq (yes,$(shell printf '${H}include <stdlib.h>\nint main(void){return reallocarray(NULL,0,0)==NULL;}' | $(CC) $(CFLAGS) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
  override CFLAGS += -DHAVE_REALLOCARRAY
  endif
 
libsepol/src/Makefile +0 -0
index 16b9bd5e..7b0e8446 100644

libsepol/src/Makefile endif · line 37

Quoted 8 lines
libsepol/src/Makefile +1 -1
@@ -31,7 +31,7 @@ endif
 
  # check for reallocarray(3) availability
  H := \#
-ifeq (yes,$(shell printf '${H}define _GNU_SOURCE\n${H}include <stdlib.h>\nint main(void){void*p=reallocarray(NULL, 1, sizeof(char));return 0;}' | $(CC) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
+ifeq (yes,$(shell printf '${H}include <stdlib.h>\nint main(void){return reallocarray(NULL,0,0)==NULL;}' | $(CC) $(CFLAGS) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
  override CFLAGS += -DHAVE_REALLOCARRAY
  endif
 

This works great, thanks!

#2 lore raw

Fine with me as well.
Thanks,
Winfried

Quoted 58 lines

Op 14-03-2024 14:59 CET schreef Jordan Williams <[email protected]>:

Quoted 45 lines · James Carter

In libselinux there is an availability check for strlcpy() and in both libselinux and libsepol there are availability checks for reallocarray() in the src Makfiles. CFLAGS and LDFLAGS are needed for cross-compiling, but, unfortunately, the default CFLAGS cause all of these availability checks to fail to compile because of compilationerrors (rather than just the function not being available).

Add CFLAGS and LDFLAGS to the availibility checks, update the checks so that a compilation error will only happen if the function being checked for is not available, and make checks for the same function the same in both libselinux and libsepol.

---
  libselinux/src/Makefile | 4 ++--
  libsepol/src/Makefile   | 2 +-
  2 files changed, 3 insertions(+), 3 deletions(-)
libselinux/src/Makefile +2 -2
index d3b981fc..41cfbdca 100644
@@ -104,13 +104,13 @@ override CFLAGS += -I../include -D_GNU_SOURCE $(DISABLE_FLAGS) $(PCRE_CFLAGS)
 
  # check for strlcpy(3) availability
  H := \#
-ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char*d,*s;strlcpy(d, s, 0);return 0;}' | $(CC) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
+ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char d[2];const char *s="a";return (size_t)strlcpy(d,s,sizeof(d))>=sizeof(d);}' | $(CC) $(CFLAGS) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
  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))
+ifeq (yes,$(shell printf '${H}include <stdlib.h>\nint main(void){return reallocarray(NULL,0,0)==NULL;}' | $(CC) $(CFLAGS) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
  override CFLAGS += -DHAVE_REALLOCARRAY
  endif
 
libsepol/src/Makefile +0 -0
index 16b9bd5e..7b0e8446 100644

libsepol/src/Makefile endif · line 37

Quoted 8 lines
libsepol/src/Makefile +1 -1
@@ -31,7 +31,7 @@ endif
 
  # check for reallocarray(3) availability
  H := \#
-ifeq (yes,$(shell printf '${H}define _GNU_SOURCE\n${H}include <stdlib.h>\nint main(void){void*p=reallocarray(NULL, 1, sizeof(char));return 0;}' | $(CC) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
+ifeq (yes,$(shell printf '${H}include <stdlib.h>\nint main(void){return reallocarray(NULL,0,0)==NULL;}' | $(CC) $(CFLAGS) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
  override CFLAGS += -DHAVE_REALLOCARRAY
  endif
 

This works great, thanks!

#3 lore raw
In reply to James Carter
Quoted 8 lines · James Carter

In libselinux there is an availability check for strlcpy() and in both libselinux and libsepol there are availability checks for reallocarray() in the src Makfiles. CFLAGS and LDFLAGS are needed for cross-compiling, but, unfortunately, the default CFLAGS cause all of these availability checks to fail to compile because of compilationerrors (rather than just the function not being available).

Quoted 8 lines

Add CFLAGS and LDFLAGS to the availibility checks, update the checks so that a compilation error will only happen if the function being checked for is not available, and make checks for the same function the same in both libselinux and libsepol.

This patch has been merged.
Jim

Quoted 41 lines
---
 libselinux/src/Makefile | 4 ++--
 libsepol/src/Makefile   | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
libselinux/src/Makefile +2 -2
index d3b981fc..41cfbdca 100644
@@ -104,13 +104,13 @@ override CFLAGS += -I../include -D_GNU_SOURCE $(DISABLE_FLAGS) $(PCRE_CFLAGS)
 
 # check for strlcpy(3) availability
 H := \#
-ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char*d,*s;strlcpy(d, s, 0);return 0;}' | $(CC) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
+ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char d[2];const char *s="a";return (size_t)strlcpy(d,s,sizeof(d))>=sizeof(d);}' | $(CC) $(CFLAGS) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
 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))
+ifeq (yes,$(shell printf '${H}include <stdlib.h>\nint main(void){return reallocarray(NULL,0,0)==NULL;}' | $(CC) $(CFLAGS) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
 override CFLAGS += -DHAVE_REALLOCARRAY
 endif
 
libsepol/src/Makefile +1 -2
index 16b9bd5e..7b0e8446 100644
@@ -31,7 +31,7 @@ endif
 
 # check for reallocarray(3) availability
 H := \#
-ifeq (yes,$(shell printf '${H}define _GNU_SOURCE\n${H}include <stdlib.h>\nint main(void){void*p=reallocarray(NULL, 1, sizeof(char));return 0;}' | $(CC) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
+ifeq (yes,$(shell printf '${H}include <stdlib.h>\nint main(void){return reallocarray(NULL,0,0)==NULL;}' | $(CC) $(CFLAGS) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
 override CFLAGS += -DHAVE_REALLOCARRAY
 endif
 
--

2.44.0