Blame SOURCES/sqlite-3.26.0-CVE-2020-13631.patch

11c328
Subject: [PATCH] Do not allow a virtual table to be renamed into the name of
11c328
 one of its shadows.
11c328
11c328
---
11c328
 src/alter.c     |  5 ++++-
11c328
 src/build.c     | 29 +++++++++++++++++++++++------
11c328
 src/sqliteInt.h |  5 +++++
11c328
 3 files changed, 32 insertions(+), 7 deletions(-)
11c328
11c328
diff --git a/src/alter.c b/src/alter.c
11c328
index 1280e90..0fa24c0 100644
11c328
--- a/src/alter.c
11c328
+++ b/src/alter.c
11c328
@@ -117,7 +117,10 @@ void sqlite3AlterRenameTable(
11c328
   /* Check that a table or index named 'zName' does not already exist
11c328
   ** in database iDb. If so, this is an error.
11c328
   */
11c328
-  if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
11c328
+  if( sqlite3FindTable(db, zName, zDb)
11c328
+   || sqlite3FindIndex(db, zName, zDb)
11c328
+   || sqlite3IsShadowTableOf(db, pTab, zName)
11c328
+  ){
11c328
     sqlite3ErrorMsg(pParse, 
11c328
         "there is already another table or index with this name: %s", zName);
11c328
     goto exit_rename_table;
11c328
diff --git a/src/build.c b/src/build.c
11c328
index e0fed8a..afe4171 100644
11c328
--- a/src/build.c
11c328
+++ b/src/build.c
11c328
@@ -1899,6 +1899,28 @@ static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
11c328
   recomputeColumnsNotIndexed(pPk);
11c328
 }
11c328
 
11c328
+
11c328
+#ifndef SQLITE_OMIT_VIRTUALTABLE
11c328
+/*
11c328
+** Return true if pTab is a virtual table and zName is a shadow table name
11c328
+** for that virtual table.
11c328
+*/
11c328
+int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){
11c328
+  int nName;                    /* Length of zName */
11c328
+  Module *pMod;                 /* Module for the virtual table */
11c328
+
11c328
+  if( !IsVirtual(pTab) ) return 0;
11c328
+  nName = sqlite3Strlen30(pTab->zName);
11c328
+  if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
11c328
+  if( zName[nName]!='_' ) return 0;
11c328
+  pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]);
11c328
+  if( pMod==0 ) return 0;
11c328
+  if( pMod->pModule->iVersion<3 ) return 0;
11c328
+  if( pMod->pModule->xShadowName==0 ) return 0;
11c328
+  return pMod->pModule->xShadowName(zName+nName+1);
11c328
+}
11c328
+#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
11c328
+
11c328
 #ifndef SQLITE_OMIT_VIRTUALTABLE
11c328
 /*
11c328
 ** Return true if zName is a shadow table name in the current database
11c328
@@ -1910,7 +1932,6 @@ static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
11c328
 static int isShadowTableName(sqlite3 *db, char *zName){
11c328
   char *zTail;                  /* Pointer to the last "_" in zName */
11c328
   Table *pTab;                  /* Table that zName is a shadow of */
11c328
-  Module *pMod;                 /* Module for the virtual table */
11c328
 
11c328
   zTail = strrchr(zName, '_');
11c328
   if( zTail==0 ) return 0;
11c328
@@ -1919,11 +1940,7 @@ static int isShadowTableName(sqlite3 *db, char *zName){
11c328
   *zTail = '_';
11c328
   if( pTab==0 ) return 0;
11c328
   if( !IsVirtual(pTab) ) return 0;
11c328
-  pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]);
11c328
-  if( pMod==0 ) return 0;
11c328
-  if( pMod->pModule->iVersion<3 ) return 0;
11c328
-  if( pMod->pModule->xShadowName==0 ) return 0;
11c328
-  return pMod->pModule->xShadowName(zTail+1);
11c328
+  return sqlite3IsShadowTableOf(db, pTab, zName);
11c328
 }
11c328
 #else
11c328
 # define isShadowTableName(x,y) 0
11c328
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
11c328
index b7d3571..76337f7 100644
11c328
--- a/src/sqliteInt.h
11c328
+++ b/src/sqliteInt.h
11c328
@@ -4407,6 +4407,11 @@ void sqlite3AutoLoadExtensions(sqlite3*);
11c328
    );
11c328
 #  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
11c328
 #endif
11c328
+#ifndef SQLITE_OMIT_VIRTUALTABLE
11c328
+  int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*);
11c328
+#else
11c328
+# define sqlite3IsShadowTableOf(A,B,C) 0
11c328
+#endif
11c328
 int sqlite3VtabEponymousTableInit(Parse*,Module*);
11c328
 void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
11c328
 void sqlite3VtabMakeWritable(Parse*,Table*);
11c328
-- 
11c328
2.24.1
11c328