From nobody Thu Apr 2 02:40:42 2026 Received: from mail-vs1-f51.google.com (mail-vs1-f51.google.com [209.85.217.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 AB1703D170D for ; Mon, 30 Mar 2026 12:52:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.217.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774875139; cv=none; b=DoG5XbCtB8muVLIjD6DVc75DSO5Yn2rCtleagfUI0moe15a9VmUfeundf1PB7RzMzOI5xoYGPjoArKw2BBLQ9k+zj8pYpmxdq5MXC8T1x41slloFKiXUnTizZ63m4vSHbttJ/SeG0zOGzhUAV6ND2lC5dDX84d41uMxhuCXuaSY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774875139; c=relaxed/simple; bh=X0XeqVk1okPDExPdxveHKfJoDl26uOIaeLMRZyIoG0U=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=hA2CddFRCTDz6ARhiRa0C9qs+uMxh90842FhKng+MQpDIu8rlIAsBp8UVvIHz48nVrcvZrxJst6oyjgX8NOe44o0Nut/RRIi7wNPkTKDiH9hdME/42R7ZkpWPtK0YYEnTkElDb+Rt9Fvpwn8FlCGa5uPniTaZ0Us4HVf/2UDcw0= 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=QJ20CY/V; arc=none smtp.client-ip=209.85.217.51 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="QJ20CY/V" Received: by mail-vs1-f51.google.com with SMTP id ada2fe7eead31-602903ad849so1519956137.3 for ; Mon, 30 Mar 2026 05:52:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774875137; x=1775479937; 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=2T5sC1X4d4lvAdzHYW3GejS6hGTQduK0sRiN708dk4s=; b=QJ20CY/VupWwefOFzUflZGWD7YDVGN0rsW0a6JkR5HOAnCeXOqlbx7Ia7qZ42J5OiY z7FaZ/ZiDUAM53+CIjxKEprQlT0U9VocUzppHlBHyE7D9k9YcuuZuIixL2DKow9uzt7n rrG6lHOq7osTr3ITC4ORahVEVInVkn4+INIih0Sk/z0juCRmpU1Hjd5dE/IpR9hcFNDr fDaPVsCCqkyPLpLaR9320C0BATVMLpVCLfKONUANa8oWNz65GOp6/f8cjCCZhRlxKnAn f2bMgmA139VLFAeU5WqpiVJSltC1VKcM3n7WMxi8jdySXOOG5E+BA34XUEIjrnkAkrOO lZ7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774875137; x=1775479937; 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=2T5sC1X4d4lvAdzHYW3GejS6hGTQduK0sRiN708dk4s=; b=cbAST3qxRT+oVYY+eitsRF+QWM/qBgKeuLswrUKpecvlqgSe5JU6GaloTFgsAV95Eh kATri+WB8DPoYc6xXeY6bJYAznTG83FIImHwaQl+kGqV5oRf+WBeje7pGf4+bEcBNDKH 2V7C9oOQe0wdSjIzU+ok+pGMY1AzDzePO7kIOv4c/Kwkz0fYR1EXoVwI/AotHEdD34ew 8GbwTsUR0BZk6YwKpVP0gbcb3Mbaqbgwko6Hh7JWZ+jJpypwCFLDh1bDYnqRvdOI8EeC xPXcKUcDPDbyhoD+os3YCcfRm1pnUkirYktYSMNVLZWd4bqkoYOde7fdZ6BaQAHXkJnn Qqgw== X-Forwarded-Encrypted: i=1; AJvYcCUcNmpJw1t6Wp5jxwMG2zEYg7Y1DjZPMs4bc6/U1k/+Un4iVl6aq8x8bVXqkvCw5YcgamvLMe4AxOF0Uy4=@vger.kernel.org X-Gm-Message-State: AOJu0YziVn6uIsGujicFK85eOsXfn0Uy4epKpoUIu46l1ppjse7VUzkn SrTpojOQItv03A5cKEwThr2pnpjf/T/E+nbohEy3/kkejr9NsxjZZV4W X-Gm-Gg: ATEYQzwReXDwTACnNBnjpYdIfn+o+NTHuFn1zQHSnjFpukbvLfMRHmYS2zK/EGjJOlZ H53cVnWGmmxMRNBvz0krFbOie47Swb+E7kA3kJKQv4tKTuHrTneQz1+QAcYoyXgoWfMsH6/vUBy uyLylR67eHzrnsq6YiU6Fhw59yaK2qepAQqDtwQouL1gGwlP5qXbopgivDckqpBHt5VfFjCpKPI GJqT0qB064MS3UsttM2bR1Qh+xkeGbklUB1xSXwWexJyuBS8pLaVj5FEoF3X381gFhd5yBx6qMD ZFNrCT36ruVC+aqeVMM0S4iFdjCsYJi/m8we3G+Q5/sMcfm8ZbRg3TU5/Do98LMOFSo9382JgNp JXi4uqN2Pca+4dqsW7WlzZ02FTZLfNPonUix2PIDtmeTGMPavIP5U0f4J+TRB/aGT6bsoQZTtcY vf+0YJi27DOk6qRdrd2eEfXDkT X-Received: by 2002:a05:6102:26d5:b0:602:87b9:89ba with SMTP id ada2fe7eead31-604f925ae38mr3523377137.19.1774875136516; Mon, 30 Mar 2026 05:52:16 -0700 (PDT) Received: from localhost.localdomain ([2a09:bac6:d6da:aa::11:1d6]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-9539e4420d9sm6888389241.8.2026.03.30.05.52.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Mar 2026 05:52:15 -0700 (PDT) From: Sebastian Josue Alba Vives To: gregkh@linuxfoundation.org Cc: marvin24@gmx.de, linux-staging@lists.linux.dev, linux-tegra@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, =?UTF-8?q?Sebasti=C3=A1n=20Alba=20Vives?= , kernel test robot Subject: [PATCH v3] staging: nvec: validate battery response length before memcpy Date: Mon, 30 Mar 2026 06:52:00 -0600 Message-ID: <20260330125200.820693-1-sebasjosue84@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Sebasti=C3=A1n Alba Vives In nvec_power_notifier(), the response length from the embedded controller is used directly as the size argument to memcpy() when copying battery manufacturer, model, and type strings. The destination buffers (bat_manu, bat_model, bat_type) are fixed at 30 bytes, but res->length is a u8 that can be up to 255, allowing a heap buffer overflow. Additionally, if res->length is less than 2, the subtraction res->length - 2 wraps around as an unsigned value, resulting in a large copy that corrupts kernel heap memory. Introduce NVEC_BAT_STRING_SIZE to replace the hardcoded buffer size, store res->length - 2 in a local copy_len variable for clarity, and add bounds checks before each memcpy to ensure the copy length does not exceed the destination buffer and that res->length is at least 2 to prevent unsigned integer underflow. Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/202603301722.axpoITcy-lkp@int= el.com/ Tested-by: Marc Dietrich Cc: stable@vger.kernel.org Signed-off-by: Sebasti=C3=A1n Alba Vives --- v3: - Fix build error: add missing closing brace for TYPE case compound statement (kernel test robot) v2: - Introduce NVEC_BAT_STRING_SIZE constant (Marc Dietrich) - Store res->length - 2 in local copy_len variable (Marc Dietrich) - Use NVEC_BAT_STRING_SIZE in strncmp call for consistency drivers/staging/nvec/nvec_power.c | 42 +++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/drivers/staging/nvec/nvec_power.c b/drivers/staging/nvec/nvec_= power.c index 2faab9fde..30719e142 100644 --- a/drivers/staging/nvec/nvec_power.c +++ b/drivers/staging/nvec/nvec_power.c @@ -19,6 +19,7 @@ #include "nvec.h" =20 #define GET_SYSTEM_STATUS 0x00 +#define NVEC_BAT_STRING_SIZE 30 =20 struct nvec_power { struct notifier_block notifier; @@ -38,9 +39,9 @@ struct nvec_power { int bat_temperature; int bat_cap; int bat_type_enum; - char bat_manu[30]; - char bat_model[30]; - char bat_type[30]; + char bat_manu[NVEC_BAT_STRING_SIZE]; + char bat_model[NVEC_BAT_STRING_SIZE]; + char bat_type[NVEC_BAT_STRING_SIZE]; }; =20 enum { @@ -192,26 +193,41 @@ static int nvec_power_bat_notifier(struct notifier_bl= ock *nb, case TEMPERATURE: power->bat_temperature =3D res->plu - 2732; break; - case MANUFACTURER: - memcpy(power->bat_manu, &res->plc, res->length - 2); - power->bat_manu[res->length - 2] =3D '\0'; + case MANUFACTURER: { + size_t copy_len =3D res->length - 2; + + if (res->length < 2 || copy_len > NVEC_BAT_STRING_SIZE - 1) + break; + memcpy(power->bat_manu, &res->plc, copy_len); + power->bat_manu[copy_len] =3D '\0'; break; - case MODEL: - memcpy(power->bat_model, &res->plc, res->length - 2); - power->bat_model[res->length - 2] =3D '\0'; + } + case MODEL: { + size_t copy_len =3D res->length - 2; + + if (res->length < 2 || copy_len > NVEC_BAT_STRING_SIZE - 1) + break; + memcpy(power->bat_model, &res->plc, copy_len); + power->bat_model[copy_len] =3D '\0'; break; - case TYPE: - memcpy(power->bat_type, &res->plc, res->length - 2); - power->bat_type[res->length - 2] =3D '\0'; + } + case TYPE: { + size_t copy_len =3D res->length - 2; + + if (res->length < 2 || copy_len > NVEC_BAT_STRING_SIZE - 1) + break; + memcpy(power->bat_type, &res->plc, copy_len); + power->bat_type[copy_len] =3D '\0'; /* * This differs a little from the spec fill in more if you find * some. */ - if (!strncmp(power->bat_type, "Li", 30)) + if (!strncmp(power->bat_type, "Li", NVEC_BAT_STRING_SIZE)) power->bat_type_enum =3D POWER_SUPPLY_TECHNOLOGY_LION; else power->bat_type_enum =3D POWER_SUPPLY_TECHNOLOGY_UNKNOWN; break; + } default: return NOTIFY_STOP; } --=20 2.43.0