Skip to contentSkip to conversation

jump_label: check entries limit in __jump_label_update

PATCH Superseded Jiri Olsa 2 participants 6 messages May 4, 2011 – May 27, 2011 Static branch/call

lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

PATCH 1/1 jump_label: check entries limit in __jump_label_update

When iterating the jump_label entries array (core or modules), the __jump_label_update function peeks over the last entry.

The reason is that the end of the for loop depends on the key value of the processed entry. Thus when going through the last array entry, we will touch the memory behind the array limit.

This bug probably will never be triggered, since most likely the memory behind the jump_label entries will be accesable and the entry->key will be different than the expected value.

---
 kernel/jump_label.c |   17 ++++++++++++-----
 1 files changed, 12 insertions(+), 5 deletions(-)
kernel/jump_label.c +12 -5
index 74d1c09..b2ee97a 100644
@@ -105,9 +105,12 @@ static int __jump_label_text_reserved(struct jump_entry *iter_start,
 }
 
 static void __jump_label_update(struct jump_label_key *key,
-		struct jump_entry *entry, int enable)
+				struct jump_entry *entry,
+				struct jump_entry *stop, int enable)
 {
-	for (; entry->key == (jump_label_t)(unsigned long)key; entry++) {
+	for (; (entry < stop) &&
+	      (entry->key == (jump_label_t)(unsigned long)key);
+	      entry++) {
 		/*
 		 * entry->code set to 0 invalidates module init text sections
 		 * kernel_text_address() verifies we are not in core kernel
@@ -158,6 +161,7 @@ early_initcall(jump_label_init);
 struct jump_label_mod {
 	struct jump_label_mod *next;
 	struct jump_entry *entries;
+	struct jump_entry *entries_stop;
 	struct module *mod;
 };
 
@@ -181,7 +185,8 @@ static void __jump_label_mod_update(struct jump_label_key *key, int enable)
 	struct jump_label_mod *mod = key->next;
 
 	while (mod) {
-		__jump_label_update(key, mod->entries, enable);
+		__jump_label_update(key, mod->entries, mod->entries_stop,
+				    enable);
 		mod = mod->next;
 	}
 }
@@ -241,11 +246,13 @@ static int jump_label_add_module(struct module *mod)
 
 		jlm->mod = mod;
 		jlm->entries = iter;
+		jlm->entries_stop = iter_stop;
 		jlm->next = key->next;
 		key->next = jlm;
 
 		if (jump_label_enabled(key))
-			__jump_label_update(key, iter, JUMP_LABEL_ENABLE);
+			__jump_label_update(key, iter, iter_stop,
+					    JUMP_LABEL_ENABLE);
 	}
 
 	return 0;
@@ -371,7 +378,7 @@ static void jump_label_update(struct jump_label_key *key, int enable)
 
 	/* if there are no users, entry can be NULL */
 	if (entry)
-		__jump_label_update(key, entry, enable);
+		__jump_label_update(key, entry, __stop___jump_table, enable);
 
 #ifdef CONFIG_MODULES
 	__jump_label_mod_update(key, enable);
Signature · 1 line
1.7.1
#1 lore raw
Quoted 46 lines · Jiri Olsa

When iterating the jump_label entries array (core or modules), the __jump_label_update function peeks over the last entry.

The reason is that the end of the for loop depends on the key value of the processed entry. Thus when going through the last array entry, we will touch the memory behind the array limit.

This bug probably will never be triggered, since most likely the memory behind the jump_label entries will be accesable and the entry->key will be different than the expected value.

---
 kernel/jump_label.c |   17 ++++++++++++-----
 1 files changed, 12 insertions(+), 5 deletions(-)
kernel/jump_label.c +6 -2
index 74d1c09..b2ee97a 100644
@@ -105,9 +105,12 @@ static int __jump_label_text_reserved(struct jump_entry *iter_start,
 }
 
 static void __jump_label_update(struct jump_label_key *key,
-		struct jump_entry *entry, int enable)
+				struct jump_entry *entry,
+				struct jump_entry *stop, int enable)
 {
-	for (; entry->key == (jump_label_t)(unsigned long)key; entry++) {
+	for (; (entry < stop) &&
+	      (entry->key == (jump_label_t)(unsigned long)key);
+	      entry++) {
 		/*
 		 * entry->code set to 0 invalidates module init text sections
 		 * kernel_text_address() verifies we are not in core kernel
@@ -158,6 +161,7 @@ early_initcall(jump_label_init);
 struct jump_label_mod {
 	struct jump_label_mod *next;
 	struct jump_entry *entries;
+	struct jump_entry *entries_stop;
 	struct module *mod;
 };
 

kernel/jump_label.c static void __jump_label_mod_update(struct jump_label_key *key, int enable) · line 190

Quoted 7 lines
kernel/jump_label.c +2 -1
@@ -181,7 +185,8 @@ static void __jump_label_mod_update(struct jump_label_key *key, int enable)
 	struct jump_label_mod *mod = key->next;
 
 	while (mod) {
-		__jump_label_update(key, mod->entries, enable);
+		__jump_label_update(key, mod->entries, mod->entries_stop,
+				    enable);
 		mod = mod->next;

hmmm. Instead of adding a new field to the 'struct jump_label_mod' data structure (and thus increasing its footprint), why not use:
mod->jump_entries + mod->num_jump_entries here?

Otherwise, I agree this is a nice fix to have.

Thanks,

-Jason

Quoted 29 lines
kernel/jump_label.c +4 -2
 	}
 }
@@ -241,11 +246,13 @@ static int jump_label_add_module(struct module *mod)
 
 		jlm->mod = mod;
 		jlm->entries = iter;
+		jlm->entries_stop = iter_stop;
 		jlm->next = key->next;
 		key->next = jlm;
 
 		if (jump_label_enabled(key))
-			__jump_label_update(key, iter, JUMP_LABEL_ENABLE);
+			__jump_label_update(key, iter, iter_stop,
+					    JUMP_LABEL_ENABLE);
 	}
 
 	return 0;
@@ -371,7 +378,7 @@ static void jump_label_update(struct jump_label_key *key, int enable)
 
 	/* if there are no users, entry can be NULL */
 	if (entry)
-		__jump_label_update(key, entry, enable);
+		__jump_label_update(key, entry, __stop___jump_table, enable);
 
 #ifdef CONFIG_MODULES
 	__jump_label_mod_update(key, enable);
Signature · 1 line
1.7.1
author
#2 lore raw

PATCH v2 1/1 jump_label: check entries limit in __jump_label_update

Quoted 49 lines · Jason Baron
Quoted 48 lines · Jiri Olsa

When iterating the jump_label entries array (core or modules), the __jump_label_update function peeks over the last entry.

The reason is that the end of the for loop depends on the key value of the processed entry. Thus when going through the last array entry, we will touch the memory behind the array limit.

This bug probably will never be triggered, since most likely the memory behind the jump_label entries will be accesable and the entry->key will be different than the expected value.

---
 kernel/jump_label.c |   17 ++++++++++++-----
 1 files changed, 12 insertions(+), 5 deletions(-)
kernel/jump_label.c +6 -2
index 74d1c09..b2ee97a 100644
@@ -105,9 +105,12 @@ static int __jump_label_text_reserved(struct jump_entry *iter_start,
 }
 
 static void __jump_label_update(struct jump_label_key *key,
-		struct jump_entry *entry, int enable)
+				struct jump_entry *entry,
+				struct jump_entry *stop, int enable)
 {
-	for (; entry->key == (jump_label_t)(unsigned long)key; entry++) {
+	for (; (entry < stop) &&
+	      (entry->key == (jump_label_t)(unsigned long)key);
+	      entry++) {
 		/*
 		 * entry->code set to 0 invalidates module init text sections
 		 * kernel_text_address() verifies we are not in core kernel
@@ -158,6 +161,7 @@ early_initcall(jump_label_init);
 struct jump_label_mod {
 	struct jump_label_mod *next;
 	struct jump_entry *entries;
+	struct jump_entry *entries_stop;
 	struct module *mod;
 };
 
@@ -181,7 +185,8 @@ static void __jump_label_mod_update(struct jump_label_key *key, int enable)
 	struct jump_label_mod *mod = key->next;
 
Quoted 9 lines

kernel/jump_label.c static void __jump_label_mod_update(struct jump_label_key *key, int enable) · line 190

Quoted 5 lines
kernel/jump_label.c +2 -1
 	while (mod) {
-		__jump_label_update(key, mod->entries, enable);
+		__jump_label_update(key, mod->entries, mod->entries_stop,
+				    enable);
 		mod = mod->next;

hmmm. Instead of adding a new field to the 'struct jump_label_mod' data structure (and thus increasing its footprint), why not use:
mod->jump_entries + mod->num_jump_entries here?

yep, overlooked the struct module pointer inside jump_label_mod attaching new patch

thanks,
jirka

---
When iterating the jump_label entries array (core or modules), the __jump_label_update function peeks over the last entry.

The reason is that the end of the for loop depends on the key value of the processed entry. Thus when going through the last array entry, we will touch the memory behind the array limit.

This bug probably will never be triggered, since most likely the memory behind the jump_label entries will be accesable and the entry->key will be different than the expected value.

---
 kernel/jump_label.c |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)
kernel/jump_label.c +13 -5
index 74d1c09..fa27e75 100644
@@ -105,9 +105,12 @@ static int __jump_label_text_reserved(struct jump_entry *iter_start,
 }
 
 static void __jump_label_update(struct jump_label_key *key,
-		struct jump_entry *entry, int enable)
+				struct jump_entry *entry,
+				struct jump_entry *stop, int enable)
 {
-	for (; entry->key == (jump_label_t)(unsigned long)key; entry++) {
+	for (; (entry < stop) &&
+	      (entry->key == (jump_label_t)(unsigned long)key);
+	      entry++) {
 		/*
 		 * entry->code set to 0 invalidates module init text sections
 		 * kernel_text_address() verifies we are not in core kernel
@@ -181,7 +184,11 @@ static void __jump_label_mod_update(struct jump_label_key *key, int enable)
 	struct jump_label_mod *mod = key->next;
 
 	while (mod) {
-		__jump_label_update(key, mod->entries, enable);
+		struct module *m = mod->mod;
+
+		__jump_label_update(key, mod->entries,
+				    m->jump_entries + m->num_jump_entries,
+				    enable);
 		mod = mod->next;
 	}
 }
@@ -245,7 +252,8 @@ static int jump_label_add_module(struct module *mod)
 		key->next = jlm;
 
 		if (jump_label_enabled(key))
-			__jump_label_update(key, iter, JUMP_LABEL_ENABLE);
+			__jump_label_update(key, iter, iter_stop,
+					    JUMP_LABEL_ENABLE);
 	}
 
 	return 0;
@@ -371,7 +379,7 @@ static void jump_label_update(struct jump_label_key *key, int enable)
 
 	/* if there are no users, entry can be NULL */
 	if (entry)
-		__jump_label_update(key, entry, enable);
+		__jump_label_update(key, entry, __stop___jump_table, enable);
 
 #ifdef CONFIG_MODULES
 	__jump_label_mod_update(key, enable);
Signature · 1 line
1.7.1
Acked-by on v2 1/1
#3 lore raw
Quoted 139 lines · Jiri Olsa
Quoted 49 lines · Jason Baron
Quoted 48 lines · Jiri Olsa

When iterating the jump_label entries array (core or modules), the __jump_label_update function peeks over the last entry.

The reason is that the end of the for loop depends on the key value of the processed entry. Thus when going through the last array entry, we will touch the memory behind the array limit.

This bug probably will never be triggered, since most likely the memory behind the jump_label entries will be accesable and the entry->key will be different than the expected value.

---
 kernel/jump_label.c |   17 ++++++++++++-----
 1 files changed, 12 insertions(+), 5 deletions(-)
kernel/jump_label.c +6 -2
index 74d1c09..b2ee97a 100644
@@ -105,9 +105,12 @@ static int __jump_label_text_reserved(struct jump_entry *iter_start,
 }
 
 static void __jump_label_update(struct jump_label_key *key,
-		struct jump_entry *entry, int enable)
+				struct jump_entry *entry,
+				struct jump_entry *stop, int enable)
 {
-	for (; entry->key == (jump_label_t)(unsigned long)key; entry++) {
+	for (; (entry < stop) &&
+	      (entry->key == (jump_label_t)(unsigned long)key);
+	      entry++) {
 		/*
 		 * entry->code set to 0 invalidates module init text sections
 		 * kernel_text_address() verifies we are not in core kernel
@@ -158,6 +161,7 @@ early_initcall(jump_label_init);
 struct jump_label_mod {
 	struct jump_label_mod *next;
 	struct jump_entry *entries;
+	struct jump_entry *entries_stop;
 	struct module *mod;
 };
 
@@ -181,7 +185,8 @@ static void __jump_label_mod_update(struct jump_label_key *key, int enable)
 	struct jump_label_mod *mod = key->next;
 
Quoted 9 lines

kernel/jump_label.c static void __jump_label_mod_update(struct jump_label_key *key, int enable) · line 192

Quoted 5 lines
kernel/jump_label.c +2 -1
 	while (mod) {
-		__jump_label_update(key, mod->entries, enable);
+		__jump_label_update(key, mod->entries, mod->entries_stop,
+				    enable);
 		mod = mod->next;

hmmm. Instead of adding a new field to the 'struct jump_label_mod' data structure (and thus increasing its footprint), why not use:
mod->jump_entries + mod->num_jump_entries here?

yep, overlooked the struct module pointer inside jump_label_mod attaching new patch

thanks,
jirka

---
When iterating the jump_label entries array (core or modules), the __jump_label_update function peeks over the last entry.

The reason is that the end of the for loop depends on the key value of the processed entry. Thus when going through the last array entry, we will touch the memory behind the array limit.

This bug probably will never be triggered, since most likely the memory behind the jump_label entries will be accesable and the entry->key will be different than the expected value.

---
 kernel/jump_label.c |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)
kernel/jump_label.c +13 -5
index 74d1c09..fa27e75 100644
@@ -105,9 +105,12 @@ static int __jump_label_text_reserved(struct jump_entry *iter_start,
 }
 
 static void __jump_label_update(struct jump_label_key *key,
-		struct jump_entry *entry, int enable)
+				struct jump_entry *entry,
+				struct jump_entry *stop, int enable)
 {
-	for (; entry->key == (jump_label_t)(unsigned long)key; entry++) {
+	for (; (entry < stop) &&
+	      (entry->key == (jump_label_t)(unsigned long)key);
+	      entry++) {
 		/*
 		 * entry->code set to 0 invalidates module init text sections
 		 * kernel_text_address() verifies we are not in core kernel
@@ -181,7 +184,11 @@ static void __jump_label_mod_update(struct jump_label_key *key, int enable)
 	struct jump_label_mod *mod = key->next;
 
 	while (mod) {
-		__jump_label_update(key, mod->entries, enable);
+		struct module *m = mod->mod;
+
+		__jump_label_update(key, mod->entries,
+				    m->jump_entries + m->num_jump_entries,
+				    enable);
 		mod = mod->next;
 	}
 }
@@ -245,7 +252,8 @@ static int jump_label_add_module(struct module *mod)
 		key->next = jlm;
 
 		if (jump_label_enabled(key))
-			__jump_label_update(key, iter, JUMP_LABEL_ENABLE);
+			__jump_label_update(key, iter, iter_stop,
+					    JUMP_LABEL_ENABLE);
 	}
 
 	return 0;
@@ -371,7 +379,7 @@ static void jump_label_update(struct jump_label_key *key, int enable)
 
 	/* if there are no users, entry can be NULL */
 	if (entry)
-		__jump_label_update(key, entry, enable);
+		__jump_label_update(key, entry, __stop___jump_table, enable);
 
 #ifdef CONFIG_MODULES
 	__jump_label_mod_update(key, enable);
Signature · 1 line
1.7.1

Looks good.

Thanks,

-Jason

author
#4 lore raw

hi,
any feedback?

thanks,
jirka

Quoted 148 lines · Jason Baron
Quoted 139 lines · Jiri Olsa
Quoted 49 lines · Jason Baron
Quoted 48 lines · Jiri Olsa

When iterating the jump_label entries array (core or modules), the __jump_label_update function peeks over the last entry.

The reason is that the end of the for loop depends on the key value of the processed entry. Thus when going through the last array entry, we will touch the memory behind the array limit.

This bug probably will never be triggered, since most likely the memory behind the jump_label entries will be accesable and the entry->key will be different than the expected value.

---
 kernel/jump_label.c |   17 ++++++++++++-----
 1 files changed, 12 insertions(+), 5 deletions(-)
kernel/jump_label.c +6 -2
index 74d1c09..b2ee97a 100644
@@ -105,9 +105,12 @@ static int __jump_label_text_reserved(struct jump_entry *iter_start,
 }
 
 static void __jump_label_update(struct jump_label_key *key,
-		struct jump_entry *entry, int enable)
+				struct jump_entry *entry,
+				struct jump_entry *stop, int enable)
 {
-	for (; entry->key == (jump_label_t)(unsigned long)key; entry++) {
+	for (; (entry < stop) &&
+	      (entry->key == (jump_label_t)(unsigned long)key);
+	      entry++) {
 		/*
 		 * entry->code set to 0 invalidates module init text sections
 		 * kernel_text_address() verifies we are not in core kernel
@@ -158,6 +161,7 @@ early_initcall(jump_label_init);
 struct jump_label_mod {
 	struct jump_label_mod *next;
 	struct jump_entry *entries;
+	struct jump_entry *entries_stop;
 	struct module *mod;
 };
 
@@ -181,7 +185,8 @@ static void __jump_label_mod_update(struct jump_label_key *key, int enable)
 	struct jump_label_mod *mod = key->next;
 
Quoted 9 lines

kernel/jump_label.c static void __jump_label_mod_update(struct jump_label_key *key, int enable) · line 192

Quoted 5 lines
kernel/jump_label.c +2 -1
 	while (mod) {
-		__jump_label_update(key, mod->entries, enable);
+		__jump_label_update(key, mod->entries, mod->entries_stop,
+				    enable);
 		mod = mod->next;

hmmm. Instead of adding a new field to the 'struct jump_label_mod' data structure (and thus increasing its footprint), why not use:
mod->jump_entries + mod->num_jump_entries here?

yep, overlooked the struct module pointer inside jump_label_mod attaching new patch

thanks,
jirka

---
When iterating the jump_label entries array (core or modules), the __jump_label_update function peeks over the last entry.

The reason is that the end of the for loop depends on the key value of the processed entry. Thus when going through the last array entry, we will touch the memory behind the array limit.

This bug probably will never be triggered, since most likely the memory behind the jump_label entries will be accesable and the entry->key will be different than the expected value.

---
 kernel/jump_label.c |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)
kernel/jump_label.c +13 -5
index 74d1c09..fa27e75 100644
@@ -105,9 +105,12 @@ static int __jump_label_text_reserved(struct jump_entry *iter_start,
 }
 
 static void __jump_label_update(struct jump_label_key *key,
-		struct jump_entry *entry, int enable)
+				struct jump_entry *entry,
+				struct jump_entry *stop, int enable)
 {
-	for (; entry->key == (jump_label_t)(unsigned long)key; entry++) {
+	for (; (entry < stop) &&
+	      (entry->key == (jump_label_t)(unsigned long)key);
+	      entry++) {
 		/*
 		 * entry->code set to 0 invalidates module init text sections
 		 * kernel_text_address() verifies we are not in core kernel
@@ -181,7 +184,11 @@ static void __jump_label_mod_update(struct jump_label_key *key, int enable)
 	struct jump_label_mod *mod = key->next;
 
 	while (mod) {
-		__jump_label_update(key, mod->entries, enable);
+		struct module *m = mod->mod;
+
+		__jump_label_update(key, mod->entries,
+				    m->jump_entries + m->num_jump_entries,
+				    enable);
 		mod = mod->next;
 	}
 }
@@ -245,7 +252,8 @@ static int jump_label_add_module(struct module *mod)
 		key->next = jlm;
 
 		if (jump_label_enabled(key))
-			__jump_label_update(key, iter, JUMP_LABEL_ENABLE);
+			__jump_label_update(key, iter, iter_stop,
+					    JUMP_LABEL_ENABLE);
 	}
 
 	return 0;
@@ -371,7 +379,7 @@ static void jump_label_update(struct jump_label_key *key, int enable)
 
 	/* if there are no users, entry can be NULL */
 	if (entry)
-		__jump_label_update(key, entry, enable);
+		__jump_label_update(key, entry, __stop___jump_table, enable);
 
 #ifdef CONFIG_MODULES
 	__jump_label_mod_update(key, enable);
Signature · 1 line
1.7.1

Looks good.

Thanks,

-Jason

#5 lore raw

[tip:perf/urgent] jump_label: Check entries limit in __jump_label_update

In reply to Jiri Olsa
Commit-ID:  7cbc5b8d4a775a43875a09e29c49a2a8195b5b2d
Gitweb:     http://git.kernel.org/tip/7cbc5b8d4a775a43875a09e29c49a2a8195b5b2d
Author:     Jiri Olsa <[email protected]>
AuthorDate: Tue, 10 May 2011 12:43:46 +0200
Committer:  Steven Rostedt <[email protected]>
CommitDate: Wed, 25 May 2011 19:56:36 -0400

jump_label: Check entries limit in __jump_label_update

When iterating the jump_label entries array (core or modules), the __jump_label_update function peeks over the last entry.

The reason is that the end of the for loop depends on the key value of the processed entry. Thus when going through the last array entry, we will touch the memory behind the array limit.

This bug probably will never be triggered, since most likely the memory behind the jump_label entries will be accesable and the entry->key will be different than the expected value.

---
 kernel/jump_label.c |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)
kernel/jump_label.c +13 -5
index 74d1c09..fa27e75 100644
@@ -105,9 +105,12 @@ static int __jump_label_text_reserved(struct jump_entry *iter_start,
 }
 
 static void __jump_label_update(struct jump_label_key *key,
-		struct jump_entry *entry, int enable)
+				struct jump_entry *entry,
+				struct jump_entry *stop, int enable)
 {
-	for (; entry->key == (jump_label_t)(unsigned long)key; entry++) {
+	for (; (entry < stop) &&
+	      (entry->key == (jump_label_t)(unsigned long)key);
+	      entry++) {
 		/*
 		 * entry->code set to 0 invalidates module init text sections
 		 * kernel_text_address() verifies we are not in core kernel
@@ -181,7 +184,11 @@ static void __jump_label_mod_update(struct jump_label_key *key, int enable)
 	struct jump_label_mod *mod = key->next;
 
 	while (mod) {
-		__jump_label_update(key, mod->entries, enable);
+		struct module *m = mod->mod;
+
+		__jump_label_update(key, mod->entries,
+				    m->jump_entries + m->num_jump_entries,
+				    enable);
 		mod = mod->next;
 	}
 }
@@ -245,7 +252,8 @@ static int jump_label_add_module(struct module *mod)
 		key->next = jlm;
 
 		if (jump_label_enabled(key))
-			__jump_label_update(key, iter, JUMP_LABEL_ENABLE);
+			__jump_label_update(key, iter, iter_stop,
+					    JUMP_LABEL_ENABLE);
 	}
 
 	return 0;
@@ -371,7 +379,7 @@ static void jump_label_update(struct jump_label_key *key, int enable)
 
 	/* if there are no users, entry can be NULL */
 	if (entry)
-		__jump_label_update(key, entry, enable);
+		__jump_label_update(key, entry, __stop___jump_table, enable);
 
 #ifdef CONFIG_MODULES
 	__jump_label_mod_update(key, enable);