github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/socket_netlink_uevent.cc (about)

     1  // Copyright 2019 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  #include <linux/filter.h>
    16  #include <linux/netlink.h>
    17  #include <sys/socket.h>
    18  #include <sys/types.h>
    19  #include <unistd.h>
    20  
    21  #include "gtest/gtest.h"
    22  #include "test/syscalls/linux/socket_netlink_util.h"
    23  #include "test/syscalls/linux/socket_test_util.h"
    24  #include "test/util/file_descriptor.h"
    25  #include "test/util/test_util.h"
    26  
    27  // Tests for NETLINK_KOBJECT_UEVENT sockets.
    28  //
    29  // gVisor never sends any messages on these sockets, so we don't test the events
    30  // themselves.
    31  
    32  namespace gvisor {
    33  namespace testing {
    34  
    35  namespace {
    36  
    37  // SO_PASSCRED can be enabled. Since no messages are sent in gVisor, we don't
    38  // actually test receiving credentials.
    39  TEST(NetlinkUeventTest, PassCred) {
    40    FileDescriptor fd =
    41        ASSERT_NO_ERRNO_AND_VALUE(NetlinkBoundSocket(NETLINK_KOBJECT_UEVENT));
    42  
    43    EXPECT_THAT(setsockopt(fd.get(), SOL_SOCKET, SO_PASSCRED, &kSockOptOn,
    44                           sizeof(kSockOptOn)),
    45                SyscallSucceeds());
    46  }
    47  
    48  // SO_DETACH_FILTER fails without a filter already installed.
    49  TEST(NetlinkUeventTest, DetachNoFilter) {
    50    FileDescriptor fd =
    51        ASSERT_NO_ERRNO_AND_VALUE(NetlinkBoundSocket(NETLINK_KOBJECT_UEVENT));
    52  
    53    int opt;
    54    EXPECT_THAT(
    55        setsockopt(fd.get(), SOL_SOCKET, SO_DETACH_FILTER, &opt, sizeof(opt)),
    56        SyscallFailsWithErrno(ENOENT));
    57  }
    58  
    59  // We can attach a BPF filter.
    60  TEST(NetlinkUeventTest, AttachFilter) {
    61    FileDescriptor fd =
    62        ASSERT_NO_ERRNO_AND_VALUE(NetlinkBoundSocket(NETLINK_KOBJECT_UEVENT));
    63  
    64    // Minimal BPF program: a single ret.
    65    struct sock_filter filter = {0x6, 0, 0, 0};
    66    struct sock_fprog prog = {};
    67    prog.len = 1;
    68    prog.filter = &filter;
    69  
    70    EXPECT_THAT(
    71        setsockopt(fd.get(), SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)),
    72        SyscallSucceeds());
    73  
    74    int opt;
    75    EXPECT_THAT(
    76        setsockopt(fd.get(), SOL_SOCKET, SO_DETACH_FILTER, &opt, sizeof(opt)),
    77        SyscallSucceeds());
    78  }
    79  
    80  }  // namespace
    81  
    82  }  // namespace testing
    83  }  // namespace gvisor