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