-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 KL-001-2016-003 : SQLite Tempdir Selection Vulnerability Title: SQLite Tempdir Selection Vulnerability Advisory ID: KL-001-2016-003 Publication Date: 2016.07.01 Publication URL: https://www.korelogic.com/Resources/Advisories/KL-001-2016-003.txt 1. Vulnerability Details Affected Vendor: SQLite/Hwaci Affected Product: SQLite Affected Version: All versions prior to 3.13.0 Platform: UNIX, GNU/Linux CWE Classification: CWE-379: Creation of Temporary File in Directory with Incorrect Permissions Impact: Data Leakage Attack vector: Local 2. Vulnerability Description Usually processes writing to temporary directories do not need to perform readdir() because they control the filenames they create, so setting /tmp/ , /var/tmp/ , etc. to be mode 1733 is a not uncommon UNIX hardening practice. Affected versions of SQLite reject potential tempdir locations if they are not readable, falling back to '.'. Thus, SQLite will favor e.g. using cwd for tempfiles on such a system, even if cwd is an unsafe location. Notably, SQLite also checks the permissions of '.', but ignores the results of that check. By itself, this is only a POLA (Principle of Least Astonishment) violation that may cause unexpected failures. However, this might in turn cause software that uses SQLite libraries to behave in unsafe ways, leaking sensitive data, opening up SQLite libraries to attack by deliberately corrupted tempfiles, etc. 3. Technical Description SQLite creates tempfiles only under certain specific circumstances, and the behavior is tunable in various ways; see https://www.sqlite.org/tempfiles.html for more background. Generally speaking, the below does not apply for rollback journals, master journals, write-ahead log (WAL) files, or shared-memory (-shm) files. They may apply for various other tempfile types. When a tempfile must be created, sanity checks are performed on candidate tempdir locations; these checks are flawed. src/os_unix.c (which is merged into sqlite3.c during the release-tarball preparation process) performs these checks when considering candidate temporary directory locations (quoted from commit 0064a8c77b, 2016-02-23): /* ** Return the name of a directory in which to put temporary files. ** If no suitable temporary file directory can be found, return NULL. */ static const char *unixTempFileDir(void){ static const char *azDirs[] = { 0, 0, "/var/tmp", "/usr/tmp", "/tmp", "." }; unsigned int i; struct stat buf; const char *zDir = sqlite3_temp_directory; if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR"); if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); for(i=0; i CREATE TEMP TABLE testtemp(text); sqlite> foo ~ # ls -l /proc/$(pidof sqlite3)/fd/ | egrep /patsy/ lrwx------ 1 patsy patsy 64 Feb 23 23:04 3 -> /home/patsy/sqlite-test/unsafe/etilqs_1974c47b45a40cc9 (deleted) lrwx------ 1 patsy patsy 64 Feb 23 23:04 4 -> /home/patsy/sqlite-test/unsafe/etilqs_81d3a73a2307205a (deleted) The contents of this advisory are copyright(c) 2016 KoreLogic, Inc. and are licensed under a Creative Commons Attribution Share-Alike 4.0 (United States) License: http://creativecommons.org/licenses/by-sa/4.0/ KoreLogic, Inc. is a founder-owned and operated company with a proven track record of providing security services to entities ranging from Fortune 500 to small and mid-sized companies. We are a highly skilled team of senior security consultants doing by-hand security assessments for the most important networks in the U.S. and around the world. We are also developers of various tools and resources aimed at helping the security community. https://www.korelogic.com/about-korelogic.html Our public vulnerability disclosure policy is available at: https://www.korelogic.com/KoreLogic-Public-Vulnerability-Disclosure-Policy.v2.2.txt -----BEGIN PGP SIGNATURE----- iQEcBAEBCAAGBQJXdpA/AAoJEE1lmiwOGYkMwRoH/A3pSJACHMBbkk+LCMN8PXca aR7410NUPB7rf4gdrkJHUXFpsVROk9VO6eQUB8nRDgd1BlAXdPE7jGvCs3kEABtY FWlJu45fGb4F55H79YYh9PP1EHAAmmV4Qn4/j/UBKQKHoSwM+kqg6Yj8RLQC80gJ PcxxFTRJoXu5xZFjN6uZWH1UgnUKY+fJWBVtgwxk/JbboX6Nb8UCAv6PaGPiRONn wQ1skAafojVecXR7vkScIdDCHNOMOWZoVGY3KPMuxcSZO0B+BmhxxDlndQXi71sO 2DvMACs3wHWIJA+EJ+E80ys6RlYjSdm7SM6Rq6q3cduklgIwFVwtrP3MdSfL4gM= =AUQ1 -----END PGP SIGNATURE-----