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