summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/nacl_message_scanner.h
blob: d1360b72519730b4a45850fc86d9db24d9c2e202 (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
// Copyright 2013 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.

#ifndef PPAPI_PROXY_NACL_MESSAGE_SCANNER_H_
#define PPAPI_PROXY_NACL_MESSAGE_SCANNER_H_

#include <map>
#include <vector>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/synchronization/lock.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/proxy/ppapi_proxy_export.h"

namespace IPC {
class Message;
}

namespace ppapi {
namespace proxy {

class SerializedHandle;

class PPAPI_PROXY_EXPORT NaClMessageScanner {
 public:
  NaClMessageScanner();
  ~NaClMessageScanner();

  // Scans the message for items that require special handling. Copies any
  // SerializedHandles in the message into |handles| and if the message must be
  // rewritten for NaCl, sets |new_msg_ptr| to the new message. If no handles
  // are found, |handles| is left unchanged. If no rewriting is needed,
  // |new_msg_ptr| is left unchanged.
  //
  // See more explanation in the method definition.
  //
  // See chrome/nacl/nacl_ipc_adapter.cc for where this is used to help convert
  // native handles to NaClDescs.
  bool ScanMessage(const IPC::Message& msg,
                   std::vector<SerializedHandle>* handles,
                   scoped_ptr<IPC::Message>* new_msg_ptr);

  // Scans an untrusted message for items that require special handling. If the
  // message had to be rewritten, sets |new_msg_ptr| to the new message.
  void ScanUntrustedMessage(const IPC::Message& untrusted_msg,
                            scoped_ptr<IPC::Message>* new_msg_ptr);

  // FileSystem information for quota auditing.
  class PPAPI_PROXY_EXPORT FileSystem {
   public:
    FileSystem();
    ~FileSystem();

    int64_t reserved_quota() const { return reserved_quota_; }

    // Adds amount to reserved quota. Returns true if reserved quota >= 0.
    bool UpdateReservedQuota(int64_t delta);

   private:
    base::Lock lock_;
    // This is the remaining amount of quota reserved for the file system.
    // Acquire the lock to modify this field, since it may be used on multiple
    // threads.
    int64_t reserved_quota_;

    DISALLOW_COPY_AND_ASSIGN(FileSystem);
  };

  // FileIO information for quota auditing.
  class PPAPI_PROXY_EXPORT FileIO {
   public:
    FileIO(FileSystem* file_system, int64_t max_written_offset);
    ~FileIO();

    int64_t max_written_offset() { return max_written_offset_; }

    void SetMaxWrittenOffset(int64_t max_written_offset);

    // Grows file by the given amount. Returns true on success.
    bool Grow(int64_t amount);

   private:
    base::Lock lock_;

    // The file system that contains this file.
    FileSystem* file_system_;

    // The maximum written offset. This is initialized by NaClMessageScanner
    // when the file is opened and modified by a NaClDescQuotaInterface when the
    // plugin writes to greater maximum offsets.
    int64_t max_written_offset_;

    DISALLOW_COPY_AND_ASSIGN(FileIO);
  };

  FileIO* GetFile(PP_Resource file_io);

 private:
  friend class NaClMessageScannerTest;

  void RegisterSyncMessageForReply(const IPC::Message& msg);
  void AuditNestedMessage(PP_Resource resource,
                          const IPC::Message& msg,
                          SerializedHandle* handle);

  // When we send a synchronous message (from untrusted to trusted), we store
  // its type here, so that later we can associate the reply with its type
  // for scanning.
  typedef std::map<int, uint32> PendingSyncMsgMap;
  PendingSyncMsgMap pending_sync_msgs_;

  // We intercept FileSystem and FileIO messages to maintain information about
  // file systems and open files. This is used by NaClQuotaDescs to calculate
  // quota consumption and check it against the reserved amount.
  typedef std::map<int32_t, FileSystem*> FileSystemMap;
  FileSystemMap file_systems_;
  typedef std::map<int32_t, FileIO*> FileIOMap;
  FileIOMap files_;

  DISALLOW_COPY_AND_ASSIGN(NaClMessageScanner);
};

}  // namespace proxy
}  // namespace ppapi

#endif  // PPAPI_PROXY_NACL_MESSAGE_SCANNER_H_