Skip to content

Commit e69dbac

Browse files
kevinAlbseramongodb
andcommitted
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 d40ca94 commit e69dbac

File tree

7 files changed

+78
-9
lines changed

7 files changed

+78
-9
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)) (_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: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,59 @@ _mongoc_cyrus_get_user (mongoc_cyrus_t *sasl,
138138
return (sasl->credentials.user != NULL) ? SASL_OK : SASL_FAIL;
139139
}
140140

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

142185
void
143186
_mongoc_cyrus_init (mongoc_cyrus_t *sasl)
144187
{
145-
sasl_callback_t callbacks[] = {
146-
{SASL_CB_AUTHNAME, SASL_CALLBACK_FN (_mongoc_cyrus_get_user), sasl},
147-
{SASL_CB_USER, SASL_CALLBACK_FN (_mongoc_cyrus_get_user), sasl},
148-
{SASL_CB_PASS, SASL_CALLBACK_FN (_mongoc_cyrus_get_pass), sasl},
149-
{SASL_CB_CANON_USER, SASL_CALLBACK_FN (_mongoc_cyrus_canon_user), sasl},
150-
{SASL_CB_LIST_END}};
188+
sasl_callback_t callbacks[] = {{SASL_CB_AUTHNAME, SASL_CALLBACK_FN (_mongoc_cyrus_get_user), sasl},
189+
{SASL_CB_USER, SASL_CALLBACK_FN (_mongoc_cyrus_get_user), sasl},
190+
{SASL_CB_PASS, SASL_CALLBACK_FN (_mongoc_cyrus_get_pass), sasl},
191+
{SASL_CB_CANON_USER, SASL_CALLBACK_FN (_mongoc_cyrus_canon_user), sasl},
192+
{SASL_CB_VERIFYFILE, SASL_CALLBACK_FN (_mongoc_cyrus_verifyfile_cb), NULL},
193+
{SASL_CB_LIST_END}};
151194

152195
BSON_ASSERT (sasl);
153196

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

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

5454
#ifdef MONGOC_ENABLE_SASL_CYRUS
5555
#include <sasl/sasl.h>
56+
#include <mongoc-cyrus-private.h> // _mongoc_cyrus_verifyfile_cb
5657

5758
static void *
5859
mongoc_cyrus_mutex_alloc (void)
@@ -113,7 +114,11 @@ static BSON_ONCE_FUN (_mongoc_do_init)
113114
mongoc_cyrus_mutex_unlock,
114115
mongoc_cyrus_mutex_free);
115116

116-
status = sasl_client_init (NULL);
117+
sasl_callback_t callbacks[] = {// Include callback to disable loading plugins.
118+
{SASL_CB_VERIFYFILE, SASL_CALLBACK_FN (_mongoc_cyrus_verifyfile_cb), NULL},
119+
{SASL_CB_LIST_END}};
120+
121+
status = sasl_client_init (callbacks);
117122
BSON_ASSERT (status == SASL_OK);
118123
#endif
119124

0 commit comments

Comments
 (0)