Fossil

Check-in [4ed1a294ff]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added checks of (-wal, -shm, -journal) db suffixes.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | reject-ckout-db
Files: files | file ages | folders
SHA3-256: 4ed1a294ff5b90e210b1d8eb98cb0f7919b9bc43241d18a0a8c8a2749afd5f57
User & Date: stephan 2020-08-17 17:34:03.411
References
2020-08-17
19:46 Closed ticket [980a72dedd]: RCE using a fake _FOSSIL_ file in a repository plus 4 other changes artifact: ef08ac1ee6 user: stephan
Context
2020-08-17
17:50
Fixed [17d00c20dd9f] by adding NULL check on F- and E-card UUID tokens. Closed-Leaf check-in: 458f30fc0b user: stephan tags: reject-ckout-db
17:34
Added checks of (-wal, -shm, -journal) db suffixes. check-in: 4ed1a294ff user: stephan tags: reject-ckout-db
16:10
Moved is_fossil_ckout_db_name() from db.c to file.c and renamed it filename_is_ckout_db(). Integrated the check into manifest_parse(), but testing it requires temporarily #if'ing out the Z-card check, which is one of the first validations. check-in: 6c19baa09b user: stephan tags: reject-ckout-db
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/file.c.
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432

2433
2434
2435
2436
2437
























2438
2439
2440
2441


2442
2443
2444
2445
2446
2447


2448
2449
2450

2451
2452
2453
2454
2455
2456
2457
**
** zFilename must, for efficiency's sake, be a
** canonicalized/normalized name, e.g. using only '/' as directory
** separators.
**
** nFilename must be the strlen of zFilename. If it is negative,
** strlen() is used to calculate it.
**
** TODO: https://fossil-scm.org/sec2020/info/972cf9c302f5413f
** TL;DR: check for the -wal, -shm, -journal suffix forms of the db
** names.
*/
int filename_is_ckout_db(const char *zFilename, int nFilename){
  const char *zEnd;


  if(nFilename>=0 && nFilename<8/*strlen _FOSSIL_*/) return 0;
  else if(nFilename<0) nFilename = (int)strlen(zFilename);
  if(nFilename<8) return 0;
  zEnd = zFilename + nFilename;
























  switch(zEnd[-1]){
    case '_': {
      return fossil_strcmp("_FOSSIL_", &zEnd[-8])
        ? 0 : (8==nFilename ? 1 : ('/'==zEnd[-9] ? 2 : 0));


    }
    case 't': {
      return (nFilename<9
              || '.'!=zEnd[-9]
              || fossil_strcmp(".fslckout", &zEnd[-9]))
        ? 0 : (9==nFilename ? 1 : ('/'==zEnd[-10] ? 2 : 0));


    }
    default:
      return 0;

  }
}

/*
** COMMAND: test-is-ckout-db
**
** Usage: %fossil test-is-ckout-db FILENAMES...







<
<
<
<


|
>

|
|
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


|
|
>
>




|
|
>
>

|

>







2419
2420
2421
2422
2423
2424
2425




2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
**
** zFilename must, for efficiency's sake, be a
** canonicalized/normalized name, e.g. using only '/' as directory
** separators.
**
** nFilename must be the strlen of zFilename. If it is negative,
** strlen() is used to calculate it.




*/
int filename_is_ckout_db(const char *zFilename, int nFilename){
  const char *zEnd;  /* one-after-the-end of zFilename */
  int gotSuffix = 0; /* length of suffix (-wal, -shm, -journal) */

  assert(zFilename && "API misuse");
  if(nFilename<0) nFilename = (int)strlen(zFilename);
  if(nFilename<8/*strlen _FOSSIL_*/) return 0;
  zEnd = zFilename + nFilename;
  if(nFilename>=12/*strlen _FOSSIL_-(shm|wal)*/){
    /* Check for (-wal, -shm, -journal) suffixes, with an eye towards
    ** runtime speed. */
    if('-'==zEnd[-4]){
      if(fossil_stricmp("wal", &zEnd[-3])
         && fossil_stricmp("shm", &zEnd[-3])){
        return 0;
      }
      gotSuffix = 4;
    }else if(nFilename>=16/*strlen _FOSSIL_-journal*/ && '-'==zEnd[-8]){
      if(fossil_stricmp("journal",&zEnd[-7])){
        return 0;
      }
      gotSuffix = 8;
    }
    if(gotSuffix){
      assert(4==gotSuffix || 8==gotSuffix);
      zEnd -= gotSuffix;
      nFilename -= gotSuffix;
      gotSuffix = 1;
    }
    assert(nFilename>=8 && "strlen _FOSSIL_");
    assert(gotSuffix==0 || gotSuffix==1);
  }
  switch(zEnd[-1]){
    case '_': {
      return fossil_strnicmp("_FOSSIL_", &zEnd[-8], 8)
        ? 0 : (8==nFilename
               ? 1
               : ('/'==zEnd[-9] ? 2 : gotSuffix));
    }
    case 't': {
      return (nFilename<9
              || '.'!=zEnd[-9]
              || fossil_strnicmp(".fslckout", &zEnd[-9], 9))
        ? 0 : (9==nFilename
               ? 1
               : ('/'==zEnd[-10] ? 2 : gotSuffix));
    }
    default: {
      return 0;
    }
  }
}

/*
** COMMAND: test-is-ckout-db
**
** Usage: %fossil test-is-ckout-db FILENAMES...