Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* Key to pathname encoder
0003  *
0004  * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #include <linux/slab.h>
0009 #include "internal.h"
0010 
0011 static const char cachefiles_charmap[64] =
0012     "0123456789"            /* 0 - 9 */
0013     "abcdefghijklmnopqrstuvwxyz"    /* 10 - 35 */
0014     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"    /* 36 - 61 */
0015     "_-"                /* 62 - 63 */
0016     ;
0017 
0018 static const char cachefiles_filecharmap[256] = {
0019     /* we skip space and tab and control chars */
0020     [33 ... 46] = 1,        /* '!' -> '.' */
0021     /* we skip '/' as it's significant to pathwalk */
0022     [48 ... 127] = 1,       /* '0' -> '~' */
0023 };
0024 
0025 static inline unsigned int how_many_hex_digits(unsigned int x)
0026 {
0027     return x ? round_up(ilog2(x) + 1, 4) / 4 : 0;
0028 }
0029 
0030 /*
0031  * turn the raw key into something cooked
0032  * - the key may be up to NAME_MAX in length (including the length word)
0033  *   - "base64" encode the strange keys, mapping 3 bytes of raw to four of
0034  *     cooked
0035  *   - need to cut the cooked key into 252 char lengths (189 raw bytes)
0036  */
0037 bool cachefiles_cook_key(struct cachefiles_object *object)
0038 {
0039     const u8 *key = fscache_get_key(object->cookie), *kend;
0040     unsigned char ch;
0041     unsigned int acc, i, n, nle, nbe, keylen = object->cookie->key_len;
0042     unsigned int b64len, len, print, pad;
0043     char *name, sep;
0044 
0045     _enter(",%u,%*phN", keylen, keylen, key);
0046 
0047     BUG_ON(keylen > NAME_MAX - 3);
0048 
0049     print = 1;
0050     for (i = 0; i < keylen; i++) {
0051         ch = key[i];
0052         print &= cachefiles_filecharmap[ch];
0053     }
0054 
0055     /* If the path is usable ASCII, then we render it directly */
0056     if (print) {
0057         len = 1 + keylen;
0058         name = kmalloc(len + 1, GFP_KERNEL);
0059         if (!name)
0060             return false;
0061 
0062         name[0] = 'D'; /* Data object type, string encoding */
0063         memcpy(name + 1, key, keylen);
0064         goto success;
0065     }
0066 
0067     /* See if it makes sense to encode it as "hex,hex,hex" for each 32-bit
0068      * chunk.  We rely on the key having been padded out to a whole number
0069      * of 32-bit words.
0070      */
0071     n = round_up(keylen, 4);
0072     nbe = nle = 0;
0073     for (i = 0; i < n; i += 4) {
0074         u32 be = be32_to_cpu(*(__be32 *)(key + i));
0075         u32 le = le32_to_cpu(*(__le32 *)(key + i));
0076 
0077         nbe += 1 + how_many_hex_digits(be);
0078         nle += 1 + how_many_hex_digits(le);
0079     }
0080 
0081     b64len = DIV_ROUND_UP(keylen, 3);
0082     pad = b64len * 3 - keylen;
0083     b64len = 2 + b64len * 4; /* Length if we base64-encode it */
0084     _debug("len=%u nbe=%u nle=%u b64=%u", keylen, nbe, nle, b64len);
0085     if (nbe < b64len || nle < b64len) {
0086         unsigned int nlen = min(nbe, nle) + 1;
0087         name = kmalloc(nlen, GFP_KERNEL);
0088         if (!name)
0089             return false;
0090         sep = (nbe <= nle) ? 'S' : 'T'; /* Encoding indicator */
0091         len = 0;
0092         for (i = 0; i < n; i += 4) {
0093             u32 x;
0094             if (nbe <= nle)
0095                 x = be32_to_cpu(*(__be32 *)(key + i));
0096             else
0097                 x = le32_to_cpu(*(__le32 *)(key + i));
0098             name[len++] = sep;
0099             if (x != 0)
0100                 len += snprintf(name + len, nlen - len, "%x", x);
0101             sep = ',';
0102         }
0103         goto success;
0104     }
0105 
0106     /* We need to base64-encode it */
0107     name = kmalloc(b64len + 1, GFP_KERNEL);
0108     if (!name)
0109         return false;
0110 
0111     name[0] = 'E';
0112     name[1] = '0' + pad;
0113     len = 2;
0114     kend = key + keylen;
0115     do {
0116         acc  = *key++;
0117         if (key < kend) {
0118             acc |= *key++ << 8;
0119             if (key < kend)
0120                 acc |= *key++ << 16;
0121         }
0122 
0123         name[len++] = cachefiles_charmap[acc & 63];
0124         acc >>= 6;
0125         name[len++] = cachefiles_charmap[acc & 63];
0126         acc >>= 6;
0127         name[len++] = cachefiles_charmap[acc & 63];
0128         acc >>= 6;
0129         name[len++] = cachefiles_charmap[acc & 63];
0130     } while (key < kend);
0131 
0132 success:
0133     name[len] = 0;
0134     object->d_name = name;
0135     object->d_name_len = len;
0136     _leave(" = %s", object->d_name);
0137     return true;
0138 }