Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp6911774pxb; Wed, 17 Feb 2021 17:45:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJxG3vTzTg/CPyLzn8EnN7ByWX7xKSp8JNuQem6T0NG66VGnjeSMVexRWYFyOTl0Oyk7zFEo X-Received: by 2002:aa7:c044:: with SMTP id k4mr1627578edo.47.1613612714053; Wed, 17 Feb 2021 17:45:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613612714; cv=none; d=google.com; s=arc-20160816; b=VN0ziq/rb582bV2kAzmH+Rl30aIqb/YTUqLyBRY3NiiO7Pt3e5YQdWIPBrLbsmrrhV QJzvWQXrMD5nkzNGv+QoeejUwDNCVTwy+LoJo4fW4KCsPW+sn7wd1zHxPVNF5+v7EuF8 LBNaR+vkJGG1FidD29BHzTVOOj+lFYTvDqcGPoccamvjY8lK8GjNUP4avymxmzNkBqLq 8HFikiv423TmO32Z7j+4JzXxFanqTIAezhmWMvrYfY4HNvD4uGwFXpH8CC358yYDcBVR fjFx15hBp3oqhb3lJL4A64Clbj8iBhtsXWFArkiHLQqpVG/6tGljlDaG9+gvmkJBS6te PTGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date; bh=VWHJPUz7Gq67rvA4nCI5ZvkZ7TKucjFSIw6c60+ibeE=; b=zaoYgzy33PKAte3Yp5WpF58OOq7ZMq6fBht/+BdIC1aPJIJI8UpgCsvzqenwktZZ8V SdeTICDZKiE75jrs4V9YA1qFA8ma9KhCIVak7wqzJRHyFsbYcL6B23Sp2vKVjWM0neo0 KVuwN6oS6l3kYwaiogHyIR3KRbuDn+FeexuCglaG+2pLjq4nUU4kibXlDRF05Wyb2Ffj KIyrVz94Hz1hRrSYzo+TrjwQzJpELOPBc/pI6028svZjYU5lfbifGgE6yDQ+A7qLrKoE es3iV8ps51ivJZ7FcOQqG88N9vViQVVi4f3tP5suArwezWH09NTVd3sO/qxR3UZT6cdq wNmA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j16si2388694edv.27.2021.02.17.17.44.50; Wed, 17 Feb 2021 17:45:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230213AbhBRBmB (ORCPT + 99 others); Wed, 17 Feb 2021 20:42:01 -0500 Received: from vmicros1.altlinux.org ([194.107.17.57]:56218 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230162AbhBRBl5 (ORCPT ); Wed, 17 Feb 2021 20:41:57 -0500 Received: from mua.local.altlinux.org (mua.local.altlinux.org [192.168.1.14]) by vmicros1.altlinux.org (Postfix) with ESMTP id 1BCF372C8B1; Thu, 18 Feb 2021 04:41:14 +0300 (MSK) Received: by mua.local.altlinux.org (Postfix, from userid 508) id 0D9777CC8A2; Thu, 18 Feb 2021 04:41:14 +0300 (MSK) Date: Wed, 17 Feb 2021 08:00:00 +0000 From: "Dmitry V. Levin" To: "David S. Miller" Cc: "Eric W. Biederman" , Alexey Gladkov , Gleb Fotengauer-Malinovskiy , Anatoly Pugachev , sparclinux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] sparc: make copy_thread honor pid namespaces Message-ID: <20210217080000.GA25861@altlinux.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On sparc, fork and clone syscalls have an unusual semantics of returning the pid of the parent process to the child process. Apparently, the implementation did not honor pid namespaces at all, so the child used to get the pid of its parent in the init namespace. This bug was found by strace test suite. Reproducer: $ gcc -Wall -O2 -xc - <<'EOF' #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include int main(void) { if (unshare(CLONE_NEWPID | CLONE_NEWUSER) < 0) err(1, "unshare"); int pid = syscall(__NR_fork); if (pid < 0) err(1, "fork"); fprintf(stderr, "current: %d, parent: %d, fork returned: %d\n", getpid(), getppid(), pid); int status; if (wait(&status) < 0) { if (errno == ECHILD) _exit(0); err(1, "wait"); } return !WIFEXITED(status) || WEXITSTATUS(status) != 0; } EOF $ sh -c ./a.out current: 10001, parent: 10000, fork returned: 10002 current: 1, parent: 0, fork returned: 10001 Cc: Eric W. Biederman Cc: stable@vger.kernel.org Signed-off-by: Dmitry V. Levin --- Although the fix seems to be obvious, I have no means to test it myself, so any help with the testing is much appreciated. arch/sparc/kernel/process_32.c | 2 +- arch/sparc/kernel/process_64.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/sparc/kernel/process_32.c b/arch/sparc/kernel/process_32.c index a02363735915..7a89969befa8 100644 --- a/arch/sparc/kernel/process_32.c +++ b/arch/sparc/kernel/process_32.c @@ -368,7 +368,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg, #endif /* Set the return value for the child. */ - childregs->u_regs[UREG_I0] = current->pid; + childregs->u_regs[UREG_I0] = task_pid_nr_ns(current, task_active_pid_ns(p)); childregs->u_regs[UREG_I1] = 1; /* Set the return value for the parent. */ diff --git a/arch/sparc/kernel/process_64.c b/arch/sparc/kernel/process_64.c index 6f8c7822fc06..ec97217ab970 100644 --- a/arch/sparc/kernel/process_64.c +++ b/arch/sparc/kernel/process_64.c @@ -629,7 +629,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg, t->utraps[0]++; /* Set the return value for the child. */ - t->kregs->u_regs[UREG_I0] = current->pid; + t->kregs->u_regs[UREG_I0] = task_pid_nr_ns(current, task_active_pid_ns(p)); t->kregs->u_regs[UREG_I1] = 1; /* Set the second return value for the parent. */ -- ldv