Index | Thread | Search

From:
Rafael Sadowski <rafael@sizeofvoid.org>
Subject:
Re: smtpd: validate the UTF-8-iness of AUTH PLAIN
To:
Omar Polo <op@omarpolo.com>
Cc:
tech@openbsd.org
Date:
Thu, 12 Nov 2026 07:31:48 +0100

Download raw body.

Thread
On Mon Nov 09, 2026 at 03:54:08PM +0100, Omar Polo wrote:
> rfc4616 (section 2 in particular) [0] describes the AUTH PLAIN method, and
> requires username and password to be UTF-8 encoded strings of unicode
> characters.

I can't see many advantages in doing that for passwords (apart from the
fact that non-compliant RFC4616 clients might encounter issues), but
I'd do it anyway!

> 
> [0]: https://www.rfc-editor.org/info/rfc4616/#section-2
> 
> At the moment we're not really caring, but some of this data can be
> passed not only to auth_userokay(), but to external programs (via
> tables) as well.  So I think the extra validation make sense.
> 
> I didn't find something upon a quick grep in src/ to validate utf8 so I
> *didn't* wrote my own, but rather re-used another one that I've
> previously used across several projects.  it's small, compact, and built
> on a state machine so it's nice =)
> 
> we could also make use of it in other parts of smtpd in the future is my
> hope.

relayd/httpd could need something like that too.

> 
> thoughs?
> 
> 
> diff /home/op/w/smtpd
> path + /home/op/w/smtpd
> commit - cf11025edfb764b317335d997f83f8ef4bee792d
> blob - a4923ed2706b368d48fec3e7e77fcf8e3f50d824
> file + smtp_session.c
> --- smtp_session.c	
> +++ smtp_session.c	
> @@ -32,6 +32,7 @@
>  #include "smtpd.h"
>  #include "log.h"
>  #include "rfc5322.h"
> +#include "utf8d.h"
>  
>  #define	SMTP_LINE_MAX			65535
>  #define	DATA_HIWAT			65535
> @@ -1912,6 +1913,18 @@ smtp_proceed_rollback(struct smtp_session *s, const ch
>  	smtp_enter_state(s, STATE_HELO);
>  }
>  
> +static int
> +is_valid_utf8(const char *s)
> +{
> +	uint32_t cp, state = UTF8_ACCEPT;
> +
> +	for (; *s; ++s) {
> +		if (utf8_decode(&state, &cp, (unsigned char)*s) == UTF8_REJECT)
> +			return 0;
> +	}
> +	return (state == UTF8_ACCEPT);
> +}
> +
>  static void
>  smtp_rfc4954_auth_plain(struct smtp_session *s, char *arg)
>  {
> @@ -1956,6 +1969,9 @@ smtp_rfc4954_auth_plain(struct smtp_session *s, char *
>  		if (pass[strcspn(pass, "\r\n")] != '\0')
>  			goto abort;
>  
> +		if (!is_valid_utf8(user) || !is_valid_utf8(pass))
> +			goto abort;
> +
>  		m_create(p_lka,  IMSG_SMTP_AUTHENTICATE, 0, 0, -1);
>  		m_add_id(p_lka, s->id);
>  		m_add_string(p_lka, s->listener->authtable);
> commit - cf11025edfb764b317335d997f83f8ef4bee792d
> blob - /dev/null
> file + utf8d.h (mode 644)
> --- /dev/null	
> +++ utf8d.h	
> @@ -0,0 +1,55 @@
> +/*
> + * Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> + * copy of this software and associated documentation files (the "Software"),
> + * to deal in the Software without restriction, including without limitation
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> + * and/or sell copies of the Software, and to permit persons to whom the
> + * Software is furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice shall be included in
> + * all copies or substantial portions of the Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
> + * DEALINGS IN THE SOFTWARE.
> + */
> +
> +// See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
> +
> +#define UTF8_ACCEPT 0
> +#define UTF8_REJECT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1,1,1,1,1,1,1,1,1,1,1,1,1,1, // s7..s8
> +};
> +
> +static uint32_t inline
> +utf8_decode(uint32_t* state, uint32_t* codep, uint32_t byte) {
> +  uint32_t type = utf8d[byte];

byte is used as an index in utf8d, so any value > 255 reads out of
bounds? I think byte should be uint8_t and you can save the cast in the
caller.

> +
> +  *codep = (*state != UTF8_ACCEPT) ?
> +    (byte & 0x3fu) | (*codep << 6) :
> +    (0xff >> type) & (byte);
> +
> +  *state = utf8d[256 + *state*16 + type];
> +  return *state;
> +}
> 
> 
>