6e7d01
From 52bf635da30c75d0fdb0a3e7e7b9a2483ca033fc Mon Sep 17 00:00:00 2001
6e7d01
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
6e7d01
Date: Thu, 29 Jul 2021 04:55:59 -0400
6e7d01
Subject: [PATCH 05/14] Add mtod_check()
6e7d01
MIME-Version: 1.0
6e7d01
Content-Type: text/plain; charset=UTF-8
6e7d01
Content-Transfer-Encoding: 8bit
6e7d01
6e7d01
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
6e7d01
Message-id: <20210708082537.1550263-2-marcandre.lureau@redhat.com>
6e7d01
Patchwork-id: 101819
6e7d01
O-Subject: [RHEL-8.5.0 qemu-kvm PATCH 1/8] Add mtod_check()
6e7d01
Bugzilla: 1970819 1970835 1970843 1970853
6e7d01
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
6e7d01
RH-Acked-by: Eric Blake <eblake@redhat.com>
6e7d01
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
6e7d01
6e7d01
From: Marc-André Lureau <marcandre.lureau@redhat.com>
6e7d01
6e7d01
Recent security issues demonstrate the lack of safety care when casting
6e7d01
a mbuf to a particular structure type. At least, it should check that
6e7d01
the buffer is large enough. The following patches will make use of this
6e7d01
function.
6e7d01
6e7d01
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
6e7d01
6e7d01
(cherry picked from commit 93e645e72a056ec0b2c16e0299fc5c6b94e4ca17)
6e7d01
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
6e7d01
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
6e7d01
---
6e7d01
 slirp/src/mbuf.c | 11 +++++++++++
6e7d01
 slirp/src/mbuf.h |  1 +
6e7d01
 2 files changed, 12 insertions(+)
6e7d01
6e7d01
diff --git a/slirp/src/mbuf.c b/slirp/src/mbuf.c
6e7d01
index 4fd62282a9..6d0653ed3d 100644
6e7d01
--- a/slirp/src/mbuf.c
6e7d01
+++ b/slirp/src/mbuf.c
6e7d01
@@ -222,3 +222,14 @@ struct mbuf *dtom(Slirp *slirp, void *dat)
6e7d01
 
6e7d01
     return (struct mbuf *)0;
6e7d01
 }
6e7d01
+
6e7d01
+void *mtod_check(struct mbuf *m, size_t len)
6e7d01
+{
6e7d01
+    if (m->m_len >= len) {
6e7d01
+        return m->m_data;
6e7d01
+    }
6e7d01
+
6e7d01
+    DEBUG_ERROR("mtod failed");
6e7d01
+
6e7d01
+    return NULL;
6e7d01
+}
6e7d01
diff --git a/slirp/src/mbuf.h b/slirp/src/mbuf.h
6e7d01
index 546e7852c5..2015e3232f 100644
6e7d01
--- a/slirp/src/mbuf.h
6e7d01
+++ b/slirp/src/mbuf.h
6e7d01
@@ -118,6 +118,7 @@ void m_inc(struct mbuf *, int);
6e7d01
 void m_adj(struct mbuf *, int);
6e7d01
 int m_copy(struct mbuf *, struct mbuf *, int, int);
6e7d01
 struct mbuf *dtom(Slirp *, void *);
6e7d01
+void *mtod_check(struct mbuf *, size_t len);
6e7d01
 
6e7d01
 static inline void ifs_init(struct mbuf *ifm)
6e7d01
 {
6e7d01
-- 
6e7d01
2.27.0
6e7d01