]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Privatize the asn_codec_ctx_t into the ASN.1 code
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Wed, 15 May 2024 18:05:11 +0000 (12:05 -0600)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Wed, 15 May 2024 18:05:11 +0000 (12:05 -0600)
Fort has `--asn1-decode-max-stack`, a global configuration option for
the maximum stack usage. So there's no need to pass this as an argument.

12 files changed:
src/asn1/asn1c/ANY.c
src/asn1/asn1c/CMSAttribute.c
src/asn1/asn1c/ContentInfo.c
src/asn1/asn1c/EncapsulatedContentInfo.c
src/asn1/asn1c/asn_application.c
src/asn1/asn1c/asn_application.h
src/asn1/asn1c/asn_codecs.h
src/asn1/asn1c/asn_internal.h
src/asn1/asn1c/ber_decoder.c
src/asn1/asn1c/ber_decoder.h
src/asn1/decode.c
src/print_file.c

index a478f9df53750f61e7b0ee76510fd228c8c30898..cde0cf9e673ee01d3012801c2a22dc382e1fae35 100644 (file)
@@ -138,7 +138,7 @@ ANY_to_type(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
                return 0;
        }
 
-       rval = ber_decode(0, td, (void **)&newst, st->buf, st->size);
+       rval = ber_decode(td, (void **)&newst, st->buf, st->size);
        if(rval.code == RC_OK) {
                *struct_ptr = newst;
                return 0;
index 5a21ca1cdcdfd9092fad146e7c74547c23b8a695..262c6792d83f2727de627a94c26cc25a0942ef9a 100644 (file)
@@ -21,7 +21,7 @@ attr2json(asn_TYPE_descriptor_t const *td, CMSAttributeValue_t const *ber)
        json_t *json;
 
        attr = NULL;
-       rval = ber_decode(NULL, td, &attr, ber->buf, ber->size);
+       rval = ber_decode(td, &attr, ber->buf, ber->size);
 
        json = (rval.code == RC_OK) ? td->op->json_encoder(td, attr) : NULL;
 
index bb3faf353f3d52f4dcd8fcb035389f792d1fc7c6..55f554e181423296819a8cf2940d8f486c7aa2d1 100644 (file)
@@ -19,7 +19,7 @@ content2json(const asn_TYPE_descriptor_t *td, ANY_t const *ber)
        json_t *json;
 
        decoded = NULL;
-       rval = ber_decode(NULL, td, &decoded, ber->buf, ber->size);
+       rval = ber_decode(td, &decoded, ber->buf, ber->size);
 
        json = (rval.code == RC_OK) ? td->op->json_encoder(td, decoded) : NULL;
 
index 3c55149824e55f4ed632a2e3ee3409d6fd79c69d..54ae8c2f7b9786768afd63d3ee52103961d9aad5 100644 (file)
@@ -20,7 +20,7 @@ econtent2json(asn_TYPE_descriptor_t const *td, OCTET_STRING_t *eContent)
        json_t *json;
 
        decoded = NULL;
-       rval = ber_decode(NULL, td, &decoded, eContent->buf, eContent->size);
+       rval = ber_decode(td, &decoded, eContent->buf, eContent->size);
 
        json = (rval.code == RC_OK) ? td->op->json_encoder(td, decoded) : NULL;
 
index 1106160f67029788d868c44e5077b4b5e9c23f7f..469a0a484cdd49cd67e49c1f09921147387e6b67 100644 (file)
@@ -290,24 +290,3 @@ asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx,
 
     return er;
 }
-
-asn_dec_rval_t
-asn_decode(const asn_codec_ctx_t *opt_codec_ctx,
-           enum asn_transfer_syntax syntax, const asn_TYPE_descriptor_t *td,
-           void **sptr, const void *buffer, size_t size) {
-    if(!td || !td->op || !sptr || (size && !buffer)) {
-        ASN__DECODE_FAILED;
-    }
-
-    switch(syntax) {
-    case ATS_CER:
-    case ATS_NONSTANDARD_PLAINTEXT:
-    default:
-        errno = ENOENT;
-        ASN__DECODE_FAILED;
-
-    case ATS_DER:
-    case ATS_BER:
-        return ber_decode(opt_codec_ctx, td, sptr, buffer, size);
-    }
-}
index 8382ca80375c3e78b9b314cd28d9d4d89714b70f..e59ab43769b1a4f54e0770b3e9f5434337338791 100644 (file)
@@ -106,18 +106,6 @@ asn_enc_rval_t asn_encode(
     asn_app_consume_bytes_f *callback, void *callback_key);
 
 
-/*
- * A generic decoder for any supported transfer syntax.
- */
-asn_dec_rval_t asn_decode(
-    const asn_codec_ctx_t *opt_codec_parameters, enum asn_transfer_syntax,
-    const struct asn_TYPE_descriptor_s *type_to_decode,
-    void **structure_ptr, /* Pointer to a target structure's pointer */
-    const void *buffer,   /* Data to be decoded */
-    size_t size           /* Size of that buffer */
-);
-
-
 /*
  * A callback of this type is called whenever constraint validation fails
  * on some ASN.1 type. See "constraints.h" for more details on constraint
index b7f25d1e7e3b8c5a21f637a384e6a4033e03d951..0b3519abdc8b607fa8fd73f023aac1c4b3fe3bd3 100644 (file)
@@ -35,6 +35,24 @@ typedef struct asn_codec_ctx_s {
         * A value from getrlimit(RLIMIT_STACK) may be used to initialize
         * this variable. Be careful in multithreaded environments, as the
         * stack size is rather limited.
+        *
+        * 2024-05-15: None of the RPKI objects employ recursive structures, and
+        * they're all somewhat shallow. So this feature seems to be clutter.
+        *
+        * In my test environment, the highest effective ASN1 stack size
+        * reported in a full run was 1296. Fort defaults
+        * `--asn1-decode-max-stack` to 4096. The asn1c default recommended
+        * maximum was 30000. My soft getrlimit(RLIMIT_STACK) is 8192...
+        * I don't feel threatened by these numbers.
+        *
+        * Also, this seems naive. The "ASN1 stack size" isn't really comparable
+        * to RLIMIT_STACK to begin with.
+        *
+        * TODO (fine) Consider (from worst to best)
+        * - switching `--asn1-decode-max-stack` to getrlimit(RLIMIT_STACK),
+        * - drop this feature altogether,
+        * - or replace it with a gcc -fstack-usage analysis/monitor. (See
+        *   https://stackoverflow.com/a/74769668/1735458.)
         */
        size_t  max_stack_size; /* 0 disables stack bounds checking */
 } asn_codec_ctx_t;
index 2bc3d9b51e380ac6c24949461055de3ff84f3b51..ae8157f68a44d4dd066051cf5edc0196cdd5387c 100644 (file)
@@ -89,10 +89,6 @@ asn__format_to_callback(
                 return -1;                          \
     } while(0)
 
-/*
- * Check stack against overflow, if limit is set.
- */
-#define        ASN__DEFAULT_STACK_MAX  (30000)
 static int CC_NOTUSED
 ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) {
        if(ctx && ctx->max_stack_size) {
index a3d2797f5d6463bad4998107e8def4d011dae4e6..b7ba96dde8b4fa49e273616b2bb274afd0f9743b 100644 (file)
@@ -6,6 +6,7 @@
 #include <assert.h>
 
 #include "asn1/asn1c/asn_internal.h"
+#include "config.h"
 
 #undef ADVANCE
 #define        ADVANCE(num_bytes)      do {                                    \
  * The BER decoder of any type.
  */
 asn_dec_rval_t
-ber_decode(const asn_codec_ctx_t *opt_codec_ctx,
-           const asn_TYPE_descriptor_t *type_descriptor, void **struct_ptr,
-           const void *ptr, size_t size) {
-    asn_codec_ctx_t s_codec_ctx;
+ber_decode(const asn_TYPE_descriptor_t *type_descriptor, void **struct_ptr,
+           const void *ptr, size_t size)
+{
+       /* Needs to be allocated on the stack! */
+       asn_codec_ctx_t s_codec_ctx = { 0 };
 
-       /*
-        * Stack checker requires that the codec context
-        * must be allocated on the stack.
-        */
-       if(opt_codec_ctx) {
-               if(opt_codec_ctx->max_stack_size) {
-                       s_codec_ctx = *opt_codec_ctx;
-                       opt_codec_ctx = &s_codec_ctx;
-               }
-       } else {
-               /* If context is not given, be security-conscious anyway */
-               memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
-               s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
-               opt_codec_ctx = &s_codec_ctx;
-       }
+       s_codec_ctx.max_stack_size = config_get_asn1_decode_max_stack();
 
-       /*
-        * Invoke type-specific decoder.
-        */
-       return type_descriptor->op->ber_decoder(opt_codec_ctx, type_descriptor,
+       /* Invoke type-specific decoder. */
+       return type_descriptor->op->ber_decoder(&s_codec_ctx, type_descriptor,
                struct_ptr,     /* Pointer to the destination structure */
                ptr, size,      /* Buffer and its size */
                0               /* Default tag mode is 0 */
-               );
+       );
 }
 
 /*
index 64e2be57bf85703f78a798c36263f01edac1b763..72fb98b783d07c460e3da81274a24fa7d9aecc10 100644 (file)
@@ -19,7 +19,6 @@ struct asn_codec_ctx_s;               /* Forward declaration */
  * which is compliant with ber_decode().
  */
 asn_dec_rval_t ber_decode(
-    const struct asn_codec_ctx_s *opt_codec_ctx,
     const struct asn_TYPE_descriptor_s *type_descriptor,
     void **struct_ptr,  /* Pointer to a target structure's pointer */
     const void *buffer, /* Data to be decoded */
index 7b0a2bec05f6b22f8fcbe4929d99a73ad033d8c0..48abd16a0f29622842b403c288e959cff936aabe 100644 (file)
@@ -1,7 +1,6 @@
 #include "asn1/decode.h"
 
 #include "common.h"
-#include "config.h"
 #include "log.h"
 #include "incidence/incidence.h"
 
@@ -36,15 +35,12 @@ int
 asn1_decode(const void *buffer, size_t buffer_size,
     asn_TYPE_descriptor_t const *descriptor, void **result, bool log)
 {
-       asn_codec_ctx_t s_codec_ctx;
        asn_dec_rval_t rval;
        int error;
 
        *result = NULL;
-       s_codec_ctx.max_stack_size = config_get_asn1_decode_max_stack();
 
-       rval = ber_decode(&s_codec_ctx, descriptor, result, buffer,
-           buffer_size);
+       rval = ber_decode(descriptor, result, buffer, buffer_size);
        if (rval.code != RC_OK) {
                /* Must free partial object according to API contracts. */
                ASN_STRUCT_FREE(*descriptor, *result);
index 3a1cb1b3233d0738ce97f1a708d299336fe5611a..06ce54384e23c3d506c77681035c0d2028467ff9 100644 (file)
@@ -209,7 +209,7 @@ bio2ci(BIO *bio)
                        goto fail;
                }
 
-               res = ber_decode(NULL, &asn_DEF_ContentInfo, (void **)&ci,
+               res = ber_decode(&asn_DEF_ContentInfo, (void **)&ci,
                                 buffer, consumed);
                pr_op_debug("Consumed: %zu", res.consumed);