1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef SECURITY_RLBOX_UTILS_H_
8 #define SECURITY_RLBOX_UTILS_H_
10 #include "mozilla/rlbox/rlbox_types.hpp"
14 /* The RLBoxTransferBufferToSandbox class is used to copy (or directly expose in
15 * the noop-sandbox case) buffers into the sandbox that are automatically freed
16 * when the RLBoxTransferBufferToSandbox is out of scope. NOTE: The sandbox
17 * lifetime must outlive all of its RLBoxTransferBufferToSandbox.
19 template <typename T
, typename S
>
20 class MOZ_STACK_CLASS RLBoxTransferBufferToSandbox
{
22 RLBoxTransferBufferToSandbox() = delete;
23 RLBoxTransferBufferToSandbox(rlbox::rlbox_sandbox
<S
>* aSandbox
, const T
* aBuf
,
25 : mSandbox(aSandbox
), mCopied(false), mBuf(nullptr) {
27 mBuf
= rlbox::copy_memory_or_grant_access(*mSandbox
, aBuf
, aLen
, false,
31 ~RLBoxTransferBufferToSandbox() {
33 mSandbox
->free_in_sandbox(mBuf
);
36 rlbox::tainted
<const T
*, S
> operator*() const { return mBuf
; };
39 rlbox::rlbox_sandbox
<S
>* mSandbox
;
41 rlbox::tainted
<const T
*, S
> mBuf
;
44 /* The RLBoxAllocateInSandbox class is used to allocate data int sandbox that is
45 * automatically freed when the RLBoxAllocateInSandbox is out of scope. NOTE:
46 * The sandbox lifetime must outlive all of its RLBoxAllocateInSandbox'ations.
48 template <typename T
, typename S
>
49 class MOZ_STACK_CLASS RLBoxAllocateInSandbox
{
51 RLBoxAllocateInSandbox() = delete;
52 explicit RLBoxAllocateInSandbox(rlbox::rlbox_sandbox
<S
>* aSandbox
)
53 : mSandbox(aSandbox
) {
54 mPtr
= mSandbox
->template malloc_in_sandbox
<T
>();
56 ~RLBoxAllocateInSandbox() {
58 mSandbox
->free_in_sandbox(mPtr
);
61 rlbox::tainted
<T
*, S
> get() const { return mPtr
; };
64 rlbox::rlbox_sandbox
<S
>* mSandbox
;
65 rlbox::tainted
<T
*, S
> mPtr
;
68 } // namespace mozilla