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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
#include "discover.h"
#include <yql/essentials/providers/common/schema/mkql/yql_mkql_schema.h>
#include <yql/essentials/providers/common/proto/udf_resolver.pb.h>
#include <yql/essentials/core/yql_type_annotation.h>
#include <yql/essentials/utils/backtrace/backtrace.h>
#include <yql/essentials/utils/sys/become_user.h>
#include <yql/essentials/utils/sys/linux_version.h>
#include <yql/essentials/minikql/mkql_function_registry.h>
#include <yql/essentials/minikql/mkql_node.h>
#include <yql/essentials/minikql/mkql_type_builder.h>
#include <yql/essentials/minikql/mkql_program_builder.h>
#include <yql/essentials/minikql/mkql_utils.h>
#include <library/cpp/getopt/last_getopt.h>
#include <util/generic/yexception.h>
#include <util/generic/string.h>
#include <util/generic/strbuf.h>
#include <util/generic/hash_set.h>
#include <util/generic/hash.h>
#include <util/system/env.h>
#include <util/system/fs.h>
#include <util/stream/output.h>
#include <util/string/builder.h>
#ifdef _linux_
#include <sys/types.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#ifndef GRND_RANDOM
#include <sys/random.h>
#endif
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <linux/audit.h>
#ifndef GRND_RANDOM
#include <linux/random.h>
#endif
#ifndef __SI_MAX_SIZE
#define __SI_MAX_SIZE 128
#endif
#ifndef __SI_PAD_SIZE
#if __WORDSIZE == 64
# define __SI_PAD_SIZE ((__SI_MAX_SIZE / sizeof (int)) - 4)
#else
# define __SI_PAD_SIZE ((__SI_MAX_SIZE / sizeof (int)) - 3)
#endif
#endif
#endif
using namespace NKikimr;
using namespace NKikimr::NMiniKQL;
void ResolveUDFs() {
NYql::TResolve inMsg;
if (!inMsg.ParseFromArcadiaStream(&Cin)) {
throw yexception() << "Bad input TResolve proto message";
}
NYql::TResolveResult outMsg;
auto functionRegistry = CreateFunctionRegistry(IBuiltinFunctionRegistry::TPtr());
auto newRegistry = functionRegistry->Clone();
newRegistry->SetBackTraceCallback(&NYql::NBacktrace::KikimrBackTrace);
TScopedAlloc alloc(__LOCATION__);
TTypeEnvironment env(alloc);
NUdf::ITypeInfoHelper::TPtr typeInfoHelper(new TTypeInfoHelper);
TSet<TString> loadedPaths;
// Load system imports first
for (auto& import : inMsg.GetImports()) {
if (import.GetSystem()) {
if (!loadedPaths.emplace(import.GetPath()).second) {
continue;
}
newRegistry->LoadUdfs(import.GetPath(), {}, NUdf::IRegistrator::TFlags::TypesOnly);
}
}
THashMap<std::pair<TString, TString>, THashSet<TString>> cachedModules;
for (auto& import : inMsg.GetImports()) {
if (!import.GetSystem()) {
auto importRes = outMsg.AddImports();
importRes->SetFileAlias(import.GetFileAlias());
importRes->SetCustomUdfPrefix(import.GetCustomUdfPrefix());
auto [it, inserted] = cachedModules.emplace(std::make_pair(import.GetPath(), import.GetCustomUdfPrefix()), THashSet<TString>());
if (inserted) {
THashSet<TString> modules;
newRegistry->LoadUdfs(import.GetPath(),
{},
NUdf::IRegistrator::TFlags::TypesOnly,
import.GetCustomUdfPrefix(),
&modules);
NUdfResolver::FillImportResultModules(modules, *importRes);
it->second = modules;
} else {
NUdfResolver::FillImportResultModules(it->second, *importRes);
}
}
}
for (size_t i = 0; i < inMsg.UdfsSize(); ++i) {
auto& udf = inMsg.GetUdfs(i);
auto udfRes = outMsg.AddUdfs();
try {
TProgramBuilder pgmBuilder(env, *newRegistry);
TType* mkqlUserType = nullptr;
if (udf.HasUserType()) {
TStringStream err;
mkqlUserType = NYql::NCommon::ParseTypeFromYson(TStringBuf{udf.GetUserType()}, pgmBuilder, err);
if (!mkqlUserType) {
udfRes->SetError(TStringBuilder() << "Invalid user type for function: "
<< udf.GetName() << ", error: " << err.Str());
continue;
}
}
TFunctionTypeInfo funcInfo;
auto status = newRegistry->FindFunctionTypeInfo(env, typeInfoHelper, nullptr,
udf.GetName(), mkqlUserType, udf.GetTypeConfig(), NUdf::IUdfModule::TFlags::TypesOnly, {}, nullptr, &funcInfo);
if (!status.IsOk()) {
udfRes->SetError(TStringBuilder() << "Failed to find UDF function: " << udf.GetName()
<< ", reason: " << status.GetError());
continue;
}
udfRes->SetCallableType(NYql::NCommon::WriteTypeToYson(funcInfo.FunctionType));
if (funcInfo.RunConfigType) {
udfRes->SetRunConfigType(NYql::NCommon::WriteTypeToYson(funcInfo.RunConfigType));
}
if (funcInfo.UserType) {
udfRes->SetNormalizedUserType(NYql::NCommon::WriteTypeToYson(funcInfo.UserType));
}
udfRes->SetSupportsBlocks(funcInfo.SupportsBlocks);
udfRes->SetIsStrict(funcInfo.IsStrict);
} catch (yexception& e) {
udfRes->SetError(TStringBuilder()
<< "Internal error was found when udf metadata is loading for function: " << udf.GetName()
<< ", reason: " << e.what());
}
}
outMsg.SerializeToArcadiaStream(&Cout);
}
void ListModules(const TString& dir) {
TVector<TString> udfPaths;
NMiniKQL::FindUdfsInDir(dir, &udfPaths);
auto funcRegistry = CreateFunctionRegistry(&NYql::NBacktrace::KikimrBackTrace, IBuiltinFunctionRegistry::TPtr(), false, udfPaths,
NUdf::IRegistrator::TFlags::TypesOnly);
for (auto& m : funcRegistry->GetAllModuleNames()) {
auto path = *funcRegistry->FindUdfPath(m);
Cout << m << '\t' << path << Endl;
}
}
// NOLINTBEGIN(readability-identifier-naming)
#ifdef _linux_
struct my_siginfo_t
{
int si_signo; /* Signal number. */
#if __SI_ERRNO_THEN_CODE
int si_errno; /* If non-zero, an errno value associated with
this signal, as defined in <errno.h>. */
int si_code; /* Signal code. */
#else
int si_code;
int si_errno;
#endif
#if __WORDSIZE == 64
int __pad0; /* Explicit padding. */
#endif
union
{
int _pad[__SI_PAD_SIZE];
struct
{
void *_call_addr; /* Calling user insn. */
int _syscall; /* Triggering system call number. */
unsigned int _arch; /* AUDIT_ARCH_* of syscall. */
} _sigsys;
} _sifields;
};
// NOLINTEND(readability-identifier-naming)
void SigSysHandler(int sig, my_siginfo_t *info, void *) {
Cerr << "SigSysHandler: " << sig << ", code: " << info->si_code << ", errno: " <<
info->si_errno << ", call: " << info->_sifields._sigsys._syscall << ", arch:" << info->_sifields._sigsys._arch << "\n";
// repeat SIGSYS signal (this will kill current process)
raise(sig);
}
#endif
int main(int argc, char **argv) {
NYql::NBacktrace::RegisterKikimrFatalActions();
NYql::NBacktrace::EnableKikimrSymbolize();
try {
#ifdef _linux_
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
typedef void (*TSigSysHandler)(int, siginfo_t *, void *);
sa.sa_sigaction = (TSigSysHandler)SigSysHandler;
sigfillset(&sa.sa_mask);
if (sigaction(SIGSYS, &sa, nullptr) == -1) {
ythrow TSystemError() << "Cannot set handler for signal " << strsignal(SIGSYS);
}
#endif
TString path;
TString user;
TString group;
bool printAsProto = true;
NLastGetopt::TOpts opts = NLastGetopt::TOpts::Default();
opts.AddLongOption('L', "list", "List UDF modules in specified directory")
.Optional()
.RequiredArgument("DIR")
.StoreResult(&path);
opts.AddLongOption('D', "discover-path", "Discover UDFs in folder or single file")
.Optional()
.RequiredArgument("DIR")
.StoreResult(&path);
opts.AddLongOption('P', "discover-proto", "Discover UDFs according to TResolve proto from cin")
.Optional()
.NoArgument();
opts.AddLongOption('A', "as-proto", "Print result in protobuf format")
.Optional()
.DefaultValue(true)
.StoreResult(&printAsProto);
opts.AddLongOption('U', "user", "Run as user")
.Optional()
.StoreResult(&user);
opts.AddLongOption('G', "group", "Run as group")
.Optional()
.StoreResult(&group);
opts.AddLongOption('F', "filter-syscalls", "Filter syscalls")
.Optional()
.NoArgument();
opts.SetFreeArgsNum(0);
NLastGetopt::TOptsParseResult res(&opts, argc, argv);
SetEnv("USER", user ? user : "udf_resolver");
if (user && !NYql::IsLinuxKernelBelow4_3()) {
// since we are going to load untrusted modules
// we have to switch into another user and drop privileges (capabilities)
NYql::BecomeUser(user, group);
NYql::TurnOffBecomeUserAbility();
}
NYql::SendSignalOnParentThreadExit(SIGTERM);
#ifdef _linux_
if (rlimit limit = {0, 0}; setrlimit(RLIMIT_CORE, &limit) != 0) {
ythrow TSystemError() << "Failed to set RLIMIT_CORE";
}
#endif
if (res.Has("filter-syscalls")) {
#ifdef _linux_
#define ArchField offsetof(struct seccomp_data, arch) // NOLINT(readability-identifier-naming)
// NOLINTNEXTLINE(readability-identifier-naming)
#define Allow(syscall) \
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SYS_##syscall, 0, 1), \
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
struct sock_filter filter[] = {
/* validate arch */
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, ArchField),
BPF_JUMP( BPF_JMP+BPF_JEQ+BPF_K, AUDIT_ARCH_X86_64, 1, 0),
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_TRAP),
/* load syscall */
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, offsetof(struct seccomp_data, nr)),
/* list of allowed syscalls */
#ifndef _arm64_
Allow(access),
#endif
Allow(brk),
Allow(chdir),
Allow(clock_gettime),
Allow(clock_nanosleep),
Allow(clone),
Allow(close),
#ifndef _arm64_
Allow(creat),
#endif
Allow(dup),
#ifndef _arm64_
Allow(dup2),
#endif
Allow(dup3),
Allow(eventfd2),
Allow(exit),
Allow(exit_group),
Allow(fadvise64),
Allow(fallocate),
Allow(flock),
Allow(fstat),
Allow(fsync),
Allow(ftruncate),
Allow(futex),
Allow(get_robust_list),
Allow(getcwd),
#ifndef _arm64_
Allow(getdents),
#endif
Allow(getdents64),
Allow(getegid),
Allow(geteuid),
Allow(getgid),
Allow(getgroups),
Allow(getpgid),
#ifndef _arm64_
Allow(getpgrp),
#endif
Allow(getpid),
Allow(getppid),
Allow(getpriority),
Allow(getrandom),
Allow(getrlimit),
Allow(getrusage),
Allow(getsid),
Allow(gettid),
Allow(gettimeofday),
Allow(getuid),
Allow(getxattr),
Allow(ioctl),
Allow(lgetxattr),
#ifndef _arm64_
Allow(link),
#endif
Allow(listxattr),
Allow(llistxattr),
Allow(lremovexattr),
Allow(lseek),
Allow(lsetxattr),
#ifndef _arm64_
Allow(lstat),
#endif
Allow(madvise),
#ifndef _arm64_
Allow(mkdir),
#endif
Allow(mkdirat),
Allow(mlock),
Allow(mlockall),
Allow(mmap),
Allow(mprotect),
Allow(munlock),
Allow(munlockall),
Allow(munmap),
Allow(nanosleep),
#ifndef _arm64_
Allow(open),
#endif
Allow(openat),
Allow(pipe2),
Allow(prctl),
Allow(pread64),
Allow(pwrite64),
Allow(read),
#ifndef _arm64_
Allow(readlink),
#endif
Allow(readv),
Allow(removexattr),
#ifndef _arm64_
Allow(rename),
#endif
Allow(renameat),
#ifndef _arm64_
Allow(rmdir),
#endif
Allow(rt_sigaction),
Allow(rt_sigpending),
Allow(rt_sigprocmask),
Allow(rt_sigqueueinfo),
Allow(rt_sigreturn),
Allow(rt_sigsuspend),
Allow(rt_sigtimedwait),
Allow(rt_tgsigqueueinfo),
Allow(sched_getaffinity),
Allow(sched_setaffinity),
Allow(set_robust_list),
Allow(setxattr),
#ifndef _arm64_
Allow(stat),
#endif
Allow(sysinfo),
Allow(uname),
#ifndef _arm64_
Allow(unlink),
#endif
Allow(unlinkat),
Allow(write),
Allow(writev),
/* and if we don't match above, die */
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_TRAP),
};
struct sock_fprog filterprog = {
.len = sizeof(filter)/sizeof(filter[0]),
.filter = filter
};
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) {
ythrow yexception() << "prctl(PR_SET_NO_NEW_PRIVS, 1, ...) failed with: " << LastSystemErrorText();
}
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &filterprog) == -1) {
ythrow yexception() << "prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, ...) failed with: " << LastSystemErrorText();
}
#endif
}
Y_UNUSED(NUdf::GetStaticSymbols());
if (res.Has("list")) {
ListModules(path);
return 0;
}
if (res.Has("discover-path")) {
NFs::EnsureExists(path);
TFileStat fstat(path);
if (fstat.IsDir()) {
NUdfResolver::DiscoverInDir(path, Cout, printAsProto);
} else {
NUdfResolver::DiscoverInFile(path, Cout, printAsProto);
}
return 0;
}
if (res.Has("discover-proto")) {
NUdfResolver::Discover(Cin, Cout, printAsProto);
return 0;
}
ResolveUDFs();
return 0;
} catch (...) {
Cerr << CurrentExceptionMessage() << Endl;
return 1;
}
}
|