Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1630224pxb; Thu, 4 Mar 2021 16:57:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJzgmKknXDm3vJVfRr8VkHy30zyISsQS13U6U+ncfbYokjVZ34LiSgAPpauUJSfLt0NOq13+ X-Received: by 2002:a6b:d80d:: with SMTP id y13mr5989547iob.75.1614905877126; Thu, 04 Mar 2021 16:57:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614905877; cv=none; d=google.com; s=arc-20160816; b=Wa+aeK34eV5O/OVq1rCkRoqdUiEkzOYfkugLVdZ5mfrNahQpZapGdzfkYanq8ViCki p/xhZ8shcgrsqnsRNicbX+Qtw4/ywmZ3H2Bxm+cfUjklzfbsbnhxBPArxGOQg5y2mD8i VinYQDclQzJy2jb8QRP/ta8M8g/1t9sbMDFQ3b8VUjJBTInsoP/KF432BzLY8u0MIrbH IPPcqVpSX71u5eWDYGbmE+SDAzt/cWE/9K1Q0KjVkrHtgKnrvXHfdW1arnGM+HSPO/vY 8AmdGNH+RIUHT0ZZl6BN04CjvUypGCO+x/OPhtNZhBOdXIRB731hEvgUC2hRgdtP2DzA kr5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=J50JP3psap6N5U8PJK0VaCpx/DyKv1Z4rwk0hsyasT8=; b=c+WXe8mZbyTde2hX6UkzLnD1V39v5X6xqN6fICAllT/WDkVB4EOHKxthzEfCA2J152 u52jXiQGPFXIiQEx0lC1U0AsLrx3xm1HKbJcPprkLJn/Ovv4DFUvxeJtsxk4073dEOmb 4nlWJgNyZ3PlZVuslkT+/ccql0yq0qbWEy4ovYsUxO6hqPiz14nugWzsDJliD4l6gT6a 6qsd+cS3lRbYuayLHzOiHTOvCNw0jefjMAyIIcDBlNyhO/WEPK7gDD56h4hzsNGtD1u8 l+R7wuqsCtU/LlZw/fdvNJzc8S3rZbskOmTFCLBUqo+mNgeeR2F0Eucavjkz501LkrHz Getw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ShyKNcNe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e11si862191iod.98.2021.03.04.16.57.44; Thu, 04 Mar 2021 16:57:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ShyKNcNe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238634AbhCDVJP (ORCPT + 99 others); Thu, 4 Mar 2021 16:09:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238317AbhCDVJK (ORCPT ); Thu, 4 Mar 2021 16:09:10 -0500 Received: from mail-oo1-xc33.google.com (mail-oo1-xc33.google.com [IPv6:2607:f8b0:4864:20::c33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C35AC061760 for ; Thu, 4 Mar 2021 13:08:30 -0800 (PST) Received: by mail-oo1-xc33.google.com with SMTP id l5so6939754ooj.7 for ; Thu, 04 Mar 2021 13:08:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=J50JP3psap6N5U8PJK0VaCpx/DyKv1Z4rwk0hsyasT8=; b=ShyKNcNe0s0B6TrwHg0Wrd8VbmCyL1Xz57elLS6JF0EdStg0/ngISbRwFn1CLIPeLQ YVvSUGEXyQEVCph+woW0FUxzpLDFcTabpL6xX2i6/WRjXhXFGlDskORQ9B0mQPxYtg2t YBOAb+kBsnKPty8uVhU13LF9HCQXVeE1A3sfx5Rd0lPsTBk5mKU6u9mDI6SUQaYc3hev ux40KnR+33BvAXJuy2pCJQPqGqlQnNh22/SGf8FykKSMx5xvrUF5toRcH3XbkFQ4C94h Oa3J5nicZhpc/BfNjgNlE84ecbz4LPkHu1RRHzgVIMGV7bkFr9233JD85rsrbV1cRosI qROw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=J50JP3psap6N5U8PJK0VaCpx/DyKv1Z4rwk0hsyasT8=; b=noFycInWpBJJOzlY3RxiIdnMBHllMmCfYm4kO35U7Pf9p5oY8+5R2EGXaYYKAlwGky 3FYEVVZPzt3e88QsbXDD0YI6ulk9tz/rUtxydqXuNjoXqBpPmCvicZtuttT54Aza9met Q/u2v8wUVdmryawAVteWKn1beAmQZ2zW8GlaAqFvwbnefZ3GE38NSvQuVLW/MT+h02ve zVzsFgR/APwqzEd8frbxfaVO1ufLx5cQFnsGSwgNGX+bsA1DmcYi21Rc00jWsZVRBUIX QYW8wD5g0VMkZ6D3IOnJKJvM44P0NWOv4b71vK0b7lGwF+ENTKB8TSMjriZTq6vDU+W2 AxgA== X-Gm-Message-State: AOAM531qDWrxh/ezUH4u9hVLrSj6SF5HkbVAWowjV1CgsThtn+xofEv1 uNa88kmRMOlhDFjKZqGGAMz2y83NpFQx33YzyuM= X-Received: by 2002:a4a:9019:: with SMTP id i25mr4896429oog.8.1614892108728; Thu, 04 Mar 2021 13:08:28 -0800 (PST) Received: from mail-oi1-f177.google.com (mail-oi1-f177.google.com. [209.85.167.177]) by smtp.gmail.com with ESMTPSA id n11sm66378oij.51.2021.03.04.13.08.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 04 Mar 2021 13:08:28 -0800 (PST) Received: by mail-oi1-f177.google.com with SMTP id f3so31663539oiw.13; Thu, 04 Mar 2021 13:08:28 -0800 (PST) X-Received: by 2002:aca:4fd3:: with SMTP id d202mr4233275oib.11.1614892107343; Thu, 04 Mar 2021 13:08:27 -0800 (PST) MIME-Version: 1.0 References: <20210303135500.24673-1-alex.bennee@linaro.org> <20210303135500.24673-3-alex.bennee@linaro.org> In-Reply-To: <20210303135500.24673-3-alex.bennee@linaro.org> From: Arnd Bergmann Date: Thu, 4 Mar 2021 22:08:11 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC PATCH 2/5] char: rpmb: provide a user space interface To: =?UTF-8?B?QWxleCBCZW5uw6ll?= Cc: "linux-kernel@vger.kernel.org" , Maxim Uvarov , Joakim Bech , Ilias Apalodimas , ruchika.gupta@linaro.org, "Winkler, Tomas" , yang.huang@intel.com, bing.zhu@intel.com, Matti.Moell@opensynergy.com, hmo@opensynergy.com, linux-mmc , linux-scsi , linux-nvme@vger.kernel.org, Ulf Hansson , Linus Walleij , Arnd Bergmann , Alexander Usyskin , Avri Altman Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 3, 2021 at 2:54 PM Alex Benn=C3=A9e wr= ote: > > + /* the rpmb is single open! */ > + if (test_and_set_bit(RPMB_DEV_OPEN, &rdev->status)) > + return -EBUSY; open counters on device nodes are fundamentally broken, because they do not stop you from using dup() or sharing the file descriptor across a fork. Just remove this. > +static long rpmb_ioctl_ver_cmd(struct rpmb_dev *rdev, > + struct rpmb_ioc_ver_cmd __user *ptr) > +{ > + struct rpmb_ioc_ver_cmd ver =3D { > + .api_version =3D RPMB_API_VERSION, > + }; > + > + return copy_to_user(ptr, &ver, sizeof(ver)) ? -EFAULT : 0; > +} Similarly, API versions are fundamentally flawed, as the kernel requires us to keep compatibility with existing user space. Remove this as well. > +static long rpmb_ioctl_cap_cmd(struct rpmb_dev *rdev, > + struct rpmb_ioc_cap_cmd __user *ptr) > +{ > + struct rpmb_ioc_cap_cmd cap; Better do a memset() here to ensure this does not leak kernel stack data to user space. > +static const struct file_operations rpmb_fops =3D { > + .open =3D rpmb_open, > + .release =3D rpmb_release, > + .unlocked_ioctl =3D rpmb_ioctl, > + .owner =3D THIS_MODULE, > + .llseek =3D noop_llseek, > +}; Add .compat_ioctl =3D compat_ptr_ioctl to make it work for 32-bit user space on 64-bit kernels. > @@ -0,0 +1,17 @@ > +/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */ > +/* > + * Copyright (C) 2015-2018 Intel Corp. All rights reserved > + */ > +#ifdef CONFIG_RPMB_INTF_DEV > +int __init rpmb_cdev_init(void); > +void __exit rpmb_cdev_exit(void); > +void rpmb_cdev_prepare(struct rpmb_dev *rdev); > +void rpmb_cdev_add(struct rpmb_dev *rdev); > +void rpmb_cdev_del(struct rpmb_dev *rdev); > +#else > +static inline int __init rpmb_cdev_init(void) { return 0; } I don't think it's necessary to make the user interface optional, I'd just always provide these. > > +#define RPMB_API_VERSION 0x80000001 Remove this > + */ > +struct rpmb_ioc_ver_cmd { > + __u32 api_version; > +} __packed; And this > + > +enum rpmb_auth_method { > + RPMB_HMAC_ALGO_SHA_256 =3D 0, > +}; > + > +/** > + * struct rpmb_ioc_cap_cmd - rpmb capabilities > + * > + * @target: rpmb target/region within RPMB partition. > + * @capacity: storage capacity (in units of 128K) > + * @block_size: storage data block size (in units of 256B) > + * @wr_cnt_max: maximal number of block that can be written in a single = request. > + * @rd_cnt_max: maximal number of block that can be read in a single req= uest. > + * @auth_method: authentication method: currently always HMAC_SHA_256 > + * @reserved: reserved to align to 4 bytes. > + */ > +struct rpmb_ioc_cap_cmd { > + __u16 target; > + __u16 capacity; > + __u16 block_size; > + __u16 wr_cnt_max; > + __u16 rd_cnt_max; > + __u16 auth_method; > + __u16 reserved; > +} __attribute__((packed)); > + Remove the packed attribute, it does not change the structure layout but just makes it less efficient to access on architectures that turn unaligned loads and stores into byte accesses. > +/** > + * struct rpmb_ioc_blocks_cmd - read/write blocks to/from RPMB > + * > + * @keyid: key_serial_t of key to use > + * @addr: index into device (units of 256B blocks) > + * @count: number of 256B blocks > + * @data: pointer to data to write/read > + */ > +struct rpmb_ioc_blocks_cmd { > + __s32 key; /* key_serial_t */ > + __u32 addr; > + __u32 count; > + __u8 __user *data; > +} __attribute__((packed)); ioctl structures should generally not have pointers in them. If this can be= done one block at a time, you can have the 256 bytes as part of the structure. This probably needs a redesign anyway based on Tomas' feedback though. If you end up needing a pointer, use a __u64 member with u64_to_user_ptr() as described in Documentation/driver-api/ioctl.rst > +#define RPMB_IOC_VER_CMD _IOR(0xB8, 80, struct rpmb_ioc_ver_cmd) > +#define RPMB_IOC_CAP_CMD _IOR(0xB8, 81, struct rpmb_ioc_cap_cmd) > +#define RPMB_IOC_PKEY_CMD _IOW(0xB8, 82, key_serial_t) > +#define RPMB_IOC_COUNTER_CMD _IOR(0xB8, 84, int) > +#define RPMB_IOC_WBLOCKS_CMD _IOW(0xB8, 85, struct rpmb_ioc_blocks_cmd) > +#define RPMB_IOC_RBLOCKS_CMD _IOR(0xB8, 86, struct rpmb_ioc_blocks_cmd) The last one should be _IOWR(), not _IOR(), since you write the metadata an= d read the data. Arnd