From 78c284e05d9cbb7575b1b55ab6fef48c8ffcfb30 Mon Sep 17 00:00:00 2001 From: headshog Date: Tue, 11 Jul 2023 18:44:31 +0300 Subject: [PATCH 1/3] fix numtrunc in str2wire.c --- sldns/rrdef.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/sldns/rrdef.c b/sldns/rrdef.c index 322eff096..130324a1f 100644 --- a/sldns/rrdef.c +++ b/sldns/rrdef.c @@ -702,7 +702,11 @@ sldns_get_rr_type_by_name(const char *name) /* TYPEXX representation */ if (strlen(name) > 4 && strncasecmp(name, "TYPE", 4) == 0) { - return atoi(name + 4); + unsigned int a = atoi(name + 4); + if (a > LDNS_RR_TYPE_LAST) { + return (enum sldns_enum_rr_type)0; + } + return a; } /* Normal types */ @@ -740,7 +744,11 @@ sldns_get_rr_class_by_name(const char *name) /* CLASSXX representation */ if (strlen(name) > 5 && strncasecmp(name, "CLASS", 5) == 0) { - return atoi(name + 5); + unsigned int a = atoi(name + 5); + if (a > LDNS_RR_TYPE_LAST) { + return (enum sldns_enum_rr_type)0; + } + return a; } /* Normal types */ From 0b131d5a317e7fd531c1ba04a7fdbdb5210857ca Mon Sep 17 00:00:00 2001 From: headshog Date: Wed, 19 Jul 2023 18:09:03 +0300 Subject: [PATCH 2/3] parse sldns_get_rr_class_by_name and sldns_get_rr_type_by_name return value 0 --- daemon/remote.c | 3 +++ sldns/rrdef.c | 2 +- testcode/dohclient.c | 4 ++++ testcode/perf.c | 8 ++++++++ testcode/streamtcp.c | 4 ++++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/daemon/remote.c b/daemon/remote.c index d89ecd165..f27111fee 100644 --- a/daemon/remote.c +++ b/daemon/remote.c @@ -1581,6 +1581,9 @@ do_flush_type(RES* ssl, struct worker* worker, char* arg) if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) return; t = sldns_get_rr_type_by_name(arg2); + if(t == 0 && strcmp(arg2, "TYPE0") != 0) { + return 0; + } do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN); free(nm); diff --git a/sldns/rrdef.c b/sldns/rrdef.c index 130324a1f..55ea2d922 100644 --- a/sldns/rrdef.c +++ b/sldns/rrdef.c @@ -746,7 +746,7 @@ sldns_get_rr_class_by_name(const char *name) if (strlen(name) > 5 && strncasecmp(name, "CLASS", 5) == 0) { unsigned int a = atoi(name + 5); if (a > LDNS_RR_TYPE_LAST) { - return (enum sldns_enum_rr_type)0; + return (enum sldns_enum_rr_class)0; } return a; } diff --git a/testcode/dohclient.c b/testcode/dohclient.c index 64af699bc..2ee3be8e5 100644 --- a/testcode/dohclient.c +++ b/testcode/dohclient.c @@ -229,6 +229,10 @@ make_query(char* qname, char* qtype, char* qclass) qinfo.qtype = sldns_get_rr_type_by_name(qtype); qinfo.qclass = sldns_get_rr_class_by_name(qclass); + if((qinfo.qtype == 0 && strcmp(qtype, "TYPE0") != 0) || + (qinfo.qclass == 0 && strcmp(qclass, "CLASS0") != 0)) { + return 0; + } qinfo.local_alias = NULL; qinfo_query_encode(buf, &qinfo); /* flips buffer */ diff --git a/testcode/perf.c b/testcode/perf.c index 7fb524e22..5a4b39491 100644 --- a/testcode/perf.c +++ b/testcode/perf.c @@ -458,9 +458,17 @@ qlist_parse_line(sldns_buffer* buf, char* p) if(strcmp(tp, "IN") == 0 || strcmp(tp, "CH") == 0) { qinfo.qtype = sldns_get_rr_type_by_name(cl); qinfo.qclass = sldns_get_rr_class_by_name(tp); + if((qinfo.qtype == 0 && strcmp(cl, "TYPE0") != 0) || + (qinfo.qclass == 0 && strcmp(tp, "CLASS0") != 0)) { + return 0; + } } else { qinfo.qtype = sldns_get_rr_type_by_name(tp); qinfo.qclass = sldns_get_rr_class_by_name(cl); + if((qinfo.qtype == 0 && strcmp(tp, "TYPE0") != 0) || + (qinfo.qclass == 0 && strcmp(cl, "CLASS0") != 0)) { + return 0; + } } if(fl[0] == '+') rec = 1; else if(fl[0] == '-') rec = 0; diff --git a/testcode/streamtcp.c b/testcode/streamtcp.c index b2c0d5328..5e54894a6 100644 --- a/testcode/streamtcp.c +++ b/testcode/streamtcp.c @@ -133,6 +133,10 @@ write_q(int fd, int udp, SSL* ssl, sldns_buffer* buf, uint16_t id, /* qtype and qclass */ qinfo.qtype = sldns_get_rr_type_by_name(strtype); qinfo.qclass = sldns_get_rr_class_by_name(strclass); + if((qinfo.qtype == 0 && strcmp(strtype, "TYPE0") != 0) || + (qinfo.qclass == 0 && strcmp(strclass, "CLASS0") != 0)) { + return 0; + } /* clear local alias */ qinfo.local_alias = NULL; From 5b7faca7dba21c86a9bcbeed37565990963d2ade Mon Sep 17 00:00:00 2001 From: George Thessalonikefs Date: Thu, 20 Jul 2023 11:42:05 +0200 Subject: [PATCH 3/3] For #909: Numeric truncation when parsing TYPEXX and CLASSXX representation - Fix return values. - Formatting nits. --- daemon/remote.c | 2 +- testcode/dohclient.c | 11 +++++++---- testcode/perf.c | 4 ++-- testcode/streamtcp.c | 10 +++++++--- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/daemon/remote.c b/daemon/remote.c index f27111fee..c7bfa4e12 100644 --- a/daemon/remote.c +++ b/daemon/remote.c @@ -1582,7 +1582,7 @@ do_flush_type(RES* ssl, struct worker* worker, char* arg) return; t = sldns_get_rr_type_by_name(arg2); if(t == 0 && strcmp(arg2, "TYPE0") != 0) { - return 0; + return; } do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN); diff --git a/testcode/dohclient.c b/testcode/dohclient.c index 2ee3be8e5..de9f39d7d 100644 --- a/testcode/dohclient.c +++ b/testcode/dohclient.c @@ -226,12 +226,15 @@ make_query(char* qname, char* qtype, char* qclass) printf("cannot parse query name: '%s'\n", qname); exit(1); } - qinfo.qtype = sldns_get_rr_type_by_name(qtype); + if(qinfo.qtype == 0 && strcmp(qtype, "TYPE0") != 0) { + printf("cannot parse query type: '%s'\n", qtype); + exit(1); + } qinfo.qclass = sldns_get_rr_class_by_name(qclass); - if((qinfo.qtype == 0 && strcmp(qtype, "TYPE0") != 0) || - (qinfo.qclass == 0 && strcmp(qclass, "CLASS0") != 0)) { - return 0; + if(qinfo.qclass == 0 && strcmp(qclass, "CLASS0") != 0) { + printf("cannot parse query class: '%s'\n", qclass); + exit(1); } qinfo.local_alias = NULL; diff --git a/testcode/perf.c b/testcode/perf.c index 5a4b39491..2be86c4bf 100644 --- a/testcode/perf.c +++ b/testcode/perf.c @@ -459,14 +459,14 @@ qlist_parse_line(sldns_buffer* buf, char* p) qinfo.qtype = sldns_get_rr_type_by_name(cl); qinfo.qclass = sldns_get_rr_class_by_name(tp); if((qinfo.qtype == 0 && strcmp(cl, "TYPE0") != 0) || - (qinfo.qclass == 0 && strcmp(tp, "CLASS0") != 0)) { + (qinfo.qclass == 0 && strcmp(tp, "CLASS0") != 0)) { return 0; } } else { qinfo.qtype = sldns_get_rr_type_by_name(tp); qinfo.qclass = sldns_get_rr_class_by_name(cl); if((qinfo.qtype == 0 && strcmp(tp, "TYPE0") != 0) || - (qinfo.qclass == 0 && strcmp(cl, "CLASS0") != 0)) { + (qinfo.qclass == 0 && strcmp(cl, "CLASS0") != 0)) { return 0; } } diff --git a/testcode/streamtcp.c b/testcode/streamtcp.c index 5e54894a6..4eee14187 100644 --- a/testcode/streamtcp.c +++ b/testcode/streamtcp.c @@ -132,10 +132,14 @@ write_q(int fd, int udp, SSL* ssl, sldns_buffer* buf, uint16_t id, /* qtype and qclass */ qinfo.qtype = sldns_get_rr_type_by_name(strtype); + if(qinfo.qtype == 0 && strcmp(strtype, "TYPE0") != 0) { + printf("cannot parse query type: '%s'\n", strtype); + exit(1); + } qinfo.qclass = sldns_get_rr_class_by_name(strclass); - if((qinfo.qtype == 0 && strcmp(strtype, "TYPE0") != 0) || - (qinfo.qclass == 0 && strcmp(strclass, "CLASS0") != 0)) { - return 0; + if(qinfo.qclass == 0 && strcmp(strclass, "CLASS0") != 0) { + printf("cannot parse query class: '%s'\n", strclass); + exit(1); } /* clear local alias */