From patchwork Fri Jun 14 20:28:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathias Krause X-Patchwork-Id: 13699106 Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (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 E003C1822CA for ; Fri, 14 Jun 2024 20:29:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718396947; cv=none; b=mIvp79Q6sjd5UOVMlnnJoFSys0SJjJsEUOMIUt3NLJfl0SZoCSISVAwUZCCW4ca3Bg7P30mo52PBgWcMJM8+Yv/Oy5/wetbL+yF5xZz/7Iq3shepsUcsxFuFA9F2R2rmxVdwwU3wbt9Be5ojwE3eBFOjNf7hnGff4afwLRx2OC0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718396947; c=relaxed/simple; bh=KIBEghULSZwV2LNMt8Zbz36I3eJCDpO9Yl12yrXumV4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fi3tKpDDv2PJB/LZLR4tkkF9Z2H/W5dK+LBGcMiafZp2fyEYpI6ZNRalZmqabacoWMl6++WHvD/B6n8V6aW66HKEwkDYQvhiwKPjGnRDZKEPJ2Vh2656DzvNIwaUl6HP9xWfYPIxGBWK6j/WK0YbtI9YAKVEz8E4dJSQ9nTB0bc= 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=ePScOFnt; arc=none smtp.client-ip=209.85.218.51 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="ePScOFnt" Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-a6efae34c83so341187666b.0 for ; Fri, 14 Jun 2024 13:29:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grsecurity.net; s=grsec; t=1718396944; x=1719001744; 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=8zCsRmWbd4EwxlQsd0rAbDyCepG9PZwu03LPjZCiCo8=; b=ePScOFntQX6wuS18zHkdxwhqEAcTjh8XQaa6Jq3IlxAoDW60yhd/YFrEXeSCC14Lt8 FSJzwOaHikXjlXUg2iAlUoyOs/c7eHpxI3M/ux1f6W5Ulx8VboCbEGVHBuaEsChsSJU6 Ga0j27g84ERsY6/N+PawY+I7Xb27w6OqM9b0QUjRGSSsxFwyfG8RD7rPQYjFU16OVU0m zcOwFyrbBfcyBDZ7pBfcyz+dcAomO2ZisIVYkdfwnLs7k6WG1IUxwogqbGKcaYInIP5I J+PlwR4UlxF7xxsCQ9TwUQ/RZbjYib/a0Ui/CPSXLvo9tLDieZ+uNLAERvPJY5kIUg64 QwWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718396944; x=1719001744; 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=8zCsRmWbd4EwxlQsd0rAbDyCepG9PZwu03LPjZCiCo8=; b=Lp1fwKEqHp2t3NCHndvT72P+PpwxQ9eyoqIfsp5yHKXWsvhp8jr90weWPb7LqGfgMz zJLwOd3bodQLkG7Mtg6gILcgYhg3wxv5mqNmJzfhHASxg2dFlxPUCuC9p/7sITfxYkc0 5MKKpkb19F81EN3k7Z8kzPR28GqNPK0EL5lToBy9i+71juTilDamIANY25/3tCJL+c+P MhkmYGqYDD6WbrZUq7Y8vro6p6jxW9FyfnHb6++UGBI9PaDzTGLOmq2exh+uJY1FmoVh 5bRkpDOlassC5+RY8NEedhgMBGLNKwMuIKwCae/Fzkqw0ffzm5g2SvfWTLWXT+yFmAlS ObNg== X-Gm-Message-State: AOJu0YxqecxVWC+oq5YXzL1Un/6KTYwRy/rBoaT1CEja9NyyvMWQlF8w 5fP2xbnrGLshQ+dfkyfC2+IX9IKysGJqVF9TyfC7TpV0Evb1ZfZDGGd8ef6/mO0= X-Google-Smtp-Source: AGHT+IGCu7l9McEqXuH6/SgA3rnUnBYPHE3nvj5iHFT/5EQNOO1irKYuEFMPxBtPwb7TsRcPCb+zXA== X-Received: by 2002:a17:906:f2da:b0:a6f:5562:167 with SMTP id a640c23a62f3a-a6f60d42710mr248152366b.38.1718396944105; Fri, 14 Jun 2024 13:29:04 -0700 (PDT) Received: from bell.fritz.box (p200300f6af332a00214df27025e50a49.dip0.t-ipconnect.de. [2003:f6:af33:2a00:214d:f270:25e5:a49]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a6f56ed3685sm217474166b.126.2024.06.14.13.29.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 13:29:03 -0700 (PDT) From: Mathias Krause To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, Mathias Krause , Emese Revfy , PaX Team Subject: [PATCH v3 1/5] KVM: Reject overly excessive IDs in KVM_CREATE_VCPU Date: Fri, 14 Jun 2024 22:28:55 +0200 Message-Id: <20240614202859.3597745-2-minipli@grsecurity.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240614202859.3597745-1-minipli@grsecurity.net> References: <20240614202859.3597745-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 --- 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 Fri Jun 14 20:28:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathias Krause X-Patchwork-Id: 13699107 Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) (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 C49FA1836F9 for ; Fri, 14 Jun 2024 20:29:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718396948; cv=none; b=o6saXUtdp9Ka6zlHUHlh0j1iSc76+GW0tgZX1CdRqlnDmNXJvXdQ2UVGQs8MV2wc4Bk6w/BI6ccbEGlW+VBu3j99Kx5EmBOA6PubrkyKMlJPjBkZ2DjQymLluYealyhKzb03F03VPwSPg8T2h3W13i8FUzAc7Fe5yBPc+mPbed0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718396948; c=relaxed/simple; bh=1waVZBSBgmkVTRzD47YLiwSpWUY2zTLGJrDORugnwOA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=M/Enctvev4+B/Rv1kXlHiISFsIo22XDfFnGGXNHm1aGkQZxLPY8W0isoWHlG8/VeAGXgbMkrAaaDUpAPeDIaTeOhCsNE59n6uYWHcKiLQ8zb42R26eJgKpczVOyrz0UhvkbDa7rE3BCBMsBU/RXazqsjEH+agdNlvvS4pTZ4hhU= 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=TzcGQvEH; arc=none smtp.client-ip=209.85.167.53 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="TzcGQvEH" Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-52bbf73f334so2811981e87.2 for ; Fri, 14 Jun 2024 13:29:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grsecurity.net; s=grsec; t=1718396945; x=1719001745; 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=TM0RpYUvwtwCNY9Q6H4dViSdw/nY+sKwQUbKlWbVJNw=; b=TzcGQvEHwh5tOM+Mg3zJe8JOtluHWkK4v7xS3Hu6gqnCTS3SAxsy1vyzhj9hrKrgP5 /kDLvQa3pna10rsVw2lY+k6ZY8qf6eeNy4rn+wDfnO1JSLCfFkr4bjbH/rwd57iz12xW YkredxNO7Uhxqej1ZMb6SUSaCfaeytMVo/VJTn0cXH+R/XC075D4sB82KT0AjV5U/mqz iMDXjyY5r4+aNbvwuDMrS06R/aGMLXErXD+EZrum8yRQC5mFtgXDQE+nNWBdc6C6am8+ p7HWulf2GMhLQd5LCRTfelhf/PaGkZ0DevpZQzd9Y246vRVkaprS34R3EEACbSCcgbWm xk3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718396945; x=1719001745; 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=TM0RpYUvwtwCNY9Q6H4dViSdw/nY+sKwQUbKlWbVJNw=; b=EdamRTuR1YtKIMpUoh7Cnt0/C3Kp2eL7tv6U6I+EMF1eG5dpn8PlfIhv2RimBHRMqa pQKjC+ZkcqsFSInzd1555ZqZRES8veORIHgBrGRW3w9LCDF6X1EIgKUyBR5bC8+rV++g Guqv0IrfwbZFKyw2SgJTc9H/QFV4Nqeo6b5qAMdJANl/P6o9yWhfIFbd/M3Z+ysVONpi irhooDf8Qvi2DST+MwGh+XuCW7Kww5ehvTeGWmBsnWHPvdelS8nAbk45iCK3fA8by+yd TB5vnVGiAPQragEX6Hfe9VDpITtDSfa5ounWB/pNtbsYscCUsf3LJhdrG40DWFjG8mfa fiMA== X-Gm-Message-State: AOJu0YxnofnTyHrPO1faP47i3gPQZ7y1SKZTi7OTQHCgQmgpHgvOfG/0 xwABQsphveJfUoTBOdvcMgvWO6VCecetM3icvvvhqIhYVS5n/Gsa6+k/iGSV3FA= X-Google-Smtp-Source: AGHT+IH0pnDNNADT+RIDBfDocnjEDQBm9SFNoNatGvNoCsRLbL/2wERti+n4EFLVtNtRtVTXr4XMrw== X-Received: by 2002:a05:6512:41a:b0:52c:8933:d743 with SMTP id 2adb3069b0e04-52ca6e91534mr2399101e87.47.1718396944893; Fri, 14 Jun 2024 13:29:04 -0700 (PDT) Received: from bell.fritz.box (p200300f6af332a00214df27025e50a49.dip0.t-ipconnect.de. [2003:f6:af33:2a00:214d:f270:25e5:a49]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a6f56ed3685sm217474166b.126.2024.06.14.13.29.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 13:29:04 -0700 (PDT) From: Mathias Krause To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, Mathias Krause Subject: [PATCH v3 2/5] KVM: x86: Limit check IDs for KVM_SET_BOOT_CPU_ID Date: Fri, 14 Jun 2024 22:28:56 +0200 Message-Id: <20240614202859.3597745-3-minipli@grsecurity.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240614202859.3597745-1-minipli@grsecurity.net> References: <20240614202859.3597745-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 and 'max_vcpu_ids' if it was already set. 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.") Co-developed-by: Sean Christopherson Signed-off-by: Mathias Krause --- v3: - check against arch.max_vcpu_ids too, if already set (Sean) - switch to else-if instead of goto (Sean) arch/x86/kvm/x86.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 082ac6d95a3a..686606f61dee 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7222,6 +7222,9 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) mutex_lock(&kvm->lock); if (kvm->created_vcpus) r = -EBUSY; + else if (arg > KVM_MAX_VCPU_IDS || + (kvm->arch.max_vcpu_ids && arg > kvm->arch.max_vcpu_ids)) + r = -EINVAL; else kvm->arch.bsp_vcpu_id = arg; mutex_unlock(&kvm->lock); From patchwork Fri Jun 14 20:28:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathias Krause X-Patchwork-Id: 13699108 Received: from mail-ed1-f47.google.com (mail-ed1-f47.google.com [209.85.208.47]) (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 5EA3D18412B for ; Fri, 14 Jun 2024 20:29:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718396949; cv=none; b=CMPVxd5iTqjRhbSonxBP60EuPtwRAzNNybtwTpJdFT2/THdTNicXrX0UQEOIs4prbgD6OlcZAh8BkhGuVHE/YMvfj4HRL0r4OWwwJo+8/79epZsMRWjvM34EswJ0BUpSU+NtRQBeDIGGj2PzgVWFCXyjKQmgntuB+X4DlGx/3ko= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718396949; c=relaxed/simple; bh=Pd2HdXuVe2UBhoq2K89r5XG0/ruO202WXRk+Va9TcqY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ATIuexFpBv5VXR4TljTZa5ODtedGlSyRa7+RMdt7MkBJmJmV5JgRaqOV/M6qm+B+31kZCvWiCUWyOun8WfP66wcZWZ7YXlIBo2InJ9vHcjd7Fa7uZq5tl2LZkteMWVHwqBIaiWwsM11ERX3/szsa/m3P4w7/Qa/XlouRYzPn+bw= 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=qGTNRSE+; arc=none smtp.client-ip=209.85.208.47 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="qGTNRSE+" Received: by mail-ed1-f47.google.com with SMTP id 4fb4d7f45d1cf-57cb9efd8d1so3651624a12.0 for ; Fri, 14 Jun 2024 13:29:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grsecurity.net; s=grsec; t=1718396946; x=1719001746; 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=YDvFl+EPL3F7DYWyyu2ZaTt9wg+uT9SOWROUYajJpQk=; b=qGTNRSE+0/ReIJPqFXn8KEGcMc8ilrPDJWrU3HbdEzpayNXdrZuRJkr3cOfSRKBkFA Zst0Gcj91kBI7KYJCI2pa65YnMjkyih8r4VvPPNudOK2665lrGqxCzvxs800qNuXsWa7 i7Py8Fi7s1Wb05l8GOtX7+z6gieLRvdBW/RvkhZ6x8PkfON9P0tnyp6Lua2e0RdbJZad uWAxVpghXz6GDsXiW0mSC/7nHhshYxy9Vv7cV+64+8tIijAh7+l8R1AE6hdfNUNtfbrI UW2gEFAVzbfCfYI3DCE0Tz54cuV8wNEYYA6zcfnPWR46VmCkKpBuVC7Mhywak7rjUyVO T2Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718396946; x=1719001746; 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=YDvFl+EPL3F7DYWyyu2ZaTt9wg+uT9SOWROUYajJpQk=; b=GUDdxOljTUTieU2uWO5kwFzIesU3DR5PWjko7kxYQn1VoMR20LYcYL70PNBadyJN6Q UDfRMi02imCo2qyeIzHIC70/2LMpg7eKYLTC5aAygFjMpCt13YQS9fe5PrDGH4EFycBx WoIcViUvjCY2x57b5fNjf9tXf1opTYwRvdHSEuT8HQPiT8vCuEDGqzEMhQMgT9s+utBz C4H7RT0xg1WK/Ridcp7ost+ERaRqCR0epLF7Xp8JlDoUSQJQ79kM7xqn9KSOzIEXvQAa xsJn25m0v2o7fIDgqbK22NVoWHTmmG6ldl6c2gt9HzQ9lPy7TVHxtu07UZpj2/RJg8iy 1Kqw== X-Gm-Message-State: AOJu0YzbLMFlz+4MR+Aknwkh8pX59K+xtobHg1563X7uw0CGzYuoAIDe oqY1tLYdOj7OQnPyqI/y5W0sU1s7zfzhW5U6CEhO/4QcEmfu34EisX+tka4kgOQ= X-Google-Smtp-Source: AGHT+IE/Fu7sIvNHatCktQF57qVLGzqP8NIDqZ0rpV7HIZCCBXqmuqPGGPxxUFO8NGSgy+dpQsvPNw== X-Received: by 2002:a17:906:d7a2:b0:a6f:1c58:754a with SMTP id a640c23a62f3a-a6f5245cd05mr425115166b.24.1718396945762; Fri, 14 Jun 2024 13:29:05 -0700 (PDT) Received: from bell.fritz.box (p200300f6af332a00214df27025e50a49.dip0.t-ipconnect.de. [2003:f6:af33:2a00:214d:f270:25e5:a49]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a6f56ed3685sm217474166b.126.2024.06.14.13.29.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 13:29:05 -0700 (PDT) From: Mathias Krause To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, Mathias Krause Subject: [PATCH v3 3/5] KVM: x86: Prevent excluding the BSP on setting max_vcpu_ids Date: Fri, 14 Jun 2024 22:28:57 +0200 Message-Id: <20240614202859.3597745-4-minipli@grsecurity.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240614202859.3597745-1-minipli@grsecurity.net> References: <20240614202859.3597745-1-minipli@grsecurity.net> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Sean Christopherson If the BSP vCPU ID was already set, ensure it doesn't get excluded when limiting vCPU IDs via KVM_CAP_MAX_VCPU_ID. [mks: provide commit message, code by Sean] Signed-off-by: Mathias Krause --- arch/x86/kvm/x86.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 686606f61dee..eeb35fb1573a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6701,7 +6701,9 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, break; mutex_lock(&kvm->lock); - if (kvm->arch.max_vcpu_ids == cap->args[0]) { + if (kvm->arch.bsp_vcpu_id > cap->args[0]) { + ; + } else if (kvm->arch.max_vcpu_ids == cap->args[0]) { r = 0; } else if (!kvm->arch.max_vcpu_ids) { kvm->arch.max_vcpu_ids = cap->args[0]; From patchwork Fri Jun 14 20:28:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathias Krause X-Patchwork-Id: 13699109 Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.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 44F141A3BB1 for ; Fri, 14 Jun 2024 20:29:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718396949; cv=none; b=jAHP+YNENFVjq6m2rG+oDwWfHLkP7KPtpOaHDb0/z34iKjdt6TgG8AJeUO5cuLipu++BpevAZf6D21OQFtJia8e/ridXRZ9TVX7pAjD6L1yNNNp7GpahXaRUxYVUEN6wHefJZOYiXhIB48AV2JscDaHWEid0L1LmdXufpHoyAT8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718396949; c=relaxed/simple; bh=eyYnQz1U9yGOFnckGuPq+jsBS8RlOehpxpbEQiSfYXc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aJRko1L1ONEFoHKupKRdFpafXQKyOyBjXxh3zfEf4V4z0N4pa0MesBA6paqAmGrTnZmYOKm8rIvTEkaDJ+h08hHovAodol+F5tNWYiz5xfSmZKlM/u+pScK2C/hJD/n2/gS4jvcX/NuMuWd2s1oJvKN2zoVWZB+UzOHxenaHMFI= 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=gIAgUzPx; arc=none smtp.client-ip=209.85.218.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="gIAgUzPx" Received: by mail-ej1-f43.google.com with SMTP id a640c23a62f3a-a6e43dad8ecso468241366b.1 for ; Fri, 14 Jun 2024 13:29:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grsecurity.net; s=grsec; t=1718396946; x=1719001746; 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=ydM2qAqGpOE3GfUvJqcQrhP7uFyvSR7c8Nfb4W373fQ=; b=gIAgUzPxHLM/5Y7730HAeUKAxviLw7pEjocFm8UXau01kI2yqLXiQ/0UXL+f73HZpR OYNvQ3gtEGkN1AAe5s7ZVlwYAM+qTjQVwrz2ZhOUnxAmHbW/B8CX1YwwnxNuF7Bg9XTk 4BHuJPsdW/oSsmbs0GQ7U4IczbBlfSEuz1LdI4LM1ZJ8OeCIVWSytXEemfsxvR1fs498 XFOJ5CxviENr8bilI64AOX7pEWmOs3nvqcHvySflFWuczfjtfeEPO49wCXpphpxAmJ7e GZ1lC/BcD1yEBMh9cPX9t4Nm6TM1h7FTnpES28RceuIW9TjUbh8gYJhAaI2ot7pxJQoL nLsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718396946; x=1719001746; 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=ydM2qAqGpOE3GfUvJqcQrhP7uFyvSR7c8Nfb4W373fQ=; b=Evt0h68cXFXvFnxDQ5iXF8WYOO6Pau3ivWcs4Op/KB/iapmf1Vpe69JIWQNdgsWYIK 800OjGolJus+71FC/lqiBGHQpbEbmguJq2WKUELiEmIzDtPoxtklwNAANquE1bCEcFSn lBm6KK9cil3dfBECh1whQtbl7/kexTPARBCrbYsCuOV9THdcQpyGBKsSpjKEVdBN8oi4 VsFvS9zkxiCiLlL0V5rPJYn0mGP+Ud9LHDYeVfg2WOZQ5H6gnbQFYd6IBg6vD/aB2/+x Hv5hrN7GopKvpi1O1IQVs6KRGXYHJNnfgE4uKi2LTeZXavQXT2ZtAZJIigiXYYY0djU6 uxVQ== X-Gm-Message-State: AOJu0Yx3GjBwer7Y1oH4ubaOCiFiUZfB7MLv5WvIonEc2ODuO4X8aG1Y HIuf5WFtHYvAXtZY9YT3oqh2VDlGfoqAe6FawqJ/5NRlmM13gXtRqlGMqXGE4ukVfk68ooFiu/8 KxeY= X-Google-Smtp-Source: AGHT+IFS9eH6uOSbfui6BkTg8xP/ujfUMfsNGtQ6SJRkhx7jAFYelkBTzNBhrPQF6v6LtNg6cxXDEA== X-Received: by 2002:a17:906:b28a:b0:a6f:1d4e:734f with SMTP id a640c23a62f3a-a6f608bbab5mr239752866b.36.1718396946698; Fri, 14 Jun 2024 13:29:06 -0700 (PDT) Received: from bell.fritz.box (p200300f6af332a00214df27025e50a49.dip0.t-ipconnect.de. [2003:f6:af33:2a00:214d:f270:25e5:a49]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a6f56ed3685sm217474166b.126.2024.06.14.13.29.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 13:29:06 -0700 (PDT) From: Mathias Krause To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, Mathias Krause Subject: [PATCH v3 4/5] KVM: selftests: Test max vCPU IDs corner cases Date: Fri, 14 Jun 2024 22:28:58 +0200 Message-Id: <20240614202859.3597745-5-minipli@grsecurity.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240614202859.3597745-1-minipli@grsecurity.net> References: <20240614202859.3597745-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. It also allowed excluding a once set boot CPU ID. Verify this no longer works and gets rejected with an error. Signed-off-by: Mathias Krause --- v3: - test BOOT_CPU_ID interaction too .../kvm/x86_64/max_vcpuid_cap_test.c | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) 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..c2da915201be 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 @@ -26,19 +26,37 @@ int main(int argc, char *argv[]) TEST_ASSERT(ret < 0, "Setting KVM_CAP_MAX_VCPU_ID beyond KVM cap should fail"); + /* Test BOOT_CPU_ID interaction (MAX_VCPU_ID cannot be lower) */ + if (kvm_has_cap(KVM_CAP_SET_BOOT_CPU_ID)) { + vm_ioctl(vm, KVM_SET_BOOT_CPU_ID, (void *)MAX_VCPU_ID); + + /* Try setting KVM_CAP_MAX_VCPU_ID below BOOT_CPU_ID */ + ret = __vm_enable_cap(vm, KVM_CAP_MAX_VCPU_ID, MAX_VCPU_ID - 1); + TEST_ASSERT(ret < 0, + "Setting KVM_CAP_MAX_VCPU_ID below BOOT_CPU_ID should fail"); + } + /* Set KVM_CAP_MAX_VCPU_ID */ vm_enable_cap(vm, KVM_CAP_MAX_VCPU_ID, MAX_VCPU_ID); - /* Try to set KVM_CAP_MAX_VCPU_ID again */ ret = __vm_enable_cap(vm, KVM_CAP_MAX_VCPU_ID, MAX_VCPU_ID + 1); 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 Fri Jun 14 20:28:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathias Krause X-Patchwork-Id: 13699110 Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.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 5CC1B1AB523 for ; Fri, 14 Jun 2024 20:29:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718396951; cv=none; b=pqV9CBPkrChk8vt0SU8Sl09iRmgAcBwJb278cFxw9GWVXXs1dFJv8lgjGmfG+dhnLO06U2sySfX3K8YLpQzoidmQJAkMr/R5U9c3SA3JZiH8xD/hbuS4gb3dlJObXWz8UKzc4vyfLn1ebZvEBKQCrN3XfCDB+UwmHi0iujV2rUU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718396951; c=relaxed/simple; bh=rE9aL80RorEZ/DIPTVxfLLrUzVpac9U2W0UUK0H057c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cpWvSV35Pe7FujkmiEF4kdFuEBRGhc01nQ+P2BV4njbEX9qzrVur1xtqEbZ01qmJ7DokNm6pjKKoCkip4UkIvbDj7r9Rnp2ltPPkc+hFpZUfbu8wio68MpucvquTHM4duzN2XBqNeG/n/LKWulz0+tQ/kbrexii7fTAdh+MCluY= 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=UTPFC3A+; arc=none smtp.client-ip=209.85.167.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="UTPFC3A+" Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-52bc3130ae6so2888044e87.3 for ; Fri, 14 Jun 2024 13:29:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grsecurity.net; s=grsec; t=1718396947; x=1719001747; 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=UTPFC3A+uKjGuRD2lh2ATw35UwG/RgzlOoYE0jvAo7AO5nCORtraTm2h/luVAPsPMW ANt8FgYY22GzUC2Fj8DEY0SKMKsu+L1ZRxgYuaoS31NIYXOgxJUoU4jbuBeezOGNtfks pVSecSqOM0HnfCz0O0syLz1xxMJpgIESF1mKaPKVJQOkpZkmGfiR1T4F/YMyyW/xJybM MQZuUIkJFRVnGxndnyT9QJ3bUsoTgUn9usg1KuCYNDnHNFWdvMqjQ/GzrBsgJhpffI3t 5dTQvmQFK3XMJRlUn+uj8FNPlvP+PsynEZWMf7rvDN0nb3o53mdBdUtBu4qlMHIGR3sp BKlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718396947; x=1719001747; 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=lnslrr6xQAhZLABSWUomI9TcQ5m+KNipNrIoh5fb5OkGE1TlKt1s+sJ20m5xi+r9HZ umXol26jKSOqq1cGz9Q3SEjyB8gQgPY1zoARDUCXSIim4m8uXw9DFpVaB30YpyVCbyp5 +qaujnuNAvrvOV+gdAut2YrxlJu2OAGDDxXkarhbN8+JRgN7mRqfFU4zYu5wPgdykexI MesP4FQtQBQ98ARmH90kGtmTtXBxuRxWIW8d9I+4oAq1QIsQZvzifwUDt+2uDmjnCCba Ry0KXV2N8Sp738TKrEqIdqH5ahmmRm7uBN1RR+iHHWVTc9emVyZGoX8iKV9BNpIg//NW 4PbA== X-Gm-Message-State: AOJu0YwI/uTvi+QLu3bqgaVSGZU1Rtbd+oxPTB5zSfK4k4v8hesBS9ei UR9CZYgEfsEyPcKFoqUPf1n1VuyCLRaj6T10amrZvGPxhEa/xQ+SLvroszpfu6s= X-Google-Smtp-Source: AGHT+IEmOHUj5/rqXIRaMS021umCTO3mf09IyNq7zDBRYOl2LevWv78ow0q8ehdYQwLgXBKGkdDqPA== X-Received: by 2002:a05:6512:3da5:b0:516:d692:5e0b with SMTP id 2adb3069b0e04-52ca6e90530mr2785424e87.54.1718396947349; Fri, 14 Jun 2024 13:29:07 -0700 (PDT) Received: from bell.fritz.box (p200300f6af332a00214df27025e50a49.dip0.t-ipconnect.de. [2003:f6:af33:2a00:214d:f270:25e5:a49]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a6f56ed3685sm217474166b.126.2024.06.14.13.29.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 13:29:07 -0700 (PDT) From: Mathias Krause To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, Mathias Krause Subject: [PATCH v3 5/5] KVM: selftests: Test vCPU boot IDs above 2^32 Date: Fri, 14 Jun 2024 22:28:59 +0200 Message-Id: <20240614202859.3597745-6-minipli@grsecurity.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240614202859.3597745-1-minipli@grsecurity.net> References: <20240614202859.3597745-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++)