From nobody Sun Dec 14 06:20:06 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 64B3EC00140 for ; Mon, 15 Aug 2022 18:38:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232043AbiHOSiA (ORCPT ); Mon, 15 Aug 2022 14:38:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243575AbiHOSgq (ORCPT ); Mon, 15 Aug 2022 14:36:46 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFC732DA92; Mon, 15 Aug 2022 11:23:15 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0961E6068D; Mon, 15 Aug 2022 18:23:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 03EB8C433C1; Mon, 15 Aug 2022 18:23:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660587792; bh=jhB3BcC4WMae6aGnSw8cXRASOVkW/NucMwW8lJkgSus=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CM3qRwLO9h9THO7BvI5+T7d28id8iR+JV/LPBweL0M/ZZgsrfxt9ylY4XSDxQNdN/ CpVmEYsfNaHLt6lFSRVLkzvWLxKLkhqkssE8lO7czx0wrxCcbZvucmG1hBOn+x0V+y V5Sc2DcgaG5pKtqsUUg8taTEGL501b/GoE/kbZFk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Len Baker , Jonathan Cameron , Sasha Levin Subject: [PATCH 5.15 197/779] drivers/iio: Remove all strcpy() uses Date: Mon, 15 Aug 2022 19:57:21 +0200 Message-Id: <20220815180345.695878214@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Len Baker [ Upstream commit d722f1e06fbc53eb369b39646945c1fa92068e74 ] strcpy() performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. So, remove all the uses and add devm_kstrdup() or devm_kasprintf() instead. Also, modify the "for" loop conditions to clarify the access to the st->orientation.rotation buffer. This patch is an effort to clean up the proliferation of str*() functions in the kernel and a previous step in the path to remove the strcpy function from the kernel entirely [1]. [1] https://github.com/KSPP/linux/issues/88 Signed-off-by: Len Baker Link: https://lore.kernel.org/r/20210815174204.126593-1-len.baker@gmx.com Signed-off-by: Jonathan Cameron Signed-off-by: Sasha Levin --- drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c | 36 +++++++++++++--------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c b/drivers/iio/imu/i= nv_mpu6050/inv_mpu_magn.c index f282e9cc34c5..6aee6c989485 100644 --- a/drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c @@ -261,6 +261,7 @@ int inv_mpu_magn_set_rate(const struct inv_mpu6050_stat= e *st, int fifo_rate) */ int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st) { + struct device *dev =3D regmap_get_device(st->map); const char *orient; char *str; int i; @@ -279,22 +280,27 @@ int inv_mpu_magn_set_orient(struct inv_mpu6050_state = *st) st->magn_orient.rotation[4] =3D st->orientation.rotation[1]; st->magn_orient.rotation[5] =3D st->orientation.rotation[2]; /* z <- -z */ - for (i =3D 0; i < 3; ++i) { - orient =3D st->orientation.rotation[6 + i]; - /* use length + 2 for adding minus sign if needed */ - str =3D devm_kzalloc(regmap_get_device(st->map), - strlen(orient) + 2, GFP_KERNEL); - if (str =3D=3D NULL) + for (i =3D 6; i < 9; ++i) { + orient =3D st->orientation.rotation[i]; + + /* + * The value is negated according to one of the following + * rules: + * + * 1) Drop leading minus. + * 2) Leave 0 as is. + * 3) Add leading minus. + */ + if (orient[0] =3D=3D '-') + str =3D devm_kstrdup(dev, orient + 1, GFP_KERNEL); + else if (!strcmp(orient, "0")) + str =3D devm_kstrdup(dev, orient, GFP_KERNEL); + else + str =3D devm_kasprintf(dev, GFP_KERNEL, "-%s", orient); + if (!str) return -ENOMEM; - if (strcmp(orient, "0") =3D=3D 0) { - strcpy(str, orient); - } else if (orient[0] =3D=3D '-') { - strcpy(str, &orient[1]); - } else { - str[0] =3D '-'; - strcpy(&str[1], orient); - } - st->magn_orient.rotation[6 + i] =3D str; + + st->magn_orient.rotation[i] =3D str; } break; default: --=20 2.35.1