-
Notifications
You must be signed in to change notification settings - Fork 159
/
scap_procs.c
1540 lines (1336 loc) · 36.1 KB
/
scap_procs.c
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2021 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>
#include <dirent.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "unixid.h"
#include "scap.h"
#include "scap-int.h"
#include "scap_linux_int.h"
#include "strerror.h"
#include "clock_helpers.h"
#include "debug_log_helpers.h"
int32_t scap_proc_fill_cwd(char* error, char* procdirname, struct scap_threadinfo* tinfo)
{
int target_res;
char filename[SCAP_MAX_PATH_SIZE];
snprintf(filename, sizeof(filename), "%scwd", procdirname);
target_res = readlink(filename, tinfo->cwd, sizeof(tinfo->cwd) - 1);
if(target_res <= 0)
{
return scap_errprintf(error, errno, "readlink %s failed", filename);
}
tinfo->cwd[target_res] = '\0';
return SCAP_SUCCESS;
}
int32_t scap_proc_fill_info_from_stats(char* error, char* procdirname, struct scap_threadinfo* tinfo)
{
char filename[SCAP_MAX_PATH_SIZE];
uint32_t pidinfo_nfound = 0;
uint32_t caps_nfound = 0;
uint32_t vm_nfound = 0;
int64_t tmp;
uint32_t uid;
uint64_t tgid;
uint64_t cap_permitted;
uint64_t cap_effective;
uint64_t cap_inheritable;
uint64_t ppid;
uint64_t vpid;
uint64_t vtid;
int64_t sid;
int64_t pgid;
int64_t vpgid;
uint32_t vmsize_kb;
uint32_t vmrss_kb;
uint32_t vmswap_kb;
uint64_t pfmajor;
uint64_t pfminor;
int32_t tty;
char line[512];
char tmpc;
char* s;
tinfo->uid = (uint32_t)-1;
tinfo->ptid = (uint32_t)-1LL;
tinfo->sid = 0;
tinfo->vpgid = 0;
tinfo->vmsize_kb = 0;
tinfo->vmrss_kb = 0;
tinfo->vmswap_kb = 0;
tinfo->pfmajor = 0;
tinfo->pfminor = 0;
tinfo->filtered_out = 0;
tinfo->tty = 0;
snprintf(filename, sizeof(filename), "%sstatus", procdirname);
FILE* f = fopen(filename, "r");
if(f == NULL)
{
ASSERT(false);
return scap_errprintf(error, errno, "open status file %s failed", filename);
}
while(fgets(line, sizeof(line), f) != NULL)
{
if(strstr(line, "Tgid") == line)
{
pidinfo_nfound++;
if(sscanf(line, "Tgid: %" PRIu64, &tgid) == 1)
{
tinfo->pid = tgid;
}
else
{
ASSERT(false);
}
}
if(strstr(line, "Uid") == line)
{
pidinfo_nfound++;
if(sscanf(line, "Uid: %" PRIu64 " %" PRIu32, &tmp, &uid) == 2)
{
tinfo->uid = uid;
}
else
{
ASSERT(false);
}
}
else if(strstr(line, "Gid") == line)
{
pidinfo_nfound++;
if(sscanf(line, "Gid: %" PRIu64 " %" PRIu32, &tmp, &uid) == 2)
{
tinfo->gid = uid;
}
else
{
ASSERT(false);
}
}
if(strstr(line, "CapInh") == line)
{
caps_nfound++;
if(sscanf(line, "CapInh: %" PRIx64, &cap_inheritable) == 1)
{
tinfo->cap_inheritable = cap_inheritable;
}
else
{
ASSERT(false);
}
}
if(strstr(line, "CapPrm") == line)
{
caps_nfound++;
if(sscanf(line, "CapPrm: %" PRIx64, &cap_permitted) == 1)
{
tinfo->cap_permitted = cap_permitted;
}
else
{
ASSERT(false);
}
}
if(strstr(line, "CapEff") == line)
{
caps_nfound++;
if(sscanf(line, "CapEff: %" PRIx64, &cap_effective) == 1)
{
tinfo->cap_effective = cap_effective;
}
else
{
ASSERT(false);
}
}
else if(strstr(line, "PPid") == line)
{
pidinfo_nfound++;
if(sscanf(line, "PPid: %" PRIu64, &ppid) == 1)
{
tinfo->ptid = ppid;
}
else
{
ASSERT(false);
}
}
else if(strstr(line, "VmSize:") == line)
{
vm_nfound++;
if(sscanf(line, "VmSize: %" PRIu32, &vmsize_kb) == 1)
{
tinfo->vmsize_kb = vmsize_kb;
}
else
{
ASSERT(false);
}
}
else if(strstr(line, "VmRSS:") == line)
{
vm_nfound++;
if(sscanf(line, "VmRSS: %" PRIu32, &vmrss_kb) == 1)
{
tinfo->vmrss_kb = vmrss_kb;
}
else
{
ASSERT(false);
}
}
else if(strstr(line, "VmSwap:") == line)
{
vm_nfound++;
if(sscanf(line, "VmSwap: %" PRIu32, &vmswap_kb) == 1)
{
tinfo->vmswap_kb = vmswap_kb;
}
else
{
ASSERT(false);
}
}
else if(strstr(line, "NSpid:") == line)
{
pidinfo_nfound++;
if(sscanf(line, "NSpid: %*u %" PRIu64, &vtid) == 1)
{
tinfo->vtid = vtid;
}
else
{
tinfo->vtid = tinfo->tid;
}
}
else if(strstr(line, "NSpgid:") == line)
{
pidinfo_nfound++;
if(sscanf(line, "NSpgid: %*u %" PRIu64, &vpgid) == 1)
{
tinfo->vpgid = vpgid;
}
}
else if(strstr(line, "NStgid:") == line)
{
pidinfo_nfound++;
if(sscanf(line, "NStgid: %*u %" PRIu64, &vpid) == 1)
{
tinfo->vpid = vpid;
}
else
{
tinfo->vpid = tinfo->pid;
}
}
if(pidinfo_nfound == 7 && caps_nfound == 3 && vm_nfound == 3)
{
break;
}
}
// We must fetch all pidinfo information
ASSERT(pidinfo_nfound == 7);
// Capability info may not be found, but it's all or nothing
ASSERT(caps_nfound == 0 || caps_nfound == 3);
// VM info may not be found, but it's all or nothing
ASSERT(vm_nfound == 0 || vm_nfound == 3);
fclose(f);
snprintf(filename, sizeof(filename), "%sstat", procdirname);
f = fopen(filename, "r");
if(f == NULL)
{
ASSERT(false);
return scap_errprintf(error, errno, "read stat file %s failed", filename);
}
size_t ssres = fread(line, 1, sizeof(line) - 1, f);
if(ssres == 0)
{
ASSERT(false);
fclose(f);
return scap_errprintf(error, errno, "Could not read from stat file %s", filename);
}
line[ssres] = 0;
s = strrchr(line, ')');
if(s == NULL)
{
ASSERT(false);
fclose(f);
return scap_errprintf(error, 0, "Could not find closing bracket in stat file %s", filename);
}
//
// Extract the line content
//
if(sscanf(s + 2, "%c %" PRId64 " %" PRId64 " %" PRId64 " %" PRId32 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
&tmpc,
&tmp,
&pgid,
&sid,
&tty,
&tmp,
&tmp,
&pfminor,
&tmp,
&pfmajor) != 10)
{
ASSERT(false);
fclose(f);
return scap_errprintf(error, 0, "Could not read expected fields from stat file %s", filename);
}
tinfo->pfmajor = pfmajor;
tinfo->pfminor = pfminor;
tinfo->sid = (uint64_t) sid;
// If we did not find vpgid above, set it to pgid from the
// global namespace.
if(tinfo->vpgid == 0)
{
tinfo->vpgid = pgid;
}
tinfo->tty = tty;
fclose(f);
return SCAP_SUCCESS;
}
//
// use prlimit to extract the RLIMIT_NOFILE for the tid. On systems where prlimit
// is not supported, just return -1
//
static int32_t scap_proc_fill_flimit(uint64_t tid, struct scap_threadinfo* tinfo)
#ifdef SYS_prlimit64
{
struct rlimit rl;
#ifdef __NR_prlimit64
if(syscall(SYS_prlimit64, tid, RLIMIT_NOFILE, NULL, &rl) == 0)
{
tinfo->fdlimit = rl.rlim_cur;
return SCAP_SUCCESS;
}
#endif
tinfo->fdlimit = -1;
return SCAP_SUCCESS;
}
#else
{
tinfo->fdlimit = -1;
return SCAP_SUCCESS;
}
#endif
int32_t scap_proc_fill_cgroups(char* error, int cgroup_version, struct scap_threadinfo* tinfo, const char* procdirname)
{
char filename[SCAP_MAX_PATH_SIZE];
char line[SCAP_MAX_CGROUPS_SIZE];
tinfo->cgroups_len = 0;
snprintf(filename, sizeof(filename), "%scgroup", procdirname);
FILE* f = fopen(filename, "r");
if(f == NULL)
{
if(errno == ENOENT || errno == EACCES)
{
return SCAP_SUCCESS;
}
ASSERT(false);
return scap_errprintf(error, errno, "open cgroup file %s failed", filename);
}
while(fgets(line, sizeof(line), f) != NULL)
{
char* token;
char* subsys_list;
char* cgroup;
char* scratch;
// Default subsys list for cgroups v2 unified hierarchy.
// These are the ones we actually use in cri container engine.
char default_subsys_list[] = "cpu,memory,cpuset";
// id
token = strtok_r(line, ":", &scratch);
if(token == NULL)
{
ASSERT(false);
fclose(f);
return scap_errprintf(error, 0, "Did not find id in cgroup file %s", filename);
}
// subsys
subsys_list = strtok_r(NULL, ":", &scratch);
if(subsys_list == NULL)
{
ASSERT(false);
fclose(f);
return scap_errprintf(error, 0, "Did not find subsys in cgroup file %s", filename);
}
// Hack to detect empty fields, because strtok does not support it
// strsep() should be used to fix this but it's not available
// on CentOS 6 (has been added from Glibc 2.19)
if(subsys_list-token-strlen(token) > 1)
{
// Subsys list empty (ie: it contains cgroup path instead)!
//
// See https://man7.org/linux/man-pages/man7/cgroups.7.html:
// 5:cpuacct,cpu,cpuset:/daemons
//
// The colon-separated fields are, from left to right:
//
// 1. For cgroups version 1 hierarchies, this field contains
// a unique hierarchy ID number that can be matched to a
// hierarchy ID in /proc/cgroups. For the cgroups version
// 2 hierarchy, this field contains the value 0.
//
// 2. For cgroups version 1 hierarchies, this field contains
// a comma-separated list of the controllers bound to the
// hierarchy. For the cgroups version 2 hierarchy, this
// field is empty.
//
// 3. This field contains the pathname of the control group
// in the hierarchy to which the process belongs. This
// pathname is relative to the mount point of the
// hierarchy.
//
// -> for cgroup2: id is always 0 and subsys list is always empty (single unified hierarchy)
// -> for cgroup1: skip subsys empty because it means controller is not mounted on any hierarchy
if (cgroup_version == 2 && strcmp(token, "0") == 0)
{
cgroup = subsys_list;
subsys_list = default_subsys_list; // force-set a default subsys list
size_t cgroup_len = strlen(cgroup);
if (cgroup_len != 0 && cgroup[cgroup_len - 1] == '\n')
{
cgroup[cgroup_len - 1] = '\0';
}
} else
{
// skip cgroups like this:
// 0::/init.scope
continue;
}
} else
{
// cgroup should be the only thing remaining so use newline as the delimiter.
cgroup = strtok_r(NULL, "\n", &scratch);
if(cgroup == NULL)
{
ASSERT(false);
fclose(f);
return scap_errprintf(error, 0, "Did not find cgroup in cgroup file %s", filename);
}
}
while((token = strtok_r(subsys_list, ",", &scratch)) != NULL)
{
subsys_list = NULL;
if(strlen(cgroup) + 1 + strlen(token) + 1 > SCAP_MAX_CGROUPS_SIZE - tinfo->cgroups_len)
{
ASSERT(false);
fclose(f);
return SCAP_SUCCESS;
}
snprintf(tinfo->cgroups + tinfo->cgroups_len, SCAP_MAX_CGROUPS_SIZE - tinfo->cgroups_len, "%s=%s", token, cgroup);
tinfo->cgroups_len += strlen(cgroup) + 1 + strlen(token) + 1;
}
}
fclose(f);
return SCAP_SUCCESS;
}
int32_t scap_proc_fill_pidns_start_ts(char* error, struct scap_threadinfo* tinfo, const char* procdirname)
{
char filename[SCAP_MAX_PATH_SIZE];
struct stat targetstat = {0};
snprintf(filename, sizeof(filename), "%sroot/proc/1", procdirname);
if(stat(filename, &targetstat) == 0)
{
tinfo->pidns_init_start_ts = targetstat.st_ctim.tv_sec * (uint64_t) 1000000000 + targetstat.st_ctim.tv_nsec;
return SCAP_SUCCESS;
}
else
{
tinfo->pidns_init_start_ts = 0;
return SCAP_FAILURE;
}
}
static int32_t scap_get_vtid(scap_t* handle, uint64_t tid, int64_t *vtid)
{
if(handle->m_vtable)
{
return handle->m_vtable->get_vtid(handle->m_engine, tid, vtid);
}
ASSERT(false);
return SCAP_FAILURE;
}
static int32_t scap_get_vpid(scap_t* handle, int64_t pid, int64_t *vpid)
{
if(handle->m_vtable)
{
return handle->m_vtable->get_vpid(handle->m_engine, pid, vpid);
}
ASSERT(false);
return SCAP_FAILURE;
}
int32_t scap_proc_fill_root(char* error, struct scap_threadinfo* tinfo, const char* procdirname)
{
char root_path[SCAP_MAX_PATH_SIZE];
snprintf(root_path, sizeof(root_path), "%sroot", procdirname);
ssize_t r = readlink(root_path, tinfo->root, sizeof(tinfo->root) - 1);
if (r > 0)
{
tinfo->root[r] = '\0';
return SCAP_SUCCESS;
}
else
{
return scap_errprintf(error, errno, "readlink %s failed", root_path);
}
}
int32_t scap_proc_fill_loginuid(char* error, struct scap_threadinfo* tinfo, const char* procdirname)
{
uint32_t loginuid;
char loginuid_path[SCAP_MAX_PATH_SIZE];
char line[512];
snprintf(loginuid_path, sizeof(loginuid_path), "%sloginuid", procdirname);
FILE* f = fopen(loginuid_path, "r");
if(f == NULL)
{
// If Linux kernel is built with CONFIG_AUDIT=n, loginuid management
// (and associated /proc file) is not implemented.
// Record default loginuid value of -1 in this case.
tinfo->loginuid = (uint32_t)-1;
return SCAP_SUCCESS;
}
if (fgets(line, sizeof(line), f) == NULL)
{
ASSERT(false);
fclose(f);
return scap_errprintf(error, errno, "Could not read loginuid from %s", loginuid_path);
}
fclose(f);
if(sscanf(line, "%" PRId32, &loginuid) == 1)
{
tinfo->loginuid = loginuid;
return SCAP_SUCCESS;
}
else
{
ASSERT(false);
return scap_errprintf(error, 0, "Could not read loginuid from %s", loginuid_path);
}
}
int32_t scap_proc_fill_exe_ino_ctime_mtime(char* error, struct scap_threadinfo* tinfo, const char *procdirname, const char *exetarget)
{
struct stat targetstat = {0};
// extract ino field from executable path if it exists
if(stat(exetarget, &targetstat) == 0)
{
tinfo->exe_ino = targetstat.st_ino;
tinfo->exe_ino_ctime = targetstat.st_ctim.tv_sec * (uint64_t) 1000000000 + targetstat.st_ctim.tv_nsec;
tinfo->exe_ino_mtime = targetstat.st_mtim.tv_sec * (uint64_t) 1000000000 + targetstat.st_mtim.tv_nsec;
}
return SCAP_SUCCESS;
}
int32_t scap_proc_fill_exe_writable(char* error, struct scap_threadinfo* tinfo, uint32_t uid, uint32_t gid, const char *procdirname, const char *exetarget)
{
char proc_exe_path[SCAP_MAX_PATH_SIZE];
struct stat targetstat;
snprintf(proc_exe_path, sizeof(proc_exe_path), "%sroot%s", procdirname, exetarget);
// if the file doesn't exist we can't determine if it was writable, assume false
if(stat(proc_exe_path, &targetstat) < 0)
{
return SCAP_SUCCESS;
}
// if you're the user owning the file you can chmod, so you can effectively write to it
if(targetstat.st_uid == uid) {
tinfo->exe_writable = true;
return SCAP_SUCCESS;
}
uid_t orig_uid = geteuid();
uid_t orig_gid = getegid();
//
// In order to check whether the current user can access the file we need to temporarily
// set the effective uid and gid of our thread to the target ones and then check access,
// but keep in mind that:
// - seteuid()/setegid() libc functions change the euid/egid of the whole process, not just
// the current thread
// - setfsuid()/setfsgid() operate on threads but cannot be paired with access(),
// so we would need to open() the file, but opening executable files in use may result
// in "text file busy" errors
//
// Therefore we need to directly call the appropriate setresuid syscall that operate on threads,
// implemented in the thread_seteuid() and thread_setegid() functions.
//
if(thread_seteuid(uid) >= 0 && thread_setegid(gid) >= 0) {
if(faccessat(0, proc_exe_path, W_OK, AT_EACCESS) == 0) {
tinfo->exe_writable = true;
}
}
int ret;
if((ret = thread_seteuid(orig_uid)) < 0)
{
return scap_errprintf(error, -ret, "Could not restore original euid from %d to %d",
uid, orig_uid);
}
if((ret = thread_setegid(orig_gid)) < 0)
{
return scap_errprintf(error, -ret, "Could not restore original egid from %d to %d",
gid, orig_gid);
}
return SCAP_SUCCESS;
}
static int scap_get_cgroup_version(const char* procdirname)
{
char dir_name[256];
int cgroup_version = -1;
FILE* f;
char line[SCAP_MAX_ENV_SIZE];
snprintf(dir_name, sizeof(dir_name), "%s/filesystems", procdirname);
f = fopen(dir_name, "r");
if (f)
{
while(fgets(line, sizeof(line), f) != NULL)
{
// NOTE: we do not support mixing cgroups v1 v2 controllers.
// Neither docker nor podman support this: https://github.com/docker/for-linux/issues/1256
if (strstr(line, "cgroup2"))
{
return 2;
}
if (strstr(line, "cgroup"))
{
cgroup_version = 1;
}
}
fclose(f);
}
return cgroup_version;
}
//
// Add a process to the list by parsing its entry under /proc
//
static int32_t scap_proc_add_from_proc(scap_t* handle, uint32_t tid, char* procdirname, struct scap_ns_socket_list** sockets_by_ns, scap_threadinfo** procinfo, uint64_t* num_fds_ret, char *error)
{
char dir_name[256];
char target_name[SCAP_MAX_PATH_SIZE];
int target_res;
char filename[252];
char line[SCAP_MAX_ENV_SIZE];
struct scap_threadinfo* tinfo;
int32_t uth_status = SCAP_SUCCESS;
FILE* f;
size_t filesize;
size_t exe_len;
bool free_tinfo = false;
int32_t res = SCAP_SUCCESS;
struct stat dirstat;
if (handle->m_cgroup_version == 0)
{
handle->m_cgroup_version = scap_get_cgroup_version(procdirname);
if(handle->m_cgroup_version < 1)
{
ASSERT(false);
return scap_errprintf(error, errno, "failed to fetch cgroup version information");
}
}
snprintf(dir_name, sizeof(dir_name), "%s/%u/", procdirname, tid);
snprintf(filename, sizeof(filename), "%sexe", dir_name);
//
// Gather the executable full name
//
target_res = readlink(filename, target_name, sizeof(target_name) - 1); // Getting the target of the exe, i.e. to which binary it points to
if(target_res <= 0)
{
//
// No exe. This either
// - a kernel thread (if there is no cmdline). In that case we skip it.
// - a process that has been containerized or has some weird thing going on. In that case
// we accept it.
//
snprintf(filename, sizeof(filename), "%scmdline", dir_name);
f = fopen(filename, "r");
if(f == NULL)
{
return SCAP_SUCCESS;
}
ASSERT(sizeof(line) >= SCAP_MAX_PATH_SIZE);
if(fgets(line, SCAP_MAX_PATH_SIZE, f) == NULL)
{
fclose(f);
return SCAP_SUCCESS;
}
else
{
fclose(f);
}
target_name[0] = 0;
}
else
{
// null-terminate target_name (readlink() does not append a null byte)
target_name[target_res] = 0;
}
//
// This is a real user level process. Allocate the procinfo structure.
//
if((tinfo = scap_proc_alloc(handle)) == NULL)
{
// Error message saved in handle->m_lasterr
return scap_errprintf(error, 0, "can't allocate procinfo struct: %s", handle->m_lasterr);
}
tinfo->tid = tid;
tinfo->fdlist = NULL;
//
// Gathers the exepath
//
snprintf(tinfo->exepath, sizeof(tinfo->exepath), "%s", target_name);
//
// Gather the command name
//
snprintf(filename, sizeof(filename), "%sstatus", dir_name);
f = fopen(filename, "r");
if(f == NULL)
{
free(tinfo);
return scap_errprintf(error, errno, "can't open %s", filename);
}
else
{
ASSERT(sizeof(line) >= SCAP_MAX_PATH_SIZE);
if(fgets(line, SCAP_MAX_PATH_SIZE, f) == NULL)
{
fclose(f);
free(tinfo);
return scap_errprintf(error, errno, "can't read from %s", filename);
}
line[SCAP_MAX_PATH_SIZE - 1] = 0;
sscanf(line, "Name:%1024s", tinfo->comm);
fclose(f);
}
bool suppressed;
if ((res = scap_update_suppressed(&handle->m_suppress, tinfo->comm, tid, 0, &suppressed)) != SCAP_SUCCESS)
{
free(tinfo);
return scap_errprintf(error, 0, "can't update set of suppressed tids");
}
if (suppressed && !procinfo)
{
free(tinfo);
return SCAP_SUCCESS;
}
//
// Gather the command line
//
snprintf(filename, sizeof(filename), "%scmdline", dir_name);
f = fopen(filename, "r");
if(f == NULL)
{
free(tinfo);
return scap_errprintf(error, errno, "can't open cmdline file %s", filename);
}
else
{
ASSERT(sizeof(line) >= SCAP_MAX_ARGS_SIZE);
filesize = fread(line, 1, SCAP_MAX_ARGS_SIZE - 1, f);
if(filesize > 0)
{
line[filesize] = 0;
exe_len = strlen(line);
if(exe_len < filesize)
{
++exe_len;
}
snprintf(tinfo->exe, SCAP_MAX_PATH_SIZE, "%s", line);
tinfo->args_len = filesize - exe_len;
memcpy(tinfo->args, line + exe_len, tinfo->args_len);
tinfo->args[SCAP_MAX_ARGS_SIZE - 1] = 0;
}
else
{
tinfo->args[0] = 0;
tinfo->exe[0] = 0;
}
fclose(f);
}
//
// Gather the environment
//
snprintf(filename, sizeof(filename), "%senviron", dir_name);
f = fopen(filename, "r");
if(f == NULL)
{
free(tinfo);
return scap_errprintf(error, errno, "can't open environ file %s", filename);
}
else
{
ASSERT(sizeof(line) >= SCAP_MAX_ENV_SIZE);
filesize = fread(line, 1, SCAP_MAX_ENV_SIZE, f);
if(filesize > 0)
{
line[filesize - 1] = 0;
tinfo->env_len = filesize;
memcpy(tinfo->env, line, tinfo->env_len);
tinfo->env[SCAP_MAX_ENV_SIZE - 1] = 0;
}
else
{
tinfo->env[0] = 0;
}
fclose(f);
}
//
// set the current working directory of the process
//
if(SCAP_FAILURE == scap_proc_fill_cwd(handle->m_lasterr, dir_name, tinfo))
{
free(tinfo);
return scap_errprintf(error, 0, "can't fill cwd for %s (%s)",
dir_name, handle->m_lasterr);
}
//
// extract the user id and ppid from /proc/pid/status
//
if(SCAP_FAILURE == scap_proc_fill_info_from_stats(handle->m_lasterr, dir_name, tinfo))
{
free(tinfo);
return scap_errprintf(error, 0, "can't fill uid and pid for %s (%s)",
dir_name, handle->m_lasterr);
}
//
// Set the file limit
//
if(SCAP_FAILURE == scap_proc_fill_flimit(tinfo->tid, tinfo))
{
free(tinfo);
return scap_errprintf(error, 0, "can't fill flimit for %s (%s)",
dir_name, handle->m_lasterr);
}
if(scap_proc_fill_cgroups(handle->m_lasterr, handle->m_cgroup_version, tinfo, dir_name) == SCAP_FAILURE)
{
free(tinfo);
return scap_errprintf(error, 0, "can't fill cgroups for %s (%s)",
dir_name, handle->m_lasterr);
}
if(scap_proc_fill_pidns_start_ts(handle->m_lasterr, tinfo, dir_name) == SCAP_FAILURE)
{
// ignore errors
// the thread may not have /proc visible so we shouldn't kill the scan if this fails
}
// These values should be read already from /status file, leave these
// fallback functions for older kernels < 4.1
if(tinfo->vtid == 0 && scap_get_vtid(handle, tinfo->tid, &tinfo->vtid) == SCAP_FAILURE)
{
tinfo->vtid = tinfo->tid;
}
if(tinfo->vpid == 0 && scap_get_vpid(handle, tinfo->tid, &tinfo->vpid) == SCAP_FAILURE)
{
tinfo->vpid = tinfo->pid;
}
//
// set the current root of the process
//
if(SCAP_FAILURE == scap_proc_fill_root(handle->m_lasterr, tinfo, dir_name))
{
free(tinfo);
return scap_errprintf(error, 0, "can't fill root for %s (%s)",
dir_name, handle->m_lasterr);
}
//
// set the loginuid
//
if(SCAP_FAILURE == scap_proc_fill_loginuid(handle->m_lasterr, tinfo, dir_name))
{
free(tinfo);
return scap_errprintf(error, 0, "can't fill loginuid for %s (%s)",
dir_name, handle->m_lasterr);
}
if(stat(dir_name, &dirstat) == 0)
{
tinfo->clone_ts = dirstat.st_ctim.tv_sec*1000000000 + dirstat.st_ctim.tv_nsec;
}
// If tid is different from pid, assume this is a thread and that the FDs are shared, and set the
// corresponding process flags.
// XXX we should see if the process creation flags are stored somewhere in /proc and handle this
// properly instead of making assumptions.
//
if(tinfo->tid == tinfo->pid)
{
tinfo->flags = 0;
}
else
{
tinfo->flags = PPM_CL_CLONE_THREAD | PPM_CL_CLONE_FILES;
}
if(SCAP_FAILURE == scap_proc_fill_exe_ino_ctime_mtime(handle->m_lasterr, tinfo, dir_name, target_name))
{