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
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <cerrno>
#include <csignal>
#include "gtest/gtest.h"
#include "test/util/cleanup.h"
#include "test/util/logging.h"
#include "test/util/posix_error.h"
#include "test/util/signal_util.h"
#include "test/util/test_util.h"
namespace gvisor {
namespace testing {
namespace {
// saved_info is set by the handler.
siginfo_t saved_info;
// has_saved_info is set to true by the handler.
volatile bool has_saved_info;
void SigHandler(int sig, siginfo_t* info, void* context) {
// Copy to the given info.
saved_info = *info;
has_saved_info = true;
}
void ClearSavedInfo() {
// Clear the cached info.
memset(&saved_info, 0, sizeof(saved_info));
has_saved_info = false;
}
PosixErrorOr<Cleanup> SetupSignalHandler(int sig) {
struct sigaction sa;
sa.sa_sigaction = SigHandler;
sigfillset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
return ScopedSigaction(sig, sa);
}
class RtSignalTest : public ::testing::Test {
protected:
void SetUp() override {
action_cleanup_ = ASSERT_NO_ERRNO_AND_VALUE(SetupSignalHandler(SIGUSR1));
mask_cleanup_ =
ASSERT_NO_ERRNO_AND_VALUE(ScopedSignalMask(SIG_UNBLOCK, SIGUSR1));
}
void TearDown() override { ClearSavedInfo(); }
private:
Cleanup action_cleanup_;
Cleanup mask_cleanup_;
};
static int rt_sigqueueinfo(pid_t tgid, int sig, siginfo_t* uinfo) {
int ret;
do {
// NOTE(b/25434735): rt_sigqueueinfo(2) could return EAGAIN for RT signals.
ret = syscall(SYS_rt_sigqueueinfo, tgid, sig, uinfo);
} while (ret == -1 && errno == EAGAIN);
return ret;
}
TEST_F(RtSignalTest, InvalidTID) {
siginfo_t uinfo;
// Depending on the kernel version, these calls may fail with
// ESRCH (goobunutu machines) or EPERM (production machines). Thus,
// the test simply ensures that they do fail.
EXPECT_THAT(rt_sigqueueinfo(-1, SIGUSR1, &uinfo), SyscallFails());
EXPECT_FALSE(has_saved_info);
EXPECT_THAT(rt_sigqueueinfo(0, SIGUSR1, &uinfo), SyscallFails());
EXPECT_FALSE(has_saved_info);
}
TEST_F(RtSignalTest, InvalidCodes) {
siginfo_t uinfo;
// We need a child for the code checks to apply. If the process is delivering
// to itself, then it can use whatever codes it wants and they will go
// through.
pid_t child = fork();
if (child == 0) {
_exit(1);
}
ASSERT_THAT(child, SyscallSucceeds());
// These are not allowed for child processes.
uinfo.si_code = 0; // SI_USER.
EXPECT_THAT(rt_sigqueueinfo(child, SIGUSR1, &uinfo),
SyscallFailsWithErrno(EPERM));
uinfo.si_code = 0x80; // SI_KERNEL.
EXPECT_THAT(rt_sigqueueinfo(child, SIGUSR1, &uinfo),
SyscallFailsWithErrno(EPERM));
uinfo.si_code = -6; // SI_TKILL.
EXPECT_THAT(rt_sigqueueinfo(child, SIGUSR1, &uinfo),
SyscallFailsWithErrno(EPERM));
uinfo.si_code = -1; // SI_QUEUE (allowed).
EXPECT_THAT(rt_sigqueueinfo(child, SIGUSR1, &uinfo), SyscallSucceeds());
// Join the child process.
EXPECT_THAT(waitpid(child, nullptr, 0), SyscallSucceeds());
}
TEST_F(RtSignalTest, ValueDelivered) {
siginfo_t uinfo;
uinfo.si_code = -1; // SI_QUEUE (allowed).
uinfo.si_errno = 0x1234;
EXPECT_EQ(saved_info.si_errno, 0x0);
EXPECT_THAT(rt_sigqueueinfo(getpid(), SIGUSR1, &uinfo), SyscallSucceeds());
EXPECT_TRUE(has_saved_info);
EXPECT_EQ(saved_info.si_errno, 0x1234);
}
TEST_F(RtSignalTest, SignoMatch) {
auto action2_cleanup = ASSERT_NO_ERRNO_AND_VALUE(SetupSignalHandler(SIGUSR2));
auto mask2_cleanup =
ASSERT_NO_ERRNO_AND_VALUE(ScopedSignalMask(SIG_UNBLOCK, SIGUSR2));
siginfo_t uinfo;
uinfo.si_code = -1; // SI_QUEUE (allowed).
EXPECT_THAT(rt_sigqueueinfo(getpid(), SIGUSR1, &uinfo), SyscallSucceeds());
EXPECT_TRUE(has_saved_info);
EXPECT_EQ(saved_info.si_signo, SIGUSR1);
ClearSavedInfo();
EXPECT_THAT(rt_sigqueueinfo(getpid(), SIGUSR2, &uinfo), SyscallSucceeds());
EXPECT_TRUE(has_saved_info);
EXPECT_EQ(saved_info.si_signo, SIGUSR2);
}
} // namespace
} // namespace testing
} // namespace gvisor
int main(int argc, char** argv) {
// These tests depend on delivering SIGUSR1/2 to the main thread (so they can
// synchronously check has_saved_info). Block these so that any other threads
// created by TestInit will also have them blocked.
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
sigaddset(&set, SIGUSR2);
TEST_PCHECK(sigprocmask(SIG_BLOCK, &set, nullptr) == 0);
gvisor::testing::TestInit(&argc, &argv);
return gvisor::testing::RunAllTests();
}
|