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