ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * xfrm_ctrl_test.cpp - unit tests for xfrm controllers. |
| 17 | */ |
| 18 | |
| 19 | #include <cerrno> |
| 20 | #include <cinttypes> |
| 21 | #include <cstdint> |
| 22 | #include <cstdio> |
| 23 | #include <cstdlib> |
| 24 | #include <set> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include <arpa/inet.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <gmock/gmock.h> |
| 30 | #include <ifaddrs.h> |
| 31 | #include <linux/if.h> |
| 32 | #include <linux/if_tun.h> |
| 33 | #include <linux/netfilter/nfnetlink.h> |
| 34 | #include <linux/netlink.h> |
| 35 | #include <netdb.h> |
| 36 | #include <netinet/in.h> |
| 37 | #include <sys/socket.h> |
| 38 | #include <sys/types.h> |
| 39 | |
| 40 | #include <android-base/macros.h> |
| 41 | #include <android-base/stringprintf.h> |
| 42 | #include <android-base/strings.h> |
| 43 | #include <android-base/unique_fd.h> |
| 44 | #include <gtest/gtest.h> |
| 45 | |
| 46 | #include "NetdConstants.h" |
| 47 | #include "NetlinkCommands.h" |
| 48 | #include "Stopwatch.h" |
| 49 | #include "XfrmController.h" |
| 50 | #include "android/net/INetd.h" |
| 51 | #include "android/net/UidRange.h" |
| 52 | #include "binder/IServiceManager.h" |
| 53 | #include "netdutils/MockSyscalls.h" |
| 54 | #include "netdutils/Netlink.h" |
| 55 | #include "tun_interface.h" |
| 56 | |
| 57 | using android::base::unique_fd; |
| 58 | using android::netdutils::Fd; |
| 59 | using android::netdutils::MockSyscalls; |
| 60 | using android::netdutils::Slice; |
| 61 | using android::netdutils::Status; |
| 62 | using android::netdutils::StatusOr; |
| 63 | using android::netdutils::Syscalls; |
| 64 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 65 | using ::testing::_; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 66 | using ::testing::DoAll; |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 67 | using ::testing::Invoke; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 68 | using ::testing::Return; |
| 69 | using ::testing::SaveArg; |
| 70 | using ::testing::SetArgPointee; |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 71 | using ::testing::Values; |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 72 | using ::testing::WithArg; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
Jonathan Basseri | c612866 | 2017-09-14 17:32:59 -0700 | [diff] [blame] | 75 | * This gMock action works like SetArgPointee, but for netdutils::Slice. |
| 76 | * It sets the memory which is pointed to by the N-th argument with the supplied value. |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 77 | */ |
Jonathan Basseri | c612866 | 2017-09-14 17:32:59 -0700 | [diff] [blame] | 78 | ACTION_TEMPLATE(SetArgSlice, HAS_1_TEMPLATE_PARAMS(int, N), AND_1_VALUE_PARAMS(value)) { |
| 79 | Slice orig = ::testing::get<N>(args); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 80 | android::netdutils::copy(orig, value); |
| 81 | } |
| 82 | |
Jonathan Basseri | c612866 | 2017-09-14 17:32:59 -0700 | [diff] [blame] | 83 | /** |
| 84 | * This gMock action works like SaveArg, but is specialized for vector<iovec>. |
| 85 | * It copies the memory pointed to by each of the iovecs into a single vector<uint8_t>. |
| 86 | * |
| 87 | * Flattening the iovec objects cannot be done later, since there is no guarantee that the memory |
| 88 | * they point to will still be valid after the mock method returns. |
| 89 | */ |
| 90 | ACTION_TEMPLATE(SaveFlattenedIovecs, HAS_1_TEMPLATE_PARAMS(int, N), AND_1_VALUE_PARAMS(resVec)) { |
| 91 | const std::vector<iovec>& iovs = ::testing::get<N>(args); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 92 | |
| 93 | for (const iovec& iov : iovs) { |
| 94 | resVec->insert(resVec->end(), reinterpret_cast<uint8_t*>(iov.iov_base), |
| 95 | reinterpret_cast<uint8_t*>(iov.iov_base) + iov.iov_len); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | namespace android { |
| 100 | namespace net { |
| 101 | |
| 102 | static constexpr int DROID_SPI = 0xD1201D; |
| 103 | static constexpr size_t KEY_LENGTH = 32; |
| 104 | static constexpr int NLMSG_DEFAULTSIZE = 8192; |
| 105 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 106 | static constexpr char LOCALHOST_V4[] = "127.0.0.1"; |
| 107 | static constexpr char LOCALHOST_V6[] = "::1"; |
| 108 | static constexpr char TEST_ADDR_V4[] = "8.8.8.8"; |
| 109 | static constexpr char TEST_ADDR_V6[] = "2001:4860:4860::8888"; |
| 110 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 111 | struct Policy { |
| 112 | xfrm_userpolicy_info info; |
| 113 | xfrm_user_tmpl tmpl; |
| 114 | }; |
| 115 | |
| 116 | struct NetlinkResponse { |
| 117 | nlmsghdr hdr; |
| 118 | char buf[NLMSG_DEFAULTSIZE]; |
| 119 | }; |
| 120 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 121 | void expectAddressEquals(int family, const std::string& expected, const xfrm_address_t& actual) { |
| 122 | char actualStr[INET6_ADDRSTRLEN]; |
| 123 | const char* ret = |
| 124 | inet_ntop(family, reinterpret_cast<const void*>(&actual), actualStr, INET6_ADDRSTRLEN); |
| 125 | EXPECT_NE(nullptr, ret) << "Unable to convert xfrm_address_t to string"; |
| 126 | EXPECT_EQ(expected, actualStr); |
| 127 | } |
| 128 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 129 | class XfrmControllerTest : public ::testing::Test { |
| 130 | public: |
| 131 | MockSyscalls mockSyscalls; |
| 132 | |
| 133 | void SetUp() override { netdutils::sSyscalls.swap(mockSyscalls); } |
| 134 | }; |
| 135 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 136 | // Test class allowing IPv4/IPv6 parameterized tests. |
| 137 | class XfrmControllerParameterizedTest : public XfrmControllerTest, |
| 138 | public ::testing::WithParamInterface<int> {}; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 139 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 140 | // Helper to make generated test names readable. |
| 141 | std::string FamilyName(::testing::TestParamInfo<int> info) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 142 | switch(info.param) { |
| 143 | case 4: return "IPv4"; |
| 144 | case 5: return "IPv64DualStack"; |
| 145 | case 6: return "IPv6"; |
| 146 | } |
| 147 | return android::base::StringPrintf("UNKNOWN family type: %d", info.param); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 150 | /* Generate function to set value refered to by 3rd argument. |
| 151 | * |
| 152 | * This allows us to mock functions that pass in a pointer, expecting the result to be put into |
| 153 | * that location. |
| 154 | */ |
| 155 | ACTION_P(SetArg3IntValue, value) { *static_cast<int*>(arg3) = value; } |
| 156 | |
| 157 | TEST_F(XfrmControllerTest, TestFchown) { |
| 158 | XfrmController ctrl; |
| 159 | unique_fd sockFd(socket(AF_INET, SOCK_DGRAM, 0)); |
| 160 | |
| 161 | EXPECT_CALL(mockSyscalls, getsockopt(Fd(sockFd), IPPROTO_UDP, UDP_ENCAP, _, _)) |
| 162 | .WillOnce(DoAll(SetArg3IntValue(UDP_ENCAP_ESPINUDP), Return(netdutils::status::ok))); |
| 163 | |
| 164 | netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(sockFd, 1001, getuid()); |
| 165 | EXPECT_EQ(netdutils::status::ok, res); |
| 166 | |
| 167 | struct stat info; |
| 168 | EXPECT_EQ(0, fstat(sockFd.get(), &info)); |
| 169 | EXPECT_EQ(1001, (int)info.st_uid); |
| 170 | } |
| 171 | |
| 172 | TEST_F(XfrmControllerTest, TestFchownInvalidFd) { |
| 173 | XfrmController ctrl; |
| 174 | |
| 175 | netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(unique_fd(), 1001, getuid()); |
| 176 | EXPECT_EQ(netdutils::statusFromErrno(errno, "Failed to stat socket file descriptor"), res); |
| 177 | } |
| 178 | |
| 179 | TEST_F(XfrmControllerTest, TestFchownIncorrectCallerUid) { |
| 180 | XfrmController ctrl; |
| 181 | unique_fd sockFd(socket(AF_INET, SOCK_DGRAM, 0)); |
| 182 | |
| 183 | netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(sockFd, 1001, 1001); |
| 184 | EXPECT_EQ(netdutils::statusFromErrno(EPERM, "fchown disabled for non-owner calls"), res); |
| 185 | } |
| 186 | |
| 187 | TEST_F(XfrmControllerTest, TestFchownNonSocketFd) { |
| 188 | XfrmController ctrl; |
| 189 | unique_fd fd(open("/dev/null", 0)); |
| 190 | |
| 191 | netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(fd, 1001, getuid()); |
| 192 | EXPECT_EQ(netdutils::statusFromErrno(EINVAL, "File descriptor was not a socket"), res); |
| 193 | } |
| 194 | |
| 195 | TEST_F(XfrmControllerTest, TestFchownNonUdp) { |
| 196 | XfrmController ctrl; |
| 197 | unique_fd sockFd(socket(AF_INET, SOCK_STREAM, 0)); |
| 198 | |
| 199 | EXPECT_CALL(mockSyscalls, getsockopt(Fd(sockFd), IPPROTO_UDP, UDP_ENCAP, _, _)) |
| 200 | .WillOnce(DoAll(SetArg3IntValue(0), Return(netdutils::status::ok))); |
| 201 | |
| 202 | netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(sockFd, 1001, getuid()); |
| 203 | EXPECT_EQ(netdutils::statusFromErrno(EINVAL, "Socket was not a UDP socket"), res); |
| 204 | } |
| 205 | |
| 206 | TEST_F(XfrmControllerTest, TestFchownNonUdpEncap) { |
| 207 | XfrmController ctrl; |
| 208 | unique_fd sockFd(socket(AF_INET, SOCK_DGRAM, 0)); |
| 209 | |
| 210 | EXPECT_CALL(mockSyscalls, getsockopt(Fd(sockFd), IPPROTO_UDP, UDP_ENCAP, _, _)) |
| 211 | .WillOnce(DoAll(SetArg3IntValue(0), Return(netdutils::status::ok))); |
| 212 | |
| 213 | netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(sockFd, 1001, getuid()); |
| 214 | EXPECT_EQ(netdutils::statusFromErrno(EINVAL, "Socket did not have UDP-encap sockopt set"), res); |
| 215 | } |
| 216 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 217 | // The TEST_P cases below will run with each of the following value parameters. |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 218 | INSTANTIATE_TEST_CASE_P(ByFamily, XfrmControllerParameterizedTest, Values(4, 5, 6), |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 219 | FamilyName); |
| 220 | |
| 221 | TEST_P(XfrmControllerParameterizedTest, TestIpSecAllocateSpi) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 222 | const int version = GetParam(); |
| 223 | const int family = (version == 6) ? AF_INET6 : AF_INET; |
| 224 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 225 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 226 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 227 | NetlinkResponse response{}; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 228 | response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 229 | Slice responseSlice = netdutils::makeSlice(response); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 230 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 231 | size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_userspi_info)); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 232 | |
| 233 | // A vector to hold the flattened netlink message for nlMsgSlice |
| 234 | std::vector<uint8_t> nlMsgBuf; |
| 235 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 236 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 237 | EXPECT_CALL(mockSyscalls, read(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 238 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 239 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 240 | XfrmController ctrl; |
| 241 | int outSpi = 0; |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 242 | Status res = ctrl.ipSecAllocateSpi(1 /* resourceId */, static_cast<int>(XfrmDirection::OUT), |
| 243 | localAddr, remoteAddr, DROID_SPI, &outSpi); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 244 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 245 | EXPECT_TRUE(isOk(res)) << res; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 246 | EXPECT_EQ(DROID_SPI, outSpi); |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 247 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 248 | |
| 249 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 250 | nlMsgSlice = drop(nlMsgSlice, NLMSG_HDRLEN); |
| 251 | |
| 252 | xfrm_userspi_info userspi{}; |
| 253 | netdutils::extract(nlMsgSlice, userspi); |
| 254 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 255 | EXPECT_EQ(family, userspi.info.sel.family); |
| 256 | expectAddressEquals(family, localAddr, userspi.info.saddr); |
| 257 | expectAddressEquals(family, remoteAddr, userspi.info.id.daddr); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 258 | |
| 259 | EXPECT_EQ(DROID_SPI, static_cast<int>(userspi.min)); |
| 260 | EXPECT_EQ(DROID_SPI, static_cast<int>(userspi.max)); |
| 261 | } |
| 262 | |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 263 | void testIpSecAddSecurityAssociation(int version, const MockSyscalls& mockSyscalls, |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 264 | const XfrmMode& mode) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 265 | const int family = (version == 6) ? AF_INET6 : AF_INET; |
| 266 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 267 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 268 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 269 | NetlinkResponse response{}; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 270 | response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 271 | Slice responseSlice = netdutils::makeSlice(response); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 272 | |
| 273 | std::vector<uint8_t> authKey(KEY_LENGTH, 0); |
| 274 | std::vector<uint8_t> cryptKey(KEY_LENGTH, 1); |
| 275 | |
| 276 | // Calculate the length of the expected netlink message. |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 277 | size_t expectedMsgLength = |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 278 | NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_usersa_info)) + |
| 279 | NLA_ALIGN(offsetof(XfrmController::nlattr_algo_crypt, key) + KEY_LENGTH) + |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 280 | NLA_ALIGN(offsetof(XfrmController::nlattr_algo_auth, key) + KEY_LENGTH); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 281 | |
| 282 | std::vector<uint8_t> nlMsgBuf; |
| 283 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 284 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 285 | EXPECT_CALL(mockSyscalls, read(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 286 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 287 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 288 | XfrmController ctrl; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 289 | Status res = ctrl.ipSecAddSecurityAssociation( |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 290 | 1 /* resourceId */, static_cast<int>(mode), |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 291 | static_cast<int>(XfrmDirection::OUT), localAddr, remoteAddr, 0 /* underlying network */, |
| 292 | DROID_SPI, "hmac(sha256)" /* auth algo */, authKey, 128 /* auth trunc length */, |
| 293 | "cbc(aes)" /* encryption algo */, cryptKey, 0 /* crypt trunc length? */, "" /* AEAD algo */, |
| 294 | {}, 0, static_cast<int>(XfrmEncapType::NONE), 0 /* local port */, 0 /* remote port */); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 295 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 296 | EXPECT_TRUE(isOk(res)) << res; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 297 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 298 | |
| 299 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 300 | nlMsgSlice = drop(nlMsgSlice, NLMSG_HDRLEN); |
| 301 | |
| 302 | xfrm_usersa_info usersa{}; |
| 303 | netdutils::extract(nlMsgSlice, usersa); |
| 304 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 305 | EXPECT_EQ(family, usersa.family); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 306 | EXPECT_EQ(1 /* Transform Id*/, static_cast<int>(usersa.reqid)); |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 307 | EXPECT_EQ(static_cast<int>(mode), usersa.mode); |
| 308 | |
| 309 | if (mode == XfrmMode::TUNNEL) { |
| 310 | EXPECT_EQ(XFRM_STATE_AF_UNSPEC, usersa.flags); |
| 311 | } else { |
| 312 | EXPECT_EQ(0, usersa.flags); |
| 313 | } |
| 314 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 315 | EXPECT_EQ(htonl(DROID_SPI), usersa.id.spi); |
| 316 | EXPECT_EQ(IPPROTO_ESP, usersa.id.proto); |
| 317 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 318 | expectAddressEquals(family, localAddr, usersa.saddr); |
| 319 | expectAddressEquals(family, remoteAddr, usersa.id.daddr); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 320 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 321 | // Extract and check the encryption/authentication algorithms. |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 322 | Slice attr_buf = drop(nlMsgSlice, NLA_ALIGN(sizeof(xfrm_usersa_info))); |
| 323 | |
| 324 | // Extract and check the encryption/authentication algorithm |
| 325 | XfrmController::nlattr_algo_crypt encryptAlgo{}; |
| 326 | XfrmController::nlattr_algo_auth authAlgo{}; |
| 327 | auto attrHandler = [&encryptAlgo, &authAlgo](const nlattr& attr, const Slice& attr_payload) { |
| 328 | Slice buf = attr_payload; |
| 329 | if (attr.nla_type == XFRMA_ALG_CRYPT) { |
| 330 | encryptAlgo.hdr = attr; |
| 331 | netdutils::extract(buf, encryptAlgo.crypt); |
| 332 | buf = drop(buf, sizeof(xfrm_algo)); |
| 333 | netdutils::extract(buf, encryptAlgo.key); |
| 334 | } else if (attr.nla_type == XFRMA_ALG_AUTH_TRUNC) { |
| 335 | authAlgo.hdr = attr; |
| 336 | netdutils::extract(buf, authAlgo.auth); |
| 337 | buf = drop(buf, sizeof(xfrm_algo_auth)); |
| 338 | netdutils::extract(buf, authAlgo.key); |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 339 | } else { |
| 340 | FAIL() << "Unexpected nlattr type: " << attr.nla_type; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 341 | } |
| 342 | }; |
| 343 | forEachNetlinkAttribute(attr_buf, attrHandler); |
| 344 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 345 | // TODO: Use ContainerEq or ElementsAreArray to get better test failure messages. |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 346 | EXPECT_EQ(0, memcmp(reinterpret_cast<void*>(cryptKey.data()), |
| 347 | reinterpret_cast<void*>(&encryptAlgo.key), KEY_LENGTH)); |
| 348 | EXPECT_EQ(0, memcmp(reinterpret_cast<void*>(authKey.data()), |
| 349 | reinterpret_cast<void*>(&authAlgo.key), KEY_LENGTH)); |
| 350 | } |
| 351 | |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 352 | TEST_P(XfrmControllerParameterizedTest, TestTransportModeIpSecAddSecurityAssociation) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 353 | const int version = GetParam(); |
| 354 | testIpSecAddSecurityAssociation(version, mockSyscalls, XfrmMode::TRANSPORT); |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | TEST_P(XfrmControllerParameterizedTest, TestTunnelModeIpSecAddSecurityAssociation) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 358 | const int version = GetParam(); |
| 359 | testIpSecAddSecurityAssociation(version, mockSyscalls, XfrmMode::TUNNEL); |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 362 | TEST_F(XfrmControllerTest, TestIpSecAddSecurityAssociationIPv4Encap) { |
| 363 | // TODO: Implement this test, which is nearly identical to |
| 364 | // TestIpSecAddSecurityAssociation. |
| 365 | } |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 366 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 367 | // Test that input validation rejects IPv6 UDP encap. |
| 368 | TEST_F(XfrmControllerTest, TestIpSecAddSecurityAssociationIPv6Encap) { |
| 369 | EXPECT_CALL(mockSyscalls, writev(_, _)).Times(0); |
| 370 | |
| 371 | XfrmController ctrl; |
| 372 | Status res = ctrl.ipSecAddSecurityAssociation( |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 373 | 1, static_cast<int>(XfrmMode::TRANSPORT), static_cast<int>(XfrmDirection::OUT), |
| 374 | LOCALHOST_V6, TEST_ADDR_V6, 0, DROID_SPI, "hmac(sha256)", {}, 128, "cbc(aes)", |
| 375 | {}, 0, "", {}, 0, static_cast<int>(XfrmEncapType::ESPINUDP_NON_IKE), 0, 0); |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 376 | |
| 377 | EXPECT_FALSE(isOk(res)) << "IPv6 UDP encap not rejected"; |
| 378 | } |
| 379 | |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 380 | TEST_F(XfrmControllerTest, TestIpSecApplyTransportModeTransformChecksFamily) { |
| 381 | struct sockaddr socketaddr; |
| 382 | socketaddr.sa_family = AF_INET; |
| 383 | |
| 384 | unique_fd sock(socket(AF_INET, SOCK_STREAM, 0)); |
| 385 | |
| 386 | EXPECT_CALL(mockSyscalls, getsockname(Fd(sock), _, _)) |
| 387 | .WillOnce(DoAll(SetArgPointee<1>(socketaddr), Return(netdutils::status::ok))); |
| 388 | |
| 389 | XfrmController ctrl; |
| 390 | Status res = ctrl.ipSecApplyTransportModeTransform(sock, 1 /* resourceId */, |
| 391 | static_cast<int>(XfrmDirection::OUT), |
| 392 | LOCALHOST_V6, TEST_ADDR_V6, DROID_SPI); |
| 393 | |
| 394 | EXPECT_EQ(res.code(), EINVAL); |
| 395 | } |
| 396 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 397 | TEST_P(XfrmControllerParameterizedTest, TestIpSecApplyTransportModeTransform) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 398 | const int version = GetParam(); |
| 399 | const int sockFamily = (version == 4) ? AF_INET : AF_INET6; |
| 400 | const int xfrmFamily = (version == 6) ? AF_INET6: AF_INET; |
| 401 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 402 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 403 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 404 | size_t optlen = 0; |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 405 | Policy policy{}; |
| 406 | // Need to cast from void* in order to "SaveArg" policy. Easier to invoke a |
| 407 | // lambda than to write a gMock action. |
| 408 | auto SavePolicy = [&policy](const void* value) { |
| 409 | policy = *reinterpret_cast<const Policy*>(value); |
| 410 | }; |
| 411 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 412 | struct sockaddr socketaddr; |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 413 | socketaddr.sa_family = sockFamily; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 414 | |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 415 | unique_fd sock(socket(sockFamily, SOCK_STREAM, 0)); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 416 | |
| 417 | EXPECT_CALL(mockSyscalls, getsockname(_, _, _)) |
| 418 | .WillOnce(DoAll(SetArgPointee<1>(socketaddr), Return(netdutils::status::ok))); |
| 419 | |
| 420 | EXPECT_CALL(mockSyscalls, setsockopt(_, _, _, _, _)) |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 421 | .WillOnce(DoAll(WithArg<3>(Invoke(SavePolicy)), SaveArg<4>(&optlen), |
| 422 | Return(netdutils::status::ok))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 423 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 424 | XfrmController ctrl; |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 425 | Status res = ctrl.ipSecApplyTransportModeTransform(sock, 1 /* resourceId */, |
| 426 | static_cast<int>(XfrmDirection::OUT), |
| 427 | localAddr, remoteAddr, DROID_SPI); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 428 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 429 | EXPECT_TRUE(isOk(res)) << res; |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 430 | EXPECT_EQ(sizeof(Policy), optlen); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 431 | |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 432 | EXPECT_EQ(1 /* resourceId */, static_cast<int>(policy.tmpl.reqid)); |
| 433 | EXPECT_EQ(htonl(DROID_SPI), policy.tmpl.id.spi); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 434 | |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 435 | expectAddressEquals(xfrmFamily, localAddr, policy.tmpl.saddr); |
| 436 | expectAddressEquals(xfrmFamily, remoteAddr, policy.tmpl.id.daddr); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 439 | TEST_P(XfrmControllerParameterizedTest, TestIpSecDeleteSecurityAssociation) { |
Benedict Wong | 6e83b39 | 2018-01-11 18:22:33 -0800 | [diff] [blame] | 440 | const int version = GetParam(); |
| 441 | const int family = (version == 6) ? AF_INET6 : AF_INET; |
| 442 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 443 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 444 | |
| 445 | NetlinkResponse response{}; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 446 | response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 447 | Slice responseSlice = netdutils::makeSlice(response); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 448 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 449 | size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_usersa_id)); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 450 | |
| 451 | std::vector<uint8_t> nlMsgBuf; |
| 452 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 453 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 454 | EXPECT_CALL(mockSyscalls, read(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 455 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 456 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 457 | XfrmController ctrl; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 458 | Status res = ctrl.ipSecDeleteSecurityAssociation( |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 459 | 1 /* resourceId */, static_cast<int>(XfrmDirection::OUT), localAddr, remoteAddr, DROID_SPI); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 460 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 461 | EXPECT_TRUE(isOk(res)) << res; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 462 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 463 | |
| 464 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 465 | nlMsgSlice = netdutils::drop(nlMsgSlice, NLMSG_HDRLEN); |
| 466 | |
| 467 | xfrm_usersa_id said{}; |
| 468 | netdutils::extract(nlMsgSlice, said); |
| 469 | |
| 470 | EXPECT_EQ(htonl(DROID_SPI), said.spi); |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 471 | expectAddressEquals(family, remoteAddr, said.daddr); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | } // namespace net |
| 475 | } // namespace android |