Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp184290lqi; Wed, 6 Mar 2024 13:55:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXvK08j/xuE4TLxFuDA+Eu2CgaNYB9fFZ9jO1w/tOg5DREQU+OiBDwLCxS5TV0ke3rALK9l06IYhoxMtEz+AqZdF/6U5NgjgJ+ZlEvYhw== X-Google-Smtp-Source: AGHT+IHL5VkjxKYpvrfNf2tpXOF2aDFYAHCRkY3wUU/msEURggF3Ip7bNmaL14wTVWX7s1mq+Fmz X-Received: by 2002:a05:6402:30ad:b0:567:23a2:5b1e with SMTP id df13-20020a05640230ad00b0056723a25b1emr7859588edb.23.1709762116513; Wed, 06 Mar 2024 13:55:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709762116; cv=pass; d=google.com; s=arc-20160816; b=HlY7FRkiAjqkvfaB7VUBPU5aelrOLq0YloHigQ3CvPTNj0dUj3FdUpPi17CANK74HI a8BcDjH19vVjpAD61ZLnI4wX9VERTSuTWD5C3iFuN1z+qfW+hBEEXl1OL1xQe84iugDE 4OoOGqxYjVqFHUF5ZGO2Bu1Ci+lHpwou0GnbQMYzTgmUqLhspoIdWR/C/Q8uXZQPmhGu is4akN4f7RgTCNk5lPJ2DZF8HYTkOalt4JLdnybBscHVj8sKpE9CkL21NjV99zh9aK7/ 67tciLRw+PUs3UsRW/zyj9rgyqI326MPLx6FX5ayvfOcCRm0qm+SjbAWZS+lHSx8K30P 0bhQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=ScocnvXp+c00tyFhBF6243Gr8LJya0OK1ZDrHrYDLnw=; fh=J81keYTTAG1SJv1mr+VoPqfHvQJfykAI+vxMBZGckP0=; b=xaMpaYdcZnlW0j6Onv8SNwrDP3Xo/pUAO28MhNlkgda/T9erDdJsazN6nx+SAc9TR3 coX5pAdSzkqxKWzwlL42a5dSroBPcGO0+ie+YNk+CISEWoeO9SdvmlbkNudE60DI+ca1 0zSLh3r732A8bhchPkw1ZUku9v1KWrZdRievgne1PCc0i+YeHxD2I9/jWioNXclcZ+pH thSSPwzCn2Q4eblghmLAZh0HAkDrB68lgiwX2SknLPQ/K3VyoQy2X6PiJCoM0WqKabh1 KrQ1e3VgDsYayKvuk1+ecyu1Uu2ZbZoyjPa9KppmXG6pGhniO3UW5HxITMdDMTRTfF1N sbfw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-94619-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94619-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id g14-20020a056402090e00b005671d4c0714si4155924edz.161.2024.03.06.13.55.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 13:55:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-94619-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-94619-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94619-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 442FB1F254D5 for ; Wed, 6 Mar 2024 21:55:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A4C681B81D; Wed, 6 Mar 2024 21:55:07 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8162817543; Wed, 6 Mar 2024 21:55:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709762107; cv=none; b=Bqqqt1yLgBS8sTP3LXu9XzShXFnYTYNM7uWEaYbL5nnwyddOe49aj4RLHMsmATIlGrDcNZgyWOBLsY/lrbC4LxsT382X5fUwhiNeGvYDLp2idFzb21p/2nD2SeAe7mi1rxyzR6cSCF70ztxKlatTF2PzKnChe0WL5naCTN0P7jo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709762107; c=relaxed/simple; bh=bvR21HUFQSjXL1lKOF4+wYG7LzV7EvRmk0SjKWA1rI8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=bZ2mgK/IHFNVAC/8x+WkH3Fl69xmLZPe7buvG1CvhgzSw/iTReyEVweQ7jNlxo6gex4AOgIuaa2Uz3NMEqN64VMFjrJ/V6hZQo7tpCvqGTzIAX5+RLpFL6RpPI/0j4BQbs/593BYoYcMfykOuNd/NWvDqde1xKuY63FXPdqP8EE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B09A01FB; Wed, 6 Mar 2024 13:55:40 -0800 (PST) Received: from [192.168.20.13] (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 020883F73F; Wed, 6 Mar 2024 13:54:58 -0800 (PST) Message-ID: <38f9541b-dd88-4d49-af3b-bc7880a4e2f4@arm.com> Date: Wed, 6 Mar 2024 15:54:57 -0600 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/1] arm64: syscall: Direct PRNG kstack randomization Content-Language: en-US To: Arnd Bergmann , Kees Cook Cc: linux-arm-kernel@lists.infradead.org, Catalin Marinas , Will Deacon , "Jason A . Donenfeld" , "Gustavo A. R. Silva" , Mark Rutland , Steven Rostedt , Mark Brown , Guo Hui , Manoj.Iyer@arm.com, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, James Yang , Shiyou Huang References: <20240305221824.3300322-1-jeremy.linton@arm.com> <20240305221824.3300322-2-jeremy.linton@arm.com> <202403051526.0BE26F99E@keescook> <34351804-ad1d-498f-932a-c1844b78589f@app.fastmail.com> From: Jeremy Linton In-Reply-To: <34351804-ad1d-498f-932a-c1844b78589f@app.fastmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hi, On 3/6/24 14:46, Arnd Bergmann wrote: > On Wed, Mar 6, 2024, at 00:33, Kees Cook wrote: >> On Tue, Mar 05, 2024 at 04:18:24PM -0600, Jeremy Linton wrote: >>> The existing arm64 stack randomization uses the kernel rng to acquire >>> 5 bits of address space randomization. This is problematic because it >>> creates non determinism in the syscall path when the rng needs to be >>> generated or reseeded. This shows up as large tail latencies in some >>> benchmarks and directly affects the minimum RT latencies as seen by >>> cyclictest. >>> >>> Other architectures are using timers/cycle counters for this function, >>> which is sketchy from a randomization perspective because it should be >>> possible to estimate this value from knowledge of the syscall return >>> time, and from reading the current value of the timer/counters. > > As I commented on the previous version, I don't want to see > a change that only addresses one architecture like this. If you > are convinced that using a cycle counter is a mistake, then we > should do the same thing on the other architectures as well > that currently use a cycle counter. I personally tend to agree as long as we aren't creating a similar set of problems for those architectures as we are seeing on arm. Currently the kstack rng on/off choice is basically zero overhead for them. > >>> +#ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET >>> +DEFINE_PER_CPU(struct rnd_state, kstackrng); >>> + >>> +static u16 kstack_rng(void) >>> +{ >>> + u32 rng = prandom_u32_state(this_cpu_ptr(&kstackrng)); >>> + >>> + return rng & 0x1ff; >>> +} >>> + >>> +/* Should we reseed? */ >>> +static int kstack_rng_setup(unsigned int cpu) >>> +{ >>> + u32 rng_seed; >>> + >>> + /* zero should be avoided as a seed */ >>> + do { >>> + rng_seed = get_random_u32(); >>> + } while (!rng_seed); >>> + prandom_seed_state(this_cpu_ptr(&kstackrng), rng_seed); >>> + return 0; >>> +} >>> + >>> +static int kstack_init(void) >>> +{ >>> + int ret; >>> + >>> + ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "arm64/cpuinfo:kstackrandomize", >>> + kstack_rng_setup, NULL); >> >> This will run initial seeding, but don't we need to reseed this with >> some kind of frequency? > > Won't that defeat the purpose of the patch that was intended > to make the syscall latency more predictable? At least the > simpler approaches of reseeding from the kstack_rng() > function itself would have this problem, deferring it to > another context comes with a separate set of problems. And that describes why I've not come up with an inline reseeding solution. Which of course isn't a problem on !arm if one just pushes a few bits of a cycle counter into the rnd_state every few dozen syscalls, or whatever. Mark R, mentioned offline the idea of just picking a few bits off CNTVCT as a seed, but its so slow it basically has to be used to fuzz a bit or two of rnd_state on some fairly long interval. Long enough that if someone has a solution for extracting rnd_state it might not add any additional security. Or that is my take, since i'm not a big fan of any independent counter/clock based RNG seeding (AFAIK, entropy from clocks requires multiple _independent_ sources). This is a bit out of my wheelhouse, so I defer to anyone with a better feel or some actual data. The best plan I have at the moment is just some deferred work to call kstack_rng_setup on some call or time based interval, which AFAIK isn't ideal for RT workloads which expect ~100% CPU isolation. Plus, that solution assumes we have some handle on how fast an attacker can extract kstackrng sufficiently to make predictions. Again, thanks to everyone for looking at this, Jeremy