diff options
author | Djalal Harouni <tixxdz@opendz.org> | 2014-10-13 23:27:07 +0100 |
---|---|---|
committer | Djalal Harouni <tixxdz@opendz.org> | 2014-10-14 20:50:26 +0100 |
commit | 72b68651f99424fb2e46c662c0b438b97160b805 (patch) | |
tree | 6c7a0f48a33c75511a6c581321e2e09ac3e7f70e /test | |
parent | 549afd6bcbf9b8ba09ec1c496f73db11ff57e220 (diff) | |
download | kdbus-bus-72b68651f99424fb2e46c662c0b438b97160b805.tar.gz kdbus-bus-72b68651f99424fb2e46c662c0b438b97160b805.tar.bz2 kdbus-bus-72b68651f99424fb2e46c662c0b438b97160b805.zip |
test-util: move RUN_UNPRIVILEGED definitions to kdbus-util.h
Will be used by other tests
Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
Diffstat (limited to 'test')
-rw-r--r-- | test/kdbus-util.h | 45 | ||||
-rw-r--r-- | test/test-policy-priv.c | 54 |
2 files changed, 50 insertions, 49 deletions
diff --git a/test/kdbus-util.h b/test/kdbus-util.h index ac1af23e30b..9e48e72d2d8 100644 --- a/test/kdbus-util.h +++ b/test/kdbus-util.h @@ -32,6 +32,9 @@ #define POOL_SIZE (16 * 1024LU * 1024LU) +#define UNPRIV_UID 65534 +#define UNPRIV_GID 65534 + /* Dump as user of process, useful for user namespace testing */ #define SUID_DUMP_USER 1 @@ -41,6 +44,48 @@ extern int kdbus_util_verbose; if (kdbus_util_verbose) \ printf(X) +#define RUN_UNPRIVILEGED(child_uid, child_gid, _child_, _parent_) ({ \ + pid_t pid, rpid; \ + int ret; \ + \ + pid = fork(); \ + if (pid == 0) { \ + ret = drop_privileges(child_uid, child_gid); \ + if (ret < 0) \ + _exit(ret); \ + \ + _child_; \ + _exit(0); \ + } else if (pid > 0) { \ + _parent_; \ + rpid = waitpid(pid, &ret, 0); \ + ASSERT_RETURN(rpid == pid); \ + ASSERT_RETURN(WIFEXITED(ret)); \ + ASSERT_RETURN(WEXITSTATUS(ret) == 0); \ + ret = TEST_OK; \ + } else { \ + ret = pid; \ + } \ + \ + ret; \ + }) + +#define RUN_UNPRIVILEGED_CONN(_var_, _bus_, _code_) \ + RUN_UNPRIVILEGED(UNPRIV_UID, UNPRIV_GID, ({ \ + struct kdbus_conn *_var_; \ + _var_ = kdbus_hello(_bus_, 0, NULL, 0); \ + ASSERT_EXIT(_var_); \ + _code_; \ + kdbus_conn_free(_var_); \ + }), ({ 0; })) + +/* Enums for parent if it should drop privs or not */ +enum kdbus_drop_parent { + DO_NOT_DROP, + DROP_SAME_UNPRIV, + DROP_OTHER_UNPRIV, +}; + struct kdbus_conn { int fd; uint64_t id; diff --git a/test/test-policy-priv.c b/test/test-policy-priv.c index 92a2347c30d..cf73f729099 100644 --- a/test/test-policy-priv.c +++ b/test/test-policy-priv.c @@ -16,50 +16,6 @@ #include "kdbus-util.h" #include "kdbus-enum.h" -#define UNPRIV_UID 65534 -#define UNPRIV_GID 65534 - -enum kdbus_drop_user { - DO_NOT_DROP, - DROP_SAME_UNPRIV_USER, - DROP_OTHER_UNPRIV_USER, -}; - -#define RUN_UNPRIVILEGED(child_uid, child_gid, _child_, _parent_) ({ \ - pid_t pid, rpid; \ - int ret; \ - \ - pid = fork(); \ - if (pid == 0) { \ - ret = drop_privileges(child_uid, child_gid); \ - if (ret < 0) \ - _exit(ret); \ - \ - _child_; \ - _exit(0); \ - } else if (pid > 0) { \ - _parent_; \ - rpid = waitpid(pid, &ret, 0); \ - ASSERT_RETURN(rpid == pid); \ - ASSERT_RETURN(WIFEXITED(ret)); \ - ASSERT_RETURN(WEXITSTATUS(ret) == 0); \ - ret = TEST_OK; \ - } else { \ - ret = pid; \ - } \ - \ - ret; \ - }) - -#define RUN_UNPRIVILEGED_CONN(_var_, _bus_, _code_) \ - RUN_UNPRIVILEGED(UNPRIV_UID, UNPRIV_GID, ({ \ - struct kdbus_conn *_var_; \ - _var_ = kdbus_hello(_bus_, 0, NULL, 0); \ - ASSERT_EXIT(_var_); \ - _code_; \ - kdbus_conn_free(_var_); \ - }), ({ 0; })) - static int test_policy_priv_by_id(const char *bus, struct kdbus_conn *conn_dst, bool drop_second_user, @@ -101,7 +57,7 @@ static int test_policy_priv_by_broadcast(const char *bus, uint64_t expected_cookie = time(NULL) ^ 0xdeadbeef; /* Drop to another unprivileged user other than UNPRIV_UID */ - if (drop_second_user == DROP_OTHER_UNPRIV_USER) { + if (drop_second_user == DROP_OTHER_UNPRIV) { second_uid = UNPRIV_UID - 1; second_gid = UNPRIV_GID - 1; } @@ -289,7 +245,7 @@ static int test_priv_before_policy_upload(struct kdbus_test_env *env) */ ret = test_policy_priv_by_broadcast(env->buspath, NULL, - DROP_SAME_UNPRIV_USER, + DROP_SAME_UNPRIV, EXIT_SUCCESS, EXIT_SUCCESS); ASSERT_RETURN(ret == 0); @@ -301,7 +257,7 @@ static int test_priv_before_policy_upload(struct kdbus_test_env *env) */ ret = test_policy_priv_by_broadcast(env->buspath, NULL, - DROP_OTHER_UNPRIV_USER, + DROP_OTHER_UNPRIV, -ETIMEDOUT, -ETIMEDOUT); ASSERT_RETURN(ret == 0); @@ -370,7 +326,7 @@ static int test_broadcast_after_policy_upload(struct kdbus_test_env *env) */ ret = test_policy_priv_by_broadcast(env->buspath, NULL, - DROP_SAME_UNPRIV_USER, + DROP_SAME_UNPRIV, EXIT_SUCCESS, EXIT_SUCCESS); ASSERT_RETURN(ret == 0); @@ -557,7 +513,7 @@ static int test_broadcast_after_policy_upload(struct kdbus_test_env *env) */ ret = test_policy_priv_by_broadcast(env->buspath, NULL, - DROP_OTHER_UNPRIV_USER, + DROP_OTHER_UNPRIV, -ETIMEDOUT, -ETIMEDOUT); ASSERT_RETURN(ret == 0); |