From 4d4fe9dbc0eb0d0eaa9a608474fecc892626f542 Mon Sep 17 00:00:00 2001 From: "Yan, Zheng" Date: Wed, 5 Aug 2015 15:19:13 +0800 Subject: [PATCH] crypto: fix unbalanced ceph::crypto::init/ceph::crypto:shutdown we may create a CephContext without calling common_init_finish(), then delete the CephContext. In this case, ceph::crypto:init() is not called, so CephContext::~CephContext() should not call ceph::crypto::shutdown(). Fixes: #12598 Signed-off-by: Yan, Zheng Reviewed-by: John Spray --- src/common/ceph_context.cc | 10 +++++++++- src/common/ceph_context.h | 5 +++++ src/common/ceph_crypto.cc | 1 + src/common/common_init.cc | 2 +- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/common/ceph_context.cc b/src/common/ceph_context.cc index 7a5c52bb0621f..877dd539284ca 100644 --- a/src/common/ceph_context.cc +++ b/src/common/ceph_context.cc @@ -399,6 +399,7 @@ CephContext::CephContext(uint32_t module_type_) _conf(new md_config_t()), _log(NULL), _module_type(module_type_), + _crypto_inited(false), _service_thread(NULL), _log_obs(NULL), _admin_socket(NULL), @@ -507,7 +508,14 @@ CephContext::~CephContext() delete _crypto_none; delete _crypto_aes; - ceph::crypto::shutdown(); + if (_crypto_inited) + ceph::crypto::shutdown(); +} + +void CephContext::init_crypto() +{ + ceph::crypto::init(this); + _crypto_inited = true; } void CephContext::start_service_thread() diff --git a/src/common/ceph_context.h b/src/common/ceph_context.h index 95f6ea59bda3f..da9e3f64a5d87 100644 --- a/src/common/ceph_context.h +++ b/src/common/ceph_context.h @@ -75,6 +75,9 @@ public: md_config_t *_conf; ceph::log::Log *_log; + /* init ceph::crypto */ + void init_crypto(); + /* Start the Ceph Context's service thread */ void start_service_thread(); @@ -137,6 +140,8 @@ private: uint32_t _module_type; + bool _crypto_inited; + /* libcommon service thread. * SIGHUP wakes this thread, which then reopens logfiles */ friend class CephContextServiceThread; diff --git a/src/common/ceph_crypto.cc b/src/common/ceph_crypto.cc index 70f934bc90116..de5a03b790abb 100644 --- a/src/common/ceph_crypto.cc +++ b/src/common/ceph_crypto.cc @@ -76,6 +76,7 @@ void ceph::crypto::init(CephContext *cct) void ceph::crypto::shutdown() { pthread_mutex_lock(&crypto_init_mutex); + assert(crypto_refs > 0); if (--crypto_refs == 0) { NSS_ShutdownContext(crypto_context); crypto_context = NULL; diff --git a/src/common/common_init.cc b/src/common/common_init.cc index 90232c19a63da..a580309463f81 100644 --- a/src/common/common_init.cc +++ b/src/common/common_init.cc @@ -115,7 +115,7 @@ void complain_about_parse_errors(CephContext *cct, * same application. */ void common_init_finish(CephContext *cct, int flags) { - ceph::crypto::init(cct); + cct->init_crypto(); if (!(flags & CINIT_FLAG_NO_DAEMON_ACTIONS)) cct->start_service_thread(); -- 2.39.5