This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathseccomp.cc
141 lines (116 loc) · 4.44 KB
/
seccomp.cc
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
// Copyright (C) 2019, Rory Bradford <[email protected]>
// MIT License
#include "seccomp.h"
namespace seccomp {
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Isolate;
using v8::Local;
using v8::NewStringType;
using v8::Number;
using v8::Object;
using v8::Persistent;
using v8::String;
using v8::Value;
const char* ToCString(const v8::String::Utf8Value& value) {
return *value;
}
Persistent<Function> NodeSeccomp::constructor;
NodeSeccomp::NodeSeccomp(scmp_filter_ctx ctx) : _ctx(ctx) {
}
NodeSeccomp::~NodeSeccomp() {
seccomp_release(_ctx);
}
void NodeSeccomp::Init(Local<Object> exports) {
Isolate* isolate = exports->GetIsolate();
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
tpl->SetClassName(String::NewFromUtf8(
isolate, "NodeSeccomp", NewStringType::kNormal).ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(tpl, "init", SeccompInit);
NODE_SET_PROTOTYPE_METHOD(tpl, "reset", SeccompReset);
NODE_SET_PROTOTYPE_METHOD(tpl, "load", SeccompLoad);
NODE_SET_PROTOTYPE_METHOD(tpl, "ruleAdd", SeccompRuleAdd);
NODE_SET_PROTOTYPE_METHOD(tpl, "ruleAddExact", SeccompRuleAddExact);
Local<Context> context = isolate->GetCurrentContext();
constructor.Reset(isolate, tpl->GetFunction(context).ToLocalChecked());
exports->Set(context, String::NewFromUtf8(
isolate, "NodeSeccomp", NewStringType::kNormal).ToLocalChecked(),
tpl->GetFunction(context).ToLocalChecked()).FromJust();
}
void NodeSeccomp::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
scmp_filter_ctx ctx;
if (args.IsConstructCall()) {
NodeSeccomp* obj = new NodeSeccomp(ctx);
obj->Wrap(args.This());
args.GetReturnValue().Set(args.This());
} else {
Local<Function> cons = Local<Function>::New(isolate, constructor);
Local<Object> instance =
cons->NewInstance(context).ToLocalChecked();
args.GetReturnValue().Set(instance);
}
}
void NodeSeccomp::SeccompInit(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
NodeSeccomp* obj = ObjectWrap::Unwrap<NodeSeccomp>(args.Holder());
if ((obj->_ctx = seccomp_init(args[0].As<v8::Number>()->Value())) == 0) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Invalid argument.",
v8::NewStringType::kNormal).ToLocalChecked()));
} else {
args.GetReturnValue().Set(args.This());
}
}
void NodeSeccomp::SeccompReset(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
NodeSeccomp* obj = ObjectWrap::Unwrap<NodeSeccomp>(args.Holder());
if ((seccomp_reset(obj->_ctx, args[0].As<v8::Number>()->Value())) != 0) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Invalid argument.",
v8::NewStringType::kNormal).ToLocalChecked()));
}
}
void NodeSeccomp::SeccompLoad(const v8::FunctionCallbackInfo<v8::Value>& args) {
NodeSeccomp* obj = ObjectWrap::Unwrap<NodeSeccomp>(args.Holder());
seccomp_load(obj->_ctx);
}
void NodeSeccomp::SeccompRuleAddExact(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
NodeSeccomp* obj = ObjectWrap::Unwrap<NodeSeccomp>(args.Holder());
int type = args[0].As<v8::Number>()->Value();
v8::String::Utf8Value syscall_str(isolate, args[1]);
if (seccomp_rule_add_exact(
obj->_ctx,
type,
seccomp_syscall_resolve_name(ToCString(syscall_str)),
0
) != 0) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Invalid argument.",
v8::NewStringType::kNormal).ToLocalChecked()));
}
}
void NodeSeccomp::SeccompRuleAdd(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
NodeSeccomp* obj = ObjectWrap::Unwrap<NodeSeccomp>(args.Holder());
int type = args[0].As<v8::Number>()->Value();
v8::String::Utf8Value syscall_str(isolate, args[1]);
if (seccomp_rule_add(
obj->_ctx,
type,
seccomp_syscall_resolve_name(ToCString(syscall_str)),
0
) != 0) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Invalid argument.",
v8::NewStringType::kNormal).ToLocalChecked()));
} else {
args.GetReturnValue().Set(args.This());
}
}
} // namespace seccomp