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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
// SPDX-License-Identifier: GPL-2.0+
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include "../kselftest_harness.h"
/* Remove a file, ignoring the result if it didn't exist. */
void rm(struct __test_metadata *_metadata, const char *pathname,
int is_dir)
{
int rc;
if (is_dir)
rc = rmdir(pathname);
else
rc = unlink(pathname);
if (rc < 0) {
ASSERT_EQ(errno, ENOENT) {
TH_LOG("Not ENOENT: %s", pathname);
}
} else {
ASSERT_EQ(rc, 0) {
TH_LOG("Failed to remove: %s", pathname);
}
}
}
FIXTURE(file) {
char *pathname;
int is_dir;
};
FIXTURE_VARIANT(file)
{
const char *name;
int expected;
int is_dir;
void (*setup)(struct __test_metadata *_metadata,
FIXTURE_DATA(file) *self,
const FIXTURE_VARIANT(file) *variant);
int major, minor, mode; /* for mknod() */
};
void setup_link(struct __test_metadata *_metadata,
FIXTURE_DATA(file) *self,
const FIXTURE_VARIANT(file) *variant)
{
const char * const paths[] = {
"/bin/true",
"/usr/bin/true",
};
int i;
for (i = 0; i < ARRAY_SIZE(paths); i++) {
if (access(paths[i], X_OK) == 0) {
ASSERT_EQ(symlink(paths[i], self->pathname), 0);
return;
}
}
ASSERT_EQ(1, 0) {
TH_LOG("Could not find viable 'true' binary");
}
}
FIXTURE_VARIANT_ADD(file, S_IFLNK)
{
.name = "S_IFLNK",
.expected = ELOOP,
.setup = setup_link,
};
void setup_dir(struct __test_metadata *_metadata,
FIXTURE_DATA(file) *self,
const FIXTURE_VARIANT(file) *variant)
{
ASSERT_EQ(mkdir(self->pathname, 0755), 0);
}
FIXTURE_VARIANT_ADD(file, S_IFDIR)
{
.name = "S_IFDIR",
.is_dir = 1,
.expected = EACCES,
.setup = setup_dir,
};
void setup_node(struct __test_metadata *_metadata,
FIXTURE_DATA(file) *self,
const FIXTURE_VARIANT(file) *variant)
{
dev_t dev;
int rc;
dev = makedev(variant->major, variant->minor);
rc = mknod(self->pathname, 0755 | variant->mode, dev);
ASSERT_EQ(rc, 0) {
if (errno == EPERM)
SKIP(return, "Please run as root; cannot mknod(%s)",
variant->name);
}
}
FIXTURE_VARIANT_ADD(file, S_IFBLK)
{
.name = "S_IFBLK",
.expected = EACCES,
.setup = setup_node,
/* /dev/loop0 */
.major = 7,
.minor = 0,
.mode = S_IFBLK,
};
FIXTURE_VARIANT_ADD(file, S_IFCHR)
{
.name = "S_IFCHR",
.expected = EACCES,
.setup = setup_node,
/* /dev/zero */
.major = 1,
.minor = 5,
.mode = S_IFCHR,
};
void setup_fifo(struct __test_metadata *_metadata,
FIXTURE_DATA(file) *self,
const FIXTURE_VARIANT(file) *variant)
{
ASSERT_EQ(mkfifo(self->pathname, 0755), 0);
}
FIXTURE_VARIANT_ADD(file, S_IFIFO)
{
.name = "S_IFIFO",
.expected = EACCES,
.setup = setup_fifo,
};
FIXTURE_SETUP(file)
{
ASSERT_GT(asprintf(&self->pathname, "%s.test", variant->name), 6);
self->is_dir = variant->is_dir;
rm(_metadata, self->pathname, variant->is_dir);
variant->setup(_metadata, self, variant);
}
FIXTURE_TEARDOWN(file)
{
rm(_metadata, self->pathname, self->is_dir);
}
TEST_F(file, exec_errno)
{
char * const argv[2] = { (char * const)self->pathname, NULL };
EXPECT_LT(execv(argv[0], argv), 0);
EXPECT_EQ(errno, variant->expected);
}
/* S_IFSOCK */
FIXTURE(sock)
{
int fd;
};
FIXTURE_SETUP(sock)
{
self->fd = socket(AF_INET, SOCK_STREAM, 0);
ASSERT_GE(self->fd, 0);
}
FIXTURE_TEARDOWN(sock)
{
if (self->fd >= 0)
ASSERT_EQ(close(self->fd), 0);
}
TEST_F(sock, exec_errno)
{
char * const argv[2] = { " magic socket ", NULL };
char * const envp[1] = { NULL };
EXPECT_LT(fexecve(self->fd, argv, envp), 0);
EXPECT_EQ(errno, EACCES);
}
TEST_HARNESS_MAIN
|