Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1030478AbdD0MdJ (ORCPT ); Thu, 27 Apr 2017 08:33:09 -0400 Received: from mail-eopbgr30090.outbound.protection.outlook.com ([40.107.3.90]:62145 "EHLO EUR03-AM5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S965424AbdD0Mc5 (ORCPT ); Thu, 27 Apr 2017 08:32:57 -0400 Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=virtuozzo.com; Subject: [PATCH v3] pid_ns: Introduce ioctl to set vector of ns_last_pid's on ns hierarhy From: Kirill Tkhai To: , , , , , , , , , , , , , , , , , Date: Thu, 27 Apr 2017 15:32:46 +0300 Message-ID: <149329634856.21195.14196911999722279118.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: DB6PR1001CA0041.EURPRD10.PROD.OUTLOOK.COM (10.168.69.155) To HE1PR0802MB2284.eurprd08.prod.outlook.com (10.172.127.14) X-MS-Office365-Filtering-Correlation-Id: 744f8a67-6e41-4b92-2791-08d48d6984fa X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(201703131423075)(201703031133081);SRVR:HE1PR0802MB2284; X-Microsoft-Exchange-Diagnostics: 1;HE1PR0802MB2284;3:tBU2y64ijl5bPTHYZ9CIo8coX35VwPHV9TXAPb4QF3DUkOWbT4WOKFQ4wbU7Ekf5KEGDrI24DZzRFS2xT7/nd9Qy7gXDdBdcq5oeCmzV0PbOsmGjy7clsiaA6pPOb3otloFoUpJHINcXNdiZt9MF7kGiSSs/VncP4v7UsWA4spaCPC+hQuoaT8GbbOs+bFSwRgWCLCq6W/xD7+BsGp5D35aCdIP3cV4IXrIcosNBWfpIPkDHMORqukdLdtg8vIibj4I3B1udFulKLn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cM7uh547OftW4a1hPM/SAFbD1gThXH9tThuOOF5jtASp/+MBuDeeHVW0uM+OH85pV+WBOMBCkM0CYrK0C2wO3pqD80m6EpPqFNf2MFdx1Lf0S9mv7tahNzlsJV9ZH1nCjwN/Dm5KJovHkmT3z5LusoCoHIBeVZLJepJor5+karQS0/oYEVU= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(601004)(2401047)(5005006)(8121501046)(10201501046)(3002001)(93006095)(93001095)(6041248)(20161123560025)(20161123555025)(20161123562025)(201703131423075)(201702281528075)(201703061421075)(20161123564025)(6072148);SRVR:HE1PR0802MB2284;BCL:0;PCL:0;RULEID:;SRVR:HE1PR0802MB2284; X-Microsoft-Exchange-Diagnostics: 1;HE1PR0802MB2284;4:/70i7oio2EinNstecIxcsdlGWhRylPkim+wGQI+lOffpb/AdC7FLavF6BgsVk3OfDfjgkH0n8A4QtThbUR86L97l+RN+wKAFjjerwPG2FtTMOZRxnYcABbvbyX1UGMJQVsznmUlgsUz6JRgnkryWiZLzRyPMfDekMMFm3t3EtG4Pui8JjWmRFUfzP/eggVOit96yKlSvSl1WjnzCju0b0oOVI56Z2ho+/JJperEELVwLSufM2RbURNYMqaWIOjNdosNXNHJ0oLciQ8vo4Pq6Lxk16u4ol3bjjdPI3uny6SW77BZ1Vs9g3kcvVYx7aXC/CgIokrG/heNQWJl8XrLqV9pMamZCNvd3Gh1h04rFiycYbRIkRZ2NIdLmjvoVlQ77ABB2uOQtD0P9mHaxqlndESGiN6XzQJAtNNIMOwV+Rxlkqw9CmIm5c1xmNheFkaUWAJNAKrv0ar4kR/ZC1PIFLHg4I+lS3WT3obm3jLsn5SmFLe1qL6PMwBtFosqe/LxgpWRiIMF+ep3L/fKPJuJ+XoWN/yEQNAXt5uRg9k+mmz+tagc0sD3VO3lGhNdaDg9PF4HGpF5NG7Rx9EM7cHXm19TBJhJdaR8TWaXKO411Nm3ppKF7zOfy8M+RBo3BBRQqwucl0mnR5G8b4M/tSf59BkyCAhpE8hEyfl+/5Dk9putnkyaW4nT3Bb3P7WaFLgt31OBMRmbXEJ464G/Ji0jrmA== X-Forefront-PRVS: 029097202E X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(4630300001)(6069001)(6009001)(39410400002)(39400400002)(39840400002)(39450400003)(103116003)(54356999)(50986999)(2906002)(2201001)(42186005)(66066001)(230700001)(47776003)(50466002)(86362001)(3846002)(61506002)(575784001)(81166006)(6116002)(8676002)(38730400002)(33646002)(7416002)(53936002)(305945005)(9686003)(6506006)(55016002)(189998001)(25786009)(5660300001)(23676002)(921003)(1121003);DIR:OUT;SFP:1102;SCL:1;SRVR:HE1PR0802MB2284;H:localhost.localdomain;FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtIRTFQUjA4MDJNQjIyODQ7MjM6Q0ZMQWd0NGFLY3RhRzVuYVBuMFNYVnNN?= =?utf-8?B?MUQ4ZE5tVmQxNEZmTkNjNDlqdjhYeW1aZ2ZOYzRTYk01Vjh0R3RLNmRub05r?= =?utf-8?B?bkhLMGF4L3kwRDlWVFY0d3BzdEpnOEYrdStqUDB3TE1LbE5MRHpIUkJNc3BL?= =?utf-8?B?Sm9oMHUxQXkzUjY0SW0vRFB4ZnVQT204dzlHOVFGUlVscVprd3g1Ky9MdkZJ?= =?utf-8?B?Q2kvbUZDSWtTcGlKME01czhVcHpGUmZmbkZlS0RhYnNnSkFqU0JVVnQwcWt2?= =?utf-8?B?cFVQVW1WOEdmU0VZaHQ2dWcxdWxoVDJCREJpeUQ2dWdmZmp1bFB0V0diOExO?= =?utf-8?B?NThzdmhXclp5NnNQWW5uWkpURUd2QlJGUm42Z2RqdFF0ZlIxMFRWUGh6Y0VJ?= =?utf-8?B?bnQyNDQwcXVVUmVWMEFlUjNFRWRMMzd4Sk1rYnJYNFVPR2E4MHRLYnNkTUFT?= =?utf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VHvNCnkLJ1NrDHOJjQ9SpnolyB3t+B3lI0y4kk3kY9lG/OgV8IfSShmNu4+zXzhnj0Oi6FOqT5RfH1F19RGeS3/hC37jG9XXYisb/wFzPnJ1SbiF0IMecrZ05Dpm95gSUoIXqRvMYciTGXhp2/cSdpS91IvU7YfFD0GRPzX1zp6Hqj5Iw== X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Apr 2017 12:32:48.5662 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0802MB2284 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 5715 Lines: 177 On implementing of nested pid namespaces support in CRIU (checkpoint-restore in userspace tool) we run into the situation, that it's impossible to create a task with specific NSpid effectively. After commit 49f4d8b93ccf "pidns: Capture the user namespace and filter ns_last_pid" it is impossible to set ns_last_pid on any pid namespace, except task's active pid_ns (before the commit it was possible to write to pid_ns_for_children). Thus, if a restored task in a container has more than one pid_ns levels, the restorer code must have a task helper for every pid namespace of the task's pid_ns hierarhy. This is a big problem, because of communication with a helper for every pid_ns in the hierarchy is not cheap. It's not performance-good as it implies many helpers wakeups to create a single task (independently, how you communicate with the helpers). This patch tries to decide the problem. It introduces a new pid_ns ioctl(NS_SET_LAST_PID_VEC), which allows to write a vector of last pids on pid_ns hierarchy. The vector is passed as array of pids in struct ns_ioc_pid_vec, written in reverse order. The first number corresponds to the opened namespace ns_last_pid, the second is to its parent, etc. So, if you have the pid namespaces hierarchy like: pid_ns1 (grand father) | v pid_ns2 (father) | v pid_ns3 (child) and the pid_ns3 is open, then the corresponding vector will be {last_ns_pid3, last_ns_pid2, last_ns_pid1}. This vector may be short and it may contain less levels. For example, {last_ns_pid3, last_ns_pid2} or even {last_ns_pid3}, in dependence of which levels you want to populate. v3: Use __u32 in uapi instead of unsigned int. v2: Kill pid_ns->child_reaper check as it's impossible to have such a pid namespace file open. Use generic namespaces ioctl() number. Pass pids as array, not as a string. Signed-off-by: Kirill Tkhai --- fs/nsfs.c | 5 +++++ include/linux/pid_namespace.h | 12 ++++++++++++ include/uapi/linux/nsfs.h | 7 +++++++ kernel/pid_namespace.c | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) diff --git a/fs/nsfs.c b/fs/nsfs.c index 323f492e0822..f669a1552003 100644 --- a/fs/nsfs.c +++ b/fs/nsfs.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -186,6 +187,10 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, argp = (uid_t __user *) arg; uid = from_kuid_munged(current_user_ns(), user_ns->owner); return put_user(uid, argp); + case NS_SET_LAST_PID_VEC: + if (ns->ops->type != CLONE_NEWPID) + return -EINVAL; + return pidns_set_last_pid_vec(ns, (void *)arg); default: return -ENOTTY; } diff --git a/include/linux/pid_namespace.h b/include/linux/pid_namespace.h index c2a989dee876..c8dc4173a4e8 100644 --- a/include/linux/pid_namespace.h +++ b/include/linux/pid_namespace.h @@ -9,6 +9,7 @@ #include #include #include +#include struct pidmap { atomic_t nr_free; @@ -103,6 +104,17 @@ static inline int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd) } #endif /* CONFIG_PID_NS */ +#if defined(CONFIG_PID_NS) && defined(CONFIG_CHECKPOINT_RESTORE) +extern long pidns_set_last_pid_vec(struct ns_common *ns, + struct ns_ioc_pid_vec __user *vec); +#else /* CONFIG_PID_NS && CONFIG_CHECKPOINT_RESTORE */ +static inline long pidns_set_last_pid_vec(struct ns_common *ns, + struct ns_ioc_pid_vec __user *vec) +{ + return -ENOTTY; +} +#endif /* CONFIG_PID_NS && CONFIG_CHECKPOINT_RESTORE */ + extern struct pid_namespace *task_active_pid_ns(struct task_struct *tsk); void pidhash_init(void); void pidmap_init(void); diff --git a/include/uapi/linux/nsfs.h b/include/uapi/linux/nsfs.h index 1a3ca79f466b..1254b02a47fa 100644 --- a/include/uapi/linux/nsfs.h +++ b/include/uapi/linux/nsfs.h @@ -14,5 +14,12 @@ #define NS_GET_NSTYPE _IO(NSIO, 0x3) /* Get owner UID (in the caller's user namespace) for a user namespace */ #define NS_GET_OWNER_UID _IO(NSIO, 0x4) +/* Set a vector of ns_last_pid for a pid namespace stack */ +#define NS_SET_LAST_PID_VEC _IO(NSIO, 0x5) + +struct ns_ioc_pid_vec { + __u32 nr; + pid_t pid[0]; +}; #endif /* __LINUX_NSFS_H */ diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c index de461aa0bf9a..08b5fef23534 100644 --- a/kernel/pid_namespace.c +++ b/kernel/pid_namespace.c @@ -21,6 +21,7 @@ #include #include #include +#include struct pid_cache { int nr_ids; @@ -428,6 +429,40 @@ static struct ns_common *pidns_get_parent(struct ns_common *ns) return &get_pid_ns(pid_ns)->ns; } +#ifdef CONFIG_CHECKPOINT_RESTORE +long pidns_set_last_pid_vec(struct ns_common *ns, + struct ns_ioc_pid_vec __user *vec) +{ + struct pid_namespace *pid_ns = to_pid_ns(ns); + pid_t pid, __user *pid_ptr; + u32 nr; + + if (get_user(nr, &vec->nr)) + return -EFAULT; + if (nr > 32 || nr < 1) + return -EINVAL; + + pid_ptr = &vec->pid[0]; + do { + if (!ns_capable(pid_ns->user_ns, CAP_SYS_ADMIN)) + return -EPERM; + + if (get_user(pid, pid_ptr)) + return -EFAULT; + if (pid < 0 || pid > pid_max) + return -EINVAL; + + /* Write directly: see the comment in pid_ns_ctl_handler() */ + pid_ns->last_pid = pid; + + pid_ns = pid_ns->parent; + pid_ptr++; + } while (--nr > 0 && pid_ns); + + return nr ? -EINVAL : 0; +} +#endif /* CONFIG_CHECKPOINT_RESTORE */ + static struct user_namespace *pidns_owner(struct ns_common *ns) { return to_pid_ns(ns)->user_ns;