Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6050429iog; Thu, 23 Jun 2022 10:16:32 -0700 (PDT) X-Google-Smtp-Source: AGRyM1siAMh9hORuCRN+iiIMNtgtajmH4cRYmaD89J6o/5QUM67KV7KPMmnbzMGZq9E06dmoBE5q X-Received: by 2002:a17:907:1314:b0:722:fc80:3b34 with SMTP id vj20-20020a170907131400b00722fc803b34mr6189158ejb.583.1656004592572; Thu, 23 Jun 2022 10:16:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656004592; cv=none; d=google.com; s=arc-20160816; b=LibOWU4xHAsm+EYZg+bgMtYO26igaDmnEHIGXfyDiRmqlSLiyVy8ACfzz6iAL9UUjc jFqasJaP8YRGQd3I4tV/g6tw/VZL8K7MMHZD3LYpP5hpEd9EtRsVIXwh4KfP9mjDa8A1 isMXXUNKDa3dNbXKa6jMKNt2FNCmToRyqb0kkgbSvp0vH8AeDT22jaCynTnuI+GucKKz jd9udgzyNSzIB0usbCFAYlRW22vUOotEdvHgGhyhhmAmi18wwRRkIJPao7P28vsWkHlU wZ2oQFN3PGekLlsX6RASaKeZ1VxwQHU6YztICLkpyt/uwP6uDAgrnBN8mAPr0KIOsMoT gqow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=0eLVBAh1NR3P72BG/OQCWODuOkcLbEVJl3ozFhL/A+g=; b=g4iHar/BNf0TatqVWglxisJHZ8a+X2WACUOKIQtGl1ZIQv6sXfVUzVoCaxQ6v9lQUB yeEYSGWc661CllexO3JPUIlrk9oaVE/RZLCbZq+8lN4GKtE3uPIrV82DsWsr5/HfindH 1p5JZNgdX3+IRREnmy0UAwKSnWct0pdnSZsEhn1+XeOov+r0tP/8ZhuMiNg2q9/0OEgE 5bhGkaOo5mIvMutKo6ez2BIymGvmwXO22TMitYGaTPeEpSnvgRj9AxABqXnX2KtkBB9c FimD584bPdKkhgm9oM26Q48pGnUJUudxE7fjAPd/3IAwtfJHGHP5S8eYVOD0CMq7HtSa nXag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="2B/hKp7s"; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a100-20020a509eed000000b0042df64a5b1csi111885edf.332.2022.06.23.10.16.07; Thu, 23 Jun 2022 10:16:32 -0700 (PDT) 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=@linuxfoundation.org header.s=korg header.b="2B/hKp7s"; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230090AbiFWRJT (ORCPT + 99 others); Thu, 23 Jun 2022 13:09:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232052AbiFWRHV (ORCPT ); Thu, 23 Jun 2022 13:07:21 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E0825250F; Thu, 23 Jun 2022 09:56:08 -0700 (PDT) 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 3D199603E0; Thu, 23 Jun 2022 16:56:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B77FC3411B; Thu, 23 Jun 2022 16:55:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003360; bh=sqG32h4dd04TUrvBf8ZFamf0IxGJmSnCHUD/y7178dg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2B/hKp7siQk6K5NAII0tgaTXSpsRUdYlJz/SnA7/2SxC+ETlEztN9xE1/3cX3+bp8 vnNClfeI5VTPkdqGUh1REFx2EOrSXL3eff7vbjePf5RcMCAossf/KnufnOh3HrF7/S mDmm4mEqrQTUF0n/96yZH/gKG3L2LvPm5DwD6Fd8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jens Axboe , Al Viro , "Jason A. Donenfeld" Subject: [PATCH 4.9 211/264] random: convert to using fops->write_iter() Date: Thu, 23 Jun 2022 18:43:24 +0200 Message-Id: <20220623164350.042457733@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164344.053938039@linuxfoundation.org> References: <20220623164344.053938039@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 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,T_SCC_BODY_TEXT_LINE 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 From: Jens Axboe commit 22b0a222af4df8ee9bb8e07013ab44da9511b047 upstream. Now that the read side has been converted to fix a regression with splice, convert the write side as well to have some symmetry in the interface used (and help deprecate ->write()). Signed-off-by: Jens Axboe [Jason: cleaned up random_ioctl a bit, require full writes in RNDADDENTROPY since it's crediting entropy, simplify control flow of write_pool(), and incorporate suggestions from Al.] Cc: Al Viro Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 67 ++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 32 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1252,39 +1252,31 @@ static unsigned int random_poll(struct f return crng_ready() ? POLLIN | POLLRDNORM : POLLOUT | POLLWRNORM; } -static int write_pool(const char __user *ubuf, size_t len) +static ssize_t write_pool(struct iov_iter *iter) { - size_t block_len; - int ret = 0; u8 block[BLAKE2S_BLOCK_SIZE]; + ssize_t ret = 0; + size_t copied; - while (len) { - block_len = min(len, sizeof(block)); - if (copy_from_user(block, ubuf, block_len)) { - ret = -EFAULT; - goto out; - } - len -= block_len; - ubuf += block_len; - mix_pool_bytes(block, block_len); + if (unlikely(!iov_iter_count(iter))) + return 0; + + for (;;) { + copied = copy_from_iter(block, sizeof(block), iter); + ret += copied; + mix_pool_bytes(block, copied); + if (!iov_iter_count(iter) || copied != sizeof(block)) + break; cond_resched(); } -out: memzero_explicit(block, sizeof(block)); - return ret; + return ret ? ret : -EFAULT; } -static ssize_t random_write(struct file *file, const char __user *ubuf, - size_t len, loff_t *ppos) +static ssize_t random_write_iter(struct kiocb *kiocb, struct iov_iter *iter) { - int ret; - - ret = write_pool(ubuf, len); - if (ret) - return ret; - - return (ssize_t)len; + return write_pool(iter); } static ssize_t urandom_read_iter(struct kiocb *kiocb, struct iov_iter *iter) @@ -1316,9 +1308,8 @@ static ssize_t random_read_iter(struct k static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) { - int size, ent_count; int __user *p = (int __user *)arg; - int retval; + int ent_count; switch (cmd) { case RNDGETENTCNT: @@ -1335,20 +1326,32 @@ static long random_ioctl(struct file *f, return -EINVAL; credit_init_bits(ent_count); return 0; - case RNDADDENTROPY: + case RNDADDENTROPY: { + struct iov_iter iter; + struct iovec iov; + ssize_t ret; + int len; + if (!capable(CAP_SYS_ADMIN)) return -EPERM; if (get_user(ent_count, p++)) return -EFAULT; if (ent_count < 0) return -EINVAL; - if (get_user(size, p++)) + if (get_user(len, p++)) + return -EFAULT; + ret = import_single_range(WRITE, p, len, &iov, &iter); + if (unlikely(ret)) + return ret; + ret = write_pool(&iter); + if (unlikely(ret < 0)) + return ret; + /* Since we're crediting, enforce that it was all written into the pool. */ + if (unlikely(ret != len)) return -EFAULT; - retval = write_pool((const char __user *)p, size); - if (retval < 0) - return retval; credit_init_bits(ent_count); return 0; + } case RNDZAPENTCNT: case RNDCLEARPOOL: /* No longer has any effect. */ @@ -1374,7 +1377,7 @@ static int random_fasync(int fd, struct const struct file_operations random_fops = { .read_iter = random_read_iter, - .write = random_write, + .write_iter = random_write_iter, .poll = random_poll, .unlocked_ioctl = random_ioctl, .fasync = random_fasync, @@ -1383,7 +1386,7 @@ const struct file_operations random_fops const struct file_operations urandom_fops = { .read_iter = urandom_read_iter, - .write = random_write, + .write_iter = random_write_iter, .unlocked_ioctl = random_ioctl, .fasync = random_fasync, .llseek = noop_llseek,