From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F423AC77B78 for ; Thu, 4 May 2023 20:04:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232500AbjEDUEY (ORCPT ); Thu, 4 May 2023 16:04:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231965AbjEDUDa (ORCPT ); Thu, 4 May 2023 16:03:30 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 530E34202; Thu, 4 May 2023 12:52:10 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id EB50F63861; Thu, 4 May 2023 19:50:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77263C4339B; Thu, 4 May 2023 19:50:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683229849; bh=4LF0XKj7irR7X2JXIKKtAhKxZntiSv1je4UdTmZXwD0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eMS8aRJfXok4WU3z+Ly8zd0sH3448IK9AzW5jfaIKx6YZk4qVI/JgoSEPKfMQO7Yb vBM4kGMzyQfe/EOWcWZJu7qnl0Tl2ZUsxBG5C33ClS51/hIwEFSfWzHI2ubtmBevBQ jLeFInAD4r5Acj77qvZuYeJpqO0r5QYgSGwx8HmoKRpdg6NBKEyjpqV+Kn/1mYdG6L YBOWy6FZLj9rMSJWi4EdVpYtLR5arVak7Q+1hlz4Kn9e3GJTED50zDkzZ42ujfoVPq GUMhtKTZyVDXgs+tyhCybAu34n767Nb40iZBJ0bugmW+nWr2yI+PIs2/iiEVrGlRpG CHPovMYtIlk/g== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Justin Tee , "Martin K . Petersen" , Sasha Levin , james.smart@broadcom.com, dick.kennedy@broadcom.com, jejb@linux.ibm.com, linux-scsi@vger.kernel.org Subject: [PATCH AUTOSEL 5.4 03/18] scsi: lpfc: Prevent lpfc_debugfs_lockstat_write() buffer overflow Date: Thu, 4 May 2023 15:50:25 -0400 Message-Id: <20230504195042.3808716-3-sashal@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230504195042.3808716-1-sashal@kernel.org> References: <20230504195042.3808716-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Justin Tee [ Upstream commit c6087b82a9146826564a55c5ca0164cac40348f5 ] A static code analysis tool flagged the possibility of buffer overflow when using copy_from_user() for a debugfs entry. Currently, it is possible that copy_from_user() copies more bytes than what would fit in the mybuf char array. Add a min() restriction check between sizeof(mybuf) - 1 and nbytes passed from the userspace buffer to protect against buffer overflow. Link: https://lore.kernel.org/r/20230301231626.9621-2-justintee8345@gmail.com Signed-off-by: Justin Tee Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin --- drivers/scsi/lpfc/lpfc_debugfs.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/scsi/lpfc/lpfc_debugfs.c b/drivers/scsi/lpfc/lpfc_debugfs.c index 69551132f304c..291fccf02d453 100644 --- a/drivers/scsi/lpfc/lpfc_debugfs.c +++ b/drivers/scsi/lpfc/lpfc_debugfs.c @@ -2046,6 +2046,7 @@ lpfc_debugfs_lockstat_write(struct file *file, const char __user *buf, char mybuf[64]; char *pbuf; int i; + size_t bsize; /* Protect copy from user */ if (!access_ok(buf, nbytes)) @@ -2053,7 +2054,9 @@ lpfc_debugfs_lockstat_write(struct file *file, const char __user *buf, memset(mybuf, 0, sizeof(mybuf)); - if (copy_from_user(mybuf, buf, nbytes)) + bsize = min(nbytes, (sizeof(mybuf) - 1)); + + if (copy_from_user(mybuf, buf, bsize)) return -EFAULT; pbuf = &mybuf[0]; @@ -2074,7 +2077,7 @@ lpfc_debugfs_lockstat_write(struct file *file, const char __user *buf, qp->lock_conflict.wq_access = 0; } } - return nbytes; + return bsize; } #endif -- 2.39.2