Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6129122iog; Thu, 23 Jun 2022 11:58:40 -0700 (PDT) X-Google-Smtp-Source: AGRyM1shfVHjCBNKYYFQseyjiq+em6WYQwnvBdmcimy9ESmb1mpxFl1Sg9KfzQw/nZPPILF6lrP8 X-Received: by 2002:a17:906:65c8:b0:713:ee3e:254d with SMTP id z8-20020a17090665c800b00713ee3e254dmr9891102ejn.0.1656010720284; Thu, 23 Jun 2022 11:58:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656010720; cv=none; d=google.com; s=arc-20160816; b=LZGkgun3dEwDw4sGWQa7Khogd9d2BD6tVmaJxFzwUzTbzpEOwzzfs28i77COQKn83F iyk/1iuQS/DpWRwlhoJwGbM0CZ4hrXatOj1j6EIC5c4IBGSYLEYp6UsNwr1FN+tPowC/ x4xRldvgpyiIuIa+Va3eOsE5DEdlIibyNdAK2SQ05ue7oM+JhruYqHjQnVrv2eElwOiu I7Kvdk+k+9zhezpokllDeA++UNb+aRfqcxOgFzS4qzXzN4nZSyO2QOofI/49908/1xB1 GJwqN6qsNNj7sEM5MpISk7w37fltH/0/58+ptj7KZPZumuH6d6gA7gYZER1UazLvSzDT hsdQ== 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=PIMs+T9vZR1jgrFfID4A6ocFB/42Nc9JYkYemW1MkGY=; b=WxFS6oOi5yUQQfEdMNAm9I3q22BGybt8U8t92+DgzSsvhqr3T0tFxj06F0QctlomHe 6/LN7v6SGXz/M2+qkF1Ncas7rB9r3bcNCkhEsaYm8/yfr+hU3jmrQ5ROtpVQvLmBSLuv fmJ3NBSu5h8aJHQhcc868NN7/RfqdRb3Kbp/EA4Js1VB/tkCsuA7UixlfOAYyrC3Hqrs REgKewHO3jd320ecg1LYR7sPIfD86vP34wRwQ8h57pw4J9PCMKzpJ7ABNYxKpUtAhIbQ I037wxZV/rVUHKnqu9GuNpNN1S0TS6n/wuMgUKcE6yy1PQ8JjU6zPK1tJP+ViNjzY/tE zscg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Nwpeo5nS; 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 eb9-20020a0564020d0900b00435bbe82254si495708edb.143.2022.06.23.11.58.14; Thu, 23 Jun 2022 11:58:40 -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=Nwpeo5nS; 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 S231653AbiFWRQ1 (ORCPT + 99 others); Thu, 23 Jun 2022 13:16:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233302AbiFWRMo (ORCPT ); Thu, 23 Jun 2022 13:12:44 -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 88CA74D25A; Thu, 23 Jun 2022 09:58:37 -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 96F3260AD7; Thu, 23 Jun 2022 16:58:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 70D09C3411B; Thu, 23 Jun 2022 16:58:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003515; bh=sbi8FXcon8b7rzkaZG1YD95/6rVLNILntmB9ekCc9/g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Nwpeo5nS+0tDKXBLXYwL6WWOIA5q7yu4nDIT6nj1GauzF+7a0QzBUCTfcdftGyxwW 175+7JBD0NvonegLikoP23R16lyEZq0s3UtKCtt2nW+X1JpuH5cGkB7tBFe+eN3c0S Q1flHyzHuWRbWCN/TlKqO2l0DyjHJUybQ/Gd5pVE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dominik Brodowski , "Jason A. Donenfeld" Subject: [PATCH 4.9 213/264] random: check for signals after page of pool writes Date: Thu, 23 Jun 2022 18:43:26 +0200 Message-Id: <20220623164350.100089050@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: "Jason A. Donenfeld" commit 1ce6c8d68f8ac587f54d0a271ac594d3d51f3efb upstream. get_random_bytes_user() checks for signals after producing a PAGE_SIZE worth of output, just like /dev/zero does. write_pool() is doing basically the same work (actually, slightly more expensive), and so should stop to check for signals in the same way. Let's also name it write_pool_user() to match get_random_bytes_user(), so this won't be misused in the future. Before this patch, massive writes to /dev/urandom would tie up the process for an extremely long time and make it unterminatable. After, it can be successfully interrupted. The following test program can be used to see this works as intended: #include #include #include #include static unsigned char x[~0U]; static void handle(int) { } int main(int argc, char *argv[]) { pid_t pid = getpid(), child; int fd; signal(SIGUSR1, handle); if (!(child = fork())) { for (;;) kill(pid, SIGUSR1); } fd = open("/dev/urandom", O_WRONLY); pause(); printf("interrupted after writing %zd bytes\n", write(fd, x, sizeof(x))); close(fd); kill(child, SIGTERM); return 0; } Result before: "interrupted after writing 2147479552 bytes" Result after: "interrupted after writing 4096 bytes" Cc: Dominik Brodowski Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1252,7 +1252,7 @@ static unsigned int random_poll(struct f return crng_ready() ? POLLIN | POLLRDNORM : POLLOUT | POLLWRNORM; } -static ssize_t write_pool(struct iov_iter *iter) +static ssize_t write_pool_user(struct iov_iter *iter) { u8 block[BLAKE2S_BLOCK_SIZE]; ssize_t ret = 0; @@ -1267,7 +1267,13 @@ static ssize_t write_pool(struct iov_ite mix_pool_bytes(block, copied); if (!iov_iter_count(iter) || copied != sizeof(block)) break; - cond_resched(); + + BUILD_BUG_ON(PAGE_SIZE % sizeof(block) != 0); + if (ret % PAGE_SIZE == 0) { + if (signal_pending(current)) + break; + cond_resched(); + } } memzero_explicit(block, sizeof(block)); @@ -1276,7 +1282,7 @@ static ssize_t write_pool(struct iov_ite static ssize_t random_write_iter(struct kiocb *kiocb, struct iov_iter *iter) { - return write_pool(iter); + return write_pool_user(iter); } static ssize_t urandom_read_iter(struct kiocb *kiocb, struct iov_iter *iter) @@ -1343,7 +1349,7 @@ static long random_ioctl(struct file *f, ret = import_single_range(WRITE, p, len, &iov, &iter); if (unlikely(ret)) return ret; - ret = write_pool(&iter); + ret = write_pool_user(&iter); if (unlikely(ret < 0)) return ret; /* Since we're crediting, enforce that it was all written into the pool. */