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: 198221 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp29659dyd; Thu, 8 Feb 2024 00:48:59 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWiWbHmH4Nz7kur6PPl5Bx4n4C0CtQy1AtBnaVH7uRko2Cbb4eDThDr4XSsFJbGi1TWoBA5sIlgEnnT6AmKzdX/KFmhXQ== X-Google-Smtp-Source: AGHT+IGiPNAYVUnNf+XqNe7q1cYc53Vi9In70AOCaX7oWJOaviiY7NC4ADTSAJAhGdvLX8cMFBfT X-Received: by 2002:a05:6214:248c:b0:68c:cc45:7981 with SMTP id gi12-20020a056214248c00b0068ccc457981mr166688qvb.16.1707382139125; Thu, 08 Feb 2024 00:48:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707382139; cv=pass; d=google.com; s=arc-20160816; b=hyocBy+dkWGRF9XCLmh63C3MoCOEIzTAXdAkdLMJREDgi7If/zx1QY+tXFxh9bgfQS IMjVqARDGm0bJanIT4pBhZJjKtG8wAQdUrq3opiC/6YjO8EY0unseCIsttJdWHAsbk2c b40nIo6XydvGcRStbdJ6vVkicCBSUTV6wardt+OpJQdDwNke4loD/kAX1Ydz2I4deU4W i9/tzixEIHLX0fVURRSHvOeSKbYgy8E21CodyRKrG4w1dPaSPkPCtBwe1RInWP2efmPU Ta/1I3P3v+sCLi+8RYDHx/IN2DFLR+dbM6NYoFSafCiL31G/WRa3Cn4TRwXXVvBuKVhZ fGWQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=wKrfMKAf/RQ4uwfLT6Wt4S7atsp9vd0IUxVezxMctnI=; fh=F3HyceY4cY/UUg81gXGsUiNQddee3Om6FG9xEZ+kr4w=; b=Uf/N4iQ9zfsNzTUOrdQ8DhvWVjy8FskidmmtQmkd35JNRb3cKNnnumz9w0SpVTj+j0 2Kt0GtQWzwyMJ9TWI7Yh4GIA6ZKh8L0dshYg0FkjO+6jzQslVCpHzW6DsNYGEciUKPcp +KksiCgAMomhYQMZktE0hhhRr6iW9vKKJxuN9mKtBJndkn0zO4y0852S83hPTaYQqU4H 0ac9j89RsNC6q/mjeCLgA8CtIDPnZWg+u1OFI8um1QgPNOKIZ5thuyLScb+8xA60kPij Zl85XnZVRzjSJs11BzS25mogVV1tQK7Ly0mGPQ72ivE09DkBWRbydQgj3BpuvWZRipi+ 5Ebg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MNxilidQ; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-57650-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-57650-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCWitVu75IRutTM+K5b7AfTa7YiUvBl0k0nOC3ASx4OcpoE8D0kt4Fw4mGbNf+v03q2d1+nOvxtiUtugcvWc2bZtNtxgEw== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id i17-20020a056214031100b0068c3a96d43dsi2750288qvu.218.2024.02.08.00.48.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 00:48:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-57650-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MNxilidQ; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-57650-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-57650-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id D58B41C24B5F for ; Thu, 8 Feb 2024 08:48:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7892A71B3E; Thu, 8 Feb 2024 08:45:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MNxilidQ" 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 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-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790319933860086521 X-GMAIL-MSGID: 1790319933860086521 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; }