coraza-proxy-wasm: GC Warning: Failed to expand heap

Hi there,

I’m receiving the following error:

│ 2023-10-12T06:07:26.021556Z    error    envoy wasm external/envoy/source/extensions/common/wasm/context.cc:1154    wasm log: GC Warning: Failed to expand heap by 131072 KiB    thread=27                        │
│ 2023-10-12T06:07:26.037565Z    error    envoy wasm external/envoy/source/extensions/common/wasm/context.cc:1154    wasm log: GC Warning: Failed to expand heap by 131072 KiB    thread=27                        │
│ 2023-10-12T06:07:26.053438Z    error    envoy wasm external/envoy/source/extensions/common/wasm/context.cc:1154    wasm log: GC Warning: Failed to expand heap by 131072 KiB    thread=27                        │
│ 2023-10-12T06:07:26.053456Z    error    envoy wasm external/envoy/source/extensions/common/wasm/context.cc:1154    wasm log: GC Warning: Failed to expand heap by 192 KiB    thread=27                           │
│ 2023-10-12T06:07:26.053459Z    error    envoy wasm external/envoy/source/extensions/common/wasm/context.cc:1154    wasm log: GC Warning: Out of Memory! Heap size: 706 MiB. Returning NULL!    thread=27         │
│ 2023-10-12T06:07:26.053577Z    error    envoy wasm external/envoy/source/extensions/common/wasm/wasm_vm.cc:38    Function: proxy_on_configure failed: Uncaught RuntimeError: unreachable                         │
│ Proxy-Wasm plugin in-VM backtrace:                                                                                                                                                                               │
│   0:  0xa4150 - runtime._panic                                                                                                                                                                                   │
│   1:  0x171ceb - (*github.com/corazawaf/coraza/v3/internal/corazawaf.RuleGroup).Add                                                                                                                              │
│   2:  0x1cee18 - github.com/corazawaf/coraza/v3/internal/seclang.directiveSecRule                                                                                                                                │
│   3:  0x1d4760 - (*github.com/corazawaf/coraza/v3/internal/seclang.Parser).FromString                                                                                                                            │
│   4:  0x1d3e93 - (*github.com/corazawaf/coraza/v3/internal/seclang.Parser).FromFile                                                                                                                              │
│   5:  0x1d48b2 - (*github.com/corazawaf/coraza/v3/internal/seclang.Parser).FromString                                                                                                                            │
│   6:  0x1da72f - proxy_on_configure    thread=27                                                                                                                                                                 │
│ 2023-10-12T06:07:26.053589Z    error    envoy wasm external/envoy/source/extensions/common/wasm/wasm.cc:109    Wasm VM failed Failed to configure thread-local Wasm plugin    thread=27

I already pumped up the istio pods to 4 GB (instead of 1 GB) but still the same error.

Do you have any idea how to troubleshoot that?

Rules are as follows:

spec:
  phase: AUTHN
  pluginConfig:
    default_directives: default
    directives_map:
      blockAll:
        - Include @recommended-conf
        - SecRuleEngine On
        - Include @crs-setup-conf
        - Include @owasp_crs/*.conf
        - >-
          SecRule REQUEST_HEADERS:user-agent "@contains firefox" "id:1, phase:1,
          deny, log, logdata:'firefox forbidden'"
        - >-
          SecRule REQUEST_HEADERS:user-agent "@contains Firefox" "id:3, phase:1,
          deny, log, logdata:'firefox forbidden'"
        - >-
          SecRule REQUEST_URI "@beginsWith /admin" "id:2, phase:1, deny, log,
          logdata:'admin forbidden'"
      default:
        - Include @recommended-conf
        - SecRuleEngine DetectionOnly
        - Include @crs-setup-conf
        - Include @owasp_crs/*.conf
    per_authority_directives:
      special.domain.com: blockAll

I’m running Istio 1.18.*

We build our own plugin because we include additional files. And it looks like that’s related to the embedded files being too big. As soon as I remove the files, it works as expected.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

Thanks for the investigation @M4tteoP! Yeah this does confirm a suspicion I have had - random data will cause issues for the GC. bdwgc is by default a conservative GC and relies on finding values in memory that look like pointers to identify what to keep. Random data looks like pointers in 32-bits, which Wasm is (it’s funny seeing the Wasm is the next compute hype when we see it taking us back 10 years 😛). Generally random data within request parsing is not as big of a deal since the buffers themselves eventually get released and stop causing any false positives, but embedded data is there forever and holding up GC of pointers that look like those values.

For reference, bdwgc has this note on 2GB heaps on 32-bit machines, while I don’t know if we’re running close to that in these cases, we are definitely using a reasonably high amount of memory on a 32-bit machine

https://github.com/ivmai/bdwgc/blob/806537be2dec4f49056cb2fe091ac7f7d78728a8/docs/faq.md?plain=1#L84

TinyGo with conservative GC will probably have similar issues but may run into them less by having more knowledge of the way it lays out memory while bdwgc is treating it as a complete black box. If it works for you, it could be a workaround for now, but just for reference, when I try the patch to use default GC and run go run mage.go ftw, luckily it doesn’t seem to OOM, but there are obvious 1-2s GC pauses throughout the run, with the total taking 90s vs 40, the majority of that time is probably GC pauses. Something to keep in mind if trying that out in production.

bdwgc does have some support for precise GC which shouldn’t have these issues, but both it and TinyGo’s related functionality for it aren’t that well documented and I was having trouble using it when experimenting once. I wasn’t sure it would have a real benefit at the time so stopped exploring it, but now seeing these cases, I think I’ll try again but can’t guarantee succeeding anytime soon if at all.

https://github.com/ivmai/bdwgc/blob/806537be2dec4f49056cb2fe091ac7f7d78728a8/include/gc/gc_typed.h#L54

Would be interested in a PR which adds GeoIP support (using an empty file for the public) which could be easily extended by dropping the database file into the repo for self compilation?

I think that would be great! Being something that requires a self-compilation I would make it opt-in, maybe with a dedicated build tag and some documented steps in the readme that could be something like adding your own GeoIP database and enabling the tag.

@M4tteoP Thank you, absolutely!

Sizes without the additional file: Bildschirmfoto 2023-10-12 um 12 07 38

Sizes with the additional file (so approximately 6MB more) Bildschirmfoto 2023-10-12 um 12 07 30

So to be fully transparent about what I’m doing: I’m trying to get geoip based blocking to work in this wasm.

Therefore, I created a custom branch from the extended plugin: https://github.com/corazawaf/coraza-geoip/compare/master...woehrl01:coraza-geoip:support_v3_4

And I add the loading of the database as an //go:embed and call that method RegisterGeoDatabase from the main.go

By purely adding the 6MB database as //go:embed (without loading the plugin) the booting of the module fails, with the mentioned error.

By switching to the “default” gc, everything works and the blocking also works based on the geoip.