From patchwork Wed Mar 23 13:50:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Rui Salvaterra X-Patchwork-Id: 1608633 X-Patchwork-Delegate: rsalvaterra@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=2VVSqkiy; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=ZdVjZ9UQ; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4KNqZg3LCmz9s0B for ; Thu, 24 Mar 2022 00:53:47 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=X2oOuGCk1+ji3apn47BGnUjhojw63pNvhom+3dLD4wk=; b=2VVSqkiyKwX1I1 fSdWzLqo0GFnZurHkaIx5e0ptVRxsT213l1T6NWtifCLsoOnNTq4xPwbwmurpzWWyu/L2UfHq8lFO O2x1hQPHr40l/Oz0OuZTEf0O2FQozqj5ymo4QgOvFByXVbxau+li8cuS3O4mOt8SL8a4l5vBBHLEN dz8vLl5dOjkSga/VvNCptNHIwLjJc2RsIhTiC7IONOJD4tt3c7lKQ9cUduIbVwqE8vVKkbDmkUK5G 5cdaAAUBfwP8mjIHwk1yTHCerFg6SiPZRe3lALeU1/+pqFfxL+SYA1atuZ4fIaohBekT4qtQxvOS3 wwNmflMSFVadeTjVdSfQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nX1Nu-00Dmn5-Ib; Wed, 23 Mar 2022 13:50:46 +0000 Received: from mail-ed1-x530.google.com ([2a00:1450:4864:20::530]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nX1Nn-00Dmm0-Fe for openwrt-devel@lists.openwrt.org; Wed, 23 Mar 2022 13:50:41 +0000 Received: by mail-ed1-x530.google.com with SMTP id w25so1894449edi.11 for ; Wed, 23 Mar 2022 06:50:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Xd190+7uILAprcf2v+8k4ETLoinOs5SRWXgHQHwIwNQ=; b=ZdVjZ9UQVrjEZODUZMDECzlCT2ogPTlebG3GWO9NI8Ylf8UpQ8ijXe8dgi+7F368yQ CvrVa7G4U5rLP0rv5SZVV489V7usjDmu2Bh+pl0LeYsY9jOQ0Isco0p8fZFpOTX80Z6X MgHgMjhM6Kdv0fFqWHneQRF89Er9MOpaDmDrRNDrLL68vttxh0d2aQfQsM6ue0odVwWf 3aMCRuGgke6d56X6Dvw41yiobx0wA3z5xk/SYS5zQ21ixUGjQBM0xHIk3PLqtpqPmUDf Lp7TSURnvvRkl+Fu1Z1HATR7XcCLtuthEGjWsqBapNLGMoBJfRNwX0C0zbOZwbBDKzib c2rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Xd190+7uILAprcf2v+8k4ETLoinOs5SRWXgHQHwIwNQ=; b=nfVVhvDc7hIfNuCVudgxA8eEA51bMRTZ6afPP4YOTHq9rtPOjr3E671ej4xR4ZLx3y hjmS2euq9gXbhFTBVqyoM0SfIDppEshEsm7xd1N0XY4xEi5YTQlzTKYqR1jgNCPPvjWG xfAysi5SBG+YsU9S+OpBJhs97gw8xSluumQGnpc8KVe7CCWKG7aiPAvPTBZhdwbbM4aI bCKpteWnlf5mPPtXYn1GovbBuR8AThkNaHibQWPIZFPugICa4upWT/Ysw336VV6HJelM kTCA01dwFkqlVuZAFDE5vO2bO8nyRq4rNcs7GPCvfZv9xrxocTWEyfW9wMgMw+bSYeCb VA0g== X-Gm-Message-State: AOAM533U1iEax4Ev8Ykuw8bwBuRcd8RGcy32vSyR8Wl9dTziXpbAQRVK XJdG28B7Vup0tHiSxr3EP8oIe/AmSw== X-Google-Smtp-Source: ABdhPJzWolc5GmRaHIZLo3Ebe29FuA0GJJrpfdZ9aZmV/R0p+f8RfHs2YCV2NQ3pQ/ia7fSqbU5O+A== X-Received: by 2002:a05:6402:b67:b0:419:1bf0:a153 with SMTP id cb7-20020a0564020b6700b004191bf0a153mr169576edb.278.1648043437481; Wed, 23 Mar 2022 06:50:37 -0700 (PDT) Received: from crystalwell.adg.lan (a95-94-70-201.cpe.netcabo.pt. [95.94.70.201]) by smtp.gmail.com with ESMTPSA id u18-20020a17090617d200b006db07a16cf5sm9834589eje.77.2022.03.23.06.50.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Mar 2022 06:50:37 -0700 (PDT) From: Rui Salvaterra To: openwrt-devel@lists.openwrt.org Cc: Jason@zx2c4.com, ynezz@true.cz, nbd@nbd.name, daniel@makrotopia.org, hauke@hauke-m.de, Rui Salvaterra Subject: [PATCH v2] ath9k: backport hw_random API support Date: Wed, 23 Mar 2022 13:50:30 +0000 Message-Id: <20220323135030.4024159-1-rsalvaterra@gmail.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220323_065039_569166_0D20EB1B X-CRM114-Status: GOOD ( 21.46 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Backport Jason's patch [1] implementing proper hw_random API support for the ath9k hwrng. The original code invoked add_hwgenerator_randomness to directly feed ADC entropy into the system pool. Since [...] Content analysis details: (-0.2 points, 5.0 required) 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:530 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 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 [rsalvaterra[at]gmail.com] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 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.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org Backport Jason's patch [1] implementing proper hw_random API support for the ath9k hwrng. The original code invoked add_hwgenerator_randomness to directly feed ADC entropy into the system pool. Since add_hwgenerator_randomness blocks until the system is low on entropy, if there is another hw_random API-based hwrng selected in the system, both of them will race to feed the entropy pool on wake-up. This unpredictability of the entropy source is a potential security risk. By turning supported ath9k devices into proper hwrngs, we allow users to choose one, if any, as they see fit. [1] https://lore.kernel.org/all/20220216113323.53332-1-Jason@zx2c4.com/ Acked-by: Petr Štetiar Signed-off-by: Rui Salvaterra --- v2: make it actually build when CONFIG_ATH9K_HWRNG is selected (and rng-core.ko isn't built-in). package/kernel/mac80211/ath.mk | 2 +- ...dom-API-instead-of-directly-dumping-.patch | 145 ++++++++++++++++++ 2 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 package/kernel/mac80211/patches/ath9k/020-v5.18-ath9k-use-hw_random-API-instead-of-directly-dumping-.patch diff --git a/package/kernel/mac80211/ath.mk b/package/kernel/mac80211/ath.mk index 50b1eed9c8..7c1f4873a8 100644 --- a/package/kernel/mac80211/ath.mk +++ b/package/kernel/mac80211/ath.mk @@ -201,7 +201,7 @@ define KernelPackage/ath9k $(call KernelPackage/mac80211/Default) TITLE:=Atheros 802.11n PCI wireless cards support URL:=https://wireless.wiki.kernel.org/en/users/drivers/ath9k - DEPENDS+= @PCI_SUPPORT||TARGET_ath79 +kmod-ath9k-common + DEPENDS+= @PCI_SUPPORT||TARGET_ath79 +ATH9K_HWRNG:kmod-random-core +kmod-ath9k-common FILES:= \ $(PKG_BUILD_DIR)/drivers/net/wireless/ath/ath9k/ath9k.ko AUTOLOAD:=$(call AutoProbe,ath9k) diff --git a/package/kernel/mac80211/patches/ath9k/020-v5.18-ath9k-use-hw_random-API-instead-of-directly-dumping-.patch b/package/kernel/mac80211/patches/ath9k/020-v5.18-ath9k-use-hw_random-API-instead-of-directly-dumping-.patch new file mode 100644 index 0000000000..e6fcba8a9a --- /dev/null +++ b/package/kernel/mac80211/patches/ath9k/020-v5.18-ath9k-use-hw_random-API-instead-of-directly-dumping-.patch @@ -0,0 +1,145 @@ +From 26a3c8256d1940dbaf0449f0cc4f4c94e321e054 Mon Sep 17 00:00:00 2001 +From: "Jason A. Donenfeld" +Date: Wed, 16 Feb 2022 12:33:23 +0100 +Subject: [PATCH] ath9k: use hw_random API instead of directly dumping into + random.c +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Hardware random number generators are supposed to use the hw_random +framework. This commit turns ath9k's kthread-based design into a proper +hw_random driver. + +Cc: Toke Høiland-Jørgensen +Cc: Kalle Valo +Cc: Rui Salvaterra +Cc: Dominik Brodowski +Cc: Herbert Xu +Signed-off-by: Jason A. Donenfeld +Signed-off-by: Rui Salvaterra +--- + drivers/net/wireless/ath/ath9k/ath9k.h | 3 +- + drivers/net/wireless/ath/ath9k/rng.c | 72 +++++++++++--------------- + 2 files changed, 33 insertions(+), 42 deletions(-) + +--- a/drivers/net/wireless/ath/ath9k/ath9k.h ++++ b/drivers/net/wireless/ath/ath9k/ath9k.h +@@ -1071,8 +1071,9 @@ struct ath_softc { + #endif + + #ifdef CPTCFG_ATH9K_HWRNG ++ struct hwrng rng_ops; + u32 rng_last; +- struct task_struct *rng_task; ++ char rng_name[sizeof("ath9k_65535")]; + #endif + }; + +--- a/drivers/net/wireless/ath/ath9k/rng.c ++++ b/drivers/net/wireless/ath/ath9k/rng.c +@@ -21,11 +21,6 @@ + #include "hw.h" + #include "ar9003_phy.h" + +-#define ATH9K_RNG_BUF_SIZE 320 +-#define ATH9K_RNG_ENTROPY(x) (((x) * 8 * 10) >> 5) /* quality: 10/32 */ +- +-static DECLARE_WAIT_QUEUE_HEAD(rng_queue); +- + static int ath9k_rng_data_read(struct ath_softc *sc, u32 *buf, u32 buf_size) + { + int i, j; +@@ -71,61 +66,56 @@ static u32 ath9k_rng_delay_get(u32 fail_ + return delay; + } + +-static int ath9k_rng_kthread(void *data) ++static int ath9k_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) + { +- int bytes_read; +- struct ath_softc *sc = data; +- u32 *rng_buf; +- u32 delay, fail_stats = 0; +- +- rng_buf = kmalloc_array(ATH9K_RNG_BUF_SIZE, sizeof(u32), GFP_KERNEL); +- if (!rng_buf) +- goto out; +- +- while (!kthread_should_stop()) { +- bytes_read = ath9k_rng_data_read(sc, rng_buf, +- ATH9K_RNG_BUF_SIZE); +- if (unlikely(!bytes_read)) { +- delay = ath9k_rng_delay_get(++fail_stats); +- wait_event_interruptible_timeout(rng_queue, +- kthread_should_stop(), +- msecs_to_jiffies(delay)); +- continue; ++ struct ath_softc *sc = container_of(rng, struct ath_softc, rng_ops); ++ u32 fail_stats = 0, word; ++ int bytes_read = 0; ++ ++ for (;;) { ++ if (max & ~3UL) ++ bytes_read = ath9k_rng_data_read(sc, buf, max >> 2); ++ if ((max & 3UL) && ath9k_rng_data_read(sc, &word, 1)) { ++ memcpy(buf + bytes_read, &word, max & 3UL); ++ bytes_read += max & 3UL; ++ memzero_explicit(&word, sizeof(word)); + } ++ if (!wait || !max || likely(bytes_read) || fail_stats > 110) ++ break; + +- fail_stats = 0; +- +- /* sleep until entropy bits under write_wakeup_threshold */ +- add_hwgenerator_randomness((void *)rng_buf, bytes_read, +- ATH9K_RNG_ENTROPY(bytes_read)); ++ msleep_interruptible(ath9k_rng_delay_get(++fail_stats)); + } + +- kfree(rng_buf); +-out: +- sc->rng_task = NULL; +- +- return 0; ++ if (wait && !bytes_read && max) ++ bytes_read = -EIO; ++ return bytes_read; + } + + void ath9k_rng_start(struct ath_softc *sc) + { ++ static atomic_t serial = ATOMIC_INIT(0); + struct ath_hw *ah = sc->sc_ah; + +- if (sc->rng_task) ++ if (sc->rng_ops.read) + return; + + if (!AR_SREV_9300_20_OR_LATER(ah)) + return; + +- sc->rng_task = kthread_run(ath9k_rng_kthread, sc, "ath9k-hwrng"); +- if (IS_ERR(sc->rng_task)) +- sc->rng_task = NULL; ++ snprintf(sc->rng_name, sizeof(sc->rng_name), "ath9k_%u", ++ (atomic_inc_return(&serial) - 1) & U16_MAX); ++ sc->rng_ops.name = sc->rng_name; ++ sc->rng_ops.read = ath9k_rng_read; ++ sc->rng_ops.quality = 320; ++ ++ if (devm_hwrng_register(sc->dev, &sc->rng_ops)) ++ sc->rng_ops.read = NULL; + } + + void ath9k_rng_stop(struct ath_softc *sc) + { +- if (sc->rng_task) { +- kthread_stop(sc->rng_task); +- sc->rng_task = NULL; ++ if (sc->rng_ops.read) { ++ devm_hwrng_unregister(sc->dev, &sc->rng_ops); ++ sc->rng_ops.read = NULL; + } + }