Skip to content

Commit 3ea056c

Browse files
Alexey Dobriyantorvalds
authored andcommitted
uts: create "struct uts_namespace" from kmem_cache
So "struct uts_namespace" can enjoy fine-grained SLAB debugging and usercopy protection. I'd prefer shorter name "utsns" but there is "user_namespace" already. Link: http://lkml.kernel.org/r/20180228215158.GA23146@avx2 Signed-off-by: Alexey Dobriyan <[email protected]> Reviewed-by: Andrew Morton <[email protected]> Cc: "Eric W. Biederman" <[email protected]> Cc: Serge Hallyn <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent bc4f2f5 commit 3ea056c

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

include/linux/utsname.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ static inline void put_uts_ns(struct uts_namespace *ns)
4444
{
4545
kref_put(&ns->kref, free_uts_ns);
4646
}
47+
48+
void uts_ns_init(void);
4749
#else
4850
static inline void get_uts_ns(struct uts_namespace *ns)
4951
{
@@ -61,6 +63,10 @@ static inline struct uts_namespace *copy_utsname(unsigned long flags,
6163

6264
return old_ns;
6365
}
66+
67+
static inline void uts_ns_init(void)
68+
{
69+
}
6470
#endif
6571

6672
#ifdef CONFIG_PROC_SYSCTL

init/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <linux/taskstats_kern.h>
5252
#include <linux/delayacct.h>
5353
#include <linux/unistd.h>
54+
#include <linux/utsname.h>
5455
#include <linux/rmap.h>
5556
#include <linux/mempolicy.h>
5657
#include <linux/key.h>
@@ -706,6 +707,7 @@ asmlinkage __visible void __init start_kernel(void)
706707
cred_init();
707708
fork_init();
708709
proc_caches_init();
710+
uts_ns_init();
709711
buffer_init();
710712
key_init();
711713
security_init();

kernel/utsname.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <linux/proc_ns.h>
2020
#include <linux/sched/task.h>
2121

22+
static struct kmem_cache *uts_ns_cache __ro_after_init;
23+
2224
static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
2325
{
2426
return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
@@ -33,7 +35,7 @@ static struct uts_namespace *create_uts_ns(void)
3335
{
3436
struct uts_namespace *uts_ns;
3537

36-
uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
38+
uts_ns = kmem_cache_alloc(uts_ns_cache, GFP_KERNEL);
3739
if (uts_ns)
3840
kref_init(&uts_ns->kref);
3941
return uts_ns;
@@ -42,7 +44,7 @@ static struct uts_namespace *create_uts_ns(void)
4244
/*
4345
* Clone a new ns copying an original utsname, setting refcount to 1
4446
* @old_ns: namespace to clone
45-
* Return ERR_PTR(-ENOMEM) on error (failure to kmalloc), new ns otherwise
47+
* Return ERR_PTR(-ENOMEM) on error (failure to allocate), new ns otherwise
4648
*/
4749
static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
4850
struct uts_namespace *old_ns)
@@ -75,7 +77,7 @@ static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
7577
return ns;
7678

7779
fail_free:
78-
kfree(ns);
80+
kmem_cache_free(uts_ns_cache, ns);
7981
fail_dec:
8082
dec_uts_namespaces(ucounts);
8183
fail:
@@ -113,7 +115,7 @@ void free_uts_ns(struct kref *kref)
113115
dec_uts_namespaces(ns->ucounts);
114116
put_user_ns(ns->user_ns);
115117
ns_free_inum(&ns->ns);
116-
kfree(ns);
118+
kmem_cache_free(uts_ns_cache, ns);
117119
}
118120

119121
static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
@@ -169,3 +171,13 @@ const struct proc_ns_operations utsns_operations = {
169171
.install = utsns_install,
170172
.owner = utsns_owner,
171173
};
174+
175+
void __init uts_ns_init(void)
176+
{
177+
uts_ns_cache = kmem_cache_create_usercopy(
178+
"uts_namespace", sizeof(struct uts_namespace), 0,
179+
SLAB_PANIC|SLAB_ACCOUNT,
180+
offsetof(struct uts_namespace, name),
181+
sizeof_field(struct uts_namespace, name),
182+
NULL);
183+
}

0 commit comments

Comments
 (0)