From: Arran Cudbard-Bell Date: Sun, 24 May 2020 18:18:41 +0000 (-0500) Subject: value: Round out assignment API X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8836245263648247aee181ff9241aaf429c05ab4;p=thirdparty%2Ffreeradius-server.git value: Round out assignment API --- diff --git a/src/lib/eap_aka_sim/state_machine.c b/src/lib/eap_aka_sim/state_machine.c index 9a568e831a2..f67f96c9118 100644 --- a/src/lib/eap_aka_sim/state_machine.c +++ b/src/lib/eap_aka_sim/state_machine.c @@ -552,7 +552,7 @@ static rlm_rcode_t pseudonym_store_resume(void *instance, UNUSED void *thread, R fr_rand_str((uint8_t *)identity + 1, inst->ephemeral_id_length, 'a'); identity[talloc_array_length(identity) - 1] = '\0'; - fr_value_box_strdup_buffer_shallow(NULL, &vp->data, NULL, identity, false); + fr_value_box_bstrdup_buffer_shallow(NULL, &vp->data, NULL, identity, false); } pair_update_request(&new, attr_session_id); fr_pair_value_memdup(new, (uint8_t const *)vp->vp_strvalue, vp->vp_length, vp->vp_tainted); @@ -678,7 +678,7 @@ static rlm_rcode_t session_and_pseudonym_store(eap_aka_sim_common_conf_t *inst, break; } identity[talloc_array_length(identity) - 1] = '\0'; - fr_value_box_strdup_buffer_shallow(NULL, &vp->data, NULL, identity, false); + fr_value_box_bstrdup_buffer_shallow(NULL, &vp->data, NULL, identity, false); } pair_update_request(&new, attr_eap_aka_sim_next_pseudonym); fr_pair_value_copy(new, vp); @@ -775,7 +775,7 @@ static rlm_rcode_t session_and_pseudonym_clear(eap_aka_sim_common_conf_t *inst, VALUE_PAIR *vp; MEM(pair_update_request(&vp, attr_eap_aka_sim_next_pseudonym) >= 0); - fr_value_box_strdup_buffer(vp, &vp->data, NULL, eap_aka_sim_session->pseudonym_sent, true); + fr_value_box_bstrdup_buffer(vp, &vp->data, NULL, eap_aka_sim_session->pseudonym_sent, true); TALLOC_FREE(eap_aka_sim_session->pseudonym_sent); return unlang_module_yield_to_section(request, diff --git a/src/lib/server/tmpl.c b/src/lib/server/tmpl.c index 1fa18950f75..f957a6c94b2 100644 --- a/src/lib/server/tmpl.c +++ b/src/lib/server/tmpl.c @@ -1691,7 +1691,7 @@ ssize_t tmpl_afrom_str(TALLOC_CTX *ctx, vp_tmpl_t **out, binlen = (inlen - 2) / 2; vpt = tmpl_alloc(ctx, TMPL_TYPE_DATA, in, inlen, type); - (void)fr_value_box_memalloc(vpt, &bin, &vpt->data.literal, NULL, binlen, false); + (void)fr_value_box_mem_alloc(vpt, &bin, &vpt->data.literal, NULL, binlen, false); len = fr_hex2bin(bin, binlen, in + 2, inlen - 2); if (len != binlen) { fr_strerror_printf("Hex string contains non-hex char"); @@ -2455,7 +2455,7 @@ ssize_t _tmpl_to_atype(TALLOC_CTX *ctx, void *out, RDEBUG4("EXPAND TMPL EXEC"); - fr_value_box_stralloc(tmp_ctx, &buff, &value, NULL, 1024, true); + fr_value_box_bstr_alloc(tmp_ctx, &buff, &value, NULL, 1024, true); if (radius_exec_program(request, buff, 1024, NULL, request, vpt->name, NULL, true, false, fr_time_delta_from_sec(EXEC_TIMEOUT)) != 0) { error: diff --git a/src/lib/unlang/xlat_builtin.c b/src/lib/unlang/xlat_builtin.c index 852a72233cb..5964ea3ff65 100644 --- a/src/lib/unlang/xlat_builtin.c +++ b/src/lib/unlang/xlat_builtin.c @@ -3024,7 +3024,7 @@ static xlat_action_t xlat_func_sub(TALLOC_CTX *ctx, fr_cursor_t *out, p = q + pattern_len; } - if (fr_value_box_strdup_buffer_shallow(vb, vb, NULL, vb_str, (*in)->vb_strvalue) < 0) { + if (fr_value_box_bstrdup_buffer_shallow(vb, vb, NULL, vb_str, (*in)->vb_strvalue) < 0) { RPEDEBUG("Failed creating output box"); talloc_free(vb); return XLAT_ACTION_FAIL; diff --git a/src/lib/unlang/xlat_eval.c b/src/lib/unlang/xlat_eval.c index 5512a24d640..3afc6205bf6 100644 --- a/src/lib/unlang/xlat_eval.c +++ b/src/lib/unlang/xlat_eval.c @@ -448,7 +448,7 @@ static xlat_action_t xlat_eval_pair_virtual(TALLOC_CTX *ctx, fr_cursor_t *out, R if (!request->client || !request->client->shortname) return XLAT_ACTION_DONE; MEM(value = fr_value_box_alloc_null(ctx)); - if (fr_value_box_strdup_buffer(ctx, value, tmpl_da(vpt), request->client->shortname, false) < 0) { + if (fr_value_box_bstrdup_buffer(ctx, value, tmpl_da(vpt), request->client->shortname, false) < 0) { error: talloc_free(value); return XLAT_ACTION_FAIL; @@ -458,13 +458,13 @@ static xlat_action_t xlat_eval_pair_virtual(TALLOC_CTX *ctx, fr_cursor_t *out, R if (!request->component) return XLAT_ACTION_DONE; MEM(value = fr_value_box_alloc_null(ctx)); - if (fr_value_box_strdup_buffer(ctx, value, tmpl_da(vpt), request->component, false) < 0) goto error; + if (fr_value_box_bstrdup_buffer(ctx, value, tmpl_da(vpt), request->component, false) < 0) goto error; goto done; } else if (tmpl_da(vpt) == attr_virtual_server) { if (!request->server_cs) return XLAT_ACTION_DONE; MEM(value = fr_value_box_alloc_null(ctx)); - if (fr_value_box_strdup_buffer(ctx, value, tmpl_da(vpt), + if (fr_value_box_bstrdup_buffer(ctx, value, tmpl_da(vpt), cf_section_name2(request->server_cs), false) < 0) goto error; goto done; } else if (tmpl_da(vpt) == attr_module_return_code) { @@ -1011,7 +1011,7 @@ xlat_action_t xlat_frame_eval(TALLOC_CTX *ctx, fr_cursor_t *out, xlat_exp_t cons * because references aren't threadsafe. */ MEM(value = fr_value_box_alloc_null(ctx)); - fr_value_box_strdup_buffer(value, value, NULL, node->fmt, false); + fr_value_box_bstrdup_buffer(value, value, NULL, node->fmt, false); fr_cursor_append(out, value); continue; diff --git a/src/lib/util/value.c b/src/lib/util/value.c index 7201f4bfe0f..f6b2ab0e45f 100644 --- a/src/lib/util/value.c +++ b/src/lib/util/value.c @@ -3386,6 +3386,58 @@ int fr_value_box_steal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t cons } } +/** Copy a nul terminated string to a #fr_value_box_t + * + * @param[in] ctx to allocate any new buffers in. + * @param[in] dst to assign new buffer to. + * @param[in] enumv Aliases for values. + * @param[in] src a nul terminated buffer. + * @param[in] tainted Whether the value came from a trusted source. + */ +int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, + char const *src, bool tainted) +{ + char const *str; + + str = talloc_typed_strdup(ctx, src); + if (!str) { + fr_strerror_printf("Failed allocating string buffer"); + return -1; + } + + fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted); + dst->vb_strvalue = str; + dst->vb_length = talloc_array_length(str) - 1; + + return 0; +} + +/** Trim the length of the string buffer to match the length of the C string + * + * @param[in] ctx to re-alloc the buffer in. + * @param[in,out] vb to trim. + */ +int fr_value_box_strtrim(TALLOC_CTX *ctx, fr_value_box_t *vb) +{ + size_t len; + char *str; + char *mutable; + + if (!fr_cond_assert(vb->type == FR_TYPE_STRING)) return -1; + + len = strlen(vb->vb_strvalue); + + memcpy(&mutable, &vb->vb_strvalue, sizeof(mutable)); + str = talloc_realloc(ctx, mutable, char, len + 1); + if (!str) { + fr_strerror_printf("Failed re-allocing string buffer"); + return -1; + } + vb->vb_length = len; + + return 0; +} + /** Print a formatted string using our internal printf wrapper and assign it to a value box * * @param[in] ctx to allocate any new buffers in. @@ -3442,6 +3494,21 @@ int fr_value_box_asprintf(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t c return ret; } +/** Assign a buffer containing a nul terminated string to a box, but don't copy it + * + * @param[in] dst to assign string to. + * @param[in] enumv Aliases for values. + * @param[in] src to copy string to. + * @param[in] tainted Whether the value came from a trusted source. + */ +void fr_value_box_strdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, + char const *src, bool tainted) +{ + fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted); + dst->vb_strvalue = src; + dst->datum.length = strlen(src); +} + /** Copy a nul terminated string to a #fr_value_box_t * * @param[in] ctx to allocate any new buffers in. @@ -3450,8 +3517,8 @@ int fr_value_box_asprintf(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t c * @param[in] enumv Aliases for values. * @param[in] tainted Whether the value came from a trusted source. */ -int fr_value_box_stralloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, - size_t len, bool tainted) +int fr_value_box_bstr_alloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, + size_t len, bool tainted) { char *str; @@ -3470,85 +3537,140 @@ int fr_value_box_stralloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, fr_d return 0; } -/** Trim the length of the string buffer to match the length of the C string +/** Copy a string to to a #fr_value_box_t * - * @param[in] ctx to re-alloc the buffer in. - * @param[in,out] vb to trim. + * @param[in] ctx to allocate any new buffers in. + * @param[in] dst to assign buffer to. + * @param[in] enumv Aliases for values. + * @param[in] src a string. + * @param[in] len of src. + * @param[in] tainted Whether the value came from a trusted source. */ -int fr_value_box_strtrim(TALLOC_CTX *ctx, fr_value_box_t *vb) +int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, + char const *src, size_t len, bool tainted) { - size_t len; - char *str; - char *mutable; - - if (!fr_cond_assert(vb->type == FR_TYPE_STRING)) return -1; - - len = strlen(vb->vb_strvalue); + char const *str; - memcpy(&mutable, &vb->vb_strvalue, sizeof(mutable)); - str = talloc_realloc(ctx, mutable, char, len + 1); + str = talloc_bstrndup(ctx, src, len); if (!str) { - fr_strerror_printf("Failed re-allocing string buffer"); + fr_strerror_printf("Failed allocating string buffer"); return -1; } - vb->vb_length = len; + + fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted); + dst->vb_strvalue = str; + dst->vb_length = len; return 0; } -/** Copy a nul terminated string to a #fr_value_box_t +/** Copy a nul terminated talloced buffer to a #fr_value_box_t + * + * Copy a talloced nul terminated buffer, setting fields in the dst value box appropriately. + * + * The buffer must be \0 terminated, or an error will be returned. * * @param[in] ctx to allocate any new buffers in. * @param[in] dst to assign new buffer to. * @param[in] enumv Aliases for values. - * @param[in] src a nul terminated buffer. + * @param[in] src a talloced nul terminated buffer. * @param[in] tainted Whether the value came from a trusted source. + * @return + * - 0 on success. + * - -1 on failure. */ -int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, - char const *src, bool tainted) +int fr_value_box_bstrdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, + char const *src, bool tainted) { - char const *str; + size_t len; - str = talloc_typed_strdup(ctx, src); - if (!str) { - fr_strerror_printf("Failed allocating string buffer"); + len = talloc_array_length(src); + if ((len == 1) || (src[len - 1] != '\0')) { + fr_strerror_printf("Input buffer not \\0 terminated"); return -1; } - fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted); - dst->vb_strvalue = str; - dst->vb_length = talloc_array_length(str) - 1; - - return 0; + return fr_value_box_bstrndup(ctx, dst, enumv, src, len - 1, tainted); } -/** Copy a string to to a #fr_value_box_t +/** Append a buffer to an existing fr_value_box_t * - * @param[in] ctx to allocate any new buffers in. - * @param[in] dst to assign buffer to. - * @param[in] enumv Aliases for values. - * @param[in] src a string. - * @param[in] len of src. - * @param[in] tainted Whether the value came from a trusted source. + * @param[in] ctx Where to allocate any talloc buffers required. + * @param[in] dst value box to append to. + * @param[in] src octets data to append. + * @param[in] len length of octets data. + * @param[in] tainted Whether src is tainted. + * @return + * - 0 on success. + * - -1 on failure. */ -int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, - char const *src, size_t len, bool tainted) +int fr_value_box_bstr_append(TALLOC_CTX *ctx, fr_value_box_t *dst, char const *src, size_t len, bool tainted) { - char const *str; + char *ptr, *nptr; + size_t nlen; - str = talloc_bstrndup(ctx, src, len); - if (!str) { - fr_strerror_printf("Failed allocating string buffer"); + if (len == 0) return 0; + + if (dst->type != FR_TYPE_STRING) { + fr_strerror_printf("%s: Expected boxed value of type %s, got type %s", __FUNCTION__, + fr_table_str_by_value(fr_value_box_type_table, FR_TYPE_STRING, ""), + fr_table_str_by_value(fr_value_box_type_table, dst->type, "")); return -1; } - fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted); - dst->vb_strvalue = str; - dst->vb_length = len; + ptr = dst->datum.ptr; + if (!fr_cond_assert(ptr)) return -1; + + if (talloc_reference_count(ptr) > 0) { + fr_strerror_printf("%s: Boxed value has too many references", __FUNCTION__); + return -1; + } + + nlen = dst->vb_length + len + 1; + nptr = talloc_realloc(ctx, ptr, char, dst->vb_length + len + 1); + if (!nptr) { + fr_strerror_printf("%s: Realloc of %s array from %zu to %zu bytes failed", + __FUNCTION__, talloc_get_name(ptr), talloc_array_length(ptr), nlen); + return -1; + } + talloc_set_type(nptr, char); + ptr = nptr; + + memcpy(ptr + dst->vb_length, src, len); /* Copy data into the realloced buffer */ + + dst->tainted = tainted; + dst->datum.ptr = ptr; + dst->vb_length += len; + + ptr[dst->vb_length] = '\0'; return 0; } +/** Append a talloced buffer to an existing fr_value_box_t + * + * @param[in] ctx Where to allocate any talloc buffers required. + * @param[in] dst value box to append to. + * @param[in] src octets data to append. + * @param[in] len length of octets data. + * @param[in] tainted Whether src is tainted. + * @return + * - 0 on success. + * - -1 on failure. + */ +int fr_value_box_bstr_append_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, char const *src, size_t len, bool tainted) +{ + (void) talloc_get_type_abort_const(src, char); + + len = talloc_array_length(src); + if ((len == 0) || (src[len - 1] != '\0')) { + fr_strerror_printf("Input buffer not \\0 terminated"); + return -1; + } + + return fr_value_box_bstr_append(ctx, dst, src, len - 1, tainted); +} + /** Assign a string to to a #fr_value_box_t * * @param[in] dst to assign new buffer to. @@ -3565,42 +3687,35 @@ void fr_value_box_bstrndup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *en dst->vb_length = len; } -/** Copy a nul terminated talloced buffer to a #fr_value_box_t - * - * Copy a talloced nul terminated buffer, setting fields in the dst value box appropriately. +/** Assign a talloced buffer containing a nul terminated string to a box, but don't copy it * - * The buffer must be \0 terminated, or an error will be returned. + * Adds a reference to the src buffer so that it cannot be freed until the ctx is freed. * - * @param[in] ctx to allocate any new buffers in. - * @param[in] dst to assign new buffer to. + * @param[in] ctx to add reference from. If NULL no reference will be added. + * @param[in] dst to assign string to. * @param[in] enumv Aliases for values. - * @param[in] src a talloced nul terminated buffer. + * @param[in] src to copy string to. * @param[in] tainted Whether the value came from a trusted source. * @return * - 0 on success. * - -1 on failure. */ -int fr_value_box_strdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, - char const *src, bool tainted) +int fr_value_box_bstrdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, + char const *src, bool tainted) { - char *str; size_t len; + (void) talloc_get_type_abort_const(src, char); + len = talloc_array_length(src); - if ((len == 1) || (src[len - 1] != '\0')) { + if ((len == 0) || (src[len - 1] != '\0')) { fr_strerror_printf("Input buffer not \\0 terminated"); return -1; } - str = talloc_bstrndup(ctx, src, len - 1); - if (!str) { - fr_strerror_printf("Failed allocating string buffer"); - return -1; - } - fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted); - dst->vb_strvalue = str; - dst->vb_length = talloc_array_length(str) - 1; + dst->vb_strvalue = ctx ? talloc_reference(ctx, src) : src; + dst->datum.length = len - 1; return 0; } @@ -3689,108 +3804,6 @@ int fr_value_box_bstrnsteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t return 0; } -/** Append a buffer to an existing fr_value_box_t - * - * @param[in] ctx Where to allocate any talloc buffers required. - * @param[in] dst value box to append to. - * @param[in] src octets data to append. - * @param[in] len length of octets data. - * @param[in] tainted Whether src is tainted. - * @return - * - 0 on success. - * - -1 on failure. - */ -int fr_value_box_bstr_append(TALLOC_CTX *ctx, fr_value_box_t *dst, char const *src, size_t len, bool tainted) -{ - char *ptr, *nptr; - size_t nlen; - - if (len == 0) return 0; - - if (dst->type != FR_TYPE_STRING) { - fr_strerror_printf("%s: Expected boxed value of type %s, got type %s", __FUNCTION__, - fr_table_str_by_value(fr_value_box_type_table, FR_TYPE_STRING, ""), - fr_table_str_by_value(fr_value_box_type_table, dst->type, "")); - return -1; - } - - ptr = dst->datum.ptr; - if (!fr_cond_assert(ptr)) return -1; - - if (talloc_reference_count(ptr) > 0) { - fr_strerror_printf("%s: Boxed value has too many references", __FUNCTION__); - return -1; - } - - nlen = dst->vb_length + len + 1; - nptr = talloc_realloc(ctx, ptr, char, dst->vb_length + len + 1); - if (!nptr) { - fr_strerror_printf("%s: Realloc of %s array from %zu to %zu bytes failed", - __FUNCTION__, talloc_get_name(ptr), talloc_array_length(ptr), nlen); - return -1; - } - talloc_set_type(nptr, char); - ptr = nptr; - - memcpy(ptr + dst->vb_length, src, len); /* Copy data into the realloced buffer */ - - dst->tainted = tainted; - dst->datum.ptr = ptr; - dst->vb_length += len; - - ptr[dst->vb_length] = '\0'; - - return 0; -} - -/** Assign a buffer containing a nul terminated string to a box, but don't copy it - * - * @param[in] dst to assign string to. - * @param[in] enumv Aliases for values. - * @param[in] src to copy string to. - * @param[in] tainted Whether the value came from a trusted source. - */ -void fr_value_box_strdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, - char const *src, bool tainted) -{ - fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted); - dst->vb_strvalue = src; - dst->datum.length = strlen(src); -} - -/** Assign a talloced buffer containing a nul terminated string to a box, but don't copy it - * - * Adds a reference to the src buffer so that it cannot be freed until the ctx is freed. - * - * @param[in] ctx to add reference from. If NULL no reference will be added. - * @param[in] dst to assign string to. - * @param[in] enumv Aliases for values. - * @param[in] src to copy string to. - * @param[in] tainted Whether the value came from a trusted source. - * @return - * - 0 on success. - * - -1 on failure. - */ -int fr_value_box_strdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, - char const *src, bool tainted) -{ - size_t len; - - (void) talloc_get_type_abort_const(src, char); - - len = talloc_array_length(src); - if ((len == 0) || (src[len - 1] != '\0')) { - fr_strerror_printf("Input buffer not \\0 terminated"); - return -1; - } - - fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted); - dst->vb_strvalue = ctx ? talloc_reference(ctx, src) : src; - dst->datum.length = len - 1; - - return 0; -} - /** Pre-allocate an octets buffer for filling by the caller * * @note Buffer will not be zeroed, as it's assumed the caller will be filling it. @@ -3810,8 +3823,8 @@ int fr_value_box_strdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_ * - 0 on success. * - -1 on failure. */ -int fr_value_box_memalloc(TALLOC_CTX *ctx, uint8_t **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, - size_t len, bool tainted) +int fr_value_box_mem_alloc(TALLOC_CTX *ctx, uint8_t **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, + size_t len, bool tainted) { uint8_t *bin; @@ -3868,7 +3881,70 @@ int fr_value_box_memdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t con return 0; } -/** Append a buffer to an existing fr_value_box_t +/** Copy a talloced buffer to a fr_value_box_t + * + * Copy a buffer containing binary data, setting fields in the dst value box appropriately. + * + * @param[in] ctx to allocate any new buffers in. + * @param[in] dst to assign new buffer to. + * @param[in] enumv Aliases for values. + * @param[in] src a buffer. + * @param[in] tainted Whether the value came from a trusted source. + * @return + * - 0 on success. + * - -1 on failure. + */ +int fr_value_box_memdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, + uint8_t *src, bool tainted) +{ + (void) talloc_get_type_abort(src, uint8_t); + + return fr_value_box_memdup(ctx, dst, enumv, src, talloc_array_length(src), tainted); +} + +/** Assign a buffer to a box, but don't copy it + * + * Adds a reference to the src buffer so that it cannot be freed until the ctx is freed. + * + * Caller should set dst->taint = true, where the value was acquired from an untrusted source. + * + * @note Will free any exiting buffers associated with the value box. + * + * @param[in] dst to assign buffer to. + * @param[in] enumv Aliases for values. + * @param[in] src a talloced buffer. + * @param[in] len of buffer. + * @param[in] tainted Whether the value came from a trusted source. + */ +void fr_value_box_memdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, + uint8_t *src, size_t len, bool tainted) +{ + fr_value_box_init(dst, FR_TYPE_OCTETS, enumv, tainted); + dst->vb_octets = src; + dst->datum.length = len; +} + +/** Assign a talloced buffer to a box, but don't copy it + * + * Adds a reference to the src buffer so that it cannot be freed until the ctx is freed. + * + * @param[in] ctx to allocate any new buffers in. + * @param[in] dst to assign buffer to. + * @param[in] enumv Aliases for values. + * @param[in] src a talloced buffer. + * @param[in] tainted Whether the value came from a trusted source. + */ +void fr_value_box_memdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, + uint8_t *src, bool tainted) +{ + (void) talloc_get_type_abort(src, uint8_t); + + fr_value_box_init(dst, FR_TYPE_OCTETS, enumv, tainted); + dst->vb_octets = ctx ? talloc_reference(ctx, src) : src; + dst->datum.length = talloc_array_length(src); +} + +/** Append data to an existing fr_value_box_t * * @param[in] ctx Where to allocate any talloc buffers required. * @param[in] dst value box to append to. @@ -3912,32 +3988,26 @@ int fr_value_box_mem_append(TALLOC_CTX *ctx, fr_value_box_t *dst, uint8_t const memcpy(ptr + dst->vb_length, src, len); /* Copy data into the realloced buffer */ - dst->tainted = tainted; + dst->tainted = dst->tainted || tainted; dst->datum.ptr = ptr; dst->vb_length += len; return 0; } -/** Copy a talloced buffer to a fr_value_box_t +/** Append a talloc buffer to an existing fr_value_box_t * - * Copy a buffer containing binary data, setting fields in the dst value box appropriately. - * - * @param[in] ctx to allocate any new buffers in. - * @param[in] dst to assign new buffer to. - * @param[in] enumv Aliases for values. - * @param[in] src a buffer. - * @param[in] tainted Whether the value came from a trusted source. + * @param[in] ctx Where to allocate any talloc buffers required. + * @param[in] dst value box to append to. + * @param[in] src octets data to append. + * @param[in] tainted Whether src is tainted. * @return * - 0 on success. - * - -1 on failure. + * - -1 on failure. */ -int fr_value_box_memdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, - uint8_t *src, bool tainted) +int fr_value_box_mem_append_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, uint8_t const *src, bool tainted) { - (void) talloc_get_type_abort(src, uint8_t); - - return fr_value_box_memdup(ctx, dst, enumv, src, talloc_array_length(src), tainted); + return fr_value_box_mem_append(ctx, dst, src, talloc_array_length(src), tainted); } /** Steal a talloced buffer into a specified ctx, and assign to a #fr_value_box_t @@ -3963,48 +4033,6 @@ void fr_value_box_memsteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t dst->datum.length = talloc_array_length(src); } -/** Assign a buffer to a box, but don't copy it - * - * Adds a reference to the src buffer so that it cannot be freed until the ctx is freed. - * - * Caller should set dst->taint = true, where the value was acquired from an untrusted source. - * - * @note Will free any exiting buffers associated with the value box. - * - * @param[in] dst to assign buffer to. - * @param[in] enumv Aliases for values. - * @param[in] src a talloced buffer. - * @param[in] len of buffer. - * @param[in] tainted Whether the value came from a trusted source. - */ -void fr_value_box_memdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, - uint8_t *src, size_t len, bool tainted) -{ - fr_value_box_init(dst, FR_TYPE_OCTETS, enumv, tainted); - dst->vb_octets = src; - dst->datum.length = len; -} - -/** Assign a talloced buffer to a box, but don't copy it - * - * Adds a reference to the src buffer so that it cannot be freed until the ctx is freed. - * - * @param[in] ctx to allocate any new buffers in. - * @param[in] dst to assign buffer to. - * @param[in] enumv Aliases for values. - * @param[in] src a talloced buffer. - * @param[in] tainted Whether the value came from a trusted source. - */ -void fr_value_box_memdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, - uint8_t *src, bool tainted) -{ - (void) talloc_get_type_abort(src, uint8_t); - - fr_value_box_init(dst, FR_TYPE_OCTETS, enumv, tainted); - dst->vb_octets = ctx ? talloc_reference(ctx, src) : src; - dst->datum.length = talloc_array_length(src); -} - /** Increment a boxed value * * Implements safe integer overflow. diff --git a/src/lib/util/value.h b/src/lib/util/value.h index 0e1f8429461..6d52178e5d9 100644 --- a/src/lib/util/value.h +++ b/src/lib/util/value.h @@ -576,8 +576,9 @@ int fr_value_box_ipaddr(fr_value_box_t *dst, fr_dict_attr_t const *enumv, int fr_value_unbox_ipaddr(fr_ipaddr_t *dst, fr_value_box_t *src); -/* - * Assignment +/** @name Box to box copying + * + * @{ */ int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src); @@ -585,56 +586,89 @@ void fr_value_box_copy_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src, bool incr_ref); int fr_value_box_steal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t const *src); +/** @} */ + +/** @name Assign and manipulate binary-unsafe C strings + * + * @{ + */ +int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, + char const *src, bool tainted); + +int fr_value_box_strtrim(TALLOC_CTX *ctx, fr_value_box_t *vb); int fr_value_box_vasprintf(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, bool tainted, char const *fmt, va_list ap) CC_HINT(format (printf, 5, 0)); + int fr_value_box_asprintf(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, bool tainted, char const *fmt, ...) CC_HINT(format (printf, 5, 6)); -int fr_value_box_stralloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, - size_t len, bool tainted); -int fr_value_box_strtrim(TALLOC_CTX *ctx, fr_value_box_t *vb); +void fr_value_box_strdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, + char const *src, bool tainted); +/** @} */ -int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, - char const *src, bool tainted); -int fr_value_box_strdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, - char const *src, bool tainted); +/** @name Assign and manipulate binary-safe strings + * + * @{ + */ +int fr_value_box_bstr_alloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, + size_t len, bool tainted); int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted); + +int fr_value_box_bstrdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, + char const *src, bool tainted); + +int fr_value_box_bstr_append(TALLOC_CTX *ctx, fr_value_box_t *dst, char const *src, size_t len, bool tainted); + +int fr_value_box_bstr_append_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, char const *src, size_t len, + bool tainted); + void fr_value_box_bstrndup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted); +int fr_value_box_bstrdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, + char const *src, bool tainted); + int fr_value_box_bstrsteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char *src, bool tainted); + int fr_value_box_bstrnsteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char **src, size_t inlen, bool tainted); +/** @} */ -int fr_value_box_bstr_append(TALLOC_CTX *ctx, fr_value_box_t *dst, char const *src, size_t len, bool tainted); - -void fr_value_box_strdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, - char const *src, bool tainted); -int fr_value_box_strdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, - char const *src, bool tainted); +/** @name Assign and manipulate octets strings + * + * @{ + */ +int fr_value_box_mem_alloc(TALLOC_CTX *ctx, uint8_t **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, + size_t len, bool tainted); int fr_value_box_memdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, size_t len, bool tainted); -int fr_value_box_memalloc(TALLOC_CTX *ctx, uint8_t **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, - size_t len, bool tainted); - -int fr_value_box_mem_append(TALLOC_CTX *ctx, fr_value_box_t *dst, - uint8_t const *src, size_t len, bool tainted); int fr_value_box_memdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t *src, bool tainted); -void fr_value_box_memsteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, - uint8_t const *src, bool tainted); + void fr_value_box_memdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t *src, size_t len, bool tainted); + void fr_value_box_memdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t *src, bool tainted); + +int fr_value_box_mem_append(TALLOC_CTX *ctx, fr_value_box_t *dst, + uint8_t const *src, size_t len, bool tainted); + +int fr_value_box_mem_append_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, uint8_t const *src, bool tainted); + + +void fr_value_box_memsteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, + uint8_t const *src, bool tainted); +/** @} */ + void fr_value_box_increment(fr_value_box_t *vb); /* diff --git a/src/modules/rlm_logtee/rlm_logtee.c b/src/modules/rlm_logtee/rlm_logtee.c index 121da0c4407..e04a25a5694 100644 --- a/src/modules/rlm_logtee/rlm_logtee.c +++ b/src/modules/rlm_logtee/rlm_logtee.c @@ -468,7 +468,7 @@ static void logtee_it(fr_log_type_t type, fr_log_lvl_t lvl, REQUEST *request, * None of this should involve mallocs unless msg > 1k */ msg = talloc_typed_vasprintf(t->msg, fmt, ap); - fr_value_box_strdup_buffer_shallow(NULL, &t->msg->data, attr_log_message, msg, true); + fr_value_box_bstrdup_buffer_shallow(NULL, &t->msg->data, attr_log_message, msg, true); t->type->vp_uint32 = (uint32_t) type; t->lvl->vp_uint32 = (uint32_t) lvl;