From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#63590: 29.0.90; can't load sqlite extension Date: Sat, 20 May 2023 12:53:12 +0300 Message-ID: <83o7mfs513.fsf@gnu.org> References: <2a8d4ca6-1fdf-98c7-6d4b-01f9cca30e8b@vogelsang.berlin> <83a5y0th16.fsf@gnu.org> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26745"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 63590@debbugs.gnu.org To: lennart@vogelsang.berlin Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat May 20 11:54:23 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q0JI7-0006jT-51 for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 20 May 2023 11:54:23 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q0JHo-0002wB-6Z; Sat, 20 May 2023 05:54:04 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q0JHn-0002vy-0B for bug-gnu-emacs@gnu.org; Sat, 20 May 2023 05:54:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1q0JHm-00070Y-OW for bug-gnu-emacs@gnu.org; Sat, 20 May 2023 05:54:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1q0JHm-0004F1-Ff for bug-gnu-emacs@gnu.org; Sat, 20 May 2023 05:54:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 20 May 2023 09:54:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63590 X-GNU-PR-Package: emacs Original-Received: via spool by 63590-submit@debbugs.gnu.org id=B63590.168457638416227 (code B ref 63590); Sat, 20 May 2023 09:54:02 +0000 Original-Received: (at 63590) by debbugs.gnu.org; 20 May 2023 09:53:04 +0000 Original-Received: from localhost ([127.0.0.1]:57894 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1q0JGp-0004Df-DX for submit@debbugs.gnu.org; Sat, 20 May 2023 05:53:03 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:45238) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1q0JGn-0004DA-M8 for 63590@debbugs.gnu.org; Sat, 20 May 2023 05:53:02 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q0JGh-0006pp-Ro; Sat, 20 May 2023 05:52:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=CKqs4l8St5mXUZxUteKoGo0d/JptDQG/9xoNhYrzqt4=; b=mL0/09bHpaFU F3NM01uChu6K0uEihRaf/DBaP6BHEZWiRJ4FapHqO/IcK98lb7cSTxPJGsn1TcgpYw4mXuDqkeK3K 0XTTGkSAru1nx+KgiUy88X9mmhidqgwj+fm2n7uo0GD9+F62NGYKIQIzhPcxZaj8S0hiQFxP5+fUV vDUtBPIs6ScWTafD45Ksl/o69NwNIQ3rI7ZnIfjQkj0VqVesF3PX2cIYZpj6SdJpJGB2H6IKNUOjG 01vZHN8G/vEJGKHonAumhvfEck2VB3YvOuIdNBFV54BmtfERXyWY2LZ1u6LMGyaML1NhkwDo71ZnN hONfIoNtJvjOUhC1Zelk6Q==; Original-Received: from [87.69.77.57] (helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q0JGh-0000LK-BK; Sat, 20 May 2023 05:52:55 -0400 In-Reply-To: <83a5y0th16.fsf@gnu.org> (message from Eli Zaretskii on Fri, 19 May 2023 19:36:21 +0300) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:262045 Archived-At: > Cc: 63590@debbugs.gnu.org > Date: Fri, 19 May 2023 19:36:21 +0300 > From: Eli Zaretskii > > Are you saying that the SQL-related tests in the Emacs test suite > don't work for you? See test/src/sqlite-tests.el. Answering myself: the relevant test considers it a "success" if sqlite-load-extension returns nil, even if the extension does exist on the system. So it doesn't really test whether the extension was successfully loaded. I will tweak the test to be more useful in this regard. Anyway, does the patch to sqlite.c below fix your problem? I decided to enable loading extensions only temporarily, while we call sqlite3_load_extension, so that no extension could be accidentally loaded out of our control, not even if some Emacs module uses the sqlite3 library on its own using the C APIs, thus bypassing sqlite-load-extension. diff --git a/src/sqlite.c b/src/sqlite.c index 0361514..2b0bc02 100644 --- a/src/sqlite.c +++ b/src/sqlite.c @@ -23,6 +23,8 @@ Copyright (C) 2021-2023 Free Software Foundation, Inc. https://github.com/syohex/emacs-sqlite3 */ #include + +#include #include "lisp.h" #include "coding.h" @@ -686,7 +692,8 @@ DEFUN ("sqlite-load-extension", Fsqlite_load_extension, /* Add names of useful and free modules here. */ const char *allowlist[3] = { "pcre", "csvtable", NULL }; char *name = SSDATA (Ffile_name_nondirectory (module)); - /* Possibly skip past a common prefix. */ + /* Possibly skip past a common prefix (libsqlite3_mod_ is used by + Debian, see https://packages.debian.org/source/sid/sqliteodbc). */ const char *prefix = "libsqlite3_mod_"; if (!strncmp (name, prefix, strlen (prefix))) name += strlen (prefix); @@ -697,7 +704,7 @@ DEFUN ("sqlite-load-extension", Fsqlite_load_extension, if (strlen (*allow) < strlen (name) && !strncmp (*allow, name, strlen (*allow)) && (!strcmp (name + strlen (*allow), ".so") - || !strcmp (name + strlen (*allow), ".DLL"))) + || !strcasecmp (name + strlen (*allow), ".dll"))) { do_allow = true; break; @@ -707,12 +714,25 @@ DEFUN ("sqlite-load-extension", Fsqlite_load_extension, if (!do_allow) xsignal1 (Qsqlite_error, build_string ("Module name not on allowlist")); - int result = sqlite3_load_extension - (XSQLITE (db)->db, - SSDATA (ENCODE_FILE (Fexpand_file_name (module, Qnil))), - NULL, NULL); - if (result == SQLITE_OK) - return Qt; + /* Expand all Lisp data explicitly, so as to avoid signaling an + error while extension loading is enabled -- we don't want to + "leak" this outside this function. */ + sqlite3 *sdb = XSQLITE (db)->db; + char *ext_fn = SSDATA (ENCODE_FILE (Fexpand_file_name (module, Qnil))); + /* Temporarily enable loading extensions via the C API. */ + int result = sqlite3_db_config (sdb, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, + NULL); + if (result == SQLITE_OK) + { + result = sqlite3_load_extension (sdb, ext_fn, NULL, NULL); + if (result == SQLITE_OK) + { + /* Disable loading extensions via C API. */ + sqlite3_db_config (sdb, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 0, + NULL); + return Qt; + } + } return Qnil; } #endif /* HAVE_SQLITE3_LOAD_EXTENSION */