Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp2040108pxt; Sun, 8 Aug 2021 09:27:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxOFH9SE5le2XpxwFsVFHSCWpM/JUSMsHsQN6OGi5lWrNccUhAUjo2Et4MGuqtFRW6iYKXL X-Received: by 2002:a17:906:3514:: with SMTP id r20mr18968198eja.351.1628440037759; Sun, 08 Aug 2021 09:27:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628440037; cv=none; d=google.com; s=arc-20160816; b=it/CXecW8Cn/IoWaTrrMnwJR7e0N6qhA1P/0HO+BT37uLHtpuMK4TOfoQT8NNROsdL Ad2UXWrEreFF+kGqyRM0odl6RQ3kVqr6+6AliU+KCSeSPWOzDz8FEolW25/yOngEsY4c FeP2E1xfWJ2Zb4z9LOn4sldK0/5J06bF2SCaqz4Yq8Jby9gbcPwWwhxCwX3Al1pXGT5+ fz6ChrizKdQeym/Z4f+iAdNhYS69JsmFxRZ2GSr0B2CIRlIWyAQ9S8xCYMusF9B7uNv8 oi7EJqnKSEhQ69sN8nBREzEXdHi8NMxE9DgGsrFRFHUxYqeFCoFEnRPFFtvA892HSlFG n9MQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=A3j6qf/K4hIubAF4tW3V+KAutizyuFUnunQ+gj1IRMw=; b=dckzDpF3DnPdkCTH9XxL6z9WnLFs3MdxjK8rFUbUulkL4MdIQ2ZKHh2PQXEyEeRsqN Lw8hu3CXkNbHLz/VjN/Y07rbNP9SZ5+FZyVIy26DQZWJm4mS37ljkaqc+fmPgKupOZNY Lg7h0u5xVWuvVpFhqI5YJRHFK3cmho90grwqlg/kfuIQ2AmPg32K4ctsku9H6hz35gXa IyU2m0Fcbcm4xPNEF4m3mr0m55KnwIOeYuDStGAIibv2eI2ndYwYGEFeTTQDFZ02Vzbs uG85bsLWkEIRub93cnN+LjZ06LBkuWG+mBO97V2URD+QMqv1DADDcvOey72PaC3YBuU3 tdXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jlk2xHjS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l23si12961157ejs.694.2021.08.08.09.26.55; Sun, 08 Aug 2021 09:27:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jlk2xHjS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232260AbhHHQZs (ORCPT + 99 others); Sun, 8 Aug 2021 12:25:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:47316 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232051AbhHHQZj (ORCPT ); Sun, 8 Aug 2021 12:25:39 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7F64B610CB; Sun, 8 Aug 2021 16:25:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1628439920; bh=BUPoxALGC7F75VOkWPfL8v5IuC/ZnFA2iXsEfysXhCI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=jlk2xHjSB3vCq6E0hlc67/375GfJRZJ9Ws1YxQ+dX1+bTRkucGkfObV2q2EHTxOls cr9ZDxbxOLgjznzzCIqlhXTj+MAhu04CuPhVT0krP7SDiHNb7YIRUcMFdd8JVv6lUq T2LEHvz2QJ/lc52ZXvyWP7WP5Ezng2UIad7JWsf2mBcJ86CwHSLHU0YwUOMgS+T39l qMvtXAH/riRRJHnyS06r8Vut06PWSdz8sxOuv6oydNMZllYoAD6KB4nkOWS4Lw9ZJu Gp1iVVVBdKJQd3qi0VpNmR8PECjq7CdzYGSqhM+mSKQh+TC9MR8z367myFOdc0nBHR L0joQoFIiupfA== Received: by pali.im (Postfix) id 3F73113DC; Sun, 8 Aug 2021 18:25:20 +0200 (CEST) From: =?UTF-8?q?Pali=20Roh=C3=A1r?= To: linux-fsdevel@vger.kernel.org, linux-ntfs-dev@lists.sourceforge.net, linux-cifs@vger.kernel.org, jfs-discussion@lists.sourceforge.net, linux-kernel@vger.kernel.org, Alexander Viro , Jan Kara , OGAWA Hirofumi , "Theodore Y . Ts'o" , Luis de Bethencourt , Salah Triki , Andrew Morton , Dave Kleikamp , Anton Altaparmakov , Pavel Machek , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Christoph Hellwig Subject: [RFC PATCH 15/20] jfs: Fix buffer overflow in jfs_strfromUCS_le() function Date: Sun, 8 Aug 2021 18:24:48 +0200 Message-Id: <20210808162453.1653-16-pali@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210808162453.1653-1-pali@kernel.org> References: <20210808162453.1653-1-pali@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Function jfs_strfromUCS_le() writes to unknown offset in buffer allocated by __get_free_page(GFP_KERNEL). So it cannot expects that there is least NLS_MAX_CHARSET_SIZE bytes space before end of that buffer. Fix this issue by add a new parameter maxlen for jfs_strfromUCS_le() function. And use it for passing remaining size of buffer to prevent buffer overflow in kernel. Signed-off-by: Pali Rohár --- fs/jfs/jfs_dtree.c | 13 ++++++++++--- fs/jfs/jfs_unicode.c | 6 +++--- fs/jfs/jfs_unicode.h | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/fs/jfs/jfs_dtree.c b/fs/jfs/jfs_dtree.c index 837d42f61464..6dbdce54f139 100644 --- a/fs/jfs/jfs_dtree.c +++ b/fs/jfs/jfs_dtree.c @@ -3013,6 +3013,7 @@ int jfs_readdir(struct file *file, struct dir_context *ctx) int d_namleft, len, outlen; unsigned long dirent_buf; char *name_ptr; + int maxlen; u32 dir_index; int do_index = 0; uint loop_count = 0; @@ -3235,7 +3236,10 @@ int jfs_readdir(struct file *file, struct dir_context *ctx) } /* copy the name of head/only segment */ - outlen = jfs_strfromUCS_le(name_ptr, d->name, len, + maxlen = PAGE_SIZE - sizeof(struct jfs_dirent) - + (name_ptr - jfs_dirent->name); + outlen = jfs_strfromUCS_le(name_ptr, maxlen, + d->name, len, codepage); jfs_dirent->name_len = outlen; @@ -3255,8 +3259,11 @@ int jfs_readdir(struct file *file, struct dir_context *ctx) goto skip_one; } len = min(d_namleft, DTSLOTDATALEN); - outlen = jfs_strfromUCS_le(name_ptr, t->name, - len, codepage); + maxlen = PAGE_SIZE - sizeof(struct jfs_dirent) - + (name_ptr - jfs_dirent->name); + outlen = jfs_strfromUCS_le(name_ptr, maxlen, + t->name, len, + codepage); jfs_dirent->name_len += outlen; next = t->next; diff --git a/fs/jfs/jfs_unicode.c b/fs/jfs/jfs_unicode.c index 1d0f65d13b58..2db923872bf1 100644 --- a/fs/jfs/jfs_unicode.c +++ b/fs/jfs/jfs_unicode.c @@ -16,7 +16,7 @@ * FUNCTION: Convert little-endian unicode string to character string * */ -int jfs_strfromUCS_le(char *to, const __le16 * from, +int jfs_strfromUCS_le(char *to, int maxlen, const __le16 * from, int len, struct nls_table *codepage) { int i; @@ -25,12 +25,12 @@ int jfs_strfromUCS_le(char *to, const __le16 * from, int warn = !!warn_again; /* once per string */ if (codepage) { - for (i = 0; (i < len) && from[i]; i++) { + for (i = 0; (i < len) && from[i] && outlen < maxlen-1; i++) { int charlen; charlen = codepage->uni2char(le16_to_cpu(from[i]), &to[outlen], - NLS_MAX_CHARSET_SIZE); + maxlen-1-outlen); if (charlen > 0) outlen += charlen; else { diff --git a/fs/jfs/jfs_unicode.h b/fs/jfs/jfs_unicode.h index 9db62d047daa..8b5c74315e07 100644 --- a/fs/jfs/jfs_unicode.h +++ b/fs/jfs/jfs_unicode.h @@ -19,7 +19,7 @@ typedef struct { extern signed char UniUpperTable[512]; extern UNICASERANGE UniUpperRange[]; extern int get_UCSname(struct component_name *, struct dentry *); -extern int jfs_strfromUCS_le(char *, const __le16 *, int, struct nls_table *); +extern int jfs_strfromUCS_le(char *, int, const __le16 *, int, struct nls_table *); #define free_UCSname(COMP) kfree((COMP)->name) -- 2.20.1