Dodany przez: Seccomp BPF w postaci drzewca, 21:35 21-07-2023

Nowy Pobierz
  1. /**< wstawianie elementów do drzewca za https://www.geeksforgeeks.org/treap-a-randomized-binary-search-tree/
  2. przechodzenie drzewa levelorder za https://www.interviewbit.com/blog/level-order-traversal/#1-c-implementation
  3. reszta moja.
  4. W pliku wejściowym każda linijka musi zawierać priorytet i nazwę wywołania systemowego od dzielone spacją.
  5. */
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <vector>
  10. #include <queue>
  11. #include <algorithm>
  12. #include <sys/syscall.h>
  13.  
  14. using namespace std;
  15.  
  16.  
  17. struct SyscallStruct{
  18.     string name;
  19.     int number;
  20. };
  21.  
  22. struct SyscallNode
  23. {
  24.     int number;
  25.     int left = -1;
  26.     int right = -1;
  27. };
  28.  
  29. vector<SyscallStruct> syscalls ({{"__NR_read", __NR_read}, {"__NR_write", __NR_write}, {"__NR_open", __NR_open}, {"__NR_close", __NR_close}, {"__NR_stat", __NR_stat}, {"__NR_fstat", __NR_fstat}, {"__NR_lstat", __NR_lstat}, {"__NR_poll", __NR_poll}, {"__NR_lseek", __NR_lseek}, {"__NR_mmap", __NR_mmap}, {"__NR_mprotect", __NR_mprotect}, {"__NR_munmap", __NR_munmap}, {"__NR_brk", __NR_brk}, {"__NR_rt_sigaction", __NR_rt_sigaction}, {"__NR_rt_sigprocmask", __NR_rt_sigprocmask}, {"__NR_rt_sigreturn", __NR_rt_sigreturn}, {"__NR_ioctl", __NR_ioctl}, {"__NR_pread64", __NR_pread64}, {"__NR_pwrite64", __NR_pwrite64}, {"__NR_readv", __NR_readv}, {"__NR_writev", __NR_writev}, {"__NR_access", __NR_access}, {"__NR_pipe", __NR_pipe}, {"__NR_select", __NR_select}, {"__NR_sched_yield", __NR_sched_yield}, {"__NR_mremap", __NR_mremap}, {"__NR_msync", __NR_msync}, {"__NR_mincore", __NR_mincore}, {"__NR_madvise", __NR_madvise}, {"__NR_shmget", __NR_shmget}, {"__NR_shmat", __NR_shmat}, {"__NR_shmctl", __NR_shmctl}, {"__NR_dup", __NR_dup}, {"__NR_dup2", __NR_dup2}, {"__NR_pause", __NR_pause}, {"__NR_nanosleep", __NR_nanosleep}, {"__NR_getitimer", __NR_getitimer}, {"__NR_alarm", __NR_alarm}, {"__NR_setitimer", __NR_setitimer}, {"__NR_getpid", __NR_getpid}, {"__NR_sendfile", __NR_sendfile}, {"__NR_socket", __NR_socket}, {"__NR_connect", __NR_connect}, {"__NR_accept", __NR_accept}, {"__NR_sendto", __NR_sendto}, {"__NR_recvfrom", __NR_recvfrom}, {"__NR_sendmsg", __NR_sendmsg}, {"__NR_recvmsg", __NR_recvmsg}, {"__NR_shutdown", __NR_shutdown}, {"__NR_bind", __NR_bind}, {"__NR_listen", __NR_listen}, {"__NR_getsockname", __NR_getsockname}, {"__NR_getpeername", __NR_getpeername}, {"__NR_socketpair", __NR_socketpair}, {"__NR_setsockopt", __NR_setsockopt}, {"__NR_getsockopt", __NR_getsockopt}, {"__NR_clone", __NR_clone}, {"__NR_fork", __NR_fork}, {"__NR_vfork", __NR_vfork}, {"__NR_execve", __NR_execve}, {"__NR_exit", __NR_exit}, {"__NR_wait4", __NR_wait4}, {"__NR_kill", __NR_kill}, {"__NR_uname", __NR_uname}, {"__NR_semget", __NR_semget}, {"__NR_semop", __NR_semop}, {"__NR_semctl", __NR_semctl}, {"__NR_shmdt", __NR_shmdt}, {"__NR_msgget", __NR_msgget}, {"__NR_msgsnd", __NR_msgsnd}, {"__NR_msgrcv", __NR_msgrcv}, {"__NR_msgctl", __NR_msgctl}, {"__NR_fcntl", __NR_fcntl}, {"__NR_flock", __NR_flock}, {"__NR_fsync", __NR_fsync}, {"__NR_fdatasync", __NR_fdatasync}, {"__NR_truncate", __NR_truncate}, {"__NR_ftruncate", __NR_ftruncate}, {"__NR_getdents", __NR_getdents}, {"__NR_getcwd", __NR_getcwd}, {"__NR_chdir", __NR_chdir}, {"__NR_fchdir", __NR_fchdir}, {"__NR_rename", __NR_rename}, {"__NR_mkdir", __NR_mkdir}, {"__NR_rmdir", __NR_rmdir}, {"__NR_creat", __NR_creat}, {"__NR_link", __NR_link}, {"__NR_unlink", __NR_unlink}, {"__NR_symlink", __NR_symlink}, {"__NR_readlink", __NR_readlink}, {"__NR_chmod", __NR_chmod}, {"__NR_fchmod", __NR_fchmod}, {"__NR_chown", __NR_chown}, {"__NR_fchown", __NR_fchown}, {"__NR_lchown", __NR_lchown}, {"__NR_umask", __NR_umask}, {"__NR_gettimeofday", __NR_gettimeofday}, {"__NR_getrlimit", __NR_getrlimit}, {"__NR_getrusage", __NR_getrusage}, {"__NR_sysinfo", __NR_sysinfo}, {"__NR_times", __NR_times}, {"__NR_ptrace", __NR_ptrace}, {"__NR_getuid", __NR_getuid}, {"__NR_syslog", __NR_syslog}, {"__NR_getgid", __NR_getgid}, {"__NR_setuid", __NR_setuid}, {"__NR_setgid", __NR_setgid}, {"__NR_geteuid", __NR_geteuid}, {"__NR_getegid", __NR_getegid}, {"__NR_setpgid", __NR_setpgid}, {"__NR_getppid", __NR_getppid}, {"__NR_getpgrp", __NR_getpgrp}, {"__NR_setsid", __NR_setsid}, {"__NR_setreuid", __NR_setreuid}, {"__NR_setregid", __NR_setregid}, {"__NR_getgroups", __NR_getgroups}, {"__NR_setgroups", __NR_setgroups}, {"__NR_setresuid", __NR_setresuid}, {"__NR_getresuid", __NR_getresuid}, {"__NR_setresgid", __NR_setresgid}, {"__NR_getresgid", __NR_getresgid}, {"__NR_getpgid", __NR_getpgid}, {"__NR_setfsuid", __NR_setfsuid}, {"__NR_setfsgid", __NR_setfsgid}, {"__NR_getsid", __NR_getsid}, {"__NR_capget", __NR_capget}, {"__NR_capset", __NR_capset}, {"__NR_rt_sigpending", __NR_rt_sigpending}, {"__NR_rt_sigtimedwait", __NR_rt_sigtimedwait}, {"__NR_rt_sigqueueinfo", __NR_rt_sigqueueinfo}, {"__NR_rt_sigsuspend", __NR_rt_sigsuspend}, {"__NR_sigaltstack", __NR_sigaltstack}, {"__NR_utime", __NR_utime}, {"__NR_mknod", __NR_mknod}, {"__NR_uselib", __NR_uselib}, {"__NR_personality", __NR_personality}, {"__NR_ustat", __NR_ustat}, {"__NR_statfs", __NR_statfs}, {"__NR_fstatfs", __NR_fstatfs}, {"__NR_sysfs", __NR_sysfs}, {"__NR_getpriority", __NR_getpriority}, {"__NR_setpriority", __NR_setpriority}, {"__NR_sched_setparam", __NR_sched_setparam}, {"__NR_sched_getparam", __NR_sched_getparam}, {"__NR_sched_setscheduler", __NR_sched_setscheduler}, {"__NR_sched_getscheduler", __NR_sched_getscheduler}, {"__NR_sched_get_priority_max", __NR_sched_get_priority_max}, {"__NR_sched_get_priority_min", __NR_sched_get_priority_min}, {"__NR_sched_rr_get_interval", __NR_sched_rr_get_interval}, {"__NR_mlock", __NR_mlock}, {"__NR_munlock", __NR_munlock}, {"__NR_mlockall", __NR_mlockall}, {"__NR_munlockall", __NR_munlockall}, {"__NR_vhangup", __NR_vhangup}, {"__NR_modify_ldt", __NR_modify_ldt}, {"__NR_pivot_root", __NR_pivot_root}, {"__NR__sysctl", __NR__sysctl}, {"__NR_prctl", __NR_prctl}, {"__NR_arch_prctl", __NR_arch_prctl}, {"__NR_adjtimex", __NR_adjtimex}, {"__NR_setrlimit", __NR_setrlimit}, {"__NR_chroot", __NR_chroot}, {"__NR_sync", __NR_sync}, {"__NR_acct", __NR_acct}, {"__NR_settimeofday", __NR_settimeofday}, {"__NR_mount", __NR_mount}, {"__NR_umount2", __NR_umount2}, {"__NR_swapon", __NR_swapon}, {"__NR_swapoff", __NR_swapoff}, {"__NR_reboot", __NR_reboot}, {"__NR_sethostname", __NR_sethostname}, {"__NR_setdomainname", __NR_setdomainname}, {"__NR_iopl", __NR_iopl}, {"__NR_ioperm", __NR_ioperm}, {"__NR_create_module", __NR_create_module}, {"__NR_init_module", __NR_init_module}, {"__NR_delete_module", __NR_delete_module}, {"__NR_get_kernel_syms", __NR_get_kernel_syms}, {"__NR_query_module", __NR_query_module}, {"__NR_quotactl", __NR_quotactl}, {"__NR_nfsservctl", __NR_nfsservctl}, {"__NR_getpmsg", __NR_getpmsg}, {"__NR_putpmsg", __NR_putpmsg}, {"__NR_afs_syscall", __NR_afs_syscall}, {"__NR_tuxcall", __NR_tuxcall}, {"__NR_security", __NR_security}, {"__NR_gettid", __NR_gettid}, {"__NR_readahead", __NR_readahead}, {"__NR_setxattr", __NR_setxattr}, {"__NR_lsetxattr", __NR_lsetxattr}, {"__NR_fsetxattr", __NR_fsetxattr}, {"__NR_getxattr", __NR_getxattr}, {"__NR_lgetxattr", __NR_lgetxattr}, {"__NR_fgetxattr", __NR_fgetxattr}, {"__NR_listxattr", __NR_listxattr}, {"__NR_llistxattr", __NR_llistxattr}, {"__NR_flistxattr", __NR_flistxattr}, {"__NR_removexattr", __NR_removexattr}, {"__NR_lremovexattr", __NR_lremovexattr}, {"__NR_fremovexattr", __NR_fremovexattr}, {"__NR_tkill", __NR_tkill}, {"__NR_time", __NR_time}, {"__NR_futex", __NR_futex}, {"__NR_sched_setaffinity", __NR_sched_setaffinity}, {"__NR_sched_getaffinity", __NR_sched_getaffinity}, {"__NR_set_thread_area", __NR_set_thread_area}, {"__NR_io_setup", __NR_io_setup}, {"__NR_io_destroy", __NR_io_destroy}, {"__NR_io_getevents", __NR_io_getevents}, {"__NR_io_submit", __NR_io_submit}, {"__NR_io_cancel", __NR_io_cancel}, {"__NR_get_thread_area", __NR_get_thread_area}, {"__NR_lookup_dcookie", __NR_lookup_dcookie}, {"__NR_epoll_create", __NR_epoll_create}, {"__NR_epoll_ctl_old", __NR_epoll_ctl_old}, {"__NR_epoll_wait_old", __NR_epoll_wait_old}, {"__NR_remap_file_pages", __NR_remap_file_pages}, {"__NR_getdents64", __NR_getdents64}, {"__NR_set_tid_address", __NR_set_tid_address}, {"__NR_restart_syscall", __NR_restart_syscall}, {"__NR_semtimedop", __NR_semtimedop}, {"__NR_fadvise64", __NR_fadvise64}, {"__NR_timer_create", __NR_timer_create}, {"__NR_timer_settime", __NR_timer_settime}, {"__NR_timer_gettime", __NR_timer_gettime}, {"__NR_timer_getoverrun", __NR_timer_getoverrun}, {"__NR_timer_delete", __NR_timer_delete}, {"__NR_clock_settime", __NR_clock_settime}, {"__NR_clock_gettime", __NR_clock_gettime}, {"__NR_clock_getres", __NR_clock_getres}, {"__NR_clock_nanosleep", __NR_clock_nanosleep}, {"__NR_exit_group", __NR_exit_group}, {"__NR_epoll_wait", __NR_epoll_wait}, {"__NR_epoll_ctl", __NR_epoll_ctl}, {"__NR_tgkill", __NR_tgkill}, {"__NR_utimes", __NR_utimes}, {"__NR_vserver", __NR_vserver}, {"__NR_mbind", __NR_mbind}, {"__NR_set_mempolicy", __NR_set_mempolicy}, {"__NR_get_mempolicy", __NR_get_mempolicy}, {"__NR_mq_open", __NR_mq_open}, {"__NR_mq_unlink", __NR_mq_unlink}, {"__NR_mq_timedsend", __NR_mq_timedsend}, {"__NR_mq_timedreceive", __NR_mq_timedreceive}, {"__NR_mq_notify", __NR_mq_notify}, {"__NR_mq_getsetattr", __NR_mq_getsetattr}, {"__NR_kexec_load", __NR_kexec_load}, {"__NR_waitid", __NR_waitid}, {"__NR_add_key", __NR_add_key}, {"__NR_request_key", __NR_request_key}, {"__NR_keyctl", __NR_keyctl}, {"__NR_ioprio_set", __NR_ioprio_set}, {"__NR_ioprio_get", __NR_ioprio_get}, {"__NR_inotify_init", __NR_inotify_init}, {"__NR_inotify_add_watch", __NR_inotify_add_watch}, {"__NR_inotify_rm_watch", __NR_inotify_rm_watch}, {"__NR_migrate_pages", __NR_migrate_pages}, {"__NR_openat", __NR_openat}, {"__NR_mkdirat", __NR_mkdirat}, {"__NR_mknodat", __NR_mknodat}, {"__NR_fchownat", __NR_fchownat}, {"__NR_futimesat", __NR_futimesat}, {"__NR_newfstatat", __NR_newfstatat}, {"__NR_unlinkat", __NR_unlinkat}, {"__NR_renameat", __NR_renameat}, {"__NR_linkat", __NR_linkat}, {"__NR_symlinkat", __NR_symlinkat}, {"__NR_readlinkat", __NR_readlinkat}, {"__NR_fchmodat", __NR_fchmodat}, {"__NR_faccessat", __NR_faccessat}, {"__NR_pselect6", __NR_pselect6}, {"__NR_ppoll", __NR_ppoll}, {"__NR_unshare", __NR_unshare}, {"__NR_set_robust_list", __NR_set_robust_list}, {"__NR_get_robust_list", __NR_get_robust_list}, {"__NR_splice", __NR_splice}, {"__NR_tee", __NR_tee}, {"__NR_sync_file_range", __NR_sync_file_range}, {"__NR_vmsplice", __NR_vmsplice}, {"__NR_move_pages", __NR_move_pages}, {"__NR_utimensat", __NR_utimensat}, {"__NR_epoll_pwait", __NR_epoll_pwait}, {"__NR_signalfd", __NR_signalfd}, {"__NR_timerfd_create", __NR_timerfd_create}, {"__NR_eventfd", __NR_eventfd}, {"__NR_fallocate", __NR_fallocate}, {"__NR_timerfd_settime", __NR_timerfd_settime}, {"__NR_timerfd_gettime", __NR_timerfd_gettime}, {"__NR_accept4", __NR_accept4}, {"__NR_signalfd4", __NR_signalfd4}, {"__NR_eventfd2", __NR_eventfd2}, {"__NR_epoll_create1", __NR_epoll_create1}, {"__NR_dup3", __NR_dup3}, {"__NR_pipe2", __NR_pipe2}, {"__NR_inotify_init1", __NR_inotify_init1}, {"__NR_preadv", __NR_preadv}, {"__NR_pwritev", __NR_pwritev}, {"__NR_rt_tgsigqueueinfo", __NR_rt_tgsigqueueinfo}, {"__NR_perf_event_open", __NR_perf_event_open}, {"__NR_recvmmsg", __NR_recvmmsg}, {"__NR_fanotify_init", __NR_fanotify_init}, {"__NR_fanotify_mark", __NR_fanotify_mark}, {"__NR_prlimit64", __NR_prlimit64}, {"__NR_name_to_handle_at", __NR_name_to_handle_at}, {"__NR_open_by_handle_at", __NR_open_by_handle_at}, {"__NR_clock_adjtime", __NR_clock_adjtime}, {"__NR_syncfs", __NR_syncfs}, {"__NR_sendmmsg", __NR_sendmmsg}, {"__NR_setns", __NR_setns}, {"__NR_getcpu", __NR_getcpu}, {"__NR_process_vm_readv", __NR_process_vm_readv}, {"__NR_process_vm_writev", __NR_process_vm_writev}, {"__NR_kcmp", __NR_kcmp}, {"__NR_finit_module", __NR_finit_module}, {"__NR_sched_setattr", __NR_sched_setattr}, {"__NR_sched_getattr", __NR_sched_getattr}, {"__NR_renameat2", __NR_renameat2}, {"__NR_seccomp", __NR_seccomp}, {"__NR_getrandom", __NR_getrandom}, {"__NR_memfd_create", __NR_memfd_create}, {"__NR_kexec_file_load", __NR_kexec_file_load}, {"__NR_bpf", __NR_bpf}, {"__NR_execveat", __NR_execveat}, {"__NR_userfaultfd", __NR_userfaultfd}, {"__NR_membarrier", __NR_membarrier}, {"__NR_mlock2", __NR_mlock2}, {"__NR_copy_file_range", __NR_copy_file_range}, {"__NR_preadv2", __NR_preadv2}, {"__NR_pwritev2", __NR_pwritev2}, {"__NR_pkey_mprotect", __NR_pkey_mprotect}, {"__NR_pkey_alloc", __NR_pkey_alloc}, {"__NR_pkey_free", __NR_pkey_free}, {"__NR_statx", __NR_statx}, {"__NR_io_pgetevents", __NR_io_pgetevents}, {"__NR_rseq", __NR_rseq}, {"__NR_pidfd_send_signal", __NR_pidfd_send_signal}, {"__NR_io_uring_setup", __NR_io_uring_setup}, {"__NR_io_uring_enter", __NR_io_uring_enter}, {"__NR_io_uring_register", __NR_io_uring_register}, {"__NR_open_tree", __NR_open_tree}, {"__NR_move_mount", __NR_move_mount}, {"__NR_fsopen", __NR_fsopen}, {"__NR_fsconfig", __NR_fsconfig}, {"__NR_fsmount", __NR_fsmount}, {"__NR_fspick", __NR_fspick}, {"__NR_pidfd_open", __NR_pidfd_open}, {"__NR_clone3", __NR_clone3}, {"__NR_close_range", __NR_close_range}, {"__NR_openat2", __NR_openat2}, {"__NR_pidfd_getfd", __NR_pidfd_getfd}, {"__NR_faccessat2", __NR_faccessat2}, {"__NR_process_madvise", __NR_process_madvise}, {"__NR_epoll_pwait2", __NR_epoll_pwait2}, {"__NR_mount_setattr", __NR_mount_setattr}, {"__NR_landlock_create_ruleset", __NR_landlock_create_ruleset}, {"__NR_landlock_add_rule", __NR_landlock_add_rule}, {"__NR_landlock_restrict_self", __NR_landlock_restrict_self}});
  30. SyscallNode * syscallsArray;
  31. int instructionsNumber = 0;
  32.  
  33. // A Treap Node
  34. struct TreapNode
  35. {
  36.     int number, priority;
  37.     TreapNode *left, *right;
  38. };
  39.  
  40. /* T1, T2 and T3 are subtrees of the tree rooted with y
  41.   (on left side) or x (on right side)
  42.                 y                               x
  43.                / \     Right Rotation          /  \
  44.               x   T3   – – – – – – – >        T1   y
  45.              / \       < - - - - - - -            / \
  46.             T1  T2     Left Rotation            T2  T3 */
  47.  
  48. // A utility function to right rotate subtree rooted with y
  49. // See the diagram given above.
  50. TreapNode *rightRotate(TreapNode *y)
  51. {
  52.     TreapNode *x = y->left,  *T2 = x->right;
  53.  
  54.     // Perform rotation
  55.     x->right = y;
  56.     y->left = T2;
  57.  
  58.     // Return new root
  59.     return x;
  60. }
  61.  
  62. // A utility function to left rotate subtree rooted with x
  63. // See the diagram given above.
  64. TreapNode *leftRotate(TreapNode *x)
  65. {
  66.     TreapNode *y = x->right, *T2 = y->left;
  67.  
  68.     // Perform rotation
  69.     y->left = x;
  70.     x->right = T2;
  71.  
  72.     // Return new root
  73.     return y;
  74. }
  75.  
  76. /* Utility function to add a new number */
  77. TreapNode* newNode(int priority, int number)
  78. {
  79.     TreapNode* temp = new TreapNode;
  80.     temp->number = number;
  81.     temp->priority = priority;
  82.     temp->left = temp->right = NULL;
  83.     return temp;
  84. }
  85.  
  86. /* Recursive implementation of insertion in Treap */
  87. TreapNode* insert(TreapNode* root, int priority, int number)
  88. {
  89.     // If root is NULL, create a new node and return it
  90.     if (!root)
  91.         return newNode(priority, number);
  92.  
  93.     // If number is smaller than root
  94.     if (number <= root->number)
  95.     {
  96.         // Insert in left subtree
  97.         root->left = insert(root->left, priority, number);
  98.  
  99.         // Fix Heap property if it is violated
  100.         if (root->left->priority > root->priority)
  101.             root = rightRotate(root);
  102.     }
  103.     else  // If number is greater
  104.     {
  105.         // Insert in right subtree
  106.         root->right = insert(root->right, priority, number);
  107.  
  108.         // Fix Heap property if it is violated
  109.         if (root->right->priority > root->priority)
  110.             root = leftRotate(root);
  111.     }
  112.     return root;
  113. }
  114.  
  115. void LevelOrder(TreapNode * root) {
  116.   static int index = 0;
  117.  
  118.   if (root == NULL) return;
  119.   queue < TreapNode * > q;
  120.   q.push(root);
  121.  
  122.   while (q.empty() == false) {
  123.     TreapNode * node = q.front();
  124.     //cout << node -> number <<endl;
  125.     q.pop();
  126.     bool hasChild = false;
  127.     if (node -> left != NULL){
  128.       q.push(node -> left);
  129.       syscallsArray[index].left = node -> left -> number;
  130.       hasChild = true;
  131.     }
  132.     if (node -> right != NULL){
  133.       q.push(node -> right);
  134.       syscallsArray[index].right = node -> right -> number;
  135.       hasChild = true;
  136.     }
  137.     syscallsArray[index].number = node -> number;
  138.     if (hasChild){
  139.       instructionsNumber++;
  140.     }
  141.     instructionsNumber++;
  142.     index++;
  143.   }
  144. }
  145.  
  146. bool comparison (SyscallStruct i, SyscallStruct j) {
  147.     return i.number<j.number;
  148. }
  149.  
  150. bool getAnswer(string question){
  151.     string answer;
  152.     while (true){
  153.         getline(cin, answer);
  154.         if (answer.empty()){
  155.             return true;
  156.         }
  157.         else if (answer.size()==1){
  158.             switch (answer[0]){
  159.             case 'Y':
  160.             case 'y':
  161.                 return true;
  162.                 break;
  163.             case 'N':
  164.             case 'n':
  165.                 return false;
  166.             default:
  167.                 cout<<question;
  168.             }
  169.         }
  170.         else{
  171.             cout<<question;
  172.         }
  173.     }
  174. }
  175.  
  176. // Driver Program to test above functions
  177. int main()
  178. {
  179.     struct TreapNode *root = NULL;
  180.  
  181.     string inputFilePath;
  182.     cout<<"Enter the path to the input file: ";
  183.     getline(cin, inputFilePath);
  184.     ifstream inputFile(inputFilePath);
  185.     if(!inputFile){
  186.         cout<<"Couldn't find input file."<<endl;
  187.         return 1;
  188.     }
  189.  
  190.     string outputFilePath;
  191.     cout<<"Enter the path to the output file: ";
  192.     getline(cin, outputFilePath);
  193.     ofstream outputFile(outputFilePath);
  194.     if(!outputFile){
  195.         cout<<"Output file couldn't be created."<<endl;
  196.         return 1;
  197.     }
  198.  
  199.     cout<<"Do you want to use whitelist mode? Y/n: ";
  200.     bool whiteListMode = getAnswer("Wrong answer. Try again... Do you want to use whitelist mode? Y/n: ");
  201.  
  202.     string currentSyscall;
  203.     int priority;
  204.     sort(syscalls.begin(),syscalls.end(), comparison);
  205.     int systemcallsSize = end(syscalls)-begin(syscalls);
  206.     int syscallsNumber = 0;
  207.     while (inputFile>>priority || !inputFile.eof()){
  208.         if (inputFile.fail()){
  209.             cout<<"Priority of the "<<++syscallsNumber<<" line isn't number"<< endl;
  210.             return 1;
  211.         }
  212.         inputFile>>currentSyscall;
  213.         if (inputFile.fail()){
  214.             cout<<"Wrong system call format in the "<<++syscallsNumber<<" line"<< endl;
  215.             return 1;
  216.         }
  217.         int i = 0;
  218.         for (; i < systemcallsSize; i++){
  219.             if ("__NR_"+currentSyscall==syscalls[i].name){
  220.                 root = insert(root, priority, syscalls[i].number);
  221.                 break;
  222.             }
  223.         }
  224.         if (i == systemcallsSize){
  225.             cout<<"Couldn't find "<<currentSyscall<<". Continue? Y/n: ";
  226.             if(! getAnswer("Wrong answer. Try again... Continue? Y/n: ")){
  227.                 return 1;
  228.             }
  229.         }
  230.         else{
  231.             syscallsNumber++;
  232.         }
  233.     }
  234.     inputFile.close();
  235.     syscallsArray = new SyscallNode[syscallsNumber];
  236.  
  237.     LevelOrder(root);
  238.     cout<<endl;
  239.  
  240.     outputFile<<"BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, arch))),"<<endl;
  241.     outputFile<<"BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, AUDIT_ARCH_X86_64, 0, "<<instructionsNumber+1<<"),"<<endl;
  242.     outputFile<<"BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, nr))),"<<endl;
  243.     int j;
  244.     int k;
  245.     for (int i = 0; i < syscallsNumber; i++){
  246.         int distance = 0;
  247.         if (syscallsArray[i].left!=-1 && syscallsArray[i].right!=-1){
  248.         j = i;
  249.             while (syscallsArray[i].left!=syscallsArray[++j].number){
  250.                 distance++;
  251.                 if (syscallsArray[j].right!=-1 || syscallsArray[j].left!=-1){
  252.                     distance++;
  253.                 }
  254.             }
  255.             k = syscallsArray[i].number+1>systemcallsSize?systemcallsSize:syscallsArray[i].number + 1;
  256.  
  257.             while (syscallsArray[i].number!=syscalls[--k].number);
  258.             outputFile<<"BPF_JUMP ( BPF_JMP | BPF_JGE | BPF_K, "<<syscalls[k].name<<", "<<0<<", "<<distance+1<<"),"<<endl;
  259.             instructionsNumber--;
  260.             do{
  261.                 distance++;
  262.                 if (syscallsArray[j].right!=-1 || syscallsArray[j].left!=-1){
  263.                     distance++;
  264.                 }
  265.             }
  266.             while (syscallsArray[i].right!=syscallsArray[++j].number);
  267.             outputFile<<"BPF_JUMP ( BPF_JMP | BPF_JEQ | BPF_K, "<<syscalls[k].name<<", "<<instructionsNumber<<", "<<distance<<"),"<<endl;
  268.             instructionsNumber--;
  269.         }
  270.         else if (syscallsArray[i].left!=-1 && syscallsArray[i].right==-1){
  271.             j = i;
  272.             while (syscallsArray[i].left!=syscallsArray[++j].number){
  273.                 distance++;
  274.                 if (syscallsArray[j].right!=-1 || syscallsArray[j].left!=-1){
  275.                     distance++;
  276.                 }
  277.             }
  278.             k = syscallsArray[i].number+1>systemcallsSize?systemcallsSize:syscallsArray[i].number + 1;
  279.  
  280.             while (syscallsArray[i].number!=syscalls[--k].number);
  281.             outputFile<<"BPF_JUMP ( BPF_JMP | BPF_JGE | BPF_K, "<<syscalls[k].name<<", "<<0<<", "<<distance+1<<"),"<<endl;
  282.             instructionsNumber--;
  283.             outputFile<<"BPF_JUMP ( BPF_JMP | BPF_JEQ | BPF_K, "<<syscalls[k].name<<", "<<instructionsNumber<<", "<<instructionsNumber-1<<"),"<<endl;
  284.             instructionsNumber--;
  285.         }
  286.         else if(syscallsArray[i].left==-1 && syscallsArray[i].right!=-1){
  287.             k = syscallsArray[i].number+1>systemcallsSize?systemcallsSize:syscallsArray[i].number + 1;
  288.  
  289.             while (syscallsArray[i].number!=syscalls[--k].number);
  290.             outputFile<<"BPF_JUMP ( BPF_JMP | BPF_JGE | BPF_K, "<<syscalls[k].name<<", "<<0<<", "<<instructionsNumber-1<<"),"<<endl;
  291.             instructionsNumber--;
  292.             j = i;
  293.             while (syscallsArray[i].right!=syscallsArray[++j].number){
  294.                 distance++;
  295.                 if (syscallsArray[j].right!=-1 || syscallsArray[j].left!=-1){
  296.                     distance++;
  297.                 }
  298.             }
  299.             outputFile<<"BPF_JUMP ( BPF_JMP | BPF_JEQ | BPF_K, "<<syscalls[k].name<<", "<<instructionsNumber<<", "<<distance<<"),"<<endl;
  300.             instructionsNumber--;
  301.         }
  302.         else{
  303.             k = syscallsArray[i].number+1>systemcallsSize?systemcallsSize:syscallsArray[i].number + 1;
  304.  
  305.             while (syscallsArray[i].number!=syscalls[--k].number);
  306.             outputFile<<"BPF_JUMP ( BPF_JMP | BPF_JEQ | BPF_K, "<<syscalls[k].name<<", "<<instructionsNumber<<", "<<instructionsNumber-1<<"),"<<endl;
  307.             instructionsNumber--;
  308.         }
  309.     }
  310.     if(whiteListMode){
  311.         outputFile<<"BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ERRNO | (EPERM & SECCOMP_RET_DATA)),"<<endl;
  312.         outputFile<<"BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW),"<<endl;
  313.     }
  314.     else{
  315.         outputFile<<"BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW),"<<endl;
  316.         outputFile<<"BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ERRNO | (EPERM & SECCOMP_RET_DATA)),"<<endl;
  317.     }
  318.     outputFile.close();
  319.  
  320.     delete[] syscallsArray;
  321.     syscallsArray = nullptr;
  322.  
  323.     cout<<"Seccomp filter created."<<endl;
  324.  
  325.     return 0;
  326. }
  327.  

Źródło:

Ostatnie wpisy

Linki

Funkcje