]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/unicrash.c
b02c5658e9dcb6ef2c7225b9088ae9bcb183e0a2
1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
10 #include <sys/types.h>
11 #include <sys/statvfs.h>
13 #include <unicode/ustring.h>
14 #include <unicode/unorm2.h>
15 #include <unicode/uspoof.h>
16 #include "libfrog/paths.h"
17 #include "xfs_scrub.h"
22 * Detect Unicode confusable names in directories and attributes.
24 * Record all the name->ino mappings in a directory/xattr, with a twist! The
25 * twist is to record the Unicode skeleton and normalized version of every
26 * name we see so that we can check for a name space (directory, extended
27 * attribute set) containing names containing malicious characters or that
28 * could be confused for one another. These entries are at best a sign of
29 * Unicode mishandling, or some sort of weird name substitution attack if the
30 * entries do not point to the same inode. Warn if we see multiple dirents
31 * that do not all point to the same inode.
33 * For extended attributes we perform the same collision checks on the
34 * attribute, though any collision is enough to trigger a warning.
36 * We avoid flagging these problems as errors because XFS treats names as a
37 * sequence of arbitrary nonzero bytes. While a Unicode collision is not
38 * technically a filesystem corruption, we ought to say something if there's a
39 * possibility for misleading a user. Unquestionably bad things (direction
40 * overrides, control characters, names that normalize to the same string)
41 * produce warnings, whereas potentially confusable names produce
42 * informational messages.
44 * The skeleton algorithm is detailed in section 4 ("Confusable Detection") of
45 * the Unicode technical standard #39. First we normalize the name, then we
46 * substitute code points according to the confusable code point table, then
49 * We take the extra step of removing non-identifier code points such as
50 * formatting characters, control characters, zero width characters, etc.
51 * from the skeleton so that we can complain about names that are confusable
52 * due to invisible control characters.
54 * In other words, skel = remove_invisible(nfd(remap_confusables(nfd(name)))).
58 struct name_entry
*next
;
60 /* NFKC normalized name */
64 /* Unicode skeletonized name */
74 #define NAME_ENTRY_SZ(nl) (sizeof(struct name_entry) + 1 + \
75 (nl * sizeof(uint8_t)))
78 struct scrub_ctx
*ctx
;
80 const UNormalizer2
*normalizer
;
82 bool is_only_root_writeable
;
84 struct name_entry
*buckets
[0];
86 #define UNICRASH_SZ(nr) (sizeof(struct unicrash) + \
87 (nr * sizeof(struct name_entry *)))
89 /* Things to complain about in Unicode naming. */
92 * Multiple names resolve to the same normalized string and therefore render
95 #define UNICRASH_NOT_UNIQUE (1 << 0)
97 /* Name contains directional overrides. */
98 #define UNICRASH_BIDI_OVERRIDE (1 << 1)
100 /* Name mixes left-to-right and right-to-left characters. */
101 #define UNICRASH_BIDI_MIXED (1 << 2)
103 /* Control characters in name. */
104 #define UNICRASH_CONTROL_CHAR (1 << 3)
106 /* Invisible characters. Only a problem if we have collisions. */
107 #define UNICRASH_ZERO_WIDTH (1 << 4)
109 /* Multiple names resolve to the same skeleton string. */
110 #define UNICRASH_CONFUSABLE (1 << 5)
113 * We only care about validating utf8 collisions if the underlying
114 * system configuration says we're using utf8. If the language
115 * specifier string used to output messages has ".UTF-8" somewhere in
116 * its name, then we conclude utf8 is in use. Otherwise, no checking is
119 * Most modern Linux systems default to utf8, so the only time this
120 * check will return false is if the administrator configured things
121 * this way or if things are so messed up there is no locale data at
124 #define UTF8_STR ".UTF-8"
125 #define UTF8_STRLEN (sizeof(UTF8_STR) - 1)
129 const char *msg_locale
;
130 static int answer
= -1;
135 msg_locale
= setlocale(LC_MESSAGES
, NULL
);
136 if (msg_locale
== NULL
)
139 if (strstr(msg_locale
, UTF8_STR
) != NULL
)
147 * Generate normalized form and skeleton of the name.
148 * If this fails, just forget everything; this is an advisory checker.
151 name_entry_compute_checknames(
153 struct name_entry
*entry
)
164 UErrorCode uerr
= U_ZERO_ERROR
;
166 /* Convert bytestr to unistr for normalization */
167 u_strFromUTF8(NULL
, 0, &unistrlen
, entry
->name
, entry
->namelen
, &uerr
);
168 if (uerr
!= U_BUFFER_OVERFLOW_ERROR
)
171 unistr
= calloc(unistrlen
+ 1, sizeof(UChar
));
174 u_strFromUTF8(unistr
, unistrlen
, NULL
, entry
->name
, entry
->namelen
,
179 /* Normalize the string. */
180 normstrlen
= unorm2_normalize(uc
->normalizer
, unistr
, unistrlen
, NULL
,
182 if (uerr
!= U_BUFFER_OVERFLOW_ERROR
)
185 normstr
= calloc(normstrlen
+ 1, sizeof(UChar
));
188 unorm2_normalize(uc
->normalizer
, unistr
, unistrlen
, normstr
, normstrlen
,
193 /* Compute skeleton. */
194 skelstrlen
= uspoof_getSkeleton(uc
->spoof
, 0, unistr
, unistrlen
, NULL
,
196 if (uerr
!= U_BUFFER_OVERFLOW_ERROR
)
199 skelstr
= calloc(skelstrlen
+ 1, sizeof(UChar
));
202 uspoof_getSkeleton(uc
->spoof
, 0, unistr
, unistrlen
, skelstr
, skelstrlen
,
207 /* Remove control/formatting characters from skeleton. */
208 for (i
= 0, j
= 0; i
< skelstrlen
; j
= i
) {
209 U16_NEXT_UNSAFE(skelstr
, i
, uchr
);
210 if (!u_isIDIgnorable(uchr
))
212 memmove(&skelstr
[j
], &skelstr
[i
],
213 (skelstrlen
- i
+ 1) * sizeof(UChar
));
214 skelstrlen
-= (i
- j
);
218 entry
->skelstr
= skelstr
;
219 entry
->skelstrlen
= skelstrlen
;
220 entry
->normstr
= normstr
;
221 entry
->normstrlen
= normstrlen
;
234 /* Create a new name entry, returns false if we could not succeed. */
240 struct name_entry
**entry
)
242 struct name_entry
*new_entry
;
243 size_t namelen
= strlen(name
);
245 /* Create new entry */
246 new_entry
= calloc(NAME_ENTRY_SZ(namelen
), 1);
249 new_entry
->next
= NULL
;
250 new_entry
->ino
= ino
;
251 memcpy(new_entry
->name
, name
, namelen
);
252 new_entry
->name
[namelen
] = 0;
253 new_entry
->namelen
= namelen
;
255 /* Normalize/skeletonize name to find collisions. */
256 if (!name_entry_compute_checknames(uc
, new_entry
))
267 /* Free a name entry */
270 struct name_entry
*entry
)
272 free(entry
->normstr
);
273 free(entry
->skelstr
);
277 /* Adapt the dirhash function from libxfs, avoid linking with libxfs. */
279 #define rol32(x, y) (((x) << (y)) | ((x) >> (32 - (y))))
282 * Implement a simple hash on a character string.
283 * Rotate the hash value by 7 bits, then XOR each character in.
284 * This is implemented with some source-level loop unrolling.
288 struct name_entry
*entry
)
294 name
= (uint8_t *)entry
->skelstr
;
295 namelen
= entry
->skelstrlen
* sizeof(UChar
);
298 * Do four characters at a time as long as we can.
300 for (hash
= 0; namelen
>= 4; namelen
-= 4, name
+= 4)
301 hash
= (name
[0] << 21) ^ (name
[1] << 14) ^ (name
[2] << 7) ^
302 (name
[3] << 0) ^ rol32(hash
, 7 * 4);
305 * Now do the rest of the characters.
309 return (name
[0] << 14) ^ (name
[1] << 7) ^ (name
[2] << 0) ^
312 return (name
[0] << 7) ^ (name
[1] << 0) ^ rol32(hash
, 7 * 2);
314 return (name
[0] << 0) ^ rol32(hash
, 7 * 1);
315 default: /* case 0: */
321 * Check a name for suspicious elements that have appeared in filename
322 * spoofing attacks. This includes names that mixed directions or contain
323 * direction overrides control characters, both of which have appeared in
324 * filename spoofing attacks.
328 struct name_entry
*entry
,
329 unsigned int *badflags
)
335 for (i
= 0; i
< entry
->normstrlen
;) {
336 U16_NEXT_UNSAFE(entry
->normstr
, i
, uchr
);
338 /* zero width character sequences */
340 case 0x200B: /* zero width space */
341 case 0x200C: /* zero width non-joiner */
342 case 0x200D: /* zero width joiner */
343 case 0xFEFF: /* zero width non breaking space */
344 case 0x2060: /* word joiner */
345 case 0x2061: /* function application */
346 case 0x2062: /* invisible times (multiply) */
347 case 0x2063: /* invisible separator (comma) */
348 case 0x2064: /* invisible plus (addition) */
349 *badflags
|= UNICRASH_ZERO_WIDTH
;
353 /* control characters */
355 *badflags
|= UNICRASH_CONTROL_CHAR
;
357 switch (u_charDirection(uchr
)) {
358 case U_LEFT_TO_RIGHT
:
361 case U_RIGHT_TO_LEFT
:
364 case U_RIGHT_TO_LEFT_OVERRIDE
:
365 *badflags
|= UNICRASH_BIDI_OVERRIDE
;
367 case U_LEFT_TO_RIGHT_OVERRIDE
:
368 *badflags
|= UNICRASH_BIDI_OVERRIDE
;
375 /* mixing left-to-right and right-to-left chars */
377 *badflags
|= UNICRASH_BIDI_MIXED
;
380 /* Initialize the collision detector. */
383 struct unicrash
**ucp
,
384 struct scrub_ctx
*ctx
,
387 bool is_only_root_writeable
)
390 UErrorCode uerr
= U_ZERO_ERROR
;
392 if (!is_utf8_locale()) {
397 if (nr_buckets
> 65536)
399 else if (nr_buckets
< 16)
402 p
= calloc(1, UNICRASH_SZ(nr_buckets
));
406 p
->nr_buckets
= nr_buckets
;
407 p
->compare_ino
= compare_ino
;
408 p
->normalizer
= unorm2_getNFKCInstance(&uerr
);
411 p
->spoof
= uspoof_open(&uerr
);
414 uspoof_setChecks(p
->spoof
, USPOOF_ALL_CHECKS
, &uerr
);
417 p
->is_only_root_writeable
= is_only_root_writeable
;
422 uspoof_close(p
->spoof
);
429 * Is this inode owned by root and not writable by others? If so, skip
430 * even the informational messages, because this was put in place by the
434 is_only_root_writable(
435 struct xfs_bulkstat
*bstat
)
437 if (bstat
->bs_uid
!= 0 || bstat
->bs_gid
!= 0)
439 return !(bstat
->bs_mode
& S_IWOTH
);
442 /* Initialize the collision detector for a directory. */
445 struct unicrash
**ucp
,
446 struct scrub_ctx
*ctx
,
447 struct xfs_bulkstat
*bstat
)
450 * Assume 64 bytes per dentry, clamp buckets between 16 and 64k.
451 * Same general idea as dir_hash_init in xfs_repair.
453 return unicrash_init(ucp
, ctx
, true, bstat
->bs_size
/ 64,
454 is_only_root_writable(bstat
));
457 /* Initialize the collision detector for an extended attribute. */
460 struct unicrash
**ucp
,
461 struct scrub_ctx
*ctx
,
462 struct xfs_bulkstat
*bstat
)
464 /* Assume 16 attributes per extent for lack of a better idea. */
465 return unicrash_init(ucp
, ctx
, false, 16 * (1 + bstat
->bs_aextents
),
466 is_only_root_writable(bstat
));
469 /* Initialize the collision detector for a filesystem label. */
471 unicrash_fs_label_init(
472 struct unicrash
**ucp
,
473 struct scrub_ctx
*ctx
)
475 return unicrash_init(ucp
, ctx
, false, 16, true);
478 /* Free the crash detector. */
483 struct name_entry
*ne
;
484 struct name_entry
*x
;
490 uspoof_close(uc
->spoof
);
491 for (i
= 0; i
< uc
->nr_buckets
; i
++) {
492 for (ne
= uc
->buckets
[i
]; ne
!= NULL
; ne
= x
) {
500 /* Complain about Unicode problems. */
506 struct name_entry
*entry
,
507 unsigned int badflags
,
508 struct name_entry
*dup_entry
)
513 bad1
= string_escape(entry
->name
);
515 bad2
= string_escape(dup_entry
->name
);
518 * Most filechooser UIs do not look for bidirectional overrides when
519 * they render names. This can result in misleading name presentation
520 * that makes "hig<rtl>gnp.sh" render like "highs.png".
522 if (badflags
& UNICRASH_BIDI_OVERRIDE
) {
523 str_warn(uc
->ctx
, descr
,
524 _("Unicode name \"%s\" in %s contains suspicious text direction overrides."),
530 * Two names that normalize to the same string will render
531 * identically even though the filesystem considers them unique
532 * names. "cafe\xcc\x81" and "caf\xc3\xa9" have different byte
533 * sequences, but they both appear as "café".
535 if (badflags
& UNICRASH_NOT_UNIQUE
) {
536 str_warn(uc
->ctx
, descr
,
537 _("Unicode name \"%s\" in %s renders identically to \"%s\"."),
543 * If a name contains invisible/nonprinting characters and can be
544 * confused with another name as a result, we should complain.
545 * "moo<zerowidthspace>cow" and "moocow" are misleading.
547 if ((badflags
& UNICRASH_ZERO_WIDTH
) &&
548 (badflags
& UNICRASH_CONFUSABLE
)) {
549 str_warn(uc
->ctx
, descr
,
550 _("Unicode name \"%s\" in %s could be confused with '%s' due to invisible characters."),
556 * Unfiltered control characters can mess up your terminal and render
557 * invisibly in filechooser UIs.
559 if (badflags
& UNICRASH_CONTROL_CHAR
) {
560 str_warn(uc
->ctx
, descr
,
561 _("Unicode name \"%s\" in %s contains control characters."),
567 * Skip the informational messages if the inode owning the name is
568 * only writeable by root, because those files were put there by the
569 * sysadmin. Also skip names less than four letters long because
570 * there's a much higher chance of collisions with short names.
572 if (!verbose
&& (uc
->is_only_root_writeable
|| entry
->namelen
< 4))
576 * It's not considered good practice (says Unicode) to mix LTR
577 * characters with RTL characters. The mere presence of different
578 * bidirectional characters isn't enough to trip up software, so don't
579 * warn about this too loudly.
581 if (badflags
& UNICRASH_BIDI_MIXED
) {
582 str_info(uc
->ctx
, descr
,
583 _("Unicode name \"%s\" in %s mixes bidirectional characters."),
589 * We'll note if two names could be confusable with each other, but
590 * whether or not the user will actually confuse them is dependent
591 * on the rendering system and the typefaces in use. Maybe "foo.1"
592 * and "moo.l" look the same, maybe they do not.
594 if (badflags
& UNICRASH_CONFUSABLE
) {
595 str_info(uc
->ctx
, descr
,
596 _("Unicode name \"%s\" in %s could be confused with \"%s\"."),
606 * Try to add a name -> ino entry to the collision detector. The name
607 * must be skeletonized according to Unicode TR39 to detect names that
608 * could be visually confused with each other.
613 struct name_entry
*new_entry
,
614 unsigned int *badflags
,
615 struct name_entry
**existing_entry
)
617 struct name_entry
*entry
;
621 /* Store name in hashtable. */
622 hash
= name_entry_hash(new_entry
);
623 bucket
= hash
% uc
->nr_buckets
;
624 entry
= uc
->buckets
[bucket
];
625 new_entry
->next
= entry
;
626 uc
->buckets
[bucket
] = new_entry
;
628 while (entry
!= NULL
) {
629 /* Same normalization? */
630 if (new_entry
->normstrlen
== entry
->normstrlen
&&
631 !u_strcmp(new_entry
->normstr
, entry
->normstr
) &&
632 (uc
->compare_ino
? entry
->ino
!= new_entry
->ino
: true)) {
633 *badflags
|= UNICRASH_NOT_UNIQUE
;
634 *existing_entry
= entry
;
639 if (new_entry
->skelstrlen
== entry
->skelstrlen
&&
640 !u_strcmp(new_entry
->skelstr
, entry
->skelstr
) &&
641 (uc
->compare_ino
? entry
->ino
!= new_entry
->ino
: true)) {
642 *badflags
|= UNICRASH_CONFUSABLE
;
643 *existing_entry
= entry
;
652 /* Check a name for unicode normalization problems or collisions. */
654 __unicrash_check_name(
657 const char *namedescr
,
661 struct name_entry
*dup_entry
= NULL
;
662 struct name_entry
*new_entry
;
663 unsigned int badflags
= 0;
666 /* If we can't create entry data, just skip it. */
667 if (!name_entry_create(uc
, name
, ino
, &new_entry
))
670 name_entry_examine(new_entry
, &badflags
);
672 moveon
= unicrash_add(uc
, new_entry
, &badflags
, &dup_entry
);
677 unicrash_complain(uc
, descr
, namedescr
, new_entry
, badflags
,
683 /* Check a directory entry for unicode normalization problems or collisions. */
685 unicrash_check_dir_name(
688 struct dirent
*dentry
)
692 return __unicrash_check_name(uc
, descr
, _("directory"),
693 dentry
->d_name
, dentry
->d_ino
);
697 * Check an extended attribute name for unicode normalization problems
701 unicrash_check_xattr_name(
704 const char *attrname
)
708 return __unicrash_check_name(uc
, descr
, _("extended attribute"),
713 * Check the fs label for unicode normalization problems or misleading bits.
716 unicrash_check_fs_label(
723 return __unicrash_check_name(uc
, descr
, _("filesystem label"),