From nobody Fri Oct 3 08:47:40 2025 Received: from out-180.mta0.migadu.com (out-180.mta0.migadu.com [91.218.175.180]) (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 2FA5C800 for ; Wed, 3 Sep 2025 00:30:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756859428; cv=none; b=WlEUSe4m2oCx5AO7TXvfsJzR7egV69x16mg7tf4ymfgNdUgT8RbAbh6y5zYkap7FYCulo5nRDq6v0NJO9kt+5/6t+cgAMRWYaBg7Iw/zofBAl0klm8UphRXNl9LzxSEXOmHPuiNocXbIYR/TOwKeqUwTcL3W2DOcQLlHgGvVx2w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756859428; c=relaxed/simple; bh=avxoggIy7iNMq1V9YTUJVFJ2AwIXBC8XWfTRiq0XybI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=df0zuS5yGJV5HyXKKEaUZhqvI9BQyhS0WdIZuNX7JeJGEGB3Ju7VJCbwB7WtZuxCzhZ1pMtYTBMOhbpDbvKfa2gxtf4AUaYMuAr+RRm83TRGt+AI3jtYpGVsjLe7/LgJKFfapW430D6IMs9Fv70C9Wk5d66egmaw1pQyLrhMgCo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=kmLSNv31; arc=none smtp.client-ip=91.218.175.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="kmLSNv31" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1756859414; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=oATve04/PpCSXjt9BZQOkk2Qc8tlxwWxSK5sGt1qgbY=; b=kmLSNv31AHeFXz4x//T4DbkgO+0bwAqi0uieRQ4eRXYRhIjH53r/biqthzW7KuMQeSDntt xusOfsKlLb8wrFH5dftIh+JA+8imUMGm6VysmuXVdG+gyUlTeX1L6VaWNuyC52Z+MHmAq9 UzRbUuMnhFQ9hdnvD+Yv9J7Cg/SEdhk= From: Thorsten Blum To: Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" Cc: Thorsten Blum , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] KVM: SVM: Replace kzalloc() + copy_from_user() with memdup_user() Date: Wed, 3 Sep 2025 02:29:50 +0200 Message-ID: <20250903002951.118912-1-thorsten.blum@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Replace kzalloc() followed by copy_from_user() with memdup_user() to improve and simplify svm_set_nested_state(). Return early if an error occurs instead of trying to allocate memory for 'save' when memory allocation for 'ctl' already failed. Signed-off-by: Thorsten Blum --- arch/x86/kvm/svm/nested.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index b7fd2e869998..826473f2d7c7 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1798,17 +1798,15 @@ static int svm_set_nested_state(struct kvm_vcpu *vc= pu, if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE) return -EINVAL; =20 - ret =3D -ENOMEM; - ctl =3D kzalloc(sizeof(*ctl), GFP_KERNEL); - save =3D kzalloc(sizeof(*save), GFP_KERNEL); - if (!ctl || !save) - goto out_free; - - ret =3D -EFAULT; - if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl))) - goto out_free; - if (copy_from_user(save, &user_vmcb->save, sizeof(*save))) - goto out_free; + ctl =3D memdup_user(&user_vmcb->control, sizeof(*ctl)); + if (IS_ERR(ctl)) + return PTR_ERR(ctl); + + save =3D memdup_user(&user_vmcb->save, sizeof(*save)); + if (IS_ERR(save)) { + kfree(ctl); + return PTR_ERR(save); + } =20 ret =3D -EINVAL; __nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl); --=20 2.51.0