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 *
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 */
17
18 #include <arpa/inet.h>
19 #include <errno.h>
20 #include <linux/if_packet.h>
21 #include <linux/if_tun.h>
22 #include <net/if.h>
23 #include <poll.h>
24 #include <sched.h>
25 #include <sys/capability.h>
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29
30 #include <gtest/gtest.h>
31
32 #include <android-base/unique_fd.h>
33
34 #define LOG_TAG "NetdTest"
35
36 #include "TcUtils.h"
37
38 namespace android {
39 namespace net {
40
41 using base::unique_fd;
42
TEST(NetUtilsWrapperTest,TestFileCapabilities)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 }
48
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
TEST(NetdSELinuxTest,CheckProperMTULabels)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 }
72
73 // Trivial thread function that simply immediately terminates successfully.
thread(void *)74 static int thread(void*) {
75 return 0;
76 }
77
78 typedef int (*thread_t)(void*);
79
nsTest(int flags,bool success,thread_t newThread)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
TEST(NetdNamespaceTest,CheckMountNamespaceSupport)98 TEST(NetdNamespaceTest, CheckMountNamespaceSupport) {
99 nsTest(CLONE_NEWNS, true, thread);
100 }
101
102 // Test kernel configuration option CONFIG_UTS_NS=y
TEST(NetdNamespaceTest,CheckUTSNamespaceSupport)103 TEST(NetdNamespaceTest, CheckUTSNamespaceSupport) {
104 nsTest(CLONE_NEWUTS, true, thread);
105 }
106
107 // Test kernel configuration option CONFIG_NET_NS=y
TEST(NetdNamespaceTest,CheckNetworkNamespaceSupport)108 TEST(NetdNamespaceTest, CheckNetworkNamespaceSupport) {
109 nsTest(CLONE_NEWNET, true, thread);
110 }
111
112 // Test kernel configuration option CONFIG_USER_NS=n
TEST(NetdNamespaceTest,CheckNoUserNamespaceSupport)113 TEST(NetdNamespaceTest, /*DISABLED_*/ CheckNoUserNamespaceSupport) {
114 nsTest(CLONE_NEWUSER, false, thread);
115 }
116
117 // Test for all of the above
TEST(NetdNamespaceTest,CheckFullNamespaceSupport)118 TEST(NetdNamespaceTest, CheckFullNamespaceSupport) {
119 nsTest(CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWNET, true, thread);
120 }
121
122 } // namespace net
123 } // namespace android
124