From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964980AbbJ0SI6 (ORCPT ); Tue, 27 Oct 2015 14:08:58 -0400 Received: from mxf912.netcup.net ([46.38.249.18]:38690 "EHLO mxf912.netcup.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932543AbbJ0SI4 (ORCPT ); Tue, 27 Oct 2015 14:08:56 -0400 X-No-Relay: not in my network X-No-Relay: not in my network X-No-Relay: not in my network X-No-Relay: not in my network X-No-Relay: not in my network X-No-Relay: not in my network X-No-Relay: not in my network X-No-Relay: not in my network X-No-Relay: not in my network Date: Tue, 27 Oct 2015 19:08:31 +0100 From: Dirk Steinmetz To: Seth Forshee Cc: Alexander Viro , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, "Eric W. Biederman" , Andy Lutomirski , Kees Cook , Serge Hallyn , Dirk Steinmetz Subject: Re: [PATCH] namei: permit linking with CAP_FOWNER in userns Message-ID: <20151027190831.70f71671@rsjtdrjgfuzkfg.com> In-Reply-To: <20151027143344.GB132460@ubuntu-hedt> References: <1444489163-24266-1-git-send-email-public@rsjtdrjgfuzkfg.com> <1445350159-5489-1-git-send-email-public@rsjtdrjgfuzkfg.com> <20151027143344.GB132460@ubuntu-hedt> X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 27 Oct 2015 09:33:44 -0500, Seth Forshee wrote: > On Tue, Oct 20, 2015 at 04:09:19PM +0200, Dirk Steinmetz wrote: > > Attempting to hardlink to an unsafe file (e.g. a setuid binary) from > > within an unprivileged user namespace fails, even if CAP_FOWNER is held > > within the namespace. This may cause various failures, such as a gentoo > > installation within a lxc container failing to build and install specific > > packages. > > > > This change permits hardlinking of files owned by mapped uids, if > > CAP_FOWNER is held for that namespace. Furthermore, it improves consistency > > by using the existing inode_owner_or_capable(), which is aware of > > namespaced capabilities as of 23adbe12ef7d3 ("fs,userns: Change > > inode_capable to capable_wrt_inode_uidgid"). > > > > Signed-off-by: Dirk Steinmetz > > Tested-by: Seth Forshee > > This is hitting us in Ubuntu during some dpkg upgrades in containers. > When upgrading a file dpkg creates a hard link to the old file to back > it up before overwriting it. When packages upgrade suid files owned by a > non-root user the link isn't permitted, and the package upgrade fails. > This patch fixes our problem. > > I did want to point what seems to be an inconsistency in how > capabilities in user namespaces are handled with respect to inodes. When > I started looking at this my initial thought was to replace > capable(CAP_FOWNER) with capable_wrt_inode_uidgid(inode, CAP_FOWNER). On > the face of it this should be equivalent to what's done here, but it > turns out that capable_wrt_inode_uidgid requires that the inode's uid > and gid are both mapped into the namespace whereas > inode_owner_or_capable only requires the uid be mapped. I'm not sure how > significant that is, but it seems a bit odd. I agree that this seems odd. I've chosen inode_owner_or_capable over capable_wrt_inode_uidgid(inode, CAP_FOWNER) as it seemed consistent: a privileged user (with CAP_SETUID) can impersonate the owner UID and thus bypass the check completely; this also matches the documented behavior of CAP_FOWNER: "Bypass permission checks on operations that normally require the filesystem UID of the process to match the UID of the file". However, thinking about it I get the feeling that checking the gid seems reasonable as well. This is, however, independently of user namespaces. Consider the following scenario in any namespace, including the init one: - A file has the setgid and user/group executable bits set, and is owned by user:group. - The user 'user' is not in the group 'group', and does not have any capabilities. - The user 'user' hardlinks the file. The permission check will succeed, as the user is the owner of the file. - The file is replaced with a newer version (for example fixing a security issue) - Now user can still use the hardlink-pinned version to execute the file as 'user:group' (and for example exploit the security issue). I would have expected the user to not be able to hardlink, as he lacks CAP_FSETID, and thus is not allowed to chmod, change or move the file without loosing the setgid bit. So it is impossible for him to make a non- hardlink copy with the setgid bit set -- why should he be able to make a hardlinked one? It seems to me as if may_linkat would additionally require a check verifying that either - not both setgid and group executable bit set - fsgid == owner gid - capable_wrt_inode_uidgid(CAP_FSETID) -- or CAP_FOWNER, depending on whether to adapt chmod's behavior or keeping everything hardlink- related in CAP_FOWNER; I don't feel qualified enough to pick ;) This would change documented behavior (at least man proc.5's description of /proc/sys/fs/protected_hardlinks), and I'd consider it a separate issue, if any (as I'm unsure how realistic that scenario is). I'd appreciate comments on that. For other situations than setgid-executable files I do not see issues with not checking the group id's mapping, as linking would be permitted without privileges outside of the user namespace (disregarding namespace-internal setuid bits). Independently of that, it might be reasonable to consider switching inode_owner_or_capable towards checking the gid as well and define something along "uid checks in user namespaces with uid/gid maps require the file's uid and gid to be mapped, else they will fail" for consistency. Dirk