1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * Ported by John George 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci/* 9f08c3bdfSopenharmony_ci * Test setregid() when executed by root. 10f08c3bdfSopenharmony_ci */ 11f08c3bdfSopenharmony_ci 12f08c3bdfSopenharmony_ci#include "tst_test.h" 13f08c3bdfSopenharmony_ci#include "tst_uid.h" 14f08c3bdfSopenharmony_ci#include "compat_tst_16.h" 15f08c3bdfSopenharmony_ci 16f08c3bdfSopenharmony_cistatic gid_t first_gid, second_gid, root_gid, neg_one = -1; 17f08c3bdfSopenharmony_ci 18f08c3bdfSopenharmony_ci/* 19f08c3bdfSopenharmony_ci * The following structure contains all test data. Each structure in the array 20f08c3bdfSopenharmony_ci * is used for a separate test. The tests are executed in the for loop below. 21f08c3bdfSopenharmony_ci */ 22f08c3bdfSopenharmony_ci 23f08c3bdfSopenharmony_cistruct test_data_t { 24f08c3bdfSopenharmony_ci gid_t *real_gid; 25f08c3bdfSopenharmony_ci gid_t *eff_gid; 26f08c3bdfSopenharmony_ci gid_t *exp_real_usr; 27f08c3bdfSopenharmony_ci gid_t *exp_eff_usr; 28f08c3bdfSopenharmony_ci const char *test_msg; 29f08c3bdfSopenharmony_ci} test_data[] = { 30f08c3bdfSopenharmony_ci { 31f08c3bdfSopenharmony_ci &root_gid, &root_gid, &root_gid, &root_gid, 32f08c3bdfSopenharmony_ci "After setregid(root, root),"}, { 33f08c3bdfSopenharmony_ci &first_gid, &neg_one, &first_gid, &root_gid, 34f08c3bdfSopenharmony_ci "After setregid(first, -1)"}, { 35f08c3bdfSopenharmony_ci &root_gid, &neg_one, &root_gid, &root_gid, 36f08c3bdfSopenharmony_ci "After setregid(root,-1),"}, { 37f08c3bdfSopenharmony_ci &neg_one, &neg_one, &root_gid, &root_gid, 38f08c3bdfSopenharmony_ci "After setregid(-1, -1),"}, { 39f08c3bdfSopenharmony_ci &neg_one, &root_gid, &root_gid, &root_gid, 40f08c3bdfSopenharmony_ci "After setregid(-1, root)"}, { 41f08c3bdfSopenharmony_ci &root_gid, &neg_one, &root_gid, &root_gid, 42f08c3bdfSopenharmony_ci "After setregid(root, -1),"}, { 43f08c3bdfSopenharmony_ci &second_gid, &first_gid, &second_gid, &first_gid, 44f08c3bdfSopenharmony_ci "After setregid(second, first)"}, { 45f08c3bdfSopenharmony_ci &neg_one, &neg_one, &second_gid, &first_gid, 46f08c3bdfSopenharmony_ci "After setregid(-1, -1)"}, { 47f08c3bdfSopenharmony_ci &neg_one, &first_gid, &second_gid, &first_gid, 48f08c3bdfSopenharmony_ci "After setregid(-1, first)"} 49f08c3bdfSopenharmony_ci}; 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_cistatic void gid_verify(gid_t rg, gid_t eg, const char *when) 52f08c3bdfSopenharmony_ci{ 53f08c3bdfSopenharmony_ci if ((getgid() != rg) || (getegid() != eg)) { 54f08c3bdfSopenharmony_ci tst_res(TFAIL, "ERROR: %s real gid = %d; effective gid = %d", 55f08c3bdfSopenharmony_ci when, getgid(), getegid()); 56f08c3bdfSopenharmony_ci tst_res(TINFO, "Expected: real gid = %d; effective gid = %d", 57f08c3bdfSopenharmony_ci rg, eg); 58f08c3bdfSopenharmony_ci } else { 59f08c3bdfSopenharmony_ci tst_res(TPASS, 60f08c3bdfSopenharmony_ci "real or effective gid was modified as expected"); 61f08c3bdfSopenharmony_ci } 62f08c3bdfSopenharmony_ci} 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_cistatic void run(unsigned int i) 65f08c3bdfSopenharmony_ci{ 66f08c3bdfSopenharmony_ci /* Set the real or effective group id */ 67f08c3bdfSopenharmony_ci TEST(SETREGID(*test_data[i].real_gid, *test_data[i].eff_gid)); 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_ci if (TST_RET == -1) { 70f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "setregid(%d, %d) failed", 71f08c3bdfSopenharmony_ci *test_data[i].real_gid, *test_data[i].eff_gid); 72f08c3bdfSopenharmony_ci return; 73f08c3bdfSopenharmony_ci } 74f08c3bdfSopenharmony_ci 75f08c3bdfSopenharmony_ci gid_verify(*test_data[i].exp_real_usr, *test_data[i].exp_eff_usr, 76f08c3bdfSopenharmony_ci test_data[i].test_msg); 77f08c3bdfSopenharmony_ci} 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_cistatic void setup(void) 80f08c3bdfSopenharmony_ci{ 81f08c3bdfSopenharmony_ci gid_t test_groups[3]; 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ci root_gid = test_groups[0] = getgid(); 84f08c3bdfSopenharmony_ci tst_get_gids(test_groups, 1, 3); 85f08c3bdfSopenharmony_ci first_gid = test_groups[1]; 86f08c3bdfSopenharmony_ci second_gid = test_groups[2]; 87f08c3bdfSopenharmony_ci} 88f08c3bdfSopenharmony_ci 89f08c3bdfSopenharmony_cistatic struct tst_test test = { 90f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(test_data), 91f08c3bdfSopenharmony_ci .needs_root = 1, 92f08c3bdfSopenharmony_ci .test = run, 93f08c3bdfSopenharmony_ci .setup = setup, 94f08c3bdfSopenharmony_ci}; 95