From 50002b5d4630699e14a61d5d6c643fe7515a6c19 Mon Sep 17 00:00:00 2001
From: Brian Wolff <bawolff+wn@gmail.com>
Date: Tue, 8 Nov 2022 06:45:04 -0800
Subject: [PATCH] SECURITY: Make sqlite DB files not world readable

Ensure that SQLite DB files are created as 0600. Previously they
were created 0644 (world-readable). This was somewhat mitigated by
the fact that if MediaWiki had to create the parent directory, it
would be created as 0700. However if the parent directory already
existed and had loose permissions, than anyone on the server
could read the DB file and all the private data contained therein.

This only affects newly created DB files during the installation
process. It does not adjust previously created DB files.

Bug: T322637
Change-Id: I167ec1b5cd9a7e96919886fd20917b5733d59fc7
---
 includes/installer/SqliteInstaller.php | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/includes/installer/SqliteInstaller.php b/includes/installer/SqliteInstaller.php
index 1270871ca22..0426d4a7c2c 100644
--- a/includes/installer/SqliteInstaller.php
+++ b/includes/installer/SqliteInstaller.php
@@ -322,13 +322,20 @@ EOT;
 	 */
 	protected function makeStubDBFile( $dir, $db ) {
 		$file = DatabaseSqlite::generateFileName( $dir, $db );
+
 		if ( file_exists( $file ) ) {
 			if ( !is_writable( $file ) ) {
 				return Status::newFatal( 'config-sqlite-readonly', $file );
 			}
-		} elseif ( file_put_contents( $file, '' ) === false ) {
+			return Status::newGood();
+		}
+
+		$oldMask = umask( 0177 );
+		if ( file_put_contents( $file, '' ) === false ) {
+			umask( $oldMask );
 			return Status::newFatal( 'config-sqlite-cant-create-db', $file );
 		}
+		umask( $oldMask );
 
 		return Status::newGood();
 	}
-- 
2.30.2

