]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
pair: Remove more memsteal uses
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 25 May 2020 03:13:38 +0000 (22:13 -0500)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 25 May 2020 03:20:21 +0000 (22:20 -0500)
src/lib/eap/compose.c
src/modules/rlm_cipher/rlm_cipher.c
src/modules/rlm_eap/rlm_eap.c
src/modules/rlm_eap/types/rlm_eap_mschapv2/rlm_eap_mschapv2.c
src/modules/rlm_eap/types/rlm_eap_peap/peap.c
src/modules/rlm_mschap/rlm_mschap.c
src/modules/rlm_sigtran/client.c
src/modules/rlm_soh/rlm_soh.c
src/protocols/radius/decode.c

index f6d2ba485f17cf6dcb37abbf2447879adb7ded37..2440d71aea0b02daa04a630a006b734c5441bd4f 100644 (file)
@@ -343,13 +343,12 @@ int eap_start(REQUEST *request, rlm_eap_method_t const methods[], bool ignore_un
                /*
                 *      Manually create an EAP Identity request
                 */
-               p = talloc_array(vp, uint8_t, 5);
+               MEM(fr_pair_value_mem_alloc(vp, &p, 5, false) == 0);
                p[0] = FR_EAP_CODE_REQUEST;
                p[1] = 0; /* ID */
                p[2] = 0;
                p[3] = 5; /* length */
                p[4] = FR_EAP_METHOD_IDENTITY;
-               fr_pair_value_memsteal(vp, p, false);
 
                return RLM_MODULE_HANDLED;
        } /* end of handling EAP-Start */
index f060784785f1dba931ba8301b2e0d7f910d26893..5df73f77e6131c953e2075142f59062e2580e3ef 100644 (file)
@@ -502,30 +502,16 @@ static xlat_action_t cipher_rsa_encrypt_xlat(TALLOC_CTX *ctx, fr_cursor_t *out,
                return XLAT_ACTION_FAIL;
        }
 
-       MEM(ciphertext = talloc_array(ctx, uint8_t, ciphertext_len));
+       MEM(vb = fr_value_box_alloc_null(ctx));
+       MEM(fr_value_box_mem_alloc(vb, &ciphertext, vb, NULL, ciphertext_len, false) == 0);
        if (EVP_PKEY_encrypt(xt->evp_encrypt_ctx, ciphertext, &ciphertext_len,
                             (unsigned char const *)plaintext, plaintext_len) <= 0) {
                fr_tls_log_error(request, "Failed encrypting plaintext");
+               talloc_free(vb);
                return XLAT_ACTION_FAIL;
        }
        RHEXDUMP3(ciphertext, ciphertext_len, "Ciphertext (%zu bytes)", ciphertext_len);
-
-       if (ciphertext_len != talloc_array_length(ciphertext)) {
-               uint8_t *n;
-
-               n = talloc_realloc_size(ctx, ciphertext, ciphertext_len);
-               if (unlikely(!n)) {
-                       REDEBUG("Failed shrinking ciphertext buffer");
-                       talloc_free(ciphertext);
-                       return XLAT_ACTION_FAIL;
-               }
-               talloc_set_type(n, uint8_t);
-
-               ciphertext = n;
-       }
-
-       MEM(vb = fr_value_box_alloc_null(ctx));
-       fr_value_box_memsteal(vb, vb, NULL, ciphertext, false);
+       MEM(fr_value_box_mem_realloc(vb, NULL, vb, ciphertext_len) == 0);
        fr_cursor_append(out, vb);
 
        return XLAT_ACTION_DONE;
@@ -601,31 +587,14 @@ static xlat_action_t cipher_rsa_sign_xlat(TALLOC_CTX *ctx, fr_cursor_t *out,
                return XLAT_ACTION_FAIL;
        }
 
-       MEM(sig = talloc_array(ctx, uint8_t, sig_len));
+       MEM(vb = fr_value_box_alloc_null(ctx));
+       MEM(fr_value_box_mem_alloc(vb, &sig, vb, NULL, sig_len, false) == 0);
        if (EVP_PKEY_sign(xt->evp_sign_ctx, sig, &sig_len, xt->digest_buff, (size_t)digest_len) <= 0) {
                fr_tls_log_error(request, "Failed signing message digest");
+               talloc_free(vb);
                return XLAT_ACTION_FAIL;
        }
-
-       /*
-        *      Fixup the output buffer
-        */
-       if (sig_len != talloc_array_length(sig)) {
-               uint8_t *n;
-
-               n = talloc_realloc_size(ctx, sig, sig_len);
-               if (unlikely(!n)) {
-                       REDEBUG("Failed shrinking signature buffer");
-                       talloc_free(sig);
-                       return XLAT_ACTION_FAIL;
-               }
-               talloc_set_type(n, uint8_t);
-
-               sig = n;
-       }
-
-       MEM(vb = fr_value_box_alloc_null(ctx));
-       fr_value_box_memsteal(vb, vb, NULL, sig, false);
+       MEM(fr_value_box_mem_realloc(vb, NULL, vb, sig_len) == 0);
        fr_cursor_append(out, vb);
 
        return XLAT_ACTION_DONE;
@@ -654,7 +623,7 @@ static xlat_action_t cipher_rsa_decrypt_xlat(TALLOC_CTX *ctx, fr_cursor_t *out,
        size_t                          ciphertext_len;
 
        char                            *plaintext;
-       size_t                          plaintext_len_est, plaintext_len;
+       size_t                          plaintext_len;
 
        fr_value_box_t                  *vb;
 
@@ -674,14 +643,13 @@ static xlat_action_t cipher_rsa_decrypt_xlat(TALLOC_CTX *ctx, fr_cursor_t *out,
         *      Decrypt the ciphertext
         */
        RHEXDUMP3(ciphertext, ciphertext_len, "Ciphertext (%zu bytes)", ciphertext_len);
-       if (EVP_PKEY_decrypt(xt->evp_decrypt_ctx, NULL, &plaintext_len_est, ciphertext, ciphertext_len) <= 0) {
+       if (EVP_PKEY_decrypt(xt->evp_decrypt_ctx, NULL, &plaintext_len, ciphertext, ciphertext_len) <= 0) {
                fr_tls_log_error(request, "Failed getting length of cleartext");
                return XLAT_ACTION_FAIL;
        }
-       plaintext_len = plaintext_len_est;
 
        MEM(vb = fr_value_box_alloc_null(ctx));
-       MEM(fr_value_box_bstr_alloc(vb, &plaintext, vb, NULL, plaintext_len_est, true) == 0);
+       MEM(fr_value_box_bstr_alloc(vb, &plaintext, vb, NULL, plaintext_len, true) == 0);
        if (EVP_PKEY_decrypt(xt->evp_decrypt_ctx, (unsigned char *)plaintext, &plaintext_len,
                             ciphertext, ciphertext_len) <= 0) {
                fr_tls_log_error(request, "Failed decrypting ciphertext");
index 4be4277d1d45f53923723ddb0fffbb22c383b867..6962f9b7a2902e8a9c3c89fcce3cbd2ae93e65be 100644 (file)
@@ -968,7 +968,7 @@ static rlm_rcode_t mod_post_auth(void *instance, UNUSED void *thread, REQUEST *r
         *      RADIUS protocol code will calculate the correct value later...
         */
        MEM(pair_update_reply(&vp, attr_message_authenticator) >= 0);
-       fr_pair_value_memsteal(vp, talloc_zero_array(vp, uint8_t, RADIUS_AUTH_VECTOR_LENGTH), false);
+       MEM(fr_pair_value_mem_alloc(vp, NULL, RADIUS_AUTH_VECTOR_LENGTH, false) == 0);
 
        return RLM_MODULE_UPDATED;
 }
index 255b2674b2206ade3459acd4b6536ec43b022e36..4c306fca1e0cc6a395c2697177622a960b4bf689 100644 (file)
@@ -454,7 +454,6 @@ static rlm_rcode_t CC_HINT(nonnull) mod_process(void *instance, UNUSED void *thr
        int                     ccode;
        uint8_t                 *p;
        size_t                  length;
-       char                    *q;
 
        if (!fr_cond_assert(eap_session->inst)) return 0;
 
@@ -494,11 +493,10 @@ static rlm_rcode_t CC_HINT(nonnull) mod_process(void *instance, UNUSED void *thr
                        fr_pair_value_memdup(auth_challenge, data->auth_challenge, MSCHAPV2_CHALLENGE_LEN, false);
 
                        MEM(pair_update_request(&cpw, attr_ms_chap2_cpw) >= 0);
-                       p = talloc_array(cpw, uint8_t, 68);
+                       MEM(fr_pair_value_mem_alloc(cpw, &p, 68, false) == 0);
                        p[0] = 7;
                        p[1] = mschap_id;
                        memcpy(p + 2, eap_round->response->type.data + 520, 66);
-                       fr_pair_value_memsteal(cpw, p, false);
 
                        /*
                         * break the encoded password into VPs (3 of them)
@@ -510,13 +508,12 @@ static rlm_rcode_t CC_HINT(nonnull) mod_process(void *instance, UNUSED void *thr
                                if (to_copy > 243) to_copy = 243;
 
                                MEM(pair_add_request(&nt_enc, attr_ms_chap_nt_enc_pw) >= 0);
-                               p = talloc_array(nt_enc, uint8_t, 4 + to_copy);
+                               MEM(fr_pair_value_mem_alloc(nt_enc, &p, 4 + to_copy, false) == 0);
                                p[0] = 6;
                                p[1] = mschap_id;
                                p[2] = 0;
                                p[3] = seq++;
                                memcpy(p + 4, eap_round->response->type.data + 4 + copied, to_copy);
-                               fr_pair_value_memsteal(nt_enc, p, false);
 
                                copied += to_copy;
                        }
@@ -650,7 +647,7 @@ failure:
        fr_pair_value_memdup(auth_challenge, data->auth_challenge, MSCHAPV2_CHALLENGE_LEN, false);
 
        MEM(pair_update_request(&response, attr_ms_chap2_response) >= 0);
-       p = talloc_array(response, uint8_t, MSCHAPV2_RESPONSE_LEN);
+       MEM(fr_pair_value_mem_alloc(response, &p, MSCHAPV2_RESPONSE_LEN, false) == 0);
        p[0] = eap_round->response->type.data[1];
        p[1] = eap_round->response->type.data[5 + MSCHAPV2_RESPONSE_LEN];
        memcpy(p + 2, &eap_round->response->type.data[5], MSCHAPV2_RESPONSE_LEN - 2);
@@ -660,18 +657,13 @@ failure:
         *      the challenge sent by the client.
         */
        if (data->has_peer_challenge) memcpy(p + 2, data->peer_challenge, MSCHAPV2_CHALLENGE_LEN);
-       fr_pair_value_memsteal(response, p, false);
 
        /*
         *      MS-Length - MS-Value - 5.
         */
        MEM(pair_update_request(&name, attr_ms_chap_user_name) >= 0);
-       name->vp_tainted = true;
-       name->vp_length = length - 49 - 5;
-       name->vp_strvalue = q = talloc_array(name, char, name->vp_length + 1);
-       memcpy(q, &eap_round->response->type.data[4 + MSCHAPV2_RESPONSE_LEN], name->vp_length);
-       q[name->vp_length] = '\0';
-
+       MEM(fr_pair_value_bstrndup(name, (char const *)&eap_round->response->type.data[4 + MSCHAPV2_RESPONSE_LEN],
+                                  length - 49 - 5, true) == 0);
 packet_ready:
 
 #ifdef WITH_PROXY
@@ -821,9 +813,8 @@ static rlm_rcode_t mod_session_init(void *instance, UNUSED void *thread, REQUEST
                 *      Get a random challenge.
                 */
                MEM(auth_challenge = fr_pair_afrom_da(eap_session, attr_ms_chap_challenge));
-               p = talloc_array(auth_challenge, uint8_t, MSCHAPV2_CHALLENGE_LEN);
+               MEM(fr_pair_value_mem_alloc(auth_challenge, &p, MSCHAPV2_CHALLENGE_LEN, false) == 0);
                for (i = 0; i < MSCHAPV2_CHALLENGE_LEN; i++) p[i] = fr_rand();
-               fr_pair_value_memsteal(auth_challenge, p, false);
        }
        RDEBUG2("Issuing Challenge");
 
index 9cf02a19c68006cd1c2a5f0bdef9cd7be30e3fdb..25e285d0e16e005db9b8017b920a03d3203b08ea 100644 (file)
@@ -278,13 +278,12 @@ static VALUE_PAIR *eap_peap_inner_to_pairs(UNUSED REQUEST *request, RADIUS_PACKE
        /*
         *      Hand-build an EAP packet from the crap in PEAP version 0.
         */
-       p = talloc_array(vp, uint8_t, EAP_HEADER_LEN + total);
+       MEM(fr_pair_value_mem_alloc(vp, &p, EAP_HEADER_LEN + total, false) == 0);
        p[0] = FR_EAP_CODE_RESPONSE;
        p[1] = eap_round->response->id;
        p[2] = (data_len + EAP_HEADER_LEN) >> 8;
        p[3] = (data_len + EAP_HEADER_LEN) & 0xff;
        memcpy(p + EAP_HEADER_LEN, data, total);
-       fr_pair_value_memsteal(vp, p, false);
 
        fr_cursor_init(&cursor, &head);
        fr_cursor_append(&cursor, vp);
@@ -672,8 +671,7 @@ rlm_rcode_t eap_peap_process(REQUEST *request, eap_session_t *eap_session, fr_tl
                t->status = PEAP_STATUS_PHASE2;
 
                MEM(vp = fr_pair_afrom_da(fake->packet, attr_eap_message));
-
-               q = talloc_array(vp, uint8_t, len);
+               MEM(fr_pair_value_mem_alloc(vp, &q, len, false) == 0);
                q[0] = FR_EAP_CODE_RESPONSE;
                q[1] = eap_round->response->id;
                q[2] = (len >> 8) & 0xff;
@@ -681,8 +679,6 @@ rlm_rcode_t eap_peap_process(REQUEST *request, eap_session_t *eap_session, fr_tl
                q[4] = FR_EAP_METHOD_IDENTITY;
                memcpy(q + EAP_HEADER_LEN + 1,
                       t->username->vp_strvalue, t->username->vp_length);
-
-               fr_pair_value_memsteal(vp, q, false);
                fr_pair_add(&fake->packet->vps, vp);
        }
                break;
index db5155e1e84eb441c41278c7ad070cbec3a95437..b8a0a5aa66224906b4742e55fb5d689265115e09 100644 (file)
@@ -951,8 +951,7 @@ ntlm_auth_err:
                 *  cleartext password as it avoids unicode hassles.
                 */
                MEM(pair_update_request(&new_hash, attr_ms_chap_new_nt_password) >= 0);
-               MEM(q = talloc_array(new_hash, uint8_t, NT_DIGEST_LENGTH));
-               fr_pair_value_memsteal(new_hash, q, false);
+               MEM(fr_pair_value_mem_alloc(new_hash, &q, NT_DIGEST_LENGTH, false) == 0);
                fr_md4_calc(q, p, passlen);
 
                /*
@@ -1533,9 +1532,7 @@ found_password:
                VALUE_PAIR      *nt_password;
 
                MEM(nt_password = fr_pair_afrom_da(request, attr_nt_password));
-               MEM(p = talloc_array(nt_password, uint8_t, NT_DIGEST_LENGTH));
-               fr_pair_value_memsteal(nt_password, p, false);
-
+               MEM(fr_pair_value_mem_alloc(nt_password, &p, NT_DIGEST_LENGTH, false) == 0);
                ret = mschap_nt_password_hash(p, password->vp_strvalue);
 
                if (ret < 0) {
@@ -2013,15 +2010,13 @@ static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, UNUSED void
                 *      continue with the authentication.
                 */
                MEM(pair_update_request(&response, attr_ms_chap2_response) >= 0);
-               MEM(p = talloc_array(response, uint8_t, 50));
+               MEM(fr_pair_value_mem_alloc(response, &p, 50, cpw->vp_tainted) == 0);
 
                /* ident & flags */
                p[0] = cpw->vp_octets[1];
                p[1] = 0;
                /* peer challenge and client NT response */
                memcpy(p + 2, cpw->vp_octets + 18, 48);
-
-               fr_pair_value_memsteal(response, p, false);
        }
 
        challenge = fr_pair_find_by_da(request->packet->vps, attr_ms_chap_challenge, TAG_ANY);
index 8603323e1733eb1bdb89bd027234d294ac715010..e84c6226270847f6f8bc83f7ef61a38a6ec35cc7 100644 (file)
@@ -314,17 +314,20 @@ static rlm_rcode_t sigtran_client_map_resume(UNUSED void *instance, UNUSED void
                                RDEBUG2("SIM auth vector %i", i);
                                RINDENT();
                                MEM(vp = fr_pair_afrom_da(request, attr_eap_aka_sim_rand));
-                               fr_pair_value_memsteal(vp, vec->sim.rand, true);
+                               MEM(fr_pair_value_memdup_buffer(vp, vec->sim.rand, true) == 0);
+                               TALLOC_FREE(vec->sim.rand);
                                RDEBUG2("&control:%pP", vp);
                                fr_cursor_append(&cursor, vp);
 
                                MEM(vp = fr_pair_afrom_da(request, attr_eap_aka_sim_sres));
-                               fr_pair_value_memsteal(vp, vec->sim.sres, true);
+                               MEM(fr_pair_value_memdup_buffer(vp, vec->sim.sres, true) == 0);
+                               TALLOC_FREE(vec->sim.sres);
                                RDEBUG2("&control:%pP", vp);
                                fr_cursor_append(&cursor, vp);
 
                                MEM(vp = fr_pair_afrom_da(request, attr_eap_aka_sim_kc));
-                               fr_pair_value_memsteal(vp, vec->sim.kc, true);
+                               MEM(fr_pair_value_memdup_buffer(vp, vec->sim.kc, true) == 0);
+                               TALLOC_FREE(vec->sim.kc);
                                RDEBUG2("&control:%pP", vp);
                                fr_cursor_append(&cursor, vp);
                                REXDENT();
@@ -342,27 +345,32 @@ static rlm_rcode_t sigtran_client_map_resume(UNUSED void *instance, UNUSED void
                                RDEBUG2("UMTS auth vector %i", i);
                                RINDENT();
                                MEM(vp = fr_pair_afrom_da(request, attr_eap_aka_sim_rand));
-                               fr_pair_value_memsteal(vp, vec->umts.rand, true);
+                               MEM(fr_pair_value_memdup_buffer(vp, vec->umts.rand, true) == 0);
+                               TALLOC_FREE(vec->umts.rand);
                                RDEBUG2("&control:%pP", vp);
                                fr_cursor_append(&cursor, vp);
 
                                MEM(vp = fr_pair_afrom_da(request, attr_eap_aka_sim_xres));
-                               fr_pair_value_memsteal(vp, vec->umts.xres, true);
+                               MEM(fr_pair_value_memdup_buffer(vp, vec->umts.xres, true) == 0);
+                               TALLOC_FREE(vec->umts.xres);
                                RDEBUG2("&control:%pP", vp);
                                fr_cursor_append(&cursor, vp);
 
                                MEM(vp = fr_pair_afrom_da(request, attr_eap_aka_sim_ck));
-                               fr_pair_value_memsteal(vp, vec->umts.ck, true);
+                               MEM(fr_pair_value_memdup_buffer(vp, vec->umts.ck, true) == 0);
+                               TALLOC_FREE(vec->umts.ck);
                                RDEBUG2("&control:%pP", vp);
                                fr_cursor_append(&cursor, vp);
 
                                MEM(vp = fr_pair_afrom_da(request, attr_eap_aka_sim_ik));
-                               fr_pair_value_memsteal(vp, vec->umts.ik, true);
+                               MEM(fr_pair_value_memdup_buffer(vp, vec->umts.ik, true) == 0);
+                               TALLOC_FREE(vec->umts.ik);
                                RDEBUG2("&control:%pP", vp);
                                fr_cursor_append(&cursor, vp);
 
                                MEM(vp = fr_pair_afrom_da(request, attr_eap_aka_sim_autn));
-                               fr_pair_value_memsteal(vp, vec->umts.authn, true);
+                               MEM(fr_pair_value_memdup_buffer(vp, vec->umts.authn, true) == 0);
+                               TALLOC_FREE(vec->umts.authn);
                                RDEBUG2("&control:%pP", vp);
                                fr_cursor_append(&cursor, vp);
                                REXDENT();
index f7a8cac4f0f0371adfaae20f797be41e8fae201e..cc7b7c1b711db4465c7c9a6c4c3303f8e9ea0cf2 100644 (file)
@@ -176,13 +176,12 @@ static rlm_rcode_t CC_HINT(nonnull) mod_post_auth(void *instance, UNUSED void *t
                                        RDEBUG2("SoH adding NAP marker to DHCP reply");
                                        /* client probe; send "NAP" in the reply */
                                        MEM(vp = fr_pair_afrom_da(request->reply, attr_dhcp_vendor));
-                                       MEM(p = talloc_array(vp, uint8_t, 5));
+                                       MEM(fr_pair_value_mem_alloc(vp, &p, 5, false) == 0);
                                        p[0] = 220;
                                        p[1] = 3;
                                        p[4] = 'N';
                                        p[3] = 'A';
                                        p[2] = 'P';
-                                       fr_pair_value_memsteal(vp, p, false);
                                        fr_pair_add(&request->reply->vps, vp);
 
                                } else {
index 1973ecfde94126e90b45e4bb7404f76372e9cbc0..3abb8d606a158eecc5490ed276f407967be5ea87 100644 (file)
@@ -390,12 +390,10 @@ static ssize_t decode_concat(TALLOC_CTX *ctx, fr_cursor_t *cursor,
        vp = fr_pair_afrom_da(ctx, parent);
        if (!vp) return -1;
 
-       p = talloc_array(vp, uint8_t, total);
-       if (!p) {
-               fr_pair_list_free(&vp);
+       if (!fr_pair_value_mem_alloc(vp, &p, total, true)) {
+               talloc_free(vp);
                return -1;
        }
-       fr_pair_value_memsteal(vp, p, true);
 
        ptr = data;
        while (ptr < end) {