File: thread_isolation.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (118 lines) | stat: -rw-r--r-- 3,536 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gin/thread_isolation.h"

#if PA_BUILDFLAG(ENABLE_THREAD_ISOLATION)

#include <sys/mman.h>
#include <sys/utsname.h>

#include <cstddef>

#include "base/check.h"
#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/memory/page_size.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "partition_alloc/thread_isolation/alignment.h"

WEAK_SYMBOL extern int pkey_alloc(unsigned int flags,
                                  unsigned int access_rights);

namespace {

bool KernelHasPkruFix() {
  // PKU was broken on Linux kernels before 5.13 (see
  // https://lore.kernel.org/all/20210623121456.399107624@linutronix.de/).
  // A fix is also included in the 5.4.182 and 5.10.103 versions ("x86/fpu:
  // Correct pkru/xstate inconsistency" by Brian Geffon <bgeffon@google.com>).
  // Thus check the kernel version we are running on, and bail out if does not
  // contain the fix.
  struct utsname uname_buffer;
  CHECK_EQ(0, uname(&uname_buffer));
  int kernel, major, minor;
  // Conservatively return if the release does not match the format we expect.
  // SAFETY: required from system when uname() returns successfully.
  if (UNSAFE_BUFFERS(sscanf(uname_buffer.release, "%d.%d.%d", &kernel, &major,
                            &minor)) != 3) {
    return -1;
  }
  return kernel > 5 || (kernel == 5 && major >= 13) ||   // anything >= 5.13
         (kernel == 5 && major == 4 && minor >= 182) ||  // 5.4 >= 5.4.182
         (kernel == 5 && major == 10 && minor >= 103);   // 5.10 >= 5.10.103
}

int PkeyAlloc(int access_rights) {
  if (!pkey_alloc) {
    return -1;
  }

  static bool kernel_has_pkru_fix = KernelHasPkruFix();
  if (!kernel_has_pkru_fix) {
    return -1;
  }

  return pkey_alloc(0, access_rights);
}

uint32_t Rdpkru() {
  uint32_t pkru;
  asm volatile(".byte 0x0f,0x01,0xee\n" : "=a"(pkru) : "c"(0), "d"(0));
  return pkru;
}

void Wrpkru(uint32_t pkru) {
  asm volatile(".byte 0x0f,0x01,0xef\n" : : "a"(pkru), "c"(0), "d"(0));
}

static constexpr uint32_t kBitsPerPkey = 2;

void PkeyDisableWriteAccess(int pkey) {
#ifdef PKEY_DISABLE_WRITE
  uint32_t pkru = Rdpkru();
  uint32_t disable_write = static_cast<uint32_t>(PKEY_DISABLE_WRITE)
                           << (static_cast<uint32_t>(pkey) * kBitsPerPkey);
  Wrpkru(pkru | disable_write);
#endif
}

}  // namespace

namespace gin {

void ThreadIsolationData::InitializeBeforeThreadCreation() {
  bool page_size_mismatch = PA_THREAD_ISOLATED_ALIGN_SZ < base::GetPageSize();
  base::UmaHistogramBoolean("V8.CFIPageSizeMismatch", page_size_mismatch);
  if (page_size_mismatch) {
    // We write-protect global variables and need to align and pad them to (a
    // multiple of) the OS page size. But since page size is not a compile time
    // constant, check at runtime that our value was large enough.
    return;
  }

  pkey = PkeyAlloc(0);
  if (pkey == -1) {
    return;
  }
  allocator->Initialize(pkey);
  PkeyDisableWriteAccess(pkey);
}

bool ThreadIsolationData::Initialized() const {
  return pkey != -1;
}

ThreadIsolationData& GetThreadIsolationData() {
  static ThreadIsolationData thread_isolation_data;
  DCHECK_EQ((reinterpret_cast<size_t>(&thread_isolation_data) %
             PA_THREAD_ISOLATED_ALIGN_SZ),
            0llu);
  return thread_isolation_data;
}

}  // namespace gin

#endif  // PA_BUILDFLAG(ENABLE_THREAD_ISOLATION)