From patchwork Tue Nov 28 08:57:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yaakov Selkowitz X-Patchwork-Id: 119805 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp1434536qgn; Tue, 28 Nov 2017 00:58:22 -0800 (PST) X-Google-Smtp-Source: AGs4zMaQX1f/tk4n/AhtCSzoKioto3LGN9TrRaLC9HjuQrQIhCVYwF1zNkd7X/kyyWcRnhIPqN7M X-Received: by 10.99.104.73 with SMTP id d70mr5535293pgc.333.1511859502259; Tue, 28 Nov 2017 00:58:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511859502; cv=none; d=google.com; s=arc-20160816; b=0MSJXx+zfzlHZlYYDTcsohT0aTIfwvwvoSi4BBZbqjT6oe8ld5IY9aCYxvXHtXKtyJ aQx0xH0+PMMopeUgXeZ77KtDbOXsC2nK8qGiMeIBcOw4HcOmBX5TnD83OsNlEAN2Kz2E nLy4WzVbnWsnOdAhg0nmzcOlR9cXBLJmxpbAaTSygwG8ZzyGrbOxcRNUKFDPqz7UTPc9 d5SEHoIWYFgeV72dnyuStsKZtpNTEpKuf4ueA+I8AJz+PCNgLktKRrg7bRP9Qb8jJl2c 0zw13W8rHG0yhtk/liBv2K58kCywf/daFjHrvstTQz1jekYmimk1k8rv4vd6TMjg+cyx YD1g== 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=+syHu05mAOUD4BR2oplzwWJl8p+z/9/z/Yrggl4xxcg=; b=GFDG1gZ1twGHAh6rlSTnWdif012EE8LbUFBY4wHxwCDZsixKKn+Q7y74EW15JFHpre glTIFtG8o891ra31Di2qwEwACo0Vmea2bWt7KiYJhJ4qqXm5NSE+KW+FdmcBSveikZHw mqZgMygIgQedImnfihhG21N3vG8ZqDWmX9h7RxFoYkTZF1d+KMOXXU+iyPXH9zFbLcK4 8Im//qlasVHJfAu1s5BAowmpWfkJG3B99dA/FomnHRs/0TkI8gGUVGxECk+4/Syg2JTW MUkKUL2NQUM6c8Cn8Tc26vnGXOegM7Q8TNzte3wM8rVdGdheEgNygkeGVY7WvxTDsKNH CJvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=m1oTc5N8; spf=pass (google.com: domain of newlib-return-15373-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=newlib-return-15373-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 t23si20654602pgu.361.2017.11.28.00.58.21 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Nov 2017 00:58:22 -0800 (PST) Received-SPF: pass (google.com: domain of newlib-return-15373-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=m1oTc5N8; spf=pass (google.com: domain of newlib-return-15373-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=newlib-return-15373-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=q/pxTTZBMSSmkVvlv/QVxZTFoghHtTq Xns/2ubmed353dNz8syXwkOjNNiZZihOYpINsb9rL31afXlsmF4iuEcxQ/xvN+sX zGw4TotGYXyrS2knfmh76gM9Nx+YfttrJ8PG3Ruetk2PJpORvDkV8TAJkl2O75NW bM9HozBPd0tc= 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=sXstxDTIWxwu26zCDThfP6JvI3Y=; b=m1oTc 5N8kGD+ImE9vd7sAUrHDfNjledggMg1mpP/Iy7NOj7I089klN4w61er55Eddkb8r PIw79+GPBkmX+C2X9KCDm7Aa9l4PLZ9Qb3bERnwipvrUpH8S43peu3wIyqdsxy99 mz9ak4qCOH8FfTnTDrOcvxxxN3Cm2dPIonkI+c= Received: (qmail 81131 invoked by alias); 28 Nov 2017 08:58:10 -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 81122 invoked by uid 89); 28 Nov 2017 08:58:09 -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= 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; Tue, 28 Nov 2017 08:58:08 +0000 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C585781DF2 for ; Tue, 28 Nov 2017 08:58:07 +0000 (UTC) Received: from localhost.localdomain (ovpn-120-142.rdu2.redhat.com [10.10.120.142]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4D4CB5D753 for ; Tue, 28 Nov 2017 08:58:07 +0000 (UTC) From: Yaakov Selkowitz To: newlib@sourceware.org Subject: [PATCH v3 1/9] ssp: add APIs for Stack Smashing Protection Date: Tue, 28 Nov 2017 02:57:47 -0600 Message-Id: <20171128085755.5928-2-yselkowi@redhat.com> In-Reply-To: <20171128085755.5928-1-yselkowi@redhat.com> References: <20171128085755.5928-1-yselkowi@redhat.com> Compiling with any of the -fstack-protector* flags requires the __stack_chk_guard data import (which needs to be initialized) and the __stack_chk_fail{,_local} functions. While GCC's own libssp can provide these, it is better that we provide these ourselves. The implementation is custom due to being OS-specific. Signed-off-by: Yaakov Selkowitz --- newlib/libc/ssp/stack_protector.c | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 newlib/libc/ssp/stack_protector.c -- 2.15.0 diff --git a/newlib/libc/ssp/stack_protector.c b/newlib/libc/ssp/stack_protector.c new file mode 100644 index 000000000..ee014b69d --- /dev/null +++ b/newlib/libc/ssp/stack_protector.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include + +uintptr_t __stack_chk_guard = 0; + +void +__attribute__((__constructor__)) +__stack_chk_init (void) +{ + if (__stack_chk_guard != 0) + return; + +#if defined(__CYGWIN__) || defined(__rtems__) + arc4random_buf(&__stack_chk_guard, sizeof(__stack_chk_guard)); +#else + /* If getentropy is not available, use the "terminator canary". */ + ((unsigned char *)&__stack_chk_guard)[0] = 0; + ((unsigned char *)&__stack_chk_guard)[1] = 0; + ((unsigned char *)&__stack_chk_guard)[2] = '\n'; + ((unsigned char *)&__stack_chk_guard)[3] = 255; +#endif +} + +void +__attribute__((__noreturn__)) +__stack_chk_fail (void) +{ + char msg[] = "*** stack smashing detected ***: terminated\n"; + write (2, msg, strlen (msg)); + raise (SIGABRT); + _exit (127); +} + +#ifdef __ELF__ +void +__attribute__((visibility ("hidden"))) +__stack_chk_fail_local (void) +{ + __stack_chk_fail(); +} +#endif