From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933541AbbLXQ2y (ORCPT ); Thu, 24 Dec 2015 11:28:54 -0500 Received: from relay1.mentorg.com ([192.94.38.131]:56904 "EHLO relay1.mentorg.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753988AbbLXQ0x (ORCPT ); Thu, 24 Dec 2015 11:26:53 -0500 From: Andrew Gabbasov To: Jan Kara , Subject: [PATCH v2 2/7] udf: Check output buffer length when converting name to CS0 Date: Thu, 24 Dec 2015 10:25:33 -0600 Message-ID: <1450974338-22762-3-git-send-email-andrew_gabbasov@mentor.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1450974338-22762-1-git-send-email-andrew_gabbasov@mentor.com> References: <1450974338-22762-1-git-send-email-andrew_gabbasov@mentor.com> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If a name contains at least some characters with Unicode values exceeding single byte, the CS0 output should have 2 bytes per character. And if other input characters have single byte Unicode values, then the single input byte is converted to 2 output bytes, and the length of output becomes larger than the length of input. And if the input name is long enough, the output length may exceed the allocated buffer length. All this means that conversion from UTF8 or NLS to CS0 requires checking of output length in order to stop when it exceeds the given output buffer size. Signed-off-by: Andrew Gabbasov --- fs/udf/unicode.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/fs/udf/unicode.c b/fs/udf/unicode.c index 95a224b..155f912 100644 --- a/fs/udf/unicode.c +++ b/fs/udf/unicode.c @@ -177,17 +177,18 @@ int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i) static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length) { unsigned c, i, max_val, utf_char; - int utf_cnt, u_len; + int utf_cnt, u_len, u_ch; memset(ocu, 0, sizeof(dstring) * length); ocu[0] = 8; max_val = 0xffU; + u_ch = 1; try_again: u_len = 0U; utf_char = 0U; utf_cnt = 0U; - for (i = 0U; i < utf->u_len; i++) { + for (i = 0U; (i < utf->u_len) && ((u_len + 1 + u_ch) < length); i++) { c = (uint8_t)utf->u_name[i]; /* Complete a multi-byte UTF-8 character */ @@ -229,6 +230,7 @@ try_again: if (max_val == 0xffU) { max_val = 0xffffU; ocu[0] = (uint8_t)0x10U; + u_ch = 2; goto try_again; } goto error_out; @@ -299,15 +301,16 @@ static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni, int len; unsigned i, max_val; uint16_t uni_char; - int u_len; + int u_len, u_ch; memset(ocu, 0, sizeof(dstring) * length); ocu[0] = 8; max_val = 0xffU; + u_ch = 1; try_again: u_len = 0U; - for (i = 0U; i < uni->u_len; i++) { + for (i = 0U; (i < uni->u_len) && ((u_len + 1 + u_ch) < length); i++) { len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char); if (!len) continue; @@ -320,6 +323,7 @@ try_again: if (uni_char > max_val) { max_val = 0xffffU; ocu[0] = (uint8_t)0x10U; + u_ch = 2; goto try_again; } -- 2.1.0