Skip to content

Commit 0e771ca

Browse files
CDRIVER-5511 disable loading Cyrus plugins on Windows by default (#1561)
* CDRIVER-5511 disable loading Cyrus plugins on Windows by default adds the CMake option `CYRUS_PLUGIN_PATH_PREFIX` to opt-in to loading plug-ins --------- Co-authored-by: Ezra Chung <[email protected]>
1 parent 788630f commit 0e771ca

File tree

7 files changed

+73
-3
lines changed

7 files changed

+73
-3
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ mongo_setting(
150150
]]
151151
)
152152

153+
mongo_setting(CYRUS_PLUGIN_PATH_PREFIX "An absolute path prefix to enable loading Cyrus SASL plugins on Windows"
154+
TYPE STRING
155+
VISIBLE_IF [[ENABLE_SASL STREQUAL "CYRUS" AND WIN32]]
156+
)
157+
153158
mongo_setting(ENABLE_CLIENT_SIDE_ENCRYPTION "Enable In-Use Encryption support. Requires additional support libraries."
154159
OPTIONS ON OFF AUTO
155160
DEFAULT VALUE AUTO)

NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
libmongoc 1.26.2 (unreleased)
2+
=============================
3+
4+
Cyrus SASL:
5+
6+
* Disable plugin loading with Cyrus SASL on Windows by default. To re-enable, set the CMake option `CYRUS_PLUGIN_PATH_PREFIX` to the absolute path prefix of the Cyrus SASL plugins.
7+
18
libmongoc 1.26.1
29
================
310

src/libmongoc/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,12 @@ if (MONGOC_ENABLE_STATIC_BUILD)
814814
set_target_properties (mcd_rpc PROPERTIES OUTPUT_NAME "mcd-rpc")
815815
endif ()
816816

817+
set_property(
818+
SOURCE ${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-cyrus.c
819+
APPEND PROPERTY COMPILE_DEFINITIONS
820+
"MONGOC_CYRUS_PLUGIN_PATH_PREFIX=$<IF:$<STREQUAL:${CYRUS_PLUGIN_PATH_PREFIX},>,NULL,\"${CYRUS_PLUGIN_PATH_PREFIX}\">"
821+
)
822+
817823
if (ENABLE_SHARED)
818824
add_library (mongoc_shared SHARED ${SOURCES} ${HEADERS} ${HEADERS_FORWARDING})
819825
if(WIN32)

src/libmongoc/doc/authentication.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ GSSAPI (Kerberos) Authentication
7979

8080
.. note::
8181

82-
On UNIX-like environments, Kerberos support requires compiling the driver against ``cyrus-sasl``.
82+
On UNIX-like environments, Kerberos support requires compiling the driver against `Cyrus SASL <https://www.cyrusimap.org/sasl/>`_.
8383

84-
On Windows, Kerberos support requires compiling the driver against Windows Native SSPI or ``cyrus-sasl``. The default configuration of the driver will use Windows Native SSPI.
84+
On Windows, Kerberos support requires compiling the driver against Windows Native SSPI or Cyrus SASL. The default configuration of the driver will use Windows Native SSPI.
85+
Using Cyrus SASL on Windows requires configuring the CMake option ``CYRUS_PLUGIN_PATH_PREFIX`` to the absolute path prefix of the ``GSSAPI`` plugin to enable loading the plugin.
8586

8687
To modify the default configuration, use the cmake option ``ENABLE_SASL``.
8788

src/libmongoc/src/mongoc/mongoc-cyrus-private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ struct _mongoc_cyrus_t {
4646
#define SASL_CALLBACK_FN(_f) ((int (*) (void)) ((void (*) (void)) (_f)))
4747
#endif
4848

49+
int
50+
_mongoc_cyrus_verifyfile_cb (void *context, const char *file, sasl_verify_type_t type);
4951
void
5052
_mongoc_cyrus_init (mongoc_cyrus_t *sasl);
5153
bool

src/libmongoc/src/mongoc/mongoc-cyrus.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,49 @@ _mongoc_cyrus_get_user (mongoc_cyrus_t *sasl, int param_id, const char **result,
126126
return (sasl->credentials.user != NULL) ? SASL_OK : SASL_FAIL;
127127
}
128128

129+
static const char *
130+
sasl_verify_type_to_str (sasl_verify_type_t type)
131+
{
132+
switch (type) {
133+
case SASL_VRFY_PLUGIN:
134+
return "SASL_VRFY_PLUGIN";
135+
case SASL_VRFY_CONF:
136+
return "SASL_VRFY_CONF";
137+
case SASL_VRFY_PASSWD:
138+
return "SASL_VRFY_PASSWD";
139+
case SASL_VRFY_OTHER:
140+
return "SASL_VRFY_OTHER";
141+
default:
142+
return "Unknown";
143+
}
144+
}
145+
146+
int
147+
_mongoc_cyrus_verifyfile_cb (void *context, const char *file, sasl_verify_type_t type)
148+
{
149+
TRACE ("Attempting to load file: `%s`. Type is %s\n", file, sasl_verify_type_to_str (type));
150+
151+
#ifdef _WIN32
152+
// On Windows, Cyrus SASL hard-codes the plugin path.
153+
// Only permit loading plugin from user configured path to prevent unintentional library loading.
154+
if (type == SASL_VRFY_PLUGIN) {
155+
const char *path_prefix = MONGOC_CYRUS_PLUGIN_PATH_PREFIX;
156+
bool has_valid_prefix = (path_prefix && file == strstr (file, path_prefix));
157+
// Check if `file` has necessary prefix.
158+
if (has_valid_prefix) {
159+
return SASL_OK;
160+
}
161+
MONGOC_WARNING ("Refusing to load Cyrus SASL plugin at: '%s'. If needed, set CYRUS_PLUGIN_PATH_PREFIX (currently "
162+
"'%s') to the absolute path prefix of the plugin during build configuration of the C Driver.",
163+
file,
164+
path_prefix ? path_prefix : "(unset)");
165+
return SASL_CONTINUE;
166+
}
167+
#endif
168+
169+
return SASL_OK;
170+
}
171+
129172

130173
void
131174
_mongoc_cyrus_init (mongoc_cyrus_t *sasl)
@@ -134,6 +177,7 @@ _mongoc_cyrus_init (mongoc_cyrus_t *sasl)
134177
{SASL_CB_USER, SASL_CALLBACK_FN (_mongoc_cyrus_get_user), sasl},
135178
{SASL_CB_PASS, SASL_CALLBACK_FN (_mongoc_cyrus_get_pass), sasl},
136179
{SASL_CB_CANON_USER, SASL_CALLBACK_FN (_mongoc_cyrus_canon_user), sasl},
180+
{SASL_CB_VERIFYFILE, SASL_CALLBACK_FN (_mongoc_cyrus_verifyfile_cb), NULL},
137181
{SASL_CB_LIST_END}};
138182

139183
BSON_ASSERT (sasl);

src/libmongoc/src/mongoc/mongoc-init.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252

5353
#ifdef MONGOC_ENABLE_SASL_CYRUS
5454
#include <sasl/sasl.h>
55+
#include <mongoc-cyrus-private.h> // _mongoc_cyrus_verifyfile_cb
5556

5657
static void *
5758
mongoc_cyrus_mutex_alloc (void)
@@ -110,7 +111,11 @@ static BSON_ONCE_FUN (_mongoc_do_init)
110111
sasl_set_mutex (
111112
mongoc_cyrus_mutex_alloc, mongoc_cyrus_mutex_lock, mongoc_cyrus_mutex_unlock, mongoc_cyrus_mutex_free);
112113

113-
status = sasl_client_init (NULL);
114+
sasl_callback_t callbacks[] = {// Include callback to disable loading plugins.
115+
{SASL_CB_VERIFYFILE, SASL_CALLBACK_FN (_mongoc_cyrus_verifyfile_cb), NULL},
116+
{SASL_CB_LIST_END}};
117+
118+
status = sasl_client_init (callbacks);
114119
BSON_ASSERT (status == SASL_OK);
115120
#endif
116121

0 commit comments

Comments
 (0)