From patchwork Fri Mar 19 21:28:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 12151973 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=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,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 3BE4AC433DB for ; Fri, 19 Mar 2021 21:28:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E4A4F60232 for ; Fri, 19 Mar 2021 21:28:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E4A4F60232 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 7F7BE8D0002; Fri, 19 Mar 2021 17:28:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7CDF76B007D; Fri, 19 Mar 2021 17:28:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 648338D0002; Fri, 19 Mar 2021 17:28:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0035.hostedemail.com [216.40.44.35]) by kanga.kvack.org (Postfix) with ESMTP id 472CA6B007B for ; Fri, 19 Mar 2021 17:28:58 -0400 (EDT) Received: from smtpin36.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 0FF7F824999B for ; Fri, 19 Mar 2021 21:28:58 +0000 (UTC) X-FDA: 77937913956.36.995B2F0 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf17.hostedemail.com (Postfix) with ESMTP id A47584000A51 for ; Fri, 19 Mar 2021 21:28:48 +0000 (UTC) Received: by mail-pj1-f52.google.com with SMTP id kk2-20020a17090b4a02b02900c777aa746fso5461059pjb.3 for ; Fri, 19 Mar 2021 14:28:48 -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=uk0/1LocoDticB91SF6FsgWpOKPsEr1Pb6N+E6L28KI=; b=ejy6cWiCTJfmWKQMk10p5UZeWDqZFvT4CND7HoHywBT5DNVyVD3UOAa8Hrycw/ps6+ 05vKXy4nxWv8bg0fVn+U3gkIt4cS3ofZ2TNfd6JbdV1F91DCWJ71jwYLEQtMn9HAXozO Mmrjz0kenbtMJXw5cpxlGpbaVeCZwXEnCNpMU= 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=uk0/1LocoDticB91SF6FsgWpOKPsEr1Pb6N+E6L28KI=; b=kMjoXJsWqrQaIORm2niyD5piQ9c2dSIDkp7Jgs+gpqo1gaNj8dZtPl2nwCf57BJnPT dI7OZcBTYbrU8OwIAL2GUWBTOpvzkjUrSWApPEWjqzyQRE/C7dbFOOAqPDy+VEK5NpPA ogDAKv/5Ll4Y+5JCfQq8PsaQ8tQGgKV1erun7gFRHNHVGgVVMqGPePS4v/GGooLuCQ0/ WiXHFLNS94+Oi3RAyn1vG6+TXUux5pJ8ebPuBYZKOiS2ntR4dfpGGJXSwsm/gKHCwPVt 7kTFNGQHEwi+nm7yJWh5ZE5laGebju/2TTedi7VBVwvZ8M5lqTkHEUokMWF9z/8kuw4i vxbw== X-Gm-Message-State: AOAM533Sku9+s8kJNYKYNDBanulcwbDfqTOaiVHe1tJWC1Yd758d8FXI 9/hnwYMdOTygdV+UhPThBQ7sYtNr1hZP+g== X-Google-Smtp-Source: ABdhPJzFbnTfmwXyUjVzk+FU45Te/V6SoFc0L6xXU2lvrEGD0zL0nHl9H9x2B3ZLK0nvFsxdZyfnYA== X-Received: by 2002:a17:90b:681:: with SMTP id m1mr476466pjz.168.1616189322441; Fri, 19 Mar 2021 14:28:42 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id y24sm6148989pfn.213.2021.03.19.14.28.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Mar 2021 14:28:41 -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 v7 6/6] lkdtm: Add REPORT_STACK for checking stack offsets Date: Fri, 19 Mar 2021 14:28:35 -0700 Message-Id: <20210319212835.3928492-7-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210319212835.3928492-1-keescook@chromium.org> References: <20210319212835.3928492-1-keescook@chromium.org> MIME-Version: 1.0 X-Patch-Hashes: v=1; h=sha256; g=fb815901a1ccc1d9c4ca5c3e3cd3729b7f382fe2; i=b69wRsxT78r/3tM1mGa7N6ME6+rlXyFg15giRWRwPAQ=; m=aFqgiEE+nAZdug79A1F+fVTg9ZceUb0WPE8cbHqssVg=; p=ZQ32/kILkW5AD3nBZHO0VMTp4prIPkm7+DdhCHX8KdA= X-Patch-Sig: m=pgp; i=keescook@chromium.org; s=0x0x8972F4DFDC6DC026; b=iQIzBAABCgAdFiEEpcP2jyKd1g9yPm4TiXL039xtwCYFAmBVF4IACgkQiXL039xtwCY3XA/9Hc7 uCdnb6IBdks8FZIXBvGn8iqxkiI9jjjTCKozGcJ39SUBhTi98eLk12QE8/L8iuMzlY7nSS93HTrGV T+BS6LWgoYdWZ1vzeRqKj2ovp8AESLtcLZoWFhX/oM7kbXFpWiChKtvW8u8occfMmeCPD0fjX9OXc 9VwR2F51qP9IA+X7ihTmMfEwT7YurTNg/POD9NMFjhkktXwKJVnxKq7yqnpI9eouW3EMIU26EH2pm b6tihBnsotHNjZJ89gD8aM1o5mg2JgcpQ/lJY3jX3YuNKdjsG2Vg8DUB+04NrbdLysjqZQ0DIHamy UVXKM0B6ZPLPz5nOimx/D5TYpWDY7Yj5lTS7gYnjJzt0UFCECHHocfss+/eC5EnJwETz/JpUcRpuS CsswP/CZZ9Hf1HqvEy3F+aIuT5uP8PgGSNRztkK76YJ6Xrj9Mln6u7bPBCn0+EYsmlsL2/oCGVc4M /nfQmQ9sjPsm1hGhs4Xdoh4HX2OpPln6fqCYaV/yWfgQysuaGi083LU3OVDKvw6HWc8ZzqF48EU42 sisXTwgZJadagLzYDaJurWB+bqV52QnkLr6/Hb0Mp0Yu1tW+0pp5IMVxP3Fo575mLgTyeWbmv/kCs TDzCPCOMxiG4lsvlDqgluKkVTdZ1PD/AylEtmoS2tOihVR0k2loLyrEXi//t+jKk= X-Stat-Signature: brwocaqpb7isn1ktshcqpsdsncg3jbzz X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: A47584000A51 Received-SPF: none (chromium.org>: No applicable sender policy available) receiver=imf17; identity=mailfrom; envelope-from=""; helo=mail-pj1-f52.google.com; client-ip=209.85.216.52 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616189328-426555 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: For validating the stack offset behavior, report the offset from a given process's first seen stack address. A quick way to measure the entropy: for i in $(seq 1 1000); do echo "REPORT_STACK" >/sys/kernel/debug/provoke-crash/DIRECT done offsets=$(dmesg | grep 'Stack offset' | cut -d: -f3 | sort | uniq -c | sort -n | wc -l) echo "$(uname -m) bits of stack entropy: $(echo "obase=2; $offsets" | bc | wc -L)" Signed-off-by: Kees Cook --- drivers/misc/lkdtm/bugs.c | 17 +++++++++++++++++ drivers/misc/lkdtm/core.c | 1 + drivers/misc/lkdtm/lkdtm.h | 1 + 3 files changed, 19 insertions(+) diff --git a/drivers/misc/lkdtm/bugs.c b/drivers/misc/lkdtm/bugs.c index 110f5a8538e9..0e8254d0cf0b 100644 --- a/drivers/misc/lkdtm/bugs.c +++ b/drivers/misc/lkdtm/bugs.c @@ -134,6 +134,23 @@ noinline void lkdtm_CORRUPT_STACK_STRONG(void) __lkdtm_CORRUPT_STACK((void *)&data); } +static pid_t stack_pid; +static unsigned long stack_addr; + +void lkdtm_REPORT_STACK(void) +{ + volatile uintptr_t magic; + pid_t pid = task_pid_nr(current); + + if (pid != stack_pid) { + pr_info("Starting stack offset tracking for pid %d\n", pid); + stack_pid = pid; + stack_addr = (uintptr_t)&magic; + } + + pr_info("Stack offset: %d\n", (int)(stack_addr - (uintptr_t)&magic)); +} + void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void) { static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5}; diff --git a/drivers/misc/lkdtm/core.c b/drivers/misc/lkdtm/core.c index b2aff4d87c01..8024b6a5cc7f 100644 --- a/drivers/misc/lkdtm/core.c +++ b/drivers/misc/lkdtm/core.c @@ -110,6 +110,7 @@ static const struct crashtype crashtypes[] = { CRASHTYPE(EXHAUST_STACK), CRASHTYPE(CORRUPT_STACK), CRASHTYPE(CORRUPT_STACK_STRONG), + CRASHTYPE(REPORT_STACK), CRASHTYPE(CORRUPT_LIST_ADD), CRASHTYPE(CORRUPT_LIST_DEL), CRASHTYPE(STACK_GUARD_PAGE_LEADING), diff --git a/drivers/misc/lkdtm/lkdtm.h b/drivers/misc/lkdtm/lkdtm.h index 5ae48c64df24..99f90d3e5e9c 100644 --- a/drivers/misc/lkdtm/lkdtm.h +++ b/drivers/misc/lkdtm/lkdtm.h @@ -17,6 +17,7 @@ void lkdtm_LOOP(void); void lkdtm_EXHAUST_STACK(void); void lkdtm_CORRUPT_STACK(void); void lkdtm_CORRUPT_STACK_STRONG(void); +void lkdtm_REPORT_STACK(void); void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void); void lkdtm_SOFTLOCKUP(void); void lkdtm_HARDLOCKUP(void);