Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segment fault when custom build with gcc. #26

Closed
fanthos opened this issue Mar 5, 2021 · 6 comments
Closed

Segment fault when custom build with gcc. #26

fanthos opened this issue Mar 5, 2021 · 6 comments

Comments

@fanthos
Copy link

fanthos commented Mar 5, 2021

I am trying to use this project with emcc, got segment fault when compiling with following command:

gcc
-O0
-DSQLITE_OMIT_LOAD_EXTENSION
-DSQLITE_DISABLE_LFS
-DSQLITE_ENABLE_FTS3
-DSQLITE_ENABLE_FTS3_PARENTHESIS
-DSQLITE_THREADSAFE=0
-DSQLITE_USE_URI=1
-DHAVE_CIPHER_AES_128_CBC=0
-DHAVE_CIPHER_AES_256_CBC=0
-DHAVE_CIPHER_RC4=0
-DSQLITE_ENABLE_EXTFUNC
-DSQLITE_ENABLE_SERIES
-DSQLITE_ENABLE_NORMALIZE
sqlite-src/sqlite3mc-1.1.4/sqlite3mc_amalgamation.c
sqlite-src/sqlite3mc-1.1.4/shell.c
-o out/sqlite3.o
-maes
-lm

Debugging with gdb --args ./out/sqlite3.o 'file:./test.db?cipher=chacha20&key=test', I got:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x0000000008107657 in sqlite3mcCodecSetup (codec=0x858c468, cipherType=1, userPassword=0x857b65a "test", passwordLength=4) at sqlite-src/sqlite3mc-1.1.4/sqlite3mc_amalgamation.c:252720
#2  0x000000000810b4bb in sqlite3mcCodecAttach (db=0x857b0f8, nDb=0, zPath=0x857bf44 "/mnt/e/Workspaces/github/javascript/sql.js/test.db", zKey=0x857b65a, nKey=4) at sqlite-src/sqlite3mc-1.1.4/sqlite3mc_amalgamation.c:254646
#3  0x000000000810b70e in sqlite3_key_v2 (db=0x857b0f8, zDbName=0x8139312 "main", zKey=0x857b65a, nKey=4) at sqlite-src/sqlite3mc-1.1.4/sqlite3mc_amalgamation.c:254720
#4  0x000000000810a3ce in sqlite3mcCodecQueryParameters (db=0x857b0f8, zDb=0x8139312 "main", zUri=0x857b63c "./test.db") at sqlite-src/sqlite3mc-1.1.4/sqlite3mc_amalgamation.c:254011
#5  0x000000000810a5d2 in sqlite3mcHandleMainKey (db=0x857b0f8, zPath=0x857b63c "./test.db") at sqlite-src/sqlite3mc-1.1.4/sqlite3mc_amalgamation.c:254071
#6  0x00000000080cc8a3 in openDatabase (zFilename=0x7ffffffedd38 "file:./test.db?cipher=chacha20&key=test", ppDb=0x7ffffffec7b0, flags=70, zVfs=0x0) at sqlite-src/sqlite3mc-1.1.4/sqlite3mc_amalgamation.c:165298
#7  0x00000000080cc92d in sqlite3_open_v2 (filename=0x7ffffffedd38 "file:./test.db?cipher=chacha20&key=test", ppDb=0x7ffffffec7b0, flags=6, zVfs=0x0) at sqlite-src/sqlite3mc-1.1.4/sqlite3mc_amalgamation.c:165321
#8  0x0000000008128792 in open_db (p=0x7ffffffec7b0, openFlags=0) at sqlite-src/sqlite3mc-1.1.4/shell.c:14155
#9  0x00000000081354ad in runOneSqlLine (p=0x7ffffffec7b0, zSql=0x857b060 "select * from sqlite_master;", in=0x0, startline=1) at sqlite-src/sqlite3mc-1.1.4/shell.c:19922
#10 0x0000000008135a2a in process_input (p=0x7ffffffec7b0) at sqlite-src/sqlite3mc-1.1.4/shell.c:20026
#11 0x000000000813751f in main (argc=2, argv=0x7ffffffeda98) at sqlite-src/sqlite3mc-1.1.4/shell.c:20808

The Makefile from sql-js/sql.js works great with sqleet, but not sqlite3mc.

@fanthos
Copy link
Author

fanthos commented Mar 5, 2021

Another thing, I found that the AES code is compiled to the target file even if no SqlCipher, AES128, AES256 flags set.

@utelle
Copy link
Owner

utelle commented Mar 5, 2021

I am trying to use this project with emcc, got segment fault when compiling with following command:

gcc
-O0
-DSQLITE_OMIT_LOAD_EXTENSION
-DSQLITE_DISABLE_LFS
-DSQLITE_ENABLE_FTS3
-DSQLITE_ENABLE_FTS3_PARENTHESIS
-DSQLITE_THREADSAFE=0
-DSQLITE_USE_URI=1
-DHAVE_CIPHER_AES_128_CBC=0
-DHAVE_CIPHER_AES_256_CBC=0
-DHAVE_CIPHER_RC4=0
-DSQLITE_ENABLE_EXTFUNC
-DSQLITE_ENABLE_SERIES
-DSQLITE_ENABLE_NORMALIZE
sqlite-src/sqlite3mc-1.1.4/sqlite3mc_amalgamation.c
sqlite-src/sqlite3mc-1.1.4/shell.c
-o out/sqlite3.o
-maes
-lm

At first glance, the list of compile-time options looks ok.

Debugging with gdb --args ./out/sqlite3.o 'file:./test.db?cipher=chacha20&key=test', I got:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x0000000008107657 in sqlite3mcCodecSetup (codec=0x858c468, cipherType=1, userPassword=0x857b65a "test", passwordLength=4) at sqlite-src/sqlite3mc-1.1.4/sqlite3mc_amalgamation.c:252720
[...]

Well, the debugger output line # 1 shows cipherType=1. This corresponds to cipher type CODEC_TYPE_AES128 (=1) (which you excluded from your build). Since CODEC_TYPE_AES128 was excluded the associated function pointers are all NULL pointers. And this is what finally leads to a crash.

The problem is that I really don't understand how you could get this value for the cipher type, unless you modified the source code. The default cipher type is CODEC_TYPE_CHACHA20 (=3), which you get if no cipher type was specified or the cipher name was misspelled.

The Makefile from sql-js/sql.js works great with sqleet, but not sqlite3mc.

Your remark is too general to allow to understand what might go wrong. There are so many packages out there which are based on SQLite in one form or the other, that I certainly can't know all of them. Minor adjustment are frequently necessary. So, please be a bit more specific.

Another thing, I found that the AES code is compiled to the target file even if no SqlCipher, AES128, AES256 flags set.

I have to admit that the precompiler statements excluding certain parts of the code were contributed by another user. Obviously they do not cleanly separate the code parts related to sqleet and sqlcipher. That is, if sqleet or sqlcipher is enabled the other cipher is included, too. Yes, I agree that could be solved better.

@fanthos
Copy link
Author

fanthos commented Mar 5, 2021

I think the problem is at cipher lookup, when aes excluded from build. There are two arrays for default cipher parameters and cipher define.
I am using unmodified version from release page, and shell.c from sqlite's zip file.
The gcc line is modified version from sqljs make file, trying use as drop in replacement for sqlite.

The code seems works if I didn't pass cipher in url.

@utelle
Copy link
Owner

utelle commented Mar 5, 2021

I think the problem is at cipher lookup, when aes excluded from build. There are two arrays for default cipher parameters and cipher define.

I will look into the issue early next week.

utelle added a commit that referenced this issue Mar 8, 2021
If cipher schemes 'aes128cbc' and/or 'aes256cbc' are excluded from the build, selecting the cipher via URI parameter leads to a crash, because a wrong (excluded) cipher is selected due to using the wrong cipher index.
@utelle
Copy link
Owner

utelle commented Mar 8, 2021

Thanks for reporting the issue. I have applied a fix now. The issue affects only builds which exclude cipher schemes from the build process.

A release including the fix will follow shortly after the release of the next SQLite version (expected end of March according to the current SQLite draft release log).

@utelle
Copy link
Owner

utelle commented Mar 13, 2021

Release of SQLite3 Multiple Ciphers version 1.2.0 done ... closing.

@utelle utelle closed this as completed Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants