[PATCH 1/3] ASN.1: fix OPTIONAL for SEQUENCE OF and TYPE_REF

Timofei Novikov <[email protected]>
Newsgroups org.kernel.vger.keyrings,org.kernel.vger.linux-crypto
Message-ID <[email protected]>
Fix two issues with OPTIONAL fields:

1. For TYPE_REF fields that are OPTIONAL, propagate the ELEMENT_SKIPPABLE
   flag to the target type via a temporary tag element.  This ensures the
   target composite type generates MATCH_JUMP_OR_SKIP instead of MATCH.

2. OPTIONAL SEQUENCE/SET OF: set ELEMENT_SKIPPABLE on sequence wrapper,
   not just on parent field. This allows the entire sequence to be
   absent from the data.

Signed-off-by: Timofei Novikov <[email protected]>
---
 scripts/asn1_compiler.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/scripts/asn1_compiler.c b/scripts/asn1_compiler.c
index 4c3f645065a4..5b118acbdbeb 100644
--- a/scripts/asn1_compiler.c
+++ b/scripts/asn1_compiler.c
@@ -1039,6 +1039,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
 			if (cursor >= end)
 				goto overrun_error;
 			element->children = parse_type(&cursor, end, NULL);
+			element->flags |= element->children->flags & ELEMENT_SKIPPABLE;
+			element->children->flags &= ~ELEMENT_SKIPPABLE;
 		} else {
 			element->children = parse_compound(&cursor, end, 0);
 		}
@@ -1056,6 +1058,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
 			if (cursor >= end)
 				goto parse_error;
 			element->children = parse_type(&cursor, end, NULL);
+			element->flags |= element->children->flags & ELEMENT_SKIPPABLE;
+			element->children->flags &= ~ELEMENT_SKIPPABLE;
 		} else {
 			element->children = parse_compound(&cursor, end, 1);
 		}
@@ -1522,7 +1526,13 @@ static void render_element(FILE *out, struct element *e, struct element *tag)
 	/* Deal with compound types */
 	switch (e->compound) {
 	case TYPE_REF:
-		render_element(out, e->type->type->element, tag);
+		if (skippable && !tag) {
+			struct element tmp = { .flags = ELEMENT_SKIPPABLE };
+
+			render_element(out, e->type->type->element, &tmp);
+		} else {
+			render_element(out, e->type->type->element, tag);
+		}
 		if (e->action)
 			render_opcode(out, "ASN1_OP_%sACT,\n",
 				      skippable ? "MAYBE_" : "");
-- 
2.43.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.