From nobody Thu Jul 23 21:53:27 2026 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.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 3660C2D3A60 for ; Wed, 24 Jun 2026 12:49:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782305345; cv=none; b=j28t1iM7IC+u70zy6aJKFUJGaUfBtATcb9LzOZ1CBzqepGqvxViY8ryNhYeENK8r4YexypLvtcdmQaDzydk/iuEtO53ilGbXdzo/4nTz6Zi+SbBRgMPhf4S2HLkAYx/nI0ySHnWWBiY+ZdFzbapIld2ccAn4Kw/2vRE4qUsnbXc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782305345; c=relaxed/simple; bh=/alkZ0R+ab5aHj8qZQWgA+0gt+na6QibfRu6SgsiOIA=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Sl5HPf1xr1aSVa9d3ZjEcRZ07G7c1UeNw25V8sWVoccojwR2jF6V3HJkE/0bccp1nDXv1fA9IHXhVTECsmkTkYZphifBbvwOK5AAVDqEMzbDrXHk94fuXmy5X63eBrHrFhFsjm4pFNdroJkZBoVz0ErqNsvN6HHPBdr2i6U/Gss= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=cT411ltz; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cT411ltz" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-490aaeabdb4so6459665e9.1 for ; Wed, 24 Jun 2026 05:49:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782305343; x=1782910143; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=NRKyao/0uQFhpjs6ysB7TR6a5cmOeMKMYRAxzRlp5+I=; b=cT411ltzaH2uFwgDEIGEyZgB8g7Rkrh88lO8UuHq+MisGRS0EHejjuLPMbHVZt7BNy 7E55KOgldNqTE9Z7PlucG+g4sF4cGagL1UbDOspXmz62RJCOMExqxYWMYX5S/mNU2IJ4 9ho5S4ACMHIKm1YAKRXlX1vqB7OPigvMjFBxFBuwBlngYpGBgpnZLtWLIEl7QNGAcq6c p6i+AziYpT8QXs/HoRkWgRUZgKwUe5g/vYtsKGWRhDG1YpbhlZWEWSK8IdRlGoc+tQ5V cb2ZkRZvtAyNDH6zaG2GqsizdQ95pDvKvmJP7zJsk+e4gsh9bfXKQL0fmxDAmWilvoSC cLVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782305343; x=1782910143; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=NRKyao/0uQFhpjs6ysB7TR6a5cmOeMKMYRAxzRlp5+I=; b=Ur6Juh6Q7OaJtV/fGYUnZIGlhrgQ3fop/kB9HQWd/nz6sn9bmBJ3NrNAF0PsHQbhym /UMj7vSmKbIE7amcIpLJ9IYqvnUPbpYgdT9b5JrR638uXtWac9Usdbxhe5brRB3OoGPQ HMXWXdDNbs9oSXTnKgX7Bkv0DbZSF/4SkXJE99eXVCs00YWng7Enqdc/vryPYdzh8rHy r8CPT3FEQIkeeGmcQaNxg0nZ9A84L8/7EBwDOpuq0LBSvQLdDpuBt/5g3WAW5HHzHarf 0IQu2HtBYDIQTSzLu+MO2Hw5QSI8WaS9LZeOVqSs/i9XLNXuy3t6JExW7nu3jVi0CxOt WDEg== X-Forwarded-Encrypted: i=1; AFNElJ9MbzesVtbvjx+MpO8tzShud+N+meuUw30BKr1RIUShJT0dV6cCOKtI3oVZoJNJRSnT0kCe1VTKntLEp5I=@vger.kernel.org X-Gm-Message-State: AOJu0Yy1OQOBoXAf3MkEN5ywpDyoq/zF3zkzDryIXHqpLjSnBXXxewWf PWOnv5jzwR0tjfkhvQNZoyfLioCf7ivymEV5MbecWcWelDO3i5qxDdxns4WZuJmQjMm3aA== X-Gm-Gg: AfdE7clYDl9PwtLJ5mE30d7nz0cXDHns1rj7p8wZW3CODIhAY8+ENd3ln6eL7nQhZj0 faveD0pcsZYBeFPgyHh/o9Q9kDGv/l4s6QnKSjGkk5Sdhw5T52ayn3mW1F0xPBH1sd89SvkT9jg XDML504P1BnZnOMHGv3bjC+srirvHC2q1odsozaM1JvAUzLKwkR12T0sXCh/isr3t5j2/96rP2o xmWrXN5dBZjPEBvo6lOo7e5rpQXWekdvqveO8Vw5mghpSfCiS1tNiwmDE/GWJehn6t8A25BLd1e Ki2I+DwnvLTXf29eG2TraUlETNb7SxlU7L1tQCjSm2hBWbGyVi7BZolnDagMJpzFugCkeoFcaWC UrirGmj69e7K4JP4p697XFI2xhXUAhe/mSGM4WArju3zI2DqoYFrBGow+nfZQcafz8/iZMXeUW6 Bi8koOOGAPcrxPVMMOYovIZjTdMtgAXfPl+9yC X-Received: by 2002:a05:600c:1d1e:b0:48a:906b:14ca with SMTP id 5b1f17b1804b1-49260873781mr43609065e9.20.1782305342275; Wed, 24 Jun 2026 05:49:02 -0700 (PDT) Received: from Dev-Null-MSI ([2a0d:3344:52ac:a808:98a4:4381:be45:536f]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4923fc47720sm797976155e9.0.2026.06.24.05.49.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jun 2026 05:49:01 -0700 (PDT) From: Yousef Alhouseen To: Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Cc: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, Yousef Alhouseen Subject: [PATCH] xen/gntalloc: validate grant count before allocation Date: Wed, 24 Jun 2026 14:47:45 +0200 Message-ID: <20260624124745.10073-1-alhouseenyousef@gmail.com> X-Mailer: git-send-email 2.54.0 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 Content-Type: text/plain; charset="utf-8" gntalloc_ioctl_alloc() allocates the grant-id array before checking whether the requested count can fit within the global grant limit. Counts above the limit cannot succeed, so reject them before the user-controlled allocation size reaches kcalloc(). The locked limit check also adds a u32 count to signed global counters. Rewrite it as a subtraction-based range check so the arithmetic cannot wrap around the limit. While there, cast the count before advancing the per-file index so the page-size multiplication is done in 64-bit arithmetic. Signed-off-by: Yousef Alhouseen --- drivers/xen/gntalloc.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/xen/gntalloc.c b/drivers/xen/gntalloc.c index eadedd1e9..ba6a25a09 100644 --- a/drivers/xen/gntalloc.c +++ b/drivers/xen/gntalloc.c @@ -272,6 +272,7 @@ static long gntalloc_ioctl_alloc(struct gntalloc_file_p= rivate_data *priv, int rc =3D 0; struct ioctl_gntalloc_alloc_gref op; uint32_t *gref_ids; + int limit_snapshot; =20 pr_debug("%s: priv %p\n", __func__, priv); =20 @@ -280,6 +281,12 @@ static long gntalloc_ioctl_alloc(struct gntalloc_file_= private_data *priv, goto out; } =20 + limit_snapshot =3D READ_ONCE(limit); + if (limit_snapshot < 0 || op.count > (uint32_t)limit_snapshot) { + rc =3D -ENOSPC; + goto out; + } + gref_ids =3D kcalloc(op.count, sizeof(gref_ids[0]), GFP_KERNEL); if (!gref_ids) { rc =3D -ENOMEM; @@ -292,14 +299,16 @@ static long gntalloc_ioctl_alloc(struct gntalloc_file= _private_data *priv, * are about to enforce, removing them here is a good idea. */ do_cleanup(); - if (gref_size + op.count > limit) { + limit_snapshot =3D READ_ONCE(limit); + if (limit_snapshot < 0 || gref_size > limit_snapshot || + op.count > (uint32_t)(limit_snapshot - gref_size)) { mutex_unlock(&gref_mutex); rc =3D -ENOSPC; goto out_free; } gref_size +=3D op.count; op.index =3D priv->index; - priv->index +=3D op.count * PAGE_SIZE; + priv->index +=3D (uint64_t)op.count * PAGE_SIZE; mutex_unlock(&gref_mutex); =20 rc =3D add_grefs(&op, gref_ids, priv); --=20 2.54.0