Received: by 10.213.65.68 with SMTP id h4csp2825388imn; Mon, 2 Apr 2018 14:59:25 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/omeCpYD0aT6kjJzCkMYdXvII2oQ2U32a4PVWdeA4Xyv6abuVsb0PO0xkKkMNrEup3yNvq X-Received: by 10.101.100.9 with SMTP id a9mr7305712pgv.209.1522706365363; Mon, 02 Apr 2018 14:59:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522706365; cv=none; d=google.com; s=arc-20160816; b=UsEHZ4E04N3vUuQDzyPApuTOgixdjFYrivcum7rzgm3OzpKmleToilZwf6iwfFGlhI UwvBjRkabvf68tJke4MLLpvnzjIiWSnB2uq5XoaScFXrQikw97uUfeZgmZ81jONG42G8 a1OUb2MewAwKho8cvIfwA3Dahx3xxCkl+TcJcuR+dnbikrYZbo5nzqHtdvyDWpmwkAm+ JMdHBzhuRs3YTfU4KIxwgD94q3Uhg7i6MKNUv4pd2ydP86Q754ITVVzzzIPuhbznbwHJ yfk7xR7E9vJ40GbfYcV2ekA18UGfT5Kppfgw94ZQYqmQahC2NB2hKwT0ZIioeWxL0XZn C+FA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=2SJyc3Cjefc+sUQXkicHTGTszOIY0mkE7X+rIjH280U=; b=sSxjNkTjCJYrmPEQ7mpyuvJJXlHRgQFqIlnJ3i61BPCr2TI6F9Yh+E8RrYjqug1lqb qqgIadsk/7vEei69vefEoTz/dGqW4W531kPEdoBkxk9OmOY76/xEX0I5Xwb/TaO97Isy 1DILTxAsoiP79VqqhQYp9IvtfYuXohxXntGk73jLuKuMejybWAjQ6iDGTairpllc1Nb8 X0S6k2Y5XooWueB7oH7YkJo6BYmmFZVyxFVzYK8ksOsuAV0QjEN+N65+h4PylQ6B9Ax2 Mu8hj9W1/IYhTMFbP36Cn9TKrZ0FHylDoFhiJCokNPcd6ReeSRyIVrIiVjPABXjP21XE 1BKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=asQ63RrQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c23-v6si1167558plo.80.2018.04.02.14.59.11; Mon, 02 Apr 2018 14:59:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=asQ63RrQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754556AbeDBV5z (ORCPT + 99 others); Mon, 2 Apr 2018 17:57:55 -0400 Received: from userp2130.oracle.com ([156.151.31.86]:45016 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754512AbeDBV5x (ORCPT ); Mon, 2 Apr 2018 17:57:53 -0400 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w32LkwlZ090659; Mon, 2 Apr 2018 21:57:45 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id; s=corp-2017-10-26; bh=2SJyc3Cjefc+sUQXkicHTGTszOIY0mkE7X+rIjH280U=; b=asQ63RrQE9O40sP3hfBqJKQfHrY73obvAUBhsah5otC0MbRlAmFD+ArUqf7h5vkMijRj A1EcC9kXIHtE6Q3pwb4pcDYWqpaSuLr9qeSlo841be83nHj7sEAqwdAR1IkJ/dJ+zTot MVYnSaMVXLF+OY9B3fqOLAXgfznuq0IRBErveIKP2UBbQajyZ+vGBcUWmS2kmBkn6WVe Y+w3I4uLmnCZpNcEQlekX6wHL7thEdWLUpg/MDYXNisgn0gr75sUTZCqdLFOhgYp4ZHF lwqc9VytacL1vGYW3wDuPQnxqg3fmPB02q0XIyIBc3xunNPbD09ap/+9tejnyHc7fQyO Ag== Received: from userv0021.oracle.com (userv0021.oracle.com [156.151.31.71]) by userp2130.oracle.com with ESMTP id 2h3vqv011t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 02 Apr 2018 21:57:45 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w32LvjZJ021125 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 2 Apr 2018 21:57:45 GMT Received: from abhmp0006.oracle.com (abhmp0006.oracle.com [141.146.116.12]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w32Lvh0f001740; Mon, 2 Apr 2018 21:57:44 GMT Received: from brm-x4170-02.us.oracle.com (/10.80.150.91) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 02 Apr 2018 14:57:43 -0700 From: nagarathnam.muthusamy@oracle.com To: linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, ebiederm@xmission.com, khlebnikov@yandex-team.ru Cc: akpm@linux-foundation.org, serge.hallyn@ubuntu.com, oleg@redhat.com, luto@amacapital.net, jannh@google.com, nagarathnam.muthusamy@oracle.com, prakash.sangappa@oracle.com Subject: [RESEND PATCH V4] pidns: introduce syscall translate_pid Date: Mon, 2 Apr 2018 15:57:29 -0600 Message-Id: <1522706249-11578-1-git-send-email-nagarathnam.muthusamy@oracle.com> X-Mailer: git-send-email 1.8.3.1 X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8851 signatures=668697 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=45 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1711220000 definitions=main-1804020229 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org pid_t translate_pid(pid_t pid, int source, int target); This syscall converts pid from source pid-ns into pid in target pid-ns. If pid is unreachable from target pid-ns it returns zero. Pid-namespaces are referred file descriptors opened to proc files /proc/[pid]/ns/pid or /proc/[pid]/ns/pid_for_children. Negative argument refers to current pid namespace, same as file /proc/self/ns/pid. Kernel expose virtual pids in /proc/[pid]/status:NSpid, but backward translation requires scanning all tasks. Also pids could be translated by sending them through unix socket between namespaces, this method is slow and insecure because other side is exposed inside pid namespace. Examples: translate_pid(pid, ns, -1) - get pid in our pid namespace translate_pid(pid, -1, ns) - get pid in other pid namespace translate_pid(1, ns, -1) - get pid of init task for namespace translate_pid(pid, -1, ns) > 0 - is pid is reachable from ns? translate_pid(1, ns1, ns2) > 0 - is ns1 inside ns2? translate_pid(1, ns1, ns2) == 0 - is ns1 outside ns2? translate_pid(1, ns1, ns2) == 1 - is ns1 equal ns2? Error codes: EBADF - file descriptor is closed EINVAL - file descriptor isn't pid-namespace ESRCH - task not found in @source namespace Signed-off-by: Konstantin Khlebnikov Signed-off-by: Nagarathnam Muthusamy --- v1: https://lkml.org/lkml/2015/9/15/411 v2: https://lkml.org/lkml/2015/9/24/278 * use namespace-fd as second/third argument * add -pid for getting parent pid * move code into kernel/sys.c next to getppid * drop ifdef CONFIG_PID_NS * add generic syscall v3: https://lkml.org/lkml/2015/9/28/3 * use proc_ns_fdget() * update description * rebase to next-20150925 * fix conflict with mlock2 v4: * rename into translate_pid() * remove syscall if CONFIG_PID_NS=n * drop -pid for parent task * drop fget-fdget optimizations * add helper get_pid_ns_by_fd() * wire only into x86 --- arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/syscalls.h | 1 + kernel/pid_namespace.c | 66 ++++++++++++++++++++++++++++++++++ kernel/sys_ni.c | 3 ++ 5 files changed, 72 insertions(+) diff --git a/arch/x86/entry/syscalls/syscall_32.tbl b/arch/x86/entry/syscalls/syscall_32.tbl index 448ac21..257d839 100644 --- a/arch/x86/entry/syscalls/syscall_32.tbl +++ b/arch/x86/entry/syscalls/syscall_32.tbl @@ -391,3 +391,4 @@ 382 i386 pkey_free sys_pkey_free 383 i386 statx sys_statx 384 i386 arch_prctl sys_arch_prctl compat_sys_arch_prctl +385 i386 translate_pid sys_translate_pid diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl index 5aef183..1ebdab8 100644 --- a/arch/x86/entry/syscalls/syscall_64.tbl +++ b/arch/x86/entry/syscalls/syscall_64.tbl @@ -339,6 +339,7 @@ 330 common pkey_alloc sys_pkey_alloc 331 common pkey_free sys_pkey_free 332 common statx sys_statx +333 common translate_pid sys_translate_pid # # x32-specific system call numbers start at 512 to avoid cache impact diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index a78186d..6467ebc 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -901,6 +901,7 @@ asmlinkage long sys_open_by_handle_at(int mountdirfd, struct file_handle __user *handle, int flags); asmlinkage long sys_setns(int fd, int nstype); +asmlinkage long sys_translate_pid(pid_t pid, int source, int target); asmlinkage long sys_process_vm_readv(pid_t pid, const struct iovec __user *lvec, unsigned long liovcnt, diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c index 773b2b3..bb56a78 100644 --- a/kernel/pid_namespace.c +++ b/kernel/pid_namespace.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -380,6 +381,71 @@ static void pidns_put(struct ns_common *ns) put_pid_ns(to_pid_ns(ns)); } +static struct pid_namespace *get_pid_ns_by_fd(int fd) +{ + struct pid_namespace *pidns; + struct ns_common *ns; + struct file *file; + + file = proc_ns_fget(fd); + if (IS_ERR(file)) + return ERR_CAST(file); + + ns = get_proc_ns(file_inode(file)); + if (ns->ops->type == CLONE_NEWPID) + pidns = get_pid_ns(to_pid_ns(ns)); + else + pidns = ERR_PTR(-EINVAL); + + fput(file); + return pidns; +} + +/* + * translate_pid - convert pid in source pid-ns into target pid-ns. + * @pid: pid for translation + * @source: pid-ns file descriptor or -1 for active namespace + * @target: pid-ns file descriptor or -1 for active namesapce + * + * Returns pid in @target pid-ns, zero if task have no pid there, + * or -ESRCH if task with @pid does not found in @source pid-ns. + */ +SYSCALL_DEFINE3(translate_pid, pid_t, pid, int, source, int, target) +{ + struct pid_namespace *source_ns, *target_ns; + struct pid *struct_pid; + pid_t result; + + if (source >= 0) { + source_ns = get_pid_ns_by_fd(source); + result = PTR_ERR(source_ns); + if (IS_ERR(source_ns)) + goto err_source; + } else + source_ns = task_active_pid_ns(current); + + if (target >= 0) { + target_ns = get_pid_ns_by_fd(target); + result = PTR_ERR(target_ns); + if (IS_ERR(target_ns)) + goto err_target; + } else + target_ns = task_active_pid_ns(current); + + rcu_read_lock(); + struct_pid = find_pid_ns(pid, source_ns); + result = struct_pid ? pid_nr_ns(struct_pid, target_ns) : -ESRCH; + rcu_read_unlock(); + + if (target >= 0) + put_pid_ns(target_ns); +err_target: + if (source >= 0) + put_pid_ns(source_ns); +err_source: + return result; +} + static int pidns_install(struct nsproxy *nsproxy, struct ns_common *ns) { struct pid_namespace *active = task_active_pid_ns(current); diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c index b518976..bf6ef46 100644 --- a/kernel/sys_ni.c +++ b/kernel/sys_ni.c @@ -259,3 +259,6 @@ asmlinkage long sys_ni_syscall(void) cond_syscall(sys_pkey_mprotect); cond_syscall(sys_pkey_alloc); cond_syscall(sys_pkey_free); + +/* pid namespace */ +cond_syscall(sys_translate_pid); -- 1.8.3.1