Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1496825rbb; Mon, 26 Feb 2024 11:04:34 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXIsJ8isV2BfnD1Pmi31u8zU9u7HHCBuOq4fnNdKSo2Xvhg/H9/CHhbGJQRfg6cNacwwA4pBU5RC6J2NYHofPdb+qiwAQ6N/5NIIyhTCw== X-Google-Smtp-Source: AGHT+IFSYQo86vrg0xv4eVQlH/O164MiPNRRs/fOMcfaVYCVktazOemM0rDj3yXA6Qei78slGVV5 X-Received: by 2002:a17:906:3ac2:b0:a3f:98e4:7dc9 with SMTP id z2-20020a1709063ac200b00a3f98e47dc9mr4972607ejd.28.1708974274159; Mon, 26 Feb 2024 11:04:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708974274; cv=pass; d=google.com; s=arc-20160816; b=k4d9Wvlcl05Kqz6QfwxaYtz2qVDqNKxaTuZNqJykKeBG6zs/4X3/gYuoQQ8ILizgTG pqzv43DhiSHqqBPhoYygQgBtdW8zCwY0uU2pJgCsLXWvZcWExQa3irdC9irb3XivdeEq r7ZzpzA6RES1yUDyQ9EItES/gc3J3Eizo+tLAZ6gJwVopq7GCINwTR0HzR2SjGK/p/0w 3cdebyczPNPV6mj/Jh0FZeOkjwKr6xMPq+TY7u6MlkKB6swx1cCTFhthkYlUXWHp+I2B OddGXUrKoxbjd4cDHzzG+xAo2I0fZbx3lCP9HfAy/IFM2cB1L1SHej8e2+1FH/CIxGNP 35zw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=SrpFxJCexxYzP/66dv8sDZ2xWIWJ/QIQbY4Xg6TPt7Q=; fh=TLdZJsFzSA0fpm6Q4iUv+uB0sG0oU+h3Bp6HiSGyZm8=; b=iLiN0yEjRKB16M+PocvB5UdrkBJyO8HnnGDzxgDzQ6mAETjWLfWcOd3GPM2tLZ6kxA FV9nGDy0NHuBaUNeEM+YbGjHBRhyo++06kaw8C0GYq2b8IVX64juutDacC6KB8SGMVpW twLpgZwlsK7xJ6+7YMUSdM2duiyWbLCCKxVA9FIhJOBFCaSlRtP3OiBsuBFZiE8MlGOU 5/Ox1XMkJOF2tAQvUljJHaEbrreu4HupjgyidrKeJFmGwYKsMPmjpsVXqccB0Kot2A0+ mkpKJ8QZ6TYI6sKjTP0JFYcAgTtBhPmp+SGnevM53mvOA9NwRnokWILsr/S/gOuYVUNw GG+g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=R5WMXILR; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-82184-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-82184-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id p15-20020a170906140f00b00a3edc9e0a54si20387ejc.904.2024.02.26.11.04.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 11:04:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-82184-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=@redhat.com header.s=mimecast20190719 header.b=R5WMXILR; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-82184-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-82184-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com 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 DF3C31F275EF for ; Mon, 26 Feb 2024 19:04:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C101E130AFA; Mon, 26 Feb 2024 19:03:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="R5WMXILR" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A93FE12F39B for ; Mon, 26 Feb 2024 19:03:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708974233; cv=none; b=EFNVkHfiJoLFDSCL+MwzvirlI/D5U2+H6kBvJH8yraJlBuHPkhHZ9T5hSBo/pkcc3tDLFln4ZzwTqnsc03JGtuHz5F2ELukc7uTCWSGZpLBId5ghzGvZ0apvIvz+niyAWHpXX2uYtfxUbJ7kl3womp0oE2OGvpdt5UbuQgoBa+A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708974233; c=relaxed/simple; bh=365HX1koM5s1rKiT2wfEYWXzusfKgDR6HIIYyfQCgvI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=OzzgD2Ec+E2BUPldzTdcaDEBbv+HcoPXR3hhUfEmeOIuCszIX+g7pEZeoMpDtCWgh5hpXTQ3Uim1uYrnXNMiOlmp0XQwRZQFRbCnqYbdyzlXEjnLmofNHeUXQnYJQFeavwHaIjVEiE8mwLmN9JcAINaqshr6RRENk8ZeeAcxZgA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=R5WMXILR; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708974229; 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=SrpFxJCexxYzP/66dv8sDZ2xWIWJ/QIQbY4Xg6TPt7Q=; b=R5WMXILRFz0LXmwV/1W1G37zd5Cct7TS8F2KYvaPY+SCcC+QnjvZObEpqdMBY82M6pBAPR YROG6HJDGrjytcZy+ncV8lvZUT2gTKfUphZFxG7WJWg/pjOeY0FSkbbRCOyMC7OrmZfsaj 2StHDnI1K2D61fD5mkwT/4YT+G3J3bY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-526-38gKcZKRMhiuJsCSNaeXIQ-1; Mon, 26 Feb 2024 14:03:46 -0500 X-MC-Unique: 38gKcZKRMhiuJsCSNaeXIQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 130A388D543; Mon, 26 Feb 2024 19:03:46 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id DE074492BC6; Mon, 26 Feb 2024 19:03:45 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, michael.roth@amd.com, aik@amd.com Subject: [PATCH v3 02/15] KVM: x86: use u64_to_user_addr() Date: Mon, 26 Feb 2024 14:03:31 -0500 Message-Id: <20240226190344.787149-3-pbonzini@redhat.com> In-Reply-To: <20240226190344.787149-1-pbonzini@redhat.com> References: <20240226190344.787149-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 There is no danger to the kernel if userspace provides a 64-bit value that has the high bits set, but for whatever reason happ[ens to resolve to an address that has something mapped there. KVM uses the checked version of put_user() in kvm_x86_dev_get_attr(). Suggested-by: Sean Christopherson Signed-off-by: Paolo Bonzini --- arch/x86/kvm/x86.c | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f3f7405e0628..14c969782d73 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4791,25 +4791,13 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) return r; } -static inline void __user *kvm_get_attr_addr(struct kvm_device_attr *attr) -{ - void __user *uaddr = (void __user*)(unsigned long)attr->addr; - - if ((u64)(unsigned long)uaddr != attr->addr) - return ERR_PTR_USR(-EFAULT); - return uaddr; -} - static int kvm_x86_dev_get_attr(struct kvm_device_attr *attr) { - u64 __user *uaddr = kvm_get_attr_addr(attr); + u64 __user *uaddr = u64_to_user_ptr(attr->addr); if (attr->group) return -ENXIO; - if (IS_ERR(uaddr)) - return PTR_ERR(uaddr); - switch (attr->attr) { case KVM_X86_XCOMP_GUEST_SUPP: if (put_user(kvm_caps.supported_xcr0, uaddr)) @@ -5664,12 +5652,9 @@ static int kvm_arch_tsc_has_attr(struct kvm_vcpu *vcpu, static int kvm_arch_tsc_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) { - u64 __user *uaddr = kvm_get_attr_addr(attr); + u64 __user *uaddr = u64_to_user_ptr(attr->addr); int r; - if (IS_ERR(uaddr)) - return PTR_ERR(uaddr); - switch (attr->attr) { case KVM_VCPU_TSC_OFFSET: r = -EFAULT; @@ -5687,13 +5672,10 @@ static int kvm_arch_tsc_get_attr(struct kvm_vcpu *vcpu, static int kvm_arch_tsc_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) { - u64 __user *uaddr = kvm_get_attr_addr(attr); + u64 __user *uaddr = u64_to_user_ptr(attr->addr); struct kvm *kvm = vcpu->kvm; int r; - if (IS_ERR(uaddr)) - return PTR_ERR(uaddr); - switch (attr->attr) { case KVM_VCPU_TSC_OFFSET: { u64 offset, tsc, ns; -- 2.39.1