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