From patchwork Thu Apr 30 13:03:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1280374 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=sourceware.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=yGJTmiA5; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49CbDR2zjzz9sSj for ; Thu, 30 Apr 2020 23:03:51 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B2AAC389851C; Thu, 30 Apr 2020 13:03:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B2AAC389851C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1588251828; bh=FU4K4dTQglzxGdYWqXeaATLdRwYGzhjVA6QT7o6ILkA=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=yGJTmiA5DB7WjyC0coTNJTIRQElSsLk3mIvCK7JJ/avtaqjdVKSNIIZzWPz+ma060 qgWcaAfPAxuSiR3C2EHWQtnoXUKr80cCGHGlQTPfPeAFurBkxENqjydTskDiJE+G77 7zZszxBqwAiXjI3NGTtsXlgbWTRUuLKwgydFn9P0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x642.google.com (mail-pl1-x642.google.com [IPv6:2607:f8b0:4864:20::642]) by sourceware.org (Postfix) with ESMTPS id 3BD3E3892007 for ; Thu, 30 Apr 2020 13:03:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3BD3E3892007 Received: by mail-pl1-x642.google.com with SMTP id s20so2219187plp.6 for ; Thu, 30 Apr 2020 06:03:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=FU4K4dTQglzxGdYWqXeaATLdRwYGzhjVA6QT7o6ILkA=; b=gZxc//YgKyEby5VPYJ7sdPzvbNIT/L6YOxhLTj+Ce7JfA+XSM9o9LhbKGjkLrmZEts woX/HOYgWkbbpgD+fTGy8kwfJa1InQWFq5YoDclSRKXepG/MT4gO6Ikz45+tsd5PSTHL sWHpFtlA2joakby/Kpd15PyfXiE1aHan8HV3pzb/+WYQZ+9lfDe1GhV4Dc4YjUG03rrt JLE7tUinwLFz9YiKhQukTaxA90itJ3GQaOd3rzBIk+Iqu5auED4KRmofRdAKF7FisuKt WuBE/seWayK/OTio7fa+/wTQyB7ZqmZZQxmM+Lfdk0yD6vY2Ae3hA6mJCA0c5aQFVWef MBGA== X-Gm-Message-State: AGi0PuZKbPHKNOZinkf12iKL3T/Oex/JHadqnZgNxckkcLZzKOf2wpNw j1Gr28cdyI+HXw7UChBdOD42bA9C X-Google-Smtp-Source: APiQypLFhr4cB4hCQlwFE0YyTfxKQFVWBNrFH3ijGpVN7+dlXT7/mWa20k1eV/Hs0JjHUUA7oXOehQ== X-Received: by 2002:a17:902:b206:: with SMTP id t6mr3701197plr.270.1588251816150; Thu, 30 Apr 2020 06:03:36 -0700 (PDT) Received: from gnu-cfl-2.localdomain (c-69-181-90-243.hsd1.ca.comcast.net. [69.181.90.243]) by smtp.gmail.com with ESMTPSA id mj4sm1763905pjb.0.2020.04.30.06.03.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Apr 2020 06:03:35 -0700 (PDT) Received: by gnu-cfl-2.localdomain (Postfix, from userid 1000) id B2ED71A0117; Thu, 30 Apr 2020 06:03:33 -0700 (PDT) Date: Thu, 30 Apr 2020 06:03:33 -0700 To: Florian Weimer Subject: V2 [PATCH] Add a C wrapper for prctl [BZ #25896] Message-ID: <20200430130333.GA254612@gmail.com> References: <20200429205217.2435607-1-hjl.tools@gmail.com> <87wo5xa0yt.fsf@mid.deneb.enyo.de> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <87wo5xa0yt.fsf@mid.deneb.enyo.de> X-Spam-Status: No, score=-24.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: "H.J. Lu via Libc-alpha" Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" On Thu, Apr 30, 2020 at 09:26:18AM +0200, Florian Weimer wrote: > * H. J. Lu via Libc-alpha: > > > 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); > > --- > > include/sys/prctl.h | 2 ++ > > sysdeps/unix/sysv/linux/Makefile | 2 +- > > sysdeps/unix/sysv/linux/prctl.c | 38 +++++++++++++++++++++++++++ > > sysdeps/unix/sysv/linux/syscalls.list | 1 - > > 4 files changed, 41 insertions(+), 2 deletions(-) > > create mode 100644 sysdeps/unix/sysv/linux/prctl.c > > > > diff --git a/include/sys/prctl.h b/include/sys/prctl.h > > index 0920ed642b..1a74d83879 100644 > > --- a/include/sys/prctl.h > > +++ b/include/sys/prctl.h > > @@ -4,6 +4,8 @@ > > # ifndef _ISOMAC > > > > extern int __prctl (int __option, ...); > > +libc_hidden_proto (__prctl) > > +libc_hidden_proto (prctl) > > I see we have some internal callers of prctl. I will clean this up > later. > > > Please add a comment that this over-reads arguments for some option > values, something like: > > /* 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). */ > > Rest looks fine. Generated code is quite a bit worse, but I don't > think this matter here. Here is the updated patch. I added the assembly version for x86. Other arches can do #include if they are similar. OK for master? Thanks. H.J. --- 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); and the glibc prctl interface: extern int prctl (int option, ...); pass the arguments identically, the assembly verion: PSEUDO (__prctl, prctl, 5) ret PSEUDO_END (__prctl) is used. --- include/sys/prctl.h | 2 + sysdeps/unix/sysv/linux/Makefile | 2 +- sysdeps/unix/sysv/linux/prctl.c | 43 ++++++++++++++++++++++ sysdeps/unix/sysv/linux/syscalls.list | 1 - sysdeps/unix/sysv/linux/x86/prctl.S | 38 +++++++++++++++++++ sysdeps/unix/sysv/linux/x86_64/x32/prctl.S | 28 ++++++++++++++ 6 files changed, 112 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..1a74d83879 100644 --- a/include/sys/prctl.h +++ b/include/sys/prctl.h @@ -4,6 +4,8 @@ # ifndef _ISOMAC extern int __prctl (int __option, ...); +libc_hidden_proto (__prctl) +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..49049fd85f --- /dev/null +++ b/sysdeps/unix/sysv/linux/prctl.c @@ -0,0 +1,43 @@ +/* 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 + . */ + +#include +#include +#include + +/* 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) +hidden_weak (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..99bb62ad39 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86/prctl.S @@ -0,0 +1,38 @@ +/* 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 + . */ + +#include + +/* 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) +hidden_weak (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 + . */ + +#include + +/* 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