Received: by 2002:a19:ef0c:0:0:0:0:0 with SMTP id n12csp950105lfh; Tue, 1 Feb 2022 12:51:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJwlnOMt7w6ag489APcGAs0smhJJySCOcfa8WI2FtTPCQXQ6IIIr+cKA/iVsIRSiFIyuG0rS X-Received: by 2002:a65:5543:: with SMTP id t3mr21895006pgr.180.1643748696869; Tue, 01 Feb 2022 12:51:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643748696; cv=none; d=google.com; s=arc-20160816; b=ykxCKi+c9nUbgGf4Fjchu2XODhCowtvJQYLa26Prerq1eUfa0XGq8sqHjEw/pacgH1 MGcxHkEst88qjDL0kNs0CdwNaNctB2md768P/BgWXDsuOGOyFodcRpq9KXRYixQ1yvZK dD7KKf9853d5tN+WaKTDLhjO23ZxpL0yx91gQw9p1mCpC1mXd7eiPpTt91Q7axgOD/3h 30dBlcDgcaVeEuTuDBGMteiSA4unP6DsxYMV+fIu0Mil1TEzHqAHoZNIgMTGx7vZF+md cFuPz+Vv4IpU9N0MAL+7vkEzbtQQ8HhKyebFkaAzSHHw9JS6+I8PRzZSOoTg3hdR2bse fqOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=cxXm9S9rBUGO+n/qf1n3on2rs5DZHI4UmQ2+ms2iYP4=; b=UK4V7RXNtCkZXTlh/CFqnXHq9jZDJWzVVeH1h+WwPxognx2QMZ3GagsWUyExetrbke 87eMaPp8huZ7e0pl/MHW6egeU0IMBw17HwRotLsoxLEeV7bU6LK1Qp8yw3UkcNX34PJy F3R6gv2qDMhSlMkzsnLOO7mCuTF5bhO+i+0AqYv4a6K/13UMhmEnzgTMGTu+HbTVp2g5 twkJF+sILx1qTBb3AdqfwM0wGDgr/LKPr4BckT21fVTYfhScs/5L18dvmw4DkQH0WQZx oK5wJdPkIccevihW0W73wXMbSBAXSUTYNvZ0SIm+E3NL2aN+DxVd+d31blbxNoA+f8kr YFXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Qo7cE3pl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w9si16233580plp.280.2022.02.01.12.51.24; Tue, 01 Feb 2022 12:51:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Qo7cE3pl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351070AbiAaVPe (ORCPT + 99 others); Mon, 31 Jan 2022 16:15:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232007AbiAaVPb (ORCPT ); Mon, 31 Jan 2022 16:15:31 -0500 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9EF4C061714 for ; Mon, 31 Jan 2022 13:15:30 -0800 (PST) Received: by mail-pl1-x636.google.com with SMTP id y17so13627143plg.7 for ; Mon, 31 Jan 2022 13:15:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=cxXm9S9rBUGO+n/qf1n3on2rs5DZHI4UmQ2+ms2iYP4=; b=Qo7cE3plH59PbdtmImyac1lX+en/JDRgSaa4LMeLw6ph0k1EzlPT/BWVNjItFxgULh uYEe69YWJNHzuLftpsMRKtLU9Yx+i9KI7fKy9zSLAwleJOGwAdqbSQ8bIZt8/KNOt0iP XZyBHj77OOJEJ+b4ZrtXZqGJFZNNPAs6Aio2E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=cxXm9S9rBUGO+n/qf1n3on2rs5DZHI4UmQ2+ms2iYP4=; b=h+b5J6WPv9704tvC9fyET8vmHzLJ2sEg8rr4eFj3fbmF0xxckaRJdeWtkxxaqDDp5u OHdX8QPMId2BzxkxH6I0DM7UyU7bTtNGKG/7p7PL6F2+qe3K4fqdybm7t2jP8DoJt2FG Wt1wT8l5q/lUW6r4xO/oDWjrZyIDCufa1+7FcyoQ4pWO0TWWbvrtSR7ikeQoEt4bK/d7 mBA3CnMCwYEHAMm6aNnpcMHkpSRqJVuVcin7ATiiDQ3Rl/BENC/DIpCuawJLthGP03MF O3/qp8rkEYhbPfFyMz5FSnS9DWNYKl1MyeQRV/Z6irRMXjgIpuhvUP+2VNUkZWlhE5Eq t7CQ== X-Gm-Message-State: AOAM530Z8Y2msqHbC1LQBmZLQrXqPNHoSODLZj3sqpaYiu01AlbMa4hk 1zsFmmJYNr9xyMT+XMjIg+dwMg== X-Received: by 2002:a17:902:d509:: with SMTP id b9mr22602105plg.3.1643663730410; Mon, 31 Jan 2022 13:15:30 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id mi18sm219840pjb.35.2022.01.31.13.15.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Jan 2022 13:15:30 -0800 (PST) Date: Mon, 31 Jan 2022 13:15:29 -0800 From: Kees Cook To: Marco Elver Cc: Thomas Gleixner , Peter Zijlstra , Ingo Molnar , Elena Reshetova , Nathan Chancellor , Nick Desaulniers , Alexander Potapenko , llvm@lists.linux.dev, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 2/2] stack: Constrain and fix stack offset randomization with Clang builds Message-ID: <202201311315.B9FDD0A@keescook> References: <20220131090521.1947110-1-elver@google.com> <20220131090521.1947110-2-elver@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220131090521.1947110-2-elver@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 31, 2022 at 10:05:21AM +0100, Marco Elver wrote: > All supported versions of Clang perform auto-init of __builtin_alloca() > when stack auto-init is on (CONFIG_INIT_STACK_ALL_{ZERO,PATTERN}). > > add_random_kstack_offset() uses __builtin_alloca() to add a stack > offset. This means, when CONFIG_INIT_STACK_ALL_{ZERO,PATTERN} is > enabled, add_random_kstack_offset() will auto-init that unused portion > of the stack used to add an offset. > > There are several problems with this: > > 1. These offsets can be as large as 1023 bytes. Performing > memset() on them isn't exactly cheap, and this is done on > every syscall entry. > > 2. Architectures adding add_random_kstack_offset() to syscall > entry implemented in C require them to be 'noinstr' (e.g. see > x86 and s390). The potential problem here is that a call to > memset may occur, which is not noinstr. > > A x86_64 defconfig kernel with Clang 11 and CONFIG_VMLINUX_VALIDATION shows: > > | vmlinux.o: warning: objtool: do_syscall_64()+0x9d: call to memset() leaves .noinstr.text section > | vmlinux.o: warning: objtool: do_int80_syscall_32()+0xab: call to memset() leaves .noinstr.text section > | vmlinux.o: warning: objtool: __do_fast_syscall_32()+0xe2: call to memset() leaves .noinstr.text section > | vmlinux.o: warning: objtool: fixup_bad_iret()+0x2f: call to memset() leaves .noinstr.text section > > Clang 14 (unreleased) will introduce a way to skip alloca initialization > via __builtin_alloca_uninitialized() (https://reviews.llvm.org/D115440). > > Constrain RANDOMIZE_KSTACK_OFFSET to only be enabled if no stack > auto-init is enabled, the compiler is GCC, or Clang is version 14+. Use > __builtin_alloca_uninitialized() if the compiler provides it, as is done > by Clang 14. > > Link: https://lkml.kernel.org/r/YbHTKUjEejZCLyhX@elver.google.com > Fixes: 39218ff4c625 ("stack: Optionally randomize kernel stack offset each syscall") > Signed-off-by: Marco Elver Thanks for the tweaks; this looks good to me now. Acked-by: Kees Cook -- Kees Cook