Skip to contentSkip to conversation

mbuf: fix out-of-bounds access

PATCH Superseded Xiaolong Ye 2 participants 4 messages Jun 10, 2020 – Jun 11, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mbuf: fix out-of-bounds access

We should make sure off + size < sizeof(struct rte_mbuf) to avoid possible out-of-bounds access of free_space array, there is no issue currently due to the low bits of free_flags (which is adjacent to free_space) are always set to 0. But we shouldn't rely on it since it's fragile and layout of struct mbuf_dyn_shm may be changed in the future. This patch adds boundary check explicitly to avoid potential risk of out-of-bounds access.

Fixes 4958ca3a443a ("mbuf: support dynamic fields and flags")
---
 lib/librte_mbuf/rte_mbuf_dyn.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_mbuf/rte_mbuf_dyn.c +2 -1
index d6931f847..8af89dde4 100644
@@ -71,7 +71,8 @@ process_score(void)
 
 	for (off = 0; off < sizeof(struct rte_mbuf); off++) {
 		/* get the size of the free zone */
-		for (size = 0; shm->free_space[off + size]; size++)
+		for (size = 0; shm->free_space[off + size] &&
+			     (off + size) < sizeof(struct rte_mbuf); size++)
 			;
 		if (size == 0)
 			continue;
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/1 mbuf: fix out-of-bounds access

We should make sure off + size < sizeof(struct rte_mbuf) to avoid possible out-of-bounds access of free_space array, there is no issue currently due to the low bits of free_flags (which is adjacent to free_space) are always set to 0. But we shouldn't rely on it since it's fragile and layout of struct mbuf_dyn_shm may be changed in the future. This patch adds boundary check explicitly to avoid potential risk of out-of-bounds access.

Fixes 4958ca3a443a ("mbuf: support dynamic fields and flags")

---

V2: put the check before accessing free_space

 lib/librte_mbuf/rte_mbuf_dyn.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_mbuf/rte_mbuf_dyn.c +2 -1
index d6931f847..9d6388cff 100644
@@ -71,7 +71,8 @@ process_score(void)
 
 	for (off = 0; off < sizeof(struct rte_mbuf); off++) {
 		/* get the size of the free zone */
-		for (size = 0; shm->free_space[off + size]; size++)
+		for (size = 0; (off + size) < sizeof(struct rte_mbuf) &&
+			     shm->free_space[off + size]; size++)
 			;
 		if (size == 0)
 			continue;
Signature · 1 line
2.17.1
Acked-by on v2 1/1
#2 lore raw
Quoted 8 lines · Xiaolong Ye

We should make sure off + size < sizeof(struct rte_mbuf) to avoid possible out-of-bounds access of free_space array, there is no issue currently due to the low bits of free_flags (which is adjacent to free_space) are always set to 0. But we shouldn't rely on it since it's fragile and layout of struct mbuf_dyn_shm may be changed in the future. This patch adds boundary check explicitly to avoid potential risk of out-of-bounds access.

Quoted 4 lines
Fixes 4958ca3a443a ("mbuf: support dynamic fields and flags")

I suggest to change the title in:
mbuf: fix out-of-bounds access at dyn field register

Thomas, as Xiaolong pointed-out, it fixes a bug in the code but there is no impact. I let you decide if it should be tagged as a fix or not, and if it should be backported. I'll tend to say yes.

Quoted 24 lines

---

V2: put the check before accessing free_space

 lib/librte_mbuf/rte_mbuf_dyn.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_mbuf/rte_mbuf_dyn.c +2 -1
index d6931f847..9d6388cff 100644
@@ -71,7 +71,8 @@ process_score(void)
 
 	for (off = 0; off < sizeof(struct rte_mbuf); off++) {
 		/* get the size of the free zone */
-		for (size = 0; shm->free_space[off + size]; size++)
+		for (size = 0; (off + size) < sizeof(struct rte_mbuf) &&
+			     shm->free_space[off + size]; size++)
 			;
 		if (size == 0)
 			continue;
Signature · 1 line
2.17.1
#3 lore raw

PATCH v3 1/1 mbuf: fix out-of-bounds access at dyn field register

In reply to Xiaolong Ye

We should make sure off + size < sizeof(struct rte_mbuf) to avoid possible out-of-bounds access of free_space array, there is no issue currently due to the low bits of free_flags (which is adjacent to free_space) are always set to 0. But we shouldn't rely on it since it's fragile and layout of struct mbuf_dyn_shm may be changed in the future. This patch adds boundary check explicitly to avoid potential risk of out-of-bounds access.

Fixes 4958ca3a443a ("mbuf: support dynamic fields and flags")

---

V3: update subject as Olivier suggested

 lib/librte_mbuf/rte_mbuf_dyn.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_mbuf/rte_mbuf_dyn.c +2 -1
index d6931f847..9d6388cff 100644
@@ -71,7 +71,8 @@ process_score(void)
 
 	for (off = 0; off < sizeof(struct rte_mbuf); off++) {
 		/* get the size of the free zone */
-		for (size = 0; shm->free_space[off + size]; size++)
+		for (size = 0; (off + size) < sizeof(struct rte_mbuf) &&
+			     shm->free_space[off + size]; size++)
 			;
 		if (size == 0)
 			continue;
Signature · 1 line
2.17.1