@@ -24,15 +24,20 @@ static const unsigned char asn1_op_lengths[ASN1_OP__NR] = {
2424 [ASN1_OP_MATCH_JUMP ] = 1 + 1 + 1 ,
2525 [ASN1_OP_MATCH_JUMP_OR_SKIP ] = 1 + 1 + 1 ,
2626 [ASN1_OP_MATCH_ANY ] = 1 ,
27+ [ASN1_OP_MATCH_ANY_OR_SKIP ] = 1 ,
2728 [ASN1_OP_MATCH_ANY_ACT ] = 1 + 1 ,
29+ [ASN1_OP_MATCH_ANY_ACT_OR_SKIP ] = 1 + 1 ,
2830 [ASN1_OP_COND_MATCH_OR_SKIP ] = 1 + 1 ,
2931 [ASN1_OP_COND_MATCH_ACT_OR_SKIP ] = 1 + 1 + 1 ,
3032 [ASN1_OP_COND_MATCH_JUMP_OR_SKIP ] = 1 + 1 + 1 ,
3133 [ASN1_OP_COND_MATCH_ANY ] = 1 ,
34+ [ASN1_OP_COND_MATCH_ANY_OR_SKIP ] = 1 ,
3235 [ASN1_OP_COND_MATCH_ANY_ACT ] = 1 + 1 ,
36+ [ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP ] = 1 + 1 ,
3337 [ASN1_OP_COND_FAIL ] = 1 ,
3438 [ASN1_OP_COMPLETE ] = 1 ,
3539 [ASN1_OP_ACT ] = 1 + 1 ,
40+ [ASN1_OP_MAYBE_ACT ] = 1 + 1 ,
3641 [ASN1_OP_RETURN ] = 1 ,
3742 [ASN1_OP_END_SEQ ] = 1 ,
3843 [ASN1_OP_END_SEQ_OF ] = 1 + 1 ,
@@ -177,6 +182,7 @@ int asn1_ber_decoder(const struct asn1_decoder *decoder,
177182 unsigned char flags = 0 ;
178183#define FLAG_INDEFINITE_LENGTH 0x01
179184#define FLAG_MATCHED 0x02
185+ #define FLAG_LAST_MATCHED 0x04 /* Last tag matched */
180186#define FLAG_CONS 0x20 /* Corresponds to CONS bit in the opcode tag
181187 * - ie. whether or not we are going to parse
182188 * a compound type.
@@ -208,9 +214,9 @@ int asn1_ber_decoder(const struct asn1_decoder *decoder,
208214 unsigned char tmp ;
209215
210216 /* Skip conditional matches if possible */
211- if ((op & ASN1_OP_MATCH__COND &&
212- flags & FLAG_MATCHED ) ||
213- dp == datalen ) {
217+ if ((op & ASN1_OP_MATCH__COND && flags & FLAG_MATCHED ) ||
218+ ( op & ASN1_OP_MATCH__SKIP && dp == datalen )) {
219+ flags &= ~ FLAG_LAST_MATCHED ;
214220 pc += asn1_op_lengths [op ];
215221 goto next_op ;
216222 }
@@ -302,7 +308,9 @@ int asn1_ber_decoder(const struct asn1_decoder *decoder,
302308 /* Decide how to handle the operation */
303309 switch (op ) {
304310 case ASN1_OP_MATCH_ANY_ACT :
311+ case ASN1_OP_MATCH_ANY_ACT_OR_SKIP :
305312 case ASN1_OP_COND_MATCH_ANY_ACT :
313+ case ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP :
306314 ret = actions [machine [pc + 1 ]](context , hdr , tag , data + dp , len );
307315 if (ret < 0 )
308316 return ret ;
@@ -319,8 +327,10 @@ int asn1_ber_decoder(const struct asn1_decoder *decoder,
319327 case ASN1_OP_MATCH :
320328 case ASN1_OP_MATCH_OR_SKIP :
321329 case ASN1_OP_MATCH_ANY :
330+ case ASN1_OP_MATCH_ANY_OR_SKIP :
322331 case ASN1_OP_COND_MATCH_OR_SKIP :
323332 case ASN1_OP_COND_MATCH_ANY :
333+ case ASN1_OP_COND_MATCH_ANY_OR_SKIP :
324334 skip_data :
325335 if (!(flags & FLAG_CONS )) {
326336 if (flags & FLAG_INDEFINITE_LENGTH ) {
@@ -422,23 +432,32 @@ int asn1_ber_decoder(const struct asn1_decoder *decoder,
422432 pc += asn1_op_lengths [op ];
423433 goto next_op ;
424434
435+ case ASN1_OP_MAYBE_ACT :
436+ if (!(flags & FLAG_LAST_MATCHED )) {
437+ pc += asn1_op_lengths [op ];
438+ goto next_op ;
439+ }
425440 case ASN1_OP_ACT :
426441 ret = actions [machine [pc + 1 ]](context , hdr , tag , data + tdp , len );
442+ if (ret < 0 )
443+ return ret ;
427444 pc += asn1_op_lengths [op ];
428445 goto next_op ;
429446
430447 case ASN1_OP_RETURN :
431448 if (unlikely (jsp <= 0 ))
432449 goto jump_stack_underflow ;
433450 pc = jump_stack [-- jsp ];
451+ flags |= FLAG_MATCHED | FLAG_LAST_MATCHED ;
434452 goto next_op ;
435453
436454 default :
437455 break ;
438456 }
439457
440458 /* Shouldn't reach here */
441- pr_err ("ASN.1 decoder error: Found reserved opcode (%u)\n" , op );
459+ pr_err ("ASN.1 decoder error: Found reserved opcode (%u) pc=%zu\n" ,
460+ op , pc );
442461 return - EBADMSG ;
443462
444463data_overrun_error :
0 commit comments