xref: /dflybsd-src/crypto/openssh/utf8.c (revision 0cbfa66cdb87e23928a110d9b02839f403e32c11)
1*0cbfa66cSDaniel Fojt /* $OpenBSD: utf8.c,v 1.11 2020/05/01 06:28:52 djm Exp $ */
2e9778795SPeter Avalos /*
3e9778795SPeter Avalos  * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
4e9778795SPeter Avalos  *
5e9778795SPeter Avalos  * Permission to use, copy, modify, and distribute this software for any
6e9778795SPeter Avalos  * purpose with or without fee is hereby granted, provided that the above
7e9778795SPeter Avalos  * copyright notice and this permission notice appear in all copies.
8e9778795SPeter Avalos  *
9e9778795SPeter Avalos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10e9778795SPeter Avalos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11e9778795SPeter Avalos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12e9778795SPeter Avalos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13e9778795SPeter Avalos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14e9778795SPeter Avalos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15e9778795SPeter Avalos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16e9778795SPeter Avalos  */
17e9778795SPeter Avalos 
18e9778795SPeter Avalos /*
19e9778795SPeter Avalos  * Utility functions for multibyte-character handling,
20e9778795SPeter Avalos  * in particular to sanitize untrusted strings for terminal output.
21e9778795SPeter Avalos  */
22e9778795SPeter Avalos 
23e9778795SPeter Avalos #include "includes.h"
24e9778795SPeter Avalos 
25e9778795SPeter Avalos #include <sys/types.h>
26e9778795SPeter Avalos #ifdef HAVE_LANGINFO_H
27e9778795SPeter Avalos # include <langinfo.h>
28e9778795SPeter Avalos #endif
29e9778795SPeter Avalos #include <limits.h>
30ce74bacaSMatthew Dillon #include <locale.h>
31e9778795SPeter Avalos #include <stdarg.h>
32e9778795SPeter Avalos #include <stdio.h>
33e9778795SPeter Avalos #include <stdlib.h>
34e9778795SPeter Avalos #include <string.h>
35e9778795SPeter Avalos #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
36e9778795SPeter Avalos # include <vis.h>
37e9778795SPeter Avalos #endif
38e9778795SPeter Avalos #ifdef HAVE_WCHAR_H
39e9778795SPeter Avalos # include <wchar.h>
40e9778795SPeter Avalos #endif
41e9778795SPeter Avalos 
42e9778795SPeter Avalos #include "utf8.h"
43e9778795SPeter Avalos 
44e9778795SPeter Avalos static int	 dangerous_locale(void);
45e9778795SPeter Avalos static int	 grow_dst(char **, size_t *, size_t, char **, size_t);
46e9778795SPeter Avalos 
47e9778795SPeter Avalos 
48e9778795SPeter Avalos /*
49e9778795SPeter Avalos  * For US-ASCII and UTF-8 encodings, we can safely recover from
50e9778795SPeter Avalos  * encoding errors and from non-printable characters.  For any
51e9778795SPeter Avalos  * other encodings, err to the side of caution and abort parsing:
52e9778795SPeter Avalos  * For state-dependent encodings, recovery is impossible.
53e9778795SPeter Avalos  * For arbitrary encodings, replacement of non-printable
54e9778795SPeter Avalos  * characters would be non-trivial and too fragile.
55664f4763Szrj  * The comments indicate what nl_langinfo(CODESET)
56664f4763Szrj  * returns for US-ASCII on various operating systems.
57e9778795SPeter Avalos  */
58e9778795SPeter Avalos 
59e9778795SPeter Avalos static int
dangerous_locale(void)60e9778795SPeter Avalos dangerous_locale(void) {
61e9778795SPeter Avalos 	char	*loc;
62e9778795SPeter Avalos 
63e9778795SPeter Avalos 	loc = nl_langinfo(CODESET);
64664f4763Szrj 	return strcmp(loc, "UTF-8") != 0 &&
65664f4763Szrj 	    strcmp(loc, "US-ASCII") != 0 &&		/* OpenBSD */
66664f4763Szrj 	    strcmp(loc, "ANSI_X3.4-1968") != 0 &&	/* Linux */
67664f4763Szrj 	    strcmp(loc, "ISO8859-1") != 0 &&		/* AIX */
68664f4763Szrj 	    strcmp(loc, "646") != 0 &&			/* Solaris, NetBSD */
69664f4763Szrj 	    strcmp(loc, "") != 0;			/* Solaris 6 */
70e9778795SPeter Avalos }
71e9778795SPeter Avalos 
72e9778795SPeter Avalos static int
grow_dst(char ** dst,size_t * sz,size_t maxsz,char ** dp,size_t need)73e9778795SPeter Avalos grow_dst(char **dst, size_t *sz, size_t maxsz, char **dp, size_t need)
74e9778795SPeter Avalos {
75e9778795SPeter Avalos 	char	*tp;
76e9778795SPeter Avalos 	size_t	 tsz;
77e9778795SPeter Avalos 
78e9778795SPeter Avalos 	if (*dp + need < *dst + *sz)
79e9778795SPeter Avalos 		return 0;
80e9778795SPeter Avalos 	tsz = *sz + 128;
81e9778795SPeter Avalos 	if (tsz > maxsz)
82e9778795SPeter Avalos 		tsz = maxsz;
83ce74bacaSMatthew Dillon 	if ((tp = recallocarray(*dst, *sz, tsz, 1)) == NULL)
84e9778795SPeter Avalos 		return -1;
85e9778795SPeter Avalos 	*dp = tp + (*dp - *dst);
86e9778795SPeter Avalos 	*dst = tp;
87e9778795SPeter Avalos 	*sz = tsz;
88e9778795SPeter Avalos 	return 0;
89e9778795SPeter Avalos }
90e9778795SPeter Avalos 
91e9778795SPeter Avalos /*
92e9778795SPeter Avalos  * The following two functions limit the number of bytes written,
93e9778795SPeter Avalos  * including the terminating '\0', to sz.  Unless wp is NULL,
94e9778795SPeter Avalos  * they limit the number of display columns occupied to *wp.
95e9778795SPeter Avalos  * Whichever is reached first terminates the output string.
96e9778795SPeter Avalos  * To stay close to the standard interfaces, they return the number of
97e9778795SPeter Avalos  * non-NUL bytes that would have been written if both were unlimited.
98e9778795SPeter Avalos  * If wp is NULL, newline, carriage return, and tab are allowed;
99e9778795SPeter Avalos  * otherwise, the actual number of columns occupied by what was
100e9778795SPeter Avalos  * written is returned in *wp.
101e9778795SPeter Avalos  */
102e9778795SPeter Avalos 
103*0cbfa66cSDaniel Fojt int
vasnmprintf(char ** str,size_t maxsz,int * wp,const char * fmt,va_list ap)104e9778795SPeter Avalos vasnmprintf(char **str, size_t maxsz, int *wp, const char *fmt, va_list ap)
105e9778795SPeter Avalos {
106e9778795SPeter Avalos 	char	*src;	/* Source string returned from vasprintf. */
107e9778795SPeter Avalos 	char	*sp;	/* Pointer into src. */
108e9778795SPeter Avalos 	char	*dst;	/* Destination string to be returned. */
109e9778795SPeter Avalos 	char	*dp;	/* Pointer into dst. */
110e9778795SPeter Avalos 	char	*tp;	/* Temporary pointer for dst. */
111e9778795SPeter Avalos 	size_t	 sz;	/* Number of bytes allocated for dst. */
112e9778795SPeter Avalos 	wchar_t	 wc;	/* Wide character at sp. */
113e9778795SPeter Avalos 	int	 len;	/* Number of bytes in the character at sp. */
114e9778795SPeter Avalos 	int	 ret;	/* Number of bytes needed to format src. */
115e9778795SPeter Avalos 	int	 width;	/* Display width of the character wc. */
116e9778795SPeter Avalos 	int	 total_width, max_width, print;
117e9778795SPeter Avalos 
118e9778795SPeter Avalos 	src = NULL;
119e9778795SPeter Avalos 	if ((ret = vasprintf(&src, fmt, ap)) <= 0)
120e9778795SPeter Avalos 		goto fail;
121e9778795SPeter Avalos 
122e9778795SPeter Avalos 	sz = strlen(src) + 1;
123e9778795SPeter Avalos 	if ((dst = malloc(sz)) == NULL) {
124e9778795SPeter Avalos 		free(src);
125ce74bacaSMatthew Dillon 		ret = -1;
126e9778795SPeter Avalos 		goto fail;
127e9778795SPeter Avalos 	}
128e9778795SPeter Avalos 
129e9778795SPeter Avalos 	if (maxsz > INT_MAX)
130e9778795SPeter Avalos 		maxsz = INT_MAX;
131e9778795SPeter Avalos 
132e9778795SPeter Avalos 	sp = src;
133e9778795SPeter Avalos 	dp = dst;
134e9778795SPeter Avalos 	ret = 0;
135e9778795SPeter Avalos 	print = 1;
136e9778795SPeter Avalos 	total_width = 0;
137e9778795SPeter Avalos 	max_width = wp == NULL ? INT_MAX : *wp;
138e9778795SPeter Avalos 	while (*sp != '\0') {
139e9778795SPeter Avalos 		if ((len = mbtowc(&wc, sp, MB_CUR_MAX)) == -1) {
140e9778795SPeter Avalos 			(void)mbtowc(NULL, NULL, MB_CUR_MAX);
141e9778795SPeter Avalos 			if (dangerous_locale()) {
142e9778795SPeter Avalos 				ret = -1;
143e9778795SPeter Avalos 				break;
144e9778795SPeter Avalos 			}
145e9778795SPeter Avalos 			len = 1;
146e9778795SPeter Avalos 			width = -1;
147e9778795SPeter Avalos 		} else if (wp == NULL &&
148e9778795SPeter Avalos 		    (wc == L'\n' || wc == L'\r' || wc == L'\t')) {
149e9778795SPeter Avalos 			/*
150e9778795SPeter Avalos 			 * Don't use width uninitialized; the actual
151e9778795SPeter Avalos 			 * value doesn't matter because total_width
152e9778795SPeter Avalos 			 * is only returned for wp != NULL.
153e9778795SPeter Avalos 			 */
154e9778795SPeter Avalos 			width = 0;
155e9778795SPeter Avalos 		} else if ((width = wcwidth(wc)) == -1 &&
156e9778795SPeter Avalos 		    dangerous_locale()) {
157e9778795SPeter Avalos 			ret = -1;
158e9778795SPeter Avalos 			break;
159e9778795SPeter Avalos 		}
160e9778795SPeter Avalos 
161e9778795SPeter Avalos 		/* Valid, printable character. */
162e9778795SPeter Avalos 
163e9778795SPeter Avalos 		if (width >= 0) {
164e9778795SPeter Avalos 			if (print && (dp - dst >= (int)maxsz - len ||
165e9778795SPeter Avalos 			    total_width > max_width - width))
166e9778795SPeter Avalos 				print = 0;
167e9778795SPeter Avalos 			if (print) {
168e9778795SPeter Avalos 				if (grow_dst(&dst, &sz, maxsz,
169e9778795SPeter Avalos 				    &dp, len) == -1) {
170e9778795SPeter Avalos 					ret = -1;
171e9778795SPeter Avalos 					break;
172e9778795SPeter Avalos 				}
173e9778795SPeter Avalos 				total_width += width;
174e9778795SPeter Avalos 				memcpy(dp, sp, len);
175e9778795SPeter Avalos 				dp += len;
176e9778795SPeter Avalos 			}
177e9778795SPeter Avalos 			sp += len;
178e9778795SPeter Avalos 			if (ret >= 0)
179e9778795SPeter Avalos 				ret += len;
180e9778795SPeter Avalos 			continue;
181e9778795SPeter Avalos 		}
182e9778795SPeter Avalos 
183e9778795SPeter Avalos 		/* Escaping required. */
184e9778795SPeter Avalos 
185e9778795SPeter Avalos 		while (len > 0) {
186e9778795SPeter Avalos 			if (print && (dp - dst >= (int)maxsz - 4 ||
187e9778795SPeter Avalos 			    total_width > max_width - 4))
188e9778795SPeter Avalos 				print = 0;
189e9778795SPeter Avalos 			if (print) {
190e9778795SPeter Avalos 				if (grow_dst(&dst, &sz, maxsz,
191e9778795SPeter Avalos 				    &dp, 4) == -1) {
192e9778795SPeter Avalos 					ret = -1;
193e9778795SPeter Avalos 					break;
194e9778795SPeter Avalos 				}
195e9778795SPeter Avalos 				tp = vis(dp, *sp, VIS_OCTAL | VIS_ALL, 0);
196e9778795SPeter Avalos 				width = tp - dp;
197e9778795SPeter Avalos 				total_width += width;
198e9778795SPeter Avalos 				dp = tp;
199e9778795SPeter Avalos 			} else
200e9778795SPeter Avalos 				width = 4;
201e9778795SPeter Avalos 			len--;
202e9778795SPeter Avalos 			sp++;
203e9778795SPeter Avalos 			if (ret >= 0)
204e9778795SPeter Avalos 				ret += width;
205e9778795SPeter Avalos 		}
206e9778795SPeter Avalos 		if (len > 0)
207e9778795SPeter Avalos 			break;
208e9778795SPeter Avalos 	}
209e9778795SPeter Avalos 	free(src);
210e9778795SPeter Avalos 	*dp = '\0';
211e9778795SPeter Avalos 	*str = dst;
212e9778795SPeter Avalos 	if (wp != NULL)
213e9778795SPeter Avalos 		*wp = total_width;
214e9778795SPeter Avalos 
215e9778795SPeter Avalos 	/*
216e9778795SPeter Avalos 	 * If the string was truncated by the width limit but
217e9778795SPeter Avalos 	 * would have fit into the size limit, the only sane way
218e9778795SPeter Avalos 	 * to report the problem is using the return value, such
219e9778795SPeter Avalos 	 * that the usual idiom "if (ret < 0 || ret >= sz) error"
220e9778795SPeter Avalos 	 * works as expected.
221e9778795SPeter Avalos 	 */
222e9778795SPeter Avalos 
223e9778795SPeter Avalos 	if (ret < (int)maxsz && !print)
224e9778795SPeter Avalos 		ret = -1;
225e9778795SPeter Avalos 	return ret;
226e9778795SPeter Avalos 
227e9778795SPeter Avalos fail:
228e9778795SPeter Avalos 	if (wp != NULL)
229e9778795SPeter Avalos 		*wp = 0;
230e9778795SPeter Avalos 	if (ret == 0) {
231e9778795SPeter Avalos 		*str = src;
232e9778795SPeter Avalos 		return 0;
233e9778795SPeter Avalos 	} else {
234e9778795SPeter Avalos 		*str = NULL;
235e9778795SPeter Avalos 		return -1;
236e9778795SPeter Avalos 	}
237e9778795SPeter Avalos }
238e9778795SPeter Avalos 
239e9778795SPeter Avalos int
snmprintf(char * str,size_t sz,int * wp,const char * fmt,...)240e9778795SPeter Avalos snmprintf(char *str, size_t sz, int *wp, const char *fmt, ...)
241e9778795SPeter Avalos {
242e9778795SPeter Avalos 	va_list	 ap;
243*0cbfa66cSDaniel Fojt 	char	*cp = NULL;
244e9778795SPeter Avalos 	int	 ret;
245e9778795SPeter Avalos 
246e9778795SPeter Avalos 	va_start(ap, fmt);
247e9778795SPeter Avalos 	ret = vasnmprintf(&cp, sz, wp, fmt, ap);
248e9778795SPeter Avalos 	va_end(ap);
249e9778795SPeter Avalos 	if (cp != NULL) {
250e9778795SPeter Avalos 		(void)strlcpy(str, cp, sz);
251e9778795SPeter Avalos 		free(cp);
252e9778795SPeter Avalos 	} else
253e9778795SPeter Avalos 		*str = '\0';
254e9778795SPeter Avalos 	return ret;
255e9778795SPeter Avalos }
256e9778795SPeter Avalos 
257*0cbfa66cSDaniel Fojt int
asmprintf(char ** outp,size_t sz,int * wp,const char * fmt,...)258*0cbfa66cSDaniel Fojt asmprintf(char **outp, size_t sz, int *wp, const char *fmt, ...)
259*0cbfa66cSDaniel Fojt {
260*0cbfa66cSDaniel Fojt 	va_list	 ap;
261*0cbfa66cSDaniel Fojt 	int	 ret;
262*0cbfa66cSDaniel Fojt 
263*0cbfa66cSDaniel Fojt 	*outp = NULL;
264*0cbfa66cSDaniel Fojt 	va_start(ap, fmt);
265*0cbfa66cSDaniel Fojt 	ret = vasnmprintf(outp, sz, wp, fmt, ap);
266*0cbfa66cSDaniel Fojt 	va_end(ap);
267*0cbfa66cSDaniel Fojt 
268*0cbfa66cSDaniel Fojt 	return ret;
269*0cbfa66cSDaniel Fojt }
270*0cbfa66cSDaniel Fojt 
271e9778795SPeter Avalos /*
272e9778795SPeter Avalos  * To stay close to the standard interfaces, the following functions
273e9778795SPeter Avalos  * return the number of non-NUL bytes written.
274e9778795SPeter Avalos  */
275e9778795SPeter Avalos 
276e9778795SPeter Avalos int
vfmprintf(FILE * stream,const char * fmt,va_list ap)277e9778795SPeter Avalos vfmprintf(FILE *stream, const char *fmt, va_list ap)
278e9778795SPeter Avalos {
279*0cbfa66cSDaniel Fojt 	char	*str = NULL;
280e9778795SPeter Avalos 	int	 ret;
281e9778795SPeter Avalos 
282*0cbfa66cSDaniel Fojt 	if ((ret = vasnmprintf(&str, INT_MAX, NULL, fmt, ap)) < 0) {
283*0cbfa66cSDaniel Fojt 		free(str);
284e9778795SPeter Avalos 		return -1;
285*0cbfa66cSDaniel Fojt 	}
286e9778795SPeter Avalos 	if (fputs(str, stream) == EOF)
287e9778795SPeter Avalos 		ret = -1;
288e9778795SPeter Avalos 	free(str);
289e9778795SPeter Avalos 	return ret;
290e9778795SPeter Avalos }
291e9778795SPeter Avalos 
292e9778795SPeter Avalos int
fmprintf(FILE * stream,const char * fmt,...)293e9778795SPeter Avalos fmprintf(FILE *stream, const char *fmt, ...)
294e9778795SPeter Avalos {
295e9778795SPeter Avalos 	va_list	 ap;
296e9778795SPeter Avalos 	int	 ret;
297e9778795SPeter Avalos 
298e9778795SPeter Avalos 	va_start(ap, fmt);
299e9778795SPeter Avalos 	ret = vfmprintf(stream, fmt, ap);
300e9778795SPeter Avalos 	va_end(ap);
301e9778795SPeter Avalos 	return ret;
302e9778795SPeter Avalos }
303e9778795SPeter Avalos 
304e9778795SPeter Avalos int
mprintf(const char * fmt,...)305e9778795SPeter Avalos mprintf(const char *fmt, ...)
306e9778795SPeter Avalos {
307e9778795SPeter Avalos 	va_list	 ap;
308e9778795SPeter Avalos 	int	 ret;
309e9778795SPeter Avalos 
310e9778795SPeter Avalos 	va_start(ap, fmt);
311e9778795SPeter Avalos 	ret = vfmprintf(stdout, fmt, ap);
312e9778795SPeter Avalos 	va_end(ap);
313e9778795SPeter Avalos 	return ret;
314e9778795SPeter Avalos }
315ce74bacaSMatthew Dillon 
316ce74bacaSMatthew Dillon /*
317ce74bacaSMatthew Dillon  * Set up libc for multibyte output in the user's chosen locale.
318ce74bacaSMatthew Dillon  *
319ce74bacaSMatthew Dillon  * XXX: we are known to have problems with Turkish (i/I confusion) so we
320ce74bacaSMatthew Dillon  *      deliberately fall back to the C locale for now. Longer term we should
321ce74bacaSMatthew Dillon  *      always prefer to select C.[encoding] if possible, but there's no
322ce74bacaSMatthew Dillon  *      standardisation in locales between systems, so we'll need to survey
323ce74bacaSMatthew Dillon  *      what's out there first.
324ce74bacaSMatthew Dillon  */
325ce74bacaSMatthew Dillon void
msetlocale(void)326ce74bacaSMatthew Dillon msetlocale(void)
327ce74bacaSMatthew Dillon {
328ce74bacaSMatthew Dillon 	const char *vars[] = { "LC_ALL", "LC_CTYPE", "LANG", NULL };
329ce74bacaSMatthew Dillon 	char *cp;
330ce74bacaSMatthew Dillon 	int i;
331ce74bacaSMatthew Dillon 
332ce74bacaSMatthew Dillon 	/*
333ce74bacaSMatthew Dillon 	 * We can't yet cope with dotless/dotted I in Turkish locales,
334ce74bacaSMatthew Dillon 	 * so fall back to the C locale for these.
335ce74bacaSMatthew Dillon 	 */
336ce74bacaSMatthew Dillon 	for (i = 0; vars[i] != NULL; i++) {
337ce74bacaSMatthew Dillon 		if ((cp = getenv(vars[i])) == NULL)
338ce74bacaSMatthew Dillon 			continue;
339ce74bacaSMatthew Dillon 		if (strncasecmp(cp, "TR", 2) != 0)
340ce74bacaSMatthew Dillon 			break;
341ce74bacaSMatthew Dillon 		/*
342ce74bacaSMatthew Dillon 		 * If we're in a UTF-8 locale then prefer to use
343ce74bacaSMatthew Dillon 		 * the C.UTF-8 locale (or equivalent) if it exists.
344ce74bacaSMatthew Dillon 		 */
345ce74bacaSMatthew Dillon 		if ((strcasestr(cp, "UTF-8") != NULL ||
346ce74bacaSMatthew Dillon 		    strcasestr(cp, "UTF8") != NULL) &&
347ce74bacaSMatthew Dillon 		    (setlocale(LC_CTYPE, "C.UTF-8") != NULL ||
348ce74bacaSMatthew Dillon 		    setlocale(LC_CTYPE, "POSIX.UTF-8") != NULL))
349ce74bacaSMatthew Dillon 			return;
350ce74bacaSMatthew Dillon 		setlocale(LC_CTYPE, "C");
351ce74bacaSMatthew Dillon 		return;
352ce74bacaSMatthew Dillon 	}
353ce74bacaSMatthew Dillon 	/* We can handle this locale */
354ce74bacaSMatthew Dillon 	setlocale(LC_CTYPE, "");
355ce74bacaSMatthew Dillon }
356