From patchwork Wed Jun 12 21:54:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathias Krause X-Patchwork-Id: 13695606 Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) (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 0AA5B82488 for ; Wed, 12 Jun 2024 21:54:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718229271; cv=none; b=KbOyJ+V0wBreKGwcqQdfjnG/Wy+Gl74WsyQvoCZlzcJ4+JVWcPSW2VfVONYonLRNNpCkSnSNDCr9YkmDnq1vEWzzHoR2Q72knU0ZcfPhlscuiuTTmcxvBRlFgUWU4y9TlZVykEuqZtt+KNdY/QTj9G0J92EKPeka5dtRC9p3W8g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718229271; c=relaxed/simple; bh=YYK68WFDRsw2ngRpORU4BvMiRT9JZvHsdKs2lAAvwXA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=twgzKqKE3ipd+ss0mA8zojKrvUeUwPWsXzSraSQyptOfMtRyjNrCQ9r5nPRQ1h8usBZ3kNGcZmRM/nl20xuFz1aoLZQOtnf6D5tdI//Icg2bN3ALFnL8vCieQ8KkVVVuKeoxONEXQ2eylAv8Q58nP8k0qqPCI3Tar7EHtKQd/E4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=grsecurity.net; spf=pass smtp.mailfrom=opensrcsec.com; dkim=pass (2048-bit key) header.d=grsecurity.net header.i=@grsecurity.net header.b=DJTf9oMc; arc=none smtp.client-ip=209.85.218.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=grsecurity.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=opensrcsec.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=grsecurity.net header.i=@grsecurity.net header.b="DJTf9oMc" Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-a6265d48ec3so51218566b.0 for ; Wed, 12 Jun 2024 14:54:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grsecurity.net; s=grsec; t=1718229268; x=1718834068; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GpnIlzasRXPDrLHeFqpUbRMekE5s61RRbf6mIjmfyP4=; b=DJTf9oMcqUxWfdBt8xMpcgOHk9lYG/+i1sEK1fpIoH7HZFacGC7qdWNEY5Ujdy/EwB cz4lExLNT4zOOdfVwCEz4QxuaogCsUqm3QxnzKRSX+UhUTaAMFBTKL9PVdiQfM786LhO SFIwV/sP4EKlxrz9+r/6jv9eJvL7rtvQGeWTCCgoE4GU3DX94AOG96XVYJehXYrBEHID nNyuAUCtCL0mvb/tLt9/OnLXZf/ZHm022VyCVpqBJ6emNqZlNczpBzgms6uSBTeMEjjL 9VrZ9HX9Cpl3GG8PbF3sGlEJYh/N1iq95RvnjNTOR84qaJxSg+kl6M+Ds+6XQGCsLz7o RAHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718229268; x=1718834068; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GpnIlzasRXPDrLHeFqpUbRMekE5s61RRbf6mIjmfyP4=; b=ByNn/jRs5ITwCRxPMYoDwP4nVNHOjlP7o6sOEqi5CBo2ak0YvQw7/p2h9aDbgwSndU WpwqdP94bg33oVwy4DteIMwPpv/1/uZv4WnrStPSgY6c7kLga+OoM/nX9NQBnixnofiP jiFNT/szlyOInRZNvnN/zLYagdRFVboK/VkZQ+HdCVpqhR1xNuqVdkURRn+Bx9yRYyU/ QPvRfcEL4BWj/1udhR3o5t1fYE/S/yh8ndnvZwdNrGRyrmewRnk2CtdoBcE4RyfUAlSl mE4JzfmoWD/boQ2rc4/OQ+IHxbDKqcuGGQTfcH0hLbGkSKlYn7PL35xRY7P05TH44vPY bz0g== X-Gm-Message-State: AOJu0YznqrMtrCqtuMzSyfxLV+UvP1lKUqv2ZoiV1A214rBPgVT5xWkL uB/zoWYgId4DFONAAYF5FhOOu8g0Yrf0hMgu65e4M6ci4iRrO4rT722MOp+Eo+Y= X-Google-Smtp-Source: AGHT+IFAP2dxYnk5y5m6tXMhQr8qkxgEQ7fuY4uqCUeHAYT2sEziLS32qJpOa3tt50BpR+y1tQlqHg== X-Received: by 2002:a17:907:2d28:b0:a6f:1f4a:dfba with SMTP id a640c23a62f3a-a6f47cc0dcemr205356466b.43.1718229268191; Wed, 12 Jun 2024 14:54:28 -0700 (PDT) Received: from bell.fritz.box (p200300f6af372f000c444b1ebcbc1017.dip0.t-ipconnect.de. [2003:f6:af37:2f00:c44:4b1e:bcbc:1017]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a6dff0247a4sm785359966b.147.2024.06.12.14.54.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 14:54:27 -0700 (PDT) From: Mathias Krause To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, Mathias Krause , Emese Revfy , PaX Team Subject: [PATCH v2 1/4] KVM: Reject overly excessive IDs in KVM_CREATE_VCPU Date: Wed, 12 Jun 2024 23:54:12 +0200 Message-Id: <20240612215415.3450952-2-minipli@grsecurity.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240612215415.3450952-1-minipli@grsecurity.net> References: <20240612215415.3450952-1-minipli@grsecurity.net> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 If, on a 64 bit system, a vCPU ID is provided that has the upper 32 bits set to a non-zero value, it may get accepted if the truncated to 32 bits integer value is below KVM_MAX_VCPU_IDS and 'max_vcpus'. This feels very wrong and triggered the reporting logic of PaX's SIZE_OVERFLOW plugin. Instead of silently truncating and accepting such values, pass the full value to kvm_vm_ioctl_create_vcpu() and make the existing limit checks return an error. Even if this is a userland ABI breaking change, no sane userland could have ever relied on that behaviour. Reported-by: PaX's SIZE_OVERFLOW plugin running on grsecurity's syzkaller Fixes: 6aa8b732ca01 ("[PATCH] kvm: userspace interface") Cc: Emese Revfy Cc: PaX Team Signed-off-by: Mathias Krause --- v2: - add comment and build bug to make truncation check more obvious (Sean) virt/kvm/kvm_main.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 14841acb8b95..b04e87f6568f 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4200,12 +4200,20 @@ static void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) /* * Creates some virtual cpus. Good luck creating more than one. */ -static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) +static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id) { int r; struct kvm_vcpu *vcpu; struct page *page; + /* + * KVM tracks vCPU IDs as 'int', be kind to userspace and reject + * too-large values instead of silently truncating. + * + * Also ensure we're not breaking this assumption by accidentally + * pushing KVM_MAX_VCPU_IDS above INT_MAX. + */ + BUILD_BUG_ON(KVM_MAX_VCPU_IDS > INT_MAX); if (id >= KVM_MAX_VCPU_IDS) return -EINVAL; From patchwork Wed Jun 12 21:54:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathias Krause X-Patchwork-Id: 13695607 Received: from mail-ej1-f52.google.com (mail-ej1-f52.google.com [209.85.218.52]) (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 C01D6127E3F for ; Wed, 12 Jun 2024 21:54:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718229273; cv=none; b=uUIImx0nZkGq8j5+7IYL5uf+mx4YsXAGl27tvJSTN4Mr2bQirFiKUVA/GTzJcoKQNQxb53IfaVZ/OHUHGsTW27hSqkzg9TGLDgIC9ZLNJqw6nSeBmUTjl7pOWsbZ1MZTg/UXx2tziswI6I/7LCn+ldheY1Wc6KaizaIUoAAkOFo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718229273; c=relaxed/simple; bh=Mef8+WrFLjcRWTeyr7/n9NEG+tIXc2iTFCcmhv9M4po=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=J/ZOj88d+vb7XqqdGBtJ26V9erDwR+D38OMnhq9PO9ySrOC8SxrjtcU5/9+Uxy2tyFrBMXBIFy+sgV3Z8//CHVZOamyqYUnkS8B5Mh+eQB3x4zpa8dNJC14x6itj90mPHCIUkP20SQStOIAg1TLTWC+Vn02+iqly1IHqelswEQ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=grsecurity.net; spf=pass smtp.mailfrom=opensrcsec.com; dkim=pass (2048-bit key) header.d=grsecurity.net header.i=@grsecurity.net header.b=I08DF1+j; arc=none smtp.client-ip=209.85.218.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=grsecurity.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=opensrcsec.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=grsecurity.net header.i=@grsecurity.net header.b="I08DF1+j" Received: by mail-ej1-f52.google.com with SMTP id a640c23a62f3a-a6f0e153eddso48134866b.0 for ; Wed, 12 Jun 2024 14:54:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grsecurity.net; s=grsec; t=1718229269; x=1718834069; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rZbb2eMSSDdlpeUuW/bDuyvE9odB04iaefVgLAtvy4k=; b=I08DF1+j26wO+mmceoQ53IGTDam+i+TSNh8FfpM5wci8+2Gaa5t8cQ0/HVDdD6Gmq7 MImETVAUWKAAVhOy+8Kh2WvAQGfg3S0HQtOm94MqsCQYtgINJX+boBv9pmQdRJ/IsvS8 7auiP4bZ/dmEZJamJNU7xvdGiM/6vQUReFWrF3vk6HikmELqDdhCaoqICxWRJP3fymW2 8eejqFxj7SNZPxF74wzyMHSaRXovtMXisxSRJt6bE3QdKA3M5Q1z4WNTR6tFmeppSuwG qFiDL2DedkKz383tpEVfQxObdc/TSsRNHk5zpDM604ayDfjLqfgN2ASd7lka0LxpcX3K t9jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718229269; x=1718834069; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rZbb2eMSSDdlpeUuW/bDuyvE9odB04iaefVgLAtvy4k=; b=FSGmT4+iXLjT4KwK+ktps8cIuiG+GWpnJ459DYvXQ84Rmtvga2rZDtg6mIroNoa+jo NR8ldVU0Pe0K7+leUFy44HlDe0ZXa+9fWbYoRJ2uUtXoupftemW+or68H0EfuW2TFsPI fWkyU0RLlo2TeJtpVfdss3QiclFMYUy7eIlZEg1Q79fFIHL4cNp4vnSz37IZV8C2/JV5 CrodTv1Agwp9xNt2AMEDNSR43Bgx72cZGQOY4OHEcnvqsAcGKEVQn24dQlVtkjDw3qDB qPMqtJyQBFpLWICfbDNHFVJP0EHvlW5fDQdTfPlvIinifcYKuOOfeJhJNHi7U7i9TWFO 3yHA== X-Gm-Message-State: AOJu0YwssNwkd+MU6Zs7E4Djo8IS23tVv7eKyi9zzYkLhc9pPve9j1gU eoRzLSK6Tj7ZX5B+WwvI85rT9swrgvQN+hO8FMgf7bRtXZqp6JS6HnyQEW31iLU/8kN05r02K6K +wRk= X-Google-Smtp-Source: AGHT+IFaXOJZmuW4RC6vvWftRaxf9A4ltZOMTTHSaCEYvTy6FkXgvBXAw0m/VsSCO2SYYloQijxCLg== X-Received: by 2002:a17:907:7e87:b0:a6f:4b7d:599b with SMTP id a640c23a62f3a-a6f4b7d6bf0mr173495966b.33.1718229269312; Wed, 12 Jun 2024 14:54:29 -0700 (PDT) Received: from bell.fritz.box (p200300f6af372f000c444b1ebcbc1017.dip0.t-ipconnect.de. [2003:f6:af37:2f00:c44:4b1e:bcbc:1017]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a6dff0247a4sm785359966b.147.2024.06.12.14.54.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 14:54:28 -0700 (PDT) From: Mathias Krause To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, Mathias Krause Subject: [PATCH v2 2/4] KVM: selftests: Test vCPU IDs above 2^32 Date: Wed, 12 Jun 2024 23:54:13 +0200 Message-Id: <20240612215415.3450952-3-minipli@grsecurity.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240612215415.3450952-1-minipli@grsecurity.net> References: <20240612215415.3450952-1-minipli@grsecurity.net> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The KVM_CREATE_VCPU ioctl ABI had an implicit integer truncation bug, allowing 2^32 aliases for a vCPU ID by setting the upper 32 bits of a 64 bit ioctl() argument. Verify this no longer works and gets rejected with an error. Signed-off-by: Mathias Krause --- .../selftests/kvm/x86_64/max_vcpuid_cap_test.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c b/tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c index 3cc4b86832fe..92cb2f4aef6d 100644 --- a/tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c +++ b/tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c @@ -35,10 +35,19 @@ int main(int argc, char *argv[]) TEST_ASSERT(ret < 0, "Setting KVM_CAP_MAX_VCPU_ID multiple times should fail"); - /* Create vCPU with id beyond KVM_CAP_MAX_VCPU_ID cap*/ + /* Create vCPU with id beyond KVM_CAP_MAX_VCPU_ID cap */ ret = __vm_ioctl(vm, KVM_CREATE_VCPU, (void *)MAX_VCPU_ID); TEST_ASSERT(ret < 0, "Creating vCPU with ID > MAX_VCPU_ID should fail"); + /* Create vCPU with id beyond UINT_MAX */ + ret = __vm_ioctl(vm, KVM_CREATE_VCPU, (void *)(1L << 32)); + TEST_ASSERT(ret < 0, "Creating vCPU with ID > UINT_MAX should fail"); + + /* Create vCPU with id within bounds */ + ret = __vm_ioctl(vm, KVM_CREATE_VCPU, (void *)0); + TEST_ASSERT(ret >= 0, "Creating vCPU with ID 0 should succeed"); + + close(ret); kvm_vm_free(vm); return 0; } From patchwork Wed Jun 12 21:54:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathias Krause X-Patchwork-Id: 13695608 Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) (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 0671A129E64 for ; Wed, 12 Jun 2024 21:54:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718229273; cv=none; b=XFOHK0uIZDxD8PfWg9kE24XH5XclBgxjUpJsvntUTswinsvBsVgPdrEzwJgyGZQSzYG2FiTo8+369ok6sKYhdYFBBn0KBrXxBW0pmag1K4a88ZCuDqyYAmmPNgtVqXaE8B6n1nepzl9w+I86zoku/zdTIHv8KLdPpMiIDYF3LuU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718229273; c=relaxed/simple; bh=onOJoJjekcC/jjF8nCYd2zIlx4A4Ov94vgOAn+tbB2g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rw3mOaQV+TSO2oIwLLSyJ9GCHCISDg+qfywPNxmUoQKLCWJxpEpSmOiLxqaiGInMPOVzKUu725+g5XuYR/jSDvM/oiusmkPHsNJaD1g1kIRD4f16WH1Mf45l5q7CWikVafoo8N9XlUm30vlkYwCmW9Ra1eIKV0nArf777SKFKLg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=grsecurity.net; spf=pass smtp.mailfrom=opensrcsec.com; dkim=pass (2048-bit key) header.d=grsecurity.net header.i=@grsecurity.net header.b=NhoPPUrH; arc=none smtp.client-ip=209.85.218.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=grsecurity.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=opensrcsec.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=grsecurity.net header.i=@grsecurity.net header.b="NhoPPUrH" Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-a6ef46d25efso47556266b.0 for ; Wed, 12 Jun 2024 14:54:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grsecurity.net; s=grsec; t=1718229270; x=1718834070; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pPO/ZwCO10YSJSUceIlXnwl+dzkbujZyp0szmagKulg=; b=NhoPPUrHEPaTCSFAQR2o0zsuTuFIfydPJ/+2e1f71Zz2VKyhXWHuJK2IHYwELq6WEg ZtWAGRbgfkwbymcVkMN9PE2HXKse9ySSRLgoMwLfKDhsN8ZY+lZgvlP1XkkKYtPIiOv0 IUg5wTSvGeuYZGx4SfLt06hly0a5zbKRN6cj9cnxQEHk1nC6MAr/ZAIX/7LRkhFX+P8S ba0juyWto9LE0s52urwpmHkWCHOjikxcv9kM8NQA0v41gI8qjSs7h0y7b/qc7bZqWyU8 7bFhk4e/6pH/U0L4AXiQZkUlXRmXOscRqragH8hDZE1CloQ5x5bwCcsoKdX2xlm/FVph ARUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718229270; x=1718834070; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pPO/ZwCO10YSJSUceIlXnwl+dzkbujZyp0szmagKulg=; b=vmcsM8tz4h4OMFhwvjMF95OolH0APxYlrbbzEBC9UAvyauS7UzirvZlWyGoDdXMxFr 3AAHDd5LK+0VM5YyTTPtKD2sPwhkaAa7AGDl8mMNuEtycRbL1fou2fPx38oNcM+TThKC kuiMSYd/jWw5quGbm6ZkqgLowwb+7z5ZP8F3gtMZ4Rze1L+2sVIRd1O59CgqyN8I+W0+ X2hGj6pQCXf3zFItoswVtqYq8uWkdzLXZvZ+fMDpW0k4xtamcM7TETphSl+Gc2uDK67t KWGc7jrZAetya/N3w0AYSM9/u7SK+88uyiMmdmzYg6zDtapHlU+OMcZwBwOynW8rqBB1 7dBA== X-Gm-Message-State: AOJu0YwGreZWy9DPsHw5n02W5aJDU8bH2OVz3NxurOOT6DqoilLrb1S4 WQ+gH8C0hjLTzO9TCNtVCxZkMKZ/HEvn8ii5fGcPJJwVAaySMhtmpLuejsGLuwY= X-Google-Smtp-Source: AGHT+IF/JLGPxcW9ZEYwXXV02cm5lOsBCHUeMRewjlFjt9i+Yecm3AVXuyheUACky2Ns0KiXre7iJg== X-Received: by 2002:a17:906:3453:b0:a6f:4ef7:85bd with SMTP id a640c23a62f3a-a6f4ef78693mr99742366b.47.1718229270251; Wed, 12 Jun 2024 14:54:30 -0700 (PDT) Received: from bell.fritz.box (p200300f6af372f000c444b1ebcbc1017.dip0.t-ipconnect.de. [2003:f6:af37:2f00:c44:4b1e:bcbc:1017]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a6dff0247a4sm785359966b.147.2024.06.12.14.54.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 14:54:29 -0700 (PDT) From: Mathias Krause To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, Mathias Krause Subject: [PATCH v2 3/4] KVM: Limit check IDs for KVM_SET_BOOT_CPU_ID Date: Wed, 12 Jun 2024 23:54:14 +0200 Message-Id: <20240612215415.3450952-4-minipli@grsecurity.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240612215415.3450952-1-minipli@grsecurity.net> References: <20240612215415.3450952-1-minipli@grsecurity.net> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Do not accept IDs which are definitely invalid by limit checking the passed value against KVM_MAX_VCPU_IDS. This ensures invalid values, especially on 64-bit systems, don't go unnoticed and lead to a valid id by chance when truncated by the final assignment. Fixes: 73880c80aa9c ("KVM: Break dependency between vcpu index in vcpus array and vcpu_id.") Signed-off-by: Mathias Krause --- arch/x86/kvm/x86.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 082ac6d95a3a..8bc7b8b2dfc5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7220,10 +7220,16 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) case KVM_SET_BOOT_CPU_ID: r = 0; mutex_lock(&kvm->lock); - if (kvm->created_vcpus) + if (kvm->created_vcpus) { r = -EBUSY; - else - kvm->arch.bsp_vcpu_id = arg; + goto set_boot_cpu_id_out; + } + if (arg > KVM_MAX_VCPU_IDS) { + r = -EINVAL; + goto set_boot_cpu_id_out; + } + kvm->arch.bsp_vcpu_id = arg; +set_boot_cpu_id_out: mutex_unlock(&kvm->lock); break; #ifdef CONFIG_KVM_XEN From patchwork Wed Jun 12 21:54:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathias Krause X-Patchwork-Id: 13695609 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (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 382AB12B177 for ; Wed, 12 Jun 2024 21:54:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718229274; cv=none; b=Ovu5gjFpHSk5Y/Ty7SPbHZ5kEaMRYC6sP4O6YqrC7gEhfcO6thG8AJmKamZx9JLhoJJbxu5eTzaQipkRRiX/6IaPQikMtTI+O8uAGxvTm2PPuu0ZxFgGwkxXyUl4Y965RkOjTWOQ3EtcINy4s7FTzz/KkevcwYa5Lw7Q4jME/XA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718229274; c=relaxed/simple; bh=rE9aL80RorEZ/DIPTVxfLLrUzVpac9U2W0UUK0H057c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Z7LFQ+fHlOx+CN0Tm4mcWS3LBdaFndx1BtqL1aW+dpXAUZVUnnnut7M3GXlO7E64siziZr8DsBU0JSCh4niOsQr7L3GpHrt+b3xG1YH//WStiCGh84X1Vat2wZmFCwCqKzkYPRO43hLYlNlnk70tpaG0EiW/205vi8QdXKgY8us= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=grsecurity.net; spf=pass smtp.mailfrom=opensrcsec.com; dkim=pass (2048-bit key) header.d=grsecurity.net header.i=@grsecurity.net header.b=X5YVAVY2; arc=none smtp.client-ip=209.85.221.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=grsecurity.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=opensrcsec.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=grsecurity.net header.i=@grsecurity.net header.b="X5YVAVY2" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-35f275c7286so426537f8f.2 for ; Wed, 12 Jun 2024 14:54:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grsecurity.net; s=grsec; t=1718229271; x=1718834071; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=luWaaedY4w32jXOpW7TpCRbskBqnHi+qmEvw5Pcn7cE=; b=X5YVAVY2jo6PXjmG5uu0KOju133PGEJvHrDBQpIXf3nxjoOTIWbxQJIJg4h5fYeLu4 ByBLJt2RgZ2xzhRUc7/XPrRloa7XXHCqDaNnLGUHqcnoUTwhnOBnHDtpBlRVNhT0UGLc ZeKoiYBXCL8tZrRRRFuSpNq4mZmU6U0sY4+Dp0FE/RxEbSK5ygEtuhJ65HtXUb5PxQno 5MYO1szdDjnqZ8JFcDklHqwin6iFE/TGQtJHUXoZkB+MzJ2aOfoo/VKmsVgWUrQs+ZfJ xORmIljStkUkyeolWzMtZIOkAmWMgj1kASVk8QVdp+s4t40cWRaLdRvSP0nzYSausIAL l7rQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718229271; x=1718834071; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=luWaaedY4w32jXOpW7TpCRbskBqnHi+qmEvw5Pcn7cE=; b=uQxV3JtM15rets9+YYWjZje7Num8vaTC/dPnYuKAnF+/zEbU6re5E2pBaXNnb5AtV2 a2qhxB3S6Dt7rBemqEuLAq0OWXjdotTvMmBPcRo3wdZt0PaVa1AVbteOfte45hGghuku YHFrvaqmR8EJmOmo9FSE+2NldSu6x/0+p1Sc+/8SqhL/Rjzh7LyIlIfR6+VuThGxhq3x iImvl2v1RVnptDBoTq9RyZGy+yq6rg2aQ8fOJRSwnrGAwKnsOUyHF3JGz9X4qQtIREM7 DbXuXkU1FVGtB6qGsUWwxEteIrjsBUOxjztEd4CWm/7CFkEcrWu5Z87tLtzNFzwmaL1H /guw== X-Gm-Message-State: AOJu0Yw6vYBN4NdDwWGwG2TKi8Sw4YDB8fikyrftr/EEL1VC6FH8zkeQ g4ZZyfzQqYFu8hAXtORnOw2k6cD7DCrva6U7Kh3NYPCEu4OTS+UEIU3mncRGgrc= X-Google-Smtp-Source: AGHT+IETn65d2hRBRBtVc+jTdn7hRbXFvnUjttjwDiC6xuRpRQfn/eMlbgoF3N640kFd25OZNkJ8yg== X-Received: by 2002:a5d:4dce:0:b0:35f:2a43:3b16 with SMTP id ffacd0b85a97d-35fe88c4c40mr2064541f8f.33.1718229271644; Wed, 12 Jun 2024 14:54:31 -0700 (PDT) Received: from bell.fritz.box (p200300f6af372f000c444b1ebcbc1017.dip0.t-ipconnect.de. [2003:f6:af37:2f00:c44:4b1e:bcbc:1017]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a6dff0247a4sm785359966b.147.2024.06.12.14.54.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 14:54:30 -0700 (PDT) From: Mathias Krause To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, Mathias Krause Subject: [PATCH v2 4/4] KVM: selftests: Test vCPU boot IDs above 2^32 Date: Wed, 12 Jun 2024 23:54:15 +0200 Message-Id: <20240612215415.3450952-5-minipli@grsecurity.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240612215415.3450952-1-minipli@grsecurity.net> References: <20240612215415.3450952-1-minipli@grsecurity.net> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The KVM_SET_BOOT_CPU_ID ioctl missed to reject invalid vCPU IDs. Verify this no longer works and gets rejected with an appropriate error code. Signed-off-by: Mathias Krause --- tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c b/tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c index d691d86e5bc3..50a0c3f61baf 100644 --- a/tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c +++ b/tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c @@ -33,6 +33,13 @@ static void guest_not_bsp_vcpu(void *arg) GUEST_DONE(); } +static void test_set_invalid_bsp(struct kvm_vm *vm) +{ + int r = __vm_ioctl(vm, KVM_SET_BOOT_CPU_ID, (void *)(1L << 32)); + + TEST_ASSERT(r == -1 && errno == EINVAL, "invalid KVM_SET_BOOT_CPU_ID set"); +} + static void test_set_bsp_busy(struct kvm_vcpu *vcpu, const char *msg) { int r = __vm_ioctl(vcpu->vm, KVM_SET_BOOT_CPU_ID, @@ -75,11 +82,15 @@ static void run_vcpu(struct kvm_vcpu *vcpu) static struct kvm_vm *create_vm(uint32_t nr_vcpus, uint32_t bsp_vcpu_id, struct kvm_vcpu *vcpus[]) { + static int invalid_bsp_tested; struct kvm_vm *vm; uint32_t i; vm = vm_create(nr_vcpus); + if (!invalid_bsp_tested++) + test_set_invalid_bsp(vm); + vm_ioctl(vm, KVM_SET_BOOT_CPU_ID, (void *)(unsigned long)bsp_vcpu_id); for (i = 0; i < nr_vcpus; i++)