mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs.git
synced 2026-10-28 12:53:14 +01:00
generic_ci_d_hash() and generic_ci_d_compare() read sb->s_encoding and
dereference the unicode map while a pathwalk in rcu mode holds nothing
but rcu_read_lock(). The filesystems that use them, ext4, f2fs and
tmpfs, drop the map with utf8_unload() from ->put_super(), which runs
when the last reference to the mount goes away. A lookup that is still
inside such a filesystem at that point reads a freed map.
With a pathwalk held up in utf8_casefold_hash() while the last reference
to a casefolded tmpfs goes away:
BUG: KASAN: slab-use-after-free in utf8nlookup+0x74c/0x950
Read of size 8 at addr ff110001030fe658 by task walker/98
Call Trace:
<TASK>
utf8nlookup+0x74c/0x950
utf8byte+0x160/0x1560
utf8_casefold_hash+0x167/0x230
generic_ci_d_hash+0xdd/0x1c0
link_path_walk+0x11ad/0x1d30
path_lookupat+0x63/0xb00
filename_lookup+0x1f1/0x5b0
vfs_statx+0xd7/0x3b0
[...]
Allocated by task 94:
__kmalloc_cache_noprof+0x249/0x560
utf8_load+0x4e/0x570
shmem_parse_opt_casefold.isra.0+0x92/0x270
[...]
Freed by task 97:
kfree+0x1db/0x5b0
shmem_put_super+0x72/0x170
generic_shutdown_super+0x14c/0x530
kill_anon_super+0x3a/0x60
deactivate_locked_super+0x9c/0xf0
cleanup_mnt+0x9f/0x130
So far that has been hard to hit. umount(2) waits for a grace period
in namespace_unlock() before it drops the mount, so only a mount whose
last reference goes away elsewhere, like the close of a file or a chdir
out of a lazily unmounted directory, can race a walker. So it is
reachable.
Delay the free until a grace period has passed. The map and the data
table it holds a reference to then outlive any pathwalk that found the
superblock before the mount went away. The reference on the data table
module is dropped from the rcu callback as well. utf8-core is built in
whenever CONFIG_UNICODE is set, so there is no module exit that would
have to wait for the callbacks.
Overlayfs points the s_encoding of an overlay at the map of its first
layer without taking a reference of its own. That stays correct. The
layer is held by the overlay, so its map is dropped only after the
overlay is gone.
Fixes: b886ee3e77 ("ext4: Support case-insensitive file name lookups")
Cc: [email protected]
Link: https://patch.msgid.link/[email protected]
Signed-off-by: Christian Brauner (Amutable) <[email protected]>
236 lines
5.0 KiB
C
236 lines
5.0 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
#include <linux/module.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/string.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/rcupdate.h>
|
|
#include <linux/parser.h>
|
|
#include <linux/errno.h>
|
|
#include <linux/stringhash.h>
|
|
|
|
#include "utf8n.h"
|
|
|
|
int utf8_validate(const struct unicode_map *um, const struct qstr *str)
|
|
{
|
|
if (utf8nlen(um, UTF8_NFDI, str->name, str->len) < 0)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(utf8_validate);
|
|
|
|
int utf8_strncmp(const struct unicode_map *um,
|
|
const struct qstr *s1, const struct qstr *s2)
|
|
{
|
|
struct utf8cursor cur1, cur2;
|
|
int c1, c2;
|
|
|
|
if (utf8ncursor(&cur1, um, UTF8_NFDI, s1->name, s1->len) < 0)
|
|
return -EINVAL;
|
|
|
|
if (utf8ncursor(&cur2, um, UTF8_NFDI, s2->name, s2->len) < 0)
|
|
return -EINVAL;
|
|
|
|
do {
|
|
c1 = utf8byte(&cur1);
|
|
c2 = utf8byte(&cur2);
|
|
|
|
if (c1 < 0 || c2 < 0)
|
|
return -EINVAL;
|
|
if (c1 != c2)
|
|
return 1;
|
|
} while (c1);
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(utf8_strncmp);
|
|
|
|
int utf8_strncasecmp(const struct unicode_map *um,
|
|
const struct qstr *s1, const struct qstr *s2)
|
|
{
|
|
struct utf8cursor cur1, cur2;
|
|
int c1, c2;
|
|
|
|
if (utf8ncursor(&cur1, um, UTF8_NFDICF, s1->name, s1->len) < 0)
|
|
return -EINVAL;
|
|
|
|
if (utf8ncursor(&cur2, um, UTF8_NFDICF, s2->name, s2->len) < 0)
|
|
return -EINVAL;
|
|
|
|
do {
|
|
c1 = utf8byte(&cur1);
|
|
c2 = utf8byte(&cur2);
|
|
|
|
if (c1 < 0 || c2 < 0)
|
|
return -EINVAL;
|
|
if (c1 != c2)
|
|
return 1;
|
|
} while (c1);
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(utf8_strncasecmp);
|
|
|
|
/* String cf is expected to be a valid UTF-8 casefolded
|
|
* string.
|
|
*/
|
|
int utf8_strncasecmp_folded(const struct unicode_map *um,
|
|
const struct qstr *cf,
|
|
const struct qstr *s1)
|
|
{
|
|
struct utf8cursor cur1;
|
|
int c1, c2;
|
|
int i = 0;
|
|
|
|
if (utf8ncursor(&cur1, um, UTF8_NFDICF, s1->name, s1->len) < 0)
|
|
return -EINVAL;
|
|
|
|
do {
|
|
c1 = utf8byte(&cur1);
|
|
c2 = cf->name[i++];
|
|
if (c1 < 0)
|
|
return -EINVAL;
|
|
if (c1 != c2)
|
|
return 1;
|
|
} while (c1);
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(utf8_strncasecmp_folded);
|
|
|
|
int utf8_casefold(const struct unicode_map *um, const struct qstr *str,
|
|
unsigned char *dest, size_t dlen)
|
|
{
|
|
struct utf8cursor cur;
|
|
size_t nlen = 0;
|
|
|
|
if (utf8ncursor(&cur, um, UTF8_NFDICF, str->name, str->len) < 0)
|
|
return -EINVAL;
|
|
|
|
for (nlen = 0; nlen < dlen; nlen++) {
|
|
int c = utf8byte(&cur);
|
|
|
|
dest[nlen] = c;
|
|
if (!c)
|
|
return nlen;
|
|
if (c == -1)
|
|
break;
|
|
}
|
|
return -EINVAL;
|
|
}
|
|
EXPORT_SYMBOL(utf8_casefold);
|
|
|
|
int utf8_casefold_hash(const struct unicode_map *um, const void *salt,
|
|
struct qstr *str)
|
|
{
|
|
struct utf8cursor cur;
|
|
int c;
|
|
unsigned long hash = init_name_hash(salt);
|
|
|
|
if (utf8ncursor(&cur, um, UTF8_NFDICF, str->name, str->len) < 0)
|
|
return -EINVAL;
|
|
|
|
while ((c = utf8byte(&cur))) {
|
|
if (c < 0)
|
|
return -EINVAL;
|
|
hash = partial_name_hash((unsigned char)c, hash);
|
|
}
|
|
str->hash = end_name_hash(hash);
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(utf8_casefold_hash);
|
|
|
|
static const struct utf8data *find_table_version(const struct utf8data *table,
|
|
size_t nr_entries, unsigned int version)
|
|
{
|
|
size_t i = nr_entries - 1;
|
|
|
|
while (version < table[i].maxage)
|
|
i--;
|
|
if (version > table[i].maxage)
|
|
return NULL;
|
|
return &table[i];
|
|
}
|
|
|
|
struct unicode_map *utf8_load(unsigned int version)
|
|
{
|
|
struct unicode_map *um;
|
|
|
|
um = kzalloc_obj(struct unicode_map);
|
|
if (!um)
|
|
return ERR_PTR(-ENOMEM);
|
|
um->version = version;
|
|
|
|
um->tables = symbol_request(utf8_data_table);
|
|
if (!um->tables)
|
|
goto out_free_um;
|
|
|
|
if (!utf8version_is_supported(um, version))
|
|
goto out_symbol_put;
|
|
um->ntab[UTF8_NFDI] = find_table_version(um->tables->utf8nfdidata,
|
|
um->tables->utf8nfdidata_size, um->version);
|
|
if (!um->ntab[UTF8_NFDI])
|
|
goto out_symbol_put;
|
|
um->ntab[UTF8_NFDICF] = find_table_version(um->tables->utf8nfdicfdata,
|
|
um->tables->utf8nfdicfdata_size, um->version);
|
|
if (!um->ntab[UTF8_NFDICF])
|
|
goto out_symbol_put;
|
|
return um;
|
|
|
|
out_symbol_put:
|
|
symbol_put(utf8_data_table);
|
|
out_free_um:
|
|
kfree(um);
|
|
return ERR_PTR(-EINVAL);
|
|
}
|
|
EXPORT_SYMBOL(utf8_load);
|
|
|
|
static void utf8_unload_rcu(struct rcu_head *head)
|
|
{
|
|
struct unicode_map *um = container_of(head, struct unicode_map, rcu);
|
|
|
|
symbol_put(utf8_data_table);
|
|
kfree(um);
|
|
}
|
|
|
|
/*
|
|
* A pathwalk in rcu mode may still be in ->d_hash() or ->d_compare() of a
|
|
* filesystem that is being torn down and read the map through its
|
|
* superblock, so the map and the tables it points to have to outlive the
|
|
* grace period.
|
|
*/
|
|
void utf8_unload(struct unicode_map *um)
|
|
{
|
|
if (um)
|
|
call_rcu(&um->rcu, utf8_unload_rcu);
|
|
}
|
|
EXPORT_SYMBOL(utf8_unload);
|
|
|
|
/**
|
|
* utf8_parse_version - Parse a UTF-8 version number from a string
|
|
*
|
|
* @version: input string
|
|
*
|
|
* Returns the parsed version on success, negative code on error
|
|
*/
|
|
int utf8_parse_version(char *version)
|
|
{
|
|
substring_t args[3];
|
|
unsigned int maj, min, rev;
|
|
static const struct match_token token[] = {
|
|
{1, "%u.%u.%u"},
|
|
{0, NULL}
|
|
};
|
|
|
|
if (match_token(version, token, args) != 1)
|
|
return -EINVAL;
|
|
|
|
if (match_uint(&args[0], &maj) || match_uint(&args[1], &min) ||
|
|
match_uint(&args[2], &rev))
|
|
return -EINVAL;
|
|
|
|
if (maj > U8_MAX || min > U8_MAX || rev > U8_MAX)
|
|
return -EINVAL;
|
|
return UNICODE_AGE(maj, min, rev);
|
|
}
|
|
EXPORT_SYMBOL(utf8_parse_version);
|