Commit 689fc11
committed
net/ssl-mbed: fix sub-context leaks on ssl_socket_init error label
Deferred follow-up noted in e3dc586 ('ssl-mbed NULL-check calloc').
ssl_socket_init's error label (line 138 pre-patch) only did
free(state), but the function initialised six mbedtls sub-
contexts on 'state' before the two failure-checked calls that
can branch to it:
mbedtls_net_init(&state->net_ctx);
mbedtls_ssl_init(&state->ctx);
mbedtls_ssl_config_init(&state->conf);
mbedtls_x509_crt_init(&state->ca);
mbedtls_ctr_drbg_init(&state->ctr_drbg);
mbedtls_entropy_init(&state->entropy);
...
if (mbedtls_ctr_drbg_seed(...) != 0)
goto error;
if (mbedtls_x509_crt_parse(...) < 0)
goto error;
Each _init pairs with a matching _free that releases internally-
allocated buffers (entropy pool, DRBG state, SSL and config
contexts, X509 chain). Pre-patch, a ctr_drbg_seed or
x509_crt_parse failure leaked all of them.
Fix: mirror ssl_socket_free's teardown in the error label, in
reverse of init order. Same five _free calls, same #ifdef
gate for the X509 chain. mbedtls_net_free is intentionally
NOT called - the net_ctx only holds state->net_ctx.fd which
is the caller's fd parameter (assigned at line 120) and the
caller still owns it on this error exit. ssl_socket_free
similarly avoids mbedtls_net_free; it just socket_close's the
fd through the separate ssl_socket_close path at line 345.
The pre-existing 'if (state)' guard is now redundant (state
is NULL-checked at line 102 immediately after the calloc, so
the error label is only reachable with state != NULL).
Kept it to minimise the diff.
Thread-safety: unchanged. ssl_socket_init runs on the http-
task worker thread and doesn't touch shared state.
Reachability: every HTTPS connection that reaches the error
label - either a ctr_drbg_seed failure (extremely rare, would
indicate entropy source failure) or an x509_crt_parse failure
on the bundled cacert_pem (would only happen after local cert
data corruption). Neither is a common runtime condition, but
when they do hit the leak is significant (~10-40KB depending
on mbedtls build config) and repeated across every retry.1 parent 0fe401c commit 689fc11
2 files changed
Lines changed: 113 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3521 | 3521 | | |
3522 | 3522 | | |
3523 | 3523 | | |
| 3524 | + | |
3524 | 3525 | | |
3525 | | - | |
3526 | | - | |
| 3526 | + | |
| 3527 | + | |
| 3528 | + | |
3527 | 3529 | | |
3528 | 3530 | | |
3529 | 3531 | | |
| |||
3545 | 3547 | | |
3546 | 3548 | | |
3547 | 3549 | | |
| 3550 | + | |
3548 | 3551 | | |
3549 | | - | |
3550 | | - | |
| 3552 | + | |
| 3553 | + | |
| 3554 | + | |
3551 | 3555 | | |
3552 | 3556 | | |
3553 | 3557 | | |
| |||
3586 | 3590 | | |
3587 | 3591 | | |
3588 | 3592 | | |
| 3593 | + | |
3589 | 3594 | | |
3590 | | - | |
3591 | | - | |
| 3595 | + | |
| 3596 | + | |
| 3597 | + | |
| 3598 | + | |
| 3599 | + | |
| 3600 | + | |
| 3601 | + | |
| 3602 | + | |
| 3603 | + | |
| 3604 | + | |
| 3605 | + | |
| 3606 | + | |
3592 | 3607 | | |
3593 | 3608 | | |
3594 | 3609 | | |
| |||
3756 | 3771 | | |
3757 | 3772 | | |
3758 | 3773 | | |
3759 | | - | |
| 3774 | + | |
| 3775 | + | |
| 3776 | + | |
| 3777 | + | |
| 3778 | + | |
| 3779 | + | |
| 3780 | + | |
3760 | 3781 | | |
3761 | 3782 | | |
3762 | 3783 | | |
| |||
3805 | 3826 | | |
3806 | 3827 | | |
3807 | 3828 | | |
3808 | | - | |
| 3829 | + | |
| 3830 | + | |
| 3831 | + | |
| 3832 | + | |
| 3833 | + | |
| 3834 | + | |
| 3835 | + | |
3809 | 3836 | | |
3810 | 3837 | | |
3811 | 3838 | | |
3812 | 3839 | | |
3813 | 3840 | | |
3814 | | - | |
| 3841 | + | |
| 3842 | + | |
| 3843 | + | |
| 3844 | + | |
| 3845 | + | |
| 3846 | + | |
| 3847 | + | |
3815 | 3848 | | |
3816 | 3849 | | |
3817 | 3850 | | |
3818 | 3851 | | |
3819 | 3852 | | |
3820 | 3853 | | |
3821 | | - | |
| 3854 | + | |
| 3855 | + | |
| 3856 | + | |
| 3857 | + | |
| 3858 | + | |
| 3859 | + | |
| 3860 | + | |
3822 | 3861 | | |
3823 | 3862 | | |
3824 | 3863 | | |
| |||
3829 | 3868 | | |
3830 | 3869 | | |
3831 | 3870 | | |
3832 | | - | |
| 3871 | + | |
| 3872 | + | |
| 3873 | + | |
| 3874 | + | |
| 3875 | + | |
| 3876 | + | |
| 3877 | + | |
3833 | 3878 | | |
3834 | 3879 | | |
3835 | 3880 | | |
| |||
4037 | 4082 | | |
4038 | 4083 | | |
4039 | 4084 | | |
4040 | | - | |
| 4085 | + | |
| 4086 | + | |
| 4087 | + | |
| 4088 | + | |
| 4089 | + | |
| 4090 | + | |
| 4091 | + | |
4041 | 4092 | | |
4042 | 4093 | | |
4043 | 4094 | | |
| |||
4076 | 4127 | | |
4077 | 4128 | | |
4078 | 4129 | | |
4079 | | - | |
| 4130 | + | |
| 4131 | + | |
| 4132 | + | |
| 4133 | + | |
| 4134 | + | |
| 4135 | + | |
| 4136 | + | |
4080 | 4137 | | |
4081 | 4138 | | |
4082 | 4139 | | |
| |||
4095 | 4152 | | |
4096 | 4153 | | |
4097 | 4154 | | |
4098 | | - | |
| 4155 | + | |
| 4156 | + | |
| 4157 | + | |
| 4158 | + | |
| 4159 | + | |
| 4160 | + | |
| 4161 | + | |
4099 | 4162 | | |
4100 | 4163 | | |
4101 | 4164 | | |
| |||
5087 | 5150 | | |
5088 | 5151 | | |
5089 | 5152 | | |
5090 | | - | |
5091 | | - | |
| 5153 | + | |
| 5154 | + | |
| 5155 | + | |
| 5156 | + | |
| 5157 | + | |
| 5158 | + | |
| 5159 | + | |
5092 | 5160 | | |
5093 | 5161 | | |
5094 | 5162 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
139 | 159 | | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
140 | 168 | | |
| 169 | + | |
141 | 170 | | |
142 | 171 | | |
143 | 172 | | |
| |||
0 commit comments