1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/Forward.h>
29#include <wtf/Noncopyable.h>
30#include <wtf/ProcessID.h>
31#include <wtf/RefCounted.h>
32#include <wtf/RefPtr.h>
33#include <wtf/Vector.h>
34#include <wtf/text/WTFString.h>
35
36namespace IPC {
37class Encoder;
38class Decoder;
39}
40
41namespace WebKit {
42
43class SandboxExtensionImpl;
44
45class SandboxExtension : public RefCounted<SandboxExtension> {
46public:
47 enum class Type {
48 ReadOnly,
49 ReadWrite,
50 Mach,
51 Generic,
52 };
53
54 class Handle {
55 WTF_MAKE_NONCOPYABLE(Handle);
56 public:
57 Handle();
58#if ENABLE(SANDBOX_EXTENSIONS)
59 Handle(Handle&&);
60 Handle& operator=(Handle&&);
61#else
62 Handle(Handle&&) = default;
63 Handle& operator=(Handle&&) = default;
64#endif
65 ~Handle();
66
67 void encode(IPC::Encoder&) const;
68 static Optional<Handle> decode(IPC::Decoder&);
69
70 private:
71 friend class SandboxExtension;
72#if ENABLE(SANDBOX_EXTENSIONS)
73 mutable std::unique_ptr<SandboxExtensionImpl> m_sandboxExtension;
74#endif
75 };
76
77 class HandleArray {
78 WTF_MAKE_NONCOPYABLE(HandleArray);
79 public:
80 HandleArray();
81 HandleArray(HandleArray&&) = default;
82 HandleArray& operator=(HandleArray&&) = default;
83 ~HandleArray();
84 void allocate(size_t);
85 Handle& operator[](size_t i);
86 Handle& at(size_t i) { return operator[](i); }
87 const Handle& operator[](size_t i) const;
88 size_t size() const;
89 void encode(IPC::Encoder&) const;
90 static Optional<HandleArray> decode(IPC::Decoder&);
91
92 private:
93#if ENABLE(SANDBOX_EXTENSIONS)
94 Vector<Handle> m_data;
95#else
96 Handle m_emptyHandle;
97#endif
98 };
99
100 static RefPtr<SandboxExtension> create(Handle&&);
101 static bool createHandle(const String& path, Type, Handle&);
102 static bool createHandleWithoutResolvingPath(const String& path, Type, Handle&);
103 static bool createHandleForReadWriteDirectory(const String& path, Handle&); // Will attempt to create the directory.
104 static String createHandleForTemporaryFile(const String& prefix, Type, Handle&);
105 static bool createHandleForGenericExtension(const String& extensionClass, Handle&);
106 static bool createHandleForMachLookupByPid(const String& service, ProcessID, Handle&);
107 ~SandboxExtension();
108
109 bool consume();
110 bool revoke();
111
112 bool consumePermanently();
113 static bool consumePermanently(const Handle&);
114
115private:
116 explicit SandboxExtension(const Handle&);
117
118#if ENABLE(SANDBOX_EXTENSIONS)
119 mutable std::unique_ptr<SandboxExtensionImpl> m_sandboxExtension;
120 size_t m_useCount { 0 };
121#endif
122};
123
124#if !ENABLE(SANDBOX_EXTENSIONS)
125inline SandboxExtension::Handle::Handle() { }
126inline SandboxExtension::Handle::~Handle() { }
127inline void SandboxExtension::Handle::encode(IPC::Encoder&) const { }
128inline Optional<SandboxExtension::Handle> SandboxExtension::Handle::decode(IPC::Decoder&) { return SandboxExtension::Handle { }; }
129inline SandboxExtension::HandleArray::HandleArray() { }
130inline SandboxExtension::HandleArray::~HandleArray() { }
131inline void SandboxExtension::HandleArray::allocate(size_t) { }
132inline size_t SandboxExtension::HandleArray::size() const { return 0; }
133inline const SandboxExtension::Handle& SandboxExtension::HandleArray::operator[](size_t) const { return m_emptyHandle; }
134inline SandboxExtension::Handle& SandboxExtension::HandleArray::operator[](size_t) { return m_emptyHandle; }
135inline void SandboxExtension::HandleArray::encode(IPC::Encoder&) const { }
136inline auto SandboxExtension::HandleArray::decode(IPC::Decoder&) -> Optional<HandleArray> { return {{ }}; }
137inline RefPtr<SandboxExtension> SandboxExtension::create(Handle&&) { return nullptr; }
138inline bool SandboxExtension::createHandle(const String&, Type, Handle&) { return true; }
139inline bool SandboxExtension::createHandleWithoutResolvingPath(const String&, Type, Handle&) { return true; }
140inline bool SandboxExtension::createHandleForReadWriteDirectory(const String&, Handle&) { return true; }
141inline String SandboxExtension::createHandleForTemporaryFile(const String& /*prefix*/, Type, Handle&) {return String();}
142inline bool SandboxExtension::createHandleForGenericExtension(const String& /*extensionClass*/, Handle&) { return true; }
143inline SandboxExtension::~SandboxExtension() { }
144inline bool SandboxExtension::revoke() { return true; }
145inline bool SandboxExtension::consume() { return true; }
146inline bool SandboxExtension::consumePermanently() { return true; }
147inline bool SandboxExtension::consumePermanently(const Handle&) { return true; }
148inline String stringByResolvingSymlinksInPath(const String& path) { return path; }
149inline String resolvePathForSandboxExtension(const String& path) { return path; }
150inline String resolveAndCreateReadWriteDirectoryForSandboxExtension(const String& path) { return path; }
151#else
152String stringByResolvingSymlinksInPath(const String& path);
153String resolvePathForSandboxExtension(const String& path);
154String resolveAndCreateReadWriteDirectoryForSandboxExtension(const String& path);
155#endif
156
157} // namespace WebKit
158