From 17a5177cbb228f22ef3e00d4bb66af71724a6d07 Mon Sep 17 00:00:00 2001 From: Giuliano Belinassi Date: Wed, 24 May 2023 18:03:15 -0300 Subject: [PATCH] Add Userspace Livepatch prologue into ASM functions Userspace Live Patching (ULP) refers to the process of applying patches to the libraries used by a running process, without interrupting it. In order to archive this, functions must have the NOP prologue. This prologue is included automatically when compiled with -fpatchable-function-entry, but for ASM functions this have to be included manually. This patch does this. Signed-off-by: Giuliano Belinassi --- Makeconfig | 5 +++++ config.h.in | 3 +++ config.make.in | 1 + configure | 20 ++++++++++++++++++++ configure.ac | 13 +++++++++++++ sysdeps/x86/sysdep.h | 22 ++++++++++++++++++---- sysdeps/x86_64/multiarch/strcmp-avx2.S | 5 +---- sysdeps/x86_64/multiarch/strcmp-evex.S | 5 +---- sysdeps/x86_64/multiarch/strcmp-sse4_2.S | 5 +---- sysdeps/x86_64/sysdep.h | 13 +++++++++++++ 10 files changed, 76 insertions(+), 16 deletions(-) diff --git a/Makeconfig b/Makeconfig index 2fda4af5f7..52a79bc0c0 100644 --- a/Makeconfig +++ b/Makeconfig @@ -961,6 +961,11 @@ endif # $(+cflags) == "" $(+stack-protector) -fno-common +gcc-nowarn := -w +# Add flags for Userspace Livepatching support. +ifeq (yes,$(enable-userspace-livepatch)) ++cflags += -fpatchable-function-entry=16,14 +endif + # Each sysdeps directory can contain header files that both will be # used to compile and will be installed. Each can also contain an # include/ subdirectory, whose header files will be used to compile diff --git a/config.h.in b/config.h.in index 43d32518ab..32fc79a357 100644 --- a/config.h.in +++ b/config.h.in @@ -199,6 +199,9 @@ /* Define to 1 if libpthread actually resides in libc. */ #define PTHREAD_IN_LIBC 0 +/* Define to 1 if support for userspace livepatching is enabled. */ +#define ENABLE_USERSPACE_LIVEPATCH 0 + /* An integer used to scale the timeout of test programs. */ #define TIMEOUTFACTOR 1 diff --git a/config.make.in b/config.make.in index d7c416cbea..c6f6909d68 100644 --- a/config.make.in +++ b/config.make.in @@ -86,6 +86,7 @@ nss-crypt = @libc_cv_nss_crypt@ static-nss-crypt = @libc_cv_static_nss_crypt@ # Configuration options. +enable-userspace-livepatch = @enable_userspace_livepatch@ build-shared = @shared@ build-profile = @profile@ build-static-nss = @static_nss@ diff --git a/configure b/configure index efb891456a..4030ccf701 100755 --- a/configure +++ b/configure @@ -591,6 +591,7 @@ LIBOBJS pthread_in_libc RELEASE VERSION +enable_userspace_livepatch mach_interface_list DEFINES static_nss @@ -789,6 +790,7 @@ enable_mathvec enable_cet enable_scv with_cpu +enable_userspace_livepatch ' ac_precious_vars='build_alias host_alias @@ -1462,6 +1464,8 @@ Optional Features: (CET), x86 only --disable-scv syscalls will not use scv instruction, even if the kernel supports it, powerpc only + --enable-userspace-livepatch + build with userspace livepatch support [default=no] Optional Packages: --with-PACKAGE[=ARG] use PACKAGE [ARG=yes] @@ -6751,6 +6755,22 @@ libc_cv_multidir=`${CC-cc} $CFLAGS $CPPFLAGS -print-multi-directory` +# Check whether --enable-userspace-livepatch was given. +if test "${enable_userspace_livepatch+set}" = set; then : + enableval=$enable_userspace_livepatch; enable_userspace_livepatch=$enableval +else + enable_userspace_livepatch=no +fi + + +# Libpulp uses -fpatchable-function-entry to add padding NOPS to the +# prologue of all functions. +if test "x$enable_userspace_livepatch" = xyes; then + $as_echo "#define ENABLE_USERSPACE_LIVEPATCH 1" >>confdefs.h + +fi + + VERSION=`sed -n -e 's/^#define VERSION "\([^"]*\)"/\1/p' < $srcdir/version.h` RELEASE=`sed -n -e 's/^#define RELEASE "\([^"]*\)"/\1/p' < $srcdir/version.h` diff --git a/configure.ac b/configure.ac index 011844a3d4..26c1a78009 100644 --- a/configure.ac +++ b/configure.ac @@ -1827,6 +1827,19 @@ AC_SUBST(DEFINES) dnl See sysdeps/mach/configure.ac for this variable. AC_SUBST(mach_interface_list) +AC_ARG_ENABLE([userspace-livepatch], + AS_HELP_STRING([--enable-userspace-livepatch], + [build with userspace livepatch support @<:@default=no@:>@]), + [enable_userspace_livepatch=$enableval], + [enable_userspace_livepatch=no]) + +# Libpulp uses -fpatchable-function-entry to add padding NOPS to the +# prologue of all functions. +if test "x$enable_userspace_livepatch" = xyes; then + AC_DEFINE(ENABLE_USERSPACE_LIVEPATCH) +fi +AC_SUBST(enable_userspace_livepatch) + VERSION=`sed -n -e 's/^#define VERSION "\([^"]*\)"/\1/p' < $srcdir/version.h` RELEASE=`sed -n -e 's/^#define RELEASE "\([^"]*\)"/\1/p' < $srcdir/version.h` AC_SUBST(VERSION) diff --git a/sysdeps/x86/sysdep.h b/sysdeps/x86/sysdep.h index 0b3483a77a..329c16306e 100644 --- a/sysdeps/x86/sysdep.h +++ b/sysdeps/x86/sysdep.h @@ -77,15 +77,29 @@ enum cf_protection_level #define ALIGNARG(log2) 1<