From patchwork Wed Nov 29 00:21:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yaakov Selkowitz X-Patchwork-Id: 119908 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp2395512qgn; Tue, 28 Nov 2017 16:22:04 -0800 (PST) X-Google-Smtp-Source: AGs4zMaO3LZRyPmhTdEeqFg08K9bHN9Dd9UCGLy0TsCIoduuYt4bHGY8MdGN5KvalrPmzO8E0Vpw X-Received: by 10.84.128.197 with SMTP id a63mr1003584pla.212.1511914924394; Tue, 28 Nov 2017 16:22:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511914924; cv=none; d=google.com; s=arc-20160816; b=fkuAydriq0aYNVG8lj965w036HEXJD9Oa7XDMiPzqL8lvm5fZg/KImthukLo3m9eNo lCPZb5TTTTy6tMZ7I4oUrsuU5UksqmLVynt+sdi1KIHNiwiO5wTPZ0a2nE9MSuLNvNZL jp8Q5Dkh1KR0NU99FM75mBvnx6FmAs+QLHselImt4TXBiCtLXxqYjUtlrfJAVPvj50BU WXMOnPUMFIeB3sd8V25OjIW+sp3v4X35uRPlB6ekTImxuoTXvDI/V1v2XwSX/RYH9NNq hUst/CLiBy8b+cgD33plu39FXuM1fbsl12qMMqNqPGABczNOs9r/hCBWORYow4+ieqvj bSrg== 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=BTj5JzE3cw2fvCBDE9TndV4uVzAwE93e+8VOn0nB6WQtWzLLlFkzf/EjmaJGMKjaaD 2fbI6uNclu3yNxRbx2emi+7Sv+gS6sFYmLiLClEEREZrbTUHH+4471ZBmnadmV30Gf9e 4UzvjxOwrQKGvvGqTOoB24PvfrlfZHkrO5LPQIpfZ65Z7/33A+cfBC3gHOES4KYNeyfI e75Axu0Ao8xHbWRgTvGIZNTRSHru5xmg+rd3jQDUnzVDeNey2IrXZRs7VT+yFu+D0YQ4 TKNODyHO2f7k3icfjDFCN6nrxtlbQEdRqATxZZvQGaOdXSJ6tvjbbAVHCu0jK2pvPHpu 68nQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=l8drBjbc; spf=pass (google.com: domain of newlib-return-15386-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=newlib-return-15386-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 k1si309320pld.40.2017.11.28.16.22.04 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Nov 2017 16:22:04 -0800 (PST) Received-SPF: pass (google.com: domain of newlib-return-15386-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=l8drBjbc; spf=pass (google.com: domain of newlib-return-15386-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=newlib-return-15386-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=TkfBn8NiHFpkVa0XlJ9+5bpRYfBE70f axJ9Fu7zKcmWkeI3jt+hgT9Cp2tT983IOAYGJ/gSGrPYUH9yzS+wwoBipo93IleP Pb1KXaUAx0hWNJ4nihfc2Wo7+sCwFUDqBOOODmyDpdceUccjXmBSzyhqRxZwHS+3 xO4y8uJAv/tM= 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=l8drB jbcQH84597H0eaVhKERwfoesyrUIX2343qiOhQBgrkdCuPHekXwOUW8Hhk3qBDvb WCA9Kb/ogEYIUgYUGj0zlwZBUV5mKDHExLqHQR8UW3VzNrLbEP7OABYqeBcbSu5o zn/2sXpjrsbp4DVsZgI/kdOXRffvqomU8QVw58= Received: (qmail 64173 invoked by alias); 29 Nov 2017 00:21:55 -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 63279 invoked by uid 89); 29 Nov 2017 00:21:55 -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; Wed, 29 Nov 2017 00:21:54 +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 C4D16C04B952 for ; Wed, 29 Nov 2017 00:21:52 +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 5E150600D1 for ; Wed, 29 Nov 2017 00:21:52 +0000 (UTC) From: Yaakov Selkowitz To: newlib@sourceware.org Subject: [PATCH v4 01/10] ssp: add APIs for Stack Smashing Protection Date: Tue, 28 Nov 2017 18:21:34 -0600 Message-Id: <20171129002143.12500-2-yselkowi@redhat.com> In-Reply-To: <20171129002143.12500-1-yselkowi@redhat.com> References: <20171129002143.12500-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