Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1326905pxf; Fri, 2 Apr 2021 07:33:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzg9giHDOFORFQc6/DczIBGPxtvwpBbkeLEj++fA5EQIL5sqNWHcImk0uyrz2/NhbO7wKmh X-Received: by 2002:aa7:c3c1:: with SMTP id l1mr15983377edr.208.1617373986245; Fri, 02 Apr 2021 07:33:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617373986; cv=none; d=google.com; s=arc-20160816; b=z6rTFerEkN0B+aH0Ch4p+pr+QjTv3OdnwKrci/RhNqif9HU3D0cvH1xPner1hrx/2/ mt0epW8K/BLl3oPXIQ4mbcibK0iR/vOpohf1Nupv09Xr71DnrXRD4Q7X2/+Mdu6nmYrQ 77FxJ4/SzvJHhpVsNEZNWrLeB11098tjV3uk1/R5fGYDFwEJpvdbcaj3ed1y0EQuFTfD 3Obq1DGSwxaUFuRmgfpEtZALnjj1osg8lcCvnChgoxy0pRIs4YAGqUSbgZSganY64a0q 47GuXbKGBhf6FoqoA19fWxt61r3kkMhsqWtr9Folp7MCexqy5K+mwtF6oU8f90HibVCt FSuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=o+Hfm0Zen8fdzE8du/u8HwKfEzA0Qs5tPYDZelUP7T8=; b=Wixo/3F9XpiErrTDtfAko51LhsLmFaFNHFRRHPOZwQXCvhJ97OlS0kDTVi3AJZh+75 ECixisO44Ka41eU4tmr+5vpBG328rqESZvC9PKtstIDFh6oOVa8NXWVHJKfYISBnTM/Q FcOiGE80ToNxmUmZ52+mpkViuPQH8gAzhCUrqDgwhqM56Y8TjXWjJEGLP2uh1+pCruRN /ymeXdPAi2J03rzKtlSih81mMmgg7FVH7Yf1bB88CgUiaWaaDOrtRqaQ1cdpFighDLH0 bWEXljhwFXn2LQtTFKJlKcyzIzpY7Wip/zLZeg02bvuh3q0Pg8hsRElQB4wPYpvPbw4Y 39CQ== 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 38si7143318edq.52.2021.04.02.07.32.42; Fri, 02 Apr 2021 07:33:06 -0700 (PDT) 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 S235696AbhDBOcR (ORCPT + 99 others); Fri, 2 Apr 2021 10:32:17 -0400 Received: from mail.hallyn.com ([178.63.66.53]:35770 "EHLO mail.hallyn.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229932AbhDBOcP (ORCPT ); Fri, 2 Apr 2021 10:32:15 -0400 Received: by mail.hallyn.com (Postfix, from userid 1001) id 93A94CD0; Fri, 2 Apr 2021 09:32:12 -0500 (CDT) Date: Fri, 2 Apr 2021 09:32:12 -0500 From: "Serge E. Hallyn" To: Giuseppe Scrivano Cc: "Eric W. Biederman" , linux-kernel@vger.kernel.org, christian.brauner@ubuntu.com, serge@hallyn.com, Linux Containers Subject: Re: [PATCH] kernel: automatically split user namespace extent Message-ID: <20210402143212.GA18282@mail.hallyn.com> References: <20201126100839.381415-1-gscrivan@redhat.com> <87ft4pe7km.fsf@x220.int.ebiederm.org> <87pn3schlg.fsf@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87pn3schlg.fsf@redhat.com> User-Agent: Mutt/1.9.4 (2018-02-28) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Dec 02, 2020 at 05:12:27PM +0100, Giuseppe Scrivano wrote: > Hi Eric, > > ebiederm@xmission.com (Eric W. Biederman) writes: > > > Nit: The tag should have been "userns:" rather than kernel. > > > > Giuseppe Scrivano writes: > > > >> writing to the id map fails when an extent overlaps multiple mappings > >> in the parent user namespace, e.g.: > >> > >> $ cat /proc/self/uid_map > >> 0 1000 1 > >> 1 100000 65536 > >> $ unshare -U sleep 100 & > >> [1] 1029703 > >> $ printf "0 0 100\n" | tee /proc/$!/uid_map > >> 0 0 100 > >> tee: /proc/1029703/uid_map: Operation not permitted > >> > >> To prevent it from happening, automatically split an extent so that > >> each portion fits in one extent in the parent user namespace. > > > > I don't see anything fundamentally wrong with relaxing this > > restriction, but more code does have more room for bugs to hide. > > > > What is the advantage of relaxing this restriction? > > we are running rootless containers in a namespace created with > newuidmap/newgidmap where the mappings look like: > > $ cat /proc/self/uid_map > 0 1000 1 > 1 110000 65536 > > users are allowed to create child user namespaces and specify the > mappings to use. Doing so, they often hit the issue that the mappings > cannot overlap multiple extents in the parent user namespace. > > The issue could be completely addressed in user space, but to me it > looks like an implementation detail that user space should not know > about. > In addition, it would also be slower (additional read of the current > uid_map and gid_map files) and must be implemented separately in each > container runtime. > > >> $ cat /proc/self/uid_map > >> 0 1000 1 > >> 1 110000 65536 > >> $ unshare -U sleep 100 & > >> [1] 1552 > >> $ printf "0 0 100\n" | tee /proc/$!/uid_map > >> 0 0 100 > >> $ cat /proc/$!/uid_map > >> 0 0 1 > >> 1 1 99 > >> > >> Signed-off-by: Giuseppe Scrivano > >> --- > >> kernel/user_namespace.c | 62 ++++++++++++++++++++++++++++++++++------- > >> 1 file changed, 52 insertions(+), 10 deletions(-) > >> > >> diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c > >> index 87804e0371fe..b5542be2bd0a 100644 > >> --- a/kernel/user_namespace.c > >> +++ b/kernel/user_namespace.c > >> @@ -706,6 +706,41 @@ const struct seq_operations proc_projid_seq_operations = { > >> .show = projid_m_show, > >> }; > >> > >> +static void split_overlapping_mappings(struct uid_gid_map *parent_map, > >> + struct uid_gid_extent *extent, > >> + struct uid_gid_extent *overflow_extent) > >> +{ > >> + unsigned int idx; > >> + > >> + overflow_extent->first = (u32) -1; > >> + > >> + /* Split extent if it not fully contained in an extent from parent_map. */ > >> + for (idx = 0; idx < parent_map->nr_extents; idx++) { > > > > Ouch! > > > > For the larger tree we perform binary searches typically and > > here you are walking every entry unconditionally. > > > > It looks like this makes the write O(N^2) from O(NlogN) > > which for a user facing function is not desirable. > > > > I think something like insert_and_split_extent may be ok. > > Incorporating your loop and the part that inserts an element. > > > > As written this almost doubles the complexity of the code, > > as well as making it perform much worse. Which is a problem. > > I've attempted to implement the new functionality at input validation > time to not touch the existing security checks. > > I've thought the pattern for iterating the extents was fine as I've > taken it from mappings_overlap (even if it is used differently on an > unsorted array). > > Thanks for the hint, I'll move the new logic when map_id_range_down() is > used and I'll send a v2. Hi, sorry if I miseed it. Did you ever send a v2?