• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "bpf/BpfMap.h"
36 #include "netdbpf/bpf_shared.h"
37 
38 #include "OffloadUtils.h"
39 
40 namespace android {
41 namespace net {
42 
43 using base::unique_fd;
44 
TEST(NetUtilsWrapperTest,TestFileCapabilities)45 TEST(NetUtilsWrapperTest, TestFileCapabilities) {
46     errno = 0;
47     ASSERT_EQ(NULL, cap_get_file("/system/bin/netutils-wrapper-1.0"));
48     ASSERT_EQ(ENODATA, errno);
49 }
50 
51 // If this test fails most likely your device is lacking device/oem specific
52 // selinux genfscon rules, something like .../vendor/.../genfs_contexts:
53 //   genfscon sysfs /devices/platform/.../net u:object_r:sysfs_net:s0
54 // Easiest debugging is via:
55 //   adb root && sleep 1 && adb shell 'ls -Z /sys/class/net/*/mtu'
56 // and look for the mislabeled item(s).
57 // Everything should be 'u:object_r:sysfs_net:s0'
58 //
59 // Another useful command is:
60 //   adb root && sleep 1 && adb shell find /sys > dump.out
61 // or in particular:
62 //   adb root && sleep 1 && adb shell find /sys | egrep '/net$'
63 // which might (among other things) print out something like:
64 //   /sys/devices/platform/11110000.usb/11110000.dwc3/gadget/net
65 // which means you need to add:
66 //   genfscon sysfs /devices/platform/11110000.usb/11110000.dwc3/gadget/net u:object_r:sysfs_net:s0
TEST(NetdSELinuxTest,CheckProperMTULabels)67 TEST(NetdSELinuxTest, CheckProperMTULabels) {
68     // Since we expect the egrep regexp to filter everything out,
69     // we thus expect no matches and thus a return code of 1
70     // NOLINTNEXTLINE(cert-env33-c)
71     ASSERT_EQ(W_EXITCODE(1, 0), system("ls -Z /sys/class/net/*/mtu | egrep -q -v "
72                                        "'^u:object_r:sysfs_net:s0 /sys/class/net/'"));
73 }
74 
75 // Trivial thread function that simply immediately terminates successfully.
thread(void *)76 static int thread(void*) {
77     return 0;
78 }
79 
80 typedef int (*thread_t)(void*);
81 
nsTest(int flags,bool success,thread_t newThread)82 static void nsTest(int flags, bool success, thread_t newThread) {
83     // We need a minimal stack, but not clear if it will grow up or down,
84     // So allocate 2 pages and give a pointer to the middle.
85     static char stack[PAGE_SIZE * 2];
86     errno = 0;
87     // VFORK: if thread is successfully created, then kernel will wait for it
88     // to terminate before we resume -> hence static stack is safe to reuse.
89     int tid = clone(newThread, &stack[PAGE_SIZE], flags | CLONE_VFORK, NULL);
90     if (success) {
91         ASSERT_EQ(errno, 0);
92         ASSERT_GE(tid, 0);
93     } else {
94         ASSERT_EQ(errno, EINVAL);
95         ASSERT_EQ(tid, -1);
96     }
97 }
98 
99 // Test kernel configuration option CONFIG_NAMESPACES=y
TEST(NetdNamespaceTest,CheckMountNamespaceSupport)100 TEST(NetdNamespaceTest, CheckMountNamespaceSupport) {
101     nsTest(CLONE_NEWNS, true, thread);
102 }
103 
104 // Test kernel configuration option CONFIG_UTS_NS=y
TEST(NetdNamespaceTest,CheckUTSNamespaceSupport)105 TEST(NetdNamespaceTest, CheckUTSNamespaceSupport) {
106     nsTest(CLONE_NEWUTS, true, thread);
107 }
108 
109 // Test kernel configuration option CONFIG_NET_NS=y
TEST(NetdNamespaceTest,CheckNetworkNamespaceSupport)110 TEST(NetdNamespaceTest, CheckNetworkNamespaceSupport) {
111     nsTest(CLONE_NEWNET, true, thread);
112 }
113 
114 // Test kernel configuration option CONFIG_USER_NS=n
TEST(NetdNamespaceTest,CheckNoUserNamespaceSupport)115 TEST(NetdNamespaceTest, /*DISABLED_*/ CheckNoUserNamespaceSupport) {
116     nsTest(CLONE_NEWUSER, false, thread);
117 }
118 
119 // Test for all of the above
TEST(NetdNamespaceTest,CheckFullNamespaceSupport)120 TEST(NetdNamespaceTest, CheckFullNamespaceSupport) {
121     nsTest(CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWNET, true, thread);
122 }
123 
124 }  // namespace net
125 }  // namespace android
126