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

[crypto] HaiCrypt_Clone(): sets up receiver clone properly #2905

Merged
merged 1 commit into from
Apr 4, 2024

Conversation

funman
Copy link
Contributor

@funman funman commented Mar 7, 2024

This fixes key rotation when kk changes
Remote TX sends one even key on initial handshake, and later sends two (even and odd) keys in a KMREQ USER packet

Here is srt-live-transmit showing initial handshake, and key update:


15:45:37.523916/SRT:RcvQ:w1.N:SRT.cn: processSrtMsg_KMRSP: cmd=4(KMRSP) len=224 KmState: SND=SECURED RCV=SECURED 15:45:37.524445/SRT:RcvQ:w1.N:SRT.cn: @234271448: Connection established to: 10.129.73.178:5000 SRT source connected

15:45:42.285301/SRT:RcvQ:w1!W:SRT.cn: KMREQ/rcv: (snd) Rx process failure - BADSECRET 15:45:42.285372/SRT:RcvQ:w1.N:SRT.cn: processSrtMsg_KMREQ: cmd=3(KMREQ) len=288 KmState: SND=BADSECRET RCV=BADSECRET 15:45:42.285410/SRT:RcvQ:w1!W:SRT.cn: @234271448: KMREQ FAILURE: BADSECRET - rejecting per enforced encryption --------------------

This fixes key rotation when kk changes
Remote TX sends one even key on initial handshake,
and later sends two (even and odd) keys in a KMREQ USER packet

Here is srt-live-transmit showing initial handshake, and key update:

--------------------
15:45:37.523916/SRT:RcvQ:w1.N:SRT.cn: processSrtMsg_KMRSP: cmd=4(KMRSP) len=224 KmState: SND=SECURED RCV=SECURED
15:45:37.524445/SRT:RcvQ:w1.N:SRT.cn: @234271448: Connection established to: 10.129.73.178:5000
SRT source connected

15:45:42.285301/SRT:RcvQ:w1!W:SRT.cn: KMREQ/rcv: (snd) Rx process failure - BADSECRET
15:45:42.285372/SRT:RcvQ:w1.N:SRT.cn: processSrtMsg_KMREQ: cmd=3(KMREQ) len=288 KmState: SND=BADSECRET RCV=BADSECRET
15:45:42.285410/SRT:RcvQ:w1!W:SRT.cn: @234271448: KMREQ FAILURE: BADSECRET - rejecting per enforced encryption
--------------------
@maxsharabayko maxsharabayko added this to the v1.5.4 milestone Mar 7, 2024
@maxsharabayko maxsharabayko added Type: Bug Indicates an unexpected problem or unintended behavior [core] Area: Changes in SRT library core labels Mar 7, 2024
@kierank
Copy link

kierank commented Mar 7, 2024

Is this a potential security issue as it was using uninitalised data before?

This comment was marked as off-topic.

@maxsharabayko
Copy link
Collaborator

From what I can see, the issue is on the caller (initiator) to become a payload receiver. It creates the TX key and clones it into the RX crypto instance. The m_hRcvCrypto->ctx then points to the m_hSndCrypto->ctx_pair[0] instead of its own. The proposed fix resolved this by setting the m_hRcvCrypto->ctx = m_hRcvCrypto->ctx_pair[0].
I assume the status should be set to HCRYPT_CTX_S_ACTIVE then. @jeandube please confirm.

The m_hRcvCrypto->ctx is however not used to decrypt the payload. The appropriate ctx_pair[ ] is used instead. Each time a payload is decrypted the m_hRcvCrypto->ctx is set to an appropriate ctx_pair (see the HaiCrypt_Rx_Data). Thus "resolving" (or masking) the issue.

@funman How to reproduce the BADSECRET issue? Should the key refresh be sent before even sending any data packet?

@jeandube
Copy link
Collaborator

jeandube commented Apr 2, 2024

@maxsharabayko going through this thread

@jeandube
Copy link
Collaborator

jeandube commented Apr 2, 2024

@maxsharabayko Any ref to the issue being fixed by this PR?

@ethouris
Copy link
Collaborator

ethouris commented Apr 2, 2024

Just such that the original reporter claims that the change fixes the problem. The matter is only that we need to make sure that there was any chance for this problem to happen and under what circumstances so that we can confirm the fix.

@kierank
Copy link

kierank commented Apr 2, 2024 via email

@kierank
Copy link

kierank commented Apr 2, 2024 via email

@ethouris

This comment was marked as off-topic.

@kierank

This comment was marked as off-topic.

@funman
Copy link
Contributor Author

funman commented Apr 2, 2024 via email

@ethouris

This comment was marked as off-topic.

@ethouris
Copy link
Collaborator

ethouris commented Apr 2, 2024

@funman The pcap would be nice, no matter the time to wait.

What is most interesting for us is that: is there any special socket options (for SRTO_KMREFRESHRATE or SRTO_KMPREANNOUNCE) or it should work also with defaults, whether the transmission was in only one on both directions, if one, which side (caller or listener) was sending, if both, which achieved the key switch period earlier, and more-less what was the transmission bitrate.

@kierank

This comment was marked as off-topic.

@kierank

This comment was marked as off-topic.

@funman
Copy link
Contributor Author

funman commented Apr 4, 2024

badsecret.pcapng.zip

% ./srt-live-transmit 'srt://10.129.73.178:5000?latency=500&passphrase=ristbetter' udp://127.0.0.1:2000  -ll info
Media path: 'srt://10.129.73.178:5000?latency=500&passphrase=ristbetter' --> 'udp://127.0.0.1:2000'
10:44:30.814802/SRT:RcvQ:w1.N:SRT.cn: processSrtMsg_KMRSP: cmd=4(KMRSP) len=224 KmState: SND=SECURED RCV=SECURED
10:44:30.816397/SRT:RcvQ:w1.N:SRT.cn: @449459863: Connection established to: 10.129.73.178:5000
SRT source connected
10:44:34.103475/SRT:RcvQ:w1!W:SRT.cn: KMREQ/rcv: (snd) Rx process failure - BADSECRET
10:44:34.103525/SRT:RcvQ:w1.N:SRT.cn: processSrtMsg_KMREQ: cmd=3(KMREQ) len=288 KmState: SND=BADSECRET RCV=BADSECRET
10:44:34.103542/SRT:RcvQ:w1!W:SRT.cn: @449459863: KMREQ FAILURE: BADSECRET - rejecting per enforced encryption
10:44:35.104698/SRT:RcvQ:w1!W:SRT.cn: KMREQ/rcv: (snd) Rx process failure - BADSECRET
10:44:35.104765/SRT:RcvQ:w1.N:SRT.cn: processSrtMsg_KMREQ: cmd=3(KMREQ) len=288 KmState: SND=BADSECRET RCV=BADSECRET
10:44:35.104787/SRT:RcvQ:w1!W:SRT.cn: @449459863: KMREQ FAILURE: BADSECRET - rejecting per enforced encryption
10:44:36.100720/SRT:RcvQ:w1!W:SRT.cn: KMREQ/rcv: (snd) Rx process failure - BADSECRET
10:44:36.100746/SRT:RcvQ:w1.N:SRT.cn: processSrtMsg_KMREQ: cmd=3(KMREQ) len=288 KmState: SND=BADSECRET RCV=BADSECRET
10:44:36.100753/SRT:RcvQ:w1!W:SRT.cn: @449459863: KMREQ FAILURE: BADSECRET - rejecting per enforced encryption
^C

Stream is generated with:

ffmpeg -re -f lavfi -i "smptehdbars=rate=60:size=1280x720" -f lavfi -i "sine=frequency=400:sample_rate=48000" -vf drawtext="text='FOO BAR:timecode=01\:00\:00\:00':rate=25:x=(w-tw)/2:y=(h-lh)/2:fontsize=48:fontcolor=white:box=1:boxcolor=black" -c:v h264 -profile:v baseline -pix_fmt yuv420p -preset ultrafast -tune zerolatency  -b:v 500k -minrate 700k -maxrate 700k -bufsize 700k -c:a aac -f mpegts -muxrate 700k udp://127.0.0.1:1212\?pkt_size=1316

@maxsharabayko
Copy link
Collaborator

@funman Thank you. Now everything is clear. Kudos for the fix! 👏

@maxsharabayko maxsharabayko merged commit c6afa19 into Haivision:master Apr 4, 2024
12 checks passed
@kierank
Copy link

kierank commented Apr 4, 2024

Is this a security issue?

@maxsharabayko
Copy link
Collaborator

I would not classify it as a security issue.
A connection with an alternative SRT implementation gets rejected (if it would instead use a wrong encryption key, that would be an issue). Also, there is no uninitialized memory access in this case. Just the RX context points to the TX context (both are the same, as RX was cloned from TX), a bug fixed by this PR.

@funman
Copy link
Contributor Author

funman commented Apr 5, 2024

That's my understanding too, also these objects should be freed at the same time so no use after free either

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[core] Area: Changes in SRT library core Type: Bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants