gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/arch_prctl.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 <asm/prctl.h>
    16  #include <sys/prctl.h>
    17  
    18  #include "gtest/gtest.h"
    19  #include "test/util/test_util.h"
    20  
    21  // glibc does not provide a prototype for arch_prctl() so declare it here.
    22  extern "C" int arch_prctl(int code, uintptr_t addr);
    23  
    24  namespace gvisor {
    25  namespace testing {
    26  
    27  namespace {
    28  
    29  TEST(ArchPrctlTest, GetSetFS) {
    30    uintptr_t orig;
    31    const uintptr_t kNonCanonicalFsbase = 0x4141414142424242;
    32  
    33    // Get the original FS.base and then set it to the same value (this is
    34    // intentional because FS.base is the TLS pointer so we cannot change it
    35    // arbitrarily).
    36    ASSERT_THAT(arch_prctl(ARCH_GET_FS, reinterpret_cast<uintptr_t>(&orig)),
    37                SyscallSucceeds());
    38    ASSERT_THAT(arch_prctl(ARCH_SET_FS, orig), SyscallSucceeds());
    39  
    40    // Trying to set FS.base to a non-canonical value should return an error.
    41    ASSERT_THAT(arch_prctl(ARCH_SET_FS, kNonCanonicalFsbase),
    42                SyscallFailsWithErrno(EPERM));
    43  }
    44  
    45  }  // namespace
    46  
    47  }  // namespace testing
    48  }  // namespace gvisor