9fa2c0
From c8665ebbdadb72f7c2cb74b9704f68704c13653b Mon Sep 17 00:00:00 2001
9fa2c0
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
9fa2c0
Date: Fri, 4 Jun 2021 20:01:20 +0400
9fa2c0
Subject: [PATCH 6/7] tftp: introduce a header structure
9fa2c0
MIME-Version: 1.0
9fa2c0
Content-Type: text/plain; charset=UTF-8
9fa2c0
Content-Transfer-Encoding: 8bit
9fa2c0
9fa2c0
Instead of using a composed structure and potentially reading past the
9fa2c0
incoming buffer, use a different structure for the header.
9fa2c0
9fa2c0
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
9fa2c0
(cherry picked from commit 990163cf3ac86b7875559f49602c4d76f46f6f30)
9fa2c0
---
9fa2c0
 src/tftp.c | 60 ++++++++++++++++++++++++++++--------------------------
9fa2c0
 src/tftp.h |  6 +++++-
9fa2c0
 2 files changed, 36 insertions(+), 30 deletions(-)
9fa2c0
9fa2c0
diff --git a/src/tftp.c b/src/tftp.c
9fa2c0
index e06911d..a19c889 100644
9fa2c0
--- a/src/tftp.c
9fa2c0
+++ b/src/tftp.c
9fa2c0
@@ -50,7 +50,7 @@ static void tftp_session_terminate(struct tftp_session *spt)
9fa2c0
 }
9fa2c0
 
9fa2c0
 static int tftp_session_allocate(Slirp *slirp, struct sockaddr_storage *srcsas,
9fa2c0
-                                 struct tftp_t *tp)
9fa2c0
+                                 struct tftphdr *hdr)
9fa2c0
 {
9fa2c0
     struct tftp_session *spt;
9fa2c0
     int k;
9fa2c0
@@ -75,7 +75,7 @@ found:
9fa2c0
     memcpy(&spt->client_addr, srcsas, sockaddr_size(srcsas));
9fa2c0
     spt->fd = -1;
9fa2c0
     spt->block_size = 512;
9fa2c0
-    spt->client_port = tp->udp.uh_sport;
9fa2c0
+    spt->client_port = hdr->udp.uh_sport;
9fa2c0
     spt->slirp = slirp;
9fa2c0
 
9fa2c0
     tftp_session_update(spt);
9fa2c0
@@ -84,7 +84,7 @@ found:
9fa2c0
 }
9fa2c0
 
9fa2c0
 static int tftp_session_find(Slirp *slirp, struct sockaddr_storage *srcsas,
9fa2c0
-                             struct tftp_t *tp)
9fa2c0
+                             struct tftphdr *hdr)
9fa2c0
 {
9fa2c0
     struct tftp_session *spt;
9fa2c0
     int k;
9fa2c0
@@ -94,7 +94,7 @@ static int tftp_session_find(Slirp *slirp, struct sockaddr_storage *srcsas,
9fa2c0
 
9fa2c0
         if (tftp_session_in_use(spt)) {
9fa2c0
             if (sockaddr_equal(&spt->client_addr, srcsas)) {
9fa2c0
-                if (spt->client_port == tp->udp.uh_sport) {
9fa2c0
+                if (spt->client_port == hdr->udp.uh_sport) {
9fa2c0
                     return k;
9fa2c0
                 }
9fa2c0
             }
9fa2c0
@@ -148,13 +148,13 @@ static struct tftp_t *tftp_prep_mbuf_data(struct tftp_session *spt,
9fa2c0
 }
9fa2c0
 
9fa2c0
 static void tftp_udp_output(struct tftp_session *spt, struct mbuf *m,
9fa2c0
-                            struct tftp_t *recv_tp)
9fa2c0
+                            struct tftphdr *hdr)
9fa2c0
 {
9fa2c0
     if (spt->client_addr.ss_family == AF_INET6) {
9fa2c0
         struct sockaddr_in6 sa6, da6;
9fa2c0
 
9fa2c0
         sa6.sin6_addr = spt->slirp->vhost_addr6;
9fa2c0
-        sa6.sin6_port = recv_tp->udp.uh_dport;
9fa2c0
+        sa6.sin6_port = hdr->udp.uh_dport;
9fa2c0
         da6.sin6_addr = ((struct sockaddr_in6 *)&spt->client_addr)->sin6_addr;
9fa2c0
         da6.sin6_port = spt->client_port;
9fa2c0
 
9fa2c0
@@ -163,7 +163,7 @@ static void tftp_udp_output(struct tftp_session *spt, struct mbuf *m,
9fa2c0
         struct sockaddr_in sa4, da4;
9fa2c0
 
9fa2c0
         sa4.sin_addr = spt->slirp->vhost_addr;
9fa2c0
-        sa4.sin_port = recv_tp->udp.uh_dport;
9fa2c0
+        sa4.sin_port = hdr->udp.uh_dport;
9fa2c0
         da4.sin_addr = ((struct sockaddr_in *)&spt->client_addr)->sin_addr;
9fa2c0
         da4.sin_port = spt->client_port;
9fa2c0
 
9fa2c0
@@ -185,14 +185,14 @@ static int tftp_send_oack(struct tftp_session *spt, const char *keys[],
9fa2c0
 
9fa2c0
     tp = tftp_prep_mbuf_data(spt, m);
9fa2c0
 
9fa2c0
-    tp->tp_op = htons(TFTP_OACK);
9fa2c0
+    tp->hdr.tp_op = htons(TFTP_OACK);
9fa2c0
     for (i = 0; i < nb; i++) {
9fa2c0
         n += slirp_fmt0(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s", keys[i]);
9fa2c0
         n += slirp_fmt0(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u", values[i]);
9fa2c0
     }
9fa2c0
 
9fa2c0
-    m->m_len = G_SIZEOF_MEMBER(struct tftp_t, tp_op) + n;
9fa2c0
-    tftp_udp_output(spt, m, recv_tp);
9fa2c0
+    m->m_len = G_SIZEOF_MEMBER(struct tftp_t, hdr.tp_op) + n;
9fa2c0
+    tftp_udp_output(spt, m, &recv_tp->hdr);
9fa2c0
 
9fa2c0
     return 0;
9fa2c0
 }
9fa2c0
@@ -213,21 +213,21 @@ static void tftp_send_error(struct tftp_session *spt, uint16_t errorcode,
9fa2c0
 
9fa2c0
     tp = tftp_prep_mbuf_data(spt, m);
9fa2c0
 
9fa2c0
-    tp->tp_op = htons(TFTP_ERROR);
9fa2c0
+    tp->hdr.tp_op = htons(TFTP_ERROR);
9fa2c0
     tp->x.tp_error.tp_error_code = htons(errorcode);
9fa2c0
     slirp_pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg),
9fa2c0
                   msg);
9fa2c0
 
9fa2c0
     m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX + 2) + 3 +
9fa2c0
                strlen(msg) - sizeof(struct udphdr);
9fa2c0
-    tftp_udp_output(spt, m, recv_tp);
9fa2c0
+    tftp_udp_output(spt, m, &recv_tp->hdr);
9fa2c0
 
9fa2c0
 out:
9fa2c0
     tftp_session_terminate(spt);
9fa2c0
 }
9fa2c0
 
9fa2c0
 static void tftp_send_next_block(struct tftp_session *spt,
9fa2c0
-                                 struct tftp_t *recv_tp)
9fa2c0
+                                 struct tftphdr *hdr)
9fa2c0
 {
9fa2c0
     struct mbuf *m;
9fa2c0
     struct tftp_t *tp;
9fa2c0
@@ -241,7 +241,7 @@ static void tftp_send_next_block(struct tftp_session *spt,
9fa2c0
 
9fa2c0
     tp = tftp_prep_mbuf_data(spt, m);
9fa2c0
 
9fa2c0
-    tp->tp_op = htons(TFTP_DATA);
9fa2c0
+    tp->hdr.tp_op = htons(TFTP_DATA);
9fa2c0
     tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
9fa2c0
 
9fa2c0
     nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf,
9fa2c0
@@ -259,7 +259,7 @@ static void tftp_send_next_block(struct tftp_session *spt,
9fa2c0
 
9fa2c0
     m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX - nobytes) -
9fa2c0
                sizeof(struct udphdr);
9fa2c0
-    tftp_udp_output(spt, m, recv_tp);
9fa2c0
+    tftp_udp_output(spt, m, hdr);
9fa2c0
 
9fa2c0
     if (nobytes == spt->block_size) {
9fa2c0
         tftp_session_update(spt);
9fa2c0
@@ -282,12 +282,12 @@ static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas,
9fa2c0
     int nb_options = 0;
9fa2c0
 
9fa2c0
     /* check if a session already exists and if so terminate it */
9fa2c0
-    s = tftp_session_find(slirp, srcsas, tp);
9fa2c0
+    s = tftp_session_find(slirp, srcsas, &tp->hdr);
9fa2c0
     if (s >= 0) {
9fa2c0
         tftp_session_terminate(&slirp->tftp_sessions[s]);
9fa2c0
     }
9fa2c0
 
9fa2c0
-    s = tftp_session_allocate(slirp, srcsas, tp);
9fa2c0
+    s = tftp_session_allocate(slirp, srcsas, &tp->hdr);
9fa2c0
 
9fa2c0
     if (s < 0) {
9fa2c0
         return;
9fa2c0
@@ -413,29 +413,29 @@ static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas,
9fa2c0
     }
9fa2c0
 
9fa2c0
     spt->block_nr = 0;
9fa2c0
-    tftp_send_next_block(spt, tp);
9fa2c0
+    tftp_send_next_block(spt, &tp->hdr);
9fa2c0
 }
9fa2c0
 
9fa2c0
 static void tftp_handle_ack(Slirp *slirp, struct sockaddr_storage *srcsas,
9fa2c0
-                            struct tftp_t *tp, int pktlen)
9fa2c0
+                            struct tftphdr *hdr)
9fa2c0
 {
9fa2c0
     int s;
9fa2c0
 
9fa2c0
-    s = tftp_session_find(slirp, srcsas, tp);
9fa2c0
+    s = tftp_session_find(slirp, srcsas, hdr);
9fa2c0
 
9fa2c0
     if (s < 0) {
9fa2c0
         return;
9fa2c0
     }
9fa2c0
 
9fa2c0
-    tftp_send_next_block(&slirp->tftp_sessions[s], tp);
9fa2c0
+    tftp_send_next_block(&slirp->tftp_sessions[s], hdr);
9fa2c0
 }
9fa2c0
 
9fa2c0
 static void tftp_handle_error(Slirp *slirp, struct sockaddr_storage *srcsas,
9fa2c0
-                              struct tftp_t *tp, int pktlen)
9fa2c0
+                              struct tftphdr *hdr)
9fa2c0
 {
9fa2c0
     int s;
9fa2c0
 
9fa2c0
-    s = tftp_session_find(slirp, srcsas, tp);
9fa2c0
+    s = tftp_session_find(slirp, srcsas, hdr);
9fa2c0
 
9fa2c0
     if (s < 0) {
9fa2c0
         return;
9fa2c0
@@ -446,23 +446,25 @@ static void tftp_handle_error(Slirp *slirp, struct sockaddr_storage *srcsas,
9fa2c0
 
9fa2c0
 void tftp_input(struct sockaddr_storage *srcsas, struct mbuf *m)
9fa2c0
 {
9fa2c0
-    struct tftp_t *tp = mtod_check(m, offsetof(struct tftp_t, x.tp_buf));
9fa2c0
+    struct tftphdr *hdr = mtod_check(m, sizeof(struct tftphdr));
9fa2c0
 
9fa2c0
-    if (tp == NULL) {
9fa2c0
+    if (hdr == NULL) {
9fa2c0
         return;
9fa2c0
     }
9fa2c0
 
9fa2c0
-    switch (ntohs(tp->tp_op)) {
9fa2c0
+    switch (ntohs(hdr->tp_op)) {
9fa2c0
     case TFTP_RRQ:
9fa2c0
-        tftp_handle_rrq(m->slirp, srcsas, tp, m->m_len);
9fa2c0
+        tftp_handle_rrq(m->slirp, srcsas,
9fa2c0
+                        mtod(m, struct tftp_t *),
9fa2c0
+                        m->m_len);
9fa2c0
         break;
9fa2c0
 
9fa2c0
     case TFTP_ACK:
9fa2c0
-        tftp_handle_ack(m->slirp, srcsas, tp, m->m_len);
9fa2c0
+        tftp_handle_ack(m->slirp, srcsas, hdr);
9fa2c0
         break;
9fa2c0
 
9fa2c0
     case TFTP_ERROR:
9fa2c0
-        tftp_handle_error(m->slirp, srcsas, tp, m->m_len);
9fa2c0
+        tftp_handle_error(m->slirp, srcsas, hdr);
9fa2c0
         break;
9fa2c0
     }
9fa2c0
 }
9fa2c0
diff --git a/src/tftp.h b/src/tftp.h
9fa2c0
index 6d75478..cafab03 100644
9fa2c0
--- a/src/tftp.h
9fa2c0
+++ b/src/tftp.h
9fa2c0
@@ -20,9 +20,13 @@
9fa2c0
 #define TFTP_FILENAME_MAX 512
9fa2c0
 #define TFTP_BLOCKSIZE_MAX 1428
9fa2c0
 
9fa2c0
-struct tftp_t {
9fa2c0
+struct tftphdr {
9fa2c0
     struct udphdr udp;
9fa2c0
     uint16_t tp_op;
9fa2c0
+} SLIRP_PACKED;
9fa2c0
+
9fa2c0
+struct tftp_t {
9fa2c0
+    struct tftphdr hdr;
9fa2c0
     union {
9fa2c0
         struct {
9fa2c0
             uint16_t tp_block_nr;
9fa2c0
-- 
9fa2c0
2.29.0
9fa2c0