From ff5d1c181beaa1bfeb3542dd10d59dcc13af1d93 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Wed, 29 Apr 2020 07:38:49 -0700
Subject: [PATCH] Add a wrapper for prctl [BZ #25896]

Add a C wrapper to pass arguments in

/* Control process execution.  */
extern int prctl (int __option, ...) __THROW;

to prctl syscall:

extern int prctl (int, unsigned long int, unsigned long int,
		  unsigned long int, unsigned long int);

On Linux/x86, since the prctl syscall interface:

extern int prctl (int, unsigned long int, unsigned long int,
		  unsigned long int, unsigned long int);

passes the arguments identically as the glibc prctl interface, an assembly
wrapper:

PSEUDO (__prctl, prctl, 5)
	ret
PSEUDO_END (__prctl)

is used to generate:

<__GI___prctl>:
	49 89 ca             	mov    %rcx,%r10
	b8 9d 00 00 00       	mov    $0x9d,%eax
	0f 05                	syscall
	48 3d 01 f0 ff ff    	cmp    $0xfffffffffffff001,%rax
	73 01                	jae    <__GI___prctl+0x13>
	c3                   	retq
	48 8b 0d 00 00 00 00 	mov    0x0(%rip),%rcx        # <__GI___prctl+0x1a>
	f7 d8                	neg    %eax
	64 89 01             	mov    %eax,%fs:(%rcx)
	48 83 c8 ff          	or     $0xffffffffffffffff,%rax
	c3                   	retq

instead of

<__GI___prctl>:
	48 8d 44 24 08       	lea    0x8(%rsp),%rax
	48 89 74 24 d8       	mov    %rsi,-0x28(%rsp)
	48 89 44 24 c0       	mov    %rax,-0x40(%rsp)
	48 8d 44 24 d0       	lea    -0x30(%rsp),%rax
	48 89 54 24 e0       	mov    %rdx,-0x20(%rsp)
	48 89 4c 24 e8       	mov    %rcx,-0x18(%rsp)
	4c 89 44 24 f0       	mov    %r8,-0x10(%rsp)
	48 8b 70 08          	mov    0x8(%rax),%rsi
	48 8b 50 10          	mov    0x10(%rax),%rdx
	4c 8b 40 20          	mov    0x20(%rax),%r8
	48 89 44 24 c8       	mov    %rax,-0x38(%rsp)
	4c 8b 50 18          	mov    0x18(%rax),%r10
	c7 44 24 b8 08 00 00 00 	movl   $0x8,-0x48(%rsp)
	b8 9d 00 00 00       	mov    $0x9d,%eax
	0f 05                	syscall
	48 3d 00 f0 ff ff    	cmp    $0xfffffffffffff000,%rax
	77 01                	ja     <__GI___prctl+0x50>
	c3                   	retq
	48 8b 15 00 00 00 00 	mov    0x0(%rip),%rdx        # <__GI___prctl+0x57>
	f7 d8                	neg    %eax
	64 89 02             	mov    %eax,%fs:(%rdx)
	b8 ff ff ff ff       	mov    $0xffffffff,%eax
	c3                   	retq

on x86-64.
---
 include/sys/prctl.h                        |  1 +
 sysdeps/unix/sysv/linux/Makefile           |  2 +-
 sysdeps/unix/sysv/linux/prctl.c            | 42 ++++++++++++++++++++++
 sysdeps/unix/sysv/linux/syscalls.list      |  1 -
 sysdeps/unix/sysv/linux/x86/prctl.S        | 37 +++++++++++++++++++
 sysdeps/unix/sysv/linux/x86_64/x32/prctl.S | 28 +++++++++++++++
 6 files changed, 109 insertions(+), 2 deletions(-)
 create mode 100644 sysdeps/unix/sysv/linux/prctl.c
 create mode 100644 sysdeps/unix/sysv/linux/x86/prctl.S
 create mode 100644 sysdeps/unix/sysv/linux/x86_64/x32/prctl.S

diff --git a/include/sys/prctl.h b/include/sys/prctl.h
index 0920ed642b..d33f3a290e 100644
--- a/include/sys/prctl.h
+++ b/include/sys/prctl.h
@@ -4,6 +4,7 @@
 # ifndef _ISOMAC
 
 extern int __prctl (int __option, ...);
+libc_hidden_proto (__prctl)
 
 # endif /* !_ISOMAC */
 #endif
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index db78eeadd1..0326f92c40 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -59,7 +59,7 @@ sysdep_routines += adjtimex clone umount umount2 readahead sysctl \
 		   eventfd eventfd_read eventfd_write prlimit \
 		   personality epoll_wait tee vmsplice splice \
 		   open_by_handle_at mlock2 pkey_mprotect pkey_set pkey_get \
-		   timerfd_gettime timerfd_settime \
+		   timerfd_gettime timerfd_settime prctl \
 		   process_vm_readv process_vm_writev
 
 CFLAGS-gethostid.c = -fexceptions
diff --git a/sysdeps/unix/sysv/linux/prctl.c b/sysdeps/unix/sysv/linux/prctl.c
new file mode 100644
index 0000000000..d5725f14cf
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/prctl.c
@@ -0,0 +1,42 @@
+/* prctl - Linux specific syscall.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sysdep.h>
+#include <stdarg.h>
+#include <sys/prctl.h>
+
+/* Unconditionally read all potential arguments.  This may pass
+   garbage values to the kernel, but avoids the need for teaching
+   glibc the argument counts of individual options (including ones
+   that are added to the kernel in the future).  */
+
+int
+__prctl (int option, ...)
+{
+  va_list arg;
+  va_start (arg, option);
+  unsigned long int arg2 = va_arg (arg, unsigned long int);
+  unsigned long int arg3 = va_arg (arg, unsigned long int);
+  unsigned long int arg4 = va_arg (arg, unsigned long int);
+  unsigned long int arg5 = va_arg (arg, unsigned long int);
+  va_end (arg);
+  return INLINE_SYSCALL_CALL (prctl, option, arg2, arg3, arg4, arg5);
+}
+
+libc_hidden_def (__prctl)
+weak_alias (__prctl, prctl)
diff --git a/sysdeps/unix/sysv/linux/syscalls.list b/sysdeps/unix/sysv/linux/syscalls.list
index ced77d49fa..1d8893d1b8 100644
--- a/sysdeps/unix/sysv/linux/syscalls.list
+++ b/sysdeps/unix/sysv/linux/syscalls.list
@@ -43,7 +43,6 @@ nfsservctl	EXTRA	nfsservctl	i:ipp	__compat_nfsservctl	nfsservctl@GLIBC_2.0:GLIBC
 pipe		-	pipe		i:f	__pipe		pipe
 pipe2		-	pipe2		i:fi	__pipe2		pipe2
 pivot_root	EXTRA	pivot_root	i:ss	pivot_root
-prctl		EXTRA	prctl		i:iiiii	__prctl		prctl
 query_module	EXTRA	query_module	i:sipip	__compat_query_module	query_module@GLIBC_2.0:GLIBC_2.23
 quotactl	EXTRA	quotactl	i:isip	quotactl
 remap_file_pages -	remap_file_pages i:pUiUi	__remap_file_pages remap_file_pages
diff --git a/sysdeps/unix/sysv/linux/x86/prctl.S b/sysdeps/unix/sysv/linux/x86/prctl.S
new file mode 100644
index 0000000000..ce9903ab15
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/x86/prctl.S
@@ -0,0 +1,37 @@
+/* prctl - Linux specific syscall.  Linux/x86 version.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sysdep.h>
+
+/* On Linux/x86, the prctl syscall interface:
+
+   extern int prctl (int, unsigned long int, unsigned long int,
+		     unsigned long int, unsigned long int);
+
+   and the glibc prctl interface:
+
+   extern int prctl (int option, ...);
+
+   pass the arguments identically.  */
+
+PSEUDO (__prctl, prctl, 5)
+	ret
+PSEUDO_END (__prctl)
+
+hidden_def (__prctl)
+weak_alias (__prctl, prctl)
diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/prctl.S b/sysdeps/unix/sysv/linux/x86_64/x32/prctl.S
new file mode 100644
index 0000000000..5acf5406a8
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/x86_64/x32/prctl.S
@@ -0,0 +1,28 @@
+/* prctl - Linux specific syscall.  Linux/x32 version.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sysdep.h>
+
+/* On Linux/x32, we need to zero-extend from 32 bits to 64 bits for the
+   last 4 arguments.  */
+
+#undef DOARGS_5
+#define DOARGS_5 \
+  movl %esi, %esi; movl %edx, %edx; movl %ecx, %r10d; movl %r8d, %r8d;
+
+#include <sysdeps/unix/sysv/linux/x86/prctl.S>
-- 
2.26.2

