From patchwork Thu Apr 1 23:23:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 12179883 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6E6DC43462 for ; Thu, 1 Apr 2021 23:23:55 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 60B756113D for ; Thu, 1 Apr 2021 23:23:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 60B756113D 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 308266B0136; Thu, 1 Apr 2021 19:23:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2DB186B0139; Thu, 1 Apr 2021 19:23:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 12B986B0138; Thu, 1 Apr 2021 19:23:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0204.hostedemail.com [216.40.44.204]) by kanga.kvack.org (Postfix) with ESMTP id E03016B0136 for ; Thu, 1 Apr 2021 19:23:53 -0400 (EDT) Received: from smtpin36.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 475AFF005 for ; Thu, 1 Apr 2021 23:23:53 +0000 (UTC) X-FDA: 77985377946.36.40666F4 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf05.hostedemail.com (Postfix) with ESMTP id 6D4E5E00010F for ; Thu, 1 Apr 2021 23:23:52 +0000 (UTC) Received: by mail-pf1-f181.google.com with SMTP id v10so2518639pfn.5 for ; Thu, 01 Apr 2021 16:23:52 -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:mime-version :content-transfer-encoding; bh=WvZ4JPpQqHS2L+8HlT+daZ1S1BgsSu5178qEy4RCC4Y=; b=YZ2XpfpKoroyGz71W+msLoD8z0VD52kPmSJv459/I/wuG/2XcHz3JsBZ/c6p6L2Nn8 8H5NsdPVJ6DSqLb594nllbPXtbErOCzCubBserjfdkJSKnhsSr95MVfacSGa+37MzJRK bF/Hk303mqASYIyN0wawBGfZ7oFpoTYBaqvCQ= 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:mime-version :content-transfer-encoding; bh=WvZ4JPpQqHS2L+8HlT+daZ1S1BgsSu5178qEy4RCC4Y=; b=msBLosR3VAXUNkMaTHAjHg6cESQlnCfXMW4RMPHT7mPDZzThb7sB99tsL+vALzQkYk L3DaHZVr6zgpukLFPpxsVR7nqn4wcOVP8BlY0B4g9htK09l5yy1wWB/UpLY/bMatGWh1 ipDJeivK0LFFcW4qgQoo5TSe9W6wD1l8wiDB4BoGtiiKqfoEBmS2yivmYwvDpaxt9PDr Yf0BMEiPVphOWTs2ZFLkhqTWhybNQqwb0SgNH0q80jekBspfyeU8PyS+ycUYjtcm14rm KaddM+hA8cK2OZg6R+SK9ydeNStFOpQtJjoO7g2afr2GZ5NVrQ1rclRiky4R5JD/0RjJ QZVA== X-Gm-Message-State: AOAM5313ij4KUMdHTDZ3aYm44U42sOzJo4mPRzg2zSTxsg0CvfMTdO6K V9TmoHfIxx+4c7aG6GwjZ8fOsQ== X-Google-Smtp-Source: ABdhPJxPVVyaaPVouPZCxZu9UN0Ld9Gm94Y/m3EFLAj4GgnjzyBntyxwYP5GLB2Jp9HjhO9Kt5fUBQ== X-Received: by 2002:a05:6a00:22c8:b029:222:7cf7:7f5c with SMTP id f8-20020a056a0022c8b02902227cf77f5cmr9512290pfj.8.1617319431829; Thu, 01 Apr 2021 16:23:51 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id k17sm6265013pfa.68.2021.04.01.16.23.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Apr 2021 16:23:51 -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 , Vlastimil Babka , David Hildenbrand , Mike Rapoport , Andrew Morton , Jonathan Corbet , Randy Dunlap , kernel-hardening@lists.openwall.com, linux-hardening@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v10 0/6] Optionally randomize kernel stack offset each syscall Date: Thu, 1 Apr 2021 16:23:41 -0700 Message-Id: <20210401232347.2791257-1-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Stat-Signature: 63nzsqqehwhyo89r4opshyec6nh31y7s X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 6D4E5E00010F Received-SPF: none (chromium.org>: No applicable sender policy available) receiver=imf05; identity=mailfrom; envelope-from=""; helo=mail-pf1-f181.google.com; client-ip=209.85.210.181 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1617319432-689650 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: Hi! This should be good to go now. :) v10: - switch from "m" to "o" constraint (will) - switch to raw_cpu_*() (tglx) - hooked LKDTM test up to kselftest v9: https://lore.kernel.org/lkml/20210331205458.1871746-1-keescook@chromium.org/ v8: https://lore.kernel.org/lkml/20210330205750.428816-1-keescook@chromium.org/ v7: https://lore.kernel.org/lkml/20210319212835.3928492-1-keescook@chromium.org/ v6: https://lore.kernel.org/lkml/20210315180229.1224655-1-keescook@chromium.org/ v5: https://lore.kernel.org/lkml/20210309214301.678739-1-keescook@chromium.org/ v4: https://lore.kernel.org/lkml/20200622193146.2985288-1-keescook@chromium.org/ v3: https://lore.kernel.org/lkml/20200406231606.37619-1-keescook@chromium.org/ v2: https://lore.kernel.org/lkml/20200324203231.64324-1-keescook@chromium.org/ rfc: https://lore.kernel.org/kernel-hardening/20190329081358.30497-1-elena.reshetova@intel.com/ This is a continuation and refactoring of Elena's earlier effort to add kernel stack base offset randomization. In the time since the earlier discussions, two attacks[1][2] were made public that depended on stack determinism, so we're no longer in the position of "this is a good idea but we have no examples of attacks". :) Earlier discussions also devolved into debates on entropy sources, which is mostly a red herring, given the already low entropy available due to stack size. Regardless, entropy can be changed/improved separately from this series as needed. Earlier discussions also got stuck debating how much syscall overhead was too much, but this is also a red herring since the feature itself needs to be selectable at boot with no cost for those that don't want it: this is solved here with static branches. So, here is the latest improved version, made as arch-agnostic as possible, with usage added for x86 and arm64. It also includes some small static branch clean ups, and addresses some surprise performance issues due to the stack canary[3]. Thanks! -Kees [1] https://a13xp0p0v.github.io/2020/02/15/CVE-2019-18683.html [2] https://repositorio-aberto.up.pt/bitstream/10216/125357/2/374717.pdf [3] https://lore.kernel.org/lkml/202003281520.A9BFF461@keescook/ Kees Cook (6): jump_label: Provide CONFIG-driven build state defaults init_on_alloc: Optimize static branches stack: Optionally randomize kernel stack offset each syscall x86/entry: Enable random_kstack_offset support arm64: entry: Enable random_kstack_offset support lkdtm: Add REPORT_STACK for checking stack offsets .../admin-guide/kernel-parameters.txt | 11 ++++ Makefile | 4 ++ arch/Kconfig | 23 ++++++++ arch/arm64/Kconfig | 1 + arch/arm64/kernel/Makefile | 5 ++ arch/arm64/kernel/syscall.c | 16 ++++++ arch/x86/Kconfig | 1 + arch/x86/entry/common.c | 3 ++ arch/x86/include/asm/entry-common.h | 16 ++++++ drivers/misc/lkdtm/bugs.c | 17 ++++++ drivers/misc/lkdtm/core.c | 1 + drivers/misc/lkdtm/lkdtm.h | 1 + include/linux/jump_label.h | 19 +++++++ include/linux/mm.h | 10 ++-- include/linux/randomize_kstack.h | 54 +++++++++++++++++++ init/main.c | 23 ++++++++ mm/page_alloc.c | 4 +- mm/slab.h | 6 ++- tools/testing/selftests/lkdtm/.gitignore | 1 + tools/testing/selftests/lkdtm/Makefile | 1 + .../testing/selftests/lkdtm/stack-entropy.sh | 36 +++++++++++++ 21 files changed, 245 insertions(+), 8 deletions(-) create mode 100644 include/linux/randomize_kstack.h create mode 100755 tools/testing/selftests/lkdtm/stack-entropy.sh