Skip to contentSkip to conversation

slub: Fix a missing-check bug in mm/slub.c file of Linux 5.1.1

PATCH In review Gen Zhang 2 participants 2 messages May 15, 2019 – May 19, 2019

lore
Share & tools
lore

Gen Zhang

author
#0 lore raw

PATCH 1/1 slub: Fix a missing-check bug in mm/slub.c file of Linux 5.1.1

Pointer s is allocated with kmem_cache_zalloc(). And s is used in the follwoing codes. However, when kmem_cache_zalloc fails, using s will cause null pointer dereference and the kernel will go wrong. Thus we check whether the kmem_cache_zalloc fails.

---

slub.c +2 -0
@@ -4201,6 +4201,8 @@ static struct kmem_cache * __init bootst
 {
 	int node;
 	struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
+	if (!s)
+		return ERR_PTR(-ENOMEM);
 	struct kmem_cache_node *n;
 
 	memcpy(s, static_cache, kmem_cache->object_size);

---

#1 lore raw
Quoted 7 lines · Gen Zhang

Pointer s is allocated with kmem_cache_zalloc(). And s is used in the follwoing codes. However, when kmem_cache_zalloc fails, using s will cause null pointer dereference and the kernel will go wrong. Thus we check whether the kmem_cache_zalloc fails.

It's ok if we encounter a NULL pointer dereference here, if we are ENOMEM then there is no way to initialize the slub allocator to be able to perform any future memory allocation. Returning an error code won't help.

slub.c static struct kmem_cache * __init bootst · line 4208

Quoted 12 lines

---

slub.c +2 -0
@@ -4201,6 +4201,8 @@ static struct kmem_cache * __init bootst
 {
 	int node;
 	struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
+	if (!s)
+		return ERR_PTR(-ENOMEM);
 	struct kmem_cache_node *n;
 
 	memcpy(s, static_cache, kmem_cache->object_size);

It's not legal to mix declarations within the C code here.