From patchwork Fri Jul 15 02:12:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Vagin X-Patchwork-Id: 9231037 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 262C560574 for ; Fri, 15 Jul 2016 02:13:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 152B028308 for ; Fri, 15 Jul 2016 02:13:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0A07228326; Fri, 15 Jul 2016 02:13:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B3BB928308 for ; Fri, 15 Jul 2016 02:13:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751923AbcGOCMh (ORCPT ); Thu, 14 Jul 2016 22:12:37 -0400 Received: from mail-pa0-f68.google.com ([209.85.220.68]:35965 "EHLO mail-pa0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751459AbcGOCMg (ORCPT ); Thu, 14 Jul 2016 22:12:36 -0400 Received: by mail-pa0-f68.google.com with SMTP id ez1so1980027pab.3; Thu, 14 Jul 2016 19:12:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=8mVll9nLqH1yjFKNL920Wn59mHyu4jbL7ibo+2OHxjg=; b=OHEnX0NeufFhjseDYqC9TuFz8SOdzjgITIBT6H5pSdSq93TNhPSdV5J0K7oQvYvx7C mnn7jRdhO30mwJ2C/M8KVLoBBx8X5FG5yoiMtuE0vfpEQY0PjoWNvPeqKhN6hLzYV5zf FvWBCRAAxBFy6GU6tcjIUNMYC/nezFgQV8gpP2RzSfAS9IeSiu6GQ3yQ3dKnzxC9DVX3 3Ji1ajzav2xqE2hh10YSTZBqQCQxxcUmtS+vJNqsl7cWk0a7CPVFPbV4d2DghrsnrLu8 Wa5oroWUpsbAZjAx0fRThL8p0hA8bX4Rp/XBTA5UZJbP8tOkMR61YnKPa8V147KBb6Ah BCzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=8mVll9nLqH1yjFKNL920Wn59mHyu4jbL7ibo+2OHxjg=; b=bswP3RS7wUsNLF/VmsIlydw2ArllVGpbHP79f6dIFSp5cuhmnltwntcprrDPoRC4es wKKjQxkXrdwgVcBcNHz2NRBi8HwXgFO0ExS128rV0NuTcPDN7fY+61oCJg/MTQSveQPF bc+1IkodhyJBPldau1ERjaHSlzs33+CvgGkfB4Z/7DJFqLn/UdKnDT3T8896FXwZGaLK Luki3KyAoavYxadMmLTQpCOfFVTCgfgLrVmCRP70ekX1ZYW8xntIxRwP1pfhrDFPcXfB o16kRqmf86xwy1VdgBf6ym5jK2yiKGYmvYS6eQZCoskyv97xzmXR1ViFGYlh7B5zKCq+ oyNw== X-Gm-Message-State: ALyK8tJuDdfxMxVzR38sbNGS2wd9lRNSFp726vUEfww0YO3FSuuEnuLZE8KI0oCRtPFMZg== X-Received: by 10.66.233.103 with SMTP id tv7mr27901745pac.46.1468548755233; Thu, 14 Jul 2016 19:12:35 -0700 (PDT) Received: from laptop.localdomain (c-67-183-159-197.hsd1.wa.comcast.net. [67.183.159.197]) by smtp.gmail.com with ESMTPSA id by5sm7559894pad.36.2016.07.14.19.12.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Jul 2016 19:12:34 -0700 (PDT) From: Andrey Vagin To: linux-kernel@vger.kernel.org Cc: linux-api@vger.kernel.org, containers@lists.linux-foundation.org, criu@openvz.org, linux-fsdevel@vger.kernel.org, "Eric W. Biederman" , James Bottomley , "Michael Kerrisk (man-pages)" , "W. Trevor King" , Alexander Viro , Serge Hallyn , Andrey Vagin Subject: [PATCH 2/5] kernel: add a helper to get an owning user namespace for a namespace Date: Thu, 14 Jul 2016 19:12:19 -0700 Message-Id: <1468548742-32136-2-git-send-email-avagin@openvz.org> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1468548742-32136-1-git-send-email-avagin@openvz.org> References: <1468548742-32136-1-git-send-email-avagin@openvz.org> 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 Return -EPERM if an owning user namespace is outside of a process current user namespace. Signed-off-by: Andrey Vagin --- include/linux/user_namespace.h | 7 +++++++ kernel/user_namespace.c | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/linux/user_namespace.h b/include/linux/user_namespace.h index a941b44..e416b76 100644 --- a/include/linux/user_namespace.h +++ b/include/linux/user_namespace.h @@ -76,6 +76,8 @@ extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *); extern int proc_setgroups_show(struct seq_file *m, void *v); extern bool userns_may_setgroups(const struct user_namespace *ns); + +struct ns_common *ns_get_owner(struct ns_common *ns); #else static inline struct user_namespace *get_user_ns(struct user_namespace *ns) @@ -104,6 +106,11 @@ static inline bool userns_may_setgroups(const struct user_namespace *ns) { return true; } + +static inline struct ns_common *ns_get_owner(struct ns_common *ns) +{ + return ERR_PTR(-ENOENT); +} #endif #endif /* _LINUX_USER_H */ diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index a5bc78c..6382e5e 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c @@ -994,6 +994,30 @@ static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns) return commit_creds(cred); } +struct ns_common *ns_get_owner(struct ns_common *ns) +{ + const struct cred *cred = current_cred(); + struct user_namespace *user_ns, *p; + + user_ns = p = ns->user_ns; + if (user_ns == NULL) { /* ns is init_user_ns */ + /* Unprivileged user should not know that it's init_user_ns. */ + if (capable(CAP_SYS_ADMIN)) + return ERR_PTR(-ENOENT); + return ERR_PTR(-EPERM); + } + + for (;;) { + if (p == cred->user_ns) + break; + if (p == &init_user_ns) + return ERR_PTR(-EPERM); + p = p->parent; + } + + return &get_user_ns(user_ns)->ns; +} + const struct proc_ns_operations userns_operations = { .name = "user", .type = CLONE_NEWUSER,