gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/vdso.cc (about)

     1  // Copyright 2018 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 <string.h>
    16  #include <sys/mman.h>
    17  
    18  #include <algorithm>
    19  
    20  #include "gtest/gtest.h"
    21  #include "absl/algorithm/container.h"
    22  #include "test/util/fs_util.h"
    23  #include "test/util/posix_error.h"
    24  #include "test/util/proc_util.h"
    25  #include "test/util/test_util.h"
    26  
    27  namespace gvisor {
    28  namespace testing {
    29  
    30  namespace {
    31  
    32  // Ensure that the vvar page cannot be made writable.
    33  TEST(VvarTest, WriteVvar) {
    34    auto contents = ASSERT_NO_ERRNO_AND_VALUE(GetContents("/proc/self/maps"));
    35    auto maps = ASSERT_NO_ERRNO_AND_VALUE(ParseProcMaps(contents));
    36    auto it = absl::c_find_if(
    37        maps, [](const ProcMapsEntry& e) { return e.filename == "[vvar]"; });
    38  
    39    SKIP_IF(it == maps.end());
    40    EXPECT_THAT(mprotect(reinterpret_cast<void*>(it->start), kPageSize,
    41                         PROT_READ | PROT_WRITE),
    42                SyscallFailsWithErrno(EACCES));
    43  }
    44  
    45  }  // namespace
    46  
    47  }  // namespace testing
    48  }  // namespace gvisor