Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2667061rwb; Thu, 17 Nov 2022 14:18:01 -0800 (PST) X-Google-Smtp-Source: AA0mqf4dZQRBiJ53HNckr1lAwPHBkFL1UXtWlKa9qOXLZgFZFROD+BlxvfqPgHrXpOxYYLQPpmfx X-Received: by 2002:a17:907:c24a:b0:7ac:2e16:bc31 with SMTP id tj10-20020a170907c24a00b007ac2e16bc31mr3733416ejc.242.1668723480839; Thu, 17 Nov 2022 14:18:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668723480; cv=none; d=google.com; s=arc-20160816; b=ZULAG3BukG+f6Sckoc65HUwlTSbIWpVhCYE5Myohz0xJ/1w4mIQwM3mfTSPT7ZCQBG c5Ta9+OnbWlxxrD+3OQBYbqhnAn8IG4WUvQP1wJI9uwFCIQGq8fkLj8Bi9OLGApk9ARK aJO1qA8DVbkXavdcdn4N6l7W6EQhicHo/1qwzYgyGh7Nv+zFERzjWu+j7p0LJSUI0GRQ 0AKOKBhVJQPwKSlL/VPpSiSuJgJnnjxxw2D1blsXF8QQM6eRhzJyy0tZ5AKklhAYDesg MLU/ZEEzcUzEBErN2IuwrESSHVrZPJ4pK9WYbcqRuGasS8BreLy7643SGTIvaUVvC4fr YeAA== 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=/HyEb+JRj1sN5jcDVViOGE116tvvuVTKsQc2QDnF6Sk=; b=lhrkawncwwRFIjxfcj3nLAN5Gzy6FoS1M2MBmEmhF2TUWeM3+dH/iXKRub6xaNiYtV gCBiYUKWWJZ4am48VLCCoHf87RMaexxMmlRqmo8FXFQ1PVl2Z+73mfq9HYsdn4kvMCY2 D+a8+N8VP+w6x3Bakiny4h3MGomtUnT4ZU7/+TPudT7G8VopvCMTxBAwyJvZmS9aaRDI PLBrhJi0sp6geRewJ9MZarlwlXAMm1uAFAFV3m6Hp7CHJHHq6k+tO79wNx5rGA5w8XZs eI2DmZyPtCn1aMHnCNmYABF38T0bJMtYGUZfErQk9SnFne5uP1kt6W5b66yz8B7Y3Jy5 oA/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="JrPNa/9v"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ss28-20020a170907c01c00b0078e11cbb722si1345988ejc.92.2022.11.17.14.17.38; Thu, 17 Nov 2022 14:18:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="JrPNa/9v"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S240446AbiKQVsW (ORCPT + 93 others); Thu, 17 Nov 2022 16:48:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240338AbiKQVsR (ORCPT ); Thu, 17 Nov 2022 16:48:17 -0500 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F2FE69DD8 for ; Thu, 17 Nov 2022 13:48:17 -0800 (PST) Received: by mail-pg1-x52e.google.com with SMTP id q1so3256832pgl.11 for ; Thu, 17 Nov 2022 13:48:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=/HyEb+JRj1sN5jcDVViOGE116tvvuVTKsQc2QDnF6Sk=; b=JrPNa/9vanKmhuJZy8rXzQjTS61AAtv3k9qysjVd2Yzu49dZaR9D++0L/kmHEL29MB PHni9yqVUqm7PXe/RRllu1ll5XoZhgUrc1sTqkiPYEVWSPFY17gp6J0v5YBPG3jDiITa 4JYrIIlCK85DhzjIoBvB9KvJpRw4ZpZElS9b8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=/HyEb+JRj1sN5jcDVViOGE116tvvuVTKsQc2QDnF6Sk=; b=2awFedfVUqCJcNkOAkQ5CrWOoHWYe1Qd76Eh2ZU3pxXdWqnyqdfPt5VLWT5BNmQUDo nZdEU284IXx0qxz1zimfRCmrj/kHuTg0QEBBKtF2UDtfVKrX2U6/YlHXUVHLyHKbU8o3 Rfv8stzj50DsXt6AwTpo+h/TKuflhqXKRnUdcHQCXCYxZ9hiuvkMuROoWKOlUHuLfpwv nk54bLlLLy5q9EVwu4qVehTtUikWHTdpAtg9/rOZxMMYhGu2B0/khd/vGHwZ7puF9Qvw 0hUHjg6yKP0XEKXPiAAwQsoJU3CZlJxR0dSyvuL7zkDAlj9iOfigVbk4a5ivwi+YFmz0 CPJQ== X-Gm-Message-State: ANoB5pkJw/1pxMSUcPoHCUUINKB8aM3H8usniHj/PGm/uvZ1nm5NtEZh KZ/TTxuZtPHlzWWoFVRcx0YpYQ== X-Received: by 2002:a63:f003:0:b0:45f:bf86:c917 with SMTP id k3-20020a63f003000000b0045fbf86c917mr3884645pgh.201.1668721696522; Thu, 17 Nov 2022 13:48:16 -0800 (PST) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id s125-20020a632c83000000b00439d071c110sm1525368pgs.43.2022.11.17.13.48.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 13:48:15 -0800 (PST) Date: Thu, 17 Nov 2022 13:48:14 -0800 From: Kees Cook To: "Jason A. Donenfeld" Cc: linux-kernel@vger.kernel.org, tytso@mit.edu, kees@kernel.org, linux@armlinux.org.uk, ydroneaud@opteya.com, gregkh@linuxfoundation.org, rdunlap@infradead.org Subject: Re: [PATCH v3] random: add helpers for random numbers with given floor or range Message-ID: <202211171347.92FDE65A6D@keescook> References: <2b3bc31e-c308-b04c-1759-26bcf8dba6d3@infradead.org> <20221117192620.2304613-1-Jason@zx2c4.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221117192620.2304613-1-Jason@zx2c4.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 17, 2022 at 08:26:20PM +0100, Jason A. Donenfeld wrote: > Now that we have get_random_u32_below(), it's nearly trivial to make > inline helpers to compute get_random_u32_above() and > get_random_u32_inclusive(), which will help clean up open coded loops > and manual computations throughout the tree. > > One snag is that in order to make get_random_u32_inclusive() operate on > closed intervals, we have to do some (unlikely) special case handling if > get_random_u32_interval(0, U32_MAX) is called. The least expensive way > of doing this is actually to adjust the slowpath of > get_random_u32_below() to have its undefined 0 result just return the > output of get_random_u32(). We can make this basically free by calling > get_random_u32() before the branch, so that the branch latency gets > interleaved. > > Signed-off-by: Jason A. Donenfeld I really like these -- unambiguous! :) Thanks for adjusting this API. Reviewed-by: Kees Cook -Kees -- Kees Cook