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

H.J. Lu hjl.tools@gmail.com
Wed Apr 19 15:35:30 GMT 2023


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
>


-- 
H.J.


More information about the Libc-alpha mailing list