aboutsummaryrefslogtreecommitdiff
path: root/lib/tst_uid.c
blob: af4ef8cf72d507c0ca91eac40b06ac0a1aa6f583 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2021 Linux Test Project
 */

#include <sys/types.h>
#include <grp.h>
#include <errno.h>

#define TST_NO_DEFAULT_MAIN
#include "tst_test.h"
#include "tst_uid.h"

#define MAX_GID 32767

gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip)
{
	gid_t ret;

	errno = 0;

	for (ret = 1; ret < MAX_GID; ret++) {
		if (ret == skip || getgrgid(ret))
			continue;

		if (errno == 0 || errno == ENOENT || errno == ESRCH) {
			tst_res_(file, lineno, TINFO | TERRNO,
				"Found unused GID %d", (int)ret);
			return ret;
		}

		tst_brk_(file, lineno, TBROK|TERRNO, "Group ID lookup failed");
		return (gid_t)-1;
	}

	tst_brk_(file, lineno, TBROK, "No free group ID found");
	return (gid_t)-1;
}

void tst_get_uids(uid_t *buf, unsigned int start, unsigned int count)
{
	unsigned int i, j;
	uid_t id;

	for (i = start, id = 1; i < count; id++) {
		for (j = 0; j < start; j++) {
			if (buf[j] == id)
				break;
		}

		if (j >= start)
			buf[i++] = id;
	}
}

void tst_get_gids(gid_t *buf, unsigned int start, unsigned int count)
{
	unsigned int i, j;
	gid_t id;

	for (i = start, id = 1; i < count; id++) {
		for (j = 0; j < start; j++) {
			if (buf[j] == id)
				break;
		}

		if (j >= start)
			buf[i++] = id;
	}
}

int tst_check_resuid_(const char *file, const int lineno, const char *callstr,
	uid_t exp_ruid, uid_t exp_euid, uid_t exp_suid)
{
	uid_t ruid, euid, suid;

	SAFE_GETRESUID(&ruid, &euid, &suid);

	if (ruid == exp_ruid && euid == exp_euid && suid == exp_suid)
		return 1;

	if (callstr) {
		tst_res_(file, lineno, TFAIL, "Unexpected process UID after %s",
			callstr);
	} else {
		tst_res_(file, lineno, TFAIL, "Unexpected process UID");
	}

	tst_res_(file, lineno, TINFO, "Got: ruid = %d, euid = %d, suid = %d",
		(int)ruid, (int)euid, (int)suid);
	tst_res_(file, lineno, TINFO,
		"Expected: ruid = %d, euid = %d, suid = %d",
		(int)exp_ruid, (int)exp_euid, (int)exp_suid);
	return 0;
}

int tst_check_resgid_(const char *file, const int lineno, const char *callstr,
	gid_t exp_rgid, gid_t exp_egid, gid_t exp_sgid)
{
	gid_t rgid, egid, sgid;

	SAFE_GETRESGID(&rgid, &egid, &sgid);

	if (rgid == exp_rgid && egid == exp_egid && sgid == exp_sgid)
		return 1;

	if (callstr) {
		tst_res_(file, lineno, TFAIL, "Unexpected process GID after %s",
			callstr);
	} else {
		tst_res_(file, lineno, TFAIL, "Unexpected process GID");
	}

	tst_res_(file, lineno, TINFO, "Got: rgid = %d, egid = %d, sgid = %d",
		(int)rgid, (int)egid, (int)sgid);
	tst_res_(file, lineno, TINFO,
		"Expected: rgid = %d, egid = %d, sgid = %d",
		(int)exp_rgid, (int)exp_egid, (int)exp_sgid);
	return 0;
}