Fix coverity issue: CID 1466486 - Resource leak in OSSL_STORE

Note that although this is a false positive currently, it could become possible if any of the methods called
change behaviour - so it is safer to add the fix than to ignore it. Added a simple test so that I could prove this was the case.

Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/12847)
This commit is contained in:
Shane Lontis 2020-09-10 16:40:24 +10:00
parent c1aba0763c
commit 3481694946
4 changed files with 92 additions and 1 deletions

View File

@ -178,6 +178,7 @@ OSSL_STORE_open_with_libctx(const char *uri,
}
OSSL_STORE_LOADER_free(fetched_loader);
OPENSSL_free(propq_copy);
OPENSSL_free(ctx);
return NULL;
}

View File

@ -36,7 +36,7 @@ IF[{- !$disabled{tests} -}]
destest mdc2test \
exptest \
evp_pkey_provided_test evp_test evp_extra_test evp_extra_test2 \
evp_fetch_prov_test acvp_test evp_libctx_test \
evp_fetch_prov_test acvp_test evp_libctx_test ossl_store_test \
v3nametest v3ext \
evp_pkey_provided_test evp_test evp_extra_test evp_extra_test2 \
evp_fetch_prov_test v3nametest v3ext \
@ -166,6 +166,10 @@ IF[{- !$disabled{tests} -}]
DEPEND[acvp_test]=../libcrypto.a libtestutil.a
ENDIF
SOURCE[ossl_store_test]=ossl_store_test.c
INCLUDE[ossl_store_test]=../include ../apps/include
DEPEND[ossl_store_test]=../libcrypto.a libtestutil.a
SOURCE[provider_status_test]=provider_status_test.c
INCLUDE[provider_status_test]=../include ../apps/include
DEPEND[provider_status_test]=../libcrypto.a libtestutil.a

67
test/ossl_store_test.c Normal file
View File

@ -0,0 +1,67 @@
/*
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <openssl/store.h>
#include <openssl/ui.h>
#include "testutil.h"
typedef enum OPTION_choice {
OPT_ERR = -1,
OPT_EOF = 0,
OPT_INFILE,
OPT_TEST_ENUM
} OPTION_CHOICE;
static const char *infile = NULL;
static int test_store_open(void)
{
int ret = 0;
OSSL_STORE_CTX *sctx = NULL;
UI_METHOD *ui_method = NULL;
ret = TEST_ptr(ui_method= UI_create_method("DummyUI"))
&& TEST_ptr(sctx = OSSL_STORE_open_with_libctx(infile, NULL, NULL,
ui_method, NULL,
NULL, NULL));
UI_destroy_method(ui_method);
OSSL_STORE_close(sctx);
return ret;
}
const OPTIONS *test_get_options(void)
{
static const OPTIONS test_options[] = {
OPT_TEST_OPTIONS_DEFAULT_USAGE,
{ "in", OPT_INFILE, '<', },
{ NULL }
};
return test_options;
}
int setup_tests(void)
{
OPTION_CHOICE o;
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_INFILE:
infile = opt_arg();
break;
case OPT_TEST_CASES:
break;
default:
case OPT_ERR:
return 0;
}
}
ADD_TEST(test_store_open);
return 1;
}

View File

@ -0,0 +1,19 @@
#! /usr/bin/env perl
# Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the Apache License 2.0 (the "License"). You may not use
# this file except in compliance with the License. You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html
use strict;
use warnings;
use OpenSSL::Test::Simple;
use OpenSSL::Test qw/:DEFAULT srctop_file/;
setup("test_ossl_store");
plan tests => 1;
ok(run(test(["ossl_store_test", "-in", srctop_file("test", "testrsa.pem")])));