thebeanogamer / rpms / qemu-kvm

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