Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp5081351pxv; Wed, 28 Jul 2021 02:37:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz3jgw5OUitOO6ED+67prhVB7QC6b8P5wXFVtsmbhCHYr1XuO6KnYOFhqcnwEC/k2GLiPR2 X-Received: by 2002:a5d:89d6:: with SMTP id a22mr23442097iot.178.1627465032698; Wed, 28 Jul 2021 02:37:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627465032; cv=none; d=google.com; s=arc-20160816; b=b7/Aeo/7Y1UJDkPiMyek/RZEBRZ01142gBsBktisZnJbJnzRnEltGKrBw0lDKQtl1Z U8FVk14S3QBrWFwNeHvYZ8tR1vWOVsHc3vAovWdEI/tTLmWKNl/+1eVDWbiJdVCgmK7V q5UYOy/eK3i8GsFYUdG35IBMb9KfEalRDu5Sona2F5EFqmyFA3/tAbPtAu7rXvuBUmj6 UFvG5LDpS+1ZyEe6iOCYU1jjF1WA9Vv4DOEeq7GAMlTa4Puj/FqVYZGlTTKmneJ7K+ax MY2/TxSALLrnMGzlwUEmHUrPiWHIqIm6JXdeDU6WM3z08rdITemIgjVj2s/cL4rGzxzS AFkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=xL5cyJMGrttPS8Tg4Vl0Rcy8TKFlSP31kRfsgxf0CRU=; b=XTbmnLHYJr1oIqJkRKbY2KikNG36H0wiJ8iGiPZF2D3z6QgY7zgIrFUOihwHKgiGoX ISJkMen1S4fCIXn/ljl8UDS5XMZ7O4jB4nksc7+I6JYGLMs2IJMii9HKIvj2PiIunZOQ 9+afTJ6AU5b+qfBYV0E8+w7vqZFyqM0B/spm/CGNxPKJ0R6cw3E5Ti0/M2HovpvlGjKQ nMHXQ1NRVhvaaW9jZMqbI0IXPypS3/BciY1RHbn3D8ynN0lVAS4osBB0tPwRCfhxELO3 h9TuM4vQnL17CTOTC8iZB9kw7y03jTK0/iUAN9GQo4kucsXvyFx6lbsGEwWwUoViNwmm kMbw== 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o8si2800635iow.53.2021.07.28.02.37.00; Wed, 28 Jul 2021 02:37:12 -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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234164AbhG1JgT (ORCPT + 99 others); Wed, 28 Jul 2021 05:36:19 -0400 Received: from foss.arm.com ([217.140.110.172]:53920 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231322AbhG1JgT (ORCPT ); Wed, 28 Jul 2021 05:36:19 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6B8701FB; Wed, 28 Jul 2021 02:36:17 -0700 (PDT) Received: from [10.57.36.146] (unknown [10.57.36.146]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 85CD53F73D; Wed, 28 Jul 2021 02:36:15 -0700 (PDT) Subject: Re: [PATCH] drivers/soc: Remove all strcpy() uses in favor of strscpy() To: David Laight , 'Geert Uytterhoeven' , Len Baker Cc: Kees Cook , Andy Gross , Bjorn Andersson , Magnus Damm , Santosh Shilimkar , "linux-hardening@vger.kernel.org" , linux-arm-msm , Linux Kernel Mailing List , Linux-Renesas , Linux ARM References: <20210725151434.7122-1-len.baker@gmx.com> <80f4574c9f6c4c6780735b0fffd83363@AcuMS.aculab.com> From: Robin Murphy Message-ID: Date: Wed, 28 Jul 2021 10:36:09 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: <80f4574c9f6c4c6780735b0fffd83363@AcuMS.aculab.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021-07-28 09:36, David Laight wrote: > From: Geert Uytterhoeven >> Sent: 26 July 2021 09:03 >> >> Hi Len, >> >> On Sun, Jul 25, 2021 at 5:15 PM 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(). >>> >>> Signed-off-by: Len Baker >> >> Thanks for your patch! >> >>> --- >>> This is a task of the KSPP [1] >>> >>> [1] https://github.com/KSPP/linux/issues/88 >> >> Any chance the almost one year old question in that ticket can be >> answered? >> >>> drivers/soc/renesas/rcar-sysc.c | 6 ++++-- >> >> Reviewed-by: Geert Uytterhoeven >> >> But please see my comments below... >> >>> --- a/drivers/soc/renesas/r8a779a0-sysc.c >>> +++ b/drivers/soc/renesas/r8a779a0-sysc.c >>> @@ -404,19 +404,21 @@ static int __init r8a779a0_sysc_pd_init(void) >>> for (i = 0; i < info->num_areas; i++) { >>> const struct r8a779a0_sysc_area *area = &info->areas[i]; >>> struct r8a779a0_sysc_pd *pd; >>> + size_t area_name_size; >> >> I wouldn't mind a shorter name, like "n". >> >>> >>> if (!area->name) { >>> /* Skip NULLified area */ >>> continue; >>> } >>> >>> - pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL); >>> + area_name_size = strlen(area->name) + 1; >>> + pd = kzalloc(sizeof(*pd) + area_name_size, GFP_KERNEL); >>> if (!pd) { >>> error = -ENOMEM; >>> goto out_put; >>> } >>> >>> - strcpy(pd->name, area->name); >>> + strscpy(pd->name, area->name, area_name_size); > > You can just use memcpy(). Indeed. In fact I'd go as far as saying that it might be worth teaching static checkers to recognise patterns that boil down to strscpy(dst, src, strlen(src) + 1) and flag them as suspect, because AFAICS that would always represent either an unnecessarily elaborate memcpy(), or far worse just an obfuscated strcpy(). Robin.