From mboxrd@z Thu Jan 1 00:00:00 1970 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 4420113472D; Tue, 16 Jan 2024 19:52:05 +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=1705434725; cv=none; b=S1LzepZxnDyCZbSpEJIM5ijBwT+3bjp8wNhdbQOWTWusbUg1j2wTy3QIDgkaE3x3NpduUUAA1Vn8AOCMQU/x2nBNcS50V4UcR7jPplHba2QLQzHRqxOA5uTX5hW/F9X6531s/pZZAdrSSh2hxAwsb7LYv++eK05KrlTzAJ8GbB4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705434725; c=relaxed/simple; bh=4iMrzRjdMSeIlYcdzK4TKN+kxIoClU5wXIq5/sx6G0Y=; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version:X-stable: X-Patchwork-Hint:X-stable-base:Content-Transfer-Encoding; b=FfcZlAscJrmQbVn1ceoMh8nzNTMOHNpsSaEmJOA2oyDPlgv6LiY7Mt8YUo2uGtSP4PRftoZymoo31NYKSjWh4FWVUSAPDuosF6qEMym/iRhlS5+kd0s2EQNfpRsBH2fPuiHItlmJ3dBnZKuaD6Jo/5Bzu53LcZFIX7uCJpjBImI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mjSGPtXL; 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="mjSGPtXL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BB8FCC43394; Tue, 16 Jan 2024 19:52:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1705434724; bh=4iMrzRjdMSeIlYcdzK4TKN+kxIoClU5wXIq5/sx6G0Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mjSGPtXLWFgrCeLXEqRcLTAuOR8KX3YwgQR5xQB1XAQOIGzRarzHNxA2+7N/5zDJo 3a3SeDhS8zrLoIORh1Rq7qmsJCu1WREcDWedtPKKC648wcYnAejicRhLZnz7f78xcb szpcWtwCS8mtD01dm2JLmIVnBlAeaAfrszWGBJr33Kba33PM5m6zY/HPucTTexIy+s NEcdjuWlGEpjcVIEibybe37Ic76IV0Yg0NWjtj5oKiGHjSlXUdf9Q410U4ZHcKAaQc W7Sh6IltsUF9Ue5QAuI7GxvCjIwiPZt3z4GmgCBaXX4tE2IwYG/cL2zg4H/VshJiFy 7GajDNZLJiU/Q== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Kees Cook , kernel test robot , "Md . Haris Iqbal" , Jack Wang , Jens Axboe , linux-block@vger.kernel.org, Guoqing Jiang , Sasha Levin Subject: [PATCH AUTOSEL 6.6 062/104] block/rnbd-srv: Check for unlikely string overflow Date: Tue, 16 Jan 2024 14:46:28 -0500 Message-ID: <20240116194908.253437-62-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240116194908.253437-1-sashal@kernel.org> References: <20240116194908.253437-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.6.12 Content-Transfer-Encoding: 8bit From: Kees Cook [ Upstream commit 9e4bf6a08d1e127bcc4bd72557f2dfafc6bc7f41 ] Since "dev_search_path" can technically be as large as PATH_MAX, there was a risk of truncation when copying it and a second string into "full_path" since it was also PATH_MAX sized. The W=1 builds were reporting this warning: drivers/block/rnbd/rnbd-srv.c: In function 'process_msg_open.isra': drivers/block/rnbd/rnbd-srv.c:616:51: warning: '%s' directive output may be truncated writing up to 254 bytes into a region of size between 0 and 4095 [-Wformat-truncation=] 616 | snprintf(full_path, PATH_MAX, "%s/%s", | ^~ In function 'rnbd_srv_get_full_path', inlined from 'process_msg_open.isra' at drivers/block/rnbd/rnbd-srv.c:721:14: drivers/block/rnbd/rnbd-srv.c:616:17: note: 'snprintf' output between 2 and 4351 bytes into a destination of size 4096 616 | snprintf(full_path, PATH_MAX, "%s/%s", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 617 | dev_search_path, dev_name); | ~~~~~~~~~~~~~~~~~~~~~~~~~~ To fix this, unconditionally check for truncation (as was already done for the case where "%SESSNAME%" was present). Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/202312100355.lHoJPgKy-lkp@intel.com/ Cc: Md. Haris Iqbal Cc: Jack Wang Cc: Jens Axboe Cc: Signed-off-by: Kees Cook Acked-by: Guoqing Jiang Acked-by: Jack Wang Link: https://lore.kernel.org/r/20231212214738.work.169-kees@kernel.org Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- drivers/block/rnbd/rnbd-srv.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/drivers/block/rnbd/rnbd-srv.c b/drivers/block/rnbd/rnbd-srv.c index c186df0ec641..b67e39a34010 100644 --- a/drivers/block/rnbd/rnbd-srv.c +++ b/drivers/block/rnbd/rnbd-srv.c @@ -585,6 +585,7 @@ static char *rnbd_srv_get_full_path(struct rnbd_srv_session *srv_sess, { char *full_path; char *a, *b; + int len; full_path = kmalloc(PATH_MAX, GFP_KERNEL); if (!full_path) @@ -596,19 +597,19 @@ static char *rnbd_srv_get_full_path(struct rnbd_srv_session *srv_sess, */ a = strnstr(dev_search_path, "%SESSNAME%", sizeof(dev_search_path)); if (a) { - int len = a - dev_search_path; + len = a - dev_search_path; len = snprintf(full_path, PATH_MAX, "%.*s/%s/%s", len, dev_search_path, srv_sess->sessname, dev_name); - if (len >= PATH_MAX) { - pr_err("Too long path: %s, %s, %s\n", - dev_search_path, srv_sess->sessname, dev_name); - kfree(full_path); - return ERR_PTR(-EINVAL); - } } else { - snprintf(full_path, PATH_MAX, "%s/%s", - dev_search_path, dev_name); + len = snprintf(full_path, PATH_MAX, "%s/%s", + dev_search_path, dev_name); + } + if (len >= PATH_MAX) { + pr_err("Too long path: %s, %s, %s\n", + dev_search_path, srv_sess->sessname, dev_name); + kfree(full_path); + return ERR_PTR(-EINVAL); } /* eliminitate duplicated slashes */ -- 2.43.0