Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp2044913pxt; Sun, 8 Aug 2021 09:36:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwrgjm54udvVD9PwldXnCNLI04kq5lNqGD6M8zipTvITz3+AFLldhfFbxjgOJ6U3RdruMhb X-Received: by 2002:a05:6402:1385:: with SMTP id b5mr24485964edv.276.1628440615423; Sun, 08 Aug 2021 09:36:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628440615; cv=none; d=google.com; s=arc-20160816; b=CZjuYjMd7IY54rGmJM2SYaevW88bRO1FTxW/JI7+5rEYy2nDxAFs6ucMyQZjJQE9dU xenFUUBkaACUY1WJdRBymZZy00WiPvdrEwmKFPCORcZ0YFbvSHiDdOLi0VqQP42Y6WGZ 7F8pHUuPgQqcL9MH74nedew7Zzr+QDNUsKdv7IcVxlFKUcwK611GJMCz8l00GnrYLv0X jE6/k8lJbcc+hEsvc5l6I9BnaaA9ZAGySaFz42mOvTgyaX3gqACazu8jI2/8Tm79jNkB xSvhLBzNJ/fvrrC8kLquqXeaMqxCEgf+Rx/qg3nXE5BFwjMZZAy6h/tgb0FB5+VPURyO nOrQ== 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 :references:in-reply-to:message-id:subject:cc:to:from:date; bh=XbGlzApoGfANGHKoe46KCzJJFu86VzuiQqV2ccgqgsg=; b=gPJBx+Rvo2Z4mr49rYBL1DqtZzhUzszITB08eA0zuy00BPDCN3pIhVJkaIQsZ5P8vd CNJgvh2/1h1bgBnzZ7PVxByegkMr5IFvHcrYMlq5hAzrZeme+JUPww+F/4FWdKT2L8x2 hPkKZNyCwQvNxT7w9anys9PSlNxcA2LEvpa6ziGHDuUipkRydZUbSqVqE/DMvqrrVYnc ZQEgjLJwKheoR1vq2lJj4qytCEhSbulIsuCQLwDYpM8GLM8jkAh2LK5AdY3BuDLFiAGB NhdVAiXK3PObnea+BhYx+Z6mLYVde29NDPHaV641Y3Q9jmAkhx8CS9yofrnl3XWfRS1a 6i8A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c10si619169edy.151.2021.08.08.09.36.31; Sun, 08 Aug 2021 09:36:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231544AbhHHQWg (ORCPT + 99 others); Sun, 8 Aug 2021 12:22:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:45944 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230049AbhHHQWe (ORCPT ); Sun, 8 Aug 2021 12:22:34 -0400 Received: from jic23-huawei (cpc108967-cmbg20-2-0-cust86.5-4.cable.virginm.net [81.101.6.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5001060F35; Sun, 8 Aug 2021 16:22:14 +0000 (UTC) Date: Sun, 8 Aug 2021 17:25:03 +0100 From: Jonathan Cameron To: Len Baker Cc: Lars-Peter Clausen , Kees Cook , linux-hardening@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] drivers/iio: Remove all strcpy() uses in favor of strscpy() Message-ID: <20210808172503.5187cd24@jic23-huawei> In-Reply-To: <20210807152225.9403-1-len.baker@gmx.com> References: <20210807152225.9403-1-len.baker@gmx.com> X-Mailer: Claws Mail 4.0.0 (GTK+ 3.24.30; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 7 Aug 2021 17:22:25 +0200 Len Baker wrote: > 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. The safe replacement is strscpy(). > > 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 Applied to the togreg branch of iio.git and pushed out as testing so 0-day can poke at it and see if we missed anything. Thanks, Jonathan > --- > Changelog v1 -> v2 > - Modify the commit changelog to inform that the motivation of this > patch is to remove the strcpy() function from the kernel entirely > (Jonathan Cameron). > > drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c | 12 +++++++----- > 1 file changed, 7 insertions(+), 5 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..3a6aa1c4bf6c 100644 > --- a/drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c > +++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c > @@ -264,6 +264,7 @@ int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st) > const char *orient; > char *str; > int i; > + size_t n; > > /* fill magnetometer orientation */ > switch (st->chip_type) { > @@ -282,17 +283,18 @@ int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st) > 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); > + n = strlen(orient) + 2; > + str = devm_kzalloc(regmap_get_device(st->map), n, > + GFP_KERNEL); > if (str == NULL) > return -ENOMEM; > if (strcmp(orient, "0") == 0) { > - strcpy(str, orient); > + strscpy(str, orient, n); > } else if (orient[0] == '-') { > - strcpy(str, &orient[1]); > + strscpy(str, &orient[1], n); > } else { > str[0] = '-'; > - strcpy(&str[1], orient); > + strscpy(&str[1], orient, n - 1); > } > st->magn_orient.rotation[6 + i] = str; > } > -- > 2.25.1 >