Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp618387rdg; Thu, 10 Aug 2023 13:39:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGRz+8+Z1FH8+0k/qFzYMg+DNyKoELE6UxyyRtkdHNtAGwulQZUnp4v4ql28Xb2e+aDmJCa X-Received: by 2002:a54:4492:0:b0:3a7:e4a:5cbd with SMTP id v18-20020a544492000000b003a70e4a5cbdmr3843920oiv.31.1691699950726; Thu, 10 Aug 2023 13:39:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691699950; cv=none; d=google.com; s=arc-20160816; b=kkIdHwT8ohtUrXD5De0rMZKYjmDBxhdpI2Bo+bRL+uzndhUEYERNH2rIoIU9z5XcQ1 NdC1FLCz2pE+v6BiRUUBL1MUxQ1KXU9TmjL2InZvfeF1tj3h4TRD1Wtf60mUVczXoSJw mwfFdg+a+6vJoJWciMLs7oG/j2+g8dWhWI1nBfubjbeA+ylNQFVALUjJadpsV6HM6rZh Nilg9HeqZZ9a6R2lr80/GW9RH+lE9wBiIC1yuomb74HnULxGNeop1gcya86Ke1fvOdSy nckaXgadT6g71uP5quoT9FuMaqua1dsbPnl23y+0pvgA8CofQOUNu64vwre9mJkRe85q mx2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=GrzpUIYsNReqF6Vjt7YeuyI+AzdvZwRKw+lBcbw3/VE=; fh=toCSZ8YSH2/KAFF856Z9fI6AQRnoSCePSSdXqx+myVw=; b=Q+xaiDxOZOJSeeJYDF23PxVkhikNNrRWiwqLpcgQDPJINSwD0FiDdsgABxlSwzSKT8 jNMatKmN0DuCuFTxOlY00G7xZyfiWrkvxy2GRtDlsUays0AnhflifsoX2PvM2uses5iu fDU4yxUF/dYRee1yUGjGNIDDmNUu90tib+TOG1FksEqF7QSjYlcQrUEavAjfESdclZjx ROr1WXyhEO0wfHXjRJKAE2GmL1xtTzd4mL1R0WXc9E3rVYVVwT7i/PhAsjngUZo+mhq6 2HRR7JcZMAwZDCSyNqaXHuJpxjwv0o+sm2hTVjRJ96CuzxuKUlvmnZ9fS4u2fknjvsmK Nmug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=AIGXxuzP; 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=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v135-20020a63618d000000b0056554b240f8si2171571pgb.175.2023.08.10.13.38.58; Thu, 10 Aug 2023 13:39:10 -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=@chromium.org header.s=google header.b=AIGXxuzP; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232383AbjHJT6J (ORCPT + 99 others); Thu, 10 Aug 2023 15:58:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229890AbjHJT6I (ORCPT ); Thu, 10 Aug 2023 15:58:08 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD564EA for ; Thu, 10 Aug 2023 12:58:07 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1bb893e6365so9968865ad.2 for ; Thu, 10 Aug 2023 12:58:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1691697487; x=1692302287; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=GrzpUIYsNReqF6Vjt7YeuyI+AzdvZwRKw+lBcbw3/VE=; b=AIGXxuzPsRszEV5Gyhfusxvte6pg5BONip5dTyc5hhzjXpSNO57GWCQT96ebiABjYS 8/90fFkIVa2FauB47bAOXuu10ZTR9hxBnVQranwCf2+5kEWBpjY2oSyb6xRiXaeGQZ18 GgxdUjnPTWhQzCQjPexgwl6fmEz94SHJUcsVs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691697487; x=1692302287; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GrzpUIYsNReqF6Vjt7YeuyI+AzdvZwRKw+lBcbw3/VE=; b=G90YHNmLTlxqoWJl5UX9nxK20Jj6isLpe6ga8ldSGanftKzu5fWFyivV1f4DHw7wjo GJIEYYc3uxrIsOR2EPbSCVvkfCDBBEfoHH98qg+MQcsATadrJD16SKsDt+zfZVT5nc4D ja00uiIDJIDI0hAHL/+KSTNAvE2IjcSsLP1IBB5BIwvbGlz8mczLkw38rxIoUfdn/2RY eW9Q+cnnP3TjCnf7N5EsYc6zOQaF9mciSh1mWkTTcqvQdqCCsqfnGzo028tbRj/NAEW0 OUNMAKkMLY+MWqAtfqMac4z6dHt4lXsJsmQ5+TwbzSonRllHKhwVMGDZYKWv76MEJnQP lrsQ== X-Gm-Message-State: AOJu0YxA0yd6U2IDUBuVTUbTQl0w1+8MnOYOzm0s4RTMjiFhzyNfX8cx ikYqhWZL8aB2FGvjwTaFGZx0yQ== X-Received: by 2002:a17:903:247:b0:1bb:b86e:8d60 with SMTP id j7-20020a170903024700b001bbb86e8d60mr4103679plh.46.1691697487304; Thu, 10 Aug 2023 12:58:07 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id h5-20020a170902f54500b001a1b66af22fsm2166866plf.62.2023.08.10.12.58.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Aug 2023 12:58:06 -0700 (PDT) Date: Thu, 10 Aug 2023 12:58:06 -0700 From: Kees Cook To: Justin Stitt Cc: Catalin Marinas , Will Deacon , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] arm64/sysreg: refactor deprecated strncpy Message-ID: <202308101257.47E6ACBD5@keescook> References: <20230810-strncpy-arch-arm64-v1-1-f67f3685cd64@google.com> <202308101155.81497C5B@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 On Thu, Aug 10, 2023 at 12:25:37PM -0700, Justin Stitt wrote: > On Thu, Aug 10, 2023 at 12:00 PM Kees Cook wrote: > > > > On Thu, Aug 10, 2023 at 06:39:03PM +0000, Justin Stitt wrote: > > > `strncpy` is deprecated for use on NUL-terminated destination strings > > > [1]. Which seems to be the case here due to the forceful setting of `buf`'s > > > tail to 0. > > > > Another note to include in these evaluations would be "does the > > destination expect to be %NUL padded?". Here, it looks like no, as all > > the routines "buf" is passed to expect a regular C string (padding > > doesn't matter). > > > > > > > > A suitable replacement is `strscpy` [2] due to the fact that it > > > guarantees NUL-termination on its destination buffer argument which is > > > _not_ the case for `strncpy`! > > > > > > In this case, there is some behavior being used in conjunction with > > > `strncpy` that `strscpy` already implements. This means we can drop some > > > of the extra stuff like `... -1` and `buf[len] = 0` > > > > > > This should have no functional change and yet uses a more robust and > > > less ambiguous interface whilst reducing code complexity. > > > > > > Link: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings[1] > > > Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2] > > > Link: https://github.com/KSPP/linux/issues/90 > > > Cc: linux-hardening@vger.kernel.org > > > > > > Signed-off-by: Justin Stitt > > > --- > > > For reference, see a part of `strscpy`'s implementation here: > > > > > > | /* Hit buffer length without finding a NUL; force NUL-termination. */ > > > | if (res) > > > | dest[res-1] = '\0'; > > > > > > Note: compile tested > > > --- > > > arch/arm64/kernel/idreg-override.c | 5 ++--- > > > 1 file changed, 2 insertions(+), 3 deletions(-) > > > > > > diff --git a/arch/arm64/kernel/idreg-override.c b/arch/arm64/kernel/idreg-override.c > > > index 2fe2491b692c..482dc5c71e90 100644 > > > --- a/arch/arm64/kernel/idreg-override.c > > > +++ b/arch/arm64/kernel/idreg-override.c > > > @@ -262,9 +262,8 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases) > > > if (!len) > > > return; > > > > > > - len = min(len, ARRAY_SIZE(buf) - 1); > > > - strncpy(buf, cmdline, len); > > > - buf[len] = 0; > > > + len = min(len, ARRAY_SIZE(buf)); > > > + strscpy(buf, cmdline, len); > > > > This, however, isn't correct: "cmdline" will be incremented by "leN" > > later, and we want a count of the characters copied into "buf", even if > > they're truncated. I think this should be: > > > > strscpy(buf, cmdline, ARRAY_SIZE(buf)); > > len = strlen(buf); > > > Thoughts on using the return value from `strscpy` here? This code seems to silently accept truncation, so -E2BIG will cause a problem if it only looks at the return value. -Kees -- Kees Cook