Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp631146rdb; Thu, 19 Oct 2023 14:34:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEDSSvG98MiCSsqI9Y6yUZNJDXq1m1mEBGnf22Te+Y9b3cVkqJbm8WhWwSkRG5fRtTLEsBw X-Received: by 2002:a17:902:d054:b0:1bf:557c:5a2c with SMTP id l20-20020a170902d05400b001bf557c5a2cmr97869pll.44.1697751294066; Thu, 19 Oct 2023 14:34:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697751294; cv=none; d=google.com; s=arc-20160816; b=PImB052ce1a4aOtWWHNZHQxgdoPmp+gxj+mZOEGwspUXVydvB90LzYuluSF3YE+8Nn 73MIkTbOV5aJ5WRbu1ijkJuoiJbZgfk11M1E7xSrlClrPFeveWcfAZ9MKh/syjuFRbhl jjD09S6p9BDVlyos4T/O+esd7RNOwhlz81iDNkSmWxks0Ial+A1rKfqqtNPYBOnDg3VZ 10WEAF026wyYPT6jK86rWjwUxK6I6SDsqh2J718Tg6j+oDtxcUzSSjuq9cjkylxCxQOE /0j4p/DeHpwDa1G7WO+Aqs1FO8pioB+TpeZ86iI0Z0CZxdBC4xBe4ZbvRlTX/mYlM6kX 38IQ== 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=lqEsMCD2petOW7LvFR3PY5d0rpovBBpxs1upvL07Joc=; fh=OaKv+yTyPpZq4t/fWvBMqfh+9KRMGwuzmGynx+/oZdA=; b=AiAUDXocsC0mRtECPaL/+jHvYcmoZ0SePYVUtq4psnk2uIeL3pyJTwRqOZKtQx4rHy 4KSUBGWuUaM0jwFgDPPy3ZSVDfthHqIHtaLp54Z1AhrOZjJPo+ulK/2npIreI9hsmWXk Qe3VsY2KA07CkQ2zW74MeCbQclygzMK/3rmgJm3YqDRqCd9+SI5OKHqEVmqGAURxWNUF zHCjsqq7rZUCB/pOQK1qe2BU8i5TwkxlKHvbQ0VhoFaIqZmlQSgW1PITV/DZokik1Et4 ZSwjum5dsaJBAKwgcaCjpNCz4gribO095Zj9j/bbk5hDk0gQc+jVRlqgSPjOKwdphcMI 3lXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=x3KwTcGu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id c1-20020a170902d48100b001b8904eadb8si417403plg.460.2023.10.19.14.34.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 14:34:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=x3KwTcGu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id B0FD182662E1; Thu, 19 Oct 2023 14:34:50 -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 S233365AbjJSVem (ORCPT + 99 others); Thu, 19 Oct 2023 17:34:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230039AbjJSVek (ORCPT ); Thu, 19 Oct 2023 17:34:40 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D94498 for ; Thu, 19 Oct 2023 14:34:39 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9cb4de3bf0so243452276.0 for ; Thu, 19 Oct 2023 14:34:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697751278; x=1698356078; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=lqEsMCD2petOW7LvFR3PY5d0rpovBBpxs1upvL07Joc=; b=x3KwTcGuXf3Wzm2xjlzyeR81q3rusVWLr41CBNirUTnQPEvyM14j7mfEqTpcpxxa6k dJAX2Je3jYCtjmFLzhY9KCLRdmGQRlskksmVSjSMNRn1FjxB4kOQb0mG/hWeKtMWqiz3 8TawVPeQpOGakMJ3OaZM0bAvheUPfZCd6xzoNMut5yjqFcoVV7Ijtt3Mb5aSZaCqqxZM gn37/DtZeQPHZ0DuV8YvtCWfruEbBgmZC76dG/zsjXYRA+6Y80LXVJR4ds1e2FGflFQC dpFB6q6tt/NzVFeDTUsMd3w3idJK5F/75BwiZBuuLxUrJs/VXr6BYpKvvEaCipL12peH ygTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697751278; x=1698356078; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=lqEsMCD2petOW7LvFR3PY5d0rpovBBpxs1upvL07Joc=; b=YfNRUpXv2JJSbUZtihHoP6QU+cSIS4F4rKa+GOOEI/NorJLHxs8i44DE5Ux61SnDy1 F6cAoGj8M9wYztpKqK5mnqe+dDWTyrt2flWS6dQs0+C5DeI4bmWMoAvSX0HCwjtYZZ6m nTGs67BprA6ztWjIlmxwcW+i2G57J66H0vaZiiipX+cTlcKDXtV9K2x93BoLyWX7O6dk IFSjY9TI2X0nYHfY8NLCTqvekKzWgPl2AuXv6MHMWk3KJ89enOh70I06THxBy0wXNlw4 IGU90i0mEifMbD36SjWRSuq9lcSL8Hwqm8o9TvVnUJoSFyy00xOeY+7D/SyqQ7hRYMok p8TQ== X-Gm-Message-State: AOJu0YycN1iNscUn7or3tcSRmLTo9tvWsSnalBYQiqU6TcYy6uzO2ojy 6zl8cvFTBjvCIMYqNHnElPfbzVsqAcZ7x0yUzg== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a25:d110:0:b0:d9c:c9a8:8c27 with SMTP id i16-20020a25d110000000b00d9cc9a88c27mr446ybg.13.1697751278223; Thu, 19 Oct 2023 14:34:38 -0700 (PDT) Date: Thu, 19 Oct 2023 21:34:35 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAOqgMWUC/x3NQQqEMAxA0atI1gbaqjh6FXGhNWoWUyUpxUG8+ xSXb/P/DUrCpNAXNwglVj5Chi0L8PsUNkJessEZV1ljO9QowZ8/XIQTiWJIX8L90IirR49TU7u 5qudPaw3kyCm08vUOhvF5/p5i6cRwAAAA X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1697751277; l=2895; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=E7cZpx/98qOWGlojxJVSbMIF5mbCSTqGliFRklJggj8=; b=r5nMNcuHW5ecWZu9xGiqdmwQIceKXPadUITLieWfMFjmPqvtIMR7d5OaDyBK0FmycCu8pXiod LiUxzFjDcRrDIgL6K4o0W5CUFygua5UAEfx8VqQx05b4PaeO3KaJgSh X-Mailer: b4 0.12.3 Message-ID: <20231019-strncpy-drivers-nvme-host-fc-c-v1-1-5805c15e4b49@google.com> Subject: [PATCH] nvme-fc: replace deprecated strncpy with strscpy From: Justin Stitt To: James Smart , Keith Busch , Jens Axboe , Christoph Hellwig , Sagi Grimberg Cc: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Justin Stitt Content-Type: text/plain; charset="utf-8" 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 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]); Thu, 19 Oct 2023 14:34:51 -0700 (PDT) strncpy() is deprecated for use on NUL-terminated destination strings [1] and as such we should prefer more robust and less ambiguous string interfaces. Let's instead use strscpy() [2] as it guarantees NUL-termination on the destination buffer. Moreover, there is no need to use: | min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE)); I imagine this was originally done to make sure the destination buffer is NUL-terminated by ensuring we copy a number of bytes less than the size of our destination, thus leaving some NUL-bytes at the end. However, with strscpy(), we no longer need to do this and we can instead opt for the more idiomatic strscpy() usage of: | strscpy(dest, src, sizeof(dest)) Also, no NUL-padding is required as lsop is zero-allocated: | lsop = kzalloc((sizeof(*lsop) + | sizeof(*assoc_rqst) + sizeof(*assoc_acc) + | ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL); ... and assoc_rqst points to a field in lsop: | assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)&lsop[1]; Therefore, any additional NUL-byte assignments (like the ones that strncpy() makes) are redundant. 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 Similar-to: https://lore.kernel.org/all/20231018-strncpy-drivers-nvme-host-fabrics-c-v1-1-b6677df40a35@google.com/ --- Note: build-tested only. --- drivers/nvme/host/fc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/nvme/host/fc.c b/drivers/nvme/host/fc.c index a15b37750d6e..680585253f7c 100644 --- a/drivers/nvme/host/fc.c +++ b/drivers/nvme/host/fc.c @@ -1218,10 +1218,10 @@ nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl, /* Linux supports only Dynamic controllers */ assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff); uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id); - strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn, - min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE)); - strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn, - min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE)); + strscpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn, + sizeof(assoc_rqst->assoc_cmd.hostnqn)); + strscpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn, + sizeof(assoc_rqst->assoc_cmd.subnqn)); lsop->queue = queue; lsreq->rqstaddr = assoc_rqst; --- base-commit: dab3e01664eaddae965699f1fec776609db0ea9d change-id: 20231019-strncpy-drivers-nvme-host-fc-c-a542b34b8710 Best regards, -- Justin Stitt