net: lwm2m: Do not enforce canonical CBOR decoding

Our decoder can handle decoding of non-deterministic CBOR just fine.
There is no need to block valid CBOR if the server does not produce
length-first deterministic CBOR.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
This commit is contained in:
Seppo Takalo 2025-02-20 13:33:25 +02:00 committed by Benjamin Cabé
commit 78ee0f0c23
2 changed files with 20 additions and 0 deletions

View file

@ -237,6 +237,8 @@ static int get_s64(struct lwm2m_input_context *in, int64_t *value)
{ {
ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0); ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0);
states->constant_state->enforce_canonical = false;
if (!zcbor_int64_decode(states, value)) { if (!zcbor_int64_decode(states, value)) {
LOG_WRN("unable to decode a 64-bit integer value"); LOG_WRN("unable to decode a 64-bit integer value");
return -EBADMSG; return -EBADMSG;
@ -253,6 +255,8 @@ static int get_s32(struct lwm2m_input_context *in, int32_t *value)
{ {
ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0); ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0);
states->constant_state->enforce_canonical = false;
if (!zcbor_int32_decode(states, value)) { if (!zcbor_int32_decode(states, value)) {
LOG_WRN("unable to decode a 32-bit integer value, err: %d", LOG_WRN("unable to decode a 32-bit integer value, err: %d",
states->constant_state->error); states->constant_state->error);
@ -270,6 +274,8 @@ static int get_float(struct lwm2m_input_context *in, double *value)
{ {
ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0); ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0);
states->constant_state->enforce_canonical = false;
if (!zcbor_float_decode(states, value)) { if (!zcbor_float_decode(states, value)) {
LOG_ERR("unable to decode a floating-point value"); LOG_ERR("unable to decode a floating-point value");
return -EBADMSG; return -EBADMSG;
@ -289,6 +295,8 @@ static int get_string(struct lwm2m_input_context *in, uint8_t *value, size_t buf
ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0); ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0);
states->constant_state->enforce_canonical = false;
if (!zcbor_tstr_decode(states, &hndl)) { if (!zcbor_tstr_decode(states, &hndl)) {
LOG_WRN("unable to decode a string"); LOG_WRN("unable to decode a string");
return -EBADMSG; return -EBADMSG;
@ -318,6 +326,8 @@ static int get_time_string(struct lwm2m_input_context *in, int64_t *value)
ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0); ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0);
states->constant_state->enforce_canonical = false;
if (!zcbor_tstr_decode(states, &hndl)) { if (!zcbor_tstr_decode(states, &hndl)) {
return -EBADMSG; return -EBADMSG;
} }
@ -336,6 +346,8 @@ static int get_time_numerical(struct lwm2m_input_context *in, int64_t *value)
{ {
ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0); ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0);
states->constant_state->enforce_canonical = false;
if (!zcbor_int64_decode(states, value)) { if (!zcbor_int64_decode(states, value)) {
LOG_WRN("unable to decode seconds since Epoch"); LOG_WRN("unable to decode seconds since Epoch");
return -EBADMSG; return -EBADMSG;
@ -355,6 +367,8 @@ static int get_time(struct lwm2m_input_context *in, time_t *value)
ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0); ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0);
states->constant_state->enforce_canonical = false;
success = zcbor_tag_decode(states, &tag); success = zcbor_tag_decode(states, &tag);
if (success) { if (success) {
@ -405,6 +419,8 @@ static int get_bool(struct lwm2m_input_context *in, bool *value)
{ {
ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0); ZCBOR_STATE_D(states, 0, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0);
states->constant_state->enforce_canonical = false;
if (!zcbor_bool_decode(states, value)) { if (!zcbor_bool_decode(states, value)) {
LOG_WRN("unable to decode a boolean value"); LOG_WRN("unable to decode a boolean value");
return -EBADMSG; return -EBADMSG;
@ -425,6 +441,8 @@ static int get_opaque(struct lwm2m_input_context *in, uint8_t *value, size_t buf
ZCBOR_STATE_D(states, 1, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0); ZCBOR_STATE_D(states, 1, ICTX_BUF_R_PTR(in), ICTX_BUF_R_LEFT_SZ(in), 1, 0);
states->constant_state->enforce_canonical = false;
/* Get the CBOR header only on first read. */ /* Get the CBOR header only on first read. */
if (opaque->offset == 0) { if (opaque->offset == 0) {
ret = zcbor_bstr_start_decode_fragment(states, &hndl); ret = zcbor_bstr_start_decode_fragment(states, &hndl);

View file

@ -212,6 +212,8 @@ static bool decode_lwm2m_senml(zcbor_state_t *state, struct lwm2m_senml *result)
{ {
zcbor_log("%s\r\n", __func__); zcbor_log("%s\r\n", __func__);
state->constant_state->enforce_canonical = false;
bool res = bool res =
(((zcbor_list_start_decode(state) && (((zcbor_list_start_decode(state) &&
((zcbor_multi_decode( ((zcbor_multi_decode(