Pierre Imai | 904ce3a | 2016-02-18 13:13:12 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | * |
Lorenzo Colitti | 93a0d64 | 2019-06-26 22:31:03 +0900 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
Pierre Imai | 904ce3a | 2016-02-18 13:13:12 +0900 | [diff] [blame] | 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 | */ |
| 17 | |
Maciej Żenczykowski | 9ce7e66 | 2020-05-29 00:41:22 -0700 | [diff] [blame] | 18 | #include <arpa/inet.h> |
Pierre Imai | 904ce3a | 2016-02-18 13:13:12 +0900 | [diff] [blame] | 19 | #include <errno.h> |
Maciej Żenczykowski | 9ce7e66 | 2020-05-29 00:41:22 -0700 | [diff] [blame] | 20 | #include <linux/if_packet.h> |
| 21 | #include <linux/if_tun.h> |
| 22 | #include <net/if.h> |
| 23 | #include <poll.h> |
Maciej Żenczykowski | 7b24246 | 2020-05-13 20:53:10 -0700 | [diff] [blame] | 24 | #include <sched.h> |
Chenbo Feng | 6616547 | 2018-07-23 19:05:56 -0700 | [diff] [blame] | 25 | #include <sys/capability.h> |
Maciej Żenczykowski | 9ce7e66 | 2020-05-29 00:41:22 -0700 | [diff] [blame] | 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/socket.h> |
| 28 | #include <sys/types.h> |
Pierre Imai | 904ce3a | 2016-02-18 13:13:12 +0900 | [diff] [blame] | 29 | |
| 30 | #include <gtest/gtest.h> |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 31 | |
Maciej Żenczykowski | 9ce7e66 | 2020-05-29 00:41:22 -0700 | [diff] [blame] | 32 | #include <android-base/unique_fd.h> |
| 33 | |
| 34 | #define LOG_TAG "NetdTest" |
Maciej Żenczykowski | 9ce7e66 | 2020-05-29 00:41:22 -0700 | [diff] [blame] | 35 | |
Patrick Rohr | 70d4250 | 2021-10-12 18:24:10 +0200 | [diff] [blame] | 36 | #include "TcUtils.h" |
Maciej Żenczykowski | 9ce7e66 | 2020-05-29 00:41:22 -0700 | [diff] [blame] | 37 | |
| 38 | namespace android { |
| 39 | namespace net { |
| 40 | |
| 41 | using base::unique_fd; |
| 42 | |
Chenbo Feng | 6616547 | 2018-07-23 19:05:56 -0700 | [diff] [blame] | 43 | TEST(NetUtilsWrapperTest, TestFileCapabilities) { |
| 44 | errno = 0; |
| 45 | ASSERT_EQ(NULL, cap_get_file("/system/bin/netutils-wrapper-1.0")); |
| 46 | ASSERT_EQ(ENODATA, errno); |
| 47 | } |
Maciej Żenczykowski | 5a91482 | 2019-05-02 21:57:10 -0700 | [diff] [blame] | 48 | |
Maciej Żenczykowski | 02a8e17 | 2021-06-08 21:18:37 -0700 | [diff] [blame] | 49 | // If this test fails most likely your device is lacking device/oem specific |
| 50 | // selinux genfscon rules, something like .../vendor/.../genfs_contexts: |
| 51 | // genfscon sysfs /devices/platform/.../net u:object_r:sysfs_net:s0 |
| 52 | // Easiest debugging is via: |
| 53 | // adb root && sleep 1 && adb shell 'ls -Z /sys/class/net/*/mtu' |
| 54 | // and look for the mislabeled item(s). |
| 55 | // Everything should be 'u:object_r:sysfs_net:s0' |
| 56 | // |
| 57 | // Another useful command is: |
| 58 | // adb root && sleep 1 && adb shell find /sys > dump.out |
| 59 | // or in particular: |
| 60 | // adb root && sleep 1 && adb shell find /sys | egrep '/net$' |
| 61 | // which might (among other things) print out something like: |
| 62 | // /sys/devices/platform/11110000.usb/11110000.dwc3/gadget/net |
| 63 | // which means you need to add: |
| 64 | // genfscon sysfs /devices/platform/11110000.usb/11110000.dwc3/gadget/net u:object_r:sysfs_net:s0 |
Maciej Żenczykowski | 5a91482 | 2019-05-02 21:57:10 -0700 | [diff] [blame] | 65 | TEST(NetdSELinuxTest, CheckProperMTULabels) { |
| 66 | // Since we expect the egrep regexp to filter everything out, |
| 67 | // we thus expect no matches and thus a return code of 1 |
| 68 | // NOLINTNEXTLINE(cert-env33-c) |
| 69 | ASSERT_EQ(W_EXITCODE(1, 0), system("ls -Z /sys/class/net/*/mtu | egrep -q -v " |
| 70 | "'^u:object_r:sysfs_net:s0 /sys/class/net/'")); |
| 71 | } |
Maciej Żenczykowski | 7b24246 | 2020-05-13 20:53:10 -0700 | [diff] [blame] | 72 | |
| 73 | // Trivial thread function that simply immediately terminates successfully. |
| 74 | static int thread(void*) { |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | typedef int (*thread_t)(void*); |
| 79 | |
| 80 | static void nsTest(int flags, bool success, thread_t newThread) { |
| 81 | // We need a minimal stack, but not clear if it will grow up or down, |
| 82 | // So allocate 2 pages and give a pointer to the middle. |
| 83 | static char stack[PAGE_SIZE * 2]; |
| 84 | errno = 0; |
| 85 | // VFORK: if thread is successfully created, then kernel will wait for it |
| 86 | // to terminate before we resume -> hence static stack is safe to reuse. |
| 87 | int tid = clone(newThread, &stack[PAGE_SIZE], flags | CLONE_VFORK, NULL); |
| 88 | if (success) { |
| 89 | ASSERT_EQ(errno, 0); |
| 90 | ASSERT_GE(tid, 0); |
| 91 | } else { |
| 92 | ASSERT_EQ(errno, EINVAL); |
| 93 | ASSERT_EQ(tid, -1); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Test kernel configuration option CONFIG_NAMESPACES=y |
| 98 | TEST(NetdNamespaceTest, CheckMountNamespaceSupport) { |
| 99 | nsTest(CLONE_NEWNS, true, thread); |
| 100 | } |
| 101 | |
| 102 | // Test kernel configuration option CONFIG_UTS_NS=y |
| 103 | TEST(NetdNamespaceTest, CheckUTSNamespaceSupport) { |
| 104 | nsTest(CLONE_NEWUTS, true, thread); |
| 105 | } |
| 106 | |
| 107 | // Test kernel configuration option CONFIG_NET_NS=y |
| 108 | TEST(NetdNamespaceTest, CheckNetworkNamespaceSupport) { |
| 109 | nsTest(CLONE_NEWNET, true, thread); |
| 110 | } |
| 111 | |
| 112 | // Test kernel configuration option CONFIG_USER_NS=n |
| 113 | TEST(NetdNamespaceTest, /*DISABLED_*/ CheckNoUserNamespaceSupport) { |
| 114 | nsTest(CLONE_NEWUSER, false, thread); |
| 115 | } |
| 116 | |
| 117 | // Test for all of the above |
| 118 | TEST(NetdNamespaceTest, CheckFullNamespaceSupport) { |
| 119 | nsTest(CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWNET, true, thread); |
| 120 | } |
Maciej Żenczykowski | 9ce7e66 | 2020-05-29 00:41:22 -0700 | [diff] [blame] | 121 | |
Maciej Żenczykowski | 9ce7e66 | 2020-05-29 00:41:22 -0700 | [diff] [blame] | 122 | } // namespace net |
| 123 | } // namespace android |