From e9b4bbc10e9b9a7346014fe608485bb1aad49f7c Mon Sep 17 00:00:00 2001
From: Giuliano Belinassi <gbelinassi@suse.de>
Date: Mon, 6 May 2024 21:06:46 -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 <gbelinassi@suse.de>
---
 Makeconfig              |  5 +++++
 config.h.in             |  3 +++
 config.make.in          |  1 +
 configure               | 20 ++++++++++++++++++++
 configure.ac            | 13 +++++++++++++
 sysdeps/x86/sysdep.h    | 24 +++++++++++++++++++-----
 sysdeps/x86_64/sysdep.h | 40 ++++++++++++++++++++++++++++++++++++++++
 7 files changed, 101 insertions(+), 5 deletions(-)

Index: glibc-2.31/Makeconfig
===================================================================
--- glibc-2.31.orig/Makeconfig
+++ glibc-2.31/Makeconfig
@@ -916,6 +916,11 @@ endif	# $(+cflags) == ""
 	   $(+stack-protector)
 +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
Index: glibc-2.31/config.h.in
===================================================================
--- glibc-2.31.orig/config.h.in
+++ glibc-2.31/config.h.in
@@ -186,6 +186,9 @@
 /* Define if the linker defines __ehdr_start.  */
 #undef HAVE_EHDR_START
 
+/* Define to 1 if support for userspace livepatching is enabled.  */
+#define ENABLE_USERSPACE_LIVEPATCH 0
+
 /*
  */
 
Index: glibc-2.31/config.make.in
===================================================================
--- glibc-2.31.orig/config.make.in
+++ glibc-2.31/config.make.in
@@ -88,6 +88,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-pic-default= @libc_cv_pic_default@
 build-pie-default= @libc_cv_pie_default@
Index: glibc-2.31/configure
===================================================================
--- glibc-2.31.orig/configure
+++ glibc-2.31/configure
@@ -590,6 +590,7 @@ ac_subst_vars='LTLIBOBJS
 LIBOBJS
 RELEASE
 VERSION
+enable_userspace_livepatch
 mach_interface_list
 DEFINES
 static_nss
@@ -795,6 +796,7 @@ enable_tunables
 enable_mathvec
 enable_cet
 with_cpu
+enable_userspace_livepatch
 '
       ac_precious_vars='build_alias
 host_alias
@@ -1471,6 +1473,8 @@ Optional Features:
                           depends on architecture]
   --enable-cet            enable Intel Control-flow Enforcement Technology
                           (CET), x86 only
+  --enable-userspace-livepatch
+                          build with userspace livepatch support [default=no]
 
 Optional Packages:
   --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
@@ -6918,6 +6922,22 @@ enable-static-pie = $static_pie"
 
 
 
+# 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`
 
Index: glibc-2.31/configure.ac
===================================================================
--- glibc-2.31.orig/configure.ac
+++ glibc-2.31/configure.ac
@@ -1896,6 +1896,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)
Index: glibc-2.31/sysdeps/x86/sysdep.h
===================================================================
--- glibc-2.31.orig/sysdeps/x86/sysdep.h
+++ glibc-2.31/sysdeps/x86/sysdep.h
@@ -72,15 +72,29 @@ enum cf_protection_level
 #define ALIGNARG(log2) 1<<log2
 #define ASM_SIZE_DIRECTIVE(name) .size name,.-name;
 
+/* For 32-bit glibc then define those macros as empty.  */
+#ifndef ULP_PRE_PROLOGUE
+# define ULP_PRE_PROLOGUE(name)
+#endif
+#ifndef ULP_POST_PROLOGUE
+# define ULP_POST_PROLOGUE(name)
+#endif
+
+/* Define the first instructions of a function.  */
+#define FUNCTION_START(name) \
+  ULP_PRE_PROLOGUE(name); \
+  C_LABEL(name); \
+  cfi_startproc; \
+  _CET_ENDBR; \
+  ULP_POST_PROLOGUE(name); \
+  CALL_MCOUNT;
+
 /* Define an entry point visible from C.  */
 #define	ENTRY(name)							      \
   .globl C_SYMBOL_NAME(name);						      \
   .type C_SYMBOL_NAME(name),@function;					      \
-  .align ALIGNARG(4);							      \
-  C_LABEL(name)								      \
-  cfi_startproc;							      \
-  _CET_ENDBR;								      \
-  CALL_MCOUNT
+  .align ALIGNARG(4);						      \
+  FUNCTION_START(name)
 
 #undef	END
 #define END(name)							      \
Index: glibc-2.31/sysdeps/x86_64/sysdep.h
===================================================================
--- glibc-2.31.orig/sysdeps/x86_64/sysdep.h
+++ glibc-2.31/sysdeps/x86_64/sysdep.h
@@ -23,6 +23,46 @@
 
 #ifdef	__ASSEMBLER__
 
+/* Libpulp uses -fpatchable-function-entry to add padding NOPS to the
+   prologue of all functions. This works for C functions. For functions
+   written in ASM, the way we do this is by adding this prologue manually.  */
+
+#if ENABLE_USERSPACE_LIVEPATCH
+
+/* Instructions to be inserted before the function label.  */
+# define ULP_NOPS_PRE_PROLOGUE     .rept 14; nop; .endr
+
+/* Instruction to be inserted after the function label.  */
+# define ULP_NOPS_POST_PROLOGUE    .rept  2; nop; .endr
+
+
+/* this macro expands according to the following condition:
+ * if name = _start, then the prologue is not inserted.
+ * if name = _dl_relocate_static_pie, then the prologue is not inserted.
+ * if name = anything else, then the prologue is inserted.
+ **/
+# define __ULP_POST_PROLOGUE_dl_relocate_static_pie   ,
+# define __ULP_PRE_PROLOGUE_start                     ,
+# define __ULP_PRE_PROLOGUE(x, y,...)                 y
+# define _ULP_PRE_PROLOGUE(x, ...)                    __ULP_PRE_PROLOGUE(x, __VA_ARGS__)
+# define ULP_PRE_PROLOGUE(name)                       _ULP_PRE_PROLOGUE(__ULP_PRE_PROLOGUE##name, ULP_NOPS_PRE_PROLOGUE,)
+
+/* this macro expands according to the following condition:
+ * if name = _start, then the postlogue is not inserted.
+ * if name = _dl_relocate_static_pie, then the postlogue is not inserted.
+ * if name = anything else, then the postlogue is inserted.
+ **/
+# define __ULP_POST_PROLOGUE_dl_relocate_static_pie   ,
+# define __ULP_POST_PROLOGUE_start                    ,
+# define __ULP_POST_PROLOGUE(x, y,...)                y
+# define _ULP_POST_PROLOGUE(x, ...)                   __ULP_POST_PROLOGUE(x, __VA_ARGS__)
+# define ULP_POST_PROLOGUE(name)                      _ULP_POST_PROLOGUE(__ULP_POST_PROLOGUE##name, ULP_NOPS_POST_PROLOGUE,)
+
+#else
+# define ULP_PRE_PROLOGUE(name)
+# define ULP_POST_PROLOGUE(name)
+#endif
+
 /* Syntactic details of assembler.  */
 
 /* This macro is for setting proper CFI with DW_CFA_expression describing
