1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2021 Linux Test Project
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
7*49cdfc7eSAndroid Build Coastguard Worker #include <grp.h>
8*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
9*49cdfc7eSAndroid Build Coastguard Worker
10*49cdfc7eSAndroid Build Coastguard Worker #define TST_NO_DEFAULT_MAIN
11*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
12*49cdfc7eSAndroid Build Coastguard Worker #include "tst_uid.h"
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Worker #define MAX_GID 32767
15*49cdfc7eSAndroid Build Coastguard Worker
tst_get_free_gid_(const char * file,const int lineno,gid_t skip)16*49cdfc7eSAndroid Build Coastguard Worker gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip)
17*49cdfc7eSAndroid Build Coastguard Worker {
18*49cdfc7eSAndroid Build Coastguard Worker gid_t ret;
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker for (ret = 1; ret < MAX_GID; ret++) {
23*49cdfc7eSAndroid Build Coastguard Worker if (ret == skip || getgrgid(ret))
24*49cdfc7eSAndroid Build Coastguard Worker continue;
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker if (errno == 0 || errno == ENOENT || errno == ESRCH) {
27*49cdfc7eSAndroid Build Coastguard Worker tst_res_(file, lineno, TINFO | TERRNO,
28*49cdfc7eSAndroid Build Coastguard Worker "Found unused GID %d", (int)ret);
29*49cdfc7eSAndroid Build Coastguard Worker return ret;
30*49cdfc7eSAndroid Build Coastguard Worker }
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK|TERRNO, "Group ID lookup failed");
33*49cdfc7eSAndroid Build Coastguard Worker return (gid_t)-1;
34*49cdfc7eSAndroid Build Coastguard Worker }
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK, "No free group ID found");
37*49cdfc7eSAndroid Build Coastguard Worker return (gid_t)-1;
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker
tst_get_uids(uid_t * buf,unsigned int start,unsigned int count)40*49cdfc7eSAndroid Build Coastguard Worker void tst_get_uids(uid_t *buf, unsigned int start, unsigned int count)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker unsigned int i, j;
43*49cdfc7eSAndroid Build Coastguard Worker uid_t id;
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker for (i = start, id = 1; i < count; id++) {
46*49cdfc7eSAndroid Build Coastguard Worker for (j = 0; j < start; j++) {
47*49cdfc7eSAndroid Build Coastguard Worker if (buf[j] == id)
48*49cdfc7eSAndroid Build Coastguard Worker break;
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker if (j >= start)
52*49cdfc7eSAndroid Build Coastguard Worker buf[i++] = id;
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker
tst_get_gids(gid_t * buf,unsigned int start,unsigned int count)56*49cdfc7eSAndroid Build Coastguard Worker void tst_get_gids(gid_t *buf, unsigned int start, unsigned int count)
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker unsigned int i, j;
59*49cdfc7eSAndroid Build Coastguard Worker gid_t id;
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker for (i = start, id = 1; i < count; id++) {
62*49cdfc7eSAndroid Build Coastguard Worker for (j = 0; j < start; j++) {
63*49cdfc7eSAndroid Build Coastguard Worker if (buf[j] == id)
64*49cdfc7eSAndroid Build Coastguard Worker break;
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker if (j >= start)
68*49cdfc7eSAndroid Build Coastguard Worker buf[i++] = id;
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker }
71*49cdfc7eSAndroid Build Coastguard Worker
tst_check_resuid_(const char * file,const int lineno,const char * callstr,uid_t exp_ruid,uid_t exp_euid,uid_t exp_suid)72*49cdfc7eSAndroid Build Coastguard Worker int tst_check_resuid_(const char *file, const int lineno, const char *callstr,
73*49cdfc7eSAndroid Build Coastguard Worker uid_t exp_ruid, uid_t exp_euid, uid_t exp_suid)
74*49cdfc7eSAndroid Build Coastguard Worker {
75*49cdfc7eSAndroid Build Coastguard Worker uid_t ruid, euid, suid;
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker SAFE_GETRESUID(&ruid, &euid, &suid);
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker if (ruid == exp_ruid && euid == exp_euid && suid == exp_suid)
80*49cdfc7eSAndroid Build Coastguard Worker return 1;
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker if (callstr) {
83*49cdfc7eSAndroid Build Coastguard Worker tst_res_(file, lineno, TFAIL, "Unexpected process UID after %s",
84*49cdfc7eSAndroid Build Coastguard Worker callstr);
85*49cdfc7eSAndroid Build Coastguard Worker } else {
86*49cdfc7eSAndroid Build Coastguard Worker tst_res_(file, lineno, TFAIL, "Unexpected process UID");
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker tst_res_(file, lineno, TINFO, "Got: ruid = %d, euid = %d, suid = %d",
90*49cdfc7eSAndroid Build Coastguard Worker (int)ruid, (int)euid, (int)suid);
91*49cdfc7eSAndroid Build Coastguard Worker tst_res_(file, lineno, TINFO,
92*49cdfc7eSAndroid Build Coastguard Worker "Expected: ruid = %d, euid = %d, suid = %d",
93*49cdfc7eSAndroid Build Coastguard Worker (int)exp_ruid, (int)exp_euid, (int)exp_suid);
94*49cdfc7eSAndroid Build Coastguard Worker return 0;
95*49cdfc7eSAndroid Build Coastguard Worker }
96*49cdfc7eSAndroid Build Coastguard Worker
tst_check_resgid_(const char * file,const int lineno,const char * callstr,gid_t exp_rgid,gid_t exp_egid,gid_t exp_sgid)97*49cdfc7eSAndroid Build Coastguard Worker int tst_check_resgid_(const char *file, const int lineno, const char *callstr,
98*49cdfc7eSAndroid Build Coastguard Worker gid_t exp_rgid, gid_t exp_egid, gid_t exp_sgid)
99*49cdfc7eSAndroid Build Coastguard Worker {
100*49cdfc7eSAndroid Build Coastguard Worker gid_t rgid, egid, sgid;
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker SAFE_GETRESGID(&rgid, &egid, &sgid);
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker if (rgid == exp_rgid && egid == exp_egid && sgid == exp_sgid)
105*49cdfc7eSAndroid Build Coastguard Worker return 1;
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker if (callstr) {
108*49cdfc7eSAndroid Build Coastguard Worker tst_res_(file, lineno, TFAIL, "Unexpected process GID after %s",
109*49cdfc7eSAndroid Build Coastguard Worker callstr);
110*49cdfc7eSAndroid Build Coastguard Worker } else {
111*49cdfc7eSAndroid Build Coastguard Worker tst_res_(file, lineno, TFAIL, "Unexpected process GID");
112*49cdfc7eSAndroid Build Coastguard Worker }
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker tst_res_(file, lineno, TINFO, "Got: rgid = %d, egid = %d, sgid = %d",
115*49cdfc7eSAndroid Build Coastguard Worker (int)rgid, (int)egid, (int)sgid);
116*49cdfc7eSAndroid Build Coastguard Worker tst_res_(file, lineno, TINFO,
117*49cdfc7eSAndroid Build Coastguard Worker "Expected: rgid = %d, egid = %d, sgid = %d",
118*49cdfc7eSAndroid Build Coastguard Worker (int)exp_rgid, (int)exp_egid, (int)exp_sgid);
119*49cdfc7eSAndroid Build Coastguard Worker return 0;
120*49cdfc7eSAndroid Build Coastguard Worker }
121