mirror of
				https://github.com/torvalds/linux.git
				synced 2025-10-31 16:48:26 +02:00 
			
		
		
		
	 6d5b940e1e
			
		
	
	
		6d5b940e1e
		
	
	
	
	
		
			
			-----BEGIN PGP SIGNATURE----- iHUEABYKAB0WIQRAhzRXHqcMeLMyaSiRxhvAZXjcogUCaDBN6wAKCRCRxhvAZXjc ok32AQD9DTiSCAoVg+7s+gSBuLTi8drPTN++mCaxdTqRh5WpRAD9GVyrGQT0s6LH eo9bm8d1TAYjilEWM0c0K0TxyQ7KcAA= =IW7H -----END PGP SIGNATURE----- Merge tag 'vfs-6.16-rc1.async.dir' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs Pull vfs directory lookup updates from Christian Brauner: "This contains cleanups for the lookup_one*() family of helpers. We expose a set of functions with names containing "lookup_one_len" and others without the "_len". This difference has nothing to do with "len". It's rater a historical accident that can be confusing. The functions without "_len" take a "mnt_idmap" pointer. This is found in the "vfsmount" and that is an important question when choosing which to use: do you have a vfsmount, or are you "inside" the filesystem. A related question is "is permission checking relevant here?". nfsd and cachefiles *do* have a vfsmount but *don't* use the non-_len functions. They pass nop_mnt_idmap and refuse to work on filesystems which have any other idmap. This work changes nfsd and cachefile to use the lookup_one family of functions and to explictily pass &nop_mnt_idmap which is consistent with all other vfs interfaces used where &nop_mnt_idmap is explicitly passed. The remaining uses of the "_one" functions do not require permission checks so these are renamed to be "_noperm" and the permission checking is removed. This series also changes these lookup function to take a qstr instead of separate name and len. In many cases this simplifies the call" * tag 'vfs-6.16-rc1.async.dir' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs: VFS: change lookup_one_common and lookup_noperm_common to take a qstr Use try_lookup_noperm() instead of d_hash_and_lookup() outside of VFS VFS: rename lookup_one_len family to lookup_noperm and remove permission check cachefiles: Use lookup_one() rather than lookup_one_len() nfsd: Use lookup_one() rather than lookup_one_len() VFS: improve interface for lookup_one functions
		
			
				
	
	
		
			137 lines
		
	
	
	
		
			3.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			137 lines
		
	
	
	
		
			3.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| // SPDX-License-Identifier: GPL-2.0-or-later
 | |
| /* Key to pathname encoder
 | |
|  *
 | |
|  * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
 | |
|  * Written by David Howells (dhowells@redhat.com)
 | |
|  */
 | |
| 
 | |
| #include <linux/slab.h>
 | |
| #include "internal.h"
 | |
| 
 | |
| static const char cachefiles_charmap[64] __nonstring =
 | |
| 	"0123456789"			/* 0 - 9 */
 | |
| 	"abcdefghijklmnopqrstuvwxyz"	/* 10 - 35 */
 | |
| 	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"	/* 36 - 61 */
 | |
| 	"_-"				/* 62 - 63 */
 | |
| 	;
 | |
| 
 | |
| static const char cachefiles_filecharmap[256] = {
 | |
| 	/* we skip space and tab and control chars */
 | |
| 	[33 ... 46] = 1,		/* '!' -> '.' */
 | |
| 	/* we skip '/' as it's significant to pathwalk */
 | |
| 	[48 ... 127] = 1,		/* '0' -> '~' */
 | |
| };
 | |
| 
 | |
| static inline unsigned int how_many_hex_digits(unsigned int x)
 | |
| {
 | |
| 	return x ? round_up(ilog2(x) + 1, 4) / 4 : 0;
 | |
| }
 | |
| 
 | |
| /*
 | |
|  * turn the raw key into something cooked
 | |
|  * - the key may be up to NAME_MAX in length (including the length word)
 | |
|  *   - "base64" encode the strange keys, mapping 3 bytes of raw to four of
 | |
|  *     cooked
 | |
|  *   - need to cut the cooked key into 252 char lengths (189 raw bytes)
 | |
|  */
 | |
| bool cachefiles_cook_key(struct cachefiles_object *object)
 | |
| {
 | |
| 	const u8 *key = fscache_get_key(object->cookie), *kend;
 | |
| 	unsigned char ch;
 | |
| 	unsigned int acc, i, n, nle, nbe, keylen = object->cookie->key_len;
 | |
| 	unsigned int b64len, len, print, pad;
 | |
| 	char *name, sep;
 | |
| 
 | |
| 	_enter(",%u,%*phN", keylen, keylen, key);
 | |
| 
 | |
| 	BUG_ON(keylen > NAME_MAX - 3);
 | |
| 
 | |
| 	print = 1;
 | |
| 	for (i = 0; i < keylen; i++) {
 | |
| 		ch = key[i];
 | |
| 		print &= cachefiles_filecharmap[ch];
 | |
| 	}
 | |
| 
 | |
| 	/* If the path is usable ASCII, then we render it directly */
 | |
| 	if (print) {
 | |
| 		len = 1 + keylen;
 | |
| 		name = kmalloc(len + 1, GFP_KERNEL);
 | |
| 		if (!name)
 | |
| 			return false;
 | |
| 
 | |
| 		name[0] = 'D'; /* Data object type, string encoding */
 | |
| 		memcpy(name + 1, key, keylen);
 | |
| 		goto success;
 | |
| 	}
 | |
| 
 | |
| 	/* See if it makes sense to encode it as "hex,hex,hex" for each 32-bit
 | |
| 	 * chunk.  We rely on the key having been padded out to a whole number
 | |
| 	 * of 32-bit words.
 | |
| 	 */
 | |
| 	n = round_up(keylen, 4);
 | |
| 	nbe = nle = 0;
 | |
| 	for (i = 0; i < n; i += 4) {
 | |
| 		u32 be = be32_to_cpu(*(__be32 *)(key + i));
 | |
| 		u32 le = le32_to_cpu(*(__le32 *)(key + i));
 | |
| 
 | |
| 		nbe += 1 + how_many_hex_digits(be);
 | |
| 		nle += 1 + how_many_hex_digits(le);
 | |
| 	}
 | |
| 
 | |
| 	b64len = DIV_ROUND_UP(keylen, 3);
 | |
| 	pad = b64len * 3 - keylen;
 | |
| 	b64len = 2 + b64len * 4; /* Length if we base64-encode it */
 | |
| 	_debug("len=%u nbe=%u nle=%u b64=%u", keylen, nbe, nle, b64len);
 | |
| 	if (nbe < b64len || nle < b64len) {
 | |
| 		unsigned int nlen = min(nbe, nle) + 1;
 | |
| 		name = kmalloc(nlen, GFP_KERNEL);
 | |
| 		if (!name)
 | |
| 			return false;
 | |
| 		sep = (nbe <= nle) ? 'S' : 'T'; /* Encoding indicator */
 | |
| 		len = 0;
 | |
| 		for (i = 0; i < n; i += 4) {
 | |
| 			u32 x;
 | |
| 			if (nbe <= nle)
 | |
| 				x = be32_to_cpu(*(__be32 *)(key + i));
 | |
| 			else
 | |
| 				x = le32_to_cpu(*(__le32 *)(key + i));
 | |
| 			name[len++] = sep;
 | |
| 			if (x != 0)
 | |
| 				len += snprintf(name + len, nlen - len, "%x", x);
 | |
| 			sep = ',';
 | |
| 		}
 | |
| 		goto success;
 | |
| 	}
 | |
| 
 | |
| 	/* We need to base64-encode it */
 | |
| 	name = kmalloc(b64len + 1, GFP_KERNEL);
 | |
| 	if (!name)
 | |
| 		return false;
 | |
| 
 | |
| 	name[0] = 'E';
 | |
| 	name[1] = '0' + pad;
 | |
| 	len = 2;
 | |
| 	kend = key + keylen;
 | |
| 	do {
 | |
| 		acc  = *key++;
 | |
| 		if (key < kend) {
 | |
| 			acc |= *key++ << 8;
 | |
| 			if (key < kend)
 | |
| 				acc |= *key++ << 16;
 | |
| 		}
 | |
| 
 | |
| 		name[len++] = cachefiles_charmap[acc & 63];
 | |
| 		acc >>= 6;
 | |
| 		name[len++] = cachefiles_charmap[acc & 63];
 | |
| 		acc >>= 6;
 | |
| 		name[len++] = cachefiles_charmap[acc & 63];
 | |
| 		acc >>= 6;
 | |
| 		name[len++] = cachefiles_charmap[acc & 63];
 | |
| 	} while (key < kend);
 | |
| 
 | |
| success:
 | |
| 	name[len] = 0;
 | |
| 	object->d_name = name;
 | |
| 	_leave(" = %s", object->d_name);
 | |
| 	return true;
 | |
| }
 |