cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
1072c8
From af72e344459614fcf2746739f05494ef7e691a78 Mon Sep 17 00:00:00 2001
1072c8
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
1072c8
Date: Thu, 29 Jul 2021 04:56:36 -0400
1072c8
Subject: [PATCH 10/14] tftp: introduce a header structure
1072c8
MIME-Version: 1.0
1072c8
Content-Type: text/plain; charset=UTF-8
1072c8
Content-Transfer-Encoding: 8bit
1072c8
1072c8
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
1072c8
Message-id: <20210708082537.1550263-7-marcandre.lureau@redhat.com>
1072c8
Patchwork-id: 101825
1072c8
O-Subject: [RHEL-8.5.0 qemu-kvm PATCH 6/8] tftp: introduce a header structure
1072c8
Bugzilla: 1970819 1970835 1970843 1970853
1072c8
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
1072c8
RH-Acked-by: Eric Blake <eblake@redhat.com>
1072c8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
1072c8
1072c8
From: Marc-André Lureau <marcandre.lureau@redhat.com>
1072c8
1072c8
Instead of using a composed structure and potentially reading past the
1072c8
incoming buffer, use a different structure for the header.
1072c8
1072c8
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
1072c8
1072c8
(cherry picked from commit 990163cf3ac86b7875559f49602c4d76f46f6f30)
1072c8
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
1072c8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
1072c8
---
1072c8
 slirp/src/tftp.c | 58 +++++++++++++++++++++++++-----------------------
1072c8
 slirp/src/tftp.h |  6 ++++-
1072c8
 2 files changed, 35 insertions(+), 29 deletions(-)
1072c8
1072c8
diff --git a/slirp/src/tftp.c b/slirp/src/tftp.c
1072c8
index 07e8f3cb2f..53e04d0aeb 100644
1072c8
--- a/slirp/src/tftp.c
1072c8
+++ b/slirp/src/tftp.c
1072c8
@@ -50,7 +50,7 @@ static void tftp_session_terminate(struct tftp_session *spt)
1072c8
 }
1072c8
 
1072c8
 static int tftp_session_allocate(Slirp *slirp, struct sockaddr_storage *srcsas,
1072c8
-                                 struct tftp_t *tp)
1072c8
+                                 struct tftphdr *hdr)
1072c8
 {
1072c8
     struct tftp_session *spt;
1072c8
     int k;
1072c8
@@ -75,7 +75,7 @@ found:
1072c8
     memcpy(&spt->client_addr, srcsas, sockaddr_size(srcsas));
1072c8
     spt->fd = -1;
1072c8
     spt->block_size = 512;
1072c8
-    spt->client_port = tp->udp.uh_sport;
1072c8
+    spt->client_port = hdr->udp.uh_sport;
1072c8
     spt->slirp = slirp;
1072c8
 
1072c8
     tftp_session_update(spt);
1072c8
@@ -84,7 +84,7 @@ found:
1072c8
 }
1072c8
 
1072c8
 static int tftp_session_find(Slirp *slirp, struct sockaddr_storage *srcsas,
1072c8
-                             struct tftp_t *tp)
1072c8
+                             struct tftphdr *hdr)
1072c8
 {
1072c8
     struct tftp_session *spt;
1072c8
     int k;
1072c8
@@ -94,7 +94,7 @@ static int tftp_session_find(Slirp *slirp, struct sockaddr_storage *srcsas,
1072c8
 
1072c8
         if (tftp_session_in_use(spt)) {
1072c8
             if (sockaddr_equal(&spt->client_addr, srcsas)) {
1072c8
-                if (spt->client_port == tp->udp.uh_sport) {
1072c8
+                if (spt->client_port == hdr->udp.uh_sport) {
1072c8
                     return k;
1072c8
                 }
1072c8
             }
1072c8
@@ -146,13 +146,13 @@ static struct tftp_t *tftp_prep_mbuf_data(struct tftp_session *spt,
1072c8
 }
1072c8
 
1072c8
 static void tftp_udp_output(struct tftp_session *spt, struct mbuf *m,
1072c8
-                            struct tftp_t *recv_tp)
1072c8
+                            struct tftphdr *hdr)
1072c8
 {
1072c8
     if (spt->client_addr.ss_family == AF_INET6) {
1072c8
         struct sockaddr_in6 sa6, da6;
1072c8
 
1072c8
         sa6.sin6_addr = spt->slirp->vhost_addr6;
1072c8
-        sa6.sin6_port = recv_tp->udp.uh_dport;
1072c8
+        sa6.sin6_port = hdr->udp.uh_dport;
1072c8
         da6.sin6_addr = ((struct sockaddr_in6 *)&spt->client_addr)->sin6_addr;
1072c8
         da6.sin6_port = spt->client_port;
1072c8
 
1072c8
@@ -161,7 +161,7 @@ static void tftp_udp_output(struct tftp_session *spt, struct mbuf *m,
1072c8
         struct sockaddr_in sa4, da4;
1072c8
 
1072c8
         sa4.sin_addr = spt->slirp->vhost_addr;
1072c8
-        sa4.sin_port = recv_tp->udp.uh_dport;
1072c8
+        sa4.sin_port = hdr->udp.uh_dport;
1072c8
         da4.sin_addr = ((struct sockaddr_in *)&spt->client_addr)->sin_addr;
1072c8
         da4.sin_port = spt->client_port;
1072c8
 
1072c8
@@ -183,7 +183,7 @@ static int tftp_send_oack(struct tftp_session *spt, const char *keys[],
1072c8
 
1072c8
     tp = tftp_prep_mbuf_data(spt, m);
1072c8
 
1072c8
-    tp->tp_op = htons(TFTP_OACK);
1072c8
+    tp->hdr.tp_op = htons(TFTP_OACK);
1072c8
     for (i = 0; i < nb; i++) {
1072c8
         n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
1072c8
                       keys[i]) +
1072c8
@@ -195,7 +195,7 @@ static int tftp_send_oack(struct tftp_session *spt, const char *keys[],
1072c8
 
1072c8
     m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX + 2) + n -
1072c8
                sizeof(struct udphdr);
1072c8
-    tftp_udp_output(spt, m, recv_tp);
1072c8
+    tftp_udp_output(spt, m, &recv_tp->hdr);
1072c8
 
1072c8
     return 0;
1072c8
 }
1072c8
@@ -216,21 +216,21 @@ static void tftp_send_error(struct tftp_session *spt, uint16_t errorcode,
1072c8
 
1072c8
     tp = tftp_prep_mbuf_data(spt, m);
1072c8
 
1072c8
-    tp->tp_op = htons(TFTP_ERROR);
1072c8
+    tp->hdr.tp_op = htons(TFTP_ERROR);
1072c8
     tp->x.tp_error.tp_error_code = htons(errorcode);
1072c8
     slirp_pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg),
1072c8
                   msg);
1072c8
 
1072c8
     m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX + 2) + 3 +
1072c8
                strlen(msg) - sizeof(struct udphdr);
1072c8
-    tftp_udp_output(spt, m, recv_tp);
1072c8
+    tftp_udp_output(spt, m, &recv_tp->hdr);
1072c8
 
1072c8
 out:
1072c8
     tftp_session_terminate(spt);
1072c8
 }
1072c8
 
1072c8
 static void tftp_send_next_block(struct tftp_session *spt,
1072c8
-                                 struct tftp_t *recv_tp)
1072c8
+                                 struct tftphdr *hdr)
1072c8
 {
1072c8
     struct mbuf *m;
1072c8
     struct tftp_t *tp;
1072c8
@@ -244,7 +244,7 @@ static void tftp_send_next_block(struct tftp_session *spt,
1072c8
 
1072c8
     tp = tftp_prep_mbuf_data(spt, m);
1072c8
 
1072c8
-    tp->tp_op = htons(TFTP_DATA);
1072c8
+    tp->hdr.tp_op = htons(TFTP_DATA);
1072c8
     tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
1072c8
 
1072c8
     nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf,
1072c8
@@ -262,7 +262,7 @@ static void tftp_send_next_block(struct tftp_session *spt,
1072c8
 
1072c8
     m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX - nobytes) -
1072c8
                sizeof(struct udphdr);
1072c8
-    tftp_udp_output(spt, m, recv_tp);
1072c8
+    tftp_udp_output(spt, m, hdr);
1072c8
 
1072c8
     if (nobytes == spt->block_size) {
1072c8
         tftp_session_update(spt);
1072c8
@@ -285,12 +285,12 @@ static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas,
1072c8
     int nb_options = 0;
1072c8
 
1072c8
     /* check if a session already exists and if so terminate it */
1072c8
-    s = tftp_session_find(slirp, srcsas, tp);
1072c8
+    s = tftp_session_find(slirp, srcsas, &tp->hdr);
1072c8
     if (s >= 0) {
1072c8
         tftp_session_terminate(&slirp->tftp_sessions[s]);
1072c8
     }
1072c8
 
1072c8
-    s = tftp_session_allocate(slirp, srcsas, tp);
1072c8
+    s = tftp_session_allocate(slirp, srcsas, &tp->hdr);
1072c8
 
1072c8
     if (s < 0) {
1072c8
         return;
1072c8
@@ -411,29 +411,29 @@ static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas,
1072c8
     }
1072c8
 
1072c8
     spt->block_nr = 0;
1072c8
-    tftp_send_next_block(spt, tp);
1072c8
+    tftp_send_next_block(spt, &tp->hdr);
1072c8
 }
1072c8
 
1072c8
 static void tftp_handle_ack(Slirp *slirp, struct sockaddr_storage *srcsas,
1072c8
-                            struct tftp_t *tp, int pktlen)
1072c8
+                            struct tftphdr *hdr)
1072c8
 {
1072c8
     int s;
1072c8
 
1072c8
-    s = tftp_session_find(slirp, srcsas, tp);
1072c8
+    s = tftp_session_find(slirp, srcsas, hdr);
1072c8
 
1072c8
     if (s < 0) {
1072c8
         return;
1072c8
     }
1072c8
 
1072c8
-    tftp_send_next_block(&slirp->tftp_sessions[s], tp);
1072c8
+    tftp_send_next_block(&slirp->tftp_sessions[s], hdr);
1072c8
 }
1072c8
 
1072c8
 static void tftp_handle_error(Slirp *slirp, struct sockaddr_storage *srcsas,
1072c8
-                              struct tftp_t *tp, int pktlen)
1072c8
+                              struct tftphdr *hdr)
1072c8
 {
1072c8
     int s;
1072c8
 
1072c8
-    s = tftp_session_find(slirp, srcsas, tp);
1072c8
+    s = tftp_session_find(slirp, srcsas, hdr);
1072c8
 
1072c8
     if (s < 0) {
1072c8
         return;
1072c8
@@ -444,23 +444,25 @@ static void tftp_handle_error(Slirp *slirp, struct sockaddr_storage *srcsas,
1072c8
 
1072c8
 void tftp_input(struct sockaddr_storage *srcsas, struct mbuf *m)
1072c8
 {
1072c8
-    struct tftp_t *tp = mtod_check(m, offsetof(struct tftp_t, x.tp_buf));
1072c8
+    struct tftphdr *hdr = mtod_check(m, sizeof(struct tftphdr));
1072c8
 
1072c8
-    if (tp == NULL) {
1072c8
+    if (hdr == NULL) {
1072c8
         return;
1072c8
     }
1072c8
 
1072c8
-    switch (ntohs(tp->tp_op)) {
1072c8
+    switch (ntohs(hdr->tp_op)) {
1072c8
     case TFTP_RRQ:
1072c8
-        tftp_handle_rrq(m->slirp, srcsas, tp, m->m_len);
1072c8
+        tftp_handle_rrq(m->slirp, srcsas,
1072c8
+                        mtod(m, struct tftp_t *),
1072c8
+                        m->m_len);
1072c8
         break;
1072c8
 
1072c8
     case TFTP_ACK:
1072c8
-        tftp_handle_ack(m->slirp, srcsas, tp, m->m_len);
1072c8
+        tftp_handle_ack(m->slirp, srcsas, hdr);
1072c8
         break;
1072c8
 
1072c8
     case TFTP_ERROR:
1072c8
-        tftp_handle_error(m->slirp, srcsas, tp, m->m_len);
1072c8
+        tftp_handle_error(m->slirp, srcsas, hdr);
1072c8
         break;
1072c8
     }
1072c8
 }
1072c8
diff --git a/slirp/src/tftp.h b/slirp/src/tftp.h
1072c8
index c47bb43c7d..021f6cf109 100644
1072c8
--- a/slirp/src/tftp.h
1072c8
+++ b/slirp/src/tftp.h
1072c8
@@ -18,9 +18,13 @@
1072c8
 #define TFTP_FILENAME_MAX 512
1072c8
 #define TFTP_BLOCKSIZE_MAX 1428
1072c8
 
1072c8
-struct tftp_t {
1072c8
+struct tftphdr {
1072c8
     struct udphdr udp;
1072c8
     uint16_t tp_op;
1072c8
+} SLIRP_PACKED;
1072c8
+
1072c8
+struct tftp_t {
1072c8
+    struct tftphdr hdr;
1072c8
     union {
1072c8
         struct {
1072c8
             uint16_t tp_block_nr;
1072c8
-- 
1072c8
2.27.0
1072c8