From patchwork Tue Mar 5 23:34:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13583095 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F1F7412D74E for ; Tue, 5 Mar 2024 23:34:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709681686; cv=none; b=c8EuU4f9Uc01l8uUHvYYpmkvN5fcL6X+DE6elHP7yMJR+8/QRybZ3Qd2iDUNkGZH49+dAlmQ4JYbUNyF3OqCceB7CArcrkwogUfH0DYK26QBIFKZQNe8/ah8ZupaL/jsVEhfDEZBKQx2zJ6Y/sxQi+ybjd113TVNvDLiF9vEA7I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709681686; c=relaxed/simple; bh=MCTI5WU0q/OjSWo0DYnEwQ71Bx+4NnviT+l0Jn0Vwtw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=p8y7b2rmSwydlFqqnlONgHBzjQY6XPnJVK2MWqxuFBPPRqQ4A4jqpFVvVgMnZqdkOcXJOUbznASujsK8AulSM6kiepu5qUjFEvP5EL4AXFaPPvGNRALaZznLEEZGW21pV+nXKzCb1griS91o380EdwS1JTy2ikqKLZ8xB3PrXXk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=rcHm0lOD; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="rcHm0lOD" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60987370f06so56243307b3.1 for ; Tue, 05 Mar 2024 15:34:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709681682; x=1710286482; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=8xf2dq5GdDyi4s4WmIv33iVv6HbbqbNsFgxWnhqrXlo=; b=rcHm0lODl3Xe6qAaVQRO42Ws3xVfyYPFO7PNs0ASoo33BZFJHstOSnUWpe1FplxvMh dmFQ4KNP06yw1bJ/PEKcsYJP3HBNDtiYxuXoU2xnYeUmRvhWm0agTlRbVtdnj7I99BXY ngZhNTGFqzDu4v/gV2ehRjCUhhx5cDUba9XIbk6k9JkS1Rjo2wWaTkvH6M52OozTOvIj n7d8GC86lxPHyb/EOC+v6mwMyfQp8NjNOKdgZeWlK0QfT54D6ZTlUkWCStN/RPUtQ41r AQkb9eYUhkEnH/8U5kROraM/EAlVf6M4IMi0BGyIxk+lWVHQAuHug8gTwHsHhosNMi6Q df6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709681682; x=1710286482; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8xf2dq5GdDyi4s4WmIv33iVv6HbbqbNsFgxWnhqrXlo=; b=d4ODZ77pskT9TRw7KRVuO0Hs7+oU+Rquyeyq9CpSTKyxFXW3UBUB9P1Ye1aJ2FE73z sJ3fPLsVgXWwmK51w71JzMr6xw9oYV+PXOtesZxMN3NTWy6lU5KekTtfR3KLQd0tJjdf vTt83iTd9+J0swtBpN/P7jNBA5RXmfWRHcXevmXHYpcc8J6ArAh6a+9w3ZX/Kk4rj6Ra zUK2Rj1raVwWJ6j1eMEQt7tV+xCsp9K44N0O7c6f9/BtIbsf5WyjpQ6vWVBPXVz7NpiR BDj6iWEHs/HAODXnt6GuFQFe1bmhLQ+gv0T4T6Iq3t94HBq3SoAMLF08mPf5mh5LkPMu wPAg== X-Forwarded-Encrypted: i=1; AJvYcCXYnN8J0xHqID0IoGGOesJKJmBRLbiplGG/7EhlHkQAQz2pcAmNzD2QjwZgNtZYSd7t/+iGb1yIHgOCc1aby0r85cH6d/38e7/LSWj5yywY X-Gm-Message-State: AOJu0Yzzcl8OGKEBLkVshYrADIpuLmuR0/ejYnuvnFgUiD1EL6s5k9/g 2DR9Y+wft9AkXq0uLhlJP3xFN/39j1uELhib6/XxcVdckW0/8DtYsh3ADAgLdKdvi5+x8paEH7j 230qac/KByr9L8R2nk4WJBg== X-Google-Smtp-Source: AGHT+IGjXP7xV9ruZsbeD4aaELcTYAXgic0nA0Hrjs4+Y+HNFaz12SX4X0SEt6kHvtfxKoyr/JXIYEI4l6ExCkDDYA== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a25:2fc1:0:b0:dc6:ebd4:cca2 with SMTP id v184-20020a252fc1000000b00dc6ebd4cca2mr457031ybv.11.1709681682039; Tue, 05 Mar 2024 15:34:42 -0800 (PST) Date: Tue, 05 Mar 2024 23:34:36 +0000 In-Reply-To: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-0-5b78a13ff984@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-0-5b78a13ff984@google.com> X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1709681680; l=2052; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=MCTI5WU0q/OjSWo0DYnEwQ71Bx+4NnviT+l0Jn0Vwtw=; b=rnmRj8jm3GgjTZnumlpPOaOkMDVx4IdzWrhmcsZBaHHv305Z07EM4eMErQjpOQbvDLsSNgotZ fSo9MlyGZrDCLxYGXzFNWqNQYlSPCU1xKVTxKIi/dth8EzpyrB/5tvE X-Mailer: b4 0.12.3 Message-ID: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-1-5b78a13ff984@google.com> Subject: [PATCH v3 1/7] scsi: mpi3mr: replace deprecated strncpy with assignments From: Justin Stitt To: Sathya Prakash Veerichetty , Kashyap Desai , Sumit Saxena , Sreekanth Reddy , "James E.J. Bottomley" , "Martin K. Petersen" , Suganath Prabu Subramani , Ariel Elior , Manish Chopra , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Saurav Kashyap , Javed Hasan , GR-QLogic-Storage-Upstream@marvell.com, Nilesh Javali , Manish Rangankar , Don Brace Cc: mpi3mr-linuxdrv.pdl@broadcom.com, linux-scsi@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , MPT-FusionLinux.pdl@broadcom.com, netdev@vger.kernel.org, storagedev@microchip.com, Justin Stitt Really, there's no bug with the current code. Let's just ditch strncpy() all together. We can just copy the const strings instead of reserving room on the stack. 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 Reviewed-by: Kees Cook Signed-off-by: Justin Stitt --- changes from v1->v2: * use const char* assignments rather than strscpy (thanks Finn+Kees) --- drivers/scsi/mpi3mr/mpi3mr_fw.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/scsi/mpi3mr/mpi3mr_fw.c b/drivers/scsi/mpi3mr/mpi3mr_fw.c index 528f19f782f2..da0710cdac1d 100644 --- a/drivers/scsi/mpi3mr/mpi3mr_fw.c +++ b/drivers/scsi/mpi3mr/mpi3mr_fw.c @@ -3676,7 +3676,7 @@ static const struct { * mpi3mr_print_ioc_info - Display controller information * @mrioc: Adapter instance reference * - * Display controller personalit, capability, supported + * Display controller personality, capability, supported * protocols etc. * * Return: Nothing @@ -3685,20 +3685,20 @@ static void mpi3mr_print_ioc_info(struct mpi3mr_ioc *mrioc) { int i = 0, bytes_written = 0; - char personality[16]; + const char *personality; char protocol[50] = {0}; char capabilities[100] = {0}; struct mpi3mr_compimg_ver *fwver = &mrioc->facts.fw_ver; switch (mrioc->facts.personality) { case MPI3_IOCFACTS_FLAGS_PERSONALITY_EHBA: - strncpy(personality, "Enhanced HBA", sizeof(personality)); + personality = "Enhanced HBA"; break; case MPI3_IOCFACTS_FLAGS_PERSONALITY_RAID_DDR: - strncpy(personality, "RAID", sizeof(personality)); + personality = "RAID"; break; default: - strncpy(personality, "Unknown", sizeof(personality)); + personality = "Unknown"; break; } From patchwork Tue Mar 5 23:34:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13583096 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2B16112D779 for ; Tue, 5 Mar 2024 23:34:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709681686; cv=none; b=KG5h7Jm0ZDeT5yqg/bEr0xj/95/vAi3zOs7l/ZaR2UJzNfFUfvbxhuiB1XuiC5nZl8+iz369tE4m6UdO4G0nYZGa9pRzQNVTTGKcXyMEpgbVr7465w2HNSap6UG4P2AYlwMmRXjb08bZZdRcsIISvc30ZT1A31cqmAThenmyBFQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709681686; c=relaxed/simple; bh=mjGXUPscyzpVG3/YOto1FjCGkFQQDTE0gGonriHY98w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KD3VYwrYQHLu2fUk3wKGLb8u0sWCmucVmEzjT3zIRHtQBpO/gqHSVJNyXSgBYm1L821yGdWQYwTRsizFEvswci7wtnr7Tx5mv9R5BPE38MmzITOjh3vL6wxIuwi9QRNHSl5yStlPL3+HKzbe0tSUUbtT9HEDG5PCLPLmgpovvt8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=YxAaMzU7; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YxAaMzU7" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-608e88ca078so96962997b3.1 for ; Tue, 05 Mar 2024 15:34:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709681683; x=1710286483; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=1gASbbpl9VKufW/p1eS9vopJ2flHWgODS8RPQYfJDec=; b=YxAaMzU7fzDLbVZY/bUoeW8wicv1vVQiDOMmwKSo6tazFLD/BETZZ5MLU6MMFzOtXE a/YoMWENXrastmFTbcArIzkRlbGZVuKVC3zuDig2S/nyFUPt3wUqKLSBEzl31pf8Kd+k ZBT+tJEmk97NVYt7Wl1lUya4t6bF2pDvMdZEC9ZFHUlJDMeiUh82UffLrcLqrFgTjPWu mnsmDq0qyJ1UfXMyIY+TAJ+ITdlnj2MoHV08YfpPT+oUf1vxBh0f9u45VCrTIwA+oxqz F0yHsykELtc9+yBYvJ3tRK67n94F802GwU44Mkzm4epExbyeaBDMclqvqfK2tjml9AFv E2wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709681683; x=1710286483; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=1gASbbpl9VKufW/p1eS9vopJ2flHWgODS8RPQYfJDec=; b=q/OgRU58Dh7OWJ4s0RfaV5bWdQGVgx7NX58SF5g/I9RY937rCxq0BBDfaX/1ZCJNji eU2dqueabkAsgOtTDTaMR9mY9ad5vFqXoOor9v/Bq4aV0woKpmJtv/zo2dZ735JPXzxL 5MGnyLWXZD/5/7z3P0cG2O94F5/9TW+YopswKkJgaV+/kMKbQxMxYk101ifsXdK6gjav kOZdAHAxQsXfHw62oyHRwgvtBBBhJKveIqrGhxo1t3+7lTSneer1XqULSfFCvHTKJLm6 hz0uHrtG/ciNjbQrQXbYqgJyV0+u0PybWwRRaf1ZhYxJEJEcmsEkTFAnUfW+DJghAquc Krsg== X-Forwarded-Encrypted: i=1; AJvYcCULcIOaQLnx7x4qd6h8FjOGAujvEd0Fz4ZzNa6FjIu7gtnkXnlYNLjKMYYKAdZO11RP/bOpD2WttarPtspdws/5d2K/YTE7jioT0TJojL4O X-Gm-Message-State: AOJu0YxyfFDpqhXe3t4rUw11trD8iJm0/fIJYiDfoM1w7PnDqX4E0aQ0 AqOFdEjhxJdJq/HQYLFGRKnaNg4lCvL9fTyMJc3tBJkyWeFSmVyDLarblS+nxt2xJCFvdx1DAiO bCMkfYEHJv9HzT/hM9T3mfw== X-Google-Smtp-Source: AGHT+IGJFBT6ZMDA0dEeZzTBzmKy73noTis25Zy8vUrUgJBFhRjua/3NOgVhMlB7JHdd5gSR/MoWCpWVy9rzF1YapA== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a25:dbc9:0:b0:dc6:dfc6:4207 with SMTP id g192-20020a25dbc9000000b00dc6dfc64207mr3308643ybf.10.1709681683257; Tue, 05 Mar 2024 15:34:43 -0800 (PST) Date: Tue, 05 Mar 2024 23:34:37 +0000 In-Reply-To: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-0-5b78a13ff984@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-0-5b78a13ff984@google.com> X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1709681680; l=2775; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=mjGXUPscyzpVG3/YOto1FjCGkFQQDTE0gGonriHY98w=; b=TMLnCvjFVMOwkdWXvxNNyfUCUsW2BsfpzmfuS/mjEg68OQGl6XD54ss0HA47iLLTV8RsqqduT qpps1Q+twCxDAMKk4tqCF9naIPIg6ZUUIkn8VVDTVqEiwCve9jn4uml X-Mailer: b4 0.12.3 Message-ID: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-2-5b78a13ff984@google.com> Subject: [PATCH v3 2/7] scsi: mpt3sas: replace deprecated strncpy with strscpy From: Justin Stitt To: Sathya Prakash Veerichetty , Kashyap Desai , Sumit Saxena , Sreekanth Reddy , "James E.J. Bottomley" , "Martin K. Petersen" , Suganath Prabu Subramani , Ariel Elior , Manish Chopra , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Saurav Kashyap , Javed Hasan , GR-QLogic-Storage-Upstream@marvell.com, Nilesh Javali , Manish Rangankar , Don Brace Cc: mpi3mr-linuxdrv.pdl@broadcom.com, linux-scsi@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , MPT-FusionLinux.pdl@broadcom.com, netdev@vger.kernel.org, storagedev@microchip.com, Justin Stitt The replacement in mpt3sas_base.c is a trivial one because desc is already zero-initialized meaning there is no functional change here. For mpt3sas_transport.c, we know edev is zero-initialized as well while manufacture_reply comes from dma_alloc_coherent(). No functional change here either. For all cases, use the more idiomatic strscpy() usage of: strscpy(dest, src, sizeof(dest)) Reviewed-by: Kees Cook Signed-off-by: Justin Stitt --- drivers/scsi/mpt3sas/mpt3sas_base.c | 2 +- drivers/scsi/mpt3sas/mpt3sas_transport.c | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/drivers/scsi/mpt3sas/mpt3sas_base.c b/drivers/scsi/mpt3sas/mpt3sas_base.c index 8761bc58d965..c1e421cb8533 100644 --- a/drivers/scsi/mpt3sas/mpt3sas_base.c +++ b/drivers/scsi/mpt3sas/mpt3sas_base.c @@ -4774,7 +4774,7 @@ _base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc) char desc[17] = {0}; u32 iounit_pg1_flags; - strncpy(desc, ioc->manu_pg0.ChipName, 16); + strscpy(desc, ioc->manu_pg0.ChipName, sizeof(desc)); ioc_info(ioc, "%s: FWVersion(%02d.%02d.%02d.%02d), ChipRevision(0x%02x)\n", desc, (ioc->facts.FWVersion.Word & 0xFF000000) >> 24, diff --git a/drivers/scsi/mpt3sas/mpt3sas_transport.c b/drivers/scsi/mpt3sas/mpt3sas_transport.c index 421ea511b664..76f9a9177198 100644 --- a/drivers/scsi/mpt3sas/mpt3sas_transport.c +++ b/drivers/scsi/mpt3sas/mpt3sas_transport.c @@ -458,17 +458,17 @@ _transport_expander_report_manufacture(struct MPT3SAS_ADAPTER *ioc, goto out; manufacture_reply = data_out + sizeof(struct rep_manu_request); - strncpy(edev->vendor_id, manufacture_reply->vendor_id, - SAS_EXPANDER_VENDOR_ID_LEN); - strncpy(edev->product_id, manufacture_reply->product_id, - SAS_EXPANDER_PRODUCT_ID_LEN); - strncpy(edev->product_rev, manufacture_reply->product_rev, - SAS_EXPANDER_PRODUCT_REV_LEN); + strscpy(edev->vendor_id, manufacture_reply->vendor_id, + sizeof(edev->vendor_id)); + strscpy(edev->product_id, manufacture_reply->product_id, + sizeof(edev->product_id)); + strscpy(edev->product_rev, manufacture_reply->product_rev, + sizeof(edev->product_rev)); edev->level = manufacture_reply->sas_format & 1; if (edev->level) { - strncpy(edev->component_vendor_id, - manufacture_reply->component_vendor_id, - SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN); + strscpy(edev->component_vendor_id, + manufacture_reply->component_vendor_id, + sizeof(edev->component_vendor_id)); tmp = (u8 *)&manufacture_reply->component_id; edev->component_id = tmp[0] << 8 | tmp[1]; edev->component_revision_id = From patchwork Tue Mar 5 23:34:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13583098 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4369712E1C5 for ; Tue, 5 Mar 2024 23:34:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709681688; cv=none; b=pVWYORPFTboZhIyNQW6pLalAZ1QkmwyegkVBkoVNBrELpCCVU4YE8/4qI5KqIILfZdSnDWKO9fnr8VDgw/x8RBf7DwidbURyT2hrJYUMed2ASipmdQ2k+7br/jIrEvTk1ucqdTvBN3DgahiXAwA/ckCTeHZF618q4KyysrveTsM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709681688; c=relaxed/simple; bh=oKHJz8KX639JHaY4FxkGeuRgysM8ixCYP31PDePvR/I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=B5qB/cPyJB1FSrB3lBPPYydbnH6E+TH3l9EYXoNPpSFsMFG6sxVwmQei8obnh30HRoKbsKS8/Hy4AQN6xzjeOA+i/78AlOWXg2BD8sF8AkpEzu7CQx6R1efJgd1MoiK4xSfvY3NvJ40+nzOXztdGyIZYojXjjmKA78h7xhCA39w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ht2ltYkY; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ht2ltYkY" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60948e99cd4so96416567b3.1 for ; Tue, 05 Mar 2024 15:34:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709681684; x=1710286484; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=R/Gd0jRGfWXRd5LSFgmnGhf286BOGmU7x4htLcq4bds=; b=ht2ltYkYovKDw+rkhfKf8Wo+0OReFa20ICI9kZuXwFDMwVBueoyUI2Co2xDjAT9IYI lDc6J1WERayjV/yvNVmXpUSk87eJjeHbTTQYQM2IuRmRqh5cD3DmgEaSLNpzUZ0Zohx3 94mzdXh9Tnpu2qyEAQJvnTjScRK8M2LbLg0/sT+DkfrsKFn9tnhUvuuBzkXhHZEI5ifV /tfgs2mMWdRuoqTbhcQeXrKvn6nFJkPGNPJL3kcIHPgJbU+e42nQAmcTcYfCZNIxrnyV 8rY5yq8cB8M5gerkAwrXS4tKg2FbeTf1dW9BW/413/2T9IC4K1EoOUrEjsGb5nRTp2Ck Jq6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709681684; x=1710286484; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=R/Gd0jRGfWXRd5LSFgmnGhf286BOGmU7x4htLcq4bds=; b=DEcK0XVEzrq4cRoeLmI+o6Ranfqkc/LlZGoAo4KO53fw6SsjyvbWsVhTk82e4CyXJe Ib1mvaqjQdmZVI/xi7AgyoKW7Q60tJ/EsmrhpceGy8/IeZpL6dxoGW5ii5ms+Qb7jr+K EcTHsC4bRXvDT7yHBdO1Nf5o0a8Wz3bA4GOvCt8pPQ1EhZkrnzCqkwROcXDVMZhWKd1N /llqlhA8SB5ktxkurma0DeHpL3Ov3OOBW8Y7JjeA4PQzVN/rK6sJspOnBjPf/FVZrh/3 c3+0hzRhcN38JQVAwPDGtzaStfNxmL+17pIDDu0Io433bv/MkE0nMRYMzXOV7YQvc3n2 //Dw== X-Forwarded-Encrypted: i=1; AJvYcCUDzQ3PjABb+9Ydp9Yzn3U0ihXaMFu5lMWtwRWznBb8b2jZle4YCG+jiCsbXolA6VSVfccBAicJl6sMlVlzrDC2Qk60DsZKDEQ3d94Uxw1z X-Gm-Message-State: AOJu0YwZQNku2yO9OiILwWa8mZJafZoLxTUgNGzLyU20NclHd8Op9Vbl pzdtCvRQ5HF293DHBcKWSILR5BT13hZbP32QBmLFQGDYNFrgq3/j2+CA4VZZg26b/ESXaOA3cCf +YA4oByWVZVo7W8s8mQnOMQ== X-Google-Smtp-Source: AGHT+IGAp9/lowPpevoGgxO6AVRrNKa1EDrxjPg50w1kHJVDcThAxX6KQnG119Aw9qN/A+OBNDlix25FOCb265goww== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a81:7947:0:b0:609:359a:9313 with SMTP id u68-20020a817947000000b00609359a9313mr2995157ywc.1.1709681684397; Tue, 05 Mar 2024 15:34:44 -0800 (PST) Date: Tue, 05 Mar 2024 23:34:38 +0000 In-Reply-To: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-0-5b78a13ff984@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-0-5b78a13ff984@google.com> X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1709681680; l=2333; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=oKHJz8KX639JHaY4FxkGeuRgysM8ixCYP31PDePvR/I=; b=it20B8oEmrPPFj34tDnAo4Ojh4hPjBzwi2qKfmpt/w+DdvBX6HYJceOYTm/Cy2VY/qxpBA2UD lqH/vh4t55GDuJiu/Zvhhq9hXwO+V8wMcpojq7qCbWnxBJHE5ZwhMEb X-Mailer: b4 0.12.3 Message-ID: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-3-5b78a13ff984@google.com> Subject: [PATCH v3 3/7] scsi: qedf: replace deprecated strncpy with strscpy From: Justin Stitt To: Sathya Prakash Veerichetty , Kashyap Desai , Sumit Saxena , Sreekanth Reddy , "James E.J. Bottomley" , "Martin K. Petersen" , Suganath Prabu Subramani , Ariel Elior , Manish Chopra , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Saurav Kashyap , Javed Hasan , GR-QLogic-Storage-Upstream@marvell.com, Nilesh Javali , Manish Rangankar , Don Brace Cc: mpi3mr-linuxdrv.pdl@broadcom.com, linux-scsi@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , MPT-FusionLinux.pdl@broadcom.com, netdev@vger.kernel.org, storagedev@microchip.com, Justin Stitt We expect slowpath_params.name to be NUL-terminated based on its future usage with other string APIs: | static int qed_slowpath_start(struct qed_dev *cdev, | struct qed_slowpath_params *params) ... | strscpy(drv_version.name, params->name, | MCP_DRV_VER_STR_SIZE - 4); Moreover, NUL-padding is not necessary as the only use for this slowpath name parameter is to copy into the drv_version.name field. Also, let's prefer using strscpy(src, dest, sizeof(src)) in two instances (one of which is outside of the scsi system but it is trivial and related to this patch). We can see the drv_version.name size here: | struct qed_mcp_drv_version { | u32 version; | u8 name[MCP_DRV_VER_STR_SIZE - 4]; | }; Reviewed-by: Kees Cook Signed-off-by: Justin Stitt --- drivers/net/ethernet/qlogic/qed/qed_main.c | 2 +- drivers/scsi/qedf/qedf_main.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/net/ethernet/qlogic/qed/qed_main.c b/drivers/net/ethernet/qlogic/qed/qed_main.c index c278f8893042..d39e198fe8db 100644 --- a/drivers/net/ethernet/qlogic/qed/qed_main.c +++ b/drivers/net/ethernet/qlogic/qed/qed_main.c @@ -1351,7 +1351,7 @@ static int qed_slowpath_start(struct qed_dev *cdev, (params->drv_rev << 8) | (params->drv_eng); strscpy(drv_version.name, params->name, - MCP_DRV_VER_STR_SIZE - 4); + sizeof(drv_version.name)); rc = qed_mcp_send_drv_version(hwfn, hwfn->p_main_ptt, &drv_version); if (rc) { diff --git a/drivers/scsi/qedf/qedf_main.c b/drivers/scsi/qedf/qedf_main.c index a58353b7b4e8..fd12439cbaab 100644 --- a/drivers/scsi/qedf/qedf_main.c +++ b/drivers/scsi/qedf/qedf_main.c @@ -3468,7 +3468,7 @@ static int __qedf_probe(struct pci_dev *pdev, int mode) slowpath_params.drv_minor = QEDF_DRIVER_MINOR_VER; slowpath_params.drv_rev = QEDF_DRIVER_REV_VER; slowpath_params.drv_eng = QEDF_DRIVER_ENG_VER; - strncpy(slowpath_params.name, "qedf", QED_DRV_VER_STR_SIZE); + strscpy(slowpath_params.name, "qedf", sizeof(slowpath_params.name)); rc = qed_ops->common->slowpath_start(qedf->cdev, &slowpath_params); if (rc) { QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n"); From patchwork Tue Mar 5 23:34:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13583097 Received: from mail-io1-f73.google.com (mail-io1-f73.google.com [209.85.166.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F9AE12EBCB for ; Tue, 5 Mar 2024 23:34:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709681688; cv=none; b=Cn06aRlGg+uepQZqHwnAZMw1tuX33e9EIlwYiZcDnG/A2EXdpNGp4ZnQyBibv3U93dtYaJGidK+G7t2ieIydL/gKeS+ufVq3wvZRa2D16VsIhpeKyjeWYiRQm2jwWmbw9NPKsLQNIqWtC7wbHClHNFEpDLqgK+i8CRsPFErmnxk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709681688; c=relaxed/simple; bh=OsMSw4z6iWbxTXjZg30kuZBO6Vj6czVVXXM7+X4U6u0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tYK0sK51C+5lpXegxXDjRM5K5I0LG8Q1Evtg3eJ6Yt/+6jPVlzfmclCsxUgDsrkFXHRg5g+ux4amoZ4R4BbVmT9cGaG6z7bo1y+6wyCDRISkcURzYI2eUMsvLFMtfACfcaJWsKp64f7hiq+ivC7KCb5JsdyiWAQwDYJv/snx9/k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=n9Cl1Ynq; arc=none smtp.client-ip=209.85.166.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="n9Cl1Ynq" Received: by mail-io1-f73.google.com with SMTP id ca18e2360f4ac-7c00a1374ecso540574939f.1 for ; Tue, 05 Mar 2024 15:34:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709681685; x=1710286485; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=RCz/Py0Ns2mXKbBtgurW4Nm4I/8gG7F1BHKnQ6umRVY=; b=n9Cl1YnqtavUFx/nHKyUvlMLgiyCyrvc5GiNlDu/8dsOET9tLPbNfYjIQ7IE0ePgU2 TAY3mqLUeQ13yUrqMSOCxO36q0AXXQ3Jf5eES4omXjO5Lw/587CmnSYxCD3OCfAD4UU0 nrMHydCiinMFxWxQ1ugKOFp2h9C2Qskn/s8/TuCbdSbFMJfswMBdqkz5xw9TqHSW+FrH I/gXvLlvWqgk1zyjaZibNv8pRFxG1cOYJe3d+NoX78SMTJ4WlMNkqUKEQ3+flw6PZLsK H494wLgdpivqBd7VknN6DyhdcDIeug/F+YFNIC/zjoYootf6EV43vRGTqlmj0HL7/yyg EPsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709681685; x=1710286485; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RCz/Py0Ns2mXKbBtgurW4Nm4I/8gG7F1BHKnQ6umRVY=; b=gh3qk+MXDI08WkNfZ5Y4xoNrZ6A9r8o2/EzAVyKTolBQ+aCZS1E/0qdexpfNbfQGSE B6Fg88bUG3pZIGFe+QmMnjs5gQs1mh3NTQHSNwlEQKws+jnbknaym3CfxeYWaHGmTHgd bwftMFRtji1IzQ4NPI8/bmi4D45oyq1XWZzEYsxEN0Dkf//5JV2AQE5NdFr8ybx1M+F5 p8mSgLN0OxTgirXMZl7UecJ9HCzKuA4hiJY0LrpXDQM8qsM5QctSZ3QCyYqZgPDmMdnd MMyXtxI+VYQG8ByVYBNiVBb7uOuoLyPu2zSijmk3bc+kUVIEI8nJkKH+ccy0WXwJXZbi 1ScA== X-Forwarded-Encrypted: i=1; AJvYcCVG0OvLcc66m37cXiOL/Ljy9Ehw1fdlfc0U3djyh6Rluv4bUOUHdwHJtVh99scNPfI4UZGenCFOMFequXJ+me+rTSrFDrOk2P4oF5UcnnId X-Gm-Message-State: AOJu0Yzrk1e0nJrIn/PWQ03KthpMLP/XvabsxJrFnuDDiQznqYhiisCS 54r3Jacu4+oa6fYYkXNkqxCdTZOQyWGZNxsSo3I+wA0pINkNUNR9p59gW5mU1qJnpRT88WMRbcs zuH6+/3sRs6RaGLaKel4vBg== X-Google-Smtp-Source: AGHT+IHg0I14q/uq1iOXxVZ/7MUvTrMtE2B2cT+hC15fDY+B3hGbY752uu2ZyIfVlk86YppQd1jP3G63j24k66Vpxw== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6602:e0d:b0:7c8:264a:8c45 with SMTP id gp13-20020a0566020e0d00b007c8264a8c45mr72249iob.4.1709681685585; Tue, 05 Mar 2024 15:34:45 -0800 (PST) Date: Tue, 05 Mar 2024 23:34:39 +0000 In-Reply-To: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-0-5b78a13ff984@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-0-5b78a13ff984@google.com> X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1709681680; l=6820; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=OsMSw4z6iWbxTXjZg30kuZBO6Vj6czVVXXM7+X4U6u0=; b=yFz+ZNCjXqzkg2Y1+WAxa057MaLfkBRd65XWFzulVwXxiDf5n3QvmIwTtlL8S/RT1Ao0b6bub SgIj+Z7PxHyCyQr7LEoG9QIUAtNgzOLVs4yZd3iG5z0q/fg43YRZnzG X-Mailer: b4 0.12.3 Message-ID: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-4-5b78a13ff984@google.com> Subject: [PATCH v3 4/7] scsi: qla4xxx: replace deprecated strncpy with strscpy From: Justin Stitt To: Sathya Prakash Veerichetty , Kashyap Desai , Sumit Saxena , Sreekanth Reddy , "James E.J. Bottomley" , "Martin K. Petersen" , Suganath Prabu Subramani , Ariel Elior , Manish Chopra , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Saurav Kashyap , Javed Hasan , GR-QLogic-Storage-Upstream@marvell.com, Nilesh Javali , Manish Rangankar , Don Brace Cc: mpi3mr-linuxdrv.pdl@broadcom.com, linux-scsi@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , MPT-FusionLinux.pdl@broadcom.com, netdev@vger.kernel.org, storagedev@microchip.com, Justin Stitt Replace 3 instances of strncpy in ql4_mbx.c No bugs exist in the current implementation as some care was taken to ensure the write length was decreased by one to leave some space for a NUL-byte. However, instead of using strncpy(dest, src, LEN-1) we can opt for strscpy(dest, src, sizeof(dest)) which will result in NUL-termination as well. It should be noted that the entire chap_table is zero-allocated so the NUL-padding provided by strncpy is not needed. While here, I noticed that MIN_CHAP_SECRET_LEN was not used anywhere. Since strscpy gives us the number of bytes copied into the destination buffer (or an -E2BIG) we can check both for an error during copying and also for a non-length compliant secret. Add a new jump label so we can properly clean up our chap_table should we have to abort due to bad secret. The third instance in this file involves some more peculiar handling of strings: | uint32_t mbox_cmd[MBOX_REG_COUNT]; | ... | memset(&mbox_cmd, 0, sizeof(mbox_cmd)); | ... | mbox_cmd[0] = MBOX_CMD_SET_PARAM; | if (param == SET_DRVR_VERSION) { | mbox_cmd[1] = SET_DRVR_VERSION; | strncpy((char *)&mbox_cmd[2], QLA4XXX_DRIVER_VERSION, | MAX_DRVR_VER_LEN - 1); mbox_cmd has a size of 8: | #define MBOX_REG_COUNT 8 ... and its type width is 4 bytes. Hence, we have 32 bytes to work with here. The first 4 bytes are used as a flag for the MBOX_CMD_SET_PARAM. The next 4 bytes are used for SET_DRVR_VERSION. We now have 32-8=24 bytes remaining -- which thankfully is what MAX_DRVR_VER_LEN is equal to | #define MAX_DRVR_VER_LEN 24 ... and the thing we're copying into this pseudo-string buffer is | #define QLA4XXX_DRIVER_VERSION "5.04.00-k6" ... which is great because its less than 24 bytes (therefore we aren't truncating the source). All to say, there's no bug in the existing implementation (yay!) but we can clean the code up a bit by using strscpy(). In ql4_os.c, there aren't any strncpy() uses to replace but there are some existing strscpy() calls that could be made more idiomatic. Where possible, use strscpy(dest, src, sizeof(dest)). Note that chap_rec->password has a size of ISCSI_CHAP_AUTH_SECRET_MAX_LEN | #define ISCSI_CHAP_AUTH_SECRET_MAX_LEN 256 ... while the current strscpy usage uses QL4_CHAP_MAX_SECRET_LEN | #define QL4_CHAP_MAX_SECRET_LEN 100 ... however since chap_table->secret was set and bounded properly in its string assignment its probably safe here to switch over to sizeof(). | struct iscsi_chap_rec { ... | char username[ISCSI_CHAP_AUTH_NAME_MAX_LEN]; | uint8_t password[ISCSI_CHAP_AUTH_SECRET_MAX_LEN]; ... | }; | strscpy(chap_rec->password, chap_table->secret, | QL4_CHAP_MAX_SECRET_LEN); Reviewed-by: Kees Cook Signed-off-by: Justin Stitt --- drivers/scsi/qla4xxx/ql4_mbx.c | 17 ++++++++++++----- drivers/scsi/qla4xxx/ql4_os.c | 14 +++++++------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/drivers/scsi/qla4xxx/ql4_mbx.c b/drivers/scsi/qla4xxx/ql4_mbx.c index 249f1d7021d4..75125d2021f5 100644 --- a/drivers/scsi/qla4xxx/ql4_mbx.c +++ b/drivers/scsi/qla4xxx/ql4_mbx.c @@ -1641,6 +1641,7 @@ int qla4xxx_set_chap(struct scsi_qla_host *ha, char *username, char *password, struct ql4_chap_table *chap_table; uint32_t chap_size = 0; dma_addr_t chap_dma; + ssize_t secret_len; chap_table = dma_pool_zalloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma); if (chap_table == NULL) { @@ -1652,9 +1653,13 @@ int qla4xxx_set_chap(struct scsi_qla_host *ha, char *username, char *password, chap_table->flags |= BIT_6; /* peer */ else chap_table->flags |= BIT_7; /* local */ - chap_table->secret_len = strlen(password); - strncpy(chap_table->secret, password, MAX_CHAP_SECRET_LEN - 1); - strncpy(chap_table->name, username, MAX_CHAP_NAME_LEN - 1); + + secret_len = strscpy(chap_table->secret, password, + sizeof(chap_table->secret)); + if (secret_len < MIN_CHAP_SECRET_LEN) + goto cleanup_chap_table; + chap_table->secret_len = (uint8_t)secret_len; + strscpy(chap_table->name, username, sizeof(chap_table->name)); chap_table->cookie = cpu_to_le16(CHAP_VALID_COOKIE); if (is_qla40XX(ha)) { @@ -1679,6 +1684,8 @@ int qla4xxx_set_chap(struct scsi_qla_host *ha, char *username, char *password, memcpy((struct ql4_chap_table *)ha->chap_list + idx, chap_table, sizeof(struct ql4_chap_table)); } + +cleanup_chap_table: dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma); if (rval != QLA_SUCCESS) ret = -EINVAL; @@ -2281,8 +2288,8 @@ int qla4_8xxx_set_param(struct scsi_qla_host *ha, int param) mbox_cmd[0] = MBOX_CMD_SET_PARAM; if (param == SET_DRVR_VERSION) { mbox_cmd[1] = SET_DRVR_VERSION; - strncpy((char *)&mbox_cmd[2], QLA4XXX_DRIVER_VERSION, - MAX_DRVR_VER_LEN - 1); + strscpy((char *)&mbox_cmd[2], QLA4XXX_DRIVER_VERSION, + MAX_DRVR_VER_LEN); } else { ql4_printk(KERN_ERR, ha, "%s: invalid parameter 0x%x\n", __func__, param); diff --git a/drivers/scsi/qla4xxx/ql4_os.c b/drivers/scsi/qla4xxx/ql4_os.c index 675332e49a7b..17cccd14765f 100644 --- a/drivers/scsi/qla4xxx/ql4_os.c +++ b/drivers/scsi/qla4xxx/ql4_os.c @@ -799,10 +799,10 @@ static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx, chap_rec->chap_tbl_idx = i; strscpy(chap_rec->username, chap_table->name, - ISCSI_CHAP_AUTH_NAME_MAX_LEN); - strscpy(chap_rec->password, chap_table->secret, - QL4_CHAP_MAX_SECRET_LEN); - chap_rec->password_length = chap_table->secret_len; + sizeof(chap_rec->username)); + chap_rec->password_length = strscpy(chap_rec->password, + chap_table->secret, + sizeof(chap_rec->password)); if (chap_table->flags & BIT_7) /* local */ chap_rec->chap_type = CHAP_TYPE_OUT; @@ -6291,8 +6291,8 @@ static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry, tddb->tpgt = sess->tpgt; tddb->port = conn->persistent_port; - strscpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE); - strscpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN); + strscpy(tddb->iscsi_name, sess->targetname, sizeof(tddb->iscsi_name)); + strscpy(tddb->ip_addr, conn->persistent_address, sizeof(tddb->ip_addr)); } static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry, @@ -7792,7 +7792,7 @@ static int qla4xxx_sysfs_ddb_logout(struct iscsi_bus_flash_session *fnode_sess, } strscpy(flash_tddb->iscsi_name, fnode_sess->targetname, - ISCSI_NAME_SIZE); + sizeof(flash_tddb->iscsi_name)); if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4)) sprintf(flash_tddb->ip_addr, "%pI6", fnode_conn->ipaddress); From patchwork Tue Mar 5 23:34:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13583099 Received: from mail-io1-f74.google.com (mail-io1-f74.google.com [209.85.166.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 76DB112FF73 for ; Tue, 5 Mar 2024 23:34:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709681689; cv=none; b=U0YDdiCeWaaDsyW7N0fwzRy8yVKpEz7c/hzcp7tVQBUQ1P8dVm3ymctyBG0OaWZjxrZ/D04iXhahMdySnUr/y9aaPh/S5ytOF4VNKJsWTCvlG8/fR3Dk0hhhRUaaj1BMEUJTBvsqhDqEM2npoJHtxCiJd/xZuilHMLqT7OIjfgw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709681689; c=relaxed/simple; bh=czOD/j/ywOqIqWqQCCJmduuSaKii8DftYSYMMRvzuOY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sp+dl3/aalPT1lIiLq/bHswTRAE2yiOpofMsJzALVqoK1YSbiXZj+w8pAowytjLF0Fk0+cPVT8gsHYQED5TqF/yO/F1re+H/ZR5a+YjUufTiCvJgoCJMg0fgi779CCuZSKMpcQm60sXfapXSx7hAQgX4nyQxqrlUzPnkxl2kPqA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=atuqRhP8; arc=none smtp.client-ip=209.85.166.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="atuqRhP8" Received: by mail-io1-f74.google.com with SMTP id ca18e2360f4ac-7bc2b7bef65so651949239f.0 for ; Tue, 05 Mar 2024 15:34:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709681687; x=1710286487; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=r2W4dqEzBOijI8wbM/wyA2IQgHsKwReDWbrG7VxHa80=; b=atuqRhP8P/TfCwGQgNWKgapNSHdl5UkHCmHx425JwKp6B8Fe5+5fnqFvcjN2AL85zc OygZnvx09Y2IBmNcQStrBAZ+SyzwjnLaunxcP0YI0h93jUtEy/oK86UgOo6oIt5EdRgP Q3jayZlFB7drRgrv+8fncqp6haAiqpsuQTk79Dt5Q92IjVhD/0naz5+EWhSjOcO9p3Le gUfq0VS/3ju2UxQUant1r9mRXcG1S0XT5+cxFB+bP/LPByYn5wgJ1nsuXpanDV/X+aFc uudytaLSc+fF0pE5DDTR3y1o3AmyMOsAn2aZoAhtmHmr2VDLxIU1EpQ++5X8jQbAc4ob 203Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709681687; x=1710286487; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=r2W4dqEzBOijI8wbM/wyA2IQgHsKwReDWbrG7VxHa80=; b=cBNwSPBryBOCl6BoG3HeoiAnJKLHjzJRhk19gPfU7FF9WmB+feZcaMQXm3VcQBwP5d h2ab1lo5amY1zVcVRcbwq10BMjMD/U7T3ORM4IvtdsOIZTxBhzul9vbJrimLPAOl5hPE pS1GPUZr51juB5duBCByNPBG2qAwt4hGaKwXSXIGaMWvfL49FvCwxHi4Ir5rTa/KXsv2 kpzyqdKHZ2YbF4mizWl5Ehm3aa8Bhk834hb22zTA8lyZ1692EJg52xQ9DhFVXiuHans0 bA/lVB34lo/iihWKx/8sA80QdWuiA1X4HGOfszRjL+7hvm2hPEJXWA7GwNjNoOdyh5vW 81eg== X-Forwarded-Encrypted: i=1; AJvYcCX2INXDdbHV8J3Y8MlgQRNLdH6NbnOOSPoQ94NkCH5WqLyAhD76rdeu3YFfb6LDFeLKkfqeQfIGvead7QLddiw7ZtQZKRyBUUKlfznD/dOZ X-Gm-Message-State: AOJu0Yw4j0w0+tt9fU0BIX5g3EC2kRNA+yaEDb6lYW0/H+oWZCgve5zR SxD4Ny/2sKmj1zdS8nPQxAtwGAbcM+wWC+6x6Hv2vGicK7jzGPDcwu0Ql7BrN8Hq20q7k5LCYf1 YpaIL6GFgETVpNmAkbyFzRg== X-Google-Smtp-Source: AGHT+IFm3ISTtxwtmwSD9NEnQwVnyHnCYQ6FJqMFCjJEXEARRfVeZC5I1MMFbX1a4GaCMitdlTH/MvnY5lXWtY+oeQ== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6e02:20e8:b0:363:9d58:805b with SMTP id q8-20020a056e0220e800b003639d58805bmr867099ilv.3.1709681686790; Tue, 05 Mar 2024 15:34:46 -0800 (PST) Date: Tue, 05 Mar 2024 23:34:40 +0000 In-Reply-To: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-0-5b78a13ff984@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-0-5b78a13ff984@google.com> X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1709681680; l=1883; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=czOD/j/ywOqIqWqQCCJmduuSaKii8DftYSYMMRvzuOY=; b=DYOpEnSj/Og/vqiokTExFjD71LvtbrcYqxy9v6/RMXqeXpYyUnnscjNm8adtp5Fg7YiqEAqDs 7RiN0Ed55KvCUdUYEjrv8wbckJ6nPxFHt4TVloBv7kJZQ1muojOhUAG X-Mailer: b4 0.12.3 Message-ID: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-5-5b78a13ff984@google.com> Subject: [PATCH v3 5/7] scsi: devinfo: replace strncpy and manual pad From: Justin Stitt To: Sathya Prakash Veerichetty , Kashyap Desai , Sumit Saxena , Sreekanth Reddy , "James E.J. Bottomley" , "Martin K. Petersen" , Suganath Prabu Subramani , Ariel Elior , Manish Chopra , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Saurav Kashyap , Javed Hasan , GR-QLogic-Storage-Upstream@marvell.com, Nilesh Javali , Manish Rangankar , Don Brace Cc: mpi3mr-linuxdrv.pdl@broadcom.com, linux-scsi@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , MPT-FusionLinux.pdl@broadcom.com, netdev@vger.kernel.org, storagedev@microchip.com, Justin Stitt Depending on the state of @compatible, we are going to do different things with our @to buffer. When @compatible is true we want a NUL-term'd and NUL-padded destination buffer. Conversely, if @compatible is false we just want a space-padded destination buffer (no NUL-term required). As per: /** * scsi_dev_info_list_add_keyed - add one dev_info list entry. * @compatible: if true, null terminate short strings. Otherwise space pad. ... Note that we can't easily use `strtomem_pad` here as the size of the @to buffer is unknown to the compiler due to indirection layers. Now, the intent of the code is more clear (I probably didn't even need to add a comment -- that's how clear it is). Reviewed-by: Kees Cook Signed-off-by: Justin Stitt --- drivers/scsi/scsi_devinfo.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/drivers/scsi/scsi_devinfo.c b/drivers/scsi/scsi_devinfo.c index 3fcaf10a9dfe..2d3dbce25629 100644 --- a/drivers/scsi/scsi_devinfo.c +++ b/drivers/scsi/scsi_devinfo.c @@ -293,14 +293,16 @@ static void scsi_strcpy_devinfo(char *name, char *to, size_t to_length, size_t from_length; from_length = strlen(from); - /* This zero-pads the destination */ - strncpy(to, from, to_length); - if (from_length < to_length && !compatible) { - /* - * space pad the string if it is short. - */ - memset(&to[from_length], ' ', to_length - from_length); - } + + /* + * null pad and null terminate if compatible + * otherwise space pad + */ + if (compatible) + strscpy_pad(to, from, to_length); + else + memcpy_and_pad(to, to_length, from, from_length, ' '); + if (from_length > to_length) printk(KERN_WARNING "%s: %s string '%s' is too long\n", __func__, name, from); From patchwork Tue Mar 5 23:34:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13583100 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC5D31332B1 for ; Tue, 5 Mar 2024 23:34:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709681690; cv=none; b=rL63Tvlpj6oOOKLhTXKSefTXIT8xRY2GKQoT8sHq5ZYxc4iVXHG90d+5eY22jUvEELLxyzLyEpzjZbbwdNqzVPmB6LwGMJ29/NLS627Ox9WzIgOVBshxnx9jBYEFfhozFoxn8mIPDKTYRLHAGm80w4b3r5AT22X9FFudul4ADsk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709681690; c=relaxed/simple; bh=eWczkNLyxUvTeaPj7py9PZ0UxpZQDpHPCSVdmRIMnbg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Mf4VVuK4/OdtH0V5BuRXJFQnjYQtUVkDHfDz121VYZsdRZ3TcHSfxXWCgS4E4aofIJu7LHAo4S1Ucydm0+V0oi5EfsL7LxV2Ai2sf9wAKulDzuoIYmkfM25driY971XAnFU9hLbspjRO/MmCLzde/jXxVZ+Tb+hD18MVOHyDXss= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=zR9ob2xc; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zR9ob2xc" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcc58cddb50so10848566276.0 for ; Tue, 05 Mar 2024 15:34:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709681688; x=1710286488; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NBdhLZXYYhg9dizdXkfUGWj63Hy4tFxJG87qbeyzQbo=; b=zR9ob2xcGrShZqxYwT3/twiY07zYJu9O9+Rs7kEedN7RC7B+elpSweLYmAxNyrLDxV 3jvNYJV0Qv/2wTnqDLz7SmQdGPXUQgsk9spWolUubtW/0DdORx9scGp6BRSeUPd4Pzki 47dS437pxMq9t6ehf019fY9+FVPRlKCtOo2Zqe6qwZbGEyBxQnTfPZ157uMCohmep2NC fOG23lDpP+Y4N/CUGv2fOqvjziKYFs0LXLy+Og46eztQyUfNZr0EKfeyWfABCvG9oyfl vTdvF5e86FdxGIrc6H5nrx8RowreyY4o2A/dwPrP4CUFZ1XY9j2EfsJ7q/0WUkTLjO3D B3wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709681688; x=1710286488; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NBdhLZXYYhg9dizdXkfUGWj63Hy4tFxJG87qbeyzQbo=; b=VldIWpCmMFvwYcDdn4OqquYx4J5f2TS80nwN7WuozcQoYL1Gz1VMM2qjpZlqNSXekl 8/pVWcD9SfMTRT5bqWlGn7yVaDQbmDSeEgCpCo8+gh3WpkVAJmIRDA42VPYzUg73irik HJdtEa8WincAvxOyOrAlQoLIJkVTzaptOtR8V6rcmCXO2/c+SmQSfc32h6IAEh/BzpAf XgXM5B1cjlNXX4jBWDvo2ZjVBLjyRzWvsc3isPt+qZ1Pv3q2JIyQ2Y+kQtPB4r3fFE6y rFrsmE97tu4FXEej6b8PKusZ/IUAaZ6yE1NeO+yQNJa32hWk6ibWoWNJJ7k1bnII3vZ1 ffdA== X-Forwarded-Encrypted: i=1; AJvYcCVUglwm6DiKh9DOXt+NZBj+OZ6+XZNtp733espbxtY+Q8k0U9/LFTc6pg7LmkrcI1XyUcSaFqAkhaGRYt7f26KBaAUulRwSfNyQ8o4bKfto X-Gm-Message-State: AOJu0Yzb1lIIFOepgfC5VYLmFLqoS053yyPKGs3ET7kDVEnxYK0Ck+JA AF9MrD3Yw+OPOtVO4NSiWHtpXksid1PKI5DYpMrRkXfcmTNXj3OI/P/BUIIzPbVCIWxdXG/EauU aBW39I+8ZrYbBa6UWH6HtGQ== X-Google-Smtp-Source: AGHT+IEcfC2kqUdFcpJebJibmkd3MEykdtlXV90KSAjS+crdwyms+df6pNbl1+KyifeufIg+Zet9xLlccF+YbtQizA== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6902:154b:b0:dc6:d678:371d with SMTP id r11-20020a056902154b00b00dc6d678371dmr3334366ybu.3.1709681688139; Tue, 05 Mar 2024 15:34:48 -0800 (PST) Date: Tue, 05 Mar 2024 23:34:41 +0000 In-Reply-To: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-0-5b78a13ff984@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-0-5b78a13ff984@google.com> X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1709681680; l=1485; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=eWczkNLyxUvTeaPj7py9PZ0UxpZQDpHPCSVdmRIMnbg=; b=GBgB6OjQexdWYhcQvFNShhxQybtX8760riu0Jt3EE6cComY2f+0docGRsCcs/GO1kkdvT5L1R 3Nmjh2x/65pBVPkRcC6uOERtx8MFRcINZrrZk6GV52DV1xNP8NRNWRa X-Mailer: b4 0.12.3 Message-ID: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-6-5b78a13ff984@google.com> Subject: [PATCH v3 6/7] scsi: smartpqi: replace deprecated strncpy with strscpy From: Justin Stitt To: Sathya Prakash Veerichetty , Kashyap Desai , Sumit Saxena , Sreekanth Reddy , "James E.J. Bottomley" , "Martin K. Petersen" , Suganath Prabu Subramani , Ariel Elior , Manish Chopra , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Saurav Kashyap , Javed Hasan , GR-QLogic-Storage-Upstream@marvell.com, Nilesh Javali , Manish Rangankar , Don Brace Cc: mpi3mr-linuxdrv.pdl@broadcom.com, linux-scsi@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , MPT-FusionLinux.pdl@broadcom.com, netdev@vger.kernel.org, storagedev@microchip.com, Justin Stitt buffer->driver_version is sized 32: | struct bmic_host_wellness_driver_version { | ... | char driver_version[32]; ... the source string "Linux " + DRIVER_VERISON is sized at 16. There's really no bug in the existing code since the buffers are sized appropriately with great care taken to manually NUL-terminate the destination buffer. Nonetheless, let's make the swap over to strscpy() for robustness' (and readability's) sake. Reviewed-by: Kees Cook Signed-off-by: Justin Stitt --- drivers/scsi/smartpqi/smartpqi_init.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/drivers/scsi/smartpqi/smartpqi_init.c b/drivers/scsi/smartpqi/smartpqi_init.c index ceff1ec13f9e..bfe6f42e8e96 100644 --- a/drivers/scsi/smartpqi/smartpqi_init.c +++ b/drivers/scsi/smartpqi/smartpqi_init.c @@ -1041,9 +1041,8 @@ static int pqi_write_driver_version_to_host_wellness( buffer->driver_version_tag[1] = 'V'; put_unaligned_le16(sizeof(buffer->driver_version), &buffer->driver_version_length); - strncpy(buffer->driver_version, "Linux " DRIVER_VERSION, - sizeof(buffer->driver_version) - 1); - buffer->driver_version[sizeof(buffer->driver_version) - 1] = '\0'; + strscpy(buffer->driver_version, "Linux " DRIVER_VERSION, + sizeof(buffer->driver_version)); buffer->dont_write_tag[0] = 'D'; buffer->dont_write_tag[1] = 'W'; buffer->end_tag[0] = 'Z'; From patchwork Tue Mar 5 23:34:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13583101 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C73C134751 for ; Tue, 5 Mar 2024 23:34:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709681691; cv=none; b=bdr6UisOt1Z4DChyFBIaTIBZgOCvU4UTBQXAj1RgXRYcWDZ+jWp6oAJ5BYgojVW9kxCx6mGZFmsOyCGIuj7KPyNec28AuFmkveq1jzXt3Lkr9P954WOQeD83Ar7UG1E2xBrjREAyfrvXZ/GpycYdLyQbQXmdUvTNlnsuF4+EBZk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709681691; c=relaxed/simple; bh=FcEcSMUZOi/4Oj8G2zljZCZLH3SJcgxPb0qa54lykhA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Dx0H5Pva1xb79TLf2I9RCdzUGcnLR5YjCy1LcjwzkdNzUp52nePhD18N7eVYaV8V132yM3y0ehXng6ZZ4YmZS+VaZ8XvL1SV+YjmPtWdIvhoL7/WSSTcTJE73IwDbyDcXVgFpD08SGEMF0in5Uxhupkh0WTuaenewm5olOgKfcI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sk4+kK4O; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sk4+kK4O" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc3645a6790so2289917276.0 for ; Tue, 05 Mar 2024 15:34:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709681689; x=1710286489; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=LsGEY9f3OqM6eFdMybydMZbUAGFeLoZyzbi8Mpvwi9w=; b=sk4+kK4OEtHjRB4RdWR/f4HFlTaYxrWMs3oYAilZqqqSTiLt3y0J6XN3zbILtB32+0 /XZPJ/eaxgY8bDFtYRWdngUaVo7rE7llv13BWh3kgss2+6zKOrItFggOACk/UmZYatA+ +At8Khwx+U56gUVqO8C8yBpbEzCnvU8V+JAlwtJHB0rYdqvpa9AAHRRPxBHEfEWtNrgn Gk5rVR9FiqpvwTNVy5ii7PNTvR8ftr4THmxH/dKM4cKydDHnvmD8ZspPuQ3r0DCrwQ+C Y5Uu0dJM8DgL6a5Qsn8qB9eBbnDpnHWbp9E/igbXcpKHk3Fm545P1XdND/poPQOL/0dP XgUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709681689; x=1710286489; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=LsGEY9f3OqM6eFdMybydMZbUAGFeLoZyzbi8Mpvwi9w=; b=XB2fY96Xpcraqw4WOnCNXOvcEuqmox0t5IX7MLray8SIHM/iCJlC2kqgbwGAAudQRt /8+/7wUkbr5rPsGjzCZrpMD8SivEtCS9gh8eTbo0GmW1CX9NdbTW2nNY542AIsKDHK+x rmqNnOeGGvKJj3b84zQfn5CohEV2Gu/nhCihvamzmgIN1ECyFjfNTGSlSZK91XIi1Ik1 yryfvdDzP5y0LJVv6vy2M6NFOc3DjJdf7CJRa/LDebFzO1R4+oxLMxoWlfH6TAy2W3NV nC/LBALAyFRLdOZvLJ2hHrsvjw8+PLhmdne/5IxZkANBxAMJurzSAxNJF1j5FfuBVXkp 8MEQ== X-Forwarded-Encrypted: i=1; AJvYcCXktOTaMwRh1xvVq8Wfnxru+tshLswYhe4Ylhb+ojqN2UZlPgh4eTCXA96N0sjU+mzjBjGQbVi6QhbUisulu8+531gFyUETRYkn5/P8fjhM X-Gm-Message-State: AOJu0YzRnUjIy2UwVU1gX/dzg2yOeQHNB1ygLUDeXruGC7+uMAcGLddj YMaxjBLEZfRVMxtQgXe3UH8zpKwig3gXOL3LDze8hA4R9DmPodB/OHO8y8apoPaoDnf2QI5I/Xk O5f9HiXRK0RGiZf1dI+GTNg== X-Google-Smtp-Source: AGHT+IFhY7mSFiskrXLQQcLXv+8xGkstxo5w0gS60+blMyypRYZeekyCqzmmOLRjPz0ivi7dNVpAi02DPBov+hPrzw== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6902:210b:b0:dcc:50ca:e153 with SMTP id dk11-20020a056902210b00b00dcc50cae153mr3627471ybb.7.1709681689467; Tue, 05 Mar 2024 15:34:49 -0800 (PST) Date: Tue, 05 Mar 2024 23:34:42 +0000 In-Reply-To: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-0-5b78a13ff984@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-0-5b78a13ff984@google.com> X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1709681680; l=1300; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=FcEcSMUZOi/4Oj8G2zljZCZLH3SJcgxPb0qa54lykhA=; b=rEqSK5v7RadAxWbNgUvH4x8qZTngDbd07BNkbZ1Pq1tjrnZ0h0MlU2EsBQmYekMKYg7eDKhfc Gc5GYce/HG/AVosyZo89GfoU8y1DHcrWZSkiwbH+zU8Q/7Cwo+zcboR X-Mailer: b4 0.12.3 Message-ID: <20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-7-5b78a13ff984@google.com> Subject: [PATCH v3 7/7] scsi: wd33c93: replace deprecated strncpy with strscpy From: Justin Stitt To: Sathya Prakash Veerichetty , Kashyap Desai , Sumit Saxena , Sreekanth Reddy , "James E.J. Bottomley" , "Martin K. Petersen" , Suganath Prabu Subramani , Ariel Elior , Manish Chopra , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Saurav Kashyap , Javed Hasan , GR-QLogic-Storage-Upstream@marvell.com, Nilesh Javali , Manish Rangankar , Don Brace Cc: mpi3mr-linuxdrv.pdl@broadcom.com, linux-scsi@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , MPT-FusionLinux.pdl@broadcom.com, netdev@vger.kernel.org, storagedev@microchip.com, Justin Stitt @p1 is assigned to @setup_buffer and then we manually assign a NUL-byte at the first index. This renders the following strlen() call useless. Moreover, we don't need to reassign p1 to setup_buffer for any reason -- neither do we need to manually set a NUL-byte at the end. strscpy() resolves all this code making it easier to read. Even considering the path where @str is falsey, the manual NUL-byte assignment is useless as setup_buffer is declared with static storage duration in the top-level scope which should NUL-initialize the whole buffer. Reviewed-by: Kees Cook Signed-off-by: Justin Stitt --- drivers/scsi/wd33c93.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/scsi/wd33c93.c b/drivers/scsi/wd33c93.c index e4fafc77bd20..a44b60c9004a 100644 --- a/drivers/scsi/wd33c93.c +++ b/drivers/scsi/wd33c93.c @@ -1721,9 +1721,7 @@ wd33c93_setup(char *str) p1 = setup_buffer; *p1 = '\0'; if (str) - strncpy(p1, str, SETUP_BUFFER_SIZE - strlen(setup_buffer)); - setup_buffer[SETUP_BUFFER_SIZE - 1] = '\0'; - p1 = setup_buffer; + strscpy(p1, str, SETUP_BUFFER_SIZE); i = 0; while (*p1 && (i < MAX_SETUP_ARGS)) { p2 = strchr(p1, ',');