9fa2c0
From 87f5d0c70bdb46d467d32e3c3a8d7a472c97e148 Mon Sep 17 00:00:00 2001
9fa2c0
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
9fa2c0
Date: Fri, 4 Jun 2021 15:58:25 +0400
9fa2c0
Subject: [PATCH 1/7] Add mtod_check()
9fa2c0
MIME-Version: 1.0
9fa2c0
Content-Type: text/plain; charset=UTF-8
9fa2c0
Content-Transfer-Encoding: 8bit
9fa2c0
9fa2c0
Recent security issues demonstrate the lack of safety care when casting
9fa2c0
a mbuf to a particular structure type. At least, it should check that
9fa2c0
the buffer is large enough. The following patches will make use of this
9fa2c0
function.
9fa2c0
9fa2c0
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
9fa2c0
(cherry picked from commit 93e645e72a056ec0b2c16e0299fc5c6b94e4ca17)
9fa2c0
---
9fa2c0
 src/mbuf.c | 11 +++++++++++
9fa2c0
 src/mbuf.h |  1 +
9fa2c0
 2 files changed, 12 insertions(+)
9fa2c0
9fa2c0
diff --git a/src/mbuf.c b/src/mbuf.c
9fa2c0
index 54ec721..cb2e971 100644
9fa2c0
--- a/src/mbuf.c
9fa2c0
+++ b/src/mbuf.c
9fa2c0
@@ -222,3 +222,14 @@ struct mbuf *dtom(Slirp *slirp, void *dat)
9fa2c0
 
9fa2c0
     return (struct mbuf *)0;
9fa2c0
 }
9fa2c0
+
9fa2c0
+void *mtod_check(struct mbuf *m, size_t len)
9fa2c0
+{
9fa2c0
+    if (m->m_len >= len) {
9fa2c0
+        return m->m_data;
9fa2c0
+    }
9fa2c0
+
9fa2c0
+    DEBUG_ERROR("mtod failed");
9fa2c0
+
9fa2c0
+    return NULL;
9fa2c0
+}
9fa2c0
diff --git a/src/mbuf.h b/src/mbuf.h
9fa2c0
index 546e785..2015e32 100644
9fa2c0
--- a/src/mbuf.h
9fa2c0
+++ b/src/mbuf.h
9fa2c0
@@ -118,6 +118,7 @@ void m_inc(struct mbuf *, int);
9fa2c0
 void m_adj(struct mbuf *, int);
9fa2c0
 int m_copy(struct mbuf *, struct mbuf *, int, int);
9fa2c0
 struct mbuf *dtom(Slirp *, void *);
9fa2c0
+void *mtod_check(struct mbuf *, size_t len);
9fa2c0
 
9fa2c0
 static inline void ifs_init(struct mbuf *ifm)
9fa2c0
 {
9fa2c0
-- 
9fa2c0
2.29.0
9fa2c0