[PATCH 1/4] Routine for generating a safe ID for kernel pointer

Pavel Emelyanov
2011-11-17T09:56:21Z

The routine XORs the given pointer with a random value thus producing
an ID (32 or 64 bit, depending on the arch) which can be shown even to
unprivileged user space processes without risking of leaking kernel
information.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>

---
include/linux/mm.h | 13 +++++++++++++
mm/Kconfig | 7 +++++++
mm/util.c | 28 ++++++++++++++++++++++++++++
3 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 7438071..80ea327 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1627,5 +1627,18 @@ extern void copy_user_huge_page(struct page *dst, struct page *src,
unsigned int pages_per_huge_page);
#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */

+enum {
+ GEN_OBJ_ID_TYPES,
+};
+
+#ifdef CONFIG_GENERIC_OBJECT_IDS
+unsigned long gen_object_id(void *ptr, int type);
+#else
+static inline unsigned long gen_object_id(void *ptr, int type)
+{
+ return 0;
+}
+#endif
+
#endif /* __KERNEL__ */
#endif /* _LINUX_MM_H */
diff --git a/mm/Kconfig b/mm/Kconfig
index f2f1ca1..1480cbf 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -370,3 +370,10 @@ config CLEANCACHE
in a negligible performance hit.

If unsure, say Y to enable cleancache
+
+config GENERIC_OBJECT_IDS
+ bool "Enable generic object ids infrastructure"
+ default n
+ help
+ Turn on the (quite simple) funtionality that can generate IDs for
+ kernel objects which is safe to export to the userspace.
diff --git a/mm/util.c b/mm/util.c
index 88ea1bd..1bcde18 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -4,6 +4,7 @@
#include <linux/module.h>
#include <linux/err.h>
#include <linux/sched.h>
+#include <linux/random.h>
#include <asm/uaccess.h>

#include "internal.h"
@@ -307,3 +308,30 @@ EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
EXPORT_TRACEPOINT_SYMBOL(kfree);
EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);
+
+#ifdef CONFIG_GENERIC_OBJECT_IDS
+static unsigned long ptr_poison[GEN_OBJ_ID_TYPES] __read_mostly;
+
+unsigned long gen_object_id(void *ptr, int type)
+{
+ if (!ptr)
+ return 0;
+
+ BUG_ON(type >= GEN_OBJ_ID_TYPES);
+ WARN_ON_ONCE(ptr_poison[type] == 0);
+
+ return ((unsigned long)ptr) ^ ptr_poison[type];
+}
+
+static int gen_object_poison_init(void)
+{
+ int i;
+
+ for (i = 0; i < GEN_OBJ_ID_TYPES; i++)
+ get_random_bytes(&ptr_poison[i], sizeof(unsigned long));
+
+ return 0;
+}
+
+late_initcall(gen_object_poison_init);
+#endif
--
1.5.5.6