From patchwork Thu Sep 3 17:47:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Adalbert_Laz=C4=83r?= X-Patchwork-Id: 11754401 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8339A6CB for ; Thu, 3 Sep 2020 17:48:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 58E742072A for ; Thu, 3 Sep 2020 17:48:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 58E742072A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bitdefender.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id AC3CD900002; Thu, 3 Sep 2020 13:48:15 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A26118E0005; Thu, 3 Sep 2020 13:48:15 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C9BB900002; Thu, 3 Sep 2020 13:48:15 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0031.hostedemail.com [216.40.44.31]) by kanga.kvack.org (Postfix) with ESMTP id 640228E0005 for ; Thu, 3 Sep 2020 13:48:15 -0400 (EDT) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 22FD3824C739 for ; Thu, 3 Sep 2020 17:48:15 +0000 (UTC) X-FDA: 77222484150.08.grade42_3600b15270ab Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin08.hostedemail.com (Postfix) with ESMTP id EB8BA1819E764 for ; Thu, 3 Sep 2020 17:48:14 +0000 (UTC) X-Spam-Summary: 30,2,0,b8f7f754cbfef8c3,d41d8cd98f00b204,alazar@bitdefender.com,,RULES_HIT:2:41:152:341:355:364:379:800:960:973:982:988:989:1260:1261:1277:1311:1313:1314:1345:1359:1431:1437:1515:1516:1518:1535:1593:1594:1605:1606:1676:1730:1747:1777:1792:1801:1981:2194:2199:2393:2559:2562:2693:2901:2915:3138:3139:3140:3141:3142:3865:3866:3867:3868:3871:4037:4118:4321:4605:5007:6119:6120:6261:7576:7875:7903:7974:8660:9121:10004:11026:11473:11657:11658:11914:12043:12291:12296:12297:12438:12517:12519:12555:12679:12986:13148:13230:13894:14659:21080:21212:21324:21451:21627:21796:21939:21990:30003:30034:30036:30051:30054:30069,0,RBL:91.199.104.161:@bitdefender.com:.lbl8.mailshell.net-64.100.201.201 62.2.8.100;04yr9bwzx6rzikge1if4rgnmuf9j6op9et9b8skuuszskqzb54f84k8z5b9ofqz.9nkmx98wga9c3yosd9jckgd3jokgzcu93gghfsjhex8xytbwj7t3nudhsbw3ceg.4-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_ru les:0:0: X-HE-Tag: grade42_3600b15270ab X-Filterd-Recvd-Size: 7075 Received: from mx01.bbu.dsd.mx.bitdefender.com (mx01.bbu.dsd.mx.bitdefender.com [91.199.104.161]) by imf23.hostedemail.com (Postfix) with ESMTP for ; Thu, 3 Sep 2020 17:48:14 +0000 (UTC) Received: from smtp.bitdefender.com (smtp01.buh.bitdefender.com [10.17.80.75]) by mx01.bbu.dsd.mx.bitdefender.com (Postfix) with ESMTPS id C447F307C935; Thu, 3 Sep 2020 20:48:09 +0300 (EEST) Received: from localhost.localdomain (unknown [195.189.155.252]) by smtp.bitdefender.com (Postfix) with ESMTPSA id 9456F3072786; Thu, 3 Sep 2020 20:48:09 +0300 (EEST) From: =?utf-8?q?Adalbert_Laz=C4=83r?= To: linux-mm@kvack.org Cc: Andrew Morton , Alexander Graf , Stefan Hajnoczi , Jerome Glisse , Paolo Bonzini , Mircea Cirjaliu , Christian Brauner , =?utf-8?q?Adalbert_Laz=C4=83r?= Subject: [RFC PATCH 5/5] pidfd_mem: implemented remote memory mapping system call Date: Thu, 3 Sep 2020 20:47:30 +0300 Message-Id: <20200903174730.2685-6-alazar@bitdefender.com> In-Reply-To: <20200903174730.2685-1-alazar@bitdefender.com> References: <20200903174730.2685-1-alazar@bitdefender.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: EB8BA1819E764 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam04 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Mircea Cirjaliu This system call returns 2 fds for inspecting the address space of a remote process: one for control and one for access. Use according to remote mapping specifications. Cc: Christian Brauner Signed-off-by: Mircea Cirjaliu Signed-off-by: Adalbert Lazăr --- arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/pid.h | 1 + include/linux/syscalls.h | 1 + include/uapi/asm-generic/unistd.h | 2 + kernel/exit.c | 2 +- kernel/pid.c | 55 ++++++++++++++++++++++++++ 7 files changed, 62 insertions(+), 1 deletion(-) diff --git a/arch/x86/entry/syscalls/syscall_32.tbl b/arch/x86/entry/syscalls/syscall_32.tbl index 54581ac671b4..ca1b5a32dbc5 100644 --- a/arch/x86/entry/syscalls/syscall_32.tbl +++ b/arch/x86/entry/syscalls/syscall_32.tbl @@ -440,5 +440,6 @@ 433 i386 fspick sys_fspick 434 i386 pidfd_open sys_pidfd_open 435 i386 clone3 sys_clone3 +436 i386 pidfd_mem sys_pidfd_mem 437 i386 openat2 sys_openat2 438 i386 pidfd_getfd sys_pidfd_getfd diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl index 37b844f839bc..6138d3d023f8 100644 --- a/arch/x86/entry/syscalls/syscall_64.tbl +++ b/arch/x86/entry/syscalls/syscall_64.tbl @@ -357,6 +357,7 @@ 433 common fspick sys_fspick 434 common pidfd_open sys_pidfd_open 435 common clone3 sys_clone3 +436 common pidfd_mem sys_pidfd_mem 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd diff --git a/include/linux/pid.h b/include/linux/pid.h index cc896f0fc4e3..9ec23ab23fd4 100644 --- a/include/linux/pid.h +++ b/include/linux/pid.h @@ -76,6 +76,7 @@ extern const struct file_operations pidfd_fops; struct file; +extern struct pid *pidfd_get_pid(unsigned int fd); extern struct pid *pidfd_pid(const struct file *file); static inline struct pid *get_pid(struct pid *pid) diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index 1815065d52f3..621f3d52ed4e 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -934,6 +934,7 @@ asmlinkage long sys_clock_adjtime32(clockid_t which_clock, asmlinkage long sys_syncfs(int fd); asmlinkage long sys_setns(int fd, int nstype); asmlinkage long sys_pidfd_open(pid_t pid, unsigned int flags); +asmlinkage long sys_pidfd_mem(int pidfd, int __user *fds, unsigned int flags); asmlinkage long sys_sendmmsg(int fd, struct mmsghdr __user *msg, unsigned int vlen, unsigned flags); asmlinkage long sys_process_vm_readv(pid_t pid, diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h index 3a3201e4618e..2663afc03c86 100644 --- a/include/uapi/asm-generic/unistd.h +++ b/include/uapi/asm-generic/unistd.h @@ -850,6 +850,8 @@ __SYSCALL(__NR_pidfd_open, sys_pidfd_open) #define __NR_clone3 435 __SYSCALL(__NR_clone3, sys_clone3) #endif +#define __NR_pidfd_mem 436 +__SYSCALL(__NR_pidfd_mem, sys_pidfd_mem) #define __NR_openat2 437 __SYSCALL(__NR_openat2, sys_openat2) diff --git a/kernel/exit.c b/kernel/exit.c index 389a88cb3081..37cd8949e606 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -1464,7 +1464,7 @@ static long do_wait(struct wait_opts *wo) return retval; } -static struct pid *pidfd_get_pid(unsigned int fd) +struct pid *pidfd_get_pid(unsigned int fd) { struct fd f; struct pid *pid; diff --git a/kernel/pid.c b/kernel/pid.c index c835b844aca7..c9c49edf4a8a 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -42,6 +42,7 @@ #include #include #include +#include struct pid init_struct_pid = { .count = REFCOUNT_INIT(1), @@ -565,6 +566,60 @@ SYSCALL_DEFINE2(pidfd_open, pid_t, pid, unsigned int, flags) return fd; } +/** + * pidfd_mem() - Allow access to process address space. + * + * @pidfd: pid file descriptor for the target process + * @fds: array where the control and access file descriptors are returned + * @flags: flags to pass + * + * This creates a pair of file descriptors used to gain access to the + * target process memory. The control fd is used to establish a linear + * mapping between an offset range and a userspace address range. + * The access fd is used to mmap(offset range) on the client side. + * + * Return: On success, 0 is returned. + * On error, a negative errno number will be returned. + */ +SYSCALL_DEFINE3(pidfd_mem, int, pidfd, int __user *, fds, unsigned int, flags) +{ + struct pid *pid; + struct task_struct *task; + int ret_fds[2]; + int ret; + + if (pidfd < 0) + return -EINVAL; + if (!fds) + return -EINVAL; + if (flags) + return -EINVAL; + + pid = pidfd_get_pid(pidfd); + if (IS_ERR(pid)) + return PTR_ERR(pid); + + task = get_pid_task(pid, PIDTYPE_PID); + put_pid(pid); + if (IS_ERR(task)) + return PTR_ERR(task); + + ret = -EPERM; + if (unlikely(task == current) || capable(CAP_SYS_PTRACE)) + ret = task_remote_map(task, ret_fds); + put_task_struct(task); + if (IS_ERR_VALUE((long)ret)) + return ret; + + if (copy_to_user(fds, ret_fds, sizeof(ret_fds))) { + put_unused_fd(ret_fds[0]); + put_unused_fd(ret_fds[1]); + return -EFAULT; + } + + return 0; +} + void __init pid_idr_init(void) { /* Verify no one has done anything silly: */