summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/renderer_sandbox_support_linux.cc
blob: 6733f75a4ed54b03ddf4931c43a314545b36ba69 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) 2009 The Chromium Authors. All rights reserved.  Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.

#include "chrome/renderer/renderer_sandbox_support_linux.h"

#include <sys/stat.h>

#include "base/eintr_wrapper.h"
#include "base/global_descriptors_posix.h"
#include "base/pickle.h"
#include "base/scoped_ptr.h"
#include "base/unix_domain_socket_posix.h"
#include "chrome/common/chrome_descriptors.h"
#include "chrome/common/sandbox_methods_linux.h"

#include "third_party/WebKit/WebKit/chromium/public/linux/WebFontRenderStyle.h"

static int GetSandboxFD() {
  return kSandboxIPCChannel + base::GlobalDescriptors::kBaseDescriptor;
}

namespace renderer_sandbox_support {

std::string getFontFamilyForCharacters(const uint16_t* utf16,
                                       size_t num_utf16) {
  Pickle request;
  request.WriteInt(LinuxSandbox::METHOD_GET_FONT_FAMILY_FOR_CHARS);
  request.WriteInt(num_utf16);
  for (size_t i = 0; i < num_utf16; ++i)
    request.WriteUInt32(utf16[i]);

  uint8_t buf[512];
  const ssize_t n = base::SendRecvMsg(GetSandboxFD(), buf, sizeof(buf), NULL,
                                      request);

  std::string family_name;
  if (n != -1) {
    Pickle reply(reinterpret_cast<char*>(buf), n);
    void* pickle_iter = NULL;
    reply.ReadString(&pickle_iter, &family_name);
  }

  return family_name;
}

void getRenderStyleForStrike(const char* family, int sizeAndStyle,
                             WebKit::WebFontRenderStyle* out) {
  Pickle request;
  request.WriteInt(LinuxSandbox::METHOD_GET_STYLE_FOR_STRIKE);
  request.WriteString(family);
  request.WriteInt(sizeAndStyle);

  uint8_t buf[512];
  const ssize_t n = base::SendRecvMsg(GetSandboxFD(), buf, sizeof(buf), NULL,
                                      request);

  out->setDefaults();
  if (n == -1) {
    return;
  }

  Pickle reply(reinterpret_cast<char*>(buf), n);
  void* pickle_iter = NULL;
  int useBitmaps, useAutoHint, useHinting, hintStyle, useAntiAlias, useSubpixel;
  if (reply.ReadInt(&pickle_iter, &useBitmaps) &&
      reply.ReadInt(&pickle_iter, &useAutoHint) &&
      reply.ReadInt(&pickle_iter, &useHinting) &&
      reply.ReadInt(&pickle_iter, &hintStyle) &&
      reply.ReadInt(&pickle_iter, &useAntiAlias) &&
      reply.ReadInt(&pickle_iter, &useSubpixel)) {
    out->useBitmaps = useBitmaps;
    out->useAutoHint = useAutoHint;
    out->useHinting = useHinting;
    out->hintStyle = hintStyle;
    out->useAntiAlias = useAntiAlias;
    out->useSubpixel = useSubpixel;
  }
}

int MakeSharedMemorySegmentViaIPC(size_t length) {
  Pickle request;
  request.WriteInt(LinuxSandbox::METHOD_MAKE_SHARED_MEMORY_SEGMENT);
  request.WriteUInt32(length);
  uint8_t reply_buf[10];
  int result_fd;
  ssize_t result = base::SendRecvMsg(GetSandboxFD(),
                                     reply_buf, sizeof(reply_buf),
                                     &result_fd, request);
  if (result == -1)
    return -1;
  return result_fd;
}

int MatchFontWithFallback(const std::string& face, bool bold,
                          bool italic, int charset) {
  Pickle request;
  request.WriteInt(LinuxSandbox::METHOD_MATCH_WITH_FALLBACK);
  request.WriteString(face);
  request.WriteBool(bold);
  request.WriteBool(italic);
  request.WriteUInt32(charset);
  uint8_t reply_buf[64];
  int fd = -1;
  base::SendRecvMsg(GetSandboxFD(), reply_buf, sizeof(reply_buf),
                    &fd, request);
  return fd;
}

bool GetFontTable(int fd, uint32_t table, uint8_t* output,
                  size_t* output_length) {
  if (table == 0) {
    struct stat st;
    if (fstat(fd, &st) < 0)
      return false;
    size_t length = st.st_size;
    if (!output) {
      *output_length = length;
      return true;
    }
    if (*output_length < length)
      return false;
    *output_length = length;
    ssize_t n = HANDLE_EINTR(pread(fd, output, length, 0));
    if (n != static_cast<ssize_t>(length))
      return false;
    return true;
  }

  unsigned num_tables;
  uint8_t num_tables_buf[2];

  ssize_t n = HANDLE_EINTR(pread(fd, &num_tables_buf, sizeof(num_tables_buf),
                           4 /* skip the font type */));
  if (n != sizeof(num_tables_buf))
    return false;

  num_tables = static_cast<unsigned>(num_tables_buf[0]) << 8 |
               num_tables_buf[1];

  // The size in bytes of an entry in the table directory.
  static const unsigned kTableEntrySize = 16;
  scoped_array<uint8_t> table_entries(
      new uint8_t[num_tables * kTableEntrySize]);
  n = HANDLE_EINTR(pread(fd, table_entries.get(), num_tables * kTableEntrySize,
                         12 /* skip the SFNT header */));
  if (n != static_cast<ssize_t>(num_tables * kTableEntrySize))
    return false;

  size_t offset;
  size_t length = 0;
  for (unsigned i = 0; i < num_tables; i++) {
    const uint8_t* entry = table_entries.get() + i * kTableEntrySize;
    if (memcmp(entry, &table, sizeof(table)) == 0) {
      offset = static_cast<size_t>(entry[8]) << 24 |
               static_cast<size_t>(entry[9]) << 16 |
               static_cast<size_t>(entry[10]) << 8  |
               static_cast<size_t>(entry[11]);
      length = static_cast<size_t>(entry[12]) << 24 |
               static_cast<size_t>(entry[13]) << 16 |
               static_cast<size_t>(entry[14]) << 8  |
               static_cast<size_t>(entry[15]);

      break;
    }
  }

  if (!length)
    return false;

  if (!output) {
    *output_length = length;
    return true;
  }

  if (*output_length < length)
    return false;

  *output_length = length;
  n = HANDLE_EINTR(pread(fd, output, length, offset));
  if (n != static_cast<ssize_t>(length))
    return false;

  return true;
}

}  // namespace render_sandbox_support