Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp467097pxy; Thu, 22 Apr 2021 06:22:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzOC6TImaSq4gBGYyG87SdiO+t3Gj+A8iA5fkG6xgIMnUfHZkpxIk+TDJWF0Rs6O7ceENPz X-Received: by 2002:a17:906:194a:: with SMTP id b10mr3356736eje.431.1619097731393; Thu, 22 Apr 2021 06:22:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619097731; cv=none; d=google.com; s=arc-20160816; b=tX/bRRrW0yjPorQBEZxLemtx2Vtc5diRd51iadMyeGjljf8YbfZ6UbRgd3jEZKJ1EU XrjsFPI3aB84xeVsp4I+wNw54rj+FeOVhwnhj7MZpeDKLawvbb1XEQNAu6+dOZEkkhmK gV180bzYO7KbhulEnEA4GYMKIbSg7VPqoUkM7YRja8eRrkLDwslbTEVTNx/B6u3081fW wqEf+brxA46cf8dFo4pjIXdy7JBCKDt/QaORWrvZbRiyRhflOjOfNb2Q54RMzCGy2xK4 bGUJ1w4gtWUFdxermXJQbFlc0858DbiLfa91pPBg+JpDe4tK9rDsmKG1tFrv3iGopjGm ORxg== 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=J2Z/oPXtOfgPXvrfORkdqZ7PlT08lQyvYtZbR5QJ29k=; b=syjvgTubufdON5Y9fZYONWQHHO1dX4VqYOKNS/xN4yAgmaGAPv4J1Ce/mjasnqYZM/ Pd+7jWAsvXZZybNRdnfUx4qp5mmR3yjnz1i+7B4YF6GwdFgym2ExE6synjKiMoJHSqu6 Gah6xrOxjKT+EoELPx3O7BunSO4IAoNiGeVV63oGHV3CC/mSFH9nJ1aQNfK64WW9NBQT YutBXPkm4WwRBpEnHslz8G6Fv3A/vPva31xw8uUcHRtI8Gr0WDr+ZDjN0wtmuSuvWy45 UHlXLqR/ctww1exxAIc0qV9IpDlRGCBGoqVwe4JHrQBOmOURSYu4F1qB3/xcPI3mBcQP cMbg== 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 o24si2211852edr.300.2021.04.22.06.21.46; Thu, 22 Apr 2021 06:22:11 -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 S236144AbhDVNVZ (ORCPT + 99 others); Thu, 22 Apr 2021 09:21:25 -0400 Received: from mail.hallyn.com ([178.63.66.53]:39666 "EHLO mail.hallyn.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235830AbhDVNVY (ORCPT ); Thu, 22 Apr 2021 09:21:24 -0400 Received: by mail.hallyn.com (Postfix, from userid 1001) id 47600BEE; Thu, 22 Apr 2021 08:20:48 -0500 (CDT) Date: Thu, 22 Apr 2021 08:20:48 -0500 From: "Serge E. Hallyn" To: "Eric W. Biederman" Cc: "Serge E. Hallyn" , Christian Brauner , lkml , Linus Torvalds , Kees Cook , "Andrew G. Morgan" , Greg Kroah-Hartman , security@kernel.org, Tycho Andersen , Andy Lutomirski Subject: Re: [PATCH v3.4] capabilities: require CAP_SETFCAP to map uid 0 Message-ID: <20210422132048.GA25068@mail.hallyn.com> References: <20210416150501.zam55gschpn2w56i@wittgenstein> <20210416213453.GA29094@mail.hallyn.com> <20210417021945.GA687@mail.hallyn.com> <20210417200434.GA17430@mail.hallyn.com> <20210419122514.GA20598@mail.hallyn.com> <20210419160911.5pguvpj7kfuj6rnr@wittgenstein> <20210420034208.GA2830@mail.hallyn.com> <20210420083129.exyn7ptahx2fg72e@wittgenstein> <20210420134334.GA11582@mail.hallyn.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 21, 2021 at 02:16:34PM -0500, Eric W. Biederman wrote: > "Serge E. Hallyn" writes: > > > +/** > > + * verify_root_map() - check the uid 0 mapping > > + * @file: idmapping file > > + * @map_ns: user namespace of the target process > > + * @new_map: requested idmap > > + * > > + * If a process requests mapping parent uid 0 into the new ns, verify that the > > + * process writing the map had the CAP_SETFCAP capability as the target process > > + * will be able to write fscaps that are valid in ancestor user namespaces. > > + * > > + * Return: true if the mapping is allowed, false if not. > > + */ > > +static bool verify_root_map(const struct file *file, > > + struct user_namespace *map_ns, > > + struct uid_gid_map *new_map) > > +{ > > + int idx; > > + const struct user_namespace *file_ns = file->f_cred->user_ns; > > + struct uid_gid_extent *extent0 = NULL; > > + > > + for (idx = 0; idx < new_map->nr_extents; idx++) { > > + if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) > > + extent0 = &new_map->extent[idx]; > > + else > > + extent0 = &new_map->forward[idx]; > > + if (extent0->lower_first == 0) > > + break; > > + > > + extent0 = NULL; > > + } > > + > > + if (!extent0) > > + return true; > > + > > + if (map_ns == file_ns) { > > + /* The process unshared its ns and is writing to its own > > + * /proc/self/uid_map. User already has full capabilites in > > + * the new namespace. Verify that the parent had CAP_SETFCAP > > + * when it unshared. > > + * */ > > + if (!file_ns->parent_could_setfcap) > > + return false; > > + } else { > > + /* Process p1 is writing to uid_map of p2, who is in a child > > + * user namespace to p1's. Verify that the opener of the map > > + * file has CAP_SETFCAP against the parent of the new map > > + * namespace */ > > + if (!file_ns_capable(file, map_ns->parent, CAP_SETFCAP)) > > + return false; > > + } > > Is there any reason this permission check is not simply: > > return map_ns->parent_could_setfcap || > file_ns_capable(file, map_ns->parent, CAP_SETFCAP); > > That is why don't we allow any mapping (that is otherwise valid) in user > namespaces whose creator had the permission to call CAP_SETFCAP? Well I guess the question is exactly who has to have the privilege. If task X does the unshare and its sibling task Y writes the mapping (technically, opens the map file), do we want to say that it suffices for *either* X or Y to have CAP_SETFCAP? I was thinking we want to require task Y to have the privilege. Task X having it would not suffice. > Why limit the case of using the creators permissions to only the case of > mapping just a single uid (that happens to be the current euid) in the > user namespace? > > I don't see any safety reasons for the map_ns == file_ns test. > > > > Is the file_ns_capable check for CAP_SETFCAP actually needed? AKA could > the permission check be simplified to: > > return map_ns->parent_could_setfcap; Currently uid 1000 can create a new user namespace, then have a fully privileged root process in uid 1000's peer userns write a 0 mapping. With just this check, that would not be possible. > That would be a much easier rule to explain to people. > > I seem to remember distributions at least trying to make newuidmap have > just CAP_SETUID and newgidmap have just CAP_SETGID. Such a simplified > check would facilitate that. Yes they would have to add an additional CAP_SETFCAP.