Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 20 Feb 2001 03:46:45 -0800
From:      Kris Kennaway <kris@obsecurity.org>
To:        arch@freebsd.org
Subject:   fmtcheck()
Message-ID:  <20010220034644.A26123@mollari.cthul.hu>

next in thread | raw e-mail | index | archive | help

--neYutvxvOLaeuPCA
Content-Type: multipart/mixed; boundary="x+6KMIRAuhnl3hBn"
Content-Disposition: inline


--x+6KMIRAuhnl3hBn
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

I'm intending to import the fmtcheck() function from NetBSD, to aid in
fixing format string bogons and potential vulnerabilities.  I'd
appreciate stylistic comments, especially about libc and mdoc magic
which may need to be applied to the attached files.

Kris


--x+6KMIRAuhnl3hBn
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=p

Index: lib/libc/gen/Makefile.inc
===================================================================
RCS file: /mnt/ncvs/src/lib/libc/gen/Makefile.inc,v
retrieving revision 1.74
diff -u -r1.74 Makefile.inc
--- lib/libc/gen/Makefile.inc	2001/02/05 14:55:14	1.74
+++ lib/libc/gen/Makefile.inc	2001/02/20 11:01:03
@@ -9,7 +9,7 @@
 	clock.c closedir.c confstr.c \
 	crypt.c ctermid.c daemon.c devname.c dirname.c disklabel.c \
 	dlfcn.c drand48.c erand48.c err.c errlst.c \
-	exec.c fnmatch.c fstab.c ftok.c fts.c getbootfile.c getbsize.c \
+	exec.c fmtcheck.c fnmatch.c fstab.c ftok.c fts.c getbootfile.c getbsize.c \
 	getcap.c getcwd.c getdomainname.c getgrent.c getgrouplist.c \
 	gethostname.c getloadavg.c getlogin.c getmntinfo.c getnetgrent.c \
 	getobjformat.c getosreldate.c getpagesize.c \
@@ -38,7 +38,7 @@
 	basename.3 \
 	confstr.3 ctermid.3 daemon.3 \
 	devname.3 directory.3 dirname.3 dladdr.3 dllockinit.3 dlopen.3 \
-	err.3 exec.3 fnmatch.3 frexp.3 ftok.3 fts.3 \
+	err.3 exec.3 fmtcheck.3 fnmatch.3 frexp.3 ftok.3 fts.3 \
 	getbootfile.3 getbsize.3 getcap.3 getcwd.3 \
 	getdiskbyname.3 getdomainname.3 getfsent.3 \
 	getgrent.3 getgrouplist.3 gethostname.3 getloadavg.3 \
Index: include/stdio.h
===================================================================
RCS file: /mnt/ncvs/src/include/stdio.h,v
retrieving revision 1.30
diff -u -r1.30 stdio.h
--- include/stdio.h	2001/02/16 06:11:21	1.30
+++ include/stdio.h	2001/02/20 11:03:54
@@ -304,6 +304,8 @@
 __BEGIN_DECLS
 int	 asprintf __P((char **, const char *, ...)) __printflike(2, 3);
 char	*ctermid_r __P((char *));
+const char *fmtcheck __P((const char *, const char *))
+	__attribute__((__format_arg__(2)));;
 char	*fgetln __P((FILE *, size_t *));
 int	 fpurge __P((FILE *));
 int	 fseeko __P((FILE *, _BSD_OFF_T_, int));

--x+6KMIRAuhnl3hBn
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="fmtcheck.c"
Content-Transfer-Encoding: quoted-printable

/*	$FreeBSD$	*/
/*	$NetBSD: fmtcheck.c,v 1.2 2000/11/01 01:17:20 briggs Exp $	*/

/*-
 * Copyright (c) 2000 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code was contributed to The NetBSD Foundation by Allen Briggs.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMI=
TED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICUL=
AR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF T=
HE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
static const char rcsid[] =3D
  "$FreeBSD$";
#endif /* not lint */

#include "namespace.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#ifdef __weak_alias
__weak_alias(fmtcheck,__fmtcheck)
#endif

enum __e_fmtcheck_types {
	FMTCHECK_START,
	FMTCHECK_SHORT,
	FMTCHECK_INT,
	FMTCHECK_LONG,
	FMTCHECK_QUAD,
	FMTCHECK_SHORTPOINTER,
	FMTCHECK_INTPOINTER,
	FMTCHECK_LONGPOINTER,
	FMTCHECK_QUADPOINTER,
	FMTCHECK_DOUBLE,
	FMTCHECK_LONGDOUBLE,
	FMTCHECK_STRING,
	FMTCHECK_WIDTH,
	FMTCHECK_PRECISION,
	FMTCHECK_DONE,
	FMTCHECK_UNKNOWN
};
typedef enum __e_fmtcheck_types EFT;

#define RETURN(pf,f,r) do { \
			*(pf) =3D (f); \
			return r; \
		       } /*NOTREACHED*/ /*CONSTCOND*/ while (0)

static EFT
get_next_format_from_precision(const char **pf)
{
	int		sh, lg, quad, longdouble;
	const char	*f;

	sh =3D lg =3D quad =3D longdouble =3D 0;

	f =3D *pf;
	switch (*f) {
	case 'h':
		f++;
		sh =3D 1;
		break;
	case 'l':
		f++;
		if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
		if (*f =3D=3D 'l') {
			f++;
			quad =3D 1;
		} else {
			lg =3D 1;
		}
		break;
	case 'q':
		f++;
		quad =3D 1;
		break;
	case 'L':
		f++;
		longdouble =3D 1;
		break;
	default:
		break;
	}
	if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
	if (strchr("diouxX", *f)) {
		if (longdouble)
			RETURN(pf,f,FMTCHECK_UNKNOWN);
		if (lg)
			RETURN(pf,f,FMTCHECK_LONG);
		if (quad)
			RETURN(pf,f,FMTCHECK_QUAD);
		RETURN(pf,f,FMTCHECK_INT);
	}
	if (*f =3D=3D 'n') {
		if (longdouble)
			RETURN(pf,f,FMTCHECK_UNKNOWN);
		if (sh)
			RETURN(pf,f,FMTCHECK_SHORTPOINTER);
		if (lg)
			RETURN(pf,f,FMTCHECK_LONGPOINTER);
		if (quad)
			RETURN(pf,f,FMTCHECK_QUADPOINTER);
		RETURN(pf,f,FMTCHECK_INTPOINTER);
	}
	if (strchr("DOU", *f)) {
		if (sh + lg + quad + longdouble)
			RETURN(pf,f,FMTCHECK_UNKNOWN);
		RETURN(pf,f,FMTCHECK_LONG);
	}
	if (strchr("eEfg", *f)) {
		if (longdouble)
			RETURN(pf,f,FMTCHECK_LONGDOUBLE);
		if (sh + lg + quad)
			RETURN(pf,f,FMTCHECK_UNKNOWN);
		RETURN(pf,f,FMTCHECK_DOUBLE);
	}
	if (*f =3D=3D 'c') {
		if (sh + lg + quad + longdouble)
			RETURN(pf,f,FMTCHECK_UNKNOWN);
		RETURN(pf,f,FMTCHECK_INT);
	}
	if (*f =3D=3D 's') {
		if (sh + lg + quad + longdouble)
			RETURN(pf,f,FMTCHECK_UNKNOWN);
		RETURN(pf,f,FMTCHECK_STRING);
	}
	if (*f =3D=3D 'p') {
		if (sh + lg + quad + longdouble)
			RETURN(pf,f,FMTCHECK_UNKNOWN);
		RETURN(pf,f,FMTCHECK_LONG);
	}
	RETURN(pf,f,FMTCHECK_UNKNOWN);
	/*NOTREACHED*/
}

static EFT
get_next_format_from_width(const char **pf)
{
	const char	*f;

	f =3D *pf;
	if (*f =3D=3D '.') {
		f++;
		if (*f =3D=3D '*') {
			RETURN(pf,f,FMTCHECK_PRECISION);
		}
		/* eat any precision (empty is allowed) */
		while (isdigit(*f)) f++;
		if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
	}
	RETURN(pf,f,get_next_format_from_precision(pf));
	/*NOTREACHED*/
}

static EFT
get_next_format(const char **pf, EFT eft)
{
	int		infmt;
	const char	*f;

	if (eft =3D=3D FMTCHECK_WIDTH) {
		(*pf)++;
		return get_next_format_from_width(pf);
	} else if (eft =3D=3D FMTCHECK_PRECISION) {
		(*pf)++;
		return get_next_format_from_precision(pf);
	}

	f =3D *pf;
	infmt =3D 0;
	while (!infmt) {
		f =3D strchr(f, '%');
		if (f =3D=3D NULL)
			RETURN(pf,f,FMTCHECK_DONE);
		f++;
		if (!*f)
			RETURN(pf,f,FMTCHECK_UNKNOWN);
		if (*f !=3D '%')
			infmt =3D 1;
		else
			f++;
	}

	/* Eat any of the flags */
	while (*f && (strchr("#0- +", *f)))
		f++;

	if (*f =3D=3D '*') {
		RETURN(pf,f,FMTCHECK_WIDTH);
	}
	/* eat any width */
	while (isdigit(*f)) f++;
	if (!*f) {
		RETURN(pf,f,FMTCHECK_UNKNOWN);
	}

	RETURN(pf,f,get_next_format_from_width(pf));
	/*NOTREACHED*/
}

__const char *
fmtcheck(const char *f1, const char *f2)
{
	const char	*f1p, *f2p;
	EFT		f1t, f2t;

	if (!f1) return f2;
=09
	f1p =3D f1;
	f1t =3D FMTCHECK_START;
	f2p =3D f2;
	f2t =3D FMTCHECK_START;
	while ((f1t =3D get_next_format(&f1p, f1t)) !=3D FMTCHECK_DONE) {
		if (f1t =3D=3D FMTCHECK_UNKNOWN)
			return f2;
		f2t =3D get_next_format(&f2p, f2t);
		if (f1t !=3D f2t)
			return f2;
	}
	return f1;
}

--x+6KMIRAuhnl3hBn
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="fmtcheck.3"

.\"	$NetBSD$
.\"
.\" Copyright (c) 2000 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This file was contributed to The NetBSD Foundation by Allen Briggs.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\"    must display the following acknowledgement:
.\"        This product includes software developed by the NetBSD
.\"        Foundation, Inc. and its contributors.
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
.\"    contributors may be used to endorse or promote products derived
.\"    from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd October 17, 2000
.Os
.Dt FMTCHECK 3
.Sh NAME
.Nm fmtcheck
.Nd sanitizes user-supplied printf(3)-style format string
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.Fd #include <stdio.h>
.Ft const char *
.Fn fmtcheck "const char *fmt_suspect" "const char *fmt_default"
.Sh DESCRIPTION
The
.Nm
function scans
.Fa fmt_suspect
and
.Fa fmt_default
to determine if
.Fa fmt_suspect
will consume the same argument types as
.Fa fmt_default
and to ensure that
.Fa fmt_suspect
is a valid format string.
.Pp
The
.Xr printf 3
family of functions can not verify the types of arguments that they are
passed at run-time.  In some cases, like
.Xr catgets 3 ,
it is useful or necessary to use a user-supplied format string with no
guarantee that the format string matches the specified parameters.
.Pp
The
.Nm
function was designed to be used in these cases, as in:
.Bd -literal -offset indent
printf(fmtcheck(user_format, standard_format), arg1, arg2);
.Ed
.Pp
In the check, field widths, fillers, precisions, etc. are ignored (unless
the field width or precision is an asterisk
.Ql *
instead of a digit string).  Also, any text other than the format specifiers
is completely ignored.
.Pp
Note that the formats may be quite different as long as they accept the
same parameters.  For example, ".Dq %p %o %30s %#llx %-10.*e %n" is
compatible with "This number %lu %d%% and string %s has %qd numbers
and %.*g floats (%n)."  However, "%o" is not equivalent to "%lx" because
the first requires an integer and the second requires a long.
.Sh RETURN VALUES
If
.Fa fmt_suspect
is a valid format and consumes the same argument types as
.Fa fmt_default ,
then the
.Nm
function will return
.Fa fmt_suspect .
Otherwise, it will return
.Fa fmt_default .
.Sh SEE ALSO
.Xr printf 3

--x+6KMIRAuhnl3hBn--

--neYutvxvOLaeuPCA
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.4 (FreeBSD)
Comment: For info see http://www.gnupg.org

iD8DBQE6klkjWry0BWjoQKURAlLCAKDbRbCcj1FATDklar6Pkp7oxn4VwACdE7Y4
q1UIgUf5mLFt6c+ywmelNaQ=
=Cq52
-----END PGP SIGNATURE-----

--neYutvxvOLaeuPCA--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010220034644.A26123>