Skip to content

Commit

Permalink
fr_pair_to_unknown() -> fr_pair_raw_from_pair()
Browse files Browse the repository at this point in the history
because all of the callers are creating a raw pair from malformed
data, and aren't creating an unknown pair.
  • Loading branch information
alandekok committed Sep 8, 2023
1 parent e5ea4ab commit 2839779
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
22 changes: 13 additions & 9 deletions src/lib/util/pair.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,36 +651,40 @@ int fr_pair_steal_prepend(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t
return 0;
}

/** Mark malformed or unrecognised attributed as unknown
/** Mark malformed attribute as raw
*
* @param[in] vp to mark as raw.
* @param[in] data to parse.
* @param[in] data_len of data to parse.
*
* @param vp to change fr_dict_attr_t of.
* @return
* - 0 on success (or if already unknown).
* - 0 on success
* - -1 on failure.
*/
int fr_pair_to_unknown(fr_pair_t *vp)
int fr_pair_raw_from_pair(fr_pair_t *vp, uint8_t const *data, size_t data_len)
{
fr_dict_attr_t *unknown;

PAIR_VERIFY(vp);

if (vp->da->flags.is_unknown) return 0;
if (!fr_cond_assert(vp->da->flags.is_unknown == false)) return -1;

if (!fr_cond_assert(vp->da->parent != NULL)) return -1;

unknown = fr_dict_unknown_afrom_da(vp, vp->da);
if (!unknown) return -1;
unknown->flags.is_raw = 1;

fr_dict_unknown_free(&vp->da); /* Only frees unknown attributes */
vp->da = unknown;
fr_assert(vp->da->type == FR_TYPE_OCTETS);

fr_value_box_init(&vp->data, FR_TYPE_OCTETS, NULL, true);

fr_pair_value_memdup(vp, data, data_len, true);

return 0;
}


/** Iterate over pairs with a specified da
*
* @param[in] list to iterate over.
Expand Down Expand Up @@ -2414,7 +2418,7 @@ int fr_pair_value_copy(fr_pair_t *dst, fr_pair_t *src)
{
if (!fr_cond_assert(src->data.type != FR_TYPE_NULL)) return -1;

if (dst->data.type != FR_TYPE_NULL) fr_value_box_clear(&dst->data);
if (dst->data.type != FR_TYPE_NULL) fr_value_box_clear_value(&dst->data);
fr_value_box_copy(dst, &dst->data, &src->data);

/*
Expand Down Expand Up @@ -2840,7 +2844,7 @@ int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tai

if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;

fr_value_box_clear(&vp->data); /* Free any existing buffers */
fr_value_box_clear_value(&vp->data); /* Free any existing buffers */
ret = fr_value_box_memdup(vp, &vp->data, vp->da, src, len, tainted);
if (ret == 0) PAIR_VERIFY(vp);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/util/pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ int fr_pair_steal_append(TALLOC_CTX *nctx, fr_pair_list_t *list, fr_pair_t *vp)
int fr_pair_steal_prepend(TALLOC_CTX *nctx, fr_pair_list_t *list, fr_pair_t *vp) CC_HINT(nonnull);

/* Searching and list modification */
int fr_pair_to_unknown(fr_pair_t *vp) CC_HINT(nonnull);
int fr_pair_raw_from_pair(fr_pair_t *vp, uint8_t const *data, size_t data_len) CC_HINT(nonnull);

bool fr_pair_matches_da(void const *item, void const *uctx) CC_HINT(nonnull);

Expand Down
11 changes: 6 additions & 5 deletions src/lib/util/pair_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,10 @@ static void test_fr_pair_steal(void)
TEST_CHECK(talloc_parent(vp) == autofree);
}

static void test_fr_pair_to_unknown(void)
static void test_fr_pair_raw_from_pair(void)
{
fr_pair_t *vp;
uint8_t value = 0;

TEST_CASE("Allocate a new attribute fr_pair_afrom_da");
TEST_CHECK((vp = fr_pair_afrom_da(autofree, fr_dict_attr_test_octets)) != NULL);
Expand All @@ -209,10 +210,10 @@ static void test_fr_pair_to_unknown(void)
PAIR_VERIFY(vp);

TEST_CASE("Converting regular 'vp' as unkown");
TEST_CHECK(fr_pair_to_unknown(vp) == 0);
TEST_CHECK(fr_pair_raw_from_pair(vp, &value, 1) == 0);

TEST_CASE("Checking if a real 'unkown' vp");
TEST_CHECK(vp && vp->da->flags.is_unknown == true);
TEST_CASE("Checking if a real 'raw' vp");
TEST_CHECK(vp && vp->vp_raw);
}

static void test_fr_pair_dcursor_by_da_init(void)
Expand Down Expand Up @@ -1393,7 +1394,7 @@ TEST_LIST = {
{ "fr_dcursor_iter_by_da_init", test_fr_pair_dcursor_by_da_init },
{ "fr_pair_dcursor_by_ancestor_init", test_fr_pair_dcursor_by_ancestor_init },
{ "fr_pair_dcursor_value_init", test_fr_pair_dcursor_value_init },
{ "fr_pair_to_unknown", test_fr_pair_to_unknown },
{ "fr_pair_raw_from_pair", test_fr_pair_raw_from_pair },
{ "fr_pair_find_by_da_idx", test_fr_pair_find_by_da_idx },
{ "fr_pair_find_by_child_num_idx", test_fr_pair_find_by_child_num_idx },
{ "fr_pair_find_by_da_nested", test_fr_pair_find_by_da_nested },
Expand Down
3 changes: 1 addition & 2 deletions src/modules/rlm_eap/types/rlm_eap_fast/eap_fast.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,7 @@ ssize_t eap_fast_decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_
ret = fr_value_box_from_network(vp, &vp->data, vp->vp_type, vp->da,
&FR_DBUFF_TMP(p, (size_t)len), len, true);
if (ret != len) {
fr_pair_to_unknown(vp);
fr_pair_value_memdup(vp, p, len, true);
fr_pair_raw_from_pair(vp, p, len);
}
fr_pair_append(out, vp);
p += len;
Expand Down
3 changes: 1 addition & 2 deletions src/modules/rlm_eap/types/rlm_eap_ttls/ttls.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@ static ssize_t eap_ttls_decode_pair(request_t *request, TALLOC_CTX *ctx, fr_dcur
goto error;
}

fr_pair_to_unknown(vp);
fr_pair_value_memdup(vp, p, value_len, true);
fr_pair_raw_from_pair(vp, p, value_len);
}

/*
Expand Down
7 changes: 4 additions & 3 deletions src/protocols/dhcpv4/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,13 @@ static ssize_t decode_value(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t

default:
slen = fr_value_box_from_network(vp, &vp->data, vp->vp_type, da,
&FR_DBUFF_TMP(p, end - p), end - p, true);
&FR_DBUFF_TMP(p, end - p), end - p, true);
if (slen < 0) {
raw:
FR_PROTO_TRACE("decoding as unknown type");
if (fr_pair_to_unknown(vp) < 0) return -1;
fr_pair_value_memdup(vp, p, end - p, true);
if (fr_pair_raw_from_pair(vp, p, (end - p)) < 0) {
return -1;
}
p = end;
break;
}
Expand Down

0 comments on commit 2839779

Please sign in to comment.