]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/unicrash.c
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"
23 * Detect Unicode confusable names in directories and attributes.
25 * Record all the name->ino mappings in a directory/xattr, with a twist! The
26 * twist is to record the Unicode skeleton and normalized version of every
27 * name we see so that we can check for a name space (directory, extended
28 * attribute set) containing names containing malicious characters or that
29 * could be confused for one another. These entries are at best a sign of
30 * Unicode mishandling, or some sort of weird name substitution attack if the
31 * entries do not point to the same inode. Warn if we see multiple dirents
32 * that do not all point to the same inode.
34 * For extended attributes we perform the same collision checks on the
35 * attribute, though any collision is enough to trigger a warning.
37 * We avoid flagging these problems as errors because XFS treats names as a
38 * sequence of arbitrary nonzero bytes. While a Unicode collision is not
39 * technically a filesystem corruption, we ought to say something if there's a
40 * possibility for misleading a user. Unquestionably bad things (direction
41 * overrides, control characters, names that normalize to the same string)
42 * produce warnings, whereas potentially confusable names produce
43 * informational messages.
45 * The skeleton algorithm is detailed in section 4 ("Confusable Detection") of
46 * the Unicode technical standard #39. First we normalize the name, then we
47 * substitute code points according to the confusable code point table, then
50 * We take the extra step of removing non-identifier code points such as
51 * formatting characters, control characters, zero width characters, etc.
52 * from the skeleton so that we can complain about names that are confusable
53 * due to invisible control characters.
55 * In other words, skel = remove_invisible(nfd(remap_confusables(nfd(name)))).
59 struct name_entry
*next
;
61 /* NFKC normalized name */
65 /* Unicode skeletonized name */
75 #define NAME_ENTRY_SZ(nl) (sizeof(struct name_entry) + 1 + \
76 (nl * sizeof(uint8_t)))
79 struct scrub_ctx
*ctx
;
81 const UNormalizer2
*normalizer
;
83 bool is_only_root_writeable
;
85 struct name_entry
*buckets
[0];
87 #define UNICRASH_SZ(nr) (sizeof(struct unicrash) + \
88 (nr * sizeof(struct name_entry *)))
90 /* Things to complain about in Unicode naming. */
93 * Multiple names resolve to the same normalized string and therefore render
96 #define UNICRASH_NOT_UNIQUE (1 << 0)
98 /* Name contains directional overrides. */
99 #define UNICRASH_BIDI_OVERRIDE (1 << 1)
101 /* Name mixes left-to-right and right-to-left characters. */
102 #define UNICRASH_BIDI_MIXED (1 << 2)
104 /* Control characters in name. */
105 #define UNICRASH_CONTROL_CHAR (1 << 3)
107 /* Invisible characters. Only a problem if we have collisions. */
108 #define UNICRASH_ZERO_WIDTH (1 << 4)
110 /* Multiple names resolve to the same skeleton string. */
111 #define UNICRASH_CONFUSABLE (1 << 5)
114 * We only care about validating utf8 collisions if the underlying
115 * system configuration says we're using utf8. If the language
116 * specifier string used to output messages has ".UTF-8" somewhere in
117 * its name, then we conclude utf8 is in use. Otherwise, no checking is
120 * Most modern Linux systems default to utf8, so the only time this
121 * check will return false is if the administrator configured things
122 * this way or if things are so messed up there is no locale data at
125 #define UTF8_STR ".UTF-8"
126 #define UTF8_STRLEN (sizeof(UTF8_STR) - 1)
130 const char *msg_locale
;
131 static int answer
= -1;
136 msg_locale
= setlocale(LC_MESSAGES
, NULL
);
137 if (msg_locale
== NULL
)
140 if (strstr(msg_locale
, UTF8_STR
) != NULL
)
148 * Generate normalized form and skeleton of the name. If this fails, just
149 * forget everything and return false; this is an advisory checker.
152 name_entry_compute_checknames(
154 struct name_entry
*entry
)
165 UErrorCode uerr
= U_ZERO_ERROR
;
167 /* Convert bytestr to unistr for normalization */
168 u_strFromUTF8(NULL
, 0, &unistrlen
, entry
->name
, entry
->namelen
, &uerr
);
169 if (uerr
!= U_BUFFER_OVERFLOW_ERROR
)
172 unistr
= calloc(unistrlen
+ 1, sizeof(UChar
));
175 u_strFromUTF8(unistr
, unistrlen
, NULL
, entry
->name
, entry
->namelen
,
180 /* Normalize the string. */
181 normstrlen
= unorm2_normalize(uc
->normalizer
, unistr
, unistrlen
, NULL
,
183 if (uerr
!= U_BUFFER_OVERFLOW_ERROR
)
186 normstr
= calloc(normstrlen
+ 1, sizeof(UChar
));
189 unorm2_normalize(uc
->normalizer
, unistr
, unistrlen
, normstr
, normstrlen
,
194 /* Compute skeleton. */
195 skelstrlen
= uspoof_getSkeleton(uc
->spoof
, 0, unistr
, unistrlen
, NULL
,
197 if (uerr
!= U_BUFFER_OVERFLOW_ERROR
)
200 skelstr
= calloc(skelstrlen
+ 1, sizeof(UChar
));
203 uspoof_getSkeleton(uc
->spoof
, 0, unistr
, unistrlen
, skelstr
, skelstrlen
,
208 /* Remove control/formatting characters from skeleton. */
209 for (i
= 0, j
= 0; i
< skelstrlen
; j
= i
) {
210 U16_NEXT_UNSAFE(skelstr
, i
, uchr
);
211 if (!u_isIDIgnorable(uchr
))
213 memmove(&skelstr
[j
], &skelstr
[i
],
214 (skelstrlen
- i
+ 1) * sizeof(UChar
));
215 skelstrlen
-= (i
- j
);
219 entry
->skelstr
= skelstr
;
220 entry
->skelstrlen
= skelstrlen
;
221 entry
->normstr
= normstr
;
222 entry
->normstrlen
= normstrlen
;
235 /* Create a new name entry, returns false if we could not succeed. */
241 struct name_entry
**entry
)
243 struct name_entry
*new_entry
;
244 size_t namelen
= strlen(name
);
246 /* Create new entry */
247 new_entry
= calloc(NAME_ENTRY_SZ(namelen
), 1);
250 new_entry
->next
= NULL
;
251 new_entry
->ino
= ino
;
252 memcpy(new_entry
->name
, name
, namelen
);
253 new_entry
->name
[namelen
] = 0;
254 new_entry
->namelen
= namelen
;
256 /* Normalize/skeletonize name to find collisions. */
257 if (!name_entry_compute_checknames(uc
, new_entry
))
268 /* Free a name entry */
271 struct name_entry
*entry
)
273 free(entry
->normstr
);
274 free(entry
->skelstr
);
278 /* Adapt the dirhash function from libxfs, avoid linking with libxfs. */
280 #define rol32(x, y) (((x) << (y)) | ((x) >> (32 - (y))))
283 * Implement a simple hash on a character string.
284 * Rotate the hash value by 7 bits, then XOR each character in.
285 * This is implemented with some source-level loop unrolling.
289 struct name_entry
*entry
)
295 name
= (uint8_t *)entry
->skelstr
;
296 namelen
= entry
->skelstrlen
* sizeof(UChar
);
299 * Do four characters at a time as long as we can.
301 for (hash
= 0; namelen
>= 4; namelen
-= 4, name
+= 4)
302 hash
= (name
[0] << 21) ^ (name
[1] << 14) ^ (name
[2] << 7) ^
303 (name
[3] << 0) ^ rol32(hash
, 7 * 4);
306 * Now do the rest of the characters.
310 return (name
[0] << 14) ^ (name
[1] << 7) ^ (name
[2] << 0) ^
313 return (name
[0] << 7) ^ (name
[1] << 0) ^ rol32(hash
, 7 * 2);
315 return (name
[0] << 0) ^ rol32(hash
, 7 * 1);
316 default: /* case 0: */
322 * Check a name for suspicious elements that have appeared in filename
323 * spoofing attacks. This includes names that mixed directions or contain
324 * direction overrides control characters, both of which have appeared in
325 * filename spoofing attacks.
329 struct name_entry
*entry
,
330 unsigned int *badflags
)
336 for (i
= 0; i
< entry
->normstrlen
;) {
337 U16_NEXT_UNSAFE(entry
->normstr
, i
, uchr
);
339 /* zero width character sequences */
341 case 0x200B: /* zero width space */
342 case 0x200C: /* zero width non-joiner */
343 case 0x200D: /* zero width joiner */
344 case 0xFEFF: /* zero width non breaking space */
345 case 0x2060: /* word joiner */
346 case 0x2061: /* function application */
347 case 0x2062: /* invisible times (multiply) */
348 case 0x2063: /* invisible separator (comma) */
349 case 0x2064: /* invisible plus (addition) */
350 *badflags
|= UNICRASH_ZERO_WIDTH
;
354 /* control characters */
356 *badflags
|= UNICRASH_CONTROL_CHAR
;
358 switch (u_charDirection(uchr
)) {
359 case U_LEFT_TO_RIGHT
:
362 case U_RIGHT_TO_LEFT
:
365 case U_RIGHT_TO_LEFT_OVERRIDE
:
366 *badflags
|= UNICRASH_BIDI_OVERRIDE
;
368 case U_LEFT_TO_RIGHT_OVERRIDE
:
369 *badflags
|= UNICRASH_BIDI_OVERRIDE
;
376 /* mixing left-to-right and right-to-left chars */
378 *badflags
|= UNICRASH_BIDI_MIXED
;
381 /* Initialize the collision detector. */
384 struct unicrash
**ucp
,
385 struct scrub_ctx
*ctx
,
388 bool is_only_root_writeable
)
391 UErrorCode uerr
= U_ZERO_ERROR
;
393 if (!is_utf8_locale()) {
398 if (nr_buckets
> 65536)
400 else if (nr_buckets
< 16)
403 p
= calloc(1, UNICRASH_SZ(nr_buckets
));
407 p
->nr_buckets
= nr_buckets
;
408 p
->compare_ino
= compare_ino
;
409 p
->normalizer
= unorm2_getNFKCInstance(&uerr
);
412 p
->spoof
= uspoof_open(&uerr
);
415 uspoof_setChecks(p
->spoof
, USPOOF_ALL_CHECKS
, &uerr
);
418 p
->is_only_root_writeable
= is_only_root_writeable
;
423 uspoof_close(p
->spoof
);
430 * Is this inode owned by root and not writable by others? If so, skip
431 * even the informational messages, because this was put in place by the
435 is_only_root_writable(
436 struct xfs_bulkstat
*bstat
)
438 if (bstat
->bs_uid
!= 0 || bstat
->bs_gid
!= 0)
440 return !(bstat
->bs_mode
& S_IWOTH
);
443 /* Initialize the collision detector for a directory. */
446 struct unicrash
**ucp
,
447 struct scrub_ctx
*ctx
,
448 struct xfs_bulkstat
*bstat
)
451 * Assume 64 bytes per dentry, clamp buckets between 16 and 64k.
452 * Same general idea as dir_hash_init in xfs_repair.
454 return unicrash_init(ucp
, ctx
, true, bstat
->bs_size
/ 64,
455 is_only_root_writable(bstat
));
458 /* Initialize the collision detector for an extended attribute. */
461 struct unicrash
**ucp
,
462 struct scrub_ctx
*ctx
,
463 struct xfs_bulkstat
*bstat
)
465 /* Assume 16 attributes per extent for lack of a better idea. */
466 return unicrash_init(ucp
, ctx
, false, 16 * (1 + bstat
->bs_aextents
),
467 is_only_root_writable(bstat
));
470 /* Initialize the collision detector for a filesystem label. */
472 unicrash_fs_label_init(
473 struct unicrash
**ucp
,
474 struct scrub_ctx
*ctx
)
476 return unicrash_init(ucp
, ctx
, false, 16, true);
479 /* Free the crash detector. */
484 struct name_entry
*ne
;
485 struct name_entry
*x
;
491 uspoof_close(uc
->spoof
);
492 for (i
= 0; i
< uc
->nr_buckets
; i
++) {
493 for (ne
= uc
->buckets
[i
]; ne
!= NULL
; ne
= x
) {
501 /* Complain about Unicode problems. */
507 struct name_entry
*entry
,
508 unsigned int badflags
,
509 struct name_entry
*dup_entry
)
514 bad1
= string_escape(entry
->name
);
516 bad2
= string_escape(dup_entry
->name
);
519 * Most filechooser UIs do not look for bidirectional overrides when
520 * they render names. This can result in misleading name presentation
521 * that makes "hig<rtl>gnp.sh" render like "highs.png".
523 if (badflags
& UNICRASH_BIDI_OVERRIDE
) {
524 str_warn(uc
->ctx
, descr_render(dsc
),
525 _("Unicode name \"%s\" in %s contains suspicious text direction overrides."),
531 * Two names that normalize to the same string will render
532 * identically even though the filesystem considers them unique
533 * names. "cafe\xcc\x81" and "caf\xc3\xa9" have different byte
534 * sequences, but they both appear as "café".
536 if (badflags
& UNICRASH_NOT_UNIQUE
) {
537 str_warn(uc
->ctx
, descr_render(dsc
),
538 _("Unicode name \"%s\" in %s renders identically to \"%s\"."),
544 * If a name contains invisible/nonprinting characters and can be
545 * confused with another name as a result, we should complain.
546 * "moo<zerowidthspace>cow" and "moocow" are misleading.
548 if ((badflags
& UNICRASH_ZERO_WIDTH
) &&
549 (badflags
& UNICRASH_CONFUSABLE
)) {
550 str_warn(uc
->ctx
, descr_render(dsc
),
551 _("Unicode name \"%s\" in %s could be confused with '%s' due to invisible characters."),
557 * Unfiltered control characters can mess up your terminal and render
558 * invisibly in filechooser UIs.
560 if (badflags
& UNICRASH_CONTROL_CHAR
) {
561 str_warn(uc
->ctx
, descr_render(dsc
),
562 _("Unicode name \"%s\" in %s contains control characters."),
568 * Skip the informational messages if the inode owning the name is
569 * only writeable by root, because those files were put there by the
570 * sysadmin. Also skip names less than four letters long because
571 * there's a much higher chance of collisions with short names.
573 if (!verbose
&& (uc
->is_only_root_writeable
|| entry
->namelen
< 4))
577 * It's not considered good practice (says Unicode) to mix LTR
578 * characters with RTL characters. The mere presence of different
579 * bidirectional characters isn't enough to trip up software, so don't
580 * warn about this too loudly.
582 if (badflags
& UNICRASH_BIDI_MIXED
) {
583 str_info(uc
->ctx
, descr_render(dsc
),
584 _("Unicode name \"%s\" in %s mixes bidirectional characters."),
590 * We'll note if two names could be confusable with each other, but
591 * whether or not the user will actually confuse them is dependent
592 * on the rendering system and the typefaces in use. Maybe "foo.1"
593 * and "moo.l" look the same, maybe they do not.
595 if (badflags
& UNICRASH_CONFUSABLE
) {
596 str_info(uc
->ctx
, descr_render(dsc
),
597 _("Unicode name \"%s\" in %s could be confused with \"%s\"."),
607 * Try to add a name -> ino entry to the collision detector. The name
608 * must be skeletonized according to Unicode TR39 to detect names that
609 * could be visually confused with each other.
614 struct name_entry
*new_entry
,
615 unsigned int *badflags
,
616 struct name_entry
**existing_entry
)
618 struct name_entry
*entry
;
622 /* Store name in hashtable. */
623 hash
= name_entry_hash(new_entry
);
624 bucket
= hash
% uc
->nr_buckets
;
625 entry
= uc
->buckets
[bucket
];
626 new_entry
->next
= entry
;
627 uc
->buckets
[bucket
] = new_entry
;
629 while (entry
!= NULL
) {
630 /* Same normalization? */
631 if (new_entry
->normstrlen
== entry
->normstrlen
&&
632 !u_strcmp(new_entry
->normstr
, entry
->normstr
) &&
633 (uc
->compare_ino
? entry
->ino
!= new_entry
->ino
: true)) {
634 *badflags
|= UNICRASH_NOT_UNIQUE
;
635 *existing_entry
= entry
;
640 if (new_entry
->skelstrlen
== entry
->skelstrlen
&&
641 !u_strcmp(new_entry
->skelstr
, entry
->skelstr
) &&
642 (uc
->compare_ino
? entry
->ino
!= new_entry
->ino
: true)) {
643 *badflags
|= UNICRASH_CONFUSABLE
;
644 *existing_entry
= entry
;
651 /* Check a name for unicode normalization problems or collisions. */
653 __unicrash_check_name(
656 const char *namedescr
,
660 struct name_entry
*dup_entry
= NULL
;
661 struct name_entry
*new_entry
= NULL
;
662 unsigned int badflags
= 0;
664 /* If we can't create entry data, just skip it. */
665 if (!name_entry_create(uc
, name
, ino
, &new_entry
))
668 name_entry_examine(new_entry
, &badflags
);
669 unicrash_add(uc
, new_entry
, &badflags
, &dup_entry
);
671 unicrash_complain(uc
, dsc
, namedescr
, new_entry
, badflags
,
678 * Check a directory entry for unicode normalization problems or collisions.
679 * If errors occur, this function will log them and return nonzero.
682 unicrash_check_dir_name(
685 struct dirent
*dentry
)
689 return __unicrash_check_name(uc
, dsc
, _("directory"),
690 dentry
->d_name
, dentry
->d_ino
);
694 * Check an extended attribute name for unicode normalization problems
695 * or collisions. If errors occur, this function will log them and return
699 unicrash_check_xattr_name(
702 const char *attrname
)
706 return __unicrash_check_name(uc
, dsc
, _("extended attribute"),
711 * Check the fs label for unicode normalization problems or misleading bits.
712 * If errors occur, this function will log them and return nonzero.
715 unicrash_check_fs_label(
722 return __unicrash_check_name(uc
, dsc
, _("filesystem label"),