From mboxrd@z Thu Jan 1 00:00:00 1970 From: LABBE Corentin Subject: Re: [PATCH] Add Ingenic JZ4780 hardware RNG driver Date: Thu, 18 Aug 2016 13:53:00 +0200 Message-ID: <20160818115300.GA6621@Red> References: <1471448151-20850-1-git-send-email-prasannatsmkumar@gmail.com> <92a00062-9a87-0053-2c99-17bd1a304a4a@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: mpm-VDJrAJ4Gl5ZBDgjK7y7TUQ@public.gmane.org, Herbert Xu , robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, mark.rutland-5wv7dgnIgG8@public.gmane.org, Ralf Baechle , davem-fT/PcQaiUtIeIZ0/mPfg9Q@public.gmane.org, geert-Td1EMuHUCqxL1ZNQvxDV9g@public.gmane.org, Andrew Morton , Greg KH , mchehab-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, Guenter Roeck , boris.brezillon-wi1+55ScJUtKEb57/3fJTNBPR1lH4CV8@public.gmane.org, harvey.hunt-1AXoQHu6uovQT0dZR+AlfA@public.gmane.org, alex.smith-1AXoQHu6uovQT0dZR+AlfA@public.gmane.org, Daniel Thompson , Lee Jones , Florian Fainelli , kieran-7hKh/agyDeatmTQ+vhA3Yw@public.gmane.org, Krzysztof Kozlowski , joshua.henderson-UWL1GkI3JZL3oGB3hsPCZA@public.gmane.org, yendapally.reddy-dY08KVG/lbpWk0Htik3J/w@public.gmane.org, narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org, wangkefeng.wang-hv44wF8Li93QT0dZR+AlfA@public.gmane.org, Christian Lamparter , =?iso-8859-1?Q?=C1lvaro_Fern=E1ndez?= Rojas , Linus Walleij , pankaj.dev-qxv4g6HH51o@public.gmane.org, Mathie To: PrasannaKumar Muralidharan Return-path: Content-Disposition: inline In-Reply-To: Sender: devicetree-owner-u79uwXL29TY76Z2rM5mHXA@public.gmane.org List-Id: linux-crypto.vger.kernel.org On Thu, Aug 18, 2016 at 10:44:18AM +0530, PrasannaKumar Muralidharan wrote: > >> +static int jz4780_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) > >> +{ > >> + struct jz4780_rng *jz4780_rng = container_of(rng, struct jz4780_rng, > >> + rng); > >> + u32 *data = buf; > >> + *data = jz4780_rng_readl(jz4780_rng, REG_RNG_DATA); > >> + return 4; > >> +} > > > > If max is less than 4, its bad > > Data will be 4 bytes. > No, according to comment in include/linux/hw_random.h "drivers can fill up to max bytes of data" So you cannot write more than max bytes without risking buffer overflow. And if max > 4, hwrng client need to recall your read function. The better example I found is tpm_get_random() in drivers/char/tpm/tpm-interface.c for handling both problem. Regards -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org More majordomo info at http://vger.kernel.org/majordomo-info.html From mboxrd@z Thu Jan 1 00:00:00 1970 Received: with ECARTIS (v1.0.0; list linux-mips); Thu, 18 Aug 2016 13:53:15 +0200 (CEST) Received: from mail-wm0-f67.google.com ([74.125.82.67]:34718 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by eddie.linux-mips.org with ESMTP id S23993243AbcHRLxJ3bonp (ORCPT ); Thu, 18 Aug 2016 13:53:09 +0200 Received: by mail-wm0-f67.google.com with SMTP id q128so5285485wma.1; Thu, 18 Aug 2016 04:53:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=AM+i8oo+lmmduyjfJ5e32IHmjZSMoFJLHpQZyB8BMuI=; b=Pb4xOMeACltZMunLKaDEuVNg0iAm/IbZIhD1RAkE3ngkE+tfSQ0NM1ce7s22t9NlUS 5u84PG0holFAcfD7AsVgPXDBG4tEXwIvKcVlHmdJnugJJ+QzkQqtcezHZ18esBn6+5Qh kdxNu3DOCXIawcHHOld+deHWcCGu2l28/IXD7B+4bwma1RjjrkdQUA27TS9NkZhQvvAq XOPvMPZ0OfNK4WiuyOkVaMGiH7kIzXELdoteU3+XuEd2EjqGn5E04jBHi4RsJk9tJX2h Uwt8T0J+wcU88w1qe8xqS5QNeTxN/xuspisI5sjkuyLngUlwNyxPTSNmHGR2MAT523hd dApA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=AM+i8oo+lmmduyjfJ5e32IHmjZSMoFJLHpQZyB8BMuI=; b=ZiayNbRRydoCFw7M+/mLI20M5mkyhUdDqZT4vnr0699xFwh5cpa08JrpjdU0Dgumy/ 9wsQK/mmfbvcl4klp/rqRy1QlRgsrW6BrISbd0/JM6xvFnieT507yON7zwXhhRnPTYtl hEqkKA2P39o5VnTzzMVV1f7PKGZ5N9MRmlKj5eos3/aQbJUKq1A1IV8TyTHdBy+TFVG2 9nBbZvwTGX/U8N3MbMFjJNi+/9InBSctZptqLDYmYH+KEFQ9Ssp68nBhNgOYiZm9ARGS NhGz5qiw3/DQ6MSCfza6yYCAGjxd4EdCdv7jfLNsW7HlesD2LR1O0FdUN82j89bvqVcL 848w== X-Gm-Message-State: AEkoousnA4A6WqesKs8KD5yfTVjOIkIMaOL14s9Qg/q9tOhvib/UmzOXMUdihsVn5C4Qug== X-Received: by 10.194.100.129 with SMTP id ey1mr2056424wjb.60.1471521184137; Thu, 18 Aug 2016 04:53:04 -0700 (PDT) Received: from Red (ANice-651-1-11-220.w86-203.abo.wanadoo.fr. [86.203.162.220]) by smtp.googlemail.com with ESMTPSA id v134sm31376789wmf.10.2016.08.18.04.53.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Aug 2016 04:53:03 -0700 (PDT) Date: Thu, 18 Aug 2016 13:53:00 +0200 From: LABBE Corentin To: PrasannaKumar Muralidharan Cc: mpm@selenic.com, Herbert Xu , robh+dt@kernel.org, mark.rutland@arm.com, Ralf Baechle , davem@davemloft.net, geert@linux-m68k.org, Andrew Morton , Greg KH , mchehab@kernel.org, Guenter Roeck , boris.brezillon@free-electrons.com, harvey.hunt@imgtec.com, alex.smith@imgtec.com, Daniel Thompson , Lee Jones , Florian Fainelli , kieran@ksquared.org.uk, Krzysztof Kozlowski , joshua.henderson@microchip.com, yendapally.reddy@broadcom.com, narmstrong@baylibre.com, wangkefeng.wang@huawei.com, Christian Lamparter , =?iso-8859-1?Q?=C1lvaro_Fern=E1ndez?= Rojas , Linus Walleij , pankaj.dev@st.com, Mathieu Poirier , linux-crypto@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@linux-mips.org Subject: Re: [PATCH] Add Ingenic JZ4780 hardware RNG driver Message-ID: <20160818115300.GA6621@Red> References: <1471448151-20850-1-git-send-email-prasannatsmkumar@gmail.com> <92a00062-9a87-0053-2c99-17bd1a304a4a@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) Return-Path: X-Envelope-To: <"|/home/ecartis/ecartis -s linux-mips"> (uid 0) X-Orcpt: rfc822;linux-mips@linux-mips.org Original-Recipient: rfc822;linux-mips@linux-mips.org X-archive-position: 54613 X-ecartis-version: Ecartis v1.0.0 Sender: linux-mips-bounce@linux-mips.org Errors-to: linux-mips-bounce@linux-mips.org X-original-sender: clabbe.montjoie@gmail.com Precedence: bulk List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: linux-mips X-List-ID: linux-mips List-subscribe: List-owner: List-post: List-archive: X-list: linux-mips On Thu, Aug 18, 2016 at 10:44:18AM +0530, PrasannaKumar Muralidharan wrote: > >> +static int jz4780_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) > >> +{ > >> + struct jz4780_rng *jz4780_rng = container_of(rng, struct jz4780_rng, > >> + rng); > >> + u32 *data = buf; > >> + *data = jz4780_rng_readl(jz4780_rng, REG_RNG_DATA); > >> + return 4; > >> +} > > > > If max is less than 4, its bad > > Data will be 4 bytes. > No, according to comment in include/linux/hw_random.h "drivers can fill up to max bytes of data" So you cannot write more than max bytes without risking buffer overflow. And if max > 4, hwrng client need to recall your read function. The better example I found is tpm_get_random() in drivers/char/tpm/tpm-interface.c for handling both problem. Regards