Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06BD0C636CC for ; Wed, 8 Feb 2023 12:17:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231152AbjBHMRi (ORCPT ); Wed, 8 Feb 2023 07:17:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230246AbjBHMRf (ORCPT ); Wed, 8 Feb 2023 07:17:35 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A7E0448C for ; Wed, 8 Feb 2023 04:16:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675858605; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rjMSi9ok1TzHSDU3w1PUUtKMgZdBUc/PnFmrcylWMYk=; b=ZV3b0JZR/JUqJyeWKigVedVfdIEFJQb9oKPjro9HOQ9it10MZEhIKBsN/1xmzYlXO4nuEI 3wh1HQvf5JzIaMR9eCnyJDbzH5DyvH5OwVZkgWHrt8TcZbXrjRE9DSUCR4PobchF9cV/nM bYVhCJ0AFmhBQtVdTnlTcV2ZZ588JmY= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-362-8keXG49yMkSB3Jf3S_rLag-1; Wed, 08 Feb 2023 07:16:43 -0500 X-MC-Unique: 8keXG49yMkSB3Jf3S_rLag-1 Received: by mail-qv1-f71.google.com with SMTP id ng1-20020a0562143bc100b004bb706b3a27so9716695qvb.20 for ; Wed, 08 Feb 2023 04:16:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:subject:from:references:cc:to :content-language:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rjMSi9ok1TzHSDU3w1PUUtKMgZdBUc/PnFmrcylWMYk=; b=7LVIurS2h5n3CYZ7cnNgBcX7SDIs0nOEcQQMKnnC9BKg8NvMWyiv+u0kowbanvaQe5 0FfsJ4pQPoI6R/AjwS6kFLHNzbFwnyMvZVLAaavrQ8k5dOJTthGCSFQ2CEIJ7qmOLYcF MVGc6SQwAJLwpFylXJSR3kO8sxCmtmZy3MqSiXtfxoETWvw154FDKuyUTsTctnyTtet/ lfn1QstGYSeYy5+xr0TnaakW2y582MNFVNoaM/yKTZiBYmbTkMWy+i5StgFUALQKCLoc L4OBdChdigQK5nfiA2u0b+gE3J6bvG9HW+qqXfD2RH9sTI5apjG+iJsrmDgp+av7UKMT zdqQ== X-Gm-Message-State: AO0yUKWzVJr7AOKgeaZSVcGwAgxN4oXl7u3FFYNXv6sP1KC4mMYhWPvh D/DC6LQNCPmhh+dl2ysCvkqB45pvEqN8b+pFK0HjWzlVKdCbRezcT6+xlENY/wlZ+6sI+gSQylc Imeke893VkYBPVNm+bJLtzwHP X-Received: by 2002:a05:6214:2348:b0:56b:ef8e:18ec with SMTP id hu8-20020a056214234800b0056bef8e18ecmr12324927qvb.12.1675858603320; Wed, 08 Feb 2023 04:16:43 -0800 (PST) X-Google-Smtp-Source: AK7set9NZULWqTl2Mcke9STbzAhWv2Gmx/BViB8dl8z0WU3BjxNZZ22NA4XHXc1de+u8uoed8iFu9Q== X-Received: by 2002:a05:6214:2348:b0:56b:ef8e:18ec with SMTP id hu8-20020a056214234800b0056bef8e18ecmr12324890qvb.12.1675858603023; Wed, 08 Feb 2023 04:16:43 -0800 (PST) Received: from [192.168.0.2] (ip-109-43-177-253.web.vodafone.de. [109.43.177.253]) by smtp.gmail.com with ESMTPSA id 22-20020ac85616000000b003b630456b8fsm11264751qtr.89.2023.02.08.04.16.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 08 Feb 2023 04:16:42 -0800 (PST) Message-ID: Date: Wed, 8 Feb 2023 13:16:38 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.13.0 Content-Language: en-US To: Steven Price , Cornelia Huck , Gavin Shan , kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson Cc: kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm-riscv@lists.infradead.org, Marc Zyngier , James Morse , Suzuki K Poulose , Oliver Upton , Zenghui Yu , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , David Hildenbrand , linuxppc-dev@lists.ozlabs.org, Eric Auger References: <20230203094230.266952-1-thuth@redhat.com> <20230203094230.266952-7-thuth@redhat.com> <7b32d58b-846f-b8d7-165b-9f505e5f00f0@redhat.com> <87zg9oleyb.fsf@redhat.com> From: Thomas Huth Subject: Re: [PATCH 6/7] KVM: arm64: Change return type of kvm_vm_ioctl_mte_copy_tags() to "int" In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 08/02/2023 12.51, Steven Price wrote: > On 08/02/2023 08:49, Cornelia Huck wrote: >> On Wed, Feb 08 2023, Gavin Shan wrote: >> >>> On 2/7/23 9:09 PM, Thomas Huth wrote: >>>> Oh, drat, I thought I had checked all return statements ... this must have fallen through the cracks, sorry! >>>> >>>> Anyway, this is already a problem now: The function is called from kvm_arch_vm_ioctl() (which still returns a long), which in turn is called from kvm_vm_ioctl() in virt/kvm/kvm_main.c. And that functions stores the return value in an "int r" variable. So the upper bits are already lost there. > > Sorry about that, I was caught out by kvm_arch_vm_ioctl() returning long... That's why I'm trying to fix that return type mess with my series, to avoid such problems in the future :-) >>>> Also, how is this supposed to work from user space? The normal "ioctl()" libc function just returns an "int" ? Is this ioctl already used in a userspace application somewhere? ... at least in QEMU, I didn't spot it yet... >>>> >> >> We will need it in QEMU to implement migration with MTE (the current >> proposal simply adds a migration blocker when MTE is enabled, as there >> are various other things that need to be figured out for this to work.) >> But maybe other VMMs already use it (and have been lucky because they >> always dealt with shorter lengths?) >> >>> >>> The ioctl command KVM_ARM_MTE_COPY_TAGS was merged recently and not used >>> by QEMU yet. I think struct kvm_arm_copy_mte_tags::length needs to be >>> '__u32' instead of '__u64' in order to standardize the return value. >>> Something like below. Documentation/virt/kvm/api.rst::section-4.130 >>> needs update accordingly. >>> >>> struct kvm_arm_copy_mte_tags { >>> __u64 guest_ipa; >>> __u32 pad; >>> __u32 length; >>> void __user *addr; >>> __u64 flags; >>> __u64 reserved[2]; >>> }; >> >> Can we do this in a more compatible way, as we are dealing with an API? >> Like returning -EINVAL if length is too big? >> > > I agree the simplest fix for the problem is simply to reject any > lengths>INT_MAX: > > diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c > index cf4c495a4321..94aed7ce85c4 100644 > --- a/arch/arm64/kvm/guest.c > +++ b/arch/arm64/kvm/guest.c > @@ -1032,6 +1032,13 @@ long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, > if (copy_tags->flags & ~KVM_ARM_TAGS_FROM_GUEST) > return -EINVAL; > > + /* > + * ioctl returns int, so lengths above INT_MAX cannot be > + * represented in the return value > + */ > + if (length > INT_MAX) > + return -EINVAL; > + > if (length & ~PAGE_MASK || guest_ipa & ~PAGE_MASK) > return -EINVAL; > > This could also be fixed in a useable way by including a new flag which > returns the length in an output field of the ioctl structure. I'm > guessing a 2GB limit would be annoying to work around. I agree that checking for length > INT_MAX is likely the best thing to do here right now. I'll add that in v2 of my series. But actually, this might even be a good thing from another point of view (so I'm not sure whether your idea with the flag should really be pursued): The code here takes a mutex and then runs a while loop that depends on the length - which could cause the lock to be held for a rather long time if length is a 64-bit value. Forcing the user space to limit the length here could help to avoid taking the lock for too long. Thomas