From patchwork Thu Oct 19 17:54:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 155671 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2010:b0:403:3b70:6f57 with SMTP id fe16csp557120vqb; Thu, 19 Oct 2023 10:54:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGU6F6F88FewvwsO7TQI+H+OoFwRV20hB83Dl6v/Zat1HvTUQPlAu1UZROiDEC4+wpRzDyT X-Received: by 2002:a05:6a00:1a87:b0:68f:ce6a:8685 with SMTP id e7-20020a056a001a8700b0068fce6a8685mr3790567pfv.14.1697738078501; Thu, 19 Oct 2023 10:54:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697738078; cv=none; d=google.com; s=arc-20160816; b=aUmVcYV81LoK3/PU7TXKzrlLh0Q2D/EPKp/ZsZ3EZl8rjtW1ZoGwJOAbye99F82DJE JLXPIfyNbRFBwJqB3GU9ek6ffpXILo6GomXI2IEKWe3QxmKPv8iGVcP7JqAPyuX/5lc6 SqjrsudTEJpL/QF3aXBrF+r3az++H1OAzs9VQ+58jVPye3W+URdU+ZxqTeOeW4c2/Qkd waOtvNy4IvrEm7F1z9Np4siJYkQNodt2fLZQoErde7f7tkEnPwwiuE2P1u7p1sHjDh0L 91JzljWx9em+QmwAvYG4L13xJVOORqxkMG7QNKUyfTszwdTeumc3e9bUjvvsFuTW6cs6 cQsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=wCaHYSvOMmjyjjo+2DaJHIApqUGU3SdaqxSKwGR/sus=; fh=iE4eEftac1ouXid/6VJgg9w32i/LqJIRCFCVqoBdEjI=; b=VU7NifJIDsRAZR24Aeo0AWZUjMTWfpAu5zijI3rdsPa/WEHP5tDuNXmXVw12gkiSdA 9P57VVaSONjQcEBVDPmk4fnavh/FEo0s0DCEbYPJLhU+sWgSjOwt4T5i5iaQjpPjUvtO LJGG0+sDxOrRAPjOin5H8PwOnpZiNgadCoUTOqJvDddB/IbrehRNm+xRDWLKPnI09jfC m74mM37/qIXEQcN1SHGVL/cLUROZR4FqIsU+zLO8c7XHbOCPvjlZfNT9C50hFn3SSKfl BUfU4eJcLi943I8EWeB0uRn+woDt9oz6gkarHTFSZEkMpcMYiSOa4abJ72fEYGW2RJSu Yj0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NZc9I8HD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id z9-20020aa78889000000b006be30cdc3d8si148683pfe.163.2023.10.19.10.54.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 10:54:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NZc9I8HD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 7E7EB835FD0C; Thu, 19 Oct 2023 10:54:35 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345907AbjJSRyW (ORCPT + 26 others); Thu, 19 Oct 2023 13:54:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235491AbjJSRyV (ORCPT ); Thu, 19 Oct 2023 13:54:21 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C69E3E8 for ; Thu, 19 Oct 2023 10:54:19 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9b9aeb4962so7688780276.3 for ; Thu, 19 Oct 2023 10:54:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697738059; x=1698342859; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=wCaHYSvOMmjyjjo+2DaJHIApqUGU3SdaqxSKwGR/sus=; b=NZc9I8HD5Mbv3+kaOY8h3qn1a4tHq979zp/1L+q+oUGb3mnBXZ02OyWWp3sDxfvsML XAZMTnzqH7+0O1qxB6Vdx4coiXRgIrlBV5mAOTxPSz91jgkS5VMBTvrJktrxnFZt//Hf T/Fg6AkKFql4BkxAHme12JIbu4I48cEOnpAyqhKaAqjkzCt1zHiqlmmOamjCvklzrvV8 1msXByhXEi+640ucGLwqAYgyaXwYVIa/DW8YxYYMLutHNsZ9lkkfrUbUbV/94Fo63JGm 72BLnEUys5MchCBh07/IoHPHd8Ydjjeas1breSM6dK09KXmHShAmZVeJcw0o7DNMIZYa TdrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697738059; x=1698342859; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=wCaHYSvOMmjyjjo+2DaJHIApqUGU3SdaqxSKwGR/sus=; b=kmoIY9PUn+XyLdqiCNXXZ3+F48QSrzzmrBqy0fR6vs8Pe5mtm0u7xGWeIjcgCM28EX S93S14+8ksP/1g3ztfecb9yCjIm1Zvq97uJd8833bZnIafZOIaiZ8BZ/+Y8+kNBT2/9E csZhI5cBKdkbAQS27XP0dHBKAVCzbAFmd+c4ZSxLUvUCtjUsXYKZaPYHeqnbIYb6kZyp 7YCVNPD1KoHIPNtm2nxYymCSN2N51eteIHkfL4t8OdNskXopbwhzCsQUgoNerolvF6yh 4jpBOXEe3hZ4jN66+0r2bmW/VyWk0FjmhMSV4AxsGxuOgl1QL/YvNnJ/qzlOX1ohVlZz yfEA== X-Gm-Message-State: AOJu0YwkEnbRcPw4VoEeydiSuJfuhQRdM4DS9dhFlUC82pwQzt5QShrI OydC6axjXk8rX4xm9i2n1s3/lkaPTCcFsOKjiA== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a25:738b:0:b0:d9c:c9a8:8c27 with SMTP id o133-20020a25738b000000b00d9cc9a88c27mr31838ybc.13.1697738058988; Thu, 19 Oct 2023 10:54:18 -0700 (PDT) Date: Thu, 19 Oct 2023 17:54:15 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAEZtMWUC/43NQQ6DIBCF4asY1p0GMEboqvdoXLQw6iQVzEBIj fHupZ6gy+8t/reLhEyYxK3ZBWOhRDFU6Esj3PwMEwL5aqGlbpVUBlLm4NYNPFNBThCKp2WBV87 gQHWjbU1vrbEoamJlHOlz5h9D9UwpR97Ot6J+65/hokBBZ2Qvx967ztn7FOP0xquLixiO4/gCb mF4u8kAAAA= X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1697738058; l=2904; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=rw760A6im3UPDmPb1PvvzFcADO0t8QLqJon9lALd7qU=; b=NYE7eTw9PMvTw2M5a8Bn23YgudGYR1Uir+9aU2nJN/yFHTP0niBKIS6Z6tV2vKAW3CaEGvUBt 56mAEMPR1qFCvf/IQUE+DNmLbFA7xeXjbNcSzdE6VeR+EPpGWLVtRMF X-Mailer: b4 0.12.3 Message-ID: <20231019-strncpy-drivers-nvdimm-btt-c-v2-1-366993878cf0@google.com> Subject: [PATCH v2] nvdimm/btt: replace deprecated strncpy with strscpy From: Justin Stitt To: Dan Williams , Vishal Verma , Dave Jiang , Ira Weiny Cc: nvdimm@lists.linux.dev, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Justin Stitt , Kees Cook X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Thu, 19 Oct 2023 10:54:35 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780207403608426479 X-GMAIL-MSGID: 1780207403608426479 Found with grep. 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 expect super->signature to be NUL-terminated based on its usage with memcmp against a NUL-term'd buffer: btt_devs.c: 253 | if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0) btt.h: 13 | #define BTT_SIG "BTT_ARENA_INFO\0" NUL-padding is not required as `super` is already zero-allocated: btt.c: 985 | super = kzalloc(sizeof(struct btt_sb), GFP_NOIO); ... rendering any additional NUL-padding superfluous. Considering the above, a suitable replacement is `strscpy` [2] due to the fact that it guarantees NUL-termination on the destination buffer without unnecessarily NUL-padding. Let's also use the more idiomatic strscpy usage of (dest, src, sizeof(dest)) instead of (dest, src, XYZ_LEN) for buffers that the compiler can determine the size of. This more tightly correlates the destination buffer to the amount of bytes copied. Side note, this pattern of memcmp() on two NUL-terminated strings should really be changed to just a strncmp(), if i'm not mistaken? I see multiple instances of this pattern in this system: | if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0) | return false; where BIT_SIG is defined (weirdly) as a double NUL-terminated string: | #define BTT_SIG "BTT_ARENA_INFO\0" Link: https://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 Reviewed-by: Kees Cook Reviewed-by: Kees Cook --- Changes in v2: - provide more details about memcmp() - fix typos in commit msg (thanks Kees) - fix subject line (thanks Alison) - Link to v1: https://lore.kernel.org/r/20231018-strncpy-drivers-nvdimm-btt-c-v1-1-58070f7dc5c9@google.com --- Note: build-tested only. --- drivers/nvdimm/btt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- base-commit: 58720809f52779dc0f08e53e54b014209d13eebb change-id: 20231018-strncpy-drivers-nvdimm-btt-c-15f93879989e Best regards, -- Justin Stitt diff --git a/drivers/nvdimm/btt.c b/drivers/nvdimm/btt.c index d5593b0dc700..9372c36e8f76 100644 --- a/drivers/nvdimm/btt.c +++ b/drivers/nvdimm/btt.c @@ -986,7 +986,7 @@ static int btt_arena_write_layout(struct arena_info *arena) if (!super) return -ENOMEM; - strncpy(super->signature, BTT_SIG, BTT_SIG_LEN); + strscpy(super->signature, BTT_SIG, sizeof(super->signature)); export_uuid(super->uuid, nd_btt->uuid); export_uuid(super->parent_uuid, parent_uuid); super->flags = cpu_to_le32(arena->flags);