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

Implement secp256k1 error callbacks #89

Closed
wants to merge 5 commits into from

Conversation

sstone
Copy link
Member

@sstone sstone commented Dec 11, 2023

These callbacks are only triggered either by arguments do not match explicit requirements of the sepc256k1 library, or by hardware failures, memory corruption or bug in secp256k1, and not by misuse of the library.

In theory we do not need to implement them, except to find bugs in our own code, but the default callbacks print a message to stderr and call abort() which is not nice especially on mobile apps.

=> Here we introduce 2 specific exceptions, Secp256k1ErrorCallbackException and Secp256k1IllegalCallbackException, which are thrown when the error callback or illegal callback are called.

It must be 0,1,2 or 3, this is an explicit requirement of the secp256k1 library.
These callbacks are only triggered either by arguments do not match explicit rquirements of the sepc256k1 library, or by hardware failures, memory corruption
or bug in secp256k1, and not by misuse of the library.
In theory we do not need to implement them, except to find bugs in our own code, but the default callbacks print a message to stderr and call abort() which
is not nice especially on mobile apps.

=> Here we introduce 2 specific exceptions, Secp256k1ErrorCallbackException and Secp256k1IllegalCallbackException, which are thrown when the error callback or illegal callback are called.
@sstone sstone linked an issue Dec 11, 2023 that may be closed by this pull request
@sstone sstone requested a review from t-bast December 11, 2023 18:18
@sstone sstone changed the title Implement secp256k1 error callback Implement secp256k1 error callbacks Dec 11, 2023
jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c Outdated Show resolved Hide resolved
jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c Outdated Show resolved Hide resolved
private typealias MyHandler = (String) -> Unit

private object CallbackHandler {
var illegalCallBackMessage: String? = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it an issue that this value (and errorCallBackMessage) isn't thread-safe? Shouldn't we use an atomic ref or something synchronized?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a global callback is a wrong approach, it has to "point" to something that is local to the call site as in the JNI implementation, it's harder here because of the limitations of kotlin's c-interop but I'll rework this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 0364ec7 I use callbacks that are local to the call site.

@t-bast
Copy link
Member

t-bast commented Dec 13, 2023

There is an issue since the secp256k1 context is global: none of this is thread-safe, which can be exploited since we have many concurrent threads doing secp256k1 operations. If an attacker injects invalid input that would trigger one of those callbacks, depending on timing, it may fail another unrelated operation that was perfectly valid. I don't think there's any simple way around this, so we should probably just scan all ARG_CHECK from the libsecp256k1 codebase and replicate them before calling the libsecp256k1 function, instead of trying to leverage these callbacks.

@sstone
Copy link
Member Author

sstone commented Dec 13, 2023

Yes, with our current model and one global native context (and I don't see yet what else we could do), implementing callbacks does not really work.
We should indeed validate all arguments first (we do must missed this one...).
Closed in favour of #94

@sstone
Copy link
Member Author

sstone commented Dec 13, 2023

I checked and bitcoin-core also uses a global secp256k1 context and does not set error callbacks.

@t-bast
Copy link
Member

t-bast commented Dec 13, 2023

Good idea to check bitcoin core, I think we're making the right move!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants