Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp669233ybc; Tue, 12 Nov 2019 07:28:39 -0800 (PST) X-Google-Smtp-Source: APXvYqwj88rHFxBYxpvNMgQ4Y5JfXQHUWZqKR2zLiA4AG7qrNJg1cHspFWX9hDojQWvBWvvzl1Ps X-Received: by 2002:a17:906:b5b:: with SMTP id v27mr28559573ejg.327.1573572519287; Tue, 12 Nov 2019 07:28:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573572519; cv=none; d=google.com; s=arc-20160816; b=eco+s7kjKiWCmHZ9pa3PV3sv2brnTGcId/2KWdHU3kfvb/KXpd1jTyDSs6R8XMEU4H uQyJRIurOAHODWiHWJGY9mH/W+CIP4E4Bv0YLtmNlWpF34cLbLzK9i4CAvUzVnpEXawQ IbB1WuO84/iyMh1k2l7daCvKNw9dDUvxk/S9g3/nnNIueCpCDn9fesLE7ewXmXXw3BR6 Eotw/RuUNR6UAuxTyjfi+auXMD2rYXaw3HdaxVrQ6V04rWPQjbTmfWveXjqh9KbU8aqX Tk2eL482jffUUchmd5g8STd9+nxvvW0No+1D4Y+Y5LiuzQ4wBIuDVIP8yCu/Ykz8tnMT 4bwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:user-agent:organization:in-reply-to :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=dJGkv89SxqX+D6V/I+khNNj8XzVZdQi06iL4/RI2mAU=; b=QY6ZYtpVN/IU6Sbv7vl+LydgHTZIeRCtyA58iOWXgm9BFJYfeQJD80lkbsudsdm02v ec1iSdVGkIlmEn9tXsmDaG4yRd/m1K66L5FhzGUiO6LPNXObtigyocg82DHwrHqsos20 ZKgCtoyncPluFuVJRP6rHPLmRjtHOymhijtrej3A7q/DIFGAnhSBg0XUY+MpPBKqIfV7 +8b75g/wYvH1Jg8Y4AQeuw/htPZEySivbmBc7VQO1qw3CZ7tyZvye5peUDOulxucBY+L 4H5ehID7liCHNorHhuaRxlEFcK9gJuZlgJwfAoQfWQznW3JN8xDxz5HHx6Rrkntx2uNd 6kUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PoZxIbne; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id jo17si159264ejb.312.2019.11.12.07.28.15; Tue, 12 Nov 2019 07:28:39 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=PoZxIbne; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727283AbfKLP1F (ORCPT + 99 others); Tue, 12 Nov 2019 10:27:05 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:25887 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727152AbfKLP1E (ORCPT ); Tue, 12 Nov 2019 10:27:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573572423; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dJGkv89SxqX+D6V/I+khNNj8XzVZdQi06iL4/RI2mAU=; b=PoZxIbne6VW4pGent/BL1W/qrN2CQC6qMJwhnLeTBLxLbl4AinW2of+9AA8FY7CS/0TBa6 u+Vb2Tb7bOMZ7NulKieNGqi5CB+4frzoos48TbNOh8cUCiKxLMgRoGfl5Oh9Mqr/Qu4vO/ MJrq8S2bxeV987Voat5RVnyOuUPs2mc= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-169-Ug5OLg3tNsGq-Dn0yDu9cQ-1; Tue, 12 Nov 2019 10:27:00 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E1DF78C5C8E; Tue, 12 Nov 2019 15:26:58 +0000 (UTC) Received: from dcbz.redhat.com (ovpn-116-65.ams2.redhat.com [10.36.116.65]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5D9B05E1AF; Tue, 12 Nov 2019 15:26:54 +0000 (UTC) Date: Tue, 12 Nov 2019 16:26:51 +0100 From: Adrian Reber To: Rasmus Villemoes Cc: Christian Brauner , Eric Biederman , Pavel Emelyanov , Jann Horn , Oleg Nesterov , Dmitry Safonov <0x7f454c46@gmail.com>, linux-kernel@vger.kernel.org, Andrei Vagin , Mike Rapoport , Radostin Stoyanov Subject: Re: [PATCH v7 1/2] fork: extend clone3() to support setting a PID Message-ID: <20191112152651.GB667444@dcbz.redhat.com> References: <20191111131704.656169-1-areber@redhat.com> MIME-Version: 1.0 In-Reply-To: X-Operating-System: Linux (5.3.8-300.fc31.x86_64) X-Load-Average: 3.47 3.27 2.34 X-Unexpected: The Spanish Inquisition X-GnuPG-Key: gpg --recv-keys D3C4906A Organization: Red Hat User-Agent: Mutt/1.12.1 (2019-06-15) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-MC-Unique: Ug5OLg3tNsGq-Dn0yDu9cQ-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Nov 11, 2019 at 09:41:39PM +0100, Rasmus Villemoes wrote: > On 11/11/2019 14.17, Adrian Reber wrote: > > The main motivation to add set_tid to clone3() is CRIU. > >=20 > > To restore a process with the same PID/TID CRIU currently uses > > /proc/sys/kernel/ns_last_pid. It writes the desired (PID - 1) to > > ns_last_pid and then (quickly) does a clone(). This works most of the > > time, but it is racy. It is also slow as it requires multiple syscalls. > >=20 > > Extending clone3() to support *set_tid makes it possible restore a > > process using CRIU without accessing /proc/sys/kernel/ns_last_pid and > > race free (as long as the desired PID/TID is available). > >=20 > > This clone3() extension places the same restrictions (CAP_SYS_ADMIN) > > on clone3() with *set_tid as they are currently in place for ns_last_pi= d. > >=20 > > The original version of this change was using a single value for > > set_tid. At the 2019 LPC, after presenting set_tid, it was, however, > > decided to change set_tid to an array to enable setting the PID of a > > process in multiple PID namespaces at the same time. If a process is > > created in a PID namespace it is possible to influence the PID inside > > and outside of the PID namespace. Details also in the corresponding > > selftest. > >=20 >=20 > > =09/* > > =09 * Verify that higher 32bits of exit_signal are unset and that > > =09 * it is a valid signal > > @@ -2556,8 +2561,17 @@ noinline static int copy_clone_args_from_user(st= ruct kernel_clone_args *kargs, > > =09=09.stack=09=09=3D args.stack, > > =09=09.stack_size=09=3D args.stack_size, > > =09=09.tls=09=09=3D args.tls, > > +=09=09.set_tid=09=3D kargs->set_tid, > > +=09=09.set_tid_size=09=3D args.set_tid_size, > > =09}; >=20 > This is a bit ugly. And is it even well-defined? I mean, it's a bit > similar to the "i =3D i++;". So it would be best to avoid. >=20 > > +=09for (i =3D 0; i < args.set_tid_size; i++) { > > +=09=09if (copy_from_user(&kargs->set_tid[i], > > +=09=09 u64_to_user_ptr(args.set_tid + (i * sizeof(args.set_tid))), > > +=09=09 sizeof(pid_t))) > > +=09=09=09return -EFAULT; > > +=09} > > + >=20 > If I'm reading this (and your test case) right, you expect the user > pointer to point at an array of u64, and here you're copying the first > half of each u64 to the pid_t array. That only works on little-endian. >=20 > It seems more obvious (since I don't think there's any disagreement > anywhere on sizeof(pid_t)) to expect the user pointer to point at an > array of pid_t and then simply copy_from_user() the whole thing in one go= . >=20 > > =09return 0; > > } > > =20 > > @@ -2631,6 +2645,10 @@ SYSCALL_DEFINE2(clone3, struct clone_args __user= *, uargs, size_t, size) > > =09int err; > > =20 > > =09struct kernel_clone_args kargs; > > +=09pid_t set_tid[MAX_PID_NS_LEVEL]; > > + > > +=09memset(set_tid, 0, sizeof(set_tid)); > > +=09kargs.set_tid =3D set_tid; >=20 > Hm, isn't it a bit much to add two cachelines (and dirtying them via the > memset) to the stack footprint of clone3, considering that almost nobody > (relatively speaking) will use this? >=20 > So how about copy_clone_args_from_user() does >=20 > if (args.set_tid) { > set_tid =3D memdup_user(u64_to_user_ptr(), ...) > if (IS_ERR(set_tid)) > return PTR_ERR(set_tid); > kargs.set_tid =3D set_tid; > } >=20 > Then somebody needs to free that, but this is probably not the last > clone extension that might need extra data, so one could do >=20 > s/long _do_fork/static long __do_fork/ >=20 > and then create a _do_fork that always cleans up the passed-in kargs, i.e= . >=20 > long _do_fork(struct kargs *args) > { > long ret =3D __do_fork(args); > kfree(args->set_tid); > return ret; > } Thanks for your review. Did you had a look at what Christian suggested? That should solve most of the points you mentioned. I will also remove the memset() as it is not necessary at all. =09=09Adrian