git: d93901491d49 - stable/14 - tftpd: Keep proper track of option length

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Mon, 16 Nov 2026 15:17:56 UTC
The branch stable/14 has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=d93901491d49258cd1a234a40f2816e8ca945329

commit d93901491d49258cd1a234a40f2816e8ca945329
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-11-11 19:45:24 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-16 14:52:04 +0000

    tftpd: Keep proper track of option length
    
    When parsing options, keep track of the length of the unparsed portion
    and pass that to get_field() instead of the length of the entire header.
    
    While here, switch both make_options() and parse_options() to using
    size_t for sizes and lengths.
    
    MFC after:      1 week
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D61048
    
    (cherry picked from commit 35cd70490f4b7ec1921db980ae3992192e4cbfbb)
---
 libexec/tftpd/tftp-options.c | 29 ++++++++++++++---------------
 libexec/tftpd/tftp-options.h |  6 +++---
 2 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/libexec/tftpd/tftp-options.c b/libexec/tftpd/tftp-options.c
index ee413603fbfb..da4a020f5e1a 100644
--- a/libexec/tftpd/tftp-options.c
+++ b/libexec/tftpd/tftp-options.c
@@ -366,13 +366,12 @@ option_windowsize(int peer)
 /*
  * Append the available options to the header
  */
-uint16_t
-make_options(int peer __unused, char *buffer, uint16_t size) {
-	int	i;
+size_t
+make_options(int peer __unused, char *buffer, size_t size)
+{
 	char	*value;
 	const char *option;
-	uint16_t length;
-	uint16_t returnsize = 0;
+	size_t	 i, length, returnsize = 0;
 
 	if (!options_rfc_enabled) return (0);
 
@@ -393,7 +392,7 @@ make_options(int peer __unused, char *buffer, uint16_t size) {
 			tftp_log(LOG_ERR,
 			    "Running out of option space for "
 			    "option '%s' with value '%s': "
-			    "needed %d bytes, got %d bytes",
+			    "needed %zu bytes, got %zu bytes",
 			    option, value, size, length);
 			continue;
 		}
@@ -411,24 +410,26 @@ make_options(int peer __unused, char *buffer, uint16_t size) {
  * Parse the received options in the header
  */
 int
-parse_options(int peer, char *buffer, uint16_t size)
+parse_options(int peer, char *buffer, size_t size)
 {
+	char	*c, *cp, *end, *option, *value;
+	size_t	len;
 	int	i, options_failed;
-	char	*c, *cp, *option, *value;
 
 	if (!options_rfc_enabled) return (0);
 
 	/* Parse the options */
 	cp = buffer;
+	end = buffer + size;
 	options_failed = 0;
-	while (size > 0) {
+	while (cp < end) {
 		option = cp;
-		i = get_field(peer, cp, size);
-		cp += i;
+		len = get_field(peer, cp, end - cp);
+		cp += len;
 
 		value = cp;
-		i = get_field(peer, cp, size);
-		cp += i;
+		len = get_field(peer, cp, end - cp);
+		cp += len;
 
 		/* We are at the end */
 		if (*option == '\0') break;
@@ -460,8 +461,6 @@ parse_options(int peer, char *buffer, uint16_t size)
 		if (options[i].o_type == NULL)
 			tftp_log(LOG_WARNING,
 			    "Unknown option: '%s'", option);
-
-		size -= strlen(option) + strlen(value) + 2;
 	}
 
 	return (options_failed);
diff --git a/libexec/tftpd/tftp-options.h b/libexec/tftpd/tftp-options.h
index f1b0a5cfaf32..26f0191b1542 100644
--- a/libexec/tftpd/tftp-options.h
+++ b/libexec/tftpd/tftp-options.h
@@ -29,9 +29,9 @@
  * Options
  */
 
-void		init_options(void);
-uint16_t	make_options(int peer, char *buffer, uint16_t size);
-int		parse_options(int peer, char *buffer, uint16_t size);
+void	init_options(void);
+size_t	make_options(int peer, char *buffer, size_t size);
+int	parse_options(int peer, char *buffer, size_t size);
 
 /* Call back functions */
 int	option_tsize(int peer, struct tftphdr *, int, struct stat *);