Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6137031iog; Thu, 23 Jun 2022 12:07:14 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t/uHAkPTulsjOFGPeOwxqqUrGxUmhsVnmf72/gY6bQil6jLTFVATJ332O9qRv2tgnpqyFW X-Received: by 2002:a17:90b:4b83:b0:1e3:3ad3:612c with SMTP id lr3-20020a17090b4b8300b001e33ad3612cmr5469847pjb.87.1656011234437; Thu, 23 Jun 2022 12:07:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011234; cv=none; d=google.com; s=arc-20160816; b=IQIgCURf0ROFc54Ws3mDkbaMpEWjOdbtV6OqfHSDoxxDgD5cO90KsnIq+Y9sg7/DKW JjhJLwyUKuQb1cq8/z8VDnuZC376Mi/MzN2Xua7OKlA5w1sRyh0l8H8kZu7r7g257xvJ MXEVLdJ3Me4qotK5uKpqAEXiRmJNMRNmnBzxvxggQeso53kfx4fBQ7+Xj7DYr2p5w2kU 7mMTXsBf0qLthM7CZZj7JxyPTbS67/dF0ylFkEfAmaCLZw7DZGLsKQKr8BAnlZMt4Ujb ENhr0j8tkEOz2FlCGhdLRIWqVHNM+nFDnuyTLgcnimEaPIqPkugpWPrda7Vch185l8Wb jfTw== 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=hVNeJNMeTPVJTAIi7x/tZnJ7G9G88x53K7nHV5LRxII=; b=j1AdesOvdcfxi8jX9ram44c/cX3GhZTwk48H+Av8Yfu2Cs8LXeuccv/W62ub2szgkL Xyfvpq4MiEOq38OC8UyGAH0IxqWggTN3UcPxjXzyk+t7+MxxJrd03eev1x5vZIx8u0yp 1Y7afq37jKAsC6+d/3D7BlJiHOmv5tnsPWcRBFYD7UdFLlXP9xvRZO+ObzT3HFAIrleC iTkzwmosHDv4xAmvKH+hCuACy9PM9sINbunepjbm66rQXVlDTd+nY5NO1p1AMeToz184 cdZrGLDnmeTjZGgrzYvezFZgKCCapjcOgyvLPi1WrB4wl09ZAZtZdNI8nA3+91DxMume 9Vyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=lwlPngiE; 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 b23-20020a17090acc1700b001eac98dbaedsi219248pju.78.2022.06.23.12.07.02; Thu, 23 Jun 2022 12:07:14 -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=lwlPngiE; 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 S235301AbiFWSPn (ORCPT + 99 others); Thu, 23 Jun 2022 14:15:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236565AbiFWSPA (ORCPT ); Thu, 23 Jun 2022 14:15:00 -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 3D432C1603; Thu, 23 Jun 2022 10:21:35 -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 C3D6B61DC6; Thu, 23 Jun 2022 17:21:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BA814C3411B; Thu, 23 Jun 2022 17:21:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004894; bh=JDuB3x/JgXrkRqWdHTztUXmkncppqyWvZMCVIUXk+GE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lwlPngiEGwnDNvf07gHPpVvAYPEzPoXwIfphmhRYoGfBV49Oo8+86xEzSrz7lfg6/ +/stsiBC18cGIoKD68TMk2MUDzuas4MqLxkTOZxUCr3MNfdWIrlKJG/NGPvtbWDwuv MC8U1HAbLDh7xYEME+pB9drPjnqbL2m2bUdX07I8= 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.19 167/234] random: convert to using fops->write_iter() Date: Thu, 23 Jun 2022 18:43:54 +0200 Message-Id: <20220623164347.779049007@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164343.042598055@linuxfoundation.org> References: <20220623164343.042598055@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 @@ -1251,39 +1251,31 @@ static __poll_t random_poll(struct file return crng_ready() ? EPOLLIN | EPOLLRDNORM : EPOLLOUT | EPOLLWRNORM; } -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) @@ -1315,9 +1307,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: @@ -1334,20 +1325,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. */ @@ -1373,7 +1376,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, @@ -1382,7 +1385,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,