kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@nvidia.com>
To: Nicolin Chen <nicolinc@nvidia.com>
Cc: "Tian, Kevin" <kevin.tian@intel.com>,
	"Liu, Yi L" <yi.l.liu@intel.com>,
	"joro@8bytes.org" <joro@8bytes.org>,
	"alex.williamson@redhat.com" <alex.williamson@redhat.com>,
	"robin.murphy@arm.com" <robin.murphy@arm.com>,
	"baolu.lu@linux.intel.com" <baolu.lu@linux.intel.com>,
	"cohuck@redhat.com" <cohuck@redhat.com>,
	"eric.auger@redhat.com" <eric.auger@redhat.com>,
	"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	"mjrosato@linux.ibm.com" <mjrosato@linux.ibm.com>,
	"chao.p.peng@linux.intel.com" <chao.p.peng@linux.intel.com>,
	"yi.y.sun@linux.intel.com" <yi.y.sun@linux.intel.com>,
	"peterx@redhat.com" <peterx@redhat.com>,
	"jasowang@redhat.com" <jasowang@redhat.com>,
	"shameerali.kolothum.thodi@huawei.com" 
	<shameerali.kolothum.thodi@huawei.com>,
	"lulu@redhat.com" <lulu@redhat.com>,
	"suravee.suthikulpanit@amd.com" <suravee.suthikulpanit@amd.com>,
	"iommu@lists.linux.dev" <iommu@lists.linux.dev>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-kselftest@vger.kernel.org"
	<linux-kselftest@vger.kernel.org>,
	"Duan, Zhenzhong" <zhenzhong.duan@intel.com>
Subject: Re: [PATCH v3 08/17] iommufd: IOMMU_HWPT_ALLOC allocation with user data
Date: Thu, 3 Aug 2023 13:47:50 -0300	[thread overview]
Message-ID: <ZMvaNqskASX3pGuF@nvidia.com> (raw)
In-Reply-To: <ZMr6lJpRDNZsvSm/@Asurada-Nvidia>

On Wed, Aug 02, 2023 at 05:53:40PM -0700, Nicolin Chen wrote:
> On Wed, Aug 02, 2023 at 08:43:12PM -0300, Jason Gunthorpe wrote:
> > On Wed, Aug 02, 2023 at 04:42:10PM -0700, Nicolin Chen wrote:
> > > On Mon, Jul 31, 2023 at 10:16:17AM -0300, Jason Gunthorpe wrote:
> > >  
> > > > > Ideally expanding uAPI structure size should come with new flag bits.
> > > > 
> > > > Flags or some kind of 'zero is the same behavior as a smaller struct'
> > > > scheme.
> > > > 
> > > > This patch is doing the zero option:
> > > > 
> > > >  	__u32 __reserved;
> > > > +	__u32 hwpt_type;
> > > > +	__u32 data_len;
> > > > +	__aligned_u64 data_uptr;
> > > >  };
> > > > 
> > > > hwpt_type == 0 means default type
> > > > data_len == 0 means no data
> > > > data_uptr is ignored (zero is safe)
> > > > 
> > > > So there is no need to change it
> > > 
> > > TEST_LENGTH passing ".size = sizeof(struct _struct) - 1" expects a
> > > -EINVAL error code from "if (ucmd.user_size < op->min_size)" check
> > > in the iommufd_fops_ioctl(). This has been working when min_size is
> > > exactly the size of the structure.
> > > 
> > > When the size of the structure becomes larger than min_size, i.e.
> > > the passing size above is larger than min_size, it bypasses that
> > > min_size sanity and goes down to an ioctl handler with a potential
> > > risk. And actually, the size range can be [min_size, struct_size),
> > > making it harder for us to sanitize with the existing code.
> > > 
> > > I wonder what's the generic way of sanitizing this case? And, it
> > > seems that TEST_LENGTH needs some rework to test min_size only?
> > 
> > Yes, it should technically test using offsetof and a matching set of
> > struct members.
> 
> OK. I copied 3 lines for offsetofend from the kernel and did this:
> --------------------------------------------------------------------------
> diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c
> index 6b075a68b928..a15a475c1243 100644
> --- a/tools/testing/selftests/iommu/iommufd.c
> +++ b/tools/testing/selftests/iommu/iommufd.c
> @@ -86,12 +86,13 @@ TEST_F(iommufd, cmd_fail)
> 
>  TEST_F(iommufd, cmd_length)
>  {
> -#define TEST_LENGTH(_struct, _ioctl)                                     \
> +#define TEST_LENGTH(_struct, _ioctl, _last)                              \
>         {                                                                \
> +               size_t min_size = offsetofend(struct _struct, _last);    \
>                 struct {                                                 \
>                         struct _struct cmd;                              \
>                         uint8_t extra;                                   \
> -               } cmd = { .cmd = { .size = sizeof(struct _struct) - 1 }, \
> +               } cmd = { .cmd = { .size = min_size - 1 },               \
>                           .extra = UINT8_MAX };                          \
>                 int old_errno;                                           \
>                 int rc;                                                  \
> --------------------------------------------------------------------------
> 
> Any misaligned size within the range of [min_size, struct_size) still
> doesn't have a coverage though. Is this something that we have to let
> it fail with a potential risk?

It looks about right, I didn't try to test all the permutations, it
could be done but I'm not sure it has value.

Jason

  reply	other threads:[~2023-08-03 16:48 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-24 11:03 [PATCH v3 00/17] iommufd: Add nesting infrastructure Yi Liu
2023-07-24 11:03 ` [PATCH v3 01/17] iommu: Add new iommu op to create domains owned by userspace Yi Liu
2023-07-28  9:37   ` Tian, Kevin
2023-07-28 16:56     ` Jason Gunthorpe
2023-07-31 12:44       ` Liu, Yi L
2023-07-31 13:19         ` Jason Gunthorpe
2023-08-03  2:28       ` Nicolin Chen
2023-07-24 11:03 ` [PATCH v3 02/17] iommu: Add nested domain support Yi Liu
2023-07-28  9:38   ` Tian, Kevin
2023-07-28 16:59   ` Jason Gunthorpe
2023-08-03  2:36     ` Nicolin Chen
2023-08-03  2:53       ` Liu, Yi L
2023-08-03  3:04         ` Nicolin Chen
2023-07-24 11:03 ` [PATCH v3 03/17] iommufd/hw_pagetable: Use domain_alloc_user op for domain allocation Yi Liu
2023-07-28  9:39   ` Tian, Kevin
2023-07-24 11:03 ` [PATCH v3 04/17] iommufd: Pass in hwpt_type/parent/user_data to iommufd_hw_pagetable_alloc() Yi Liu
2023-07-28  9:49   ` Tian, Kevin
2023-07-24 11:03 ` [PATCH v3 05/17] iommufd/hw_pagetable: Do not populate user-managed hw_pagetables Yi Liu
2023-07-28  9:51   ` Tian, Kevin
2023-07-24 11:03 ` [PATCH v3 06/17] iommufd: Only enforce IOMMU_RESV_SW_MSI when attaching user-managed HWPT Yi Liu
2023-07-28 10:02   ` Tian, Kevin
2023-07-28 17:06     ` Jason Gunthorpe
2023-07-24 11:03 ` [PATCH v3 07/17] iommufd: Add IOMMU_RESV_IOVA_RANGES Yi Liu
2023-07-28 10:07   ` Tian, Kevin
2023-07-28 17:16     ` Jason Gunthorpe
2023-07-31  6:14       ` Tian, Kevin
2023-07-31 13:12         ` Jason Gunthorpe
2023-07-28 17:44   ` Jason Gunthorpe
2023-07-31  6:21     ` Tian, Kevin
2023-07-31  9:53       ` Liu, Yi L
2023-07-31 13:23       ` Jason Gunthorpe
2023-08-01  2:40         ` Tian, Kevin
2023-08-01 18:22           ` Jason Gunthorpe
2023-08-02  1:09             ` Tian, Kevin
2023-08-02 12:22               ` Jason Gunthorpe
2023-08-03  1:23               ` Nicolin Chen
2023-08-03  1:25                 ` Tian, Kevin
2023-08-03  2:17                   ` Nicolin Chen
2023-07-24 11:03 ` [PATCH v3 08/17] iommufd: IOMMU_HWPT_ALLOC allocation with user data Yi Liu
2023-07-28 17:55   ` Jason Gunthorpe
2023-07-28 19:10     ` Nicolin Chen
2023-07-31  7:22       ` Liu, Yi L
2023-07-31  6:31     ` Tian, Kevin
2023-07-31 13:16       ` Jason Gunthorpe
2023-08-01  2:35         ` Tian, Kevin
2023-08-02 23:42         ` Nicolin Chen
2023-08-02 23:43           ` Jason Gunthorpe
2023-08-03  0:53             ` Nicolin Chen
2023-08-03 16:47               ` Jason Gunthorpe [this message]
2023-07-24 11:03 ` [PATCH v3 09/17] iommufd: Add IOMMU_HWPT_INVALIDATE Yi Liu
2023-07-28 18:02   ` Jason Gunthorpe
2023-07-31 10:07     ` Liu, Yi L
2023-07-31 13:19       ` Jason Gunthorpe
2023-08-03  2:16         ` Nicolin Chen
2023-08-03  3:07           ` Liu, Yi L
2023-08-03  3:13             ` Nicolin Chen
2023-08-03  2:56         ` Liu, Yi L
2023-08-03  2:07     ` Nicolin Chen
2023-07-24 11:03 ` [PATCH v3 10/17] iommufd/selftest: Add a helper to get test device Yi Liu
2023-07-24 11:04 ` [PATCH v3 11/17] iommufd/selftest: Add IOMMU_TEST_OP_DEV_[ADD|DEL]_RESERVED to add/del reserved regions to selftest device Yi Liu
2023-07-24 11:04 ` [PATCH v3 12/17] iommufd/selftest: Add .get_resv_regions() for mock_dev Yi Liu
2023-07-24 11:04 ` [PATCH v3 13/17] iommufd/selftest: Add coverage for IOMMU_RESV_IOVA_RANGES Yi Liu
2023-07-24 11:04 ` [PATCH v3 14/17] iommufd/selftest: Add domain_alloc_user() support in iommu mock Yi Liu
2023-07-24 11:04 ` [PATCH v3 15/17] iommufd/selftest: Add coverage for IOMMU_HWPT_ALLOC with user data Yi Liu
2023-07-24 11:04 ` [PATCH v3 16/17] iommufd/selftest: Add IOMMU_TEST_OP_MD_CHECK_IOTLB test op Yi Liu
2023-07-24 11:04 ` [PATCH v3 17/17] iommufd/selftest: Add coverage for IOMMU_HWPT_INVALIDATE ioctl Yi Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZMvaNqskASX3pGuF@nvidia.com \
    --to=jgg@nvidia.com \
    --cc=alex.williamson@redhat.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=chao.p.peng@linux.intel.com \
    --cc=cohuck@redhat.com \
    --cc=eric.auger@redhat.com \
    --cc=iommu@lists.linux.dev \
    --cc=jasowang@redhat.com \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=lulu@redhat.com \
    --cc=mjrosato@linux.ibm.com \
    --cc=nicolinc@nvidia.com \
    --cc=peterx@redhat.com \
    --cc=robin.murphy@arm.com \
    --cc=shameerali.kolothum.thodi@huawei.com \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=yi.l.liu@intel.com \
    --cc=yi.y.sun@linux.intel.com \
    --cc=zhenzhong.duan@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).