diff mbox series

[v5,1/3] x86_64: Set the syscall register right before doing the syscall.

Message ID 20230419135821.4113156-2-josimmon@redhat.com
State New
Headers show
Series x86_64: aarch64: Set call number just before syscall | expand

Commit Message

Joe Simmons-Talbott April 19, 2023, 1:58 p.m. UTC
To make identifying syscalls easier during call tree analysis load the
syscall number just before performing the syscall.

Compiler optimizations can place quite a few instructions between the
setting of the syscall number and the syscall instruction.  During call
tree analysis the number of instructions between the two can lead to
more difficulty for both tools and humans in properly identifying the
syscall number.  Having the syscall number set in the prior instruction
to the syscall instruction makes this task easier and less error prone.
Being able to reliably identify syscalls made by a given API will make
it easier to understand and verify the safety and security of glibc.
---
 sysdeps/unix/sysv/linux/x86_64/sysdep.h | 27 ++++++++++++++-----------
 1 file changed, 15 insertions(+), 12 deletions(-)

Comments

H.J. Lu April 19, 2023, 3:35 p.m. UTC | #1
On Wed, Apr 19, 2023 at 6:59 AM Joe Simmons-Talbott via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> To make identifying syscalls easier during call tree analysis load the
> syscall number just before performing the syscall.
>
> Compiler optimizations can place quite a few instructions between the
> setting of the syscall number and the syscall instruction.  During call
> tree analysis the number of instructions between the two can lead to
> more difficulty for both tools and humans in properly identifying the
> syscall number.  Having the syscall number set in the prior instruction
> to the syscall instruction makes this task easier and less error prone.
> Being able to reliably identify syscalls made by a given API will make
> it easier to understand and verify the safety and security of glibc.
> ---
>  sysdeps/unix/sysv/linux/x86_64/sysdep.h | 27 ++++++++++++++-----------
>  1 file changed, 15 insertions(+), 12 deletions(-)
>
> diff --git a/sysdeps/unix/sysv/linux/x86_64/sysdep.h b/sysdeps/unix/sysv/linux/x86_64/sysdep.h
> index cfb51be8c5..fd9eb4b02f 100644
> --- a/sysdeps/unix/sysv/linux/x86_64/sysdep.h
> +++ b/sysdeps/unix/sysv/linux/x86_64/sysdep.h
> @@ -250,6 +250,9 @@
>      (long int) resultvar;                                              \
>  })
>
> +#define MSTR_HELPER(x) #x
> +#define MSTR(x) MSTR_HELPER(x)

These are unused.

>  #undef internal_syscall1
>  #define internal_syscall1(number, arg1)                                        \
>  ({                                                                     \
> @@ -257,9 +260,9 @@
>      TYPEFY (arg1, __arg1) = ARGIFY (arg1);                             \
>      register TYPEFY (arg1, _a1) asm ("rdi") = __arg1;                  \
>      asm volatile (                                                     \
> -    "syscall\n\t"                                                      \
> +    "movl %1, %k0\n\tsyscall\n\t"                                      \
>      : "=a" (resultvar)                                                 \
> -    : "0" (number), "r" (_a1)                                          \
> +    : "g" (number), "r" (_a1)                                          \
>      : "memory", REGISTERS_CLOBBERED_BY_SYSCALL);                       \
>      (long int) resultvar;                                              \
>  })
> @@ -273,9 +276,9 @@
>      register TYPEFY (arg2, _a2) asm ("rsi") = __arg2;                  \
>      register TYPEFY (arg1, _a1) asm ("rdi") = __arg1;                  \
>      asm volatile (                                                     \
> -    "syscall\n\t"                                                      \
> +    "movl %1, %k0\n\tsyscall\n\t"                                      \
>      : "=a" (resultvar)                                                 \
> -    : "0" (number), "r" (_a1), "r" (_a2)                               \
> +    : "g" (number), "r" (_a1), "r" (_a2)                               \
>      : "memory", REGISTERS_CLOBBERED_BY_SYSCALL);                       \
>      (long int) resultvar;                                              \
>  })
> @@ -291,9 +294,9 @@
>      register TYPEFY (arg2, _a2) asm ("rsi") = __arg2;                  \
>      register TYPEFY (arg1, _a1) asm ("rdi") = __arg1;                  \
>      asm volatile (                                                     \
> -    "syscall\n\t"                                                      \
> +    "movl %1, %k0\n\tsyscall\n\t"                                      \
>      : "=a" (resultvar)                                                 \
> -    : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3)                    \
> +    : "g" (number), "r" (_a1), "r" (_a2), "r" (_a3)                    \
>      : "memory", REGISTERS_CLOBBERED_BY_SYSCALL);                       \
>      (long int) resultvar;                                              \
>  })
> @@ -311,9 +314,9 @@
>      register TYPEFY (arg2, _a2) asm ("rsi") = __arg2;                  \
>      register TYPEFY (arg1, _a1) asm ("rdi") = __arg1;                  \
>      asm volatile (                                                     \
> -    "syscall\n\t"                                                      \
> +    "movl %1, %k0\n\tsyscall\n\t"                                      \
>      : "=a" (resultvar)                                                 \
> -    : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4)         \
> +    : "g" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4)         \
>      : "memory", REGISTERS_CLOBBERED_BY_SYSCALL);                       \
>      (long int) resultvar;                                              \
>  })
> @@ -333,9 +336,9 @@
>      register TYPEFY (arg2, _a2) asm ("rsi") = __arg2;                  \
>      register TYPEFY (arg1, _a1) asm ("rdi") = __arg1;                  \
>      asm volatile (                                                     \
> -    "syscall\n\t"                                                      \
> +    "movl %1, %k0\n\tsyscall\n\t"                                      \
>      : "=a" (resultvar)                                                 \
> -    : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4),                \
> +    : "g" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4),                \
>        "r" (_a5)                                                                \
>      : "memory", REGISTERS_CLOBBERED_BY_SYSCALL);                       \
>      (long int) resultvar;                                              \
> @@ -358,9 +361,9 @@
>      register TYPEFY (arg2, _a2) asm ("rsi") = __arg2;                  \
>      register TYPEFY (arg1, _a1) asm ("rdi") = __arg1;                  \
>      asm volatile (                                                     \
> -    "syscall\n\t"                                                      \
> +    "movl %1, %k0\n\tsyscall\n\t"                                      \
>      : "=a" (resultvar)                                                 \
> -    : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4),                \
> +    : "g" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4),                \
>        "r" (_a5), "r" (_a6)                                             \
>      : "memory", REGISTERS_CLOBBERED_BY_SYSCALL);                       \
>      (long int) resultvar;                                              \
> --
> 2.39.2
>
Joe Simmons-Talbott April 19, 2023, 3:48 p.m. UTC | #2
On Wed, Apr 19, 2023 at 08:35:30AM -0700, H.J. Lu wrote:
> On Wed, Apr 19, 2023 at 6:59 AM Joe Simmons-Talbott via Libc-alpha
> <libc-alpha@sourceware.org> wrote:
> >
> > To make identifying syscalls easier during call tree analysis load the
> > syscall number just before performing the syscall.
> >
> > Compiler optimizations can place quite a few instructions between the
> > setting of the syscall number and the syscall instruction.  During call
> > tree analysis the number of instructions between the two can lead to
> > more difficulty for both tools and humans in properly identifying the
> > syscall number.  Having the syscall number set in the prior instruction
> > to the syscall instruction makes this task easier and less error prone.
> > Being able to reliably identify syscalls made by a given API will make
> > it easier to understand and verify the safety and security of glibc.
> > ---
> >  sysdeps/unix/sysv/linux/x86_64/sysdep.h | 27 ++++++++++++++-----------
> >  1 file changed, 15 insertions(+), 12 deletions(-)
> >
> > diff --git a/sysdeps/unix/sysv/linux/x86_64/sysdep.h b/sysdeps/unix/sysv/linux/x86_64/sysdep.h
> > index cfb51be8c5..fd9eb4b02f 100644
> > --- a/sysdeps/unix/sysv/linux/x86_64/sysdep.h
> > +++ b/sysdeps/unix/sysv/linux/x86_64/sysdep.h
> > @@ -250,6 +250,9 @@
> >      (long int) resultvar;                                              \
> >  })
> >
> > +#define MSTR_HELPER(x) #x
> > +#define MSTR(x) MSTR_HELPER(x)
> 
> These are unused.
> 

I'll remove this in v6.

Thanks,
Joe
diff mbox series

Patch

diff --git a/sysdeps/unix/sysv/linux/x86_64/sysdep.h b/sysdeps/unix/sysv/linux/x86_64/sysdep.h
index cfb51be8c5..fd9eb4b02f 100644
--- a/sysdeps/unix/sysv/linux/x86_64/sysdep.h
+++ b/sysdeps/unix/sysv/linux/x86_64/sysdep.h
@@ -250,6 +250,9 @@ 
     (long int) resultvar;						\
 })
 
+#define MSTR_HELPER(x) #x
+#define MSTR(x) MSTR_HELPER(x)
+
 #undef internal_syscall1
 #define internal_syscall1(number, arg1)					\
 ({									\
@@ -257,9 +260,9 @@ 
     TYPEFY (arg1, __arg1) = ARGIFY (arg1);			 	\
     register TYPEFY (arg1, _a1) asm ("rdi") = __arg1;			\
     asm volatile (							\
-    "syscall\n\t"							\
+    "movl %1, %k0\n\tsyscall\n\t"					\
     : "=a" (resultvar)							\
-    : "0" (number), "r" (_a1)						\
+    : "g" (number), "r" (_a1)						\
     : "memory", REGISTERS_CLOBBERED_BY_SYSCALL);			\
     (long int) resultvar;						\
 })
@@ -273,9 +276,9 @@ 
     register TYPEFY (arg2, _a2) asm ("rsi") = __arg2;			\
     register TYPEFY (arg1, _a1) asm ("rdi") = __arg1;			\
     asm volatile (							\
-    "syscall\n\t"							\
+    "movl %1, %k0\n\tsyscall\n\t"					\
     : "=a" (resultvar)							\
-    : "0" (number), "r" (_a1), "r" (_a2)				\
+    : "g" (number), "r" (_a1), "r" (_a2)				\
     : "memory", REGISTERS_CLOBBERED_BY_SYSCALL);			\
     (long int) resultvar;						\
 })
@@ -291,9 +294,9 @@ 
     register TYPEFY (arg2, _a2) asm ("rsi") = __arg2;			\
     register TYPEFY (arg1, _a1) asm ("rdi") = __arg1;			\
     asm volatile (							\
-    "syscall\n\t"							\
+    "movl %1, %k0\n\tsyscall\n\t"					\
     : "=a" (resultvar)							\
-    : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3)			\
+    : "g" (number), "r" (_a1), "r" (_a2), "r" (_a3)			\
     : "memory", REGISTERS_CLOBBERED_BY_SYSCALL);			\
     (long int) resultvar;						\
 })
@@ -311,9 +314,9 @@ 
     register TYPEFY (arg2, _a2) asm ("rsi") = __arg2;			\
     register TYPEFY (arg1, _a1) asm ("rdi") = __arg1;			\
     asm volatile (							\
-    "syscall\n\t"							\
+    "movl %1, %k0\n\tsyscall\n\t"					\
     : "=a" (resultvar)							\
-    : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4)		\
+    : "g" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4)		\
     : "memory", REGISTERS_CLOBBERED_BY_SYSCALL);			\
     (long int) resultvar;						\
 })
@@ -333,9 +336,9 @@ 
     register TYPEFY (arg2, _a2) asm ("rsi") = __arg2;			\
     register TYPEFY (arg1, _a1) asm ("rdi") = __arg1;			\
     asm volatile (							\
-    "syscall\n\t"							\
+    "movl %1, %k0\n\tsyscall\n\t"					\
     : "=a" (resultvar)							\
-    : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4),		\
+    : "g" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4),		\
       "r" (_a5)								\
     : "memory", REGISTERS_CLOBBERED_BY_SYSCALL);			\
     (long int) resultvar;						\
@@ -358,9 +361,9 @@ 
     register TYPEFY (arg2, _a2) asm ("rsi") = __arg2;			\
     register TYPEFY (arg1, _a1) asm ("rdi") = __arg1;			\
     asm volatile (							\
-    "syscall\n\t"							\
+    "movl %1, %k0\n\tsyscall\n\t"					\
     : "=a" (resultvar)							\
-    : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4),		\
+    : "g" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4),		\
       "r" (_a5), "r" (_a6)						\
     : "memory", REGISTERS_CLOBBERED_BY_SYSCALL);			\
     (long int) resultvar;						\