From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 0ADD3368D55 for ; Thu, 30 Jul 2026 04:12:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785384765; cv=none; b=CYSVKDRblVQcpmVT+IfOTCHwGPsyW7cCCwNHljIkuk1RsX2nmqadOMV+YNTcM0QzBdnqjtB3Wsuc1pFStiK0FbFS8rHz/dNgP8CLjYvMx9U7Ooj4JK9CMZBSYzGjsycHDfv9OigUHr8rqmhpUdp7fJ+Bt4hJSv8DRltq71wRQ0o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785384765; c=relaxed/simple; bh=A8xI8/uO0nyr4mkTXdCUQtJZL8/Y+4SmdbbBQ+cYk9s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lSLpn+ExjCIERda3FT6RDdwHPJxXfwjpdMjgszsnMjQQlfTSGqr5jSG+plqvGJe9QLB49cf9D6O3lf6CVlIfrmVC7iVspT7kl2NPqRedLYWBRfkAJL9vk4obrDxEtmQDsKLDy7OM3hgs6LDbx2tO0Fu3+ByyYwk1XUJiMwf4XIQ= 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=e6uPMCxE; arc=none smtp.client-ip=209.85.210.179 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="e6uPMCxE" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-84864086bfeso1728223b3a.1 for ; Wed, 29 Jul 2026 21:12:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785384763; x=1785989563; 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:content-type; bh=jY9HS5XLRtv5Lc9POKc9VtUHE0COzAXpfC43+pdnnFk=; b=e6uPMCxEVuaZUQWIGK75wVQKototuFkU2BB5IVcxa17BYoJIZIyygw8VYVV9q1P7Qn OhkrwxwQGT9tU3b16qMTDU1Ta09ivox3jtSI2ocksXP31X4FBd+MXsL7ZruuroWAiEWm 7p+AirUJfFlktyxdVHX+LHVdRMxOYJ3jYy0P9Vg5NmkRXn0Wwj7Yu9DOqdfMegVFLfUf 91ciLuS2R6qfcOpiFAETFqh71Cs0pxFXT1LAbiiTM3ZHf/8EjMw0YKvPKf6riWpHbc/4 HYtbEImgnJRmnFKE4GvpIDUy8RUOeIis5Dn549TW5X6eT/BQY/haMhD3VHCU2QpmCKiz +czQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785384763; x=1785989563; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=jY9HS5XLRtv5Lc9POKc9VtUHE0COzAXpfC43+pdnnFk=; b=pNf60x+9V3fEOMY7TEKaYEPNclvjo7y89sHmgQqLJ3tu+tv/SYJcILAgz5bBTdxSkv CA0AckaQ5VxNs0y8dpFk4SO/I1DSOyjW09bru64U7eOsTSvcmgPG13Og6b48KDvbEPiZ vri10ZsGgcu1sewr0fTM2gTs3elYPKcpMJAg+VPWVby7R0d6cdtnDH9mAz15CDBquOsY Z0ZGgTi3ELxeqyCf42fJ2grHG2de2uWX2YC/JWdSFWdaqekJY70hgpvKPVJvjmhPoWuc eYiJgsFIjDoV7TlyzY4QljpUfI2BuzBpn6PQFzxRQ4eeK2sV3ryEYnOux+qTBcDpcRka 5A1g== X-Forwarded-Encrypted: i=1; AHgh+Rp5HdRhyrnMGasEQa+qbaqD4c0xpPfgz8ZaxG50qTXBKX2z86kW7aZocWF2wYLJ/GlULyYrkoU+DBRe4ZQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yy/mMyMdM8Sa7YCReOiNDNKn8Xi2QQ3urwgiAHtyZNLHHkGar4R sWME1E3ZlR4foY9+U1FNOf42Jv/DWQsvKZlCowndPbAOCUEAdBTF4BU8 X-Gm-Gg: AR+sD12C2M9abcmJXBpQz4F6cml2fWUvYPjuVIHOUsZxxOAXrub/Dr5RoiqyN9r0iIB fTfkc8GAPbybyFfA1CBoGqOwO7a+PUG4gQA9bASy+GmNt7qrD886bi9+tFBYgitHuxoOvVQaqYR Vd7RaZ2o9wIK4v0moy/5ktHCksGObzGhGsY0iA6CxvSbed0byoSCnMzlPqBzqyM3WuMWuQZTKHl zYaDjeb2totHnFzn/2XFCkioH96H9J5l+Ig0LTzqa6t3XEkGSVMwEbUIP2sWS19uKU+LVswq/0+ zeO50w469+QmPGSoM274bnkq9pcurBLFv69xjSWFH4nExO8GRv64yCqerJ+8EmRvJ23JRKIvH0F nelyWDtLTp9tnH+y9xf2vptgoQkxJvYtnrhEp9YErOCoHOiDUR4YIodiTAch6fnqGo0rdUQK9tH X2dt+ekh5XN+Ko1BaMYVN4aLjtwYRhldPJRiWsSKwV96Q/hVb5bLI7l7TDrNKIeLlTa/HCUmDPM DiAGc8= X-Received: by 2002:a05:6a21:b90:b0:3c6:55f5:269b with SMTP id adf61e73a8af0-3c900a07f07mr911836637.66.1785384763304; Wed, 29 Jul 2026 21:12:43 -0700 (PDT) Received: from ArchLinux.taild01bf5.ts.net ([115.76.55.213]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-13e7262c23bsm13906182c88.1.2026.07.29.21.12.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Jul 2026 21:12:42 -0700 (PDT) From: Dang Vu Duc Hien To: marvin24@gmx.de, gregkh@linuxfoundation.org Cc: ac100@lists.launchpad.net, linux-tegra@vger.kernel.org, linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org, luquijeffrey@gmail.com, Dang Vu Duc Hien Subject: [PATCH] staging: nvec: fix out-of-bounds string copies Date: Thu, 30 Jul 2026 11:12:20 +0700 Message-ID: <20260730041220.1064608-1-dvdh12707@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The function `nvec_power_bat_notifier()` assumes that `res->length` is both >= 2 and within the battery info buffers. Technically, `res->length` is provided by the embedded controller (EC) and is unverified (ie. a malformed response). If `res->length` < 2, the expression `res->length - 2` evaluates to a massive size_t value which is passed to `memcpy()`, causing severe memory corruption. Conversely, `res->length - 2` being larger than the buffer also causes an out-of-bounds write. Introduce helper function `nvec_bat_str_copy()` to safely handle these string copies by: 1. Ensuring that it returns an error to the upper layer along with a `dev_warn()` message when `res->length` < 2. 2. Ensuring that it provides toleration by copying as much as it can along with a `dev_warn()` when `res->length - 2` is too big. 3. Guaranteeing a properly sized, null-terminated destination string if the copy succeeds. Signed-off-by: Dang Vu Duc Hien --- Note: Coincidentally, I have been working on this exact issue earlier and had this patch prepared just before Lucas Jeffrey submitted his RFC. I am sending it here to answer his protocol validation question with code. --- drivers/staging/nvec/nvec_power.c | 41 ++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/drivers/staging/nvec/nvec_power.c b/drivers/staging/nvec/nvec_power.c index c514d51a9e57..12699515356d 100644 --- a/drivers/staging/nvec/nvec_power.c +++ b/drivers/staging/nvec/nvec_power.c @@ -117,6 +117,32 @@ static void get_bat_mfg_data(struct nvec_power *power) } } +static int nvec_bat_str_copy(struct nvec_power *power, + struct bat_response *res, char *dst, + unsigned int dst_size, const char *prop_name) +{ + unsigned int len; + + if (res->length < 2) { + dev_warn(power->nvec->dev, + "battery %s response length too short (%u)\n", + prop_name, res->length); + return -EINVAL; + } + + len = res->length - 2; + if (len >= dst_size) { + dev_warn(power->nvec->dev, + "battery %s string too long (%u)\n", + prop_name, len); + len = dst_size - 1; + } + + memcpy(dst, &res->plc, len); + dst[len] = '\0'; + return 0; +} + static int nvec_power_bat_notifier(struct notifier_block *nb, unsigned long event_type, void *data) { @@ -193,16 +219,19 @@ static int nvec_power_bat_notifier(struct notifier_block *nb, power->bat_temperature = res->plu - 2732; break; case MANUFACTURER: - memcpy(power->bat_manu, &res->plc, res->length - 2); - power->bat_manu[res->length - 2] = '\0'; + if (nvec_bat_str_copy(power, res, power->bat_manu, + sizeof(power->bat_manu), "manufacturer") < 0) + break; break; case MODEL: - memcpy(power->bat_model, &res->plc, res->length - 2); - power->bat_model[res->length - 2] = '\0'; + if (nvec_bat_str_copy(power, res, power->bat_model, + sizeof(power->bat_model), "model") < 0) + break; break; case TYPE: - memcpy(power->bat_type, &res->plc, res->length - 2); - power->bat_type[res->length - 2] = '\0'; + if (nvec_bat_str_copy(power, res, power->bat_type, + sizeof(power->bat_type), "type") < 0) + break; /* * This differs a little from the spec fill in more if you find * some. -- 2.55.0