From patchwork Thu Jul 16 11:26:51 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: 40115 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4CB17388CC0F; Thu, 16 Jul 2020 11:26:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4CB17388CC0F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1594898817; bh=DfHePW8DVnux8VajikXffkBmWTkizzrLUkpjO1jZG4Q=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=lra8rb9Sh+tPtL0VAaTr3BgKIumfxI7e2Wx66w0/w/cyQZtD+jLpAOLkIOqCK167W cHnnnK5YD1nI8TRf62d3wHnLpsb3JFN2YY2DMZY3t6gfWy5bjEA975xuvyYJgcohXr 3FLUdFGsfDK+yXuuzIyeTCkwTMV+f1sX8S8Ni3Qw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) by sourceware.org (Postfix) with ESMTPS id 16278388A837 for ; Thu, 16 Jul 2020 11:26:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 16278388A837 Received: by mail-pf1-x444.google.com with SMTP id a23so1933141pfk.13 for ; Thu, 16 Jul 2020 04:26:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=DfHePW8DVnux8VajikXffkBmWTkizzrLUkpjO1jZG4Q=; b=BhFe4dEj8OfwlTisBfNtudRIKhx8gwJaaF8S+T+uETU2dL+jryDja+W3CX6IT2IvS1 PxFHCq29XH/PV3RG8kC/AsdAZKAr2hJZsc2iq+WYIixeft7Qa1XwfKdrGWjgUEnMHYOk GMXcHojd7qeN2zUQNKiOeD9GTZU4WPKvX6eQP7/n/GpHKSxrg3sbcB5KmBzceuRRKAKb HNyTnHRAM5TzwXkQQjkFRAMJaEYIdyNlmc1zXPFddSKT5fdHRCGIgCFpizW4QY8SneHX R6KqbvO3grtaqOhD6EvmfhSfCeRP7h/SIPxyRpUqKHukPb8ay7nsNnQn7c0I8zQF+44+ U2vQ== X-Gm-Message-State: AOAM530A9m9Fl42gv5FScv4WvA5J7M8NGKA6AICp/d99KHWMi+kbKWsR cboIfRmoTKUO835WnzoPP3yNefr9XvI= X-Google-Smtp-Source: ABdhPJxQKakZhInqumPFOlwSumJKiK6ucg0l0QfoHYKOM1rT8EYKbXsmKKVvcPRcqN7jZ9d48kSwGg== X-Received: by 2002:a63:5a20:: with SMTP id o32mr3898939pgb.15.1594898812824; Thu, 16 Jul 2020 04:26:52 -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 g22sm4787135pgb.82.2020.07.16.04.26.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jul 2020 04:26:52 -0700 (PDT) Received: from gnu-cfl-2.localdomain (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id 5FD911A00CC for ; Thu, 16 Jul 2020 04:26:51 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH] nptl: Properly inline setgroups syscall [BZ #26248] Date: Thu, 16 Jul 2020 04:26:51 -0700 Message-Id: <20200716112651.2257283-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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" Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" nptl has /* Opcodes and data types for communication with the signal handler to change user/group IDs. */ struct xid_command { int syscall_no; long int id[3]; volatile int cntr; volatile int error; }; /* This must be last, otherwise the current thread might not have permissions to send SIGSETXID syscall to the other threads. */ result = INTERNAL_SYSCALL_NCS (cmdp->syscall_no, 3, cmdp->id[0], cmdp->id[1], cmdp->id[2]); But the second argument of setgroups syscal is a pointer: int setgroups(size_t size, const gid_t *list); But on x32, pointers passed to syscall must have pointer type so that they will be zero-extended. Add to define INTERNAL_SETXID_SYSCALL_NCS and use it, instead of INTERNAL_SYSCALL_NCS, for SETXID syscalls. X32 override it with pointer type for setgroups. A testcase is added and setgroups returned with EFAULT when running as root without the fix. --- nptl/allocatestack.c | 4 +- nptl/nptl-init.c | 4 +- sysdeps/nptl/setxid-internal.h | 21 +++++++ sysdeps/unix/sysv/linux/x86_64/x32/Makefile | 4 ++ .../sysv/linux/x86_64/x32/setxid-internal.h | 36 +++++++++++ .../sysv/linux/x86_64/x32/tst-setgroups.c | 62 +++++++++++++++++++ 6 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 sysdeps/nptl/setxid-internal.h create mode 100644 sysdeps/unix/sysv/linux/x86_64/x32/setxid-internal.h create mode 100644 sysdeps/unix/sysv/linux/x86_64/x32/tst-setgroups.c diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c index 4ae4b5a986..af5fc5f882 100644 --- a/nptl/allocatestack.c +++ b/nptl/allocatestack.c @@ -32,6 +32,7 @@ #include #include #include +#include #ifndef NEED_SEPARATE_REGISTER_STACK @@ -1159,8 +1160,7 @@ __nptl_setxid (struct xid_command *cmdp) /* This must be last, otherwise the current thread might not have permissions to send SIGSETXID syscall to the other threads. */ - result = INTERNAL_SYSCALL_NCS (cmdp->syscall_no, 3, - cmdp->id[0], cmdp->id[1], cmdp->id[2]); + result = INTERNAL_SETXID_SYSCALL_NCS (cmdp); int error = 0; if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (result))) { diff --git a/nptl/nptl-init.c b/nptl/nptl-init.c index 95c60a524a..80771e7788 100644 --- a/nptl/nptl-init.c +++ b/nptl/nptl-init.c @@ -39,6 +39,7 @@ #include #include #include +#include #ifndef TLS_MULTIPLE_THREADS_IN_TCB /* Pointer to the corresponding variable in libc. */ @@ -188,8 +189,7 @@ sighandler_setxid (int sig, siginfo_t *si, void *ctx) || si->si_code != SI_TKILL) return; - result = INTERNAL_SYSCALL_NCS (__xidcmd->syscall_no, 3, __xidcmd->id[0], - __xidcmd->id[1], __xidcmd->id[2]); + result = INTERNAL_SETXID_SYSCALL_NCS (__xidcmd); int error = 0; if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (result))) error = INTERNAL_SYSCALL_ERRNO (result); diff --git a/sysdeps/nptl/setxid-internal.h b/sysdeps/nptl/setxid-internal.h new file mode 100644 index 0000000000..d378b90db1 --- /dev/null +++ b/sysdeps/nptl/setxid-internal.h @@ -0,0 +1,21 @@ +/* INTERNAL_SETXID_SYSCALL_NCS. Generic 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 + . */ + +#define INTERNAL_SETXID_SYSCALL_NCS(cmd) \ + INTERNAL_SYSCALL_NCS (cmd->syscall_no, 3, cmd->id[0], cmd->id[1], \ + cmd->id[2]) diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/Makefile b/sysdeps/unix/sysv/linux/x86_64/x32/Makefile index 16b768d8ba..1a6c984f96 100644 --- a/sysdeps/unix/sysv/linux/x86_64/x32/Makefile +++ b/sysdeps/unix/sysv/linux/x86_64/x32/Makefile @@ -5,6 +5,10 @@ ifeq ($(subdir),misc) sysdep_routines += arch_prctl endif +ifeq ($(subdir),nptl) +xtests += tst-setgroups +endif + ifeq ($(subdir),conform) # For bugs 16437 and 21279. conformtest-xfail-conds += x86_64-x32-linux diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/setxid-internal.h b/sysdeps/unix/sysv/linux/x86_64/x32/setxid-internal.h new file mode 100644 index 0000000000..bed30ba040 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86_64/x32/setxid-internal.h @@ -0,0 +1,36 @@ +/* INTERNAL_SETXID_SYSCALL_NCS. 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 + . */ + +/* Enforce zero-extension for the pointer argument in + + int setgroups(size_t size, const gid_t *list); + + */ +#define INTERNAL_SETXID_SYSCALL_NCS(cmd) \ + ({ \ + int __result; \ + if (__glibc_unlikely (cmd->syscall_no == __NR_setgroups)) \ + __result = INTERNAL_SYSCALL_NCS (__NR_setgroups, 2, \ + cmd->id[0], \ + (void *) cmd->id[1]); \ + else \ + __result = INTERNAL_SYSCALL_NCS (cmd->syscall_no, 3, \ + cmd->id[0], cmd->id[1], \ + cmd->id[2]); \ + __result; \ + }) diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/tst-setgroups.c b/sysdeps/unix/sysv/linux/x86_64/x32/tst-setgroups.c new file mode 100644 index 0000000000..a7167b0e26 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86_64/x32/tst-setgroups.c @@ -0,0 +1,62 @@ +/* Basic test for setgroups + 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 +#include +#include +#include +#include +#include +#include + +static void * +start_routine (void *args) +{ + return NULL; +} + +static int +do_test (void) +{ + int size; + /* NB: Stack address is at 0xfffXXXXX. */ + gid_t list[NGROUPS_MAX]; + int status = EXIT_SUCCESS; + + pthread_t thread = xpthread_create (NULL, start_routine, NULL); + + size = getgroups (sizeof (list) / sizeof (list[0]), list); + if (size < 0) + { + status = EXIT_FAILURE; + error (0, errno, "getgroups failed"); + } + if (setgroups (size, list) < 0 && errno != EPERM) + { + status = EXIT_FAILURE; + error (0, errno, "setgroups failed"); + } + + xpthread_join (thread); + + return status; +} + +#include