udf: Prevent buffer overrun with multi-byte characters
[firefly-linux-kernel-4.4.55.git] / fs / udf / unicode.c
1 /*
2  * unicode.c
3  *
4  * PURPOSE
5  *      Routines for converting between UTF-8 and OSTA Compressed Unicode.
6  *      Also handles filename mangling
7  *
8  * DESCRIPTION
9  *      OSTA Compressed Unicode is explained in the OSTA UDF specification.
10  *              http://www.osta.org/
11  *      UTF-8 is explained in the IETF RFC XXXX.
12  *              ftp://ftp.internic.net/rfc/rfcxxxx.txt
13  *
14  * COPYRIGHT
15  *      This file is distributed under the terms of the GNU General Public
16  *      License (GPL). Copies of the GPL can be obtained from:
17  *              ftp://prep.ai.mit.edu/pub/gnu/GPL
18  *      Each contributing author retains all rights to their own work.
19  */
20
21 #include "udfdecl.h"
22
23 #include <linux/kernel.h>
24 #include <linux/string.h>       /* for memset */
25 #include <linux/nls.h>
26 #include <linux/crc-itu-t.h>
27 #include <linux/slab.h>
28
29 #include "udf_sb.h"
30
31 static int udf_translate_to_linux(uint8_t *, int, uint8_t *, int, uint8_t *,
32                                   int);
33
34 static int udf_char_to_ustr(struct ustr *dest, const uint8_t *src, int strlen)
35 {
36         if ((!dest) || (!src) || (!strlen) || (strlen > UDF_NAME_LEN - 2))
37                 return 0;
38
39         memset(dest, 0, sizeof(struct ustr));
40         memcpy(dest->u_name, src, strlen);
41         dest->u_cmpID = 0x08;
42         dest->u_len = strlen;
43
44         return strlen;
45 }
46
47 /*
48  * udf_build_ustr
49  */
50 int udf_build_ustr(struct ustr *dest, dstring *ptr, int size)
51 {
52         int usesize;
53
54         if (!dest || !ptr || !size)
55                 return -1;
56         BUG_ON(size < 2);
57
58         usesize = min_t(size_t, ptr[size - 1], sizeof(dest->u_name));
59         usesize = min(usesize, size - 2);
60         dest->u_cmpID = ptr[0];
61         dest->u_len = usesize;
62         memcpy(dest->u_name, ptr + 1, usesize);
63         memset(dest->u_name + usesize, 0, sizeof(dest->u_name) - usesize);
64
65         return 0;
66 }
67
68 /*
69  * udf_build_ustr_exact
70  */
71 static void udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
72 {
73         memset(dest, 0, sizeof(struct ustr));
74         dest->u_cmpID = ptr[0];
75         dest->u_len = exactsize - 1;
76         memcpy(dest->u_name, ptr + 1, exactsize - 1);
77 }
78
79 /*
80  * udf_CS0toUTF8
81  *
82  * PURPOSE
83  *      Convert OSTA Compressed Unicode to the UTF-8 equivalent.
84  *
85  * PRE-CONDITIONS
86  *      utf                     Pointer to UTF-8 output buffer.
87  *      ocu                     Pointer to OSTA Compressed Unicode input buffer
88  *                              of size UDF_NAME_LEN bytes.
89  *                              both of type "struct ustr *"
90  *
91  * POST-CONDITIONS
92  *      <return>                >= 0 on success.
93  *
94  * HISTORY
95  *      November 12, 1997 - Andrew E. Mileski
96  *      Written, tested, and released.
97  */
98 int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i)
99 {
100         const uint8_t *ocu;
101         uint8_t cmp_id, ocu_len;
102         int i;
103
104         ocu_len = ocu_i->u_len;
105         if (ocu_len == 0) {
106                 memset(utf_o, 0, sizeof(struct ustr));
107                 return 0;
108         }
109
110         cmp_id = ocu_i->u_cmpID;
111         if (cmp_id != 8 && cmp_id != 16) {
112                 memset(utf_o, 0, sizeof(struct ustr));
113                 pr_err("unknown compression code (%d) stri=%s\n",
114                        cmp_id, ocu_i->u_name);
115                 return -EINVAL;
116         }
117
118         ocu = ocu_i->u_name;
119         utf_o->u_len = 0;
120         for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
121
122                 /* Expand OSTA compressed Unicode to Unicode */
123                 uint32_t c = ocu[i++];
124                 if (cmp_id == 16)
125                         c = (c << 8) | ocu[i++];
126
127                 /* Compress Unicode to UTF-8 */
128                 if (c < 0x80U)
129                         utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
130                 else if (c < 0x800U) {
131                         if (utf_o->u_len > (UDF_NAME_LEN - 4))
132                                 break;
133                         utf_o->u_name[utf_o->u_len++] =
134                                                 (uint8_t)(0xc0 | (c >> 6));
135                         utf_o->u_name[utf_o->u_len++] =
136                                                 (uint8_t)(0x80 | (c & 0x3f));
137                 } else {
138                         if (utf_o->u_len > (UDF_NAME_LEN - 5))
139                                 break;
140                         utf_o->u_name[utf_o->u_len++] =
141                                                 (uint8_t)(0xe0 | (c >> 12));
142                         utf_o->u_name[utf_o->u_len++] =
143                                                 (uint8_t)(0x80 |
144                                                           ((c >> 6) & 0x3f));
145                         utf_o->u_name[utf_o->u_len++] =
146                                                 (uint8_t)(0x80 | (c & 0x3f));
147                 }
148         }
149         utf_o->u_cmpID = 8;
150
151         return utf_o->u_len;
152 }
153
154 /*
155  *
156  * udf_UTF8toCS0
157  *
158  * PURPOSE
159  *      Convert UTF-8 to the OSTA Compressed Unicode equivalent.
160  *
161  * DESCRIPTION
162  *      This routine is only called by udf_lookup().
163  *
164  * PRE-CONDITIONS
165  *      ocu                     Pointer to OSTA Compressed Unicode output
166  *                              buffer of size UDF_NAME_LEN bytes.
167  *      utf                     Pointer to UTF-8 input buffer.
168  *      utf_len                 Length of UTF-8 input buffer in bytes.
169  *
170  * POST-CONDITIONS
171  *      <return>                Zero on success.
172  *
173  * HISTORY
174  *      November 12, 1997 - Andrew E. Mileski
175  *      Written, tested, and released.
176  */
177 static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length)
178 {
179         unsigned c, i, max_val, utf_char;
180         int utf_cnt, u_len;
181
182         memset(ocu, 0, sizeof(dstring) * length);
183         ocu[0] = 8;
184         max_val = 0xffU;
185
186 try_again:
187         u_len = 0U;
188         utf_char = 0U;
189         utf_cnt = 0U;
190         for (i = 0U; i < utf->u_len; i++) {
191                 c = (uint8_t)utf->u_name[i];
192
193                 /* Complete a multi-byte UTF-8 character */
194                 if (utf_cnt) {
195                         utf_char = (utf_char << 6) | (c & 0x3fU);
196                         if (--utf_cnt)
197                                 continue;
198                 } else {
199                         /* Check for a multi-byte UTF-8 character */
200                         if (c & 0x80U) {
201                                 /* Start a multi-byte UTF-8 character */
202                                 if ((c & 0xe0U) == 0xc0U) {
203                                         utf_char = c & 0x1fU;
204                                         utf_cnt = 1;
205                                 } else if ((c & 0xf0U) == 0xe0U) {
206                                         utf_char = c & 0x0fU;
207                                         utf_cnt = 2;
208                                 } else if ((c & 0xf8U) == 0xf0U) {
209                                         utf_char = c & 0x07U;
210                                         utf_cnt = 3;
211                                 } else if ((c & 0xfcU) == 0xf8U) {
212                                         utf_char = c & 0x03U;
213                                         utf_cnt = 4;
214                                 } else if ((c & 0xfeU) == 0xfcU) {
215                                         utf_char = c & 0x01U;
216                                         utf_cnt = 5;
217                                 } else {
218                                         goto error_out;
219                                 }
220                                 continue;
221                         } else {
222                                 /* Single byte UTF-8 character (most common) */
223                                 utf_char = c;
224                         }
225                 }
226
227                 /* Choose no compression if necessary */
228                 if (utf_char > max_val) {
229                         if (max_val == 0xffU) {
230                                 max_val = 0xffffU;
231                                 ocu[0] = (uint8_t)0x10U;
232                                 goto try_again;
233                         }
234                         goto error_out;
235                 }
236
237                 if (max_val == 0xffffU)
238                         ocu[++u_len] = (uint8_t)(utf_char >> 8);
239                 ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
240         }
241
242         if (utf_cnt) {
243 error_out:
244                 ocu[++u_len] = '?';
245                 printk(KERN_DEBUG pr_fmt("bad UTF-8 character\n"));
246         }
247
248         ocu[length - 1] = (uint8_t)u_len + 1;
249
250         return u_len + 1;
251 }
252
253 static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
254                         const struct ustr *ocu_i)
255 {
256         const uint8_t *ocu;
257         uint8_t cmp_id, ocu_len;
258         int i, len;
259
260
261         ocu_len = ocu_i->u_len;
262         if (ocu_len == 0) {
263                 memset(utf_o, 0, sizeof(struct ustr));
264                 return 0;
265         }
266
267         cmp_id = ocu_i->u_cmpID;
268         if (cmp_id != 8 && cmp_id != 16) {
269                 memset(utf_o, 0, sizeof(struct ustr));
270                 pr_err("unknown compression code (%d) stri=%s\n",
271                        cmp_id, ocu_i->u_name);
272                 return -EINVAL;
273         }
274
275         ocu = ocu_i->u_name;
276         utf_o->u_len = 0;
277         for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
278                 /* Expand OSTA compressed Unicode to Unicode */
279                 uint32_t c = ocu[i++];
280                 if (cmp_id == 16)
281                         c = (c << 8) | ocu[i++];
282
283                 len = nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
284                                     UDF_NAME_LEN - 2 - utf_o->u_len);
285                 /* Valid character? */
286                 if (len >= 0)
287                         utf_o->u_len += len;
288                 else
289                         utf_o->u_name[utf_o->u_len++] = '?';
290         }
291         utf_o->u_cmpID = 8;
292
293         return utf_o->u_len;
294 }
295
296 static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
297                         int length)
298 {
299         int len;
300         unsigned i, max_val;
301         uint16_t uni_char;
302         int u_len;
303
304         memset(ocu, 0, sizeof(dstring) * length);
305         ocu[0] = 8;
306         max_val = 0xffU;
307
308 try_again:
309         u_len = 0U;
310         for (i = 0U; i < uni->u_len; i++) {
311                 len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
312                 if (!len)
313                         continue;
314                 /* Invalid character, deal with it */
315                 if (len < 0) {
316                         len = 1;
317                         uni_char = '?';
318                 }
319
320                 if (uni_char > max_val) {
321                         max_val = 0xffffU;
322                         ocu[0] = (uint8_t)0x10U;
323                         goto try_again;
324                 }
325
326                 if (max_val == 0xffffU)
327                         ocu[++u_len] = (uint8_t)(uni_char >> 8);
328                 ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
329                 i += len - 1;
330         }
331
332         ocu[length - 1] = (uint8_t)u_len + 1;
333         return u_len + 1;
334 }
335
336 int udf_get_filename(struct super_block *sb, uint8_t *sname, int slen,
337                      uint8_t *dname, int dlen)
338 {
339         struct ustr *filename, *unifilename;
340         int ret;
341
342         if (!slen)
343                 return -EIO;
344
345         filename = kmalloc(sizeof(struct ustr), GFP_NOFS);
346         if (!filename)
347                 return -ENOMEM;
348
349         unifilename = kmalloc(sizeof(struct ustr), GFP_NOFS);
350         if (!unifilename) {
351                 ret = -ENOMEM;
352                 goto out1;
353         }
354
355         udf_build_ustr_exact(unifilename, sname, slen);
356         if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
357                 ret = udf_CS0toUTF8(filename, unifilename);
358                 if (ret < 0) {
359                         udf_debug("Failed in udf_get_filename: sname = %s\n",
360                                   sname);
361                         goto out2;
362                 }
363         } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
364                 ret = udf_CS0toNLS(UDF_SB(sb)->s_nls_map, filename,
365                                    unifilename);
366                 if (ret < 0) {
367                         udf_debug("Failed in udf_get_filename: sname = %s\n",
368                                   sname);
369                         goto out2;
370                 }
371         } else
372                 BUG();
373
374         ret = udf_translate_to_linux(dname, dlen,
375                                      filename->u_name, filename->u_len,
376                                      unifilename->u_name, unifilename->u_len);
377         /* Zero length filename isn't valid... */
378         if (ret == 0)
379                 ret = -EINVAL;
380 out2:
381         kfree(unifilename);
382 out1:
383         kfree(filename);
384         return ret;
385 }
386
387 int udf_put_filename(struct super_block *sb, const uint8_t *sname,
388                      uint8_t *dname, int flen)
389 {
390         struct ustr unifilename;
391         int namelen;
392
393         if (!udf_char_to_ustr(&unifilename, sname, flen))
394                 return 0;
395
396         if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
397                 namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
398                 if (!namelen)
399                         return 0;
400         } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
401                 namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
402                                         &unifilename, UDF_NAME_LEN);
403                 if (!namelen)
404                         return 0;
405         } else
406                 return 0;
407
408         return namelen;
409 }
410
411 #define ILLEGAL_CHAR_MARK       '_'
412 #define EXT_MARK                '.'
413 #define CRC_MARK                '#'
414 #define EXT_SIZE                5
415 /* Number of chars we need to store generated CRC to make filename unique */
416 #define CRC_LEN                 5
417
418 static int udf_translate_to_linux(uint8_t *newName, int newLen,
419                                   uint8_t *udfName, int udfLen,
420                                   uint8_t *fidName, int fidNameLen)
421 {
422         int index, newIndex = 0, needsCRC = 0;
423         int extIndex = 0, newExtIndex = 0, hasExt = 0;
424         unsigned short valueCRC;
425         uint8_t curr;
426
427         if (udfName[0] == '.' &&
428             (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
429                 needsCRC = 1;
430                 newIndex = udfLen;
431                 memcpy(newName, udfName, udfLen);
432         } else {
433                 for (index = 0; index < udfLen; index++) {
434                         curr = udfName[index];
435                         if (curr == '/' || curr == 0) {
436                                 needsCRC = 1;
437                                 curr = ILLEGAL_CHAR_MARK;
438                                 while (index + 1 < udfLen &&
439                                                 (udfName[index + 1] == '/' ||
440                                                  udfName[index + 1] == 0))
441                                         index++;
442                         }
443                         if (curr == EXT_MARK &&
444                                         (udfLen - index - 1) <= EXT_SIZE) {
445                                 if (udfLen == index + 1)
446                                         hasExt = 0;
447                                 else {
448                                         hasExt = 1;
449                                         extIndex = index;
450                                         newExtIndex = newIndex;
451                                 }
452                         }
453                         if (newIndex < newLen)
454                                 newName[newIndex++] = curr;
455                         else
456                                 needsCRC = 1;
457                 }
458         }
459         if (needsCRC) {
460                 uint8_t ext[EXT_SIZE];
461                 int localExtIndex = 0;
462
463                 if (hasExt) {
464                         int maxFilenameLen;
465                         for (index = 0;
466                              index < EXT_SIZE && extIndex + index + 1 < udfLen;
467                              index++) {
468                                 curr = udfName[extIndex + index + 1];
469
470                                 if (curr == '/' || curr == 0) {
471                                         needsCRC = 1;
472                                         curr = ILLEGAL_CHAR_MARK;
473                                         while (extIndex + index + 2 < udfLen &&
474                                               (index + 1 < EXT_SIZE &&
475                                                 (udfName[extIndex + index + 2] == '/' ||
476                                                  udfName[extIndex + index + 2] == 0)))
477                                                 index++;
478                                 }
479                                 ext[localExtIndex++] = curr;
480                         }
481                         maxFilenameLen = newLen - CRC_LEN - localExtIndex;
482                         if (newIndex > maxFilenameLen)
483                                 newIndex = maxFilenameLen;
484                         else
485                                 newIndex = newExtIndex;
486                 } else if (newIndex > newLen - CRC_LEN)
487                         newIndex = newLen - CRC_LEN;
488                 newName[newIndex++] = CRC_MARK;
489                 valueCRC = crc_itu_t(0, fidName, fidNameLen);
490                 newName[newIndex++] = hex_asc_upper_hi(valueCRC >> 8);
491                 newName[newIndex++] = hex_asc_upper_lo(valueCRC >> 8);
492                 newName[newIndex++] = hex_asc_upper_hi(valueCRC);
493                 newName[newIndex++] = hex_asc_upper_lo(valueCRC);
494
495                 if (hasExt) {
496                         newName[newIndex++] = EXT_MARK;
497                         for (index = 0; index < localExtIndex; index++)
498                                 newName[newIndex++] = ext[index];
499                 }
500         }
501
502         return newIndex;
503 }