1/*
2 * Copyright (C) 2013 Intel Corporation. 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#include "config.h"
27#include "Syscall.h"
28
29#if ENABLE(SECCOMP_FILTERS)
30
31#include "ArgumentCoders.h"
32#include "OpenSyscall.h"
33#include "SigactionSyscall.h"
34#include "SigprocmaskSyscall.h"
35#include <seccomp.h>
36
37namespace WebKit {
38
39std::unique_ptr<Syscall> Syscall::createFromContext(ucontext_t* ucontext)
40{
41    mcontext_t* mcontext = &ucontext->uc_mcontext;
42
43    switch (mcontext->gregs[REG_SYSCALL]) {
44    case __NR_open:
45        return std::make_unique<OpenSyscall>(mcontext);
46    case __NR_openat:
47        return OpenSyscall::createFromOpenatContext(mcontext);
48    case __NR_creat:
49        return OpenSyscall::createFromCreatContext(mcontext);
50    case __NR_sigprocmask:
51    case __NR_rt_sigprocmask:
52        return SigprocmaskSyscall::createFromContext(ucontext);
53    case __NR_sigaction:
54    case __NR_rt_sigaction:
55        return SigactionSyscall::createFromContext(mcontext);
56    default:
57        CRASH();
58    }
59
60    return nullptr;
61}
62
63std::unique_ptr<Syscall> Syscall::createFromDecoder(IPC::ArgumentDecoder* decoder)
64{
65    int type;
66    if (!decoder->decode(type))
67        return nullptr;
68
69    std::unique_ptr<Syscall> syscall;
70    if (type == __NR_open)
71        syscall = std::make_unique<OpenSyscall>(nullptr);
72
73    if (!syscall->decode(decoder))
74        return nullptr;
75
76    return syscall;
77}
78
79Syscall::Syscall(int type, mcontext_t* context)
80    : m_type(type)
81    , m_context(context)
82{
83}
84
85std::unique_ptr<SyscallResult> SyscallResult::createFromDecoder(IPC::ArgumentDecoder* decoder, int fd)
86{
87    int type;
88    if (!decoder->decode(type))
89        return nullptr;
90
91    std::unique_ptr<SyscallResult> result;
92    if (type == __NR_open)
93        result = std::make_unique<OpenSyscallResult>(-1, 0);
94
95    if (!result->decode(decoder, fd))
96        return nullptr;
97
98    return result;
99}
100
101SyscallResult::SyscallResult(int type)
102    : m_type(type)
103{
104}
105
106} // namespace WebKit
107
108#endif // ENABLE(SECCOMP_FILTERS)
109