From patchwork Wed Nov 29 00:21:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yaakov Selkowitz X-Patchwork-Id: 119915 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp2401725qgn; Tue, 28 Nov 2017 16:29:13 -0800 (PST) X-Google-Smtp-Source: AGs4zMadGoa8WVMV43CXWvpK+6nBMaugmtReEJy7MXWctybvMRXP9ciEhyzsuMu75Hy/kT7HMTo8 X-Received: by 10.98.185.8 with SMTP id z8mr1026769pfe.166.1511915352941; Tue, 28 Nov 2017 16:29:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511915352; cv=none; d=google.com; s=arc-20160816; b=LQnshL3vO/NXzW7sXOQ4YngCkj3Q+1V5UwSGrAhxRFkhujpBI/DNvgX9KNA6m67UDw mnwvUQuvYr3ZdVQgv5AEjMa55LEvPluGgqhUg5Lq9QfTBrAzgGbnqfZkP5Le2FQ/rUm3 /BIcuINEmF/gyjdLA/v/CYIPUGcJHiPfNoMQ3hOsgJqK+nbvgSyCSTb5lQgrwpspK3WU lPMuw2gmZ827ZEncGRRY/1B6UPW0sNrHTnbJhqy4jVFbzgH81M9/+sx6eQ4+aeEn9V/t B4rfmpHpssfznyOW6/AX8EIobwxUemNgN9n3h1TlKWwMcgTOHLm+byWrWJ5IrtakrVW3 elQw== 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=WODpfnjt/HdSVYvu3YDNXGmkqyDDlkL5idcQdZRpPu0=; b=rFQGEJKbVVCWO2EIVsshWjSixu/jHO35Mik6G5zCxXNADPisWh+qCAuciRJQEgvmNq 9gYgT6nUQpCfCLc+hUTKT73roJzqmyw125S6WABsKHXNq3fkB9FB6MJX5lygGPfOfWbR A5ueAr8z5yzJywTYsB2v7r9yq/9wJ4AoemhRERrheSNtdoFicAbsC8KK7EyHqCtCGFnQ 96FgUvlbkixTh48WLxOiQWqEuS+GFWahKKBSL1dn51SkBsRNTZ7S98TBVLNuQi84GKyu CWLgyFHoFNw42ZGVTfF15y1X5RVr6ZIGJuqfLCeTrJcmBBTcZrOdf8M37qOhc6bfgjYv tCBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=eMxfhivU; spf=pass (google.com: domain of newlib-return-15393-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=newlib-return-15393-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 c5si299196plr.407.2017.11.28.16.29.12 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Nov 2017 16:29:12 -0800 (PST) Received-SPF: pass (google.com: domain of newlib-return-15393-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=eMxfhivU; spf=pass (google.com: domain of newlib-return-15393-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=newlib-return-15393-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=NwAQjkfajGAX/hgwyzoBaMtuxHIIWXi WjJ3FYjj4oiY7aWBSa39DjEmCbuZsI4g3NyitRsEO8XWyIdV20nOushUKLdXc3lz nSz06Y0U+dGwGCH7tXGUaGF1s8YIH/544VovquWnWTzuXeMC+SrGICYTgjxqrTWw 3d/cieZ6FVlA= 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=wZcDhGCv6/zGt4rlI+JfNBDuqEs=; b=eMxfh ivUdMAq7lN9FNyBIOxqAalwHWQMveDk+GtZYuSoZKcbXUtbNhxban6H+Ru/18gdr 4MxhYiBXp7ZbBZpkkSVo51TMP8vdTcvQZk0a6dhVg2f01unfJV0NoeSYCjnxZznh BqxvK4m5L5flA1FoLY2W+xhZ/OEJWxNTTPByC8= Received: (qmail 6911 invoked by alias); 29 Nov 2017 00:29:03 -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 6589 invoked by uid 89); 29 Nov 2017 00:29:03 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, 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 00:29:00 +0000 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9FE787CB84 for ; Wed, 29 Nov 2017 00:21:55 +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 12AD7600D1 for ; Wed, 29 Nov 2017 00:21:54 +0000 (UTC) From: Yaakov Selkowitz To: newlib@sourceware.org Subject: [PATCH v4 05/10] ssp: add Object Size Checking for stdio.h functions, part 1 Date: Tue, 28 Nov 2017 18:21:38 -0600 Message-Id: <20171129002143.12500-6-yselkowi@redhat.com> In-Reply-To: <20171129002143.12500-1-yselkowi@redhat.com> References: <20171129002143.12500-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 | 12 +++++ 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, 424 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..050b9d9ed --- /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, 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, 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, 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, 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..e0490485e 100644 --- a/newlib/libc/include/stdio.h +++ b/newlib/libc/include/stdio.h @@ -210,7 +210,9 @@ int _EXFUN(vprintf, (const char *, __VALIST) int _EXFUN(vsprintf, (char *__restrict, const char *__restrict, __VALIST) _ATTRIBUTE ((__format__ (__printf__, 2, 0)))); int _EXFUN(fgetc, (FILE *)); +#if !__SSP_FORTIFY_LEVEL char * _EXFUN(fgets, (char *__restrict, int, FILE *__restrict)); +#endif int _EXFUN(fputc, (int, FILE *)); int _EXFUN(fputs, (const char *__restrict, FILE *__restrict)); int _EXFUN(getc, (FILE *)); @@ -220,7 +222,9 @@ int _EXFUN(putc, (int, FILE *)); int _EXFUN(putchar, (int)); int _EXFUN(puts, (const char *)); int _EXFUN(ungetc, (int, FILE *)); +#if !__SSP_FORTIFY_LEVEL size_t _EXFUN(fread, (_PTR __restrict, size_t _size, size_t _n, FILE *__restrict)); +#endif size_t _EXFUN(fwrite, (const _PTR __restrict , size_t _size, size_t _n, FILE *)); #ifdef _COMPILING_NEWLIB int _EXFUN(fgetpos, (FILE *, _fpos_t *)); @@ -539,12 +543,16 @@ int _EXFUN(fileno_unlocked, (FILE *)); int _EXFUN(fflush_unlocked, (FILE *)); int _EXFUN(fgetc_unlocked, (FILE *)); int _EXFUN(fputc_unlocked, (int, FILE *)); +#if !__SSP_FORTIFY_LEVEL size_t _EXFUN(fread_unlocked, (_PTR __restrict, size_t _size, size_t _n, FILE *__restrict)); +#endif size_t _EXFUN(fwrite_unlocked, (const _PTR __restrict , size_t _size, size_t _n, FILE *)); #endif #if __GNU_VISIBLE +#if !__SSP_FORTIFY_LEVEL char * _EXFUN(fgets_unlocked, (char *__restrict, int, FILE *__restrict)); +#endif int _EXFUN(fputs_unlocked, (const char *__restrict, FILE *__restrict)); #endif @@ -796,4 +804,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; +}