LCOV - code coverage report
Current view: top level - fs/cachefiles - key.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 0 47 0.0 %
Date: 2020-09-30 20:25:40 Functions: 0 1 0.0 %
Branches: 0 24 0.0 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-or-later
       2                 :            : /* Key to pathname encoder
       3                 :            :  *
       4                 :            :  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
       5                 :            :  * Written by David Howells (dhowells@redhat.com)
       6                 :            :  */
       7                 :            : 
       8                 :            : #include <linux/slab.h>
       9                 :            : #include "internal.h"
      10                 :            : 
      11                 :            : static const char cachefiles_charmap[64] =
      12                 :            :         "0123456789"                  /* 0 - 9 */
      13                 :            :         "abcdefghijklmnopqrstuvwxyz"  /* 10 - 35 */
      14                 :            :         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"  /* 36 - 61 */
      15                 :            :         "_-"                          /* 62 - 63 */
      16                 :            :         ;
      17                 :            : 
      18                 :            : static const char cachefiles_filecharmap[256] = {
      19                 :            :         /* we skip space and tab and control chars */
      20                 :            :         [33 ... 46] = 1,                /* '!' -> '.' */
      21                 :            :         /* we skip '/' as it's significant to pathwalk */
      22                 :            :         [48 ... 127] = 1,               /* '0' -> '~' */
      23                 :            : };
      24                 :            : 
      25                 :            : /*
      26                 :            :  * turn the raw key into something cooked
      27                 :            :  * - the raw key should include the length in the two bytes at the front
      28                 :            :  * - the key may be up to 514 bytes in length (including the length word)
      29                 :            :  *   - "base64" encode the strange keys, mapping 3 bytes of raw to four of
      30                 :            :  *     cooked
      31                 :            :  *   - need to cut the cooked key into 252 char lengths (189 raw bytes)
      32                 :            :  */
      33                 :          0 : char *cachefiles_cook_key(const u8 *raw, int keylen, uint8_t type)
      34                 :            : {
      35                 :            :         unsigned char csum, ch;
      36                 :            :         unsigned int acc;
      37                 :            :         char *key;
      38                 :            :         int loop, len, max, seg, mark, print;
      39                 :            : 
      40                 :            :         _enter(",%d", keylen);
      41                 :            : 
      42         [ #  # ]:          0 :         BUG_ON(keylen < 2 || keylen > 514);
      43                 :            : 
      44                 :          0 :         csum = raw[0] + raw[1];
      45                 :            :         print = 1;
      46         [ #  # ]:          0 :         for (loop = 2; loop < keylen; loop++) {
      47                 :          0 :                 ch = raw[loop];
      48                 :          0 :                 csum += ch;
      49                 :          0 :                 print &= cachefiles_filecharmap[ch];
      50                 :            :         }
      51                 :            : 
      52         [ #  # ]:          0 :         if (print) {
      53                 :            :                 /* if the path is usable ASCII, then we render it directly */
      54                 :            :                 max = keylen - 2;
      55                 :            :                 max += 2;       /* two base64'd length chars on the front */
      56                 :            :                 max += 5;       /* @checksum/M */
      57                 :            :                 max += 3 * 2;   /* maximum number of segment dividers (".../M")
      58                 :            :                                  * is ((514 + 251) / 252) = 3
      59                 :            :                                  */
      60                 :          0 :                 max += 1;       /* NUL on end */
      61                 :            :         } else {
      62                 :            :                 /* calculate the maximum length of the cooked key */
      63                 :          0 :                 keylen = (keylen + 2) / 3;
      64                 :            : 
      65                 :          0 :                 max = keylen * 4;
      66                 :            :                 max += 5;       /* @checksum/M */
      67                 :            :                 max += 3 * 2;   /* maximum number of segment dividers (".../M")
      68                 :            :                                  * is ((514 + 188) / 189) = 3
      69                 :            :                                  */
      70                 :          0 :                 max += 1;       /* NUL on end */
      71                 :            :         }
      72                 :            : 
      73                 :          0 :         max += 1;       /* 2nd NUL on end */
      74                 :            : 
      75                 :            :         _debug("max: %d", max);
      76                 :            : 
      77                 :          0 :         key = kmalloc(max, cachefiles_gfp);
      78         [ #  # ]:          0 :         if (!key)
      79                 :            :                 return NULL;
      80                 :            : 
      81                 :            :         len = 0;
      82                 :            : 
      83                 :            :         /* build the cooked key */
      84                 :          0 :         sprintf(key, "@%02x%c+", (unsigned) csum, 0);
      85                 :            :         len = 5;
      86                 :            :         mark = len - 1;
      87                 :            : 
      88         [ #  # ]:          0 :         if (print) {
      89                 :          0 :                 acc = *(uint16_t *) raw;
      90                 :          0 :                 raw += 2;
      91                 :            : 
      92                 :          0 :                 key[len + 1] = cachefiles_charmap[acc & 63];
      93                 :          0 :                 acc >>= 6;
      94                 :          0 :                 key[len] = cachefiles_charmap[acc & 63];
      95                 :            :                 len += 2;
      96                 :            : 
      97                 :            :                 seg = 250;
      98         [ #  # ]:          0 :                 for (loop = keylen; loop > 0; loop--) {
      99                 :            :                         if (seg <= 0) {
     100                 :            :                                 key[len++] = '\0';
     101                 :            :                                 mark = len;
     102                 :            :                                 key[len++] = '+';
     103                 :            :                                 seg = 252;
     104                 :            :                         }
     105                 :            : 
     106                 :          0 :                         key[len++] = *raw++;
     107         [ #  # ]:          0 :                         ASSERT(len < max);
     108                 :            :                 }
     109                 :            : 
     110      [ #  #  # ]:          0 :                 switch (type) {
     111                 :            :                 case FSCACHE_COOKIE_TYPE_INDEX:         type = 'I';     break;
     112                 :          0 :                 case FSCACHE_COOKIE_TYPE_DATAFILE:      type = 'D';     break;
     113                 :          0 :                 default:                                type = 'S';     break;
     114                 :            :                 }
     115                 :            :         } else {
     116                 :            :                 seg = 252;
     117         [ #  # ]:          0 :                 for (loop = keylen; loop > 0; loop--) {
     118                 :            :                         if (seg <= 0) {
     119                 :            :                                 key[len++] = '\0';
     120                 :            :                                 mark = len;
     121                 :            :                                 key[len++] = '+';
     122                 :            :                                 seg = 252;
     123                 :            :                         }
     124                 :            : 
     125                 :          0 :                         acc = *raw++;
     126                 :          0 :                         acc |= *raw++ << 8;
     127                 :          0 :                         acc |= *raw++ << 16;
     128                 :            : 
     129                 :            :                         _debug("acc: %06x", acc);
     130                 :            : 
     131                 :          0 :                         key[len++] = cachefiles_charmap[acc & 63];
     132                 :          0 :                         acc >>= 6;
     133                 :          0 :                         key[len++] = cachefiles_charmap[acc & 63];
     134                 :          0 :                         acc >>= 6;
     135                 :          0 :                         key[len++] = cachefiles_charmap[acc & 63];
     136                 :          0 :                         acc >>= 6;
     137                 :          0 :                         key[len++] = cachefiles_charmap[acc & 63];
     138                 :            : 
     139         [ #  # ]:          0 :                         ASSERT(len < max);
     140                 :            :                 }
     141                 :            : 
     142      [ #  #  # ]:          0 :                 switch (type) {
     143                 :            :                 case FSCACHE_COOKIE_TYPE_INDEX:         type = 'J';     break;
     144                 :          0 :                 case FSCACHE_COOKIE_TYPE_DATAFILE:      type = 'E';     break;
     145                 :          0 :                 default:                                type = 'T';     break;
     146                 :            :                 }
     147                 :            :         }
     148                 :            : 
     149                 :          0 :         key[mark] = type;
     150                 :          0 :         key[len++] = 0;
     151                 :          0 :         key[len] = 0;
     152                 :            : 
     153                 :            :         _leave(" = %p %d", key, len);
     154                 :          0 :         return key;
     155                 :            : }

Generated by: LCOV version 1.14