Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp2931975iog; Mon, 20 Jun 2022 07:45:15 -0700 (PDT) X-Google-Smtp-Source: AGRyM1swrEmL68JdoInfn06gSr6zNUBSJ68uRfVCsw1pqt58NcmcLxJTCJdmCUnIK/gojc4hCYcK X-Received: by 2002:a65:6944:0:b0:408:aa65:9464 with SMTP id w4-20020a656944000000b00408aa659464mr21804090pgq.160.1655736314902; Mon, 20 Jun 2022 07:45:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655736314; cv=none; d=google.com; s=arc-20160816; b=O4zSG2bxmdvvduDLK3w4CSoxzCvjDpiqMoKnFUjsNfmiEytHQkz9bELuHulb70524E 6i4MPTmkRYeqsgXR5abh5/j/1yq8ipbzsmezidwvAE13YObC9/fPrW82uTYnBiFXrdva ATI/Ym875A01yOrPxFl68eMTBg5GBzTW+s3Zs4LX50DTyEs0Moq8I/otLg0sVL7feMCs 2F9GlufttOelJUO3GZ91M29kmGQmTEBr7jO1uoCaJwJMSw82VJAPvkJOslSRuZJIDwae qvMmXBFuzWK3tr0YCyQN8R0JGYNcKH5v2Ed/5706tVjKbSb2PkbFkjrmbO9Mr8eIueUf R5ng== 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=4INLRMRmRoR2y3RnPpRCzoZDGpsymboEwWWFVuST6Ps=; b=RgRlimcGaoy5u8Dvn43hoGEDFTSxeLosgCA7riSMesxfskXOhywzvhCbV1xecv01yD NdXPMYxpdZcuxJ5Fvfxq6bAFFi5C5OUySYexynUhEo4MV+B6J1hLEAO7/8S+JV0uaSTW VR4sfx6tEevELTigifbbUVx4EhBz9lf+x4mXzjyXcn0surWTJCZDGMQUoLLjDh9Vd5+e BIrLIYmHJXnumJm7TTEIedr//0+PWToTtsz+XmDdujGtGbEldJcKC0qoPg/mARmDmA6C ZIfw7s1bMzSUCS2IK6dEcdJzhZEijrltYesZS3zIW7dWpohQDm7+khf1NA4yx61IxN75 rtEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=u3oT2OuO; 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 q12-20020a056a00150c00b0050605f7112bsi17392424pfu.130.2022.06.20.07.45.02; Mon, 20 Jun 2022 07:45: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=u3oT2OuO; 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 S1351040AbiFTNuK (ORCPT + 99 others); Mon, 20 Jun 2022 09:50:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349007AbiFTNrp (ORCPT ); Mon, 20 Jun 2022 09:47:45 -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 784DA1A056; Mon, 20 Jun 2022 06:17:40 -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 0D38360FEF; Mon, 20 Jun 2022 13:16:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EDBF8C3411B; Mon, 20 Jun 2022 13:16:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655731010; bh=FHCpQXUyZuymp9eUMYo1Xrjj9Tv2JqAA3JogK1g4j7U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u3oT2OuOV9zFQ0RYY/hu7jpeF5T7+iYcZsm3RwlLVW9bdVFbd9YOQVgOFFOKCbN49 gyw7WFP+lhB28FAiTmrp0ubQ0Ous1BWYOvbXCAA31s1FXgrBeF+dYCiS+cf6rJf4Rs pjat6pzd2Jbg2B6kskVSu9LNEy08gXgTqYoNmBb8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Linus Torvalds , Jann Horn , "Jason A. Donenfeld" Subject: [PATCH 5.4 126/240] random: allow partial reads if later user copies fail Date: Mon, 20 Jun 2022 14:50:27 +0200 Message-Id: <20220620124742.663624602@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220620124737.799371052@linuxfoundation.org> References: <20220620124737.799371052@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: "Jason A. Donenfeld" commit 5209aed5137880fa229746cb521f715e55596460 upstream. Rather than failing entirely if a copy_to_user() fails at some point, instead we should return a partial read for the amount that succeeded prior, unless none succeeded at all, in which case we return -EFAULT as before. This makes it consistent with other reader interfaces. For example, the following snippet for /dev/zero outputs "4" followed by "1": int fd; void *x = mmap(NULL, 4096, PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); assert(x != MAP_FAILED); fd = open("/dev/zero", O_RDONLY); assert(fd >= 0); printf("%zd\n", read(fd, x, 4)); printf("%zd\n", read(fd, x + 4095, 4)); close(fd); This brings that same standard behavior to the various RNG reader interfaces. While we're at it, we can streamline the loop logic a little bit. Suggested-by: Linus Torvalds Cc: Jann Horn Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -523,8 +523,7 @@ EXPORT_SYMBOL(get_random_bytes); static ssize_t get_random_bytes_user(void __user *buf, size_t nbytes) { - ssize_t ret = 0; - size_t len; + size_t len, left, ret = 0; u32 chacha_state[CHACHA_BLOCK_SIZE / sizeof(u32)]; u8 output[CHACHA_BLOCK_SIZE]; @@ -543,37 +542,40 @@ static ssize_t get_random_bytes_user(voi * the user directly. */ if (nbytes <= CHACHA_KEY_SIZE) { - ret = copy_to_user(buf, &chacha_state[4], nbytes) ? -EFAULT : nbytes; + ret = nbytes - copy_to_user(buf, &chacha_state[4], nbytes); goto out_zero_chacha; } - do { + for (;;) { chacha20_block(chacha_state, output); if (unlikely(chacha_state[12] == 0)) ++chacha_state[13]; len = min_t(size_t, nbytes, CHACHA_BLOCK_SIZE); - if (copy_to_user(buf, output, len)) { - ret = -EFAULT; + left = copy_to_user(buf, output, len); + if (left) { + ret += len - left; break; } - nbytes -= len; buf += len; ret += len; + nbytes -= len; + if (!nbytes) + break; BUILD_BUG_ON(PAGE_SIZE % CHACHA_BLOCK_SIZE != 0); - if (!(ret % PAGE_SIZE) && nbytes) { + if (ret % PAGE_SIZE == 0) { if (signal_pending(current)) break; cond_resched(); } - } while (nbytes); + } memzero_explicit(output, sizeof(output)); out_zero_chacha: memzero_explicit(chacha_state, sizeof(chacha_state)); - return ret; + return ret ? ret : -EFAULT; } /*