From patchwork Mon Jun 22 19:31:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 11618823 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CC64014B7 for ; Mon, 22 Jun 2020 19:31:54 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 97F6C208B8 for ; Mon, 22 Jun 2020 19:31:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="J/uzTV9o" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 97F6C208B8 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A21936B0026; Mon, 22 Jun 2020 15:31:53 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 9D0856B0027; Mon, 22 Jun 2020 15:31:53 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8707A6B0028; Mon, 22 Jun 2020 15:31:53 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0002.hostedemail.com [216.40.44.2]) by kanga.kvack.org (Postfix) with ESMTP id 690136B0026 for ; Mon, 22 Jun 2020 15:31:53 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id EAB7C8248047 for ; Mon, 22 Jun 2020 19:31:52 +0000 (UTC) X-FDA: 76957842864.14.arm34_4006fef26e35 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin14.hostedemail.com (Postfix) with ESMTP id AA044183C9472 for ; Mon, 22 Jun 2020 19:31:52 +0000 (UTC) X-Spam-Summary: 2,0,0,0739bfaf697e6a29,d41d8cd98f00b204,keescook@chromium.org,,RULES_HIT:41:355:379:541:800:960:973:988:989:1260:1311:1314:1345:1359:1437:1515:1535:1542:1711:1730:1747:1777:1792:2197:2199:2393:2559:2562:2693:3138:3139:3140:3141:3142:3353:3653:3865:3866:4321:4605:5007:6261:6653:6742:10004:11026:11473:11658:11914:12043:12114:12296:12297:12438:12517:12519:12555:12895:12986:13894:14096:14181:14394:14721:21080:21221:21444:21451:21627:21966:30054,0,RBL:209.85.214.194:@chromium.org:.lbl8.mailshell.net-66.201.201.201 62.14.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:25,LUA_SUMMARY:none X-HE-Tag: arm34_4006fef26e35 X-Filterd-Recvd-Size: 5532 Received: from mail-pl1-f194.google.com (mail-pl1-f194.google.com [209.85.214.194]) by imf42.hostedemail.com (Postfix) with ESMTP for ; Mon, 22 Jun 2020 19:31:52 +0000 (UTC) Received: by mail-pl1-f194.google.com with SMTP id j4so8005790plk.3 for ; Mon, 22 Jun 2020 12:31:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LkjcF2W3iR+CPlZbMflQ6gTadXRXXr87sQKE5d5Gz6g=; b=J/uzTV9owlvRdao+cxwp8Vslq/cFyZE3Lw8w6UUjzYWCr5LjrjVVNFSCVkinxX4Y2H kx/K7FzydtI/KfWHnOel3UOO4tgm0ampnIQbO/M+28yKFT7/EZw/h52Jk9ZvEqY4xdyo Gk7/un3Abi/4AXl0tPJ/akjqNspTqiopNeHAw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LkjcF2W3iR+CPlZbMflQ6gTadXRXXr87sQKE5d5Gz6g=; b=m/JCegmgu4Y2Rje6JtQTA1jrH50H0IS1BRi3dDQx3rkMNMKilWSZhNYoOLyZh4C2xs y3C5MzP46nQCcVTHzvuTDeoBnrysBoljkOzlUKEgyJelVMHzWa/JODjhXuhtHKWVGVUn Z4C5v15QCQENBtNkJlcuNSPS8YxiPun8DoIc5Job4zTrFs7Wy2GT8gx2Af2vZ7cILG8x MKLBdryrb6uU82rLjjPu4iSKPCrlSF1c4QGYDreqVHUz+FEEinZ+jXMTB4X6ZG54fWXO gZrqXqQz+vYbf9t1se++brc3nw70nvX+XUTfkv3fx5jIu3tkD5Ar4Gn2Og3FxhspVydk 28mw== X-Gm-Message-State: AOAM532JiqKMYHilVM93GlWcDWyHv9lENvHzUvyYsSFsywlKB1GysIzv ugoGZNB3TR9eFcdu3Icqw8VbaQ== X-Google-Smtp-Source: ABdhPJzsD2uLlmJ7YMnz76yJmcuXkfmLp3DfOApUsk4PZt0cKKx5QBWDZp5Da97pcoyR3Ss/b2BgBg== X-Received: by 2002:a17:902:8bc7:: with SMTP id r7mr7968646plo.174.1592854311241; Mon, 22 Jun 2020 12:31:51 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id i196sm11565386pgc.55.2020.06.22.12.31.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jun 2020 12:31:50 -0700 (PDT) From: Kees Cook To: Thomas Gleixner Cc: Kees Cook , Elena Reshetova , x86@kernel.org, Andy Lutomirski , Peter Zijlstra , Catalin Marinas , Will Deacon , Mark Rutland , Alexander Potapenko , Alexander Popov , Ard Biesheuvel , Jann Horn , kernel-hardening@lists.openwall.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 4/5] x86/entry: Enable random_kstack_offset support Date: Mon, 22 Jun 2020 12:31:45 -0700 Message-Id: <20200622193146.2985288-5-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200622193146.2985288-1-keescook@chromium.org> References: <20200622193146.2985288-1-keescook@chromium.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: AA044183C9472 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Allow for a randomized stack offset on a per-syscall basis, with roughly 5 bits of entropy. Signed-off-by: Kees Cook --- arch/x86/Kconfig | 1 + arch/x86/entry/common.c | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 6a0cc524882d..57c2a458150e 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -156,6 +156,7 @@ config X86 select HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD if X86_64 select HAVE_ARCH_USERFAULTFD_WP if X86_64 && USERFAULTFD select HAVE_ARCH_VMAP_STACK if X86_64 + select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET select HAVE_ARCH_WITHIN_STACK_FRAMES select HAVE_ASM_MODVERSIONS select HAVE_CMPXCHG_DOUBLE diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c index bd3f14175193..681125bbf09a 100644 --- a/arch/x86/entry/common.c +++ b/arch/x86/entry/common.c @@ -26,6 +26,7 @@ #include #include #include +#include #ifdef CONFIG_XEN_PV #include @@ -240,6 +241,13 @@ static void __prepare_exit_to_usermode(struct pt_regs *regs) lockdep_assert_irqs_disabled(); lockdep_sys_exit(); + /* + * x86_64 stack alignment means 3 bits are ignored, so keep + * the top 5 bits. x86_32 needs only 2 bits of alignment, so + * the top 6 bits will be used. + */ + choose_random_kstack_offset(rdtsc() & 0xFF); + cached_flags = READ_ONCE(ti->flags); if (unlikely(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS)) @@ -346,6 +354,7 @@ __visible noinstr void do_syscall_64(unsigned long nr, struct pt_regs *regs) { struct thread_info *ti; + add_random_kstack_offset(); enter_from_user_mode(); instrumentation_begin(); @@ -409,6 +418,7 @@ static void do_syscall_32_irqs_on(struct pt_regs *regs) /* Handles int $0x80 */ __visible noinstr void do_int80_syscall_32(struct pt_regs *regs) { + add_random_kstack_offset(); enter_from_user_mode(); instrumentation_begin(); @@ -467,6 +477,7 @@ __visible noinstr long do_fast_syscall_32(struct pt_regs *regs) */ regs->ip = landing_pad; + add_random_kstack_offset(); enter_from_user_mode(); instrumentation_begin();