From patchwork Wed Nov 29 00:21:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yaakov Selkowitz X-Patchwork-Id: 119912 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp2396057qgn; Tue, 28 Nov 2017 16:22:38 -0800 (PST) X-Google-Smtp-Source: AGs4zMYAArUCvSO9GoYeIpAV1UR2tsiarJ8Zz5STOQJZ95JFMSz42vOzAV5hmn0QHHyZV4/9EgvE X-Received: by 10.84.217.150 with SMTP id p22mr944398pli.427.1511914958109; Tue, 28 Nov 2017 16:22:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511914958; cv=none; d=google.com; s=arc-20160816; b=wSOfJDDBy8b2TmFmxWiljaK+EqsXqqC7/iatC5z6XHo3hjCq9OrBtsPdtd+WZgIVAi DXV1AWQXu5qlt8qONR3H0ZwhZ+rd9K7pMN+9UC+0kTvOiSshniO87vxAA0i65GHJuGs2 XBuh2nZOE6W2xKgJ6YCactNjfc2wW2zxpt2IzuziRny9PQzMDbp4ozzq+Vp5/DXHCf7d u349xOJkqxAR1VXZaMTIZ/olohQJQNyTyIp75tz08ThHoIGk6SMUqW8H5fbxFuWtpbpj v+lqG19eyem0xemJBc0HldLYL4f+oUksGnZhZIyasOUY7mpLvFGFFkvgLiZsmQ5fdRKr 09tw== 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=4teDZCuoL53FbRtDhK6A5q60o49Qj/TqvDNZpnwUpx0=; b=0hA7rMQSM4CQBGrK0fiL2bdnFAxDriXh8rAaKfZZDE3OS10liUUjYxzwrTaB1Nphqq umnhA7nDDN67Ymgb9iKpsfa2ltSAW+i7WjSN7dW1q9Lldql2T/wGCpVz9IzuAeANN15y bOy0mLJAzHodY6bbmMyxVGLLrZys1agGyGbiGeyaNBu22A11WGkXdA/goP+LQcocnuYl gJ6t2zhT2kXVLlO+cw/bkVSiSusil+u1TC3JsRA4DG1v7Ql/QGZq7UYehbMtM0D89FZd Jbfm9WbX12jXZ8Gvs1pxIyQ5VFKhj1QQ1TbS1kisK5Ek5jKt+8i6bBVVHVBkyv0ipSTU jZtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=OP4PA1uH; spf=pass (google.com: domain of newlib-return-15390-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=newlib-return-15390-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 r2si292051pgs.685.2017.11.28.16.22.37 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Nov 2017 16:22:38 -0800 (PST) Received-SPF: pass (google.com: domain of newlib-return-15390-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=OP4PA1uH; spf=pass (google.com: domain of newlib-return-15390-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=newlib-return-15390-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=FeQ/NJg24xj6YB/txgCuOHauNkgfPJw MILQzJLeYmwln2SCyOjeC2bVw+4vl0HMk30bjfFmKA5ZQ+boW+1dKVM3fPcL4IhM WrARMak226pDUrX/qcQgv4SxRQC8ixMX1oZJkNcMERBECKDDnaavRJBK/+6DApai j/SSPfu1ztIA= 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=QmUz6cVDUk0ergPPsJpYhTNzbI4=; b=OP4PA 1uHPFeCpAD6xEzoTkJK9gZ/lI2virRZCEIdxV4XhALZ7aewlrvRKD////meJH5n6 MdM9E8hbT9IjvomlkXaoaSoTM8jjv+eOgjA+IoDyISesiPobQ9GefzXLhJN/3/T/ me53GcreP7BQ2sO9XodGUnTKpm2j965oA7x+ek= Received: (qmail 66272 invoked by alias); 29 Nov 2017 00:22:00 -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 66137 invoked by uid 89); 29 Nov 2017 00:21:59 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KB_WAM_FROM_NAME_SINGLEWORD, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=1115 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:21:55 +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 26202822 for ; Wed, 29 Nov 2017 00:21:54 +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 ADCF3600D1 for ; Wed, 29 Nov 2017 00:21:53 +0000 (UTC) From: Yaakov Selkowitz To: newlib@sourceware.org Subject: [PATCH v4 03/10] ssp: add Object Size Checking for string.h functions Date: Tue, 28 Nov 2017 18:21:36 -0600 Message-Id: <20171129002143.12500-4-yselkowi@redhat.com> In-Reply-To: <20171129002143.12500-1-yselkowi@redhat.com> References: <20171129002143.12500-1-yselkowi@redhat.com> The implementation is from NetBSD, with the addition of mempcpy (a GNU extension) for parity with glibc and libssp. Signed-off-by: Yaakov Selkowitz --- newlib/libc/include/ssp/string.h | 115 +++++++++++++++++++++++++++++++++++++++ newlib/libc/include/string.h | 4 ++ newlib/libc/ssp/memcpy_chk.c | 54 ++++++++++++++++++ newlib/libc/ssp/memmove_chk.c | 50 +++++++++++++++++ newlib/libc/ssp/mempcpy_chk.c | 21 +++++++ newlib/libc/ssp/memset_chk.c | 49 +++++++++++++++++ newlib/libc/ssp/stpcpy_chk.c | 58 ++++++++++++++++++++ newlib/libc/ssp/stpncpy_chk.c | 56 +++++++++++++++++++ newlib/libc/ssp/strcat_chk.c | 62 +++++++++++++++++++++ newlib/libc/ssp/strcpy_chk.c | 55 +++++++++++++++++++ newlib/libc/ssp/strncat_chk.c | 73 +++++++++++++++++++++++++ newlib/libc/ssp/strncpy_chk.c | 55 +++++++++++++++++++ 12 files changed, 652 insertions(+) create mode 100644 newlib/libc/include/ssp/string.h create mode 100644 newlib/libc/ssp/memcpy_chk.c create mode 100644 newlib/libc/ssp/memmove_chk.c create mode 100644 newlib/libc/ssp/mempcpy_chk.c create mode 100644 newlib/libc/ssp/memset_chk.c create mode 100644 newlib/libc/ssp/stpcpy_chk.c create mode 100644 newlib/libc/ssp/stpncpy_chk.c create mode 100644 newlib/libc/ssp/strcat_chk.c create mode 100644 newlib/libc/ssp/strcpy_chk.c create mode 100644 newlib/libc/ssp/strncat_chk.c create mode 100644 newlib/libc/ssp/strncpy_chk.c -- 2.15.0 diff --git a/newlib/libc/include/ssp/string.h b/newlib/libc/include/ssp/string.h new file mode 100644 index 000000000..cd5592ba6 --- /dev/null +++ b/newlib/libc/include/ssp/string.h @@ -0,0 +1,115 @@ +/* $NetBSD: string.h,v 1.13 2014/11/29 13:23:48 pooka 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_STRING_H_ +#define _SSP_STRING_H_ + +#include +#include + +__BEGIN_DECLS +void *__memcpy_chk(void *, const void *, size_t, size_t); +void *__memmove_chk(void *, void *, size_t, size_t); +void *__mempcpy_chk(void *, const void *, size_t, size_t); +void *__memset_chk(void *, int, size_t, size_t); +char *__stpcpy_chk(char *, const char *, size_t); +char *__strcat_chk(char *, const char *, size_t); +char *__strcpy_chk(char *, const char *, size_t); +char *__strncat_chk(char *, const char *, size_t, size_t); +char *__strncpy_chk(char *, const char *, size_t, size_t); +__END_DECLS + +#if __SSP_FORTIFY_LEVEL > 0 + +#define __ssp_bos_check3(fun, dst, src, len) \ + ((__ssp_bos0(dst) != (size_t)-1) ? \ + __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)) : \ + __ ## fun ## _ichk(dst, src, len)) + +#define __ssp_bos_check2(fun, dst, src) \ + ((__ssp_bos0(dst) != (size_t)-1) ? \ + __builtin___ ## fun ## _chk(dst, src, __ssp_bos0(dst)) : \ + __ ## fun ## _ichk(dst, src)) + +#define __ssp_bos_icheck3_restrict(fun, type1, type2) \ +_ELIDABLE_INLINE type1 __ ## fun ## _ichk(type1 __restrict, type2 __restrict, size_t); \ +_ELIDABLE_INLINE type1 \ +__ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src, size_t len) { \ + return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \ +} + +#define __ssp_bos_icheck3(fun, type1, type2) \ +_ELIDABLE_INLINE type1 __ ## fun ## _ichk(type1, type2, size_t); \ +_ELIDABLE_INLINE type1 \ +__ ## fun ## _ichk(type1 dst, type2 src, size_t len) { \ + return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \ +} + +#define __ssp_bos_icheck2_restrict(fun, type1, type2) \ +_ELIDABLE_INLINE type1 __ ## fun ## _ichk(type1, type2); \ +_ELIDABLE_INLINE type1 \ +__ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src) { \ + return __builtin___ ## fun ## _chk(dst, src, __ssp_bos0(dst)); \ +} + +__BEGIN_DECLS +__ssp_bos_icheck3_restrict(memcpy, void *, const void *) +__ssp_bos_icheck3(memmove, void *, const void *) +__ssp_bos_icheck3_restrict(mempcpy, void *, const void *) +__ssp_bos_icheck3(memset, void *, int) +__ssp_bos_icheck2_restrict(stpcpy, char *, const char *) +#if __GNUC_PREREQ__(4,8) || defined(__clang__) +__ssp_bos_icheck3_restrict(stpncpy, char *, const char *) +#endif +__ssp_bos_icheck2_restrict(strcpy, char *, const char *) +__ssp_bos_icheck2_restrict(strcat, char *, const char *) +__ssp_bos_icheck3_restrict(strncpy, char *, const char *) +__ssp_bos_icheck3_restrict(strncat, char *, const char *) +__END_DECLS + +#define memcpy(dst, src, len) __ssp_bos_check3(memcpy, dst, src, len) +#define memmove(dst, src, len) __ssp_bos_check3(memmove, dst, src, len) +#if __GNU_VISIBLE +#define mempcpy(dst, src, len) __ssp_bos_check3(mempcpy, dst, src, len) +#endif +#define memset(dst, val, len) __ssp_bos_check3(memset, dst, val, len) +#if __POSIX_VISIBLE >= 200809 +#define stpcpy(dst, src) __ssp_bos_check2(stpcpy, dst, src) +#if __GNUC_PREREQ__(4,8) || defined(__clang__) +#define stpncpy(dst, src, len) __ssp_bos_check3(stpncpy, dst, src, len) +#endif +#endif +#define strcpy(dst, src) __ssp_bos_check2(strcpy, dst, src) +#define strcat(dst, src) __ssp_bos_check2(strcat, dst, src) +#define strncpy(dst, src, len) __ssp_bos_check3(strncpy, dst, src, len) +#define strncat(dst, src, len) __ssp_bos_check3(strncat, dst, src, len) + +#endif /* __SSP_FORTIFY_LEVEL > 0 */ +#endif /* _SSP_STRING_H_ */ diff --git a/newlib/libc/include/string.h b/newlib/libc/include/string.h index 57db7742b..b54b83335 100644 --- a/newlib/libc/include/string.h +++ b/newlib/libc/include/string.h @@ -176,4 +176,8 @@ char *_EXFUN(__nonnull ((1)) basename,(const char *)) __asm__(__ASMNAME("__gnu_b _END_STD_C +#if __SSP_FORTIFY_LEVEL > 0 +#include +#endif + #endif /* _STRING_H_ */ diff --git a/newlib/libc/ssp/memcpy_chk.c b/newlib/libc/ssp/memcpy_chk.c new file mode 100644 index 000000000..63f536dc5 --- /dev/null +++ b/newlib/libc/ssp/memcpy_chk.c @@ -0,0 +1,54 @@ +/* $NetBSD: memcpy_chk.c,v 1.7 2015/05/13 19:57:16 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. + */ +#include +__RCSID("$NetBSD: memcpy_chk.c,v 1.7 2015/05/13 19:57:16 joerg Exp $"); + +/*LINTLIBRARY*/ + +#include +#include + +#undef memcpy + +void *__memcpy_chk(void * __restrict, const void * __restrict, size_t, size_t); + +void * +__memcpy_chk(void * __restrict dst, const void * __restrict src, size_t len, + size_t slen) +{ + if (len > slen) + __chk_fail(); + + if (__ssp_overlap((const char *)src, (const char *)dst, len)) + __chk_fail(); + + return memcpy(dst, src, len); +} diff --git a/newlib/libc/ssp/memmove_chk.c b/newlib/libc/ssp/memmove_chk.c new file mode 100644 index 000000000..f8f03d778 --- /dev/null +++ b/newlib/libc/ssp/memmove_chk.c @@ -0,0 +1,50 @@ +/* $NetBSD: memmove_chk.c,v 1.5 2014/09/17 00:39:28 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. + */ +#include +__RCSID("$NetBSD: memmove_chk.c,v 1.5 2014/09/17 00:39:28 joerg Exp $"); + +/*LINTLIBRARY*/ + +#include +#include + +#undef memmove + +void *__memmove_chk(void *, void *src, size_t, size_t); + +void * +__memmove_chk(void *dst, void *src, size_t len, + size_t slen) +{ + if (len > slen) + __chk_fail(); + return memmove(dst, src, len); +} diff --git a/newlib/libc/ssp/mempcpy_chk.c b/newlib/libc/ssp/mempcpy_chk.c new file mode 100644 index 000000000..fc2ccf894 --- /dev/null +++ b/newlib/libc/ssp/mempcpy_chk.c @@ -0,0 +1,21 @@ +#define _GNU_SOURCE +#include +#include +#include + +#undef mempcpy + +void *__mempcpy_chk(void * __restrict, const void * __restrict, size_t, size_t); + +void * +__mempcpy_chk(void * __restrict dst, const void * __restrict src, size_t len, + size_t slen) +{ + if (len > slen) + __chk_fail(); + + if (__ssp_overlap((const char *)src, (const char *)dst, len)) + __chk_fail(); + + return mempcpy(dst, src, len); +} diff --git a/newlib/libc/ssp/memset_chk.c b/newlib/libc/ssp/memset_chk.c new file mode 100644 index 000000000..0e303b9eb --- /dev/null +++ b/newlib/libc/ssp/memset_chk.c @@ -0,0 +1,49 @@ +/* $NetBSD: memset_chk.c,v 1.5 2014/09/17 00:39:28 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. + */ +#include +__RCSID("$NetBSD: memset_chk.c,v 1.5 2014/09/17 00:39:28 joerg Exp $"); + +/*LINTLIBRARY*/ + +#include +#include + +#undef memset + +void *__memset_chk(void * __restrict, int, size_t, size_t); + +void * +__memset_chk(void * __restrict dst, int val, size_t len, size_t slen) +{ + if (len > slen) + __chk_fail(); + return memset(dst, val, len); +} diff --git a/newlib/libc/ssp/stpcpy_chk.c b/newlib/libc/ssp/stpcpy_chk.c new file mode 100644 index 000000000..ed1d74ad7 --- /dev/null +++ b/newlib/libc/ssp/stpcpy_chk.c @@ -0,0 +1,58 @@ +/* $NetBSD: stpcpy_chk.c,v 1.6 2015/05/09 15:42:21 christos Exp $ */ + +/*- + * Copyright (c) 2013 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: stpcpy_chk.c,v 1.6 2015/05/09 15:42:21 christos Exp $"); + +/*LINTLIBRARY*/ + +#include +#include + +#undef memcpy + +#if !__GNUC_PREREQ__(4, 8) +char *__stpcpy_chk(char * __restrict, const char * __restrict, size_t); +#endif + +char * +__stpcpy_chk(char * __restrict dst, const char * __restrict src, size_t slen) +{ + size_t len = strlen(src); + + if (len >= slen) + __chk_fail(); + + if (__ssp_overlap(src, dst, len)) + __chk_fail(); + + (void)memcpy(dst, src, len + 1); + return dst + len; +} diff --git a/newlib/libc/ssp/stpncpy_chk.c b/newlib/libc/ssp/stpncpy_chk.c new file mode 100644 index 000000000..756626153 --- /dev/null +++ b/newlib/libc/ssp/stpncpy_chk.c @@ -0,0 +1,56 @@ +/* $NetBSD: stpncpy_chk.c,v 1.3 2015/05/09 15:42:21 christos Exp $ */ + +/*- + * Copyright (c) 2013 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: stpncpy_chk.c,v 1.3 2015/05/09 15:42:21 christos Exp $"); + +/*LINTLIBRARY*/ + +#include +#include + +#undef stpncpy + +#if !__GNUC_PREREQ__(4, 8) +char *__stpncpy_chk(char * __restrict, const char * __restrict, size_t, size_t); +#endif + +char * +__stpncpy_chk(char * __restrict dst, const char * __restrict src, size_t len, + size_t slen) +{ + if (len > slen) + __chk_fail(); + + if (__ssp_overlap(src, dst, len)) + __chk_fail(); + + return stpncpy(dst, src, len); +} diff --git a/newlib/libc/ssp/strcat_chk.c b/newlib/libc/ssp/strcat_chk.c new file mode 100644 index 000000000..d57f9559b --- /dev/null +++ b/newlib/libc/ssp/strcat_chk.c @@ -0,0 +1,62 @@ +/* $NetBSD: strcat_chk.c,v 1.5 2014/09/17 00:39:28 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. + */ +#include +__RCSID("$NetBSD: strcat_chk.c,v 1.5 2014/09/17 00:39:28 joerg Exp $"); + +/*LINTLIBRARY*/ + +#include +#include + +char *__strcat_chk(char * __restrict, const char * __restrict, size_t); + +char * +__strcat_chk(char * __restrict dst, const char * __restrict src, size_t slen) +{ + char *d; + + for (d = dst; *d; d++) { + if (slen-- == 0) + __chk_fail(); + } + + while (*src) { + if (slen-- == 0) + __chk_fail(); + *d++ = *src++; + } + + if (slen-- == 0) + __chk_fail(); + + *d = '\0'; + return dst; +} diff --git a/newlib/libc/ssp/strcpy_chk.c b/newlib/libc/ssp/strcpy_chk.c new file mode 100644 index 000000000..cef160a62 --- /dev/null +++ b/newlib/libc/ssp/strcpy_chk.c @@ -0,0 +1,55 @@ +/* $NetBSD: strcpy_chk.c,v 1.8 2015/05/09 15:42:21 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: strcpy_chk.c,v 1.8 2015/05/09 15:42:21 christos Exp $"); + +/*LINTLIBRARY*/ + +#include +#include + +#undef memcpy + +char *__strcpy_chk(char * __restrict, const char * __restrict, size_t); + +char * +__strcpy_chk(char * __restrict dst, const char * __restrict src, size_t slen) +{ + size_t len = strlen(src) + 1; + + if (len > slen) + __chk_fail(); + + if (__ssp_overlap(src, dst, len)) + __chk_fail(); + + return memcpy(dst, src, len); +} diff --git a/newlib/libc/ssp/strncat_chk.c b/newlib/libc/ssp/strncat_chk.c new file mode 100644 index 000000000..5ce5a9ef6 --- /dev/null +++ b/newlib/libc/ssp/strncat_chk.c @@ -0,0 +1,73 @@ +/* $NetBSD: strncat_chk.c,v 1.5 2014/09/17 00:39:28 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. + */ +#include +__RCSID("$NetBSD: strncat_chk.c,v 1.5 2014/09/17 00:39:28 joerg Exp $"); + +/*LINTLIBRARY*/ + +#include +#include +#include + +char *__strncat_chk(char * __restrict, const char * __restrict, size_t, + size_t); + +char * +__strncat_chk(char * __restrict dst, const char * __restrict src, size_t len, + size_t slen) +{ + char *d; + + if (len == 0) + return dst; + + if (len > slen) + __chk_fail(); + + for (d = dst; *d; d++) { + if (slen-- == 0) + __chk_fail(); + } + + do { + if ((*d = *src++) == '\0') + break; + if (slen-- == 0) + __chk_fail(); + d++; + } while (--len != 0); + + if (slen-- == 0) + __chk_fail(); + + *d = '\0'; + return dst; +} diff --git a/newlib/libc/ssp/strncpy_chk.c b/newlib/libc/ssp/strncpy_chk.c new file mode 100644 index 000000000..591157a25 --- /dev/null +++ b/newlib/libc/ssp/strncpy_chk.c @@ -0,0 +1,55 @@ +/* $NetBSD: strncpy_chk.c,v 1.6 2015/05/09 15:42:21 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: strncpy_chk.c,v 1.6 2015/05/09 15:42:21 christos Exp $"); + +/*LINTLIBRARY*/ + +#include +#include + +#undef strncpy + +char *__strncpy_chk(char * __restrict, const char * __restrict, size_t, + size_t); + +char * +__strncpy_chk(char * __restrict dst, const char * __restrict src, size_t len, + size_t slen) +{ + if (len > slen) + __chk_fail(); + + if (__ssp_overlap(src, dst, len)) + __chk_fail(); + + return strncpy(dst, src, len); +}