Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2494392rwb; Sat, 19 Nov 2022 17:45:48 -0800 (PST) X-Google-Smtp-Source: AA0mqf4k2Oamdp1BlplTftOSuWCVsFrNwpcXirB18GE12BHpeGAunNyj5AjlXmrZ1NWat23PLHJ/ X-Received: by 2002:a05:6402:1f0a:b0:459:b29:d896 with SMTP id b10-20020a0564021f0a00b004590b29d896mr11034811edb.9.1668908748218; Sat, 19 Nov 2022 17:45:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668908748; cv=none; d=google.com; s=arc-20160816; b=A/ThJC3AQC96rrLMgQGLpg2eU5k7V/+YAVoE/TyG7rfObnP5gUdEf/q/5l9hOAnKOk tkg9beKQwcEg+mD+jMpy/VMVyYrIGv6F+ux+KoUJKvOM7NBwn3gdNFv1KohoYmn1yeAZ 0feAkQXQNPb5QSWArAeYuEGNvUx7fhwknbC8AuyDdshCV8PyxoPU3eEwGbIVukTNkknB vFaZXJItfwEiMdLMqdz/CTYVGCtZURjy0ncxGwZeMbQJWWvCHN4Jy6j1BWdTZBnNUbpL ndaIM4OaqCI2SOSHRhqZlKY6qpOka/Q6DqxWnD3Khl5gQnOEabdP9YGAoi05FXdx0W9g 9O1A== 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=ziZ3QCR9TegXvRfAVw3dus3LlZT6rLRjJmtl8arGDzk=; b=u7gV6GlRoNUQsIv30g8Va87pS7ZgKoOCPPNjJO+sMooEThC+p1MGMa0jIgt0Mp+sxQ tbMv01hqdYNccICrimjDet+tAnIjQsWGI444uGcv0JPX9QLXRDFJ+loYA5cyg+VRgzlM 6RGaxES/zdoMfKq4E6b+f1Z84iJyiXl1Os2TXNPO7IDee4N8BqsJG3q87wQx1YYep4kf M8mlMe2f34hHXPighroHAxTS7pzMyFZQ92Jiait7/KLcbQl2nW+JSb0F3kTpKFxDVa/U pztlCFhwxHI3ti3VvMh19w7e8gmcNrPcMlEwLSHJbYvVVqcWsmYsOO5vHXKR4uEHK+IN XcGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=G5nq+us0; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x7-20020a056402414700b004677d91cdeasi5995523eda.244.2022.11.19.17.45.24; Sat, 19 Nov 2022 17:45:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto-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=@kernel.org header.s=k20201202 header.b=G5nq+us0; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229520AbiKTBkJ (ORCPT + 99 others); Sat, 19 Nov 2022 20:40:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229499AbiKTBkI (ORCPT ); Sat, 19 Nov 2022 20:40:08 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AFB0B94AD; Sat, 19 Nov 2022 17:40:08 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A402060BCB; Sun, 20 Nov 2022 01:40:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F92EC433D6; Sun, 20 Nov 2022 01:40:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668908406; bh=mB2/5SFy2jr4OJagL4mr8wczTaB3RfCbSmiQnv5noa4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=G5nq+us05sEucQ4RJRBz5n6pXnWIBxZo6AvKSt7WtLvvmxC2q8kgGEci49G0FEKMT R0cptdirpgBxNX303fr0yMWEkWQw4j1e42S21cywB1YXa9hGiDHaS65lvGeJX6KjMs Jyq30v5N2jWc2hmdwGXwQnoIiFRX9YR8dY8OyiGoMIg1Xm0QI32iXwilgwpTvLFGP3 tnmHb+371mqvMfXx0zl9lDXExjGT7NV9sorUk3JCI1ISrRMggtGpY22Dsu7t0VeaCl vVOdQTp5OmwjeP1qjPYYIbl7HnMJT89KpssX5Q7Z8du7uEPNmvxbjwIl9jzh7weVMf sstirvDV/4+1w== Date: Sat, 19 Nov 2022 17:40:04 -0800 From: Eric Biggers To: "Jason A. Donenfeld" Cc: linux-kernel@vger.kernel.org, patches@lists.linux.dev, linux-crypto@vger.kernel.org, x86@kernel.org, Thomas Gleixner , Greg Kroah-Hartman , Adhemerval Zanella Netto , Carlos O'Donell Subject: Re: [PATCH v5 1/3] random: add vgetrandom_alloc() syscall Message-ID: References: <20221119120929.2963813-1-Jason@zx2c4.com> <20221119120929.2963813-2-Jason@zx2c4.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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-crypto@vger.kernel.org On Sun, Nov 20, 2022 at 12:59:07AM +0100, Jason A. Donenfeld wrote: > Hi Eric, > > On Sat, Nov 19, 2022 at 12:39:26PM -0800, Eric Biggers wrote: > > On Sat, Nov 19, 2022 at 01:09:27PM +0100, Jason A. Donenfeld wrote: > > > +SYSCALL_DEFINE3(vgetrandom_alloc, unsigned long __user *, num, > > > + unsigned long __user *, size_per_each, unsigned int, flags) > > > +{ > > > + unsigned long alloc_size; > > > + unsigned long num_states; > > > + unsigned long pages_addr; > > > + int ret; > > > + > > > + if (flags) > > > + return -EINVAL; > > > + > > > + if (get_user(num_states, num)) > > > + return -EFAULT; > > > + > > > + alloc_size = size_mul(num_states, sizeof(struct vgetrandom_state)); > > > + if (alloc_size == SIZE_MAX) > > > + return -EOVERFLOW; > > > + alloc_size = roundup(alloc_size, PAGE_SIZE); > > > > Small detail: the roundup to PAGE_SIZE can make alloc_size overflow to 0. > > > > Also, 'roundup(alloc_size, PAGE_SIZE)' could be 'PAGE_ALIGN(alloc_size)'. > > Good catch, thanks. So perhaps this? > > alloc_size = size_mul(num_states, sizeof(struct vgetrandom_state)); > if (alloc_size > SIZE_MAX - PAGE_SIZE + 1) > return -EOVERFLOW; > alloc_size = PAGE_ALIGN(alloc_size); > > Does that look right? Yes. Maybe use 'SIZE_MAX & PAGE_MASK'? Another alternative is the following: if (num_states > (SIZE_MAX & PAGE_MASK) / sizeof(struct vgetrandom_state)) return -EOVERFLOW; alloc_size = PAGE_ALIGN(num_states * sizeof(struct vgetrandom_state)); - Eric