Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2810471rwb; Mon, 15 Aug 2022 11:48:25 -0700 (PDT) X-Google-Smtp-Source: AA6agR7DqZrVB6x+XCpy3Umgw3I9MSJ1huoVipCcn14AR/sfEPtSBX9PgFuTSkbsVd+7nNoj/vdt X-Received: by 2002:a05:6402:64c:b0:43c:ea8e:85d6 with SMTP id u12-20020a056402064c00b0043cea8e85d6mr16006665edx.269.1660589305073; Mon, 15 Aug 2022 11:48:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660589305; cv=none; d=google.com; s=arc-20160816; b=e6IOFMh+DKMkY6sjEdCIG8Vobp4XLTy3KtCWggFfcp0p4mwZ/tcNAPLBTHd6DAlCPq vrnQ9kU7iGg/LOi+ZBmf90u8QK73N1foTUMfhKHu5fwopN9g3W4V1iKkdtac/ZQWoQW7 P4mmmXPs41iAxrD58+FxMChS8N1fSFs6RyEvjaPnTWMSsjBCCwvCt6d6LZZxrfbzM43o HbjD2JcbYYMVqcw1aaT0GaGvCZsc6gqbHXhiKP0xE3le9w3mDWJK8ywDGctovPCWcG4a oAVLRIFZjBrv78fXEU71RdAMqkZAw7xyGrIvkHn61Q1FoEV77O4dgqCPtSRhm1/ctLVD IWyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=DsHi+x9f4Vewf/24SSI671WWP93JlXMjZXNnH0EWc6A=; b=oMkGJ5K6PKLxOIyyw/OGdstz7dAnCpQbxNOh1M68yQafYJeuMNACzb+1JiqCIWXOsa +lqjMGvabOoSPNHgGrQwOp5FcBbrqDKWxC0nGl7y/Y2/ISNsuVjipBy+8ehgPRz53x7o Bgw6eoFPG5+JPQdhPFrYYHFNdJzAw8g3gZouLMbcraCgswrvBD36XdxkUJChioXRSERh bzPbB46J4jb8Cc6hSVDo/ZcVFYAS1ajjzKgSoZ9KvCFjzio2z4ru4js2AASnVo3hDq11 gAHnwWWV3WpRmgBvPzG+YoOE8LdTzb0jT12aa7qfSKD7QnBPzWgiv0PbVppX2tYNnRq5 2csQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=CM3qRwLO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y7-20020a056402270700b0043beaa7f1b3si9052821edd.247.2022.08.15.11.47.59; Mon, 15 Aug 2022 11:48:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=CM3qRwLO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241918AbiHOSh7 (ORCPT + 99 others); Mon, 15 Aug 2022 14:37:59 -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-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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/inv_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_state *st, int fifo_rate) */ int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st) { + struct device *dev = 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] = st->orientation.rotation[1]; st->magn_orient.rotation[5] = st->orientation.rotation[2]; /* z <- -z */ - for (i = 0; i < 3; ++i) { - orient = st->orientation.rotation[6 + i]; - /* use length + 2 for adding minus sign if needed */ - str = devm_kzalloc(regmap_get_device(st->map), - strlen(orient) + 2, GFP_KERNEL); - if (str == NULL) + for (i = 6; i < 9; ++i) { + orient = 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] == '-') + str = devm_kstrdup(dev, orient + 1, GFP_KERNEL); + else if (!strcmp(orient, "0")) + str = devm_kstrdup(dev, orient, GFP_KERNEL); + else + str = devm_kasprintf(dev, GFP_KERNEL, "-%s", orient); + if (!str) return -ENOMEM; - if (strcmp(orient, "0") == 0) { - strcpy(str, orient); - } else if (orient[0] == '-') { - strcpy(str, &orient[1]); - } else { - str[0] = '-'; - strcpy(&str[1], orient); - } - st->magn_orient.rotation[6 + i] = str; + + st->magn_orient.rotation[i] = str; } break; default: -- 2.35.1