aboutsummaryrefslogtreecommitdiffstats
path: root/net/core/net_namespace.c
diff options
context:
space:
mode:
authorTyler Hicks <tyhicks@canonical.com>2018-07-20 17:56:53 -0400
committerDavid S. Miller <davem@davemloft.net>2018-07-21 02:44:36 -0400
commitfbdeaed408cf2728c62640c10848ddb1b67e63d3 (patch)
tree4c06f4a43d4516e7f1f83db877f4f4c49b01077f /net/core/net_namespace.c
parentb0e37c0d8a6abed0cd1b611314a7ebf50b0a8ed4 (diff)
net: create reusable function for getting ownership info of sysfs inodes
Make net_ns_get_ownership() reusable by networking code outside of core. This is useful, for example, to allow bridge related sysfs files to be owned by container root. Add a function comment since this is a potentially dangerous function to use given the way that kobject_get_ownership() works by initializing uid and gid before calling .get_ownership(). Signed-off-by: Tyler Hicks <tyhicks@canonical.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/core/net_namespace.c')
-rw-r--r--net/core/net_namespace.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c
index a11e03f920d3..738871af5efa 100644
--- a/net/core/net_namespace.c
+++ b/net/core/net_namespace.c
@@ -17,6 +17,7 @@
17#include <linux/user_namespace.h> 17#include <linux/user_namespace.h>
18#include <linux/net_namespace.h> 18#include <linux/net_namespace.h>
19#include <linux/sched/task.h> 19#include <linux/sched/task.h>
20#include <linux/uidgid.h>
20 21
21#include <net/sock.h> 22#include <net/sock.h>
22#include <net/netlink.h> 23#include <net/netlink.h>
@@ -448,6 +449,33 @@ dec_ucounts:
448 return net; 449 return net;
449} 450}
450 451
452/**
453 * net_ns_get_ownership - get sysfs ownership data for @net
454 * @net: network namespace in question (can be NULL)
455 * @uid: kernel user ID for sysfs objects
456 * @gid: kernel group ID for sysfs objects
457 *
458 * Returns the uid/gid pair of root in the user namespace associated with the
459 * given network namespace.
460 */
461void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid)
462{
463 if (net) {
464 kuid_t ns_root_uid = make_kuid(net->user_ns, 0);
465 kgid_t ns_root_gid = make_kgid(net->user_ns, 0);
466
467 if (uid_valid(ns_root_uid))
468 *uid = ns_root_uid;
469
470 if (gid_valid(ns_root_gid))
471 *gid = ns_root_gid;
472 } else {
473 *uid = GLOBAL_ROOT_UID;
474 *gid = GLOBAL_ROOT_GID;
475 }
476}
477EXPORT_SYMBOL_GPL(net_ns_get_ownership);
478
451static void unhash_nsid(struct net *net, struct net *last) 479static void unhash_nsid(struct net *net, struct net *last)
452{ 480{
453 struct net *tmp; 481 struct net *tmp;