Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp14915rdb; Fri, 29 Sep 2023 14:58:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF06P59RSDxVvt/na4ehGGiJOHDz51fpXFZM/dV6tD2/Y6RLL/dJjhwvRAceRVToNwLkJ6T X-Received: by 2002:a17:902:ecd2:b0:1c4:29dd:2519 with SMTP id a18-20020a170902ecd200b001c429dd2519mr5951399plh.67.1696024739408; Fri, 29 Sep 2023 14:58:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696024739; cv=none; d=google.com; s=arc-20160816; b=APIwc7kwggj1QDqKkQDoKnsVKY/aGFwKSSMD1+nxrppivU/nWrEfuquOrV8jDLCXBH cX5A+qDxsjKOVmHu63XKtSZzHC0uvEovy/+R2jpnU74XEROrMhI5ARf139WIdguYJhwH jIq05BODywNy7FGny8bmuviEEm4c7lVKrV3JriP5J4WmDW0igIc1ROqVNpXycfgpvMEq oo3uKJAKUbKtzUqVN211oWB/K8JYrG/XkmfhHekNoN1/2lgXz0LlpxxNJwZ4Zduxflpr 1YJRz0pZEdTZa1g/S2A4a9FK2SosrEi04x9QpomxQJ+/uqOtZpE3R0O+MfhEIl9lMQRy 2qCA== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=a819MkWwG+HUW3DecPflS1GgW7AmRacZ2O8fhQTbwAc=; fh=Zmnz3APWHps52r7yicvGbev+2h5ER7kbQfcIH+SvUrI=; b=dG47Ri2qkRCPHbwNazpzOwlYOOOp89CXxI1s2rv1g5FlzLTn2iwa+w+NkNIS7aJBBw xT+pmCst0X/9uLk6Y3KhlY6fZ5tFlCggeWHD9NDNnGEAjwwUDlFUrk+SvFZWRMQa3Fih 4PignMlAGlwsj9f342/6pIfvYYctaPomkrppYFNebSr5ig5quZ2BnNTO9rPUIP5dsKve 7BBtx6nT3fmnU3s0POLsb5zAUwEWCe0mh3+rkF1iQlmVfiGO+Os9apl9nrCHOt5Yubr4 Mf0TlLXm/9L4RmsftzNc3/ESK51BSzltYzq4/TVsSv2w00qiXJdG2gCO7PvXMgxuOMVA wDbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=dG8t6WDi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id lg5-20020a170902fb8500b001c3a4fb5861si20275201plb.517.2023.09.29.14.58.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Sep 2023 14:58:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=dG8t6WDi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 456AC83CF91A; Fri, 29 Sep 2023 12:56:46 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232834AbjI2T41 (ORCPT + 99 others); Fri, 29 Sep 2023 15:56:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232748AbjI2T40 (ORCPT ); Fri, 29 Sep 2023 15:56:26 -0400 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40AFC113 for ; Fri, 29 Sep 2023 12:56:24 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-690d2e13074so11803911b3a.1 for ; Fri, 29 Sep 2023 12:56:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1696017384; x=1696622184; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=a819MkWwG+HUW3DecPflS1GgW7AmRacZ2O8fhQTbwAc=; b=dG8t6WDicmZpz6llDpNvlOD74K/eKRxsAGor2GpbtoofGpWQTCYvmYxLyymuh/Hmeo ZOelb+e/SCk3pXGB4sOOfptG/xCMaqEZgxQi1JCtjDnzTpdpbW3wxn9MheEh0Q9pJE6a 7HIC4dNe01A9B3EIsOwm+BbQzOHEpQvB5f/HM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696017384; x=1696622184; h=in-reply-to: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=a819MkWwG+HUW3DecPflS1GgW7AmRacZ2O8fhQTbwAc=; b=FcC3e+azvwzaekKWeciJl0bQoBZAULkX+7AT1u0Qm83In91JWOWx+S2fISfzO5Np5U Wbmn4oR9IWPIUtDjz/HpLL/ba3cvwJ1I9/aHYcQiV3fd7m/Jn7snNo1qZn4+L7H0u3uv AuXU4sfXJ8/Fslq/UzBCgeOOm+qnGgm27LzCovAz+h/IOwRTOS8Ry3vrAAZ+PHTCKsST ASFUnbRtAPXjzgCLKGzTk/hPuUUxAWaBlNbUn+dd0km8al0RuGu0CLN4ZumkmFJ8z8vl HFUKXerBkGo+vaqJdXNGttQLQn0pvA02uXyPTJDkyJ4cFuXtxllPCmdpR902ws428T0J FUjg== X-Gm-Message-State: AOJu0Yxgq1pKYsX6K4wTFQkB+kQAI8k7G8zXsBCTj5pHAWIp9c7hfBWQ bm+sxAKMExIs0m/PsIgoCMjV6A== X-Received: by 2002:a05:6a00:1951:b0:68f:dd50:aef8 with SMTP id s17-20020a056a00195100b0068fdd50aef8mr5166207pfk.4.1696017383659; Fri, 29 Sep 2023 12:56:23 -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 x9-20020a056a00270900b00690fdeb5c07sm15830854pfv.13.2023.09.29.12.56.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Sep 2023 12:56:22 -0700 (PDT) Date: Fri, 29 Sep 2023 12:56:22 -0700 From: Kees Cook To: Dean Luick Cc: Leon Romanovsky , Dennis Dalessandro , Jason Gunthorpe , Justin Stitt , linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] IB/hfi1: replace deprecated strncpy Message-ID: <202309291255.C5485E6811@keescook> References: <20230921-strncpy-drivers-infiniband-hw-hfi1-chip-c-v1-1-37afcf4964d9@google.com> <169537858725.3339131.15264681410291677148.b4-ty@kernel.org> <2f4bd46c-664e-4253-8d57-16bd46dd3be8@cornelisnetworks.com> <202309232019.BE78A9C0@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,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 groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Fri, 29 Sep 2023 12:56:46 -0700 (PDT) On Tue, Sep 26, 2023 at 07:56:34AM -0500, Dean Luick wrote: > On 9/23/2023 10:20 PM, Kees Cook wrote: > > On Fri, Sep 22, 2023 at 09:25:39AM -0500, Dean Luick wrote: > >> On 9/22/2023 5:29 AM, Leon Romanovsky wrote: > >>> > >>> On Thu, 21 Sep 2023 07:17:47 +0000, Justin Stitt wrote: > >>>> `strncpy` is deprecated for use on NUL-terminated destination strings > >>>> [1] and as such we should prefer more robust and less ambiguous string > >>>> interfaces. > >>>> > >>>> We see that `buf` is expected to be NUL-terminated based on it's use > >>>> within a trace event wherein `is_misc_err_name` and `is_various_name` > >>>> map to `is_name` through `is_table`: > >>>> | TRACE_EVENT(hfi1_interrupt, > >>>> | TP_PROTO(struct hfi1_devdata *dd, const struct is_table *is_entry, > >>>> | int src), > >>>> | TP_ARGS(dd, is_entry, src), > >>>> | TP_STRUCT__entry(DD_DEV_ENTRY(dd) > >>>> | __array(char, buf, 64) > >>>> | __field(int, src) > >>>> | ), > >>>> | TP_fast_assign(DD_DEV_ASSIGN(dd); > >>>> | is_entry->is_name(__entry->buf, 64, > >>>> | src - is_entry->start); > >>>> | __entry->src = src; > >>>> | ), > >>>> | TP_printk("[%s] source: %s [%d]", __get_str(dev), __entry->buf, > >>>> | __entry->src) > >>>> | ); > >>>> > >>>> [...] > >>> > >>> Applied, thanks! > >> > >> It is unfortunate that this and the qib patch was accepted so quickly. The replacement is functionally correct. However, I was going to suggest using strscpy() since the return value is never looked at and all use cases only require a NUL-terminated string. Padding is not needed. > > > > Is the trace buffer already guaranteed to be zeroed? Since this is > > defined as a fixed-size string in the buffer, it made sense to me to be > > sure that the unused bytes were 0 before copying them to userspace. > > I was not aware that binary trace records were exposed to user space. If so, and the event records are not zeroed (either the buffer as a whole, or individual records), then strscpy_pad() is the correct solution. My quick review of the tracing system suggests that nothing is zeroed and the record is embedded in a larger structure. However, this begs the question for all users of tracing: Aren't alignment holes in the fast assign record a leak? I thought they were passed over direct to userspace somehow, but I haven't looked at the details in a long time. I could very well be misunderstanding it. -- Kees Cook