From patchwork Thu Feb 8 08:44:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 13549456 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 93B786D1AA; Thu, 8 Feb 2024 08:45:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381927; cv=none; b=gOj0INfpeSWwWToc24QmiiDLry8NlBiAQEY9hMeaBwKD3OKIJBil3JSbgy/7dYKYO8p/dEziCugKyqpMUkwcuK2tGUU9FZapljN+uUpUa5voYuvApDVyWFdClDtP4W3/GrnGQSy1myvwmfeKRfYLb+/Zscj92ewnAQbe5nlIai4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381927; c=relaxed/simple; bh=kPQEC7JNk1nG41qzG8wfP+IddSOveAbpNnPvKYhI/4o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DmON6N66CxrLyQPExgsS1GkXVS0XCKK33xy4iG8FEYdtMLpnT+vEytHQK9GHTkPBvhvGcHriXJL+qaOI10BvgQQFnoBnL0VsJTMeAUSxzTzLUcvCNgM6Pna2RgERmqAvpCItbmFMDWy+4q6lECHYZs8u565QrL0GpOWYnN8zK6w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=j4F5tGSr; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="j4F5tGSr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6B7CAC43142; Thu, 8 Feb 2024 08:45:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707381927; bh=kPQEC7JNk1nG41qzG8wfP+IddSOveAbpNnPvKYhI/4o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=j4F5tGSrD6ALzwj7JwdKg6N+UE+nGeZW0wCkS9VI3EkJV0qqDGxmuGDBKLgCPQXMK iXxeiPa/3/f5uaaaYz5302sabTiuOhOzBQ3WAaWPU5L3nrmQ7mLMjgeE5b6/rSNHZg iOBrDaxlgdQvVyy39RRyZJxP7KnzZULipwN0HI4I836QMXbIwPxkcnvyE3dMNuAKcv /N26Y1C+rCx8iia2dw6Yw1F7QLX21WKuKtqvIEz7S8E7EIR6k7+GxMV4cUwMjHV3Hw MG2SyFuKMKPvNh/hYb4GI3gdbqqybdhXR1QuIXORcwlsinZAWiR9G7QXQ+uigjISdb 7dioSC5x11JAg== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Finn Thain , Michael Schmitz , "James E.J. Bottomley" , "Martin K. Petersen" , drew@colorado.edu, Tnx to , linux-scsi@vger.kernel.org Subject: [PATCH 03/10] scsi: NCR5380: Replace snprintf() with the safer scnprintf() variant Date: Thu, 8 Feb 2024 08:44:15 +0000 Message-ID: <20240208084512.3803250-4-lee@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240208084512.3803250-1-lee@kernel.org> References: <20240208084512.3803250-1-lee@kernel.org> Precedence: bulk X-Mailing-List: linux-scsi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 There is a general misunderstanding amongst engineers that {v}snprintf() returns the length of the data *actually* encoded into the destination array. However, as per the C99 standard {v}snprintf() really returns the length of the data that *would have been* written if there were enough space for it. This misunderstanding has led to buffer-overruns in the past. It's generally considered safer to use the {v}scnprintf() variants in their place (or even sprintf() in simple cases). So let's do that. Link: https://lwn.net/Articles/69419/ Link: https://github.com/KSPP/linux/issues/105 Signed-off-by: Lee Jones Reviewed-by: Kees Cook --- Cc: Finn Thain Cc: Michael Schmitz Cc: "James E.J. Bottomley" Cc: "Martin K. Petersen" Cc: drew@colorado.edu Cc: Tnx to Cc: linux-scsi@vger.kernel.org --- drivers/scsi/NCR5380.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/scsi/NCR5380.c b/drivers/scsi/NCR5380.c index cea3a79d538e4..ea565e843c765 100644 --- a/drivers/scsi/NCR5380.c +++ b/drivers/scsi/NCR5380.c @@ -421,14 +421,14 @@ static int NCR5380_init(struct Scsi_Host *instance, int flags) if (!hostdata->work_q) return -ENOMEM; - snprintf(hostdata->info, sizeof(hostdata->info), - "%s, irq %d, io_port 0x%lx, base 0x%lx, can_queue %d, cmd_per_lun %d, sg_tablesize %d, this_id %d, flags { %s%s%s}", - instance->hostt->name, instance->irq, hostdata->io_port, - hostdata->base, instance->can_queue, instance->cmd_per_lun, - instance->sg_tablesize, instance->this_id, - hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "", - hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "", - hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : ""); + scnprintf(hostdata->info, sizeof(hostdata->info), + "%s, irq %d, io_port 0x%lx, base 0x%lx, can_queue %d, cmd_per_lun %d, sg_tablesize %d, this_id %d, flags { %s%s%s}", + instance->hostt->name, instance->irq, hostdata->io_port, + hostdata->base, instance->can_queue, instance->cmd_per_lun, + instance->sg_tablesize, instance->this_id, + hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "", + hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "", + hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : ""); NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); NCR5380_write(MODE_REG, MR_BASE); From patchwork Thu Feb 8 08:44:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 13549457 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 061A76F50E; Thu, 8 Feb 2024 08:45:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381929; cv=none; b=PuxU7Pxv8ALkJrMwC54G/g7fmS8kZSFVl/gmehvS2cPweZNg4BHkWC7nRgtuw7/xCWA9pXR9nCeiCVn29ZnycXpWdcwF9uR1B1NLLrOVEWz1Ave+kGxlO+DIkIizH9SQYYmk/Sh1yQ+rYBYSYH7INXwBFGaUuXZEnj6Beg9xMcQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381929; c=relaxed/simple; bh=Z2CP+O5Bv9LbkXXAh5F8xDT7ZCfBYn4Mn2nqmn37ldw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qYDKVCT2vptlHlrH7QLbHOwo6FAmqL9d9/BxBKk+35znhSBiNNRl2NIEFDOC2sZDM0LKk72Oeqf47l/bSBBGqqD458/YszqNlXm8lH4jFFv9zMoyRv/G9c3RvwiD2ecf+L8QIx26pd3BwpaMFzyWn6JeTleHjh27OS0H93R4UZI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MNxilidQ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MNxilidQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 721C2C43394; Thu, 8 Feb 2024 08:45:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707381928; bh=Z2CP+O5Bv9LbkXXAh5F8xDT7ZCfBYn4Mn2nqmn37ldw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MNxilidQzOvdSPg22Q6azfmgMcS4E2C/rBzHDGxIpkSY0f83QhtuhCxMwfxhldgwD LLbe9tmty92PjuqBCqdNdX07LGA02ZVepLkQPlqK4Pfzcr65X1l/8FBnE2oNX+TwhX /N85C1wBdDetTek4BewQukeCTSDFf17sBn9sZBWRiYR5Z+gRMj5Hj3MrA1TOD42KQ7 UUPKr5jWJqoEIbhv7mP45UDHfVqoJYLDp0sXWqYoYuIpBghDBjSgDSC4V3w9skguj7 RzgIZMAGtlAcIXi69/qOqKosBgbeQwHSbLBRT2IpItV/g9NnoEItd1iny1Cfk6lgmP jmNIqcBhhhSdg== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Adaptec OEM Raid Solutions , "James E.J. Bottomley" , "Martin K. Petersen" , "PMC-Sierra, Inc" , linux-scsi@vger.kernel.org Subject: [PATCH 04/10] scsi: aacraid: linit: Remove snprintf() from sysfs call-backs and replace with sysfs_emit() Date: Thu, 8 Feb 2024 08:44:16 +0000 Message-ID: <20240208084512.3803250-5-lee@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240208084512.3803250-1-lee@kernel.org> References: <20240208084512.3803250-1-lee@kernel.org> Precedence: bulk X-Mailing-List: linux-scsi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Since snprintf() has the documented, but still rather strange trait of returning the length of the data that *would have been* written to the array if space were available, rather than the arguably more useful length of data *actually* written, it is usually considered wise to use something else instead in order to avoid confusion. In the case of sysfs call-backs, new wrappers exist that do just that. Link: https://lwn.net/Articles/69419/ Link: https://github.com/KSPP/linux/issues/105 Signed-off-by: Lee Jones Reviewed-by: Kees Cook --- Cc: Adaptec OEM Raid Solutions Cc: "James E.J. Bottomley" Cc: "Martin K. Petersen" Cc: "PMC-Sierra, Inc" Cc: linux-scsi@vger.kernel.org --- drivers/scsi/aacraid/linit.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/drivers/scsi/aacraid/linit.c b/drivers/scsi/aacraid/linit.c index 68f4dbcfff492..69526e2bd2e78 100644 --- a/drivers/scsi/aacraid/linit.c +++ b/drivers/scsi/aacraid/linit.c @@ -558,11 +558,9 @@ static ssize_t aac_show_raid_level(struct device *dev, struct device_attribute * struct scsi_device *sdev = to_scsi_device(dev); struct aac_dev *aac = (struct aac_dev *)(sdev->host->hostdata); if (sdev_channel(sdev) != CONTAINER_CHANNEL) - return snprintf(buf, PAGE_SIZE, sdev->no_uld_attach - ? "Hidden\n" : + return sysfs_emit(buf, sdev->no_uld_attach ? "Hidden\n" : ((aac->jbod && (sdev->type == TYPE_DISK)) ? "JBOD\n" : "")); - return snprintf(buf, PAGE_SIZE, "%s\n", - get_container_type(aac->fsa_dev[sdev_id(sdev)].type)); + return sysfs_emit(buf, "%s\n", get_container_type(aac->fsa_dev[sdev_id(sdev)].type)); } static struct device_attribute aac_raid_level_attr = { @@ -1195,10 +1193,9 @@ static ssize_t aac_show_model(struct device *device, ++cp; while (*cp == ' ') ++cp; - len = snprintf(buf, PAGE_SIZE, "%s\n", cp); + len = sysfs_emit(buf, "%s\n", cp); } else - len = snprintf(buf, PAGE_SIZE, "%s\n", - aac_drivers[dev->cardtype].model); + len = sysfs_emit(buf, "%s\n", aac_drivers[dev->cardtype].model); return len; } @@ -1214,12 +1211,11 @@ static ssize_t aac_show_vendor(struct device *device, char *cp = sup_adap_info->adapter_type_text; while (*cp && *cp != ' ') ++cp; - len = snprintf(buf, PAGE_SIZE, "%.*s\n", + len = sysfs_emit(buf, "%.*s\n", (int)(cp - (char *)sup_adap_info->adapter_type_text), sup_adap_info->adapter_type_text); } else - len = snprintf(buf, PAGE_SIZE, "%s\n", - aac_drivers[dev->cardtype].vname); + len = sysfs_emit(buf, "%s\n", aac_drivers[dev->cardtype].vname); return len; } @@ -1230,7 +1226,7 @@ static ssize_t aac_show_flags(struct device *cdev, struct aac_dev *dev = (struct aac_dev*)class_to_shost(cdev)->hostdata; if (nblank(dprintk(x))) - len = snprintf(buf, PAGE_SIZE, "dprintk\n"); + len = sysfs_emit(buf, "dprintk\n"); #ifdef AAC_DETAILED_STATUS_INFO len += scnprintf(buf + len, PAGE_SIZE - len, "AAC_DETAILED_STATUS_INFO\n"); @@ -1258,7 +1254,7 @@ static ssize_t aac_show_kernel_version(struct device *device, int len, tmp; tmp = le32_to_cpu(dev->adapter_info.kernelrev); - len = snprintf(buf, PAGE_SIZE, "%d.%d-%d[%d]\n", + len = sysfs_emit(buf, "%d.%d-%d[%d]\n", tmp >> 24, (tmp >> 16) & 0xff, tmp & 0xff, le32_to_cpu(dev->adapter_info.kernelbuild)); return len; @@ -1272,7 +1268,7 @@ static ssize_t aac_show_monitor_version(struct device *device, int len, tmp; tmp = le32_to_cpu(dev->adapter_info.monitorrev); - len = snprintf(buf, PAGE_SIZE, "%d.%d-%d[%d]\n", + len = sysfs_emit(buf, "%d.%d-%d[%d]\n", tmp >> 24, (tmp >> 16) & 0xff, tmp & 0xff, le32_to_cpu(dev->adapter_info.monitorbuild)); return len; @@ -1286,7 +1282,7 @@ static ssize_t aac_show_bios_version(struct device *device, int len, tmp; tmp = le32_to_cpu(dev->adapter_info.biosrev); - len = snprintf(buf, PAGE_SIZE, "%d.%d-%d[%d]\n", + len = sysfs_emit(buf, "%d.%d-%d[%d]\n", tmp >> 24, (tmp >> 16) & 0xff, tmp & 0xff, le32_to_cpu(dev->adapter_info.biosbuild)); return len; @@ -1296,7 +1292,7 @@ static ssize_t aac_show_driver_version(struct device *device, struct device_attribute *attr, char *buf) { - return snprintf(buf, PAGE_SIZE, "%s\n", aac_driver_version); + return sysfs_emit(buf, "%s\n", aac_driver_version); } static ssize_t aac_show_serial_number(struct device *device, @@ -1322,15 +1318,13 @@ static ssize_t aac_show_serial_number(struct device *device, static ssize_t aac_show_max_channel(struct device *device, struct device_attribute *attr, char *buf) { - return snprintf(buf, PAGE_SIZE, "%d\n", - class_to_shost(device)->max_channel); + return sysfs_emit(buf, "%d\n", class_to_shost(device)->max_channel); } static ssize_t aac_show_max_id(struct device *device, struct device_attribute *attr, char *buf) { - return snprintf(buf, PAGE_SIZE, "%d\n", - class_to_shost(device)->max_id); + return sysfs_emit(buf, "%d\n", class_to_shost(device)->max_id); } static ssize_t aac_store_reset_adapter(struct device *device, @@ -1360,7 +1354,7 @@ static ssize_t aac_show_reset_adapter(struct device *device, tmp = aac_adapter_check_health(dev); if ((tmp == 0) && dev->in_reset) tmp = -EBUSY; - len = snprintf(buf, PAGE_SIZE, "0x%x\n", tmp); + len = sysfs_emit(buf, "0x%x\n", tmp); return len; } From patchwork Thu Feb 8 08:44:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 13549458 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD9E76F50B; Thu, 8 Feb 2024 08:45:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381930; cv=none; b=pRNvSYdpR6WtHe0ixlMojdHwOLJw2ek08pdHY5Gh/THdpAHUK0RktIrMgFf4ETHlQvyCHYMf0eYDQwP6ccsLhYhAPHOVHcO47NPJcyVbhzRRdxCO/7xqPIvqELZ0TfzyOcYK1d/Z+m7/6g0mXEg1AsCfNK9zopYMVjv7taI6kRA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381930; c=relaxed/simple; bh=POI6uCurjz7K0SS0gUH7C3Dj3W11IBfcHS/B+rMzmkI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MzKDprN1HdM+KjIMnES3wfAaXrhCWSlkBmRcPwwVZw9/u8IcUpH6K1vPXoVcp7QdC+67ffTUJJJoqHccse8M3njbSqeJCH8e+DF5U2RsRHSwnl5KNwOH7OQycsLSUV43WqbMp/tD/LGNY5ReMtcRSS2D43o0Hjd4mg/NbVmXItY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Oj59yN2b; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Oj59yN2b" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 35D6BC43390; Thu, 8 Feb 2024 08:45:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707381930; bh=POI6uCurjz7K0SS0gUH7C3Dj3W11IBfcHS/B+rMzmkI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Oj59yN2bU3sJBgizsn6x4fu41c/QIs6ntSTGN6Z6dvCS6XlPGrpVhwSHlXNhm9jYA BBwC+V2MHkoX+xdCMOcKeEIgSyivDMoGE33cZyG8h5dsmw80W5L7ddNrgWnq+JOGOY hVto3EU37cOv/sJvJH7tQRWIIqxk9tMHVc0myon3u/a8NBRPOaPeZVHf3dGTA/3YYl RzeI1rmPJ/BZHLFQx9+ctK99WAV7ebDzgMq9h7lQULRsUzX6IvJzq3SpsaZ8uOUMzL TkZe+CbIPCDJmesRHbtfmWSdsLeR2vbt0ClkeMbRGCnu0VDW4/w3fLJIgkg53EXybs pXeitHIQV0JkQ== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Adaptec OEM Raid Solutions , "James E.J. Bottomley" , "Martin K. Petersen" , "PMC-Sierra, Inc" , linux-scsi@vger.kernel.org Subject: [PATCH 05/10] scsi: aacraid: linit: Replace snprintf() with the safer scnprintf() variant Date: Thu, 8 Feb 2024 08:44:17 +0000 Message-ID: <20240208084512.3803250-6-lee@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240208084512.3803250-1-lee@kernel.org> References: <20240208084512.3803250-1-lee@kernel.org> Precedence: bulk X-Mailing-List: linux-scsi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 There is a general misunderstanding amongst engineers that {v}snprintf() returns the length of the data *actually* encoded into the destination array. However, as per the C99 standard {v}snprintf() really returns the length of the data that *would have been* written if there were enough space for it. This misunderstanding has led to buffer-overruns in the past. It's generally considered safer to use the {v}scnprintf() variants in their place (or even sprintf() in simple cases). So let's do that. Link: https://lwn.net/Articles/69419/ Link: https://github.com/KSPP/linux/issues/105 Signed-off-by: Lee Jones --- Cc: Adaptec OEM Raid Solutions Cc: "James E.J. Bottomley" Cc: "Martin K. Petersen" Cc: "PMC-Sierra, Inc" Cc: linux-scsi@vger.kernel.org --- drivers/scsi/aacraid/linit.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/scsi/aacraid/linit.c b/drivers/scsi/aacraid/linit.c index 69526e2bd2e78..0e47d9c4cff23 100644 --- a/drivers/scsi/aacraid/linit.c +++ b/drivers/scsi/aacraid/linit.c @@ -583,7 +583,7 @@ static ssize_t aac_show_unique_id(struct device *dev, if (sdev_channel(sdev) == CONTAINER_CHANNEL) memcpy(sn, aac->fsa_dev[sdev_id(sdev)].identifier, sizeof(sn)); - return snprintf(buf, 16 * 2 + 2, + return scnprintf(buf, 16 * 2 + 2, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n", sn[0], sn[1], sn[2], sn[3], sn[4], sn[5], sn[6], sn[7], @@ -1302,13 +1302,13 @@ static ssize_t aac_show_serial_number(struct device *device, int len = 0; if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0) - len = snprintf(buf, 16, "%06X\n", + len = scnprintf(buf, 16, "%06X\n", le32_to_cpu(dev->adapter_info.serial[0])); if (len && !memcmp(&dev->supplement_adapter_info.mfg_pcba_serial_no[ sizeof(dev->supplement_adapter_info.mfg_pcba_serial_no)-len], buf, len-1)) - len = snprintf(buf, 16, "%.*s\n", + len = scnprintf(buf, 16, "%.*s\n", (int)sizeof(dev->supplement_adapter_info.mfg_pcba_serial_no), dev->supplement_adapter_info.mfg_pcba_serial_no); From patchwork Thu Feb 8 08:44:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 13549459 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7DC7371B4D; Thu, 8 Feb 2024 08:45:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381933; cv=none; b=mBW3B3d6pI8fMVCqMGsEJ1yCBoWlnRRpAhNhmUckW6SKupjNUijJkYQxxs39wFTbEFdyitu/mLXzWpCxKKlsk7uwoXybugGl/0xrnX8Rb2jRCG2hcjfldQEREL4NdFwbK4iihNJ4WiFjD9c+gpicmKEgotfOD9xJ2VLKEtRJx5g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381933; c=relaxed/simple; bh=PbzPd7ROc5va5PvBniGi9yT6iZxcViGR7FsGtUiYZPQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ax340BOOpImeQx4RMtE5KZ8gORbhvmtEyCceuXWAQwz3GV7QtcS30zAWS+bcRwBA5XxycrFrx15NGfQn+J5vBYHk+TDHnePeltwPi1rdUfYYr1ZqmmbiKz9P3eYhzaqEmtjPrdyiUiG2gb9k7RacFeGdEGDdUh2zy8MvBFhXLJw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JYv3wWWf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JYv3wWWf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ECCBEC43394; Thu, 8 Feb 2024 08:45:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707381932; bh=PbzPd7ROc5va5PvBniGi9yT6iZxcViGR7FsGtUiYZPQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JYv3wWWfitgg/S4wiWCNHUkrl6YEq7UHvBTMANfUjrWE6exn3/m3eE4TUhkWPXN+Y rRCC2FVaAkQZqV1mHCuFnaHh1E4JzU8l38xR/iT/qXy3v/vrN2OxQVG3lrZhMJjvCx o4T+oxyev2+cxar+vWyjTi6fPg7ODFt8JIJFQLVx0HGBORpVmo99miUmNAWAEXz00J mMdCOBIQwXoI84M6/sfhzlFbrNwoOdvX9cGpijg9bbVq5vEsPeuulOXxkJeOp03YD5 OMN6+cLmOLws/InUorErs1VlC3zfGGKrspeOEalGaY6iN4L2ycp8QkTyggjGOJ6JbQ S9VvuxHfX7/Ug== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org Subject: [PATCH 06/10] scsi: aha1542: Replace snprintf() with the safer scnprintf() variant Date: Thu, 8 Feb 2024 08:44:18 +0000 Message-ID: <20240208084512.3803250-7-lee@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240208084512.3803250-1-lee@kernel.org> References: <20240208084512.3803250-1-lee@kernel.org> Precedence: bulk X-Mailing-List: linux-scsi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 There is a general misunderstanding amongst engineers that {v}snprintf() returns the length of the data *actually* encoded into the destination array. However, as per the C99 standard {v}snprintf() really returns the length of the data that *would have been* written if there were enough space for it. This misunderstanding has led to buffer-overruns in the past. It's generally considered safer to use the {v}scnprintf() variants in their place (or even sprintf() in simple cases). So let's do that. Link: https://lwn.net/Articles/69419/ Link: https://github.com/KSPP/linux/issues/105 Signed-off-by: Lee Jones Reviewed-by: Kees Cook --- Cc: "James E.J. Bottomley" Cc: "Martin K. Petersen" Cc: linux-scsi@vger.kernel.org --- drivers/scsi/aha1542.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/scsi/aha1542.c b/drivers/scsi/aha1542.c index 9503996c63256..b5ec7887801a5 100644 --- a/drivers/scsi/aha1542.c +++ b/drivers/scsi/aha1542.c @@ -772,7 +772,7 @@ static struct Scsi_Host *aha1542_hw_init(const struct scsi_host_template *tpnt, goto unregister; if (sh->dma_channel != 0xFF) - snprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel); + scnprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel); shost_printk(KERN_INFO, sh, "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d, %s\n", sh->this_id, base_io, sh->irq, dma_info); if (aha1542->bios_translation == BIOS_TRANSLATION_25563) From patchwork Thu Feb 8 08:44:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 13549460 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 355D271B58; Thu, 8 Feb 2024 08:45:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381934; cv=none; b=iBQOgW5CcxPy1nLu58I3GFl8OCwbtmc/Lk7VrKJqOGM4TO615LPMpiz7nOjxtAkJ2n0oH48XiBbIK79A1efMpd5q/6wK7teAiFWs9pph174Sz6K1cG2lpe3a7j7NRI1H4o0AVvuy+IsF9k0LKt/K+8clJMf+cwZIgzT4LuofA2g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381934; c=relaxed/simple; bh=WGjy4Cm0cEOn+F+u0AjX0H2bpY30XTKx3DCJD60T9WI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KsCRccDtfe1zXF6GNw22OxvLl5aJsaA+Yi7Io+lsoXYDV2PNSH8bV34MQiH9EyJN7183KkeVgBR0hsbioF9iZLKwbASYL1eSrOugjat2XjT4sXVKo7R4/lWvzelhp+U/v0/xGoxgHu2GZZgDncsjiYSH7YUck6asCgAeMnbkqR4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nY08ObA9; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nY08ObA9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 69997C433F1; Thu, 8 Feb 2024 08:45:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707381933; bh=WGjy4Cm0cEOn+F+u0AjX0H2bpY30XTKx3DCJD60T9WI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nY08ObA9mT1r7BdDI4+PYZMJ5u34xPUmGzv18dfsF8Em8732jTJOMbNbTyoN87pF9 fc8fw9r6k5sKza1jJjXSIT5b/1FhiNa9UvFTNwCYHUVBjFyPU3R7BrVKPfOvtyyVZO to0fM5gFUcrWWbx7m5NaeEa0mxxzeRg/xWziXcGG7XbyIwkt4M4nT78CcLTMOca3e3 z6vd+9tRfdOHtqKky+XLcZDol0BzBUbyqoMoVtt52FCv/dRhqR/3z6OtBi1laxffHx BshFNRs59+jDW671Jzz16Zxz50WXaAoYuCER6s2pj42+1x8utsSZXLmnGiVE9EoBYw 5c41mD3T6eUaQ== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Hannes Reinecke , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org Subject: [PATCH 07/10] scsi: aic7xxx: aicasm: Trivial: Remove trailing whitespace Date: Thu, 8 Feb 2024 08:44:19 +0000 Message-ID: <20240208084512.3803250-8-lee@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240208084512.3803250-1-lee@kernel.org> References: <20240208084512.3803250-1-lee@kernel.org> Precedence: bulk X-Mailing-List: linux-scsi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Removing out of need rather than want. I wish to make proper changes to this file, but my editor is insistent on removing whitespace on save. Rather than go down the rabbit hole of debugging my editor right now, I'd rather stay productive. So, out it comes! EDIT: Found it. Looks like 5a602de99797b ("Add .editorconfig file for basic formatting") now forces editors to trim whitespace. Signed-off-by: Lee Jones Reviewed-by: Kees Cook --- Cc: Hannes Reinecke Cc: "James E.J. Bottomley" Cc: "Martin K. Petersen" Cc: linux-scsi@vger.kernel.org --- drivers/scsi/aic7xxx/aicasm/aicasm.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/scsi/aic7xxx/aicasm/aicasm.c b/drivers/scsi/aic7xxx/aicasm/aicasm.c index cd692a4c5f857..8d995186e557a 100644 --- a/drivers/scsi/aic7xxx/aicasm/aicasm.c +++ b/drivers/scsi/aic7xxx/aicasm/aicasm.c @@ -132,7 +132,7 @@ main(int argc, char *argv[]) /* Set Sentinal scope node */ sentinal = scope_alloc(); sentinal->type = SCOPE_ROOT; - + includes_search_curdir = 1; appname = *argv; regfile = NULL; @@ -553,7 +553,7 @@ output_listing(char *ifilename) if (func_values == NULL) stop("Could not malloc", EX_OSERR); - + func_values[0] = 0; /* FALSE func */ func_count--; @@ -561,13 +561,13 @@ output_listing(char *ifilename) * Ask the user to fill in the return values for * the rest of the functions. */ - - + + for (cur_func = SLIST_FIRST(&patch_functions); cur_func != NULL && SLIST_NEXT(cur_func, links) != NULL; cur_func = SLIST_NEXT(cur_func, links), func_count--) { int input; - + fprintf(stdout, "\n(%s)\n", cur_func->symbol->name); fprintf(stdout, "Enter the return value for " @@ -751,7 +751,7 @@ cs_alloc() if (new_cs == NULL) stop("Unable to malloc critical_section object", EX_SOFTWARE); memset(new_cs, 0, sizeof(*new_cs)); - + TAILQ_INSERT_TAIL(&cs_tailq, new_cs, links); return new_cs; } @@ -766,7 +766,7 @@ scope_alloc() stop("Unable to malloc scope object", EX_SOFTWARE); memset(new_scope, 0, sizeof(*new_scope)); TAILQ_INIT(&new_scope->inner_scope); - + if (SLIST_FIRST(&scope_stack) != NULL) { TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack)->inner_scope, new_scope, scope_links); From patchwork Thu Feb 8 08:44:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 13549461 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CEF3973197; Thu, 8 Feb 2024 08:45:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381935; cv=none; b=P5SIe56mslZgqf4RR5nwrJDU9Yk1J9q87rR60UE8q21T7MMvb5y4xU+TCseACIZnx+QIc8OcQYwFfoX+TNeDCljTXMMX4PZ4i26Ca1lP0E/+kisWZRrORX02sqxThLQsfUURFOl3zvRDO+cfvuPhSG1zKF7N1BGVRnG7VUHQZsI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381935; c=relaxed/simple; bh=QcWkvLbuXqKsmCSILThA1RfLwMeDripObvnvLFWiq34=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fDe2rypoyWtpnB06izoJQSLDq47rc6o3uHT2waOB85hlw/BcTJUUZnfYbeUp3EsQTDq0EUcNO5rxyjbhb5E1slsTIchW3Sazk6TiYqp9A765vXVJLh0DLs7RkcMVnVY7uDeFdKCMumFBfiXUhV5RtL7qjQX2wiJkpj5Ilpu9JNs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=flkB5vKC; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="flkB5vKC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0A257C433C7; Thu, 8 Feb 2024 08:45:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707381935; bh=QcWkvLbuXqKsmCSILThA1RfLwMeDripObvnvLFWiq34=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=flkB5vKCBQYz/TpY0INSUaOZjllm7VrkDUbKFVgCIc3ASXj/qazrW94Yryh3JnsNX NJlgNNRcRUnXProGsUi8C60pgUya/Fr+EbvRfWtqbhO2YHZE25gcmPGLdWv0WujarZ cWbsO1A+1oAk30b0gsn/JJRxxxx7v+Ya+QMQQeTauB49jBXRvaud7rZaPGDH6v1Sm9 BshS6lbql3Lky3xkzhJ3qkII+o16i592ZP1lJ90hMoELZdsv6nOxN6LrniXK5qTPYr QEA8Dul1ponHXK0/KmqqAqeurIbVE98UbjMLuIfEWXQG0CQZgxl22y8j+OgKOr8sY/ u8EUQhEi+CBGg== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Hannes Reinecke , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org Subject: [PATCH 08/10] scsi: aic7xxx: aicasm: Replace snprintf() with the safer scnprintf() variant Date: Thu, 8 Feb 2024 08:44:20 +0000 Message-ID: <20240208084512.3803250-9-lee@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240208084512.3803250-1-lee@kernel.org> References: <20240208084512.3803250-1-lee@kernel.org> Precedence: bulk X-Mailing-List: linux-scsi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 There is a general misunderstanding amongst engineers that {v}snprintf() returns the length of the data *actually* encoded into the destination array. However, as per the C99 standard {v}snprintf() really returns the length of the data that *would have been* written if there were enough space for it. This misunderstanding has led to buffer-overruns in the past. It's generally considered safer to use the {v}scnprintf() variants in their place (or even sprintf() in simple cases). So let's do that. Link: https://lwn.net/Articles/69419/ Link: https://github.com/KSPP/linux/issues/105 Signed-off-by: Lee Jones Reviewed-by: Kees Cook --- Cc: Hannes Reinecke Cc: "James E.J. Bottomley" Cc: "Martin K. Petersen" Cc: Lee Jones Cc: linux-scsi@vger.kernel.org --- drivers/scsi/aic7xxx/aicasm/aicasm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/scsi/aic7xxx/aicasm/aicasm.c b/drivers/scsi/aic7xxx/aicasm/aicasm.c index 8d995186e557a..bc758e78d3c06 100644 --- a/drivers/scsi/aic7xxx/aicasm/aicasm.c +++ b/drivers/scsi/aic7xxx/aicasm/aicasm.c @@ -331,7 +331,7 @@ back_patch() if (cur_instr->patch_label->type != LABEL) { char buf[255]; - snprintf(buf, sizeof(buf), + scnprintf(buf, sizeof(buf), "Undefined label %s", cur_instr->patch_label->name); stop(buf, EX_DATAERR); From patchwork Thu Feb 8 08:44:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 13549462 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 702AC76026; Thu, 8 Feb 2024 08:45:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381937; cv=none; b=hhjbRqxB40t4BvmfYSTN/D582LT6/uvh1HD7WeffP0D25EX09pQCL2JEtu1YJisjeBw0fqVKrmi0sdTUIvnFm0HlEkF796H/UA1n012sFuXhI5lqenfaZuBXuN39AhzFOVBBCiMb+tdjAP4Y5YL88fnK+peuEt8eDBoQ7135rTA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381937; c=relaxed/simple; bh=lMT5ds09tPKnvt5Z0XoIWL0MTp/63mQAtpaTLMS6Bp8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FvMA2J2qSZEdw0hZErbGteyrLrWJhalxQRYfJk6vLMB/tkvzTBTgddfdaBOeoH/wcJOfc+imY0JRhPeg8auWB2BclF9yYdK7Mp5N3R/fziVbzt2Mn1w16QwIv59GE2rcdFpaPLBvA037rIPF0vM0VZxqjZnIXc8IxjVibrYNB9c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=J+AyC1IJ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="J+AyC1IJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9CEFEC43399; Thu, 8 Feb 2024 08:45:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707381937; bh=lMT5ds09tPKnvt5Z0XoIWL0MTp/63mQAtpaTLMS6Bp8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J+AyC1IJGKuoOUKM5zUpx63JFqLmK3QCBMLMNCt2a87u7tEU2INr5mCviZhYP0aY4 jE+FZIXhMhOtL29JNAm/HuvP83u7gFMWthjuqXFRePQ2ccCLhWzb0P61wzQQGncLvY Wq+d7cvzmlMBf3QGUprdbxancpf64qlSJ+G+LbUosm8B8m6hhaqb0ZG2qC7L2uwgBa jRFgq2L8l84PNrXWXGck3eqqxs1MuTR8RRwAPmgdmAUOJIZzh8bixKKsR47yefhpKQ FS+pCBiRWxQMQxdOlSMZvgoXMhzs/81weorCR11zGzY79j2ctTvt+MoDfrj5gcDC09 +oCg4Lw/QFfYA== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, "James E.J. Bottomley" , "Martin K. Petersen" , John Garry , Luben Tuikov , linux-scsi@vger.kernel.org Subject: [PATCH 09/10] scsi: aic94xx: Remove snprintf() from sysfs call-backs and replace with sysfs_emit() Date: Thu, 8 Feb 2024 08:44:21 +0000 Message-ID: <20240208084512.3803250-10-lee@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240208084512.3803250-1-lee@kernel.org> References: <20240208084512.3803250-1-lee@kernel.org> Precedence: bulk X-Mailing-List: linux-scsi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Since snprintf() has the documented, but still rather strange trait of returning the length of the data that *would have been* written to the array if space were available, rather than the arguably more useful length of data *actually* written, it is usually considered wise to use something else instead in order to avoid confusion. In the case of sysfs call-backs, new wrappers exist that do just that. Link: https://lwn.net/Articles/69419/ Link: https://github.com/KSPP/linux/issues/105 Signed-off-by: Lee Jones Reviewed-by: Kees Cook --- Cc: "James E.J. Bottomley" Cc: "Martin K. Petersen" Cc: John Garry Cc: Luben Tuikov Cc: linux-scsi@vger.kernel.org --- drivers/scsi/aic94xx/aic94xx_init.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/drivers/scsi/aic94xx/aic94xx_init.c b/drivers/scsi/aic94xx/aic94xx_init.c index 8a3340d8d7ad4..c976e4b77c71e 100644 --- a/drivers/scsi/aic94xx/aic94xx_init.c +++ b/drivers/scsi/aic94xx/aic94xx_init.c @@ -264,8 +264,7 @@ static ssize_t asd_show_dev_rev(struct device *dev, struct device_attribute *attr, char *buf) { struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev); - return snprintf(buf, PAGE_SIZE, "%s\n", - asd_dev_rev[asd_ha->revision_id]); + return sysfs_emit(buf, "%s\n", asd_dev_rev[asd_ha->revision_id]); } static DEVICE_ATTR(aic_revision, S_IRUGO, asd_show_dev_rev, NULL); @@ -273,7 +272,7 @@ static ssize_t asd_show_dev_bios_build(struct device *dev, struct device_attribute *attr,char *buf) { struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev); - return snprintf(buf, PAGE_SIZE, "%d\n", asd_ha->hw_prof.bios.bld); + return sysfs_emit(buf, "%d\n", asd_ha->hw_prof.bios.bld); } static DEVICE_ATTR(bios_build, S_IRUGO, asd_show_dev_bios_build, NULL); @@ -281,7 +280,7 @@ static ssize_t asd_show_dev_pcba_sn(struct device *dev, struct device_attribute *attr, char *buf) { struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev); - return snprintf(buf, PAGE_SIZE, "%s\n", asd_ha->hw_prof.pcba_sn); + return sysfs_emit(buf, "%s\n", asd_ha->hw_prof.pcba_sn); } static DEVICE_ATTR(pcba_sn, S_IRUGO, asd_show_dev_pcba_sn, NULL); @@ -452,7 +451,7 @@ static ssize_t asd_show_update_bios(struct device *dev, if (asd_ha->bios_status != FLASH_IN_PROGRESS) asd_ha->bios_status = FLASH_OK; - return snprintf(buf, PAGE_SIZE, "status=%x %s\n", + return sysfs_emit(buf, "status=%x %s\n", flash_error_table[i].err_code, flash_error_table[i].reason); } @@ -937,7 +936,7 @@ static int asd_scan_finished(struct Scsi_Host *shost, unsigned long time) static ssize_t version_show(struct device_driver *driver, char *buf) { - return snprintf(buf, PAGE_SIZE, "%s\n", ASD_DRIVER_VERSION); + return sysfs_emit(buf, "%s\n", ASD_DRIVER_VERSION); } static DRIVER_ATTR_RO(version); From patchwork Thu Feb 8 08:44:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 13549463 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C056E74E2A; Thu, 8 Feb 2024 08:45:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381938; cv=none; b=XvpprrpUgadia5CVM1NSfbgfarfEg2WH3gAFQsQQQ/RMtNqLK9CMP09m9oZfyaSzCDTQUCw8UC82r1TDvo6ejO9arz3C8Uah3zWxuBqIEkbYXwvjwKav66MOquzOCS5knTWGAjwg0A3uhAGOkQe7A2LOs2rOJq/r2Vc/UI7eNyQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381938; c=relaxed/simple; bh=qmbHfN/f8YgSLjrFL2hPNhuDBX1LYqccIkoXslfVQ1o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rRrK3aks/8IQLgXXaD9OhBbxitdQf8znf7J0WXTZz27P6djHe7VX4KzeiHhR87JYXwX1iZZSF+Is6sNxEnKZXHVoI+GYwbuTYKZzGCT5Dhu4aNM3vUk2t7bYT/pDC7+ZNkQql88yrraDSq3eNIvbqzcB3qvuj7z64a8IhLnjIv0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LOEmzCzg; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LOEmzCzg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5E6F1C433B1; Thu, 8 Feb 2024 08:45:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707381938; bh=qmbHfN/f8YgSLjrFL2hPNhuDBX1LYqccIkoXslfVQ1o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LOEmzCzgxxjlFn+1XmC8QqhwKpmjJFNJilrBiaaa5pcylqyqEEg8E4wf2GwOCaUU9 fegkkGzEwK1cmtc+QpLPwbHg96F2C7Ie9tVls5Ah66PKkdfldLMY9XXQaNy+MNK3dQ TzGwHdq/qNrgG/Cr+pjuxFRRm9NPhHiumqhc+QF3WTcRiCnwaCytOg2fObaVSrBmY3 ox5G+HNe7WQOAeVbl057M+XYrRXShmn+DlS41O5L/tlxnFXemnFTuln958Q11MuV1e mbJcvIB59oJizF3UieAN2VSxFeZK+qYR8l+ueWh663SQ/ZO+NG9tBSATUUryXlpqrB XKnFiJpyUgDUQ== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, "James E.J. Bottomley" , "Martin K. Petersen" , support@areca.com.tw, linux-scsi@vger.kernel.org Subject: [PATCH 10/10] scsi: arcmsr: Remove snprintf() from sysfs call-backs and replace with sysfs_emit() Date: Thu, 8 Feb 2024 08:44:22 +0000 Message-ID: <20240208084512.3803250-11-lee@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240208084512.3803250-1-lee@kernel.org> References: <20240208084512.3803250-1-lee@kernel.org> Precedence: bulk X-Mailing-List: linux-scsi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Since snprintf() has the documented, but still rather strange trait of returning the length of the data that *would have been* written to the array if space were available, rather than the arguably more useful length of data *actually* written, it is usually considered wise to use something else instead in order to avoid confusion. In the case of sysfs call-backs, new wrappers exist that do just that. Link: https://lwn.net/Articles/69419/ Link: https://github.com/KSPP/linux/issues/105 Signed-off-by: Lee Jones Reviewed-by: Kees Cook --- Cc: "James E.J. Bottomley" Cc: "Martin K. Petersen" Cc: support@areca.com.tw Cc: linux-scsi@vger.kernel.org --- drivers/scsi/arcmsr/arcmsr_attr.c | 40 ++++++++----------------------- 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/drivers/scsi/arcmsr/arcmsr_attr.c b/drivers/scsi/arcmsr/arcmsr_attr.c index baeb5e7956902..c430c835503be 100644 --- a/drivers/scsi/arcmsr/arcmsr_attr.c +++ b/drivers/scsi/arcmsr/arcmsr_attr.c @@ -258,9 +258,7 @@ static ssize_t arcmsr_attr_host_driver_version(struct device *dev, struct device_attribute *attr, char *buf) { - return snprintf(buf, PAGE_SIZE, - "%s\n", - ARCMSR_DRIVER_VERSION); + return sysfs_emit(buf, "%s\n", ARCMSR_DRIVER_VERSION); } static ssize_t @@ -270,9 +268,7 @@ arcmsr_attr_host_driver_posted_cmd(struct device *dev, struct Scsi_Host *host = class_to_shost(dev); struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata; - return snprintf(buf, PAGE_SIZE, - "%4d\n", - atomic_read(&acb->ccboutstandingcount)); + return sysfs_emit(buf, "%4d\n", atomic_read(&acb->ccboutstandingcount)); } static ssize_t @@ -282,9 +278,7 @@ arcmsr_attr_host_driver_reset(struct device *dev, struct Scsi_Host *host = class_to_shost(dev); struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata; - return snprintf(buf, PAGE_SIZE, - "%4d\n", - acb->num_resets); + return sysfs_emit(buf, "%4d\n", acb->num_resets); } static ssize_t @@ -294,9 +288,7 @@ arcmsr_attr_host_driver_abort(struct device *dev, struct Scsi_Host *host = class_to_shost(dev); struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata; - return snprintf(buf, PAGE_SIZE, - "%4d\n", - acb->num_aborts); + return sysfs_emit(buf, "%4d\n", acb->num_aborts); } static ssize_t @@ -306,9 +298,7 @@ arcmsr_attr_host_fw_model(struct device *dev, struct device_attribute *attr, struct Scsi_Host *host = class_to_shost(dev); struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata; - return snprintf(buf, PAGE_SIZE, - "%s\n", - acb->firm_model); + return sysfs_emit(buf, "%s\n", acb->firm_model); } static ssize_t @@ -319,9 +309,7 @@ arcmsr_attr_host_fw_version(struct device *dev, struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata; - return snprintf(buf, PAGE_SIZE, - "%s\n", - acb->firm_version); + return sysfs_emit(buf, "%s\n", acb->firm_version); } static ssize_t @@ -332,9 +320,7 @@ arcmsr_attr_host_fw_request_len(struct device *dev, struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata; - return snprintf(buf, PAGE_SIZE, - "%4d\n", - acb->firm_request_len); + return sysfs_emit(buf, "%4d\n", acb->firm_request_len); } static ssize_t @@ -345,9 +331,7 @@ arcmsr_attr_host_fw_numbers_queue(struct device *dev, struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata; - return snprintf(buf, PAGE_SIZE, - "%4d\n", - acb->firm_numbers_queue); + return sysfs_emit(buf, "%4d\n", acb->firm_numbers_queue); } static ssize_t @@ -358,9 +342,7 @@ arcmsr_attr_host_fw_sdram_size(struct device *dev, struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata; - return snprintf(buf, PAGE_SIZE, - "%4d\n", - acb->firm_sdram_size); + return sysfs_emit(buf, "%4d\n", acb->firm_sdram_size); } static ssize_t @@ -371,9 +353,7 @@ arcmsr_attr_host_fw_hd_channels(struct device *dev, struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata; - return snprintf(buf, PAGE_SIZE, - "%4d\n", - acb->firm_hd_channels); + return sysfs_emit(buf, "%4d\n", acb->firm_hd_channels); } static DEVICE_ATTR(host_driver_version, S_IRUGO, arcmsr_attr_host_driver_version, NULL);