1*5d439467SDavid Howells // SPDX-License-Identifier: GPL-2.0-or-later
2*5d439467SDavid Howells /* Key to pathname encoder
3*5d439467SDavid Howells *
4*5d439467SDavid Howells * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5*5d439467SDavid Howells * Written by David Howells (dhowells@redhat.com)
6*5d439467SDavid Howells */
7*5d439467SDavid Howells
8*5d439467SDavid Howells #include <linux/slab.h>
9*5d439467SDavid Howells #include "internal.h"
10*5d439467SDavid Howells
11*5d439467SDavid Howells static const char cachefiles_charmap[64] =
12*5d439467SDavid Howells "0123456789" /* 0 - 9 */
13*5d439467SDavid Howells "abcdefghijklmnopqrstuvwxyz" /* 10 - 35 */
14*5d439467SDavid Howells "ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* 36 - 61 */
15*5d439467SDavid Howells "_-" /* 62 - 63 */
16*5d439467SDavid Howells ;
17*5d439467SDavid Howells
18*5d439467SDavid Howells static const char cachefiles_filecharmap[256] = {
19*5d439467SDavid Howells /* we skip space and tab and control chars */
20*5d439467SDavid Howells [33 ... 46] = 1, /* '!' -> '.' */
21*5d439467SDavid Howells /* we skip '/' as it's significant to pathwalk */
22*5d439467SDavid Howells [48 ... 127] = 1, /* '0' -> '~' */
23*5d439467SDavid Howells };
24*5d439467SDavid Howells
how_many_hex_digits(unsigned int x)25*5d439467SDavid Howells static inline unsigned int how_many_hex_digits(unsigned int x)
26*5d439467SDavid Howells {
27*5d439467SDavid Howells return x ? round_up(ilog2(x) + 1, 4) / 4 : 0;
28*5d439467SDavid Howells }
29*5d439467SDavid Howells
30*5d439467SDavid Howells /*
31*5d439467SDavid Howells * turn the raw key into something cooked
32*5d439467SDavid Howells * - the key may be up to NAME_MAX in length (including the length word)
33*5d439467SDavid Howells * - "base64" encode the strange keys, mapping 3 bytes of raw to four of
34*5d439467SDavid Howells * cooked
35*5d439467SDavid Howells * - need to cut the cooked key into 252 char lengths (189 raw bytes)
36*5d439467SDavid Howells */
cachefiles_cook_key(struct cachefiles_object * object)37*5d439467SDavid Howells bool cachefiles_cook_key(struct cachefiles_object *object)
38*5d439467SDavid Howells {
39*5d439467SDavid Howells const u8 *key = fscache_get_key(object->cookie), *kend;
40*5d439467SDavid Howells unsigned char ch;
41*5d439467SDavid Howells unsigned int acc, i, n, nle, nbe, keylen = object->cookie->key_len;
42*5d439467SDavid Howells unsigned int b64len, len, print, pad;
43*5d439467SDavid Howells char *name, sep;
44*5d439467SDavid Howells
45*5d439467SDavid Howells _enter(",%u,%*phN", keylen, keylen, key);
46*5d439467SDavid Howells
47*5d439467SDavid Howells BUG_ON(keylen > NAME_MAX - 3);
48*5d439467SDavid Howells
49*5d439467SDavid Howells print = 1;
50*5d439467SDavid Howells for (i = 0; i < keylen; i++) {
51*5d439467SDavid Howells ch = key[i];
52*5d439467SDavid Howells print &= cachefiles_filecharmap[ch];
53*5d439467SDavid Howells }
54*5d439467SDavid Howells
55*5d439467SDavid Howells /* If the path is usable ASCII, then we render it directly */
56*5d439467SDavid Howells if (print) {
57*5d439467SDavid Howells len = 1 + keylen;
58*5d439467SDavid Howells name = kmalloc(len + 1, GFP_KERNEL);
59*5d439467SDavid Howells if (!name)
60*5d439467SDavid Howells return false;
61*5d439467SDavid Howells
62*5d439467SDavid Howells name[0] = 'D'; /* Data object type, string encoding */
63*5d439467SDavid Howells memcpy(name + 1, key, keylen);
64*5d439467SDavid Howells goto success;
65*5d439467SDavid Howells }
66*5d439467SDavid Howells
67*5d439467SDavid Howells /* See if it makes sense to encode it as "hex,hex,hex" for each 32-bit
68*5d439467SDavid Howells * chunk. We rely on the key having been padded out to a whole number
69*5d439467SDavid Howells * of 32-bit words.
70*5d439467SDavid Howells */
71*5d439467SDavid Howells n = round_up(keylen, 4);
72*5d439467SDavid Howells nbe = nle = 0;
73*5d439467SDavid Howells for (i = 0; i < n; i += 4) {
74*5d439467SDavid Howells u32 be = be32_to_cpu(*(__be32 *)(key + i));
75*5d439467SDavid Howells u32 le = le32_to_cpu(*(__le32 *)(key + i));
76*5d439467SDavid Howells
77*5d439467SDavid Howells nbe += 1 + how_many_hex_digits(be);
78*5d439467SDavid Howells nle += 1 + how_many_hex_digits(le);
79*5d439467SDavid Howells }
80*5d439467SDavid Howells
81*5d439467SDavid Howells b64len = DIV_ROUND_UP(keylen, 3);
82*5d439467SDavid Howells pad = b64len * 3 - keylen;
83*5d439467SDavid Howells b64len = 2 + b64len * 4; /* Length if we base64-encode it */
84*5d439467SDavid Howells _debug("len=%u nbe=%u nle=%u b64=%u", keylen, nbe, nle, b64len);
85*5d439467SDavid Howells if (nbe < b64len || nle < b64len) {
86*5d439467SDavid Howells unsigned int nlen = min(nbe, nle) + 1;
87*5d439467SDavid Howells name = kmalloc(nlen, GFP_KERNEL);
88*5d439467SDavid Howells if (!name)
89*5d439467SDavid Howells return false;
90*5d439467SDavid Howells sep = (nbe <= nle) ? 'S' : 'T'; /* Encoding indicator */
91*5d439467SDavid Howells len = 0;
92*5d439467SDavid Howells for (i = 0; i < n; i += 4) {
93*5d439467SDavid Howells u32 x;
94*5d439467SDavid Howells if (nbe <= nle)
95*5d439467SDavid Howells x = be32_to_cpu(*(__be32 *)(key + i));
96*5d439467SDavid Howells else
97*5d439467SDavid Howells x = le32_to_cpu(*(__le32 *)(key + i));
98*5d439467SDavid Howells name[len++] = sep;
99*5d439467SDavid Howells if (x != 0)
100*5d439467SDavid Howells len += snprintf(name + len, nlen - len, "%x", x);
101*5d439467SDavid Howells sep = ',';
102*5d439467SDavid Howells }
103*5d439467SDavid Howells goto success;
104*5d439467SDavid Howells }
105*5d439467SDavid Howells
106*5d439467SDavid Howells /* We need to base64-encode it */
107*5d439467SDavid Howells name = kmalloc(b64len + 1, GFP_KERNEL);
108*5d439467SDavid Howells if (!name)
109*5d439467SDavid Howells return false;
110*5d439467SDavid Howells
111*5d439467SDavid Howells name[0] = 'E';
112*5d439467SDavid Howells name[1] = '0' + pad;
113*5d439467SDavid Howells len = 2;
114*5d439467SDavid Howells kend = key + keylen;
115*5d439467SDavid Howells do {
116*5d439467SDavid Howells acc = *key++;
117*5d439467SDavid Howells if (key < kend) {
118*5d439467SDavid Howells acc |= *key++ << 8;
119*5d439467SDavid Howells if (key < kend)
120*5d439467SDavid Howells acc |= *key++ << 16;
121*5d439467SDavid Howells }
122*5d439467SDavid Howells
123*5d439467SDavid Howells name[len++] = cachefiles_charmap[acc & 63];
124*5d439467SDavid Howells acc >>= 6;
125*5d439467SDavid Howells name[len++] = cachefiles_charmap[acc & 63];
126*5d439467SDavid Howells acc >>= 6;
127*5d439467SDavid Howells name[len++] = cachefiles_charmap[acc & 63];
128*5d439467SDavid Howells acc >>= 6;
129*5d439467SDavid Howells name[len++] = cachefiles_charmap[acc & 63];
130*5d439467SDavid Howells } while (key < kend);
131*5d439467SDavid Howells
132*5d439467SDavid Howells success:
133*5d439467SDavid Howells name[len] = 0;
134*5d439467SDavid Howells object->d_name = name;
135*5d439467SDavid Howells object->d_name_len = len;
136*5d439467SDavid Howells _leave(" = %s", object->d_name);
137*5d439467SDavid Howells return true;
138*5d439467SDavid Howells }
139