From patchwork Mon Jul 20 10:53:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Filardo X-Patchwork-Id: 1332186 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=Z3jMvbma; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=cFzAm6YU; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4B9JXQ42nmz9s1x for ; Mon, 20 Jul 2020 20:55:02 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=wfx3kr2gX+Zfq8pjVWXC8PW4qaf6a6wavC4neL1r4Eo=; b=Z3jMvbmao7XKXfr6FdfZv5IGE 2XaPF/fFh9YqAwe6ksOi6u7ApafNiPRuPbqZyp+MZxtJ1TQ6Z8pYdFwzAdO78wS9WpIQn8uIIxExn MD58ao776scZLQcq9ZnWprynqLGpGzKwUT73r70ALxiDCLlmrsRdKz8ML39iNlCZctKlsuTEoxmFe 1rcNvs1C7eWwze+IjHWsTRxD7gC/M38L//H9E7BOQpo+IxbzrYOv29Ati7e5K3WROpUq/KT1N8ZVG hFAanSnS5OfDdMKa33UPjq6LYFbwNofFaNOOkkqdGl8Zm6X/XgL1JZs6yUoQQwAKgqIzWhMPUq+WF W97TdZ/TA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jxTQ2-0003zo-Pb; Mon, 20 Jul 2020 10:53:14 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jxTQ0-0003yM-7D for openwrt-devel@lists.openwrt.org; Mon, 20 Jul 2020 10:53:13 +0000 Received: by mail-wm1-x341.google.com with SMTP id o2so24759255wmh.2 for ; Mon, 20 Jul 2020 03:53:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bfOBNJO6ULzuUaI1wzCbsx2DL9pKJc5bOfEHyANW92I=; b=cFzAm6YU5yQgWRiqbUgnAlxo2nsULCPYbMpPtfi5lNCbQgsvzkXCjcnegB47pHWj6k oCNb8b5JuevWVk3vquyFTaXn6jlf/8e5OEE46CLL+Q8QBkQeZvPdpNdOgoKeXdGqhdAb aMq5G3BtJFrQO003uuRxwndVVg90pDs/rNOVOA/82onfNidkesLfgbv62rBuiIG89sFi 3/worf5tJl5QPxU3RN+XIhYE1rk76ppNTrqBmmbUdTGO7OthOE6Ng4nO3iu2zWIWnQnz QvRlqYqfPo1BJOQPOz2sxTaA4yZHUihD1Z4zNOqBosq2NGaKGL02T4EJ/+SsJp/e+ymM 5mEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bfOBNJO6ULzuUaI1wzCbsx2DL9pKJc5bOfEHyANW92I=; b=j16MD4ChktDcZkE+tn0gMqHHXFinv1Muv/gy32V8NBAdLVfaYj2+BDJR02awd2K4dT s3PcqptA1jyITDAcIPrdZWlo0qLgBtF0URPE0OyPf8WySK7nUqp5R7q6RMKBFAO2d85d prHZHr6RvRvHaXkT3C9oxgJw/jmwfyV8PAwAbZ5Tll5f+kqMzHZdq2wQ+1hDQQTizTyC 2t34uDd5lln0czz8+m5YL4ha6GK4l4CW8nIQ/pE+oeHJcFrPuZZelUO2E0GD/iGrgJEp +0kNc7HnYFhwM6sdDSVoMzT4ngw4l6bekgdJhNKulNhGcbQHUp3nPjONporvp/Vndk6r +VgQ== X-Gm-Message-State: AOAM531ZXykVviEsoe8B1HGoA2oCRXfWKOuI38pORto0n9pctoIGceOI O9I5O498Ke/CQp0D4jsduIUt0jQnlRw= X-Google-Smtp-Source: ABdhPJwMYguFTyCz9IJlf7qbMhb90A8GwArZEstOxEA6EBtmprXxuUatsIBgAT7Jl1ROdA/u9F9iww== X-Received: by 2002:a1c:7e44:: with SMTP id z65mr20434909wmc.52.1595242389495; Mon, 20 Jul 2020 03:53:09 -0700 (PDT) Received: from pf.fritz.box ([2a02:8010:63ca:0:ad41:fb8f:65c1:1e24]) by smtp.gmail.com with ESMTPSA id x18sm32079991wrq.13.2020.07.20.03.53.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jul 2020 03:53:08 -0700 (PDT) From: nwfilardo@gmail.com To: openwrt-devel@lists.openwrt.org Subject: [PATCH v2 1/4] urngd: Refactor out persistant rand_pool_info Date: Mon, 20 Jul 2020 11:53:01 +0100 Message-Id: <20200720105304.1090741-1-nwfilardo@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200712205739.238197-1-nwfilardo@gmail.com> References: <20200712205739.238197-1-nwfilardo@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200720_065312_293576_CD2F8550 X-CRM114-Status: GOOD ( 14.23 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:341 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [nwfilardo[at]gmail.com] -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Nathaniel Wesley Filardo , =?utf-8?q?Petr_=C5=A0tet?= =?utf-8?q?iar?= Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org From: Nathaniel Wesley Filardo There's no need to hold one of these structures across invocations of low_entropy_cb. Just put one on the stack and use that. Signed-off-by: Nathaniel Wesley Filardo --- urngd.c | 62 +++++++++++++++++---------------------------------------- 1 file changed, 18 insertions(+), 44 deletions(-) diff --git a/urngd.c b/urngd.c index 35ccdec..f782a9a 100644 --- a/urngd.c +++ b/urngd.c @@ -54,8 +54,8 @@ #define OVERSAMPLINGFACTOR 2 #define DEV_RANDOM "/dev/random" #define ENTROPYAVAIL "/proc/sys/kernel/random/entropy_avail" -#define ENTROPYPOOLBYTES (sizeof(struct rand_pool_info) + \ - (ENTROPYBYTES * OVERSAMPLINGFACTOR * sizeof(char))) +#define ENTROPYPOOLBYTES \ + (ENTROPYBYTES * OVERSAMPLINGFACTOR * sizeof(char)) #ifdef URNGD_DEBUG unsigned int debug; @@ -64,7 +64,6 @@ unsigned int debug; struct urngd { struct uloop_fd rnd_fd; struct rand_data *ec; - struct rand_pool_info *rpi; }; static struct urngd urngd_service; @@ -75,53 +74,40 @@ static inline void memset_secure(void *s, int c, size_t n) __asm__ __volatile__("" : : "r" (s) : "memory"); } -static size_t write_entropy(struct urngd *u, char *buf, size_t len, - size_t entropy_bytes) +static size_t write_entropy(struct urngd *u, struct rand_pool_info *rpi) { int ret; - size_t written = 0; - - /* value is in bits */ - u->rpi->entropy_count = (entropy_bytes * 8); - u->rpi->buf_size = len; - memcpy(u->rpi->buf, buf, len); - memset(buf, 0, len); - - ret = ioctl(u->rnd_fd.fd, RNDADDENTROPY, u->rpi); + ret = ioctl(u->rnd_fd.fd, RNDADDENTROPY, rpi); if (0 > ret) { ERROR("error injecting entropy: %s\n", strerror(errno)); + return 0; } else { - DEBUG(1, "injected %zub (%zub of entropy)\n", len, entropy_bytes); - written = len; + DEBUG(1, "injected %ub (%ub of entropy)\n", + rpi->buf_size, rpi->entropy_count/8); + ret = rpi->buf_size; } - u->rpi->entropy_count = 0; - u->rpi->buf_size = 0; - memset(u->rpi->buf, 0, len); - - return written; + return ret; } static size_t gather_entropy(struct urngd *u) { + ssize_t ent; size_t ret = 0; - char buf[(ENTROPYBYTES * OVERSAMPLINGFACTOR)]; + struct rand_pool_info *rpi = alloca(sizeof(*rpi) + ENTROPYPOOLBYTES); - if (jent_read_entropy(u->ec, buf, sizeof(buf)) < 0) { + ent = jent_read_entropy(u->ec, (char *)&rpi->buf[0], ENTROPYPOOLBYTES); + if (ent < 0) { ERROR("cannot read entropy\n"); return 0; } - ret = write_entropy(u, buf, sizeof(buf), ENTROPYBYTES); - if (sizeof(buf) != ret) { - ERROR("injected %zub of entropy, less then %zub expected\n", - ret, sizeof(buf)); - } else { - ret = sizeof(buf); - } + rpi->buf_size = ENTROPYPOOLBYTES; + rpi->entropy_count = 8 * ENTROPYBYTES; - memset_secure(buf, 0, sizeof(buf)); - DEBUG(2, DEV_RANDOM " fed with %zub of entropy\n", ret); + ret = write_entropy(u, rpi); + + memset_secure(&rpi->buf, 0, ENTROPYPOOLBYTES); return ret; } @@ -141,12 +127,6 @@ static void urngd_done(struct urngd *u) u->ec = NULL; } - if (u->rpi) { - memset(u->rpi, 0, ENTROPYPOOLBYTES); - free(u->rpi); - u->rpi = NULL; - } - if (u->rnd_fd.fd) { close(u->rnd_fd.fd); u->rnd_fd.fd = 0; @@ -167,12 +147,6 @@ static bool urngd_init(struct urngd *u) return false; } - u->rpi = malloc(ENTROPYPOOLBYTES); - if (!u->rpi) { - ERROR("rand pool alloc failed\n"); - return false; - } - u->rnd_fd.cb = low_entropy_cb; u->rnd_fd.fd = open(DEV_RANDOM, O_WRONLY); if (u->rnd_fd.fd < 1) {