From patchwork Mon Mar 14 05:06:15 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Lutomirski X-Patchwork-Id: 8576351 Return-Path: X-Original-To: patchwork-linux-fsdevel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id DAE3A9F6E1 for ; Mon, 14 Mar 2016 05:06:27 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0516320460 for ; Mon, 14 Mar 2016 05:06:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 074B02045E for ; Mon, 14 Mar 2016 05:06:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754554AbcCNFGW (ORCPT ); Mon, 14 Mar 2016 01:06:22 -0400 Received: from mail.kernel.org ([198.145.29.136]:60533 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753842AbcCNFGV (ORCPT ); Mon, 14 Mar 2016 01:06:21 -0400 Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 3076A2045A; Mon, 14 Mar 2016 05:06:20 +0000 (UTC) Received: from localhost (c-71-202-137-17.hsd1.ca.comcast.net [71.202.137.17]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1B1C7203DA; Mon, 14 Mar 2016 05:06:19 +0000 (UTC) From: Andy Lutomirski To: Linux FS Devel , , "Eric W. Biederman" Cc: gnome-os-list@gnome.org, James Bottomley , Andy Lutomirski , Alexander Larsson , mclasen@redhat.com, Linux Containers Subject: [PATCH] devpts: Make ptmx be owned by the userns owner instead of userns-local 0 Date: Sun, 13 Mar 2016 22:06:15 -0700 Message-Id: X-Mailer: git-send-email 2.5.0 X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP We used to have ptmx be owned by the inner uid and gid 0. Change this: if the owner and group are both mapped but are not both 0, then use the owner instead. For container-style namespaces (LXC, etc), this should have no effect -- UID 0 is will either be the owner or will be unmapped. The important behavior change is for sandboxes: many sandboxes intentionally do not create an inner uid 0. Without this patch, mounting devpts in such a sandbox is awkward. With this patch, it will just work and ptmx will be owned by the namespace owner. Cc: Alexander Larsson Cc: mclasen@redhat.com Cc: "Eric W. Biederman" Cc: Linux Containers Signed-off-by: Andy Lutomirski Tested-by: Alexander Larsson --- fs/devpts/inode.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/fs/devpts/inode.c b/fs/devpts/inode.c index 655f21f99160..d6fa2d1beee3 100644 --- a/fs/devpts/inode.c +++ b/fs/devpts/inode.c @@ -27,6 +27,7 @@ #include #include #include +#include #define DEVPTS_DEFAULT_MODE 0600 /* @@ -250,10 +251,35 @@ static int mknod_ptmx(struct super_block *sb) kuid_t root_uid; kgid_t root_gid; - root_uid = make_kuid(current_user_ns(), 0); - root_gid = make_kgid(current_user_ns(), 0); - if (!uid_valid(root_uid) || !gid_valid(root_gid)) - return -EINVAL; + /* + * For a new devpts instance, ptmx is owned by the creating user + * namespace's owner. Usually, that will be 0 as seen by the + * user namespace, but for unprivileged sandbox namespaces, + * there may not be a uid 0 or gid 0 at all. + */ + root_uid = current_user_ns()->owner; + root_gid = current_user_ns()->group; + + if (!uid_valid(root_uid) || !gid_valid(root_gid)) { + /* + * It's very unlikely for us to get here if the userns + * owner is not mapped, but it's possible -- we'd have + * to be running in the userns with capabilities granted + * by unshare or setns, since there is no inner + * privileged user. Nonetheless, this could happen, and + * we don't want ptmx to be owned by an unmapped user or + * group. + * + * If this happens fall back to historical behavior: + * try to have ptmx be owned by 0:0. + */ + root_uid = make_kuid(current_user_ns(), 0); + root_gid = make_kgid(current_user_ns(), 0); + + /* If this still doesn't work, give up. */ + if (!uid_valid(root_uid) || !gid_valid(root_gid)) + return -EINVAL; + } inode_lock(d_inode(root));