Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2422647rwb; Sat, 19 Nov 2022 16:03:26 -0800 (PST) X-Google-Smtp-Source: AA0mqf727/e7veVgM1t1t8fkyHqNWYZZyDI+mtL1iAutu1Rjbz+IdfpSLkOxmH8McqTTEzZLu8cd X-Received: by 2002:a05:6402:184a:b0:469:4d00:9f95 with SMTP id v10-20020a056402184a00b004694d009f95mr2875581edy.64.1668902606518; Sat, 19 Nov 2022 16:03:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668902606; cv=none; d=google.com; s=arc-20160816; b=bufUYKOU6G844Y9IEwLN9lp9upo/eWsUheOZwBglovbEjNc8C1E+VlDiRNc4YYkgD+ Fx5C4/NE5q80KVF4DDdPM1EX2Sx5JOvAyTSRXh7M76I6mNmf2gnlo1hS2/cuLxwr4pko VGO1iIXWeS0jFeYRRFa1J1FgTWhazdaWK+M/OW8nNiyrmh2a58ee08QE/GlCnqtshUF8 ejSzg96Our/QNhSCB3iAMlQDFdC1yzSAqTRNgfWdv4Qi6PitSXUnUsmziALPyOY6Lro1 2bbEkSBJswh/QBiNo7y4t3jdkYU4AUclybpHtV2/OuI5sGyoPai9THgUClJy6mfi1hLY I0RA== 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=pF5hygQOZimknkE9dVS3ToHPgi15Fs/0Dk7gv5XPx2I=; b=wICTBPz+hlhiF169nx1TZvBFdS/slfZ0dUM68NQRJdpMS8rqy6pAZlgFh8HE/ECETq EPWz9GYjC05AqGjC3VpMPyFNNshWI+58UhbubZziNs/zYi45yHQUxwyHcvMkr2YudfPN Rb75ZV4Mblti/OWqanYKiLwP4hIUzVdxT8FV2Y7o1VXAz9TPbWdSOJ76tJPG1WFIDaCE rSK7JxN/cM/zi5BEsYvl9Z5no4JPGJmfQL+EH/vUJFWrkaHWCe6J2ESSXY2XUqiejAKH NfH5/5MSm9Z1B76fBMCXDhZId/aFzURoEH4dIBgr3NupBobDgDPEej0yzco/B62bZ9h6 jB8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=gQCJ1S+O; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x12-20020a170906148c00b00791a3dd01b6si5464476ejc.864.2022.11.19.16.02.54; Sat, 19 Nov 2022 16:03:26 -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=@zx2c4.com header.s=20210105 header.b=gQCJ1S+O; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231666AbiKSX7R (ORCPT + 99 others); Sat, 19 Nov 2022 18:59:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229635AbiKSX7R (ORCPT ); Sat, 19 Nov 2022 18:59:17 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D3FE14D32; Sat, 19 Nov 2022 15:59:15 -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 2D89F60BFA; Sat, 19 Nov 2022 23:59:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4B85CC433C1; Sat, 19 Nov 2022 23:59:13 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="gQCJ1S+O" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1668902350; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=pF5hygQOZimknkE9dVS3ToHPgi15Fs/0Dk7gv5XPx2I=; b=gQCJ1S+OriuBT7to95epQyIA2rapjpQ/C5zZiKyFZeQnVxr41IehVybTQz1bYZ23k99xST cAbM5WFiac4vkg2pXEsah7omTBW1qQjex1gv7LaKbB4tHeC9XG9+JmfOguXy2mXHQ5a2W8 6FU171U7Q+37zlYEkJiztmMtxU3E0PU= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 01fc7d27 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sat, 19 Nov 2022 23:59:10 +0000 (UTC) Date: Sun, 20 Nov 2022 00:59:07 +0100 From: "Jason A. Donenfeld" To: Eric Biggers 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=utf-8 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, 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 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? > > + pages_addr = vm_mmap(NULL, 0, alloc_size, PROT_READ | PROT_WRITE, > > + MAP_PRIVATE | MAP_ANONYMOUS | MAP_LOCKED, 0); > > + if (IS_ERR_VALUE(pages_addr)) > > + return pages_addr; > > This will only succeed if the userspace process has permission to mlock pages, > i.e. if there is space available in RLIMIT_MEMLOCK or if process has > CAP_IPC_LOCK. I suppose this is working as intended, as this syscall can be > used to try to allocate and mlock arbitrary amounts of memory. > > I wonder if this permission check will cause problems. Maybe there could be a > way to relax it for just one page per task? I don't know how that would work, > though, especially when the planned usage involves userspace allocating a single > pool of these contexts per process that get handed out to threads. Probably though, we don't want to create a mlock backdoor, right? I suppose if a user is above RLIMIT_MEMLOCK, it'll just fallback to the slowpath, which still works. That seems like an okay enough circumstance. Jason