From patchwork Tue Aug 16 22:33:15 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 659851 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3sDRxs2X5fz9t0X for ; Wed, 17 Aug 2016 08:34:57 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=xkHAnwQs; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753910AbcHPWeg (ORCPT ); Tue, 16 Aug 2016 18:34:36 -0400 Received: from mail-pa0-f65.google.com ([209.85.220.65]:33804 "EHLO mail-pa0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753522AbcHPWdW (ORCPT ); Tue, 16 Aug 2016 18:33:22 -0400 Received: by mail-pa0-f65.google.com with SMTP id hh10so5884782pac.1; Tue, 16 Aug 2016 15:33:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=r2LH7PX/4kkcOejg9IseOuIeJaOY5ygG+Pvy1QGkX/k=; b=xkHAnwQs/kwIOroC+87Hh0N7Itkx0J8Q5L3HxfN8/YPaTIfhCJxAhZa9JheMWS/241 gQBqx5kGlgSAKyJHV3ahDSMnXiWhCIzHM4zTd9H4KpLrcKRqcxcjMzXtc2lB8I9uPq8n 4VnmJU/72OfUxTvthU2Qa7VPO61QxHOTo39XiZVmCeMcn19o+CyS5n9dmhZ4F2IK3Tg2 zfFmw4pBozs7V4IvgdZ2FZ4Wqi1H5yx0Q0MyuKP/5E8FExgs5I1oQLDT3aRuGG61XtvQ /vOXS9R0086KV2iMxWTgjXjs1P/E1l1exVF5GoeExtbBPS6x+tII9XhBMuPHRfN15L5b jmNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=r2LH7PX/4kkcOejg9IseOuIeJaOY5ygG+Pvy1QGkX/k=; b=NgLtkmDrmvrsB24pXYVoYMOBC+rRLNyVwPHxpdgnwhgVefUHuuRZ+equB/ipb/y96v X6FRR4Qg2dDhoCVtth5Lph5PUl5yo/T7LkU5TUrkm9mfJxjPu+T9WTjWR+xajYWEWg52 md5twUrUo/SnlnulFHBDM3P7SApjyefdHQ5GGyVVUKWVRvDtEEjkLL4qkCAG+x0YUH4H lZKlbWR8iH+sjyFT2XTL4YypHFrUHCoBwwCrHbXF0lqe/mNEoN4Ro5Kpiuz5nCIHYy9b h8NWZWrnwDkAfRFrMb8EHbKKEvSgKAunLBr6SeA5z8mG/uCuvXy6W2wDKzy7Z/dB6ejy Modg== X-Gm-Message-State: AEkooutxVkB7Au+qSSWWB6JMD4QzHC56rpXYRQYKo/Fepf3OLc9CNK3qH0VqzhzdRedv7A== X-Received: by 10.66.233.38 with SMTP id tt6mr5600366pac.99.1471386801910; Tue, 16 Aug 2016 15:33:21 -0700 (PDT) Received: from dtor-ws.mtv.corp.google.com ([172.22.152.21]) by smtp.gmail.com with ESMTPSA id cp11sm41819736pac.28.2016.08.16.15.33.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 16 Aug 2016 15:33:21 -0700 (PDT) From: Dmitry Torokhov To: Tejun Heo , "Eric W. Biederman" , "David S. Miller" Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH 5/5] net-sysfs: make sure objects belong to contrainer's owner Date: Tue, 16 Aug 2016 15:33:15 -0700 Message-Id: <1471386795-32918-6-git-send-email-dmitry.torokhov@gmail.com> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 In-Reply-To: <1471386795-32918-1-git-send-email-dmitry.torokhov@gmail.com> References: <1471386795-32918-1-git-send-email-dmitry.torokhov@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org When creating various objects in /sys/class/net/... make sure that they belong to container's owner instead of global root (if they belong to a container/namespace). Signed-off-by: Dmitry Torokhov --- net/core/net-sysfs.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c index 7a0b616..ef997bb 100644 --- a/net/core/net-sysfs.c +++ b/net/core/net-sysfs.c @@ -882,11 +882,35 @@ static const void *rx_queue_namespace(struct kobject *kobj) return ns; } +static void net_ns_get_ownership(const struct net *net, + kuid_t *uid, kgid_t *gid) +{ + if (net) { + kuid_t ns_root_uid = make_kuid(net->user_ns, 0); + kgid_t ns_root_gid = make_kgid(net->user_ns, 0); + + if (uid_valid(ns_root_uid)) + *uid = ns_root_uid; + + if (gid_valid(ns_root_gid)) + *gid = ns_root_gid; + } +} + +static void rx_queue_get_ownership(struct kobject *kobj, + kuid_t *uid, kgid_t *gid) +{ + const struct net *net = rx_queue_namespace(kobj); + + net_ns_get_ownership(net, uid, gid); +} + static struct kobj_type rx_queue_ktype = { .sysfs_ops = &rx_queue_sysfs_ops, .release = rx_queue_release, .default_attrs = rx_queue_default_attrs, - .namespace = rx_queue_namespace + .namespace = rx_queue_namespace, + .get_ownership = rx_queue_get_ownership, }; static int rx_queue_add_kobject(struct net_device *dev, int index) @@ -1274,11 +1298,20 @@ static const void *netdev_queue_namespace(struct kobject *kobj) return ns; } +static void netdev_queue_get_ownership(struct kobject *kobj, + kuid_t *uid, kgid_t *gid) +{ + const struct net *net = netdev_queue_namespace(kobj); + + net_ns_get_ownership(net, uid, gid); +} + static struct kobj_type netdev_queue_ktype = { .sysfs_ops = &netdev_queue_sysfs_ops, .release = netdev_queue_release, .default_attrs = netdev_queue_default_attrs, .namespace = netdev_queue_namespace, + .get_ownership = netdev_queue_get_ownership, }; static int netdev_queue_add_kobject(struct net_device *dev, int index) @@ -1463,6 +1496,14 @@ static const void *net_namespace(struct device *d) return dev_net(dev); } +static void net_get_ownership(struct device *d, kuid_t *uid, kgid_t *gid) +{ + struct net_device *dev = to_net_dev(d); + const struct net *net = dev_net(dev); + + net_ns_get_ownership(net, uid, gid); +} + static struct class net_class = { .name = "net", .dev_release = netdev_release, @@ -1470,6 +1511,7 @@ static struct class net_class = { .dev_uevent = netdev_uevent, .ns_type = &net_ns_type_operations, .namespace = net_namespace, + .get_ownership = net_get_ownership, }; #ifdef CONFIG_OF_NET