Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp759013ybp; Fri, 11 Oct 2019 04:02:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqyBgS6PLgLvVUgiSpu0bDYLiSkf//T7GMEbPoyuOCXLfJFnXD0NT1Byyus/getFPb9yvrT2 X-Received: by 2002:a17:906:35d6:: with SMTP id p22mr13093633ejb.274.1570791764772; Fri, 11 Oct 2019 04:02:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570791764; cv=none; d=google.com; s=arc-20160816; b=W5/voJcV5W87615XqDRggFPmxqdke479Mc42bGNej3Q5jXhG7qVflrAmpECXTNOsD/ eAQAlaJCy8pJ6NV/D8/tVkfM66w0zhsm5j18W2zhh5hSq1zodlaJUfujdYjw8G6qhTIC 3vjG9KF+bqmLDGk8hby5KJJnllZTXtSrw3wDa09KjU6l6h9MXthMucnhSOXeFLpZsuws aHgkJkEVfCCAuEKHHGh3xI/aQpHpLI6byfBYAHBTN5hOEd+hACtZi8j0HEv3ZtEnqp6F XvDdgFrnYb7Y0zEOgZPKKLuexs23AT/T/3eB04FM/Ki/PXuawP6fBGxpErDxtj2jkXiG 6udA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:from:references:cc:to:subject; bh=ilIH+S44VAenSA/gCY0swn9f9AlkAyINd/KVDysBVKs=; b=gKjIf7ZW8uYvtpN1csOvXBnzDfH7Zc5IS9nRf5bq5kBO9OVctWSZZKKSarePNKIc7e sDr2fv+VLOZ33BDS12QkSac+utu3k7Blus46B2ki8mODCSN657+IruAMp8+x8EzcbzIk Wt1GFXsmqIlKA0tY66biCyunViYiN371tQw+KHBObo5rqAwKhe9MzCITLTElgRCusrDW I6ggc0svvRkVyqAGkoqmxPl+bEkqGPxaLNHK0sBEh48+UrB78VmRXzAvt4mO/a1mALqq WC6U9Asz9AqxUQ3Wo7j7LE1X6LKM/KvotwBxawjBzjxbR4HLbGt36hKO508aVRy00swx Ln9Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b14si297493eju.187.2019.10.11.04.02.20; Fri, 11 Oct 2019 04:02:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727891AbfJKLA7 (ORCPT + 99 others); Fri, 11 Oct 2019 07:00:59 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37907 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726885AbfJKLA7 (ORCPT ); Fri, 11 Oct 2019 07:00:59 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 590E010C092E; Fri, 11 Oct 2019 11:00:58 +0000 (UTC) Received: from [10.36.116.143] (ovpn-116-143.ams2.redhat.com [10.36.116.143]) by smtp.corp.redhat.com (Postfix) with ESMTP id 38E845C1B2; Fri, 11 Oct 2019 11:00:56 +0000 (UTC) Subject: Re: [PATCH] hw_random: move add_early_randomness() out of rng_mutex To: Marek Szyprowski , linux-kernel@vger.kernel.org Cc: linux-crypto@vger.kernel.org, Herbert Xu , Matt Mackall , 'Linux Samsung SOC' References: <20190912133022.14870-1-lvivier@redhat.com> <5aec0832-e18f-a557-3614-33970bc4ff1f@samsung.com> From: Laurent Vivier Autocrypt: addr=lvivier@redhat.com; prefer-encrypt=mutual; keydata= mQINBFYFJhkBEAC2me7w2+RizYOKZM+vZCx69GTewOwqzHrrHSG07MUAxJ6AY29/+HYf6EY2 WoeuLWDmXE7A3oJoIsRecD6BXHTb0OYS20lS608anr3B0xn5g0BX7es9Mw+hV/pL+63EOCVm SUVTEQwbGQN62guOKnJJJfphbbv82glIC/Ei4Ky8BwZkUuXd7d5NFJKC9/GDrbWdj75cDNQx UZ9XXbXEKY9MHX83Uy7JFoiFDMOVHn55HnncflUncO0zDzY7CxFeQFwYRbsCXOUL9yBtqLer Ky8/yjBskIlNrp0uQSt9LMoMsdSjYLYhvk1StsNPg74+s4u0Q6z45+l8RAsgLw5OLtTa+ePM JyS7OIGNYxAX6eZk1+91a6tnqfyPcMbduxyBaYXn94HUG162BeuyBkbNoIDkB7pCByed1A7q q9/FbuTDwgVGVLYthYSfTtN0Y60OgNkWCMtFwKxRaXt1WFA5ceqinN/XkgA+vf2Ch72zBkJL RBIhfOPFv5f2Hkkj0MvsUXpOWaOjatiu0fpPo6Hw14UEpywke1zN4NKubApQOlNKZZC4hu6/ 8pv2t4HRi7s0K88jQYBRPObjrN5+owtI51xMaYzvPitHQ2053LmgsOdN9EKOqZeHAYG2SmRW LOxYWKX14YkZI5j/TXfKlTpwSMvXho+efN4kgFvFmP6WT+tPnwARAQABtCNMYXVyZW50IFZp dmllciA8bHZpdmllckByZWRoYXQuY29tPokCOAQTAQIAIgUCVgVQgAIbAwYLCQgHAwIGFQgC CQoLBBYCAwECHgECF4AACgkQ8ww4vT8vvjwpgg//fSGy0Rs/t8cPFuzoY1cex4limJQfReLr SJXCANg9NOWy/bFK5wunj+h/RCFxIFhZcyXveurkBwYikDPUrBoBRoOJ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AlYFJhkCGwwACgkQ8ww4vT8vvjwg9w//VQrcnVg3 TsjEybxDEUBm8dBmnKqcnTBFmxN5FFtIWlEuY8+YMiWRykd8Ln9RJ/98/ghABHz9TN8TRo2b 6WimV64FmlVn17Ri6FgFU3xNt9TTEChqAcNg88eYryKsYpFwegGpwUlaUaaGh1m9OrTzcQy+ klVfZWaVJ9Nw0keoGRGb8j4XjVpL8+2xOhXKrM1fzzb8JtAuSbuzZSQPDwQEI5CKKxp7zf76 J21YeRrEW4WDznPyVcDTa+tz++q2S/BpP4W98bXCBIuQgs2m+OflERv5c3Ojldp04/S4NEjX EYRWdiCxN7ca5iPml5gLtuvhJMSy36glU6IW9kn30IWuSoBpTkgV7rLUEhh9Ms82VWW/h2Tx L8enfx40PrfbDtWwqRID3WY8jLrjKfTdR3LW8BnUDNkG+c4FzvvGUs8AvuqxxyHbXAfDx9o/ jXfPHVRmJVhSmd+hC3mcQ+4iX5bBPBPMoDqSoLt5w9GoQQ6gDVP2ZjTWqwSRMLzNr37rJjZ1 pt0DCMMTbiYIUcrhX8eveCJtY7NGWNyxFCRkhxRuGcpwPmRVDwOl39MB3iTsRighiMnijkbL XiKoJ5CDVvX5yicNqYJPKh5MFXN1bvsBkmYiStMRbrD0HoY1kx5/VozBtc70OU0EB8Wrv9hZ D+Ofp0T3KOr1RUHvCZoLURfFhSQ= Message-ID: Date: Fri, 11 Oct 2019 13:00:55 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.1.0 MIME-Version: 1.0 In-Reply-To: <5aec0832-e18f-a557-3614-33970bc4ff1f@samsung.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mx1.redhat.com [10.5.110.66]); Fri, 11 Oct 2019 11:00:58 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/10/2019 10:45, Marek Szyprowski wrote: > Hi Laurent, > > On 12.09.2019 15:30, Laurent Vivier wrote: >> add_early_randomness() is called every time a new rng backend is added >> and every time it is set as the current rng provider. >> >> add_early_randomness() is called from functions locking rng_mutex, >> and if it hangs all the hw_random framework hangs: we can't read sysfs, >> add or remove a backend. >> >> This patch move add_early_randomness() out of the rng_mutex zone. >> It only needs the reading_mutex. >> >> Signed-off-by: Laurent Vivier > > This patch landed in today's linux-next and causes the following warning > on ARM 32bit Exynos5420-based Chromebook Peach-Pit board: > > tpm_i2c_infineon 9-0020: 1.2 TPM (device-id 0x1A) > ------------[ cut here ]------------ > WARNING: CPU: 3 PID: 1 at lib/refcount.c:156 hwrng_register+0x13c/0x1b4 > refcount_t: increment on 0; use-after-free. > Modules linked in: > CPU: 3 PID: 1 Comm: swapper/0 Not tainted 5.4.0-rc1-00061-gdaae28debcb0 > #6714 > Hardware name: SAMSUNG EXYNOS (Flattened Device Tree) > [] (unwind_backtrace) from [] (show_stack+0x10/0x14) > [] (show_stack) from [] (dump_stack+0xa8/0xd4) > [] (dump_stack) from [] (__warn+0xf4/0x10c) > [] (__warn) from [] (warn_slowpath_fmt+0x74/0xb8) > [] (warn_slowpath_fmt) from [] > (hwrng_register+0x13c/0x1b4) This can happen if hwrng_init() has not been called for rng, that is called by set_current_rng(). It appears with this patch because I have introduced the kref_get() before the call of add_early_randomness() when the rng device is new but is not set as the current one. So add_early_randomness() was called on an unitialized device (it was already the case before) I wanted to take the ref before releasing the mutex to avoid race condition, but if the new rng device is not the new current_rng one, we don't need that as the ref is only used with current_rng device. I'm going to rework this patch. Thanks, Laurent