Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp3004897rdb; Tue, 6 Feb 2024 04:37:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IFDrPc5OFM9xjOjCMB6o3uLjzsQQMxP9Cm4arGwoTUawBWlo9EfBOH5KQqHZRoag+JOz/nl X-Received: by 2002:a17:906:2c14:b0:a38:4537:2a43 with SMTP id e20-20020a1709062c1400b00a3845372a43mr437585ejh.19.1707223030876; Tue, 06 Feb 2024 04:37:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707223030; cv=pass; d=google.com; s=arc-20160816; b=VZXb30Z321aU1ij7nEFc6chQWL8Ddfs+nR12HUpFHTKR9ojAjx8I687g+4ZrEmfZP9 vYb/t/qHi8JoAp2HdeuiMwrC2xVU5AoyEj1bOLAkOiqI/mPk+S/QHFEBJiTCHsaMFKjs Lfi3yCxbgjHWAvf8lZv8jWjzVIuj20KMj1U163qkDmFR8VZOqFtQrX3uwBfXc47aJ2vg B9Gx1bxyV2C8/6m83JglGCT/McEr1fwFWdwsq1+zv96m3gpUF4wkDsicy/9ssCBXEoD0 SSJcBhPH/+Rl72B3M3pj3R7muYJpRdUT1ZK0w2yj+S5Wt3EpKRRKzvbwkl+hkIBuBw47 zXCg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=gFY/t9ZvMmydoyHyTCZk9/AaeOGiThxuGUqkKGbjuF4=; fh=mYvDcpUzRVW23wkJpCwIJKQreaMvlUAa3C6zSnhzg0A=; b=gpSsp4KBdHRTPc4+844A0Y+2vhuUk6BGBs28CFEixajzkmuEnW+N+mIq7a8sLPxEJ1 DHWx7U0ipIpx8JcHuwUymaU2k/LqH/WgkL3iXo+pJS8PLeslx0JeLMd/4SvyeX8Bt+ZK rATMAbjZh8PFAMEyQJRDluLIsBZBuHEm3rTRnmd1npC497/10U8Ljt4UC5+cdbPkkmSU MWRbotWXXfvhzlaUYIpwfI+0BdU3f2/zD4QSpJtpYZ+iZ0cOvbG9ZqJzGqGQ1SmeesfW GwhkbScNDERNi0wdRm7FeArfSripBIh1dhlbLlMuoddRsxe2BkZ1bUNnCRYSIjCH+pF3 wlIw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AUUrpDz4; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-54957-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54957-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org X-Forwarded-Encrypted: i=1; AJvYcCXBNye4qXxbHxL45yyB0uo5U+I1i6dctiIv0/1tm6P2sDB1sIJmNyuzhyM/URh+VIyJhfKNDwD9Owk39lDVHBBYq49wLsyiugOOcKj2Nw== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id qh25-20020a170906ecb900b00a3837d4d5d7si431456ejb.312.2024.02.06.04.37.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 04:37:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54957-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AUUrpDz4; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-54957-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54957-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id CBE0C1F232E7 for ; Tue, 6 Feb 2024 12:37:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E7C7C5677B; Tue, 6 Feb 2024 12:34:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="AUUrpDz4" Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A01DB13174F for ; Tue, 6 Feb 2024 12:34:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707222846; cv=none; b=s6tGBBQaCXaPifAUIswORetuvErrJWCef5PdCGHfN5p52jIy4Q9LYBfsDgOU3rUKh+uPmY43e+VYJEn6qL94DWOj5VPS0A4trCesmuz742H0RbFdQr9Wyew07A7KQNE8TYvmUNI+d/ReuUfU6fj3QouD3POdGA5c98WOgpEXY54= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707222846; c=relaxed/simple; bh=3sLJ2sl2D1Oah3+ouhgAEdp1SXatnOmvgV7IWT1hVDM=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=FPHxcPCeBEUqYPUyURtDdHEhk+q7vu04iTZFQ2uCTOKpMsOq9fnBWVrNzxnPKFFtDiHokm5kp1TxxQ0gW7XY6P07tUf2NatA5B7pSD558g+P8lYGqRdT1+Z/UBNSfSa3lw1H9FVVVu6+us6fUO+s3zMv1qlqUhgQC23VqPMx1cg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=AUUrpDz4; arc=none smtp.client-ip=209.85.128.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-60482a5a570so2414337b3.0 for ; Tue, 06 Feb 2024 04:34:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1707222843; x=1707827643; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=gFY/t9ZvMmydoyHyTCZk9/AaeOGiThxuGUqkKGbjuF4=; b=AUUrpDz401hxkiKSY1LShgREcyavqVYM5oOgwFLBuu9bdRLK+oWIJbnXP4qL7tRuVc IDyofd7C0bokN7TvTGO+bEzpVM3w8ZBvkDI0xm+HLsdqeniWTDnDg9RYTvmhQ/RrT8fg ThCPHmYqPysVRBDoME6eGEYy739WmwCVneCDin+33YNPEa4ICeNAXGicz1r0LCyqmN+M rhw3PrDe1yNvog07OUG4Py8sEYOUwBnza74LnJY+R0Ho65FGspECWZT0eeQCtZPg9FXb aKMAPXkp0qZKUbbpaQAkkzGYx8c7MiPedQGAznjhoUsAGovg1R1wb8DyEgQN12Q7D2UD VaOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707222843; x=1707827643; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gFY/t9ZvMmydoyHyTCZk9/AaeOGiThxuGUqkKGbjuF4=; b=QHxixasVlqqm2FUAV7Anka2DJLkYtaNRq88bFBimrIFpdybbcyv1BzoNEwft5j321b cr0L9ovmkdxkKxU79vohCEG8LB8YKnWUHXlYrQHWcm/emqDjGXw/WEV3QV/ehjDeF5BY IGAQVE2DAgO8qWB8Af208zfrSXxiSOAT/AzX2Q+FoVcp9XPAq6p8+IOXz5HEyrmHm3JU LIOTGX329F1wbg1sjj0f0Y25s0bV4sDhtxvhPSUWISVG9HiSnu+N8rWrSJTweo1hSvSi ihc03r0iV/L105E+UxaJxcG918c8aliYmcNN/xA3mpVWi1XiF/IVSM1LlWACYUGmj6T9 DvSw== X-Gm-Message-State: AOJu0Yz8TYjUb1SsoOTWemPSJQ4Mfrj8yBBmyg3DnJ0h45tVim9CjVCw okOCft/vSiOYd9OgoDrprtFCBy/FT9sCphD+WnIC1IXJZnNJ3UhlFV7aZkMyZrbuGMvCO2GOsSe CFX2/ke/o0+W6t/phaLrimkan05EN4eciXfYtNA== X-Received: by 2002:a81:e808:0:b0:604:7bab:dacd with SMTP id a8-20020a81e808000000b006047babdacdmr1392728ywm.0.1707222843467; Tue, 06 Feb 2024 04:34:03 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240131174347.510961-1-jens.wiklander@linaro.org> <20240131174347.510961-2-jens.wiklander@linaro.org> In-Reply-To: <20240131174347.510961-2-jens.wiklander@linaro.org> From: Ulf Hansson Date: Tue, 6 Feb 2024 13:33:27 +0100 Message-ID: Subject: Re: [PATCH v2 1/3] rpmb: add Replay Protected Memory Block (RPMB) subsystem To: Jens Wiklander Cc: linux-kernel@vger.kernel.org, linux-mmc@vger.kernel.org, op-tee@lists.trustedfirmware.org, Shyam Saini , Jerome Forissier , Sumit Garg , Ilias Apalodimas , Bart Van Assche , Randy Dunlap , Ard Biesheuvel , Arnd Bergmann , Greg Kroah-Hartman , Tomas Winkler , =?UTF-8?B?QWxleCBCZW5uw6ll?= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, 31 Jan 2024 at 18:44, Jens Wiklander wr= ote: > > A number of storage technologies support a specialised hardware > partition designed to be resistant to replay attacks. The underlying > HW protocols differ but the operations are common. The RPMB partition > cannot be accessed via standard block layer, but by a set of specific > RPMB commands: WRITE, READ, GET_WRITE_COUNTER, and PROGRAM_KEY. Such a > partition provides authenticated and replay protected access, hence > suitable as a secure storage. > > The initial aim of this patch is to provide a simple RPMB Driver which > can be accessed by the optee driver to facilitate early RPMB access to > OP-TEE OS (secure OS) during the boot time. How early do we expect OP-TEE to need RPMB access? The way things work for mmc today, is that the eMMC card gets discovered/probed via a workqueue. The work is punted by the mmc host driver (typically a module-platform-driver), when it has probed successfully. The point is, it looks like we need some kind of probe deferral mechanism too. Whether we want the OP-TEE driver to manage this itself or whether we should let rpmb_dev_find_device() deal with it, I don't know. > > A TEE device driver can claim the RPMB interface, for example, via > class_interface_register() or rpmb_dev_find_device(). The RPMB driver > provides a callback to route RPMB frames to the RPMB device accessible > via rpmb_route_frames(). By looking at the design of the interface, I do like it. It's simple and straightforward. However, I wonder if you considered avoiding using a class-device altogether? Even if it helps with lifecycle problems and the ops-lookup, we really don't need another struct device with a sysfs node, etc. To deal with the lifecycle issue, we could probably just add reference counting for the corresponding struct device that we already have at hand, which represents the eMMC/UFS/NVME card. That together with a simple list that contains the registered rpmb ops. But I may be overlooking something, so perhaps it's more complicated than that? > > The detailed operation of implementing the access is left to the TEE > device driver itself. > > Signed-off-by: Tomas Winkler > Signed-off-by: Alex Benn=C3=A9e > Signed-off-by: Shyam Saini > Signed-off-by: Jens Wiklander > --- > MAINTAINERS | 7 ++ > drivers/misc/Kconfig | 9 ++ > drivers/misc/Makefile | 1 + > drivers/misc/rpmb-core.c | 247 +++++++++++++++++++++++++++++++++++++++ > include/linux/rpmb.h | 184 +++++++++++++++++++++++++++++ > 5 files changed, 448 insertions(+) > create mode 100644 drivers/misc/rpmb-core.c > create mode 100644 include/linux/rpmb.h > > diff --git a/MAINTAINERS b/MAINTAINERS > index 8999497011a2..e83152c42499 100644 > --- a/MAINTAINERS > +++ b/MAINTAINERS > @@ -19012,6 +19012,13 @@ T: git git://linuxtv.org/media_tree.git > F: Documentation/devicetree/bindings/media/allwinner,sun8i-a83t-de2-= rotate.yaml > F: drivers/media/platform/sunxi/sun8i-rotate/ > > +RPMB SUBSYSTEM > +M: Jens Wiklander > +L: linux-kernel@vger.kernel.org > +S: Supported > +F: drivers/misc/rpmb-core.c > +F: include/linux/rpmb.h > + > RPMSG TTY DRIVER > M: Arnaud Pouliquen > L: linux-remoteproc@vger.kernel.org > diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig > index 4fb291f0bf7c..891aa5763666 100644 > --- a/drivers/misc/Kconfig > +++ b/drivers/misc/Kconfig > @@ -104,6 +104,15 @@ config PHANTOM > If you choose to build module, its name will be phantom. If uns= ure, > say N here. > > +config RPMB > + tristate "RPMB partition interface" Should we add a "depends on MMC"? (We can add the other NVME and UFS later on too). > + help > + Unified RPMB unit interface for RPMB capable devices such as eM= MC and > + UFS. Provides interface for in kernel security controllers to a= ccess > + RPMB unit. > + > + If unsure, select N. > + > config TIFM_CORE > tristate "TI Flash Media interface support" > depends on PCI > diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile > index ea6ea5bbbc9c..8af058ad1df4 100644 > --- a/drivers/misc/Makefile > +++ b/drivers/misc/Makefile > @@ -15,6 +15,7 @@ obj-$(CONFIG_LKDTM) +=3D lkdtm/ > obj-$(CONFIG_TIFM_CORE) +=3D tifm_core.o > obj-$(CONFIG_TIFM_7XX1) +=3D tifm_7xx1.o > obj-$(CONFIG_PHANTOM) +=3D phantom.o > +obj-$(CONFIG_RPMB) +=3D rpmb-core.o > obj-$(CONFIG_QCOM_COINCELL) +=3D qcom-coincell.o > obj-$(CONFIG_QCOM_FASTRPC) +=3D fastrpc.o > obj-$(CONFIG_SENSORS_BH1770) +=3D bh1770glc.o > diff --git a/drivers/misc/rpmb-core.c b/drivers/misc/rpmb-core.c > new file mode 100644 > index 000000000000..a3c289051687 > --- /dev/null > +++ b/drivers/misc/rpmb-core.c > @@ -0,0 +1,247 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Copyright(c) 2015 - 2019 Intel Corporation. All rights reserved. > + * Copyright(c) 2021 - 2024 Linaro Ltd. > + */ > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +static DEFINE_IDA(rpmb_ida); > +static DEFINE_MUTEX(rpmb_mutex); > + > +/** > + * rpmb_dev_get() - increase rpmb device ref counter > + * @rdev: rpmb device > + */ > +struct rpmb_dev *rpmb_dev_get(struct rpmb_dev *rdev) > +{ > + if (rdev) > + get_device(&rdev->dev); > + return rdev; > +} > +EXPORT_SYMBOL_GPL(rpmb_dev_get); > + > +/** > + * rpmb_dev_put() - decrease rpmb device ref counter > + * @rdev: rpmb device > + */ > +void rpmb_dev_put(struct rpmb_dev *rdev) > +{ > + if (rdev) > + put_device(&rdev->dev); > +} > +EXPORT_SYMBOL_GPL(rpmb_dev_put); > + > +/** > + * rpmb_route_frames() - route rpmb frames to rpmb device > + * @rdev: rpmb device > + * @req: rpmb request frames > + * @req_len: length of rpmb request frames in bytes > + * @rsp: rpmb response frames > + * @rsp_len: length of rpmb response frames in bytes > + * > + * @return < 0 on failure > + */ > +int rpmb_route_frames(struct rpmb_dev *rdev, u8 *req, > + unsigned int req_len, u8 *rsp, unsigned int rsp_len= ) > +{ > + struct rpmb_frame *frm =3D (struct rpmb_frame *)req; Is there a reason why we are passing an u8 *req, in favor of a "rpmb_frame *frame" directly as the in-parameter? > + u16 req_type; > + bool write; > + > + if (!req || req_len < sizeof(*frm) || !rsp || !rsp_len) > + return -EINVAL; > + > + req_type =3D be16_to_cpu(frm->req_resp); > + switch (req_type) { > + case RPMB_PROGRAM_KEY: > + if (req_len !=3D sizeof(struct rpmb_frame) || > + rsp_len !=3D sizeof(struct rpmb_frame)) > + return -EINVAL; > + write =3D true; > + break; > + case RPMB_GET_WRITE_COUNTER: > + if (req_len !=3D sizeof(struct rpmb_frame) || > + rsp_len !=3D sizeof(struct rpmb_frame)) > + return -EINVAL; > + write =3D false; > + break; > + case RPMB_WRITE_DATA: > + if (req_len % sizeof(struct rpmb_frame) || > + rsp_len !=3D sizeof(struct rpmb_frame)) > + return -EINVAL; > + write =3D true; > + break; > + case RPMB_READ_DATA: > + if (req_len !=3D sizeof(struct rpmb_frame) || > + rsp_len % sizeof(struct rpmb_frame)) > + return -EINVAL; > + write =3D false; > + break; > + default: > + return -EINVAL; > + } > + > + return rdev->ops->route_frames(rdev->dev.parent, write, > + req, req_len, rsp, rsp_len); > +} > +EXPORT_SYMBOL_GPL(rpmb_route_frames); [...] > + > +/** > + * enum rpmb_type - type of underlaying storage technology > + * > + * @RPMB_TYPE_EMMC : emmc (JESD84-B50.1) > + * @RPMB_TYPE_UFS : UFS (JESD220) > + * @RPMB_TYPE_NVME : NVM Express > + */ > +enum rpmb_type { > + RPMB_TYPE_EMMC, > + RPMB_TYPE_UFS, > + RPMB_TYPE_NVME, > +}; In what way do we expect these to be useful? Perhaps we should add some information about this, because currently in the series they seem not to be used. Maybe the OP-TEE driver needs it when extending support to NVME and UFS? [...] Kind regards Uffe