From 94a4a847bc4b0e2871fd1db258796cd09de07afb Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Fri, 6 Nov 2015 20:54:13 +0100 Subject: sbus: Check string arguments for valid UTF-8 strings libdbus abort()s when a string argument is not valid UTF-8. Since the arguments sometimes come from untrusted sources, it's better to check the string validity explicitly. --- src/tests/sbus_tests.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/tests/sbus_tests.c') diff --git a/src/tests/sbus_tests.c b/src/tests/sbus_tests.c index 598cc536d..b47265963 100644 --- a/src/tests/sbus_tests.c +++ b/src/tests/sbus_tests.c @@ -43,6 +43,7 @@ #define PILOT_IFACE "test.Pilot" #define PILOT_BLINK "Blink" #define PILOT_EAT "Eat" +#define PILOT_CRASH "Crash" #define PILOT_IFACE_INTROSPECT \ "\n" \ " \n" \ " \n" \ + " \n" \ " \n" \ "\n" @@ -80,6 +82,7 @@ struct pilot_vtable { struct sbus_vtable vtable; sbus_msg_handler_fn Blink; sbus_msg_handler_fn Eat; + sbus_msg_handler_fn Crash; }; const struct sbus_method_meta pilot_methods[] = { @@ -97,6 +100,13 @@ const struct sbus_method_meta pilot_methods[] = { offsetof(struct pilot_vtable, Eat), NULL }, + { + PILOT_CRASH, /* method name */ + NULL, /* in args: manually parsed */ + NULL, /* out args: manually parsed */ + offsetof(struct pilot_vtable, Crash), + NULL + }, { NULL, } }; @@ -169,10 +179,21 @@ static int eat_handler(struct sbus_request *req, void *data) return sbus_request_return_and_finish(req, DBUS_TYPE_INVALID); } +static int crash_handler(struct sbus_request *req, void *data) +{ + /* Pilot crashes by returning a malformed UTF-8 string */ + const char *invalid = "ad\351la\357d"; + + return sbus_request_return_and_finish(req, + DBUS_TYPE_STRING, &invalid, + DBUS_TYPE_INVALID); +} + struct pilot_vtable pilot_impl = { { &pilot_meta, 0 }, .Blink = blink_handler, .Eat = eat_handler, + .Crash = crash_handler, }; static int pilot_test_server_init(struct sbus_connection *server, void *unused) @@ -304,6 +325,33 @@ START_TEST(test_request_parse_bad_args) } END_TEST +START_TEST(test_request_dontcrash) +{ +#ifdef HAVE_DBUSBASICVALUE + TALLOC_CTX *ctx; + DBusConnection *client; + DBusError error = DBUS_ERROR_INIT; + DBusMessage *reply; + + ctx = talloc_new(NULL); + client = test_dbus_setup_mock(ctx, NULL, pilot_test_server_init, NULL); + + reply = test_dbus_call_sync(client, + "/test/leela", + PILOT_IFACE, + PILOT_CRASH, + &error, + DBUS_TYPE_INVALID); /* bad agruments */ + ck_assert(reply == NULL); + ck_assert(dbus_error_is_set(&error)); + ck_assert(dbus_error_has_name(&error, DBUS_ERROR_INVALID_ARGS)); + dbus_error_free(&error); + + talloc_free(ctx); +#endif /* HAVE_DBUSBASICVALUE */ +} +END_TEST + START_TEST(test_introspection) { TALLOC_CTX *ctx; @@ -373,6 +421,7 @@ TCase *create_sbus_tests(void) tcase_add_test(tc, test_raw_handler); tcase_add_test(tc, test_request_parse_ok); tcase_add_test(tc, test_request_parse_bad_args); + tcase_add_test(tc, test_request_dontcrash); tcase_add_test(tc, test_introspection); tcase_add_test(tc, test_sbus_new_error); -- cgit