Skip to content

Commit dec6334

Browse files
committed
upstream: add a sshbuf_get_nulterminated_string() function to pull a
\0- terminated string from a sshbuf. Intended to be used to improve parsing of SOCKS headers for dynamic forwarding. ok deraadt; feedback Tim van der Molen OpenBSD-Commit-ID: cf93d6db4730f7518d5269c279e16b172b484b36
1 parent a8718c3 commit dec6334

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

sshbuf-getput-basic.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: sshbuf-getput-basic.c,v 1.13 2022/05/25 06:03:44 djm Exp $ */
1+
/* $OpenBSD: sshbuf-getput-basic.c,v 1.14 2025/11/21 01:29:06 djm Exp $ */
22
/*
33
* Copyright (c) 2011 Damien Miller
44
*
@@ -629,3 +629,41 @@ sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf,
629629
}
630630
return 0;
631631
}
632+
633+
int
634+
sshbuf_get_nulterminated_string(struct sshbuf *buf, size_t maxlen,
635+
char **valp, size_t *lenp)
636+
{
637+
const u_char zero = 0;
638+
char *val = NULL;
639+
size_t len = 0;
640+
int r;
641+
642+
if (valp != NULL)
643+
*valp = NULL;
644+
if (lenp != NULL)
645+
*lenp = 0;
646+
if ((r = sshbuf_find(buf, 0, &zero, sizeof(zero), &len)) != 0) {
647+
if (r == SSH_ERR_INVALID_FORMAT && sshbuf_len(buf) < maxlen)
648+
return SSH_ERR_MESSAGE_INCOMPLETE;
649+
return r;
650+
}
651+
if (len > maxlen)
652+
return SSH_ERR_INVALID_FORMAT;
653+
/* can strdup() because it's definitely nul-terminated */
654+
if ((val = strdup(sshbuf_ptr(buf))) == NULL)
655+
return SSH_ERR_ALLOC_FAIL;
656+
if ((r = sshbuf_consume(buf, len + 1)) != 0)
657+
goto out;
658+
/* success */
659+
r = 0;
660+
if (valp != NULL) {
661+
*valp = val;
662+
val = NULL;
663+
}
664+
if (lenp != NULL)
665+
*lenp = len;
666+
out:
667+
free(val);
668+
return r;
669+
}

sshbuf.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: sshbuf.h,v 1.32 2025/09/02 09:41:23 djm Exp $ */
1+
/* $OpenBSD: sshbuf.h,v 1.33 2025/11/21 01:29:06 djm Exp $ */
22
/*
33
* Copyright (c) 2011 Damien Miller
44
*
@@ -229,6 +229,10 @@ int sshbuf_put_ec_pkey(struct sshbuf *buf, EVP_PKEY *pkey);
229229
# endif /* OPENSSL_HAS_ECC */
230230
#endif /* WITH_OPENSSL */
231231

232+
/* Functions to extract or store various non-SSH wire encoded values */
233+
int sshbuf_get_nulterminated_string(struct sshbuf *buf, size_t maxlen,
234+
char **valp, size_t *lenp);
235+
232236
/* Dump the contents of the buffer in a human-readable format */
233237
void sshbuf_dump(const struct sshbuf *buf, FILE *f);
234238

0 commit comments

Comments
 (0)