Skip to content

Add a test for TLS pipelining - #19456

Closed
mattcaswell wants to merge 9 commits into
openssl:masterfrom
mattcaswell:test-pipeline
Closed

mattcaswell wants to merge 9 commits into
openssl:masterfrom
mattcaswell:test-pipeline

Conversation

@mattcaswell

Copy link
Copy Markdown
Member

TLS pipelining provides the ability for libssl to read or write multiple records in parallel. It requires special ciphers to do this, and there are currently no built-in ciphers that provide this capability. However, the dasync engine does have such a cipher, so we add a test for this capability using that engine.

Checklist
  • documentation is added or updated
  • tests are added or updated

TLS pipelining provides the ability for libssl to read or write multiple
records in parallel. It requires special ciphers to do this, and there are
currently no built-in ciphers that provide this capability. However, the
dasync engine does have such a cipher, so we add a test for this capability
using that engine.
@mattcaswell mattcaswell added branch: master Applies to master branch approval: review pending This pull request needs review by a committer approval: otc review pending labels Oct 20, 2022
@mattcaswell

Copy link
Copy Markdown
Member Author

This went in surprisingly smoothly and easily. Given the length of time this code has been there without a test, and the recent refactor of the record layer - I was expecting problems.

@mattcaswell

Copy link
Copy Markdown
Member Author

It would be a nice feature to extend the pipelining to TLSv1.3 and to provider based ciphers (DTLS could be done but will require new public API calls).

It would also be quite neat to utilise the newly added thread pooling functionality to make our existing ciphers pipeline capable in the default provider.

@slontis

slontis commented Oct 21, 2022

Copy link
Copy Markdown
Member

The address sanitizer disagrees :)

…lled

The pipeline input/output buf arrays must remain accessible to the
EVP_CIPHER_CTX until EVP_Cipher is subsequently called. This fixes an
asan error discovered by the newly added pipeline test.
@mattcaswell

Copy link
Copy Markdown
Member Author

The address sanitizer disagrees

Should be fixed now.

@t8m t8m left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approved with the typos fixed.

Comment thread test/sslapitest.c Resolved
Comment thread test/sslapitest.c Resolved
@t8m t8m added triaged: feature The issue/pr requests/adds a feature and removed approval: otc review pending labels Oct 21, 2022
@mattcaswell

Copy link
Copy Markdown
Member Author

Typos fixed.

@mattcaswell

Copy link
Copy Markdown
Member Author

Ping @openssl/committers for second review?

@t-j-h t-j-h added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Oct 24, 2022

@tmshort tmshort left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Mostly just a question on the use of ENGINE in the master branch, and whether this testing is sufficient (given the small data size).

Comment thread test/sslapitest.c
char buf[80];
ENGINE *e;

if (!TEST_ptr(e = ENGINE_by_id("dasync")))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I still don't get the support level of engines in the master branch. Are they fully deprecated? Should this not be a provider or something else? Feels as though we're taking two steps back to advance one step.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Are they fully deprecated

Yes

Should this not be a provider or something else?

Yes, really this test should be using a provider. Unfortunately at the moment we are in the slightly strange situation that libssl pipeline support is not deprecated, but in order to use that support you must use a pipeline capable cipher - and we have not added the support for pipeline capable ciphers into providers. So the only way you can possibly use the non-deprecated libssl pipeline support is by using a deprecated ENGINE. I consider this an accidental omission which we should fix. But that goes way beyond what I was planning to do with this PR, i.e. just make sure what we've currently got isn't broken.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

See also #18298

Comment thread test/sslapitest.c Resolved
Comment thread test/sslapitest.c Resolved
@openssl-machine

Copy link
Copy Markdown
Collaborator

24 hours has passed since 'approval: done' was set, but this PR has failing CI tests. Once the tests pass it will get moved to 'approval: ready to merge' automatically, alternatively please review and set the label manually.

During read pipelining we must ensure that the buffer is sufficiently large
to read enough data to fill our pipelines. We also remove some code that
moved data to the start of the packet if we can. This was unnecessary
because of later code which would end up moving it anyway. The earlier move
was also incorrect in the case that |clearold| was 0. This would cause the
read pipelining code to fail with sufficiently large records.
Document the effect on the internal read buffer when using pipelining.
@mattcaswell mattcaswell added approval: review pending This pull request needs review by a committer approval: otc review pending and removed approval: done This pull request has the required number of approvals labels Oct 25, 2022
@mattcaswell

Copy link
Copy Markdown
Member Author

Adding the additional testing to this as suggested by @tmshort was a worthwhile exercise since it highlighted a couple of problems in the read pipelining implementation. I've fixed those issues and also made a tweak to the documentation as a result.

I suspect that the issues I fixed predated the record layer refactor so its possible those issues exist in 3.0 and 1.1.1 - but I've not investigated that.

@mattcaswell

Copy link
Copy Markdown
Member Author

This now needs re-review since there have been some significant changes since the original version.

Comment thread test/sslapitest.c Resolved
Comment thread test/sslapitest.c Resolved
Comment thread ssl/record/methods/tls_common.c Resolved
@mattcaswell

Copy link
Copy Markdown
Member Author

All feedback addressed. @t8m - please take another look.

@mattcaswell

Copy link
Copy Markdown
Member Author

Ping for second review @openssl/committers

@hlandau hlandau added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Oct 27, 2022
EVP_CIPHER_CTX *ds;
size_t reclen[SSL_MAX_PIPELINES];
unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
unsigned char *data[SSL_MAX_PIPELINES];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why was this moved to the outer scope? It's the only change in this function.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was due to use-after-scope error picked up by asan. The buffers set by EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS must still be valid at the point that we call EVP_CipherUpdate - which was not the case when this was in its original location.

Comment on lines -289 to -307
} else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) {
/*
* check if next packet length is large enough to justify payload
* alignment...
*/
pkt = rb->buf + rb->offset;
if (pkt[0] == SSL3_RT_APPLICATION_DATA
&& (pkt[3] << 8 | pkt[4]) >= 128) {
/*
* Note that even if packet is corrupted and its length field
* is insane, we can only be led to wrong decision about
* whether memmove will occur or not. Header values has no
* effect on memmove arguments and therefore no buffer
* overrun can be triggered.
*/
memmove(rb->buf + align, pkt, left);
rb->offset = align;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this special case no longer apply?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This memmove was originally intended as an optimisation. It ensures that the payload data bytes are properly aligned for maximum assembler performance. There are two cases to consider: one where clearold is 0 and one where clearold is 1.

In the case that clearold is 0 this means "don't invalidate data earlier in the buffer because we're still using it". So this memove is just wrong in that instance. We must not move the data to an earlier position in the buffer because we're still using it. This actually was causing a problem in the pipelining case because we have multiple pipelines of data all using the same buffer - so this memmove was resulting in "bad record mac" error.

In the case that clearold is 1 it would still be a valid optimisation - but is entirely redundant because of the memmove that occurs a few lines later. This always occurs if clearold is 1 and the data is not already aligned.

So, since this code is either wrong or redundant (depending on the value of clearold) it can safely be removed completely.

@tmshort tmshort left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the explanations @mattcaswell

@openssl-machine openssl-machine added approval: ready to merge The 24 hour grace period has passed, ready to merge and removed approval: done This pull request has the required number of approvals labels Oct 28, 2022
@openssl-machine

Copy link
Copy Markdown
Collaborator

This pull request is ready to merge

@t8m

t8m commented Nov 2, 2022

Copy link
Copy Markdown
Member

Merged to master branch. Thank you.

@t8m t8m closed this Nov 2, 2022
@mattcaswell

Copy link
Copy Markdown
Member Author

Merged to master branch. Thank you.

You beat me to it! I was just about to "git push"!

openssl-machine pushed a commit that referenced this pull request Nov 2, 2022
TLS pipelining provides the ability for libssl to read or write multiple
records in parallel. It requires special ciphers to do this, and there are
currently no built-in ciphers that provide this capability. However, the
dasync engine does have such a cipher, so we add a test for this capability
using that engine.

Reviewed-by: Hugo Landau <[email protected]>
Reviewed-by: Todd Short <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from #19456)
openssl-machine pushed a commit that referenced this pull request Nov 2, 2022
…lled

The pipeline input/output buf arrays must remain accessible to the
EVP_CIPHER_CTX until EVP_Cipher is subsequently called. This fixes an
asan error discovered by the newly added pipeline test.

Reviewed-by: Hugo Landau <[email protected]>
Reviewed-by: Todd Short <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from #19456)
openssl-machine pushed a commit that referenced this pull request Nov 2, 2022
During read pipelining we must ensure that the buffer is sufficiently large
to read enough data to fill our pipelines. We also remove some code that
moved data to the start of the packet if we can. This was unnecessary
because of later code which would end up moving it anyway. The earlier move
was also incorrect in the case that |clearold| was 0. This would cause the
read pipelining code to fail with sufficiently large records.

Reviewed-by: Hugo Landau <[email protected]>
Reviewed-by: Todd Short <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from #19456)
openssl-machine pushed a commit that referenced this pull request Nov 2, 2022
Document the effect on the internal read buffer when using pipelining.

Reviewed-by: Hugo Landau <[email protected]>
Reviewed-by: Todd Short <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from #19456)
beldmit pushed a commit to beldmit/openssl that referenced this pull request Dec 26, 2022
TLS pipelining provides the ability for libssl to read or write multiple
records in parallel. It requires special ciphers to do this, and there are
currently no built-in ciphers that provide this capability. However, the
dasync engine does have such a cipher, so we add a test for this capability
using that engine.

Reviewed-by: Hugo Landau <[email protected]>
Reviewed-by: Todd Short <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from openssl#19456)
beldmit pushed a commit to beldmit/openssl that referenced this pull request Dec 26, 2022
…lled

The pipeline input/output buf arrays must remain accessible to the
EVP_CIPHER_CTX until EVP_Cipher is subsequently called. This fixes an
asan error discovered by the newly added pipeline test.

Reviewed-by: Hugo Landau <[email protected]>
Reviewed-by: Todd Short <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from openssl#19456)
beldmit pushed a commit to beldmit/openssl that referenced this pull request Dec 26, 2022
During read pipelining we must ensure that the buffer is sufficiently large
to read enough data to fill our pipelines. We also remove some code that
moved data to the start of the packet if we can. This was unnecessary
because of later code which would end up moving it anyway. The earlier move
was also incorrect in the case that |clearold| was 0. This would cause the
read pipelining code to fail with sufficiently large records.

Reviewed-by: Hugo Landau <[email protected]>
Reviewed-by: Todd Short <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from openssl#19456)
beldmit pushed a commit to beldmit/openssl that referenced this pull request Dec 26, 2022
Document the effect on the internal read buffer when using pipelining.

Reviewed-by: Hugo Landau <[email protected]>
Reviewed-by: Todd Short <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from openssl#19456)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval: ready to merge The 24 hour grace period has passed, ready to merge branch: master Applies to master branch triaged: feature The issue/pr requests/adds a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants