From patchwork Wed Nov 29 09:42:55 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yaakov Selkowitz X-Patchwork-Id: 119939 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp2831930qgn; Wed, 29 Nov 2017 01:43:59 -0800 (PST) X-Google-Smtp-Source: AGs4zMYoOHrxi2ipw1b8W4O9whRVzIXfv2Y0rzfBBWLAU+69YVpVw4QtQ1OQ2nQeDI9wT57tpO3b X-Received: by 10.101.77.201 with SMTP id q9mr2205868pgt.226.1511948639718; Wed, 29 Nov 2017 01:43:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511948639; cv=none; d=google.com; s=arc-20160816; b=lbalPwoQCxkTXCXJpPQO+f2Jm3KjQE2QE31/aITYTaI3ST6b766emlMiI467IA5RlM P31VtaGRsKAPkSvw3gkrT6ev/hQMDVRVnqfuTPHNRdgy2hMV4/7sqJLb1feiB6pmHyEn ZcZu3oEKcVkFQoOWefofMIg4zHUCNDmQcvMNM4B5BPppjn8QvihcKSqK7aYF253GCPQH B1mTEkIwf0LHIWbcXFYtWM/qWtw+O472/rce00tWujAGW2sTMY2hWM4jNglwgsOrf5HB ATS0lZfpqChM8tori08s+lTuGJVn+aTVf5d0wSwCHDfWNuhipIJi3nFY0losinOsUFlS dMKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=uuPnDBoqwEReOzLeuQOxW56zbQ0e8//u9h7gs8VoLro=; b=Q43P1OZvef3VvorHgq3Q/htmWLokE3W961/Rwpu3fqlT/9J1HNLSUfKnRph+jdLjr2 nEUXPUGFInpZEQOAITfubWVMpba5l6DrnRuStb4rQCFfnqEZq4AV1fBsNhaMJN+uFpth lMkzwOFxNSw89ZD8cxkikpF3gCxk5/Rd4WXoWX6uaECY76WYdO1YRdHab5jLpGCjolHQ JaEUW6HbWG/DSyMm/DbOobvCcNggRh0PnEJcF2a+1dCHCDZk+Sphg/CE+WetsG1uKD+7 grdBfNwqW1OyBjzxQl3/xqNwtpm1WW13GgvWtB0o18N/Vl603cZZBs/p3Yumw5jiK4XD vouA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=umqX97V0; spf=pass (google.com: domain of newlib-return-15400-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=newlib-return-15400-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id c128si1083701pfg.138.2017.11.29.01.43.59 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Nov 2017 01:43:59 -0800 (PST) Received-SPF: pass (google.com: domain of newlib-return-15400-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=umqX97V0; spf=pass (google.com: domain of newlib-return-15400-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=newlib-return-15400-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=UtXWYLxH/znDWcrAvQ/nMxUCNFkLl63 QwGKH+vzpqcmNReMEMArkcWRl6b8t113gsAT2ptvPGFv28cf+Ft09f9T4E5cdzsq NulLpAJYatNC2SI9oReZufxrZWXahFT1PJb7+02hSV9gqLH644jH8m8/cChhPaLz Q5We5yd2NzsU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=CuJKXfueX05hsIcGrB14Mc+nKjA=; b=umqX9 7V0OSJIu2nNEcgkBv0x1cC1X8eO72tslXZl/pLv798G4JsXg8/zN/jylsUUXhJDB BBqCmm3bPtlkFdtjo7r0AyrwadAWwz9eR8NgJOzKQZbWj1lK+52p7qFZSkZOWW5/ 9lo19TbDX5NKb6VMFpuSOn7OgxA0cqunNzc6mc= Received: (qmail 66899 invoked by alias); 29 Nov 2017 09:43:23 -0000 Mailing-List: contact newlib-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: newlib-owner@sourceware.org Delivered-To: mailing list newlib@sourceware.org Received: (qmail 66646 invoked by uid 89); 29 Nov 2017 09:43:20 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, KAM_NUMSUBJECT, KB_WAM_FROM_NAME_SINGLEWORD, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 29 Nov 2017 09:43:17 +0000 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EB37C13A42 for ; Wed, 29 Nov 2017 09:43:15 +0000 (UTC) Received: from localhost.localdomain (ovpn-120-11.rdu2.redhat.com [10.10.120.11]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 552CB60BE5 for ; Wed, 29 Nov 2017 09:43:15 +0000 (UTC) From: Yaakov Selkowitz To: newlib@sourceware.org Subject: [PATCH v5 05/10] ssp: add Object Size Checking for stdio.h, part 1 Date: Wed, 29 Nov 2017 03:42:55 -0600 Message-Id: <20171129094300.20296-6-yselkowi@redhat.com> In-Reply-To: <20171129094300.20296-1-yselkowi@redhat.com> References: <20171129094300.20296-1-yselkowi@redhat.com> The implementation is mostly from NetBSD, except for switching fgets to pure inline, and the addition of fgets_unlocked, fread, and fread_unlocked for parity with glibc. The following functions are also guarded in glibc: asprintf, dprintf, fprintf, printf, vasprintf, vdprintf, vfprintf, vprintf. Signed-off-by: Yaakov Selkowitz --- newlib/libc/include/ssp/stdio.h | 101 ++++++++++++++++++++++++++++++++++++++++ newlib/libc/include/stdio.h | 4 ++ newlib/libc/ssp/gets_chk.c | 78 +++++++++++++++++++++++++++++++ newlib/libc/ssp/snprintf_chk.c | 59 +++++++++++++++++++++++ newlib/libc/ssp/sprintf_chk.c | 63 +++++++++++++++++++++++++ newlib/libc/ssp/vsnprintf_chk.c | 51 ++++++++++++++++++++ newlib/libc/ssp/vsprintf_chk.c | 60 ++++++++++++++++++++++++ 7 files changed, 416 insertions(+) create mode 100644 newlib/libc/include/ssp/stdio.h create mode 100644 newlib/libc/ssp/gets_chk.c create mode 100644 newlib/libc/ssp/snprintf_chk.c create mode 100644 newlib/libc/ssp/sprintf_chk.c create mode 100644 newlib/libc/ssp/vsnprintf_chk.c create mode 100644 newlib/libc/ssp/vsprintf_chk.c -- 2.15.0 diff --git a/newlib/libc/include/ssp/stdio.h b/newlib/libc/include/ssp/stdio.h new file mode 100644 index 000000000..df2cd1859 --- /dev/null +++ b/newlib/libc/include/ssp/stdio.h @@ -0,0 +1,101 @@ +/* $NetBSD: stdio.h,v 1.5 2011/07/17 20:54:34 joerg Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * + * 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. + */ +#ifndef _SSP_STDIO_H_ +#define _SSP_STDIO_H_ + +#include + +__BEGIN_DECLS +int __sprintf_chk(char *__restrict, int, size_t, const char *__restrict, ...) + __printflike(4, 5); +int __vsprintf_chk(char *__restrict, int, size_t, const char *__restrict, + __va_list) + __printflike(4, 0); +int __snprintf_chk(char *__restrict, size_t, int, size_t, + const char *__restrict, ...) + __printflike(5, 6); +int __vsnprintf_chk(char *__restrict, size_t, int, size_t, + const char *__restrict, __va_list) + __printflike(5, 0); +char *__gets_chk(char *, size_t); +__END_DECLS + +#if __SSP_FORTIFY_LEVEL > 0 + + +#define sprintf(str, ...) \ + __builtin___sprintf_chk(str, 0, __ssp_bos(str), __VA_ARGS__) + +#define vsprintf(str, fmt, ap) \ + __builtin___vsprintf_chk(str, 0, __ssp_bos(str), fmt, ap) + +#define snprintf(str, len, ...) \ + __builtin___snprintf_chk(str, len, 0, __ssp_bos(str), __VA_ARGS__) + +#define vsnprintf(str, len, fmt, ap) \ + __builtin___vsnprintf_chk(str, len, 0, __ssp_bos(str), fmt, ap) + +#define gets(str) \ + __gets_chk(str, __ssp_bos(str)) + +__ssp_decl(char *, fgets, (char *__restrict __buf, int __len, FILE *__fp)) +{ + if (__len > 0) + __ssp_check(__buf, (size_t)__len, __ssp_bos); + return __ssp_real_fgets(__buf, __len, __fp); +} + +#if __GNU_VISIBLE +__ssp_decl(char *, fgets_unlocked, (char *__restrict __buf, int __len, FILE *__fp)) +{ + if (__len > 0) + __ssp_check(__buf, (size_t)__len, __ssp_bos); + return __ssp_real_fgets_unlocked(__buf, __len, __fp); +} +#endif /* __GNU_VISIBLE */ + +__ssp_decl(size_t, fread, (void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __fp)) +{ + __ssp_check(__ptr, __size * __n, __ssp_bos0); + return __ssp_real_fread(__ptr, __size, __n, __fp); +} + +#if __MISC_VISIBLE +__ssp_decl(size_t, fread_unlocked, (void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __fp)) +{ + __ssp_check(__ptr, __size * __n, __ssp_bos0); + return __ssp_real_fread_unlocked(__ptr, __size, __n, __fp); +} +#endif /* __MISC_VISIBLE */ + +#endif /* __SSP_FORTIFY_LEVEL > 0 */ + +#endif /* _SSP_STDIO_H_ */ diff --git a/newlib/libc/include/stdio.h b/newlib/libc/include/stdio.h index ee0f612c4..b648e6201 100644 --- a/newlib/libc/include/stdio.h +++ b/newlib/libc/include/stdio.h @@ -796,4 +796,8 @@ _putchar_unlocked(int _c) _END_STD_C +#if __SSP_FORTIFY_LEVEL > 0 +#include +#endif + #endif /* _STDIO_H_ */ diff --git a/newlib/libc/ssp/gets_chk.c b/newlib/libc/ssp/gets_chk.c new file mode 100644 index 000000000..b4f7015bc --- /dev/null +++ b/newlib/libc/ssp/gets_chk.c @@ -0,0 +1,78 @@ +/* $NetBSD: gets_chk.c,v 1.7 2013/10/04 20:49:16 christos Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * + * 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. + */ +#include +__RCSID("$NetBSD: gets_chk.c,v 1.7 2013/10/04 20:49:16 christos Exp $"); + +/*LINTLIBRARY*/ + +#include +#include +#include +#include +#include +#include + +extern char *__gets(char *); +#undef gets +#ifdef __NEWLIB__ +#define __gets gets +#endif + +char * +__gets_chk(char * __restrict buf, size_t slen) +{ + char *abuf; + size_t len; + + if (slen >= (size_t)INT_MAX) + return __gets(buf); + + if ((abuf = malloc(slen + 1)) == NULL) + return __gets(buf); + + if (fgets(abuf, (int)(slen + 1), stdin) == NULL) { + free(abuf); + return NULL; + } + + len = strlen(abuf); + if (len > 0 && abuf[len - 1] == '\n') + --len; + + if (len >= slen) + __chk_fail(); + + (void)memcpy(buf, abuf, len); + + buf[len] = '\0'; + free(abuf); + return buf; +} diff --git a/newlib/libc/ssp/snprintf_chk.c b/newlib/libc/ssp/snprintf_chk.c new file mode 100644 index 000000000..cede5a4bd --- /dev/null +++ b/newlib/libc/ssp/snprintf_chk.c @@ -0,0 +1,59 @@ +/* $NetBSD: snprintf_chk.c,v 1.5 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * + * 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. + */ +#include +__RCSID("$NetBSD: snprintf_chk.c,v 1.5 2008/04/28 20:23:00 martin Exp $"); + +/*LINTLIBRARY*/ + +#include +#include +#include +#include + +#undef vsnprintf + +/*ARGSUSED*/ +int +__snprintf_chk(char * __restrict buf, size_t len, int flags, size_t slen, + const char * __restrict fmt, ...) +{ + va_list ap; + int rv; + + if (len > slen) + __chk_fail(); + + va_start(ap, fmt); + rv = vsnprintf(buf, len, fmt, ap); + va_end(ap); + + return rv; +} diff --git a/newlib/libc/ssp/sprintf_chk.c b/newlib/libc/ssp/sprintf_chk.c new file mode 100644 index 000000000..1e924799b --- /dev/null +++ b/newlib/libc/ssp/sprintf_chk.c @@ -0,0 +1,63 @@ +/* $NetBSD: sprintf_chk.c,v 1.6 2009/02/05 05:40:36 lukem Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * + * 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. + */ +#include +__RCSID("$NetBSD: sprintf_chk.c,v 1.6 2009/02/05 05:40:36 lukem Exp $"); + +/*LINTLIBRARY*/ + +#include +#include +#include +#include +#include + +#undef vsnprintf +#undef vsprintf + +int +/*ARGSUSED*/ +__sprintf_chk(char * __restrict buf, int flags, size_t slen, + const char * __restrict fmt, ...) +{ + va_list ap; + int rv; + + va_start(ap, fmt); + if (slen > (size_t)INT_MAX) + rv = vsprintf(buf, fmt, ap); + else { + if ((rv = vsnprintf(buf, slen, fmt, ap)) >= 0 && (size_t)rv >= slen) + __chk_fail(); + } + va_end(ap); + + return rv; +} diff --git a/newlib/libc/ssp/vsnprintf_chk.c b/newlib/libc/ssp/vsnprintf_chk.c new file mode 100644 index 000000000..2b8802908 --- /dev/null +++ b/newlib/libc/ssp/vsnprintf_chk.c @@ -0,0 +1,51 @@ +/* $NetBSD: vsnprintf_chk.c,v 1.5 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * + * 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. + */ +#include +__RCSID("$NetBSD: vsnprintf_chk.c,v 1.5 2008/04/28 20:23:00 martin Exp $"); + +/*LINTLIBRARY*/ + +#include +#include +#include +#include + +#undef vsnprintf + +int +__vsnprintf_chk(char * __restrict buf, size_t len, int flags, size_t slen, + const char * __restrict fmt, va_list ap) +{ + if (len > slen) + __chk_fail(); + + return vsnprintf(buf, len, fmt, ap); +} diff --git a/newlib/libc/ssp/vsprintf_chk.c b/newlib/libc/ssp/vsprintf_chk.c new file mode 100644 index 000000000..fec8a18c7 --- /dev/null +++ b/newlib/libc/ssp/vsprintf_chk.c @@ -0,0 +1,60 @@ +/* $NetBSD: vsprintf_chk.c,v 1.6 2009/02/05 05:39:38 lukem Exp $ */ + +/*- + * Copyright (c) 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * + * 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. + */ +#include +__RCSID("$NetBSD: vsprintf_chk.c,v 1.6 2009/02/05 05:39:38 lukem Exp $"); + +/*LINTLIBRARY*/ + +#include +#include +#include +#include +#include + +#undef vsprintf +#undef vsnprintf + +/*ARGSUSED*/ +int +__vsprintf_chk(char * __restrict buf, int flags, size_t slen, + const char * __restrict fmt, va_list ap) +{ + int rv; + + if (slen > (size_t)INT_MAX) + rv = vsprintf(buf, fmt, ap); + else { + if ((rv = vsnprintf(buf, slen, fmt, ap)) >= 0 && (size_t)rv >= slen) + __chk_fail(); + } + + return rv; +}