#include <linux/slab.h>
#include <linux/security.h>
#include <linux/hash.h>
+#include <linux/ns_common.h>
#include "kernfs-internal.h"
return parent;
}
+/*
+ * kernfs_ns_id - return the namespace id for a given namespace
+ * @ns: namespace tag (may be NULL)
+ *
+ * Use the 64-bit namespace id instead of raw pointers for hashing
+ * and comparison to avoid leaking kernel addresses to userspace.
+ */
+static u64 kernfs_ns_id(const struct ns_common *ns)
+{
+ return ns ? ns->ns_id : 0;
+}
+
/**
* kernfs_name_hash - calculate hash of @ns + @name
* @name: Null terminated string to hash
static unsigned int kernfs_name_hash(const char *name,
const struct ns_common *ns)
{
- unsigned long hash = init_name_hash(ns);
+ unsigned long hash = init_name_hash(kernfs_ns_id(ns));
unsigned int len = strlen(name);
while (len--)
hash = partial_name_hash(*name++, hash);
static int kernfs_name_compare(unsigned int hash, const char *name,
const struct ns_common *ns, const struct kernfs_node *kn)
{
+ u64 ns_id = kernfs_ns_id(ns);
+ u64 kn_ns_id = kernfs_ns_id(kn->ns);
+
if (hash < kn->hash)
return -1;
if (hash > kn->hash)
return 1;
- if (ns < kn->ns)
+ if (ns_id < kn_ns_id)
return -1;
- if (ns > kn->ns)
+ if (ns_id > kn_ns_id)
return 1;
return strcmp(name, kernfs_rcu_name(kn));
}
/* The kernfs node has been moved to a different namespace */
if (parent && kernfs_ns_enabled(parent) &&
- kernfs_info(dentry->d_sb)->ns != kn->ns)
+ kernfs_ns_id(kernfs_info(dentry->d_sb)->ns) != kernfs_ns_id(kn->ns))
goto out_bad;
up_read(&root->kernfs_rwsem);
old_name = kernfs_rcu_name(kn);
if (!new_name)
new_name = old_name;
- if ((old_parent == new_parent) && (kn->ns == new_ns) &&
+ if ((old_parent == new_parent) &&
+ (kernfs_ns_id(kn->ns) == kernfs_ns_id(new_ns)) &&
(strcmp(old_name, new_name) == 0))
goto out; /* nothing to rename */
}
}
/* Skip over entries which are dying/dead or in the wrong namespace */
- while (pos && (!kernfs_active(pos) || pos->ns != ns)) {
+ while (pos && (!kernfs_active(pos) ||
+ kernfs_ns_id(pos->ns) != kernfs_ns_id(ns))) {
struct rb_node *node = rb_next(&pos->rb);
if (!node)
pos = NULL;
pos = NULL;
else
pos = rb_to_kn(node);
- } while (pos && (!kernfs_active(pos) || pos->ns != ns));
+ } while (pos && (!kernfs_active(pos) ||
+ kernfs_ns_id(pos->ns) != kernfs_ns_id(ns)));
}
return pos;
}