Received: by 2002:a05:7412:85a1:b0:e2:908c:2ebd with SMTP id n33csp105943rdh; Mon, 30 Oct 2023 15:37:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGBc0Nzz6oDqQav8fYWzElIWXHo6r0IWfuMVT/+jM5mk8r3hB1aIsEcO+DY4kGRBwppoQFZ X-Received: by 2002:a05:6870:860c:b0:1ea:a11:71f4 with SMTP id h12-20020a056870860c00b001ea0a1171f4mr13582739oal.59.1698705431584; Mon, 30 Oct 2023 15:37:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698705431; cv=none; d=google.com; s=arc-20160816; b=BPVJUwVJheAQihGWUAnnu53mldeqziDwBmQKlC/lOV5HkMOiB4qajvj+h4U5kpLppf WkF45ijFezi5u6kL5+4c4SamSG3+e4xM77FNj4ZHtJGIF5eTjeH56bkR0BuLN/CA950h 2YpKO+IaJ+xnpWJzE7UJX5tU4xyrxNqLWARNN2v6m/0sfwC5c/MyovcM0+fsUWyBa6Lz AIt7sW8Ap/M3uWTV2NCGzovJ7eVEDYnRnxMl8Uq2C06vf16+USWbBYnXvVVk5woEQ7BY GF93WvMEEysuPd4Ykj9bHQkRhHFKeN/zYb0PnnXT30QvHzn3Ysm4x2mMNORGiWgMI4X7 YkTQ== 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=lq5TwK+3n6zGzzimtcAIoXXorjYRH73jd8dtvn3Ft6I=; fh=xj8nTl55YM40VdqnxfQs4WpvsRY9vFNkcAfpZFL4Mds=; b=lOJ9xboHkcgDpV1kPbD6MTKX7rxFgPigor8NlKYGdUFk/41lfJB3UCb/5d4AuFRJZc PrvTw8TKJ7NwWD7ryfCI/kZNCcy9Cmotpn3URMQ1Gqzvzd7jTs+a4RCVYbIK67RatDy2 fVjuWVrrJyOX4oc3a9CUSt8PNdog38FIAxOjcVkIF5/WN0BGH+h3JRiZHlKroSnzO4q7 KnAUMJRMKQxkC3gGnQArH81mZlFaKWk4OT8/hDQlW5JFJTIvNcaeHwfOlNW6Iyjl/QKW 0g9lsW+7K0YQLxRsEnSLvzl9cpPTyvw9/jZ4/ZFgb5OMLnyVWfycTGBp6bXj7WuKL3J/ rFTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="RdGbf/No"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id t4-20020a63f344000000b005acd5108042si80441pgj.521.2023.10.30.15.37.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 15:37:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="RdGbf/No"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 AAF44807079A; Mon, 30 Oct 2023 15:37:08 -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 S230315AbjJ3WhC (ORCPT + 99 others); Mon, 30 Oct 2023 18:37:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229738AbjJ3WhB (ORCPT ); Mon, 30 Oct 2023 18:37:01 -0400 Received: from mail-ot1-x349.google.com (mail-ot1-x349.google.com [IPv6:2607:f8b0:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93591E8 for ; Mon, 30 Oct 2023 15:36:58 -0700 (PDT) Received: by mail-ot1-x349.google.com with SMTP id 46e09a7af769-6ce2cce763eso7358670a34.0 for ; Mon, 30 Oct 2023 15:36:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698705418; x=1699310218; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=lq5TwK+3n6zGzzimtcAIoXXorjYRH73jd8dtvn3Ft6I=; b=RdGbf/NoSiOuyh5onpF3BZoHSI1vl0NLpmmOHSolZPmMOyhBJMYsa/7V1/FJsFBM6a lql4DnFNZwfUde9/4v3VWLSNAD3XIoUGjibURqVKFM2u+EKn2t4ZvISAK6ZZ1r4gHP10 3ICQNxrFUryjHIRXZGo3qDdGf8cMXl3nPlp7uHtf4UUebbtkb+b+DWHaE47lPKJ+Tzlf 5RK//2wmpeoRc27RQI/9CWni5Z6P7dB5DZc+GKN3kTlenRIujv+AVn6SqZPwYAsZ1mZj ZlivGFvUawiPOr/J1EYYTNrXJaH/cirIkhLvS1HynALB3m8Qt2Y5cIYA8UnMhJYdYafK q8PA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698705418; x=1699310218; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=lq5TwK+3n6zGzzimtcAIoXXorjYRH73jd8dtvn3Ft6I=; b=U8z02NqQ8g6JP3rYBK1SwXzE45LqtSN3/WGq6x+uFDgmuazFx/tB+yLJhUEShoVsbc uVRb/fCb7jGvdwfiSNrMoaJI7xB+9hucMDMaQkjyrlqOEvUrgSnEDgrni1kWkZZfCxOV muPKmoKUKmhums0Pixqn+Qxe7Tsln38Vd06FNmg7T9O0qijKcoqimvs2Q0OXKZSXmxKX KGGUDeOWDeB679Ad5LC6vSTeoTKiozZu+t8iHSaUgqWz/igeWZrq4TN5qO+307P522ML oxA2HyHoKDw+gAYz1hHskgaUR28/rEWWxR1HEsCLN1o2XMh5QCv6Rw0cUjPiHn0XSvmi yA/Q== X-Gm-Message-State: AOJu0YzuEdu3ifF7x4VM3T2MW1G5Vpl0Qa/+jU9hSmI+w2QZleS4kPQX P1aJDzZwczsN+zyCFHbsQk9es+iHklbkaHQE8A== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6830:4502:b0:6c4:58bd:290d with SMTP id i2-20020a056830450200b006c458bd290dmr222233otv.3.1698705417918; Mon, 30 Oct 2023 15:36:57 -0700 (PDT) Date: Mon, 30 Oct 2023 22:36:57 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAAgwQGUC/x3NQQqDQAxA0atI1g2MIxX0KiLFJhkNyCiJSIt49 w5dvs3/F7iYikNfXWByquuWC+pHBbRMeRZULoYYYlOHJqAflmn/IpueYo5OrrjqOxEmekmmjQU XfE7cxi61LB1Bie0mST//0TDe9w/THRIxeAAAAA== X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1698705417; l=3578; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=ZF9U9vNqxuweLPAWDBaMtDMKK84GlPUqwF7Byf2+S7s=; b=F1b2huUJ/pDsyqCT80uhn3Zf7p1L3WobUXybolIxFi5AJ2jvthi3HulpjB6iOjb1ZfU9kv111 QIVZJMEINgNAVdeMOm48S4aw+a0kgwvm8A0d34cYpiKqJhOyUf5Kp3i X-Mailer: b4 0.12.3 Message-ID: <20231030-strncpy-drivers-scsi-libfc-fc_encode-h-v1-1-c08c2be6befa@google.com> Subject: [PATCH] scsi: libfc: replace deprecated strncpy with memcpy From: Justin Stitt To: Hannes Reinecke , "James E.J. Bottomley" , "Martin K. Petersen" Cc: linux-scsi@vger.kernel.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 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]); Mon, 30 Oct 2023 15:37:08 -0700 (PDT) strncpy() is deprecated [1] and as such we should use different apis to copy string data. We can see that ct is NUL-initialized with fc_ct_hdr_fill: | ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rspn) + len, ... In fc_ct_hdr_fill(): | memset(ct, 0, ct_plen); We also calculate the length of the source string: | len = strnlen(fc_host_symbolic_name(lport->host), 255); then this argument is used in strncpy(), which is bad because the pattern of (dest, src, strlen(src)) usually leaves the destination buffer without NUL-termination. However, this seems to be correct in this instance since fr_name is part of a seq_buf-like structure monitoring the length: | struct fc_ns_rspn { | struct fc_ns_fid fr_fid; /* port ID object */ | __u8 fr_name_len; | char fr_name[]; | } __attribute__((__packed__)); So, this is really just a byte copy into a length-bounded string. Let's use memcpy() and mark the buffers as __nonstring. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] Link: https://github.com/KSPP/linux/issues/90 Cc: linux-hardening@vger.kernel.org Signed-off-by: Justin Stitt --- Note: build-tested only. Found with: $ rg "strncpy\(" --- drivers/scsi/libfc/fc_encode.h | 8 ++++---- include/uapi/scsi/fc/fc_ns.h | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/scsi/libfc/fc_encode.h b/drivers/scsi/libfc/fc_encode.h index 7dcac3b6baa7..ba8cc4ee650c 100644 --- a/drivers/scsi/libfc/fc_encode.h +++ b/drivers/scsi/libfc/fc_encode.h @@ -140,8 +140,8 @@ static inline int fc_ct_ns_fill(struct fc_lport *lport, ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rspn) + len, FC_FST_DIR, FC_NS_SUBTYPE); hton24(ct->payload.spn.fr_fid.fp_fid, lport->port_id); - strncpy(ct->payload.spn.fr_name, - fc_host_symbolic_name(lport->host), len); + memcpy(ct->payload.spn.fr_name, + fc_host_symbolic_name(lport->host), len); ct->payload.spn.fr_name_len = len; break; @@ -150,8 +150,8 @@ static inline int fc_ct_ns_fill(struct fc_lport *lport, ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rsnn) + len, FC_FST_DIR, FC_NS_SUBTYPE); put_unaligned_be64(lport->wwnn, &ct->payload.snn.fr_wwn); - strncpy(ct->payload.snn.fr_name, - fc_host_symbolic_name(lport->host), len); + memcpy(ct->payload.snn.fr_name, + fc_host_symbolic_name(lport->host), len); ct->payload.snn.fr_name_len = len; break; diff --git a/include/uapi/scsi/fc/fc_ns.h b/include/uapi/scsi/fc/fc_ns.h index 4cf0a40a099a..8a6f6c6b5213 100644 --- a/include/uapi/scsi/fc/fc_ns.h +++ b/include/uapi/scsi/fc/fc_ns.h @@ -145,7 +145,7 @@ struct fc_gid_pn_resp { */ struct fc_gspn_resp { __u8 fp_name_len; - char fp_name[]; + char fp_name[] __nonstring; }; /* @@ -171,7 +171,7 @@ struct fc_ns_rn_id { struct fc_ns_rsnn { __be64 fr_wwn; /* node name */ __u8 fr_name_len; - char fr_name[]; + char fr_name[] __nonstring; } __attribute__((__packed__)); /* @@ -180,7 +180,7 @@ struct fc_ns_rsnn { struct fc_ns_rspn { struct fc_ns_fid fr_fid; /* port ID object */ __u8 fr_name_len; - char fr_name[]; + char fr_name[] __nonstring; } __attribute__((__packed__)); /* --- base-commit: ffc253263a1375a65fa6c9f62a893e9767fbebfa change-id: 20231030-strncpy-drivers-scsi-libfc-fc_encode-h-5ad629f6de9c Best regards, -- Justin Stitt