From 371cd5b864f227c2567e4bd04bb23c6e5cc054a7 Mon Sep 17 00:00:00 2001 From: Jordan Williams Date: Thu, 6 Jun 2024 02:47:36 -0500 Subject: [PATCH] (#22952) libselinux: Fix cross-compilation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * libselinux: Fix cross-compilation Apply patches for 3.6 to fix detection of available functions when cross-compiling. Use the PKG_CONFIG_LIBDIR to point pkg-config to the correct directory containing Conan's generated pkg-config files. Without this, the pcre2 from the system will be picked up before the one provided by Conan. When cross-compiling, this causes system include directories to poison the build. * Bump pkgconf * Only set PKG_CONFIG_LIBDIR when cross-compiling * Update recipes/libselinux/all/conanfile.py --------- Co-authored-by: Francisco Ramírez --- recipes/libselinux/all/conandata.yml | 9 +++ recipes/libselinux/all/conanfile.py | 8 ++- ...-Makefile-fix-reallocarray-detection.patch | 48 +++++++++++++++ ...inux-libsepol-Add-CFLAGS-and-LDFLAGS.patch | 61 +++++++++++++++++++ 4 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 recipes/libselinux/all/patches/0004-libsepol-src-Makefile-fix-reallocarray-detection.patch create mode 100644 recipes/libselinux/all/patches/0005-libselinux-libsepol-Add-CFLAGS-and-LDFLAGS.patch diff --git a/recipes/libselinux/all/conandata.yml b/recipes/libselinux/all/conandata.yml index 7387838ec6a10..6ec0dbf8b0f22 100644 --- a/recipes/libselinux/all/conandata.yml +++ b/recipes/libselinux/all/conandata.yml @@ -40,6 +40,15 @@ patches: base_path: libselinux-3.6 patch_description: "Fix a missing #include " patch_type: "portability" + - patch_file: patches/0004-libsepol-src-Makefile-fix-reallocarray-detection.patch + patch_description: "libsepol/src/Makefile: fix reallocarray detection" + patch_source: "https://lore.kernel.org/selinux/20240108210314.339682-1-fontaine.fabrice@gmail.com/" + patch_type: "portability" + base_path: libsepol-3.6 + - patch_file: patches/0005-libselinux-libsepol-Add-CFLAGS-and-LDFLAGS.patch + patch_description: "libselinux, libsepol: Add CFLAGS and LDFLAGS to Makefile checks" + patch_source: "https://lore.kernel.org/selinux/20240313224806.2859045-1-jwcart2@gmail.com/T/#u" + patch_type: "portability" "3.3": - patch_file: patches/0003-fix-link-pcre.patch base_path: libselinux-3.3 diff --git a/recipes/libselinux/all/conanfile.py b/recipes/libselinux/all/conanfile.py index 11d688ce34c15..19bf1f194402c 100644 --- a/recipes/libselinux/all/conanfile.py +++ b/recipes/libselinux/all/conanfile.py @@ -2,6 +2,7 @@ from conan import ConanFile from conan.errors import ConanInvalidConfiguration +from conan.tools.build import cross_building from conan.tools.env import VirtualBuildEnv from conan.tools.files import apply_conandata_patches, chdir, copy, export_conandata_patches, get, rename, replace_in_file, save from conan.tools.gnu import Autotools, AutotoolsToolchain, PkgConfigDeps @@ -62,7 +63,7 @@ def validate(self): def build_requirements(self): self.tool_requires("flex/2.6.4") if not self.conf.get("tools.gnu:pkg_config", default=False, check_type=str): - self.tool_requires("pkgconf/2.0.3") + self.tool_requires("pkgconf/2.2.0") def source(self): for download in self.conan_data["sources"][self.version]: @@ -87,7 +88,10 @@ def generate(self): sepol_lib_folder = os.path.join(self._sepol_source_folder, "src") tc.extra_ldflags.append(f"-L{sepol_lib_folder}") tc.make_args.append("USE_PCRE2=y") - tc.generate() + env = tc.environment() + if cross_building(self): + env.append_path("PKG_CONFIG_LIBDIR", self.generators_folder) + tc.generate(env=env) def build(self): apply_conandata_patches(self) diff --git a/recipes/libselinux/all/patches/0004-libsepol-src-Makefile-fix-reallocarray-detection.patch b/recipes/libselinux/all/patches/0004-libsepol-src-Makefile-fix-reallocarray-detection.patch new file mode 100644 index 0000000000000..c9a5ee550ab55 --- /dev/null +++ b/recipes/libselinux/all/patches/0004-libsepol-src-Makefile-fix-reallocarray-detection.patch @@ -0,0 +1,48 @@ +From a520f972bce9ec267f4e76b729bc3b7c1bdf13e6 Mon Sep 17 00:00:00 2001 +From: Fabrice Fontaine +Date: Mon, 8 Jan 2024 22:03:14 +0100 +Subject: [PATCH 1/3] libsepol/src/Makefile: fix reallocarray detection + +Pass LDFLAGS when checking for reallocarray to avoid the following +static build failure with musl raised since version 3.4 and +https://github.com/SELinuxProject/selinux/commit/f0a5f6e33084bd83d409bb7c932256139f471e71 +because -static is not passed when checking for reallocarray: + +/home/autobuild/autobuild/instance-9/output-1/host/bin/armeb-buildroot-linux-musleabi-gcc -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -O0 -g0 -static -I. -I../include -D_GNU_SOURCE -I../cil/include -fPIC -c -o assertion.o assertion.c +In file included from assertion.c:28: +private.h:88:21: error: static declaration of 'reallocarray' follows non-static declaration + 88 | static inline void* reallocarray(void *ptr, size_t nmemb, size_t size) { + | ^~~~~~~~~~~~ +In file included from ../include/sepol/policydb/mls_types.h:35, + from ../include/sepol/policydb/context.h:23, + from ../include/sepol/policydb/policydb.h:62, + from assertion.c:24: +/home/autobuild/autobuild/instance-9/output-1/host/armeb-buildroot-linux-musleabi/sysroot/usr/include/stdlib.h:150:7: note: previous declaration of 'reallocarray' with type 'void *(void *, size_t, size_t)' {aka 'void *(void *, unsigned int, unsigned int)'} + 150 | void *reallocarray (void *, size_t, size_t); + | ^~~~~~~~~~~~ + +Fixes: + - http://autobuild.buildroot.org/results/0170032548a38e2c991d62dc5823808458ad03b3 + +Signed-off-by: Fabrice Fontaine +Acked-by: James Carter +--- +src/Makefile | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/src/Makefile b/src/Makefile +index d80a941f..16b9bd5e 100644 +--- a/src/Makefile ++++ b/src/Makefile +@@ -31,7 +31,7 @@ endif + + # check for reallocarray(3) availability + H := \# +-ifeq (yes,$(shell printf '${H}define _GNU_SOURCE\n${H}include \nint main(void){void*p=reallocarray(NULL, 1, sizeof(char));return 0;}' | $(CC) -x c -o /dev/null - >/dev/null 2>&1 && echo yes)) ++ifeq (yes,$(shell printf '${H}define _GNU_SOURCE\n${H}include \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)) + override CFLAGS += -DHAVE_REALLOCARRAY + endif + +-- +2.44.0 + diff --git a/recipes/libselinux/all/patches/0005-libselinux-libsepol-Add-CFLAGS-and-LDFLAGS.patch b/recipes/libselinux/all/patches/0005-libselinux-libsepol-Add-CFLAGS-and-LDFLAGS.patch new file mode 100644 index 0000000000000..bea3664ddd66d --- /dev/null +++ b/recipes/libselinux/all/patches/0005-libselinux-libsepol-Add-CFLAGS-and-LDFLAGS.patch @@ -0,0 +1,61 @@ +[PATCH] libselinux, libsepol: Add CFLAGS and LDFLAGS to Makefile checks +@ 2024-03-13 22:48 James Carter + 0 siblings, 0 replies; only message in thread +From: James Carter @ 2024-03-13 22:48 UTC (permalink / raw) + To: selinux; +Cc: jordan, winfried_mb2, 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. + +Suggested-by: Jordan Williams +Suggested-by: Winfried Dobbe +Signed-off-by: James Carter +--- + libselinux/src/Makefile | 4 ++-- + libsepol/src/Makefile | 2 +- + 2 files changed, 3 insertions(+), 3 deletions(-) + +diff --git a/libselinux-3.6/src/Makefile b/libselinux-3.6/src/Makefile +index d3b981fc..41cfbdca 100644 +--- a/libselinux-3.6/src/Makefile ++++ b/libselinux-3.6/src/Makefile +@@ -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 \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 \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 \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 \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 + +diff --git a/libsepol-3.6/src/Makefile b/libsepol-3.6/src/Makefile +index 16b9bd5e..7b0e8446 100644 +--- a/libsepol-3.6/src/Makefile ++++ b/libsepol-3.6/src/Makefile +@@ -31,7 +31,7 @@ endif + + # check for reallocarray(3) availability + H := \# +-ifeq (yes,$(shell printf '${H}define _GNU_SOURCE\n${H}include \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 \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