Beispiel #1
0
static int kdbus_test_broadcast_quota(struct kdbus_test_env *env)
{
	int ret;
	uint64_t offset;
	unsigned int i;
	struct kdbus_msg *msg;
	struct kdbus_conn *privileged_a;
	struct kdbus_conn *privileged_b;
	struct kdbus_conn *holder;
	struct kdbus_policy_access access = {
		.type = KDBUS_POLICY_ACCESS_WORLD,
		.id = getuid(),
		.access = KDBUS_POLICY_TALK,
	};
	uint64_t expected_cookie = time(NULL) ^ 0xdeadbeef;

	holder = kdbus_hello_registrar(env->buspath, "com.example.a",
				       &access, 1,
				       KDBUS_HELLO_POLICY_HOLDER);
	ASSERT_RETURN(holder);

	privileged_a = kdbus_hello(env->buspath, 0, NULL, 0);
	ASSERT_RETURN(privileged_a);

	privileged_b = kdbus_hello(env->buspath, 0, NULL, 0);
	ASSERT_RETURN(privileged_b);

	/* Acquire name with access world so they can talk to us */
	ret = kdbus_name_acquire(privileged_a, "com.example.a", NULL);
	ASSERT_RETURN(ret >= 0);

	/* Broadcast matches for privileged connections */
	ret = kdbus_add_match_empty(privileged_a);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_add_match_empty(privileged_b);
	ASSERT_RETURN(ret == 0);

	/*
	 * We start accouting after KDBUS_CONN_MAX_MSGS_UNACCOUNTED
	 * so the first sender will at least send
	 * KDBUS_CONN_MAX_MSGS_UNACCOUNTED + KDBUS_CONN_MAX_MSGS_PER_USER
	 */
	ret = RUN_UNPRIVILEGED_CONN(unpriv, env->buspath, ({
		unsigned int cnt;

		cnt = kdbus_fill_conn_queue(unpriv, KDBUS_DST_ID_BROADCAST,
					    MAX_USER_TOTAL_MSGS);
		ASSERT_EXIT(cnt == MAX_USER_TOTAL_MSGS);

		/*
		 * Another message that will trigger the lost count
		 *
		 * Broadcasts always succeed
		 */
		ret = kdbus_msg_send(unpriv, NULL, 0xdeadbeef, 0, 0,
				     0, KDBUS_DST_ID_BROADCAST);
		ASSERT_EXIT(ret == 0);
	}));
Beispiel #2
0
int kdbus_test_daemon(struct kdbus_test_env *env)
{
	struct pollfd fds[2];
	int count;
	int ret;

	/* This test doesn't make any sense in non-interactive mode */
	if (!kdbus_util_verbose)
		return TEST_OK;

	printf("Created connection %llu on bus '%s'\n",
		(unsigned long long) env->conn->id, env->buspath);

	kdbus_name_acquire(env->conn, "com.example.kdbus-test", 0);
	kdbus_printf("  Aquired name: com.example.kdbus-test\n");

	fds[0].fd = env->conn->fd;
	fds[1].fd = STDIN_FILENO;

	printf("Monitoring connections:\n");

	for (count = 0;; count++) {
		int i, nfds = sizeof(fds) / sizeof(fds[0]);

		for (i = 0; i < nfds; i++) {
			fds[i].events = POLLIN | POLLPRI | POLLHUP;
			fds[i].revents = 0;
		}

		ret = poll(fds, nfds, -1);
		if (ret <= 0)
			break;

		if (fds[0].revents & POLLIN)
			kdbus_msg_recv(env->conn, NULL);

		/* stdin */
		if (fds[1].revents & POLLIN)
			break;
	}

	printf("Closing bus connection\n");

	return TEST_OK;
}
Beispiel #3
0
int kdbus_test_policy_ns(struct kdbus_test_env *env)
{
	int i;
	int ret;
	struct kdbus_conn *activator = NULL;
	struct kdbus_conn *policy_holder = NULL;
	char *bus = env->buspath;

	ret = test_is_capable(CAP_SETUID, CAP_SETGID, -1);
	ASSERT_RETURN(ret >= 0);

	/* no enough privileges, SKIP test */
	if (!ret)
		return TEST_SKIP;

	/* we require user-namespaces */
	if (access("/proc/self/uid_map", F_OK) != 0)
		return TEST_SKIP;

	/* uids/gids must be mapped */
	if (!all_uids_gids_are_mapped())
		return TEST_SKIP;

	conn_db = calloc(MAX_CONN, sizeof(struct kdbus_conn *));
	ASSERT_RETURN(conn_db);

	memset(conn_db, 0, MAX_CONN * sizeof(struct kdbus_conn *));

	conn_db[0] = kdbus_hello(bus, 0, NULL, 0);
	ASSERT_RETURN(conn_db[0]);

	ret = kdbus_add_match_empty(conn_db[0]);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_fork_test_by_id(bus, conn_db, -EPERM, -EPERM);
	ASSERT_EXIT(ret == 0);

	ret = kdbus_register_policy_holder(bus, POLICY_NAME,
					   &policy_holder);
	ASSERT_RETURN(ret == 0);

	/* Try to register the same name with an activator */
	ret = kdbus_register_same_activator(bus, POLICY_NAME,
					    &activator);
	ASSERT_RETURN(ret == 0);

	/* Acquire POLICY_NAME */
	ret = kdbus_name_acquire(conn_db[0], POLICY_NAME, NULL);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_normal_test(bus, POLICY_NAME, conn_db);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_list(conn_db[0], KDBUS_LIST_NAMES |
				     KDBUS_LIST_UNIQUE |
				     KDBUS_LIST_ACTIVATORS |
				     KDBUS_LIST_QUEUED);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_fork_test(bus, POLICY_NAME, conn_db, EXIT_SUCCESS);
	ASSERT_RETURN(ret == 0);

	/*
	 * children connections are able to talk to conn_db[0] since
	 * current POLICY_NAME TALK type is KDBUS_POLICY_ACCESS_WORLD,
	 * so expect EXIT_SUCCESS when sending from child. However,
	 * since the child's connection does not own any well-known
	 * name, The parent connection conn_db[0] should fail with
	 * -EPERM but since it is a privileged bus user the TALK is
	 *  allowed.
	 */
	ret = kdbus_fork_test_by_id(bus, conn_db,
				    EXIT_SUCCESS, EXIT_SUCCESS);
	ASSERT_EXIT(ret == 0);

	/*
	 * Connections that can talk are perhaps being destroyed now.
	 * Restrict the policy and purge cache entries where the
	 * conn_db[0] is the destination.
	 *
	 * Now only connections with uid == 0 are allowed to talk.
	 */
	ret = kdbus_set_policy_talk(policy_holder, POLICY_NAME,
				    geteuid(), KDBUS_POLICY_ACCESS_USER);
	ASSERT_RETURN(ret == 0);

	/*
	 * Testing connections (FORK+DROP) again:
	 * After setting the policy re-check connections
	 * we expect the children to fail with -EPERM
	 */
	ret = kdbus_fork_test(bus, POLICY_NAME, conn_db, -EPERM);
	ASSERT_RETURN(ret == 0);

	/*
	 * Now expect that both parent and child to fail.
	 *
	 * Child should fail with -EPERM since we just restricted
	 * the POLICY_NAME TALK to uid 0 and its uid is 65534.
	 *
	 * Since the parent's connection will timeout when receiving
	 * from the child, we never continue. FWIW just put -EPERM.
	 */
	ret = kdbus_fork_test_by_id(bus, conn_db, -EPERM, -EPERM);
	ASSERT_EXIT(ret == 0);

	/* Check if the name can be reached in a new userns */
	ret = kdbus_clone_userns_test(bus, POLICY_NAME, conn_db, -EPERM);
	ASSERT_RETURN(ret == 0);

	for (i = 0; i < MAX_CONN; i++)
		kdbus_conn_free(conn_db[i]);

	kdbus_conn_free(activator);
	kdbus_conn_free(policy_holder);

	free(conn_db);

	return ret;
}
Beispiel #4
0
int kdbus_test_activator(struct kdbus_test_env *env)
{
	int ret;
	struct kdbus_conn *activator;
	struct pollfd fds[2];
	bool activator_done = false;
	struct kdbus_policy_access access[2];

	access[0].type = KDBUS_POLICY_ACCESS_USER;
	access[0].id = 1001;
	access[0].access = KDBUS_POLICY_OWN;

	access[1].type = KDBUS_POLICY_ACCESS_WORLD;
	access[1].access = KDBUS_POLICY_TALK;

	activator = kdbus_hello_activator(env->buspath, "foo.test.activator",
					  access, 2);
	ASSERT_RETURN(activator);

	ret = kdbus_add_match_empty(env->conn);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_name_list(env->conn, KDBUS_NAME_LIST_NAMES |
					 KDBUS_NAME_LIST_UNIQUE |
					 KDBUS_NAME_LIST_ACTIVATORS |
					 KDBUS_NAME_LIST_QUEUED);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_msg_send(env->conn, "foo.test.activator", 0xdeafbeef,
			     0, 0, 0, KDBUS_DST_ID_NAME);
	ASSERT_RETURN(ret == 0);

	fds[0].fd = activator->fd;
	fds[1].fd = env->conn->fd;

	kdbus_printf("-- entering poll loop ...\n");

	for (;;) {
		int i, nfds = sizeof(fds) / sizeof(fds[0]);

		for (i = 0; i < nfds; i++) {
			fds[i].events = POLLIN | POLLPRI;
			fds[i].revents = 0;
		}

		ret = poll(fds, nfds, 3000);
		ASSERT_RETURN(ret >= 0);

		ret = kdbus_name_list(env->conn, KDBUS_NAME_LIST_NAMES);
		ASSERT_RETURN(ret == 0);

		if ((fds[0].revents & POLLIN) && !activator_done) {
			kdbus_printf("Starter was called back!\n");

			ret = kdbus_name_acquire(env->conn,
						 "foo.test.activator",
						 KDBUS_NAME_REPLACE_EXISTING);
			ASSERT_RETURN(ret == 0);

			activator_done = true;
		}

		if (fds[1].revents & POLLIN) {
			kdbus_msg_recv(env->conn, NULL);
			break;
		}
	}

	kdbus_conn_free(activator);

	return TEST_OK;
}
Beispiel #5
0
int kdbus_test_chat(struct kdbus_test_env *env)
{
	int ret, cookie;
	struct kdbus_conn *conn_a, *conn_b;
	struct pollfd fds[2];
	uint64_t flags;
	int count;

	conn_a = kdbus_hello(env->buspath, 0, NULL, 0);
	conn_b = kdbus_hello(env->buspath, 0, NULL, 0);
	ASSERT_RETURN(conn_a && conn_b);

	flags = KDBUS_NAME_ALLOW_REPLACEMENT;
	ret = kdbus_name_acquire(conn_a, "foo.bar.test", &flags);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_name_acquire(conn_a, "foo.bar.baz", NULL);
	ASSERT_RETURN(ret == 0);

	flags = KDBUS_NAME_QUEUE;
	ret = kdbus_name_acquire(conn_b, "foo.bar.baz", &flags);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_name_acquire(conn_a, "foo.bar.double", NULL);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_name_acquire(conn_a, "foo.bar.double", NULL);
	ASSERT_RETURN(ret == -EALREADY);

	ret = kdbus_name_release(conn_a, "foo.bar.double");
	ASSERT_RETURN(ret == 0);

	ret = kdbus_name_release(conn_a, "foo.bar.double");
	ASSERT_RETURN(ret == -ESRCH);

	ret = kdbus_list(conn_b, KDBUS_LIST_UNIQUE |
				 KDBUS_LIST_NAMES  |
				 KDBUS_LIST_QUEUED |
				 KDBUS_LIST_ACTIVATORS);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_add_match_empty(conn_a);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_add_match_empty(conn_b);
	ASSERT_RETURN(ret == 0);

	cookie = 0;
	ret = kdbus_msg_send(conn_b, NULL, 0xc0000000 | cookie, 0, 0, 0,
			     KDBUS_DST_ID_BROADCAST);
	ASSERT_RETURN(ret == 0);

	fds[0].fd = conn_a->fd;
	fds[1].fd = conn_b->fd;

	kdbus_printf("-- entering poll loop ...\n");

	for (count = 0;; count++) {
		int i, nfds = sizeof(fds) / sizeof(fds[0]);

		for (i = 0; i < nfds; i++) {
			fds[i].events = POLLIN | POLLPRI | POLLHUP;
			fds[i].revents = 0;
		}

		ret = poll(fds, nfds, 3000);
		ASSERT_RETURN(ret >= 0);

		if (fds[0].revents & POLLIN) {
			if (count > 2)
				kdbus_name_release(conn_a, "foo.bar.baz");

			ret = kdbus_msg_recv(conn_a, NULL, NULL);
			ASSERT_RETURN(ret == 0);
			ret = kdbus_msg_send(conn_a, NULL,
					     0xc0000000 | cookie++,
					     0, 0, 0, conn_b->id);
			ASSERT_RETURN(ret == 0);
		}

		if (fds[1].revents & POLLIN) {
			ret = kdbus_msg_recv(conn_b, NULL, NULL);
			ASSERT_RETURN(ret == 0);
			ret = kdbus_msg_send(conn_b, NULL,
					     0xc0000000 | cookie++,
					     0, 0, 0, conn_a->id);
			ASSERT_RETURN(ret == 0);
		}

		ret = kdbus_list(conn_b, KDBUS_LIST_UNIQUE |
					 KDBUS_LIST_NAMES  |
					 KDBUS_LIST_QUEUED |
					 KDBUS_LIST_ACTIVATORS);
		ASSERT_RETURN(ret == 0);

		if (count > 10)
			break;
	}

	kdbus_printf("-- closing bus connections\n");
	kdbus_conn_free(conn_a);
	kdbus_conn_free(conn_b);

	return TEST_OK;
}