Skip to content

Extreme number of memory allocation calls #14837

Closed

Description

@DDvO

In the context of #14833 I tentatively limited the number of successful {m,re}alloc calls
and found that OpenSSL application runs can cause terribly many such calls!

The worst example I found in our test suite is make test TESTS="test_encoder_decoder"
which takes between 5,400,000 and 5,500,000 such calls.

Activity

  1. added
    issue: bug report
    on Apr 12, 2021
  2. paulidale commented on Apr 12, 2021

    @paulidale
    Contributor

    Yes :(

  3. DDvO commented on Apr 12, 2021

    @DDvO
    ContributorAuthor

    BTW, also the rather blind trail&error approach in OSSL_STORE when parsing credential files appears pretty inefficient.
    Though make test TESTS="test_encoder_decoder" requires "only" some 210,000 to 220,000 mem alloc calls per run.

  4. paulidale commented on Apr 12, 2021

    @paulidale
    Contributor

    Yes, it's very inefficient. How else should we parse files whose formats we don't necessarily know?
    The parsing is from a provider and there will be (are?) providers written by other people.

  5. DDvO commented on Apr 12, 2021

    @DDvO
    ContributorAuthor

    I don't have much insight in the provider topic,
    but as far as known credential types and formats (in particular, PEM and DER/ASN.1) are involved,
    theses formats include some header information that should help at least excluding most of the multitude of cases.
    The remaining actually possible case(s) should be tried first (in the order of how promising/likely they are, if more than one)
    before trying externally provided parsers, about which maybe no assumptions can be made.

  6. davidben commented on Apr 12, 2021

    @davidben
    Contributor

    PEM has a type header, but DER does not. Two ASN.1 structures whose top-level tag is SEQUENCE will have the same set of initial headers. You have to continue parsing to distinguish them.

    But I think the header business is a red herring. I think the bigger issue is why the provider APIs require OpenSSL to guess at what format it is parsing. That sounds like a design flaw. Having to guess formats results in fragile systems and security issues. (Suppose the two syntaxes overlap and attackers can force an ambiguous inputs in.) For examples, see the many many problems that have come from content-sniffing in the web.

    Instead, APIs to decode or encode structures should require the caller pass in enough information to determine the format.

  7. t8m commented on Apr 12, 2021

    @t8m
    Member

    Instead, APIs to decode or encode structures should require the caller pass in enough information to determine the format.

    No, that is impossible - it would mean you always know what you're reading and decoding. But you simply do not have this information in many cases. But yes, in cases the caller knows what it is decoding, it should supply the information and the decoding process should limit the decoders to only the relevant ones. IMO that is already the case in many cases but maybe there are callers which do not fully supply the information.

  8. davidben commented on Apr 13, 2021

    @davidben
    Contributor

    Can you give an example of when the caller would not know?

  9. t8m commented on Apr 14, 2021

    @t8m
    Member

    Reading a private key to perform a signature where I do not know whether the key is in PEM or DER format and what algorithm the key uses? Something quite common and possible with the current interfaces that give you the crypto agility.

  10. t8m commented on Apr 14, 2021

    @t8m
    Member

    It is actually much less common that the caller knows exactly what the user is trying to load.

  11. t8m commented on Apr 14, 2021

    @t8m
    Member

    And yes, we need to support both cases

    • the caller knows exactly what he is loading - in that case it should not try decoders that are irrelevant and it should properly fail if the supplied data is something else than what was expected
    • the caller does not know anything or has only some partial knowledge - in that case the decoding code should be flexible enough to load the data by trying the available decoders

    And IMO, apart from possible bugs, the current code already handles both cases.

  12. davidben commented on Apr 14, 2021

    @davidben
    Contributor

    DER is not a single format. There are DER SubjectPublicKeyInfos, DER RSAPublicKeys, etc., each of which need separate parsing entrypoints because DER structures are not, a priori, distinguishable. Treating PEM as a single entrypoint is a bit more plausible because there is a type in it.

    Not knowing what algorithm the key uses is fine. (Well, it's not pretty dangerous but we'll set that aside.) There are plenty of algorithm-agnostic formats like PEM, SubjectPublicKeyInfo, PrivateKeyInfo, etc. But if you're instead trying to parse an algorithm-specific format like RSAPublicKey, X9.62 EC point serialization, raw Ed25519 keys, etc., you really need to know what format you've got.

    Yes, it's true that many pairs of formats coincidentally don't overlap. RSAPrivateKey and ECPrivateKey are both SEQUENCEs that begin with an INTEGER, but the fields afterwards don't align. But there's no guarantee of this, especially for a generic pluggable provider framework. Any 32-byte string can be parsed as either a raw Ed25519 private key or a raw X25519 private key (not the same). Maybe some other provider also sometimes serializes as 32 bytes. Even PEM and a DER structure, while non-overlapping on the surface, overlap. PEM usually ignores non-PEM lines and DER encodes bytes as-is. So an X.509 certificate or PKCS#8 PrivateKeyInfo that happened to include a PEM string in an attribute would parse as either.

    This overlap means sniffing-based parsers can be tricked to misinterpreting an input in the wrong format. This can result in security bugs, such as my favorite paper title ever. :-)

    The example isn't very concrete, but I'm guessing you mean some command-line utility that tries to be friendly and guess the format of some random file passed on the command-line. This is very risky, but sure, there are applications like that. Those can still be supported without blessing the dangerous patterns in OpenSSL's API. Instead, the applications can calling the separate parsers explicitly. It means they must enumerate the formats, but that's desirable. It limits the confusable formats to a closed set and they can be analyzed for overlap. It also avoids encouraging this behavior in other applications, where this implicitness is a security bug. Those applications can still support providers by including an algorithm-agnostic format.

    As an analogy, suppose an application is stuck needing to guess base64 vs. hex. You don't see standard libraries provide a decodeBase64OrHex function. The functions are simply decodeBase64 and decodeHex. If the application needs to guess, the application can try one followed by the other. That's good because base64 and hex encodings have overlaps and this kind of guessing is usually dangerous.

  13. added
    triaged: refactor
    and removed
    issue: bug report
    on Apr 16, 2021
  14. DDvO commented on Apr 26, 2021

    @DDvO
    ContributorAuthor

    And yes, we need to support both cases

    * the caller knows exactly what he is loading - in that case it should not try decoders that are irrelevant and it should properly fail if the supplied data  is something else than what was expected
    
    * the caller does not know anything or has only some partial knowledge - in that case the decoding code should be flexible enough to load the data by trying the available decoders
    

    And IMO, apart from possible bugs, the current code already handles both cases.

    Unfortunately, at least for file_load() in file_store.c as typically called by OSSL_STORE_load(), as called by load_key_certs_crls() in apps.c there is a related major inefficiency:

    decoder_process() in decoder_lib.c runs through currently 160 decoder instances regardless whether OSSL_STORE_expect(ctx, expect)) has been called, e.g, with expect being OSSL_STORE_INFO_CERT. The latter is just used to filter the results after having been parsed. This may be a partly inherent problems for, e.g., PEM and PKCS#12 files which form a "mixed bag", potentially containing not only certs but also keys etc.
    Even worse, all iterations are done regardless whether already one of them was successful parsing, e.g., a cert - @levitte, why?

    Moreover, 160 iterations is done not only for each credential found in the file but also at EOF.

    So in case a file holds one cert, 2*160 decoders are called ☹️

  15. 80 remaining items

  16. DDvO commented on Sep 8, 2022

    @DDvO
    ContributorAuthor

    This got closed for a reason that appears not to the point: commit f0191d0.

  17. reopened this on Sep 8, 2022
  18. DDvO commented on Sep 8, 2022

    @DDvO
    ContributorAuthor

    With the current master, the figures have improved since last year, but still are not good.
    For

    test/endecode_test -rsa test/certs/ee-key.pem -pss test/certs/ca-pss-key.pem -context -config test/default.cnf -provider default
    

    I just got

    malloc count: 1051112 realloc count: 9370
    

    For

    apps/openssl verify -trusted test/certs/root-cert.pem -untrusted test/certs/ca-cert.pem test/certs/ee-cert.pem
    

    I just got

    malloc count: 7928 realloc count: 768
    
  19. removed this from the 3.1.0 milestone on Jan 26, 2023
  20. added
    triaged: performance
    on Nov 7, 2023
  21. nhorman commented on Jun 10, 2024

    @nhorman
    Contributor

    Reading this, I dont immediately see an actionable task here. @DDvO could you (re)state what your goal is. It seem evident you would like to see fewer calls to *alloc (the goal being improved performance), but I'm missing a level of profiling that indicates (a) what the impact of the larger number of calls is in terms of performance, and (b) what a reasonable goal to achieve here is. With that information we may be able to make additional forward progress here.

  22. DDvO commented on Jun 11, 2024

    @DDvO
    ContributorAuthor

    The performance impact of memory allocation cannot be reasonably measured in isolation and assigned a goal figure as it is not the root cause of the issue.

    The extreme number of *alloc calls is not only a matter of inefficiency in itself but more fundamentally an alarming symptom of overly complex design and implementation.

    As is the excessive growth of execution time and memory consumption (compared to pre-3.0) of some aspects of OpenSSL, such as the DER handling mentioned above.

  23. nhorman commented on Jun 11, 2024

    @nhorman
    Contributor

    @DDvO Thats all very true, but it doesn't answer the fundamental question - Why is this issue here?

    From my reading of the history of this issue:

    you identified that we had a huge number of memory allocations

    General discussion suggested decoder inefficiencies were at least partially the cause

    After much additional discussion and analysis, Things have improved

    You note there is still a significant increase in memory usage in the decoders

    @levitte Offers a another patch to further improve the situation

    You note a slight regression

    @t8m offers a reason for it

    You note that malloc counts are much better, but still not 'good'

    At which point the issue receives no further comments for 2+ years.

    that last part concerns me greatly, because it suggests that, despite several things getting fixed, there's still "something" to do, but no one has offered a suggestion as to what that something is. Without knowing that, this issue isn't going to change, and no progress will be made.

    Note, I'm not saying there isn't a problem here, only that there is no actionable statement of that problem, nor is there a proposal as to what to do about it.

    Your statements above, about this being alarming are all true, I can't deny that, but none of those statements point to a specific problem, or action to take on them, and thats what we need to drive issues to closure, which is what I'm hoping to discern here in my comment.

    Are you advocating for a radical redesign of the decoder process with the goal of simplifying its mechanics, and returning to a performance profile to that of earlier openssl releases? Thats a huge undertaking, but in fact something that can be considered, and likely something we should have a separate issue (ne epic, with lots of issues) for.

    Are you advocating for something more targeted? More targeted DER decoder handling? thats also great, but a statement to that effect would be appreciated, and something we can create a separate issue for.

    Are you advocating for something else? Also fine, its just unclear to me what that something is, and I would greatly appreciate clarification on that.

    In summary, This issue has identified, and fixed several problems (which is great, by the way), but without further concrete direction, I'm concerned this issue is going to continue to atrophy without resolution. So my ask to you is to identify what problem remains (and ideally make a suggestion as to what to do about it), so that we can plan what needs to be done.

    I know this feels a bit like bean counting, and I apologize for that, but I've got this goal to get our issue backlog to something more manageable, and to do that, I need to get issues refined to the point where they are actionable.

    I should note here as well, that this isn't the first issue of this nature. We have lots of issues that I would describe as 'general concerns' that result in variious fixes taking place, but no clear path to resolution. Perhaps it would be beneficial to create a label for something like 'investegatory' issues. Such issues could be opened, assigned to the creator, and researched at will, but themselves have no directly actionable tasks (instead driving the creation of other, truly actionable tasks). That might give you the freedom to reasearch and investigate farther reaching concerns on an independent time frame, while slicing off specific fixes/changes that could reasonably be planned for given releases. Thoughts?

  24. DDvO commented on Jun 11, 2024

    @DDvO
    ContributorAuthor

    Thank you for your detailed response.

    Yes, very good that this issue already sparked some improvements.

    You are right that (regular) issues should be actionable with some tangible results and this is nearly impossible here because the issue as given is too broad/superficial.

    I also have full understanding that issues like this are being questioned in the strive of cutting down the vast number of open issues.

    Unfortunately I do not know if and when I or others will have the time to investigate in more detail into the sources of the symptom mentioned here.
    Clearly those how are deeper into the specific implementations (such as DER stuff) would be better candidates to do so than me.

    I very much like the idea of introducing a new category for general issues like this. Other projects have similar categories, for instance Mozilla using the tag 'meta'.

  25. nhorman commented on Jun 11, 2024

    @nhorman
    Contributor

    Agreed, the more I think about it, the more I like the idea of a category of "not directly actionable" issues, that allow for general research and discussion, that can be ignored for planning purposes. Let me think about how best to do that, and I'll get it implemented

  26. nhorman commented on Jun 11, 2024

    @nhorman
    Contributor

    @DDvO #24608

    Created a new label and issue template for what we discussed above

  27. nhorman commented on Oct 3, 2024

    @nhorman
    Contributor

    The PR for the investigation template got shot down, suggesting this and issues like it should be discussions instead. Any objection to moving it?

  28. locked and limited conversation to collaborators on Feb 6, 2025
  29. converted this issue into a discussion #26659 on Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triaged: performancetriaged: refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions