Dodany przez: Seccomp BPF w postaci drzewa AVL, 21:24 21-07-2023

Nowy Pobierz
  1. /**< wstawianie elementów do Drzewa AVL za https://www.geeksforgeeks.org/insertion-in-an-avl-tree
  2. przechodzenie drzewa levelorder za https://www.interviewbit.com/blog/level-order-traversal/#1-c-implementation
  3. reszta moja
  4. */
  5.  
  6. #include <iostream>
  7. #include <fstream>
  8. #include <vector>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <sys/syscall.h>
  12.  
  13. using namespace std;
  14.  
  15. struct SyscallStruct{
  16.     string name;
  17.     int number;
  18. };
  19.  
  20. struct SyscallNode
  21. {
  22.     int number;
  23.     int left = -1;
  24.     int right = -1;
  25. };
  26.  
  27. 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}});
  28. SyscallNode * syscallsArray;
  29. int instructionsNumber = 0;
  30.  
  31. // An AVL tree node
  32. struct Node
  33. {
  34.     int number;
  35.     struct Node *left;
  36.     struct Node *right;
  37.     int height;
  38. };
  39.  
  40. // A utility function to get the height of the tree
  41. int height(struct Node *N)
  42. {
  43.     if (N == NULL)
  44.         return 0;
  45.     return N->height;
  46. }
  47.  
  48. // A utility function to get maximum of two integers
  49. int max(int a, int b)
  50. {
  51.     return (a > b)? a : b;
  52. }
  53.  
  54. /* Helper function that allocates a new node with the given number and
  55.     NULL left and right pointers. */
  56. struct Node* newNode(int number)
  57. {
  58.     struct Node* node = (struct Node*)
  59.                         malloc(sizeof(struct Node));
  60.     node->number   = number;
  61.     node->left   = NULL;
  62.     node->right  = NULL;
  63.     node->height = 1;  // new node is initially added at leaf
  64.     return(node);
  65. }
  66.  
  67. // A utility function to right rotate subtree rooted with y
  68. // See the diagram given above.
  69. struct Node *rightRotate(struct Node *y)
  70. {
  71.     struct Node *x = y->left;
  72.     struct Node *T2 = x->right;
  73.  
  74.     // Perform rotation
  75.     x->right = y;
  76.     y->left = T2;
  77.  
  78.     // Update heights
  79.     y->height = max(height(y->left),
  80.                     height(y->right)) + 1;
  81.     x->height = max(height(x->left),
  82.                     height(x->right)) + 1;
  83.  
  84.     // Return new root
  85.     return x;
  86. }
  87.  
  88. // A utility function to left rotate subtree rooted with x
  89. // See the diagram given above.
  90. struct Node *leftRotate(struct Node *x)
  91. {
  92.     struct Node *y = x->right;
  93.     struct Node *T2 = y->left;
  94.  
  95.     // Perform rotation
  96.     y->left = x;
  97.     x->right = T2;
  98.  
  99.     //  Update heights
  100.     x->height = max(height(x->left),
  101.                     height(x->right)) + 1;
  102.     y->height = max(height(y->left),
  103.                     height(y->right)) + 1;
  104.  
  105.     // Return new root
  106.     return y;
  107. }
  108.  
  109. // Get Balance factor of node N
  110. int getBalance(struct Node *N)
  111. {
  112.     if (N == NULL)
  113.         return 0;
  114.     return height(N->left) - height(N->right);
  115. }
  116.  
  117. // Recursive function to insert a number in the subtree rooted
  118. // with node and returns the new root of the subtree.
  119. struct Node* insert(struct Node* node, int number)
  120. {
  121.     /* 1.  Perform the normal BST insertion */
  122.     if (node == NULL)
  123.         return(newNode(number));
  124.  
  125.     if (number < node->number)
  126.         node->left  = insert(node->left, number);
  127.     else if (number > node->number)
  128.         node->right = insert(node->right, number);
  129.     else // Equal numbers are not allowed in BST
  130.         return node;
  131.  
  132.     /* 2. Update height of this ancestor node */
  133.     node->height = 1 + max(height(node->left),
  134.                         height(node->right));
  135.  
  136.     /* 3. Get the balance factor of this ancestor
  137.           node to check whether this node became
  138.           unbalanced */
  139.     int balance = getBalance(node);
  140.  
  141.     // If this node becomes unbalanced, then
  142.     // there are 4 cases
  143.  
  144.     // Left Left Case
  145.     if (balance > 1 && number < node->left->number)
  146.         return rightRotate(node);
  147.  
  148.     // Right Right Case
  149.     if (balance < -1 && number > node->right->number)
  150.         return leftRotate(node);
  151.  
  152.     // Left Right Case
  153.     if (balance > 1 && number > node->left->number)
  154.     {
  155.         node->left =  leftRotate(node->left);
  156.         return rightRotate(node);
  157.     }
  158.  
  159.     // Right Left Case
  160.     if (balance < -1 && number < node->right->number)
  161.     {
  162.         node->right = rightRotate(node->right);
  163.         return leftRotate(node);
  164.     }
  165.  
  166.     /* return the (unchanged) node pointer */
  167.     return node;
  168. }
  169.  
  170. int height1(struct Node* node)
  171. {
  172.     if (node == NULL)
  173.         return 0;
  174.     else {
  175.  
  176.         // Compute the height of each subtree
  177.         int lheight = height1(node->left);
  178.         int rheight = height1(node->right);
  179.  
  180.         // Use the larger one
  181.         if (lheight > rheight)
  182.             return (lheight + 1);
  183.         else
  184.             return (rheight + 1);
  185.     }
  186. }
  187.  
  188. void LevelOrder(Node * root) {
  189.   static int index = 0;
  190.  
  191.   if (root == NULL) return;
  192.   queue < Node * > q;
  193.   q.push(root);
  194.  
  195.   while (q.empty() == false) {
  196.     Node * node = q.front();
  197.     q.pop();
  198.     bool hasChild = false;
  199.     if (node -> left != NULL){
  200.       q.push(node -> left);
  201.       syscallsArray[index].left = node -> left -> number;
  202.       hasChild = true;
  203.     }
  204.     if (node -> right != NULL){
  205.       q.push(node -> right);
  206.       syscallsArray[index].right = node -> right -> number;
  207.       hasChild = true;
  208.     }
  209.     syscallsArray[index].number = node -> number;
  210.     if (hasChild){
  211.       instructionsNumber++;
  212.     }
  213.     instructionsNumber++;
  214.     index++;
  215.   }
  216. }
  217.  
  218. bool comparison (SyscallStruct i, SyscallStruct j)
  219. {
  220.     return i.number<j.number;
  221. }
  222.  
  223. bool getAnswer(string question){
  224.     string answer;
  225.     while (true){
  226.         getline(cin, answer);
  227.         if (answer.empty()){
  228.             return true;
  229.         }
  230.         else if (answer.size()==1){
  231.             switch (answer[0]){
  232.             case 'Y':
  233.             case 'y':
  234.                 return true;
  235.                 break;
  236.             case 'N':
  237.             case 'n':
  238.                 return false;
  239.             default:
  240.                 cout<<question;
  241.             }
  242.         }
  243.         else{
  244.             cout<<question;
  245.         }
  246.     }
  247. }
  248.  
  249. int main()
  250. {
  251.  
  252.   struct Node *root = NULL;
  253.  
  254.   string inputFilePath;
  255.   cout<<"Enter the path to the input file: ";
  256.   getline(cin, inputFilePath);
  257.   ifstream inputFile(inputFilePath);
  258.   if(!inputFile){
  259.     cout<<"Couldn't find input file."<<endl;
  260.     return 1;
  261.   }
  262.  
  263.   string outputFilePath;
  264.   cout<<"Enter the path to the output file: ";
  265.   getline(cin, outputFilePath);
  266.   ofstream outputFile(outputFilePath);
  267.   if(!outputFile){
  268.     cout<<"Output file couldn't be created."<<endl;
  269.     return 1;
  270.   }
  271.  
  272.   cout<<"Do you want to use whitelist mode? Y/n: ";
  273.   bool whiteListMode = getAnswer("Wrong answer. Try again... Do you want to use whitelist mode? Y/n: ");
  274.  
  275.   string currentSyscall;
  276.   sort(syscalls.begin(),syscalls.end(), comparison);
  277.   int systemcallsSize = end(syscalls)-begin(syscalls);
  278.   int syscallsNumber = 0;
  279.   while (getline(inputFile, currentSyscall)){
  280.     int i = 0;
  281.     for (; i < systemcallsSize; i++){
  282.         if ("__NR_"+currentSyscall==syscalls[i].name){
  283.             root = insert(root, syscalls[i].number);
  284.             break;
  285.         }
  286.     }
  287.     if (i == systemcallsSize){
  288.         cout<<"couldn't find "<<currentSyscall<<". Continue? Y/n: ";
  289.         if(! getAnswer("Wrong answer. Try again... Continue? Y/n: ")){
  290.             return 1;
  291.         }
  292.     }
  293.     else{
  294.         syscallsNumber++;
  295.     }
  296.   }
  297.   inputFile.close();
  298.   syscallsArray = new SyscallNode[syscallsNumber];
  299.  
  300.   LevelOrder(root);
  301.  
  302.   outputFile<<"BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, arch))),"<<endl;
  303.   outputFile<<"BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, AUDIT_ARCH_X86_64, 0, "<<instructionsNumber+1<<"),"<<endl;
  304.   outputFile<<"BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, nr))),"<<endl;
  305.   int j;
  306.   int k;
  307.   for (int i = 0; i < syscallsNumber; i++){
  308.     int distance = 0;
  309.     if (syscallsArray[i].left!=-1 && syscallsArray[i].right!=-1){
  310.       j = i;
  311.       while (syscallsArray[i].left!=syscallsArray[++j].number){
  312.         distance++;
  313.         if (syscallsArray[j].right!=-1 || syscallsArray[j].left!=-1){
  314.           distance++;
  315.         }
  316.       }
  317.       k = syscallsArray[i].number+1>systemcallsSize?systemcallsSize:syscallsArray[i].number + 1;
  318.       while (syscallsArray[i].number!=syscalls[--k].number);
  319.       outputFile<<"BPF_JUMP ( BPF_JMP | BPF_JGE | BPF_K, "<<syscalls[k].name<<", "<<0<<", "<<distance+1<<"),"<<endl;
  320.       instructionsNumber--;
  321.       do{
  322.         distance++;
  323.         if (syscallsArray[j].right!=-1 || syscallsArray[j].left!=-1){
  324.           distance++;
  325.         }
  326.       }
  327.       while (syscallsArray[i].right!=syscallsArray[++j].number);
  328.       outputFile<<"BPF_JUMP ( BPF_JMP | BPF_JEQ | BPF_K, "<<syscalls[k].name<<", "<<instructionsNumber<<", "<<distance<<"),"<<endl;
  329.       instructionsNumber--;
  330.     }
  331.     else if (syscallsArray[i].left!=-1 && syscallsArray[i].right==-1){
  332.       j = i;
  333.       while (syscallsArray[i].left!=syscallsArray[++j].number){
  334.         distance++;
  335.         if (syscallsArray[j].right!=-1 || syscallsArray[j].left!=-1){
  336.           distance++;
  337.         }
  338.       }
  339.       k = syscallsArray[i].number+1>systemcallsSize?systemcallsSize:syscallsArray[i].number + 1;
  340.       while (syscallsArray[i].number!=syscalls[--k].number);
  341.       outputFile<<"BPF_JUMP ( BPF_JMP | BPF_JGE | BPF_K, "<<syscalls[k].name<<", "<<0<<", "<<distance+1<<"),"<<endl;
  342.       instructionsNumber--;
  343.       outputFile<<"BPF_JUMP ( BPF_JMP | BPF_JEQ | BPF_K, "<<syscalls[k].name<<", "<<instructionsNumber<<", "<<instructionsNumber-1<<"),"<<endl;
  344.       instructionsNumber--;
  345.     }
  346.     else if(syscallsArray[i].left==-1 && syscallsArray[i].right!=-1){
  347.       k = syscallsArray[i].number+1>systemcallsSize?systemcallsSize:syscallsArray[i].number + 1;
  348.       while (syscallsArray[i].number!=syscalls[--k].number);
  349.       outputFile<<"BPF_JUMP ( BPF_JMP | BPF_JGE | BPF_K, "<<syscalls[k].name<<", "<<0<<", "<<instructionsNumber-1<<"),"<<endl;
  350.       instructionsNumber--;
  351.       j = i;
  352.       while (syscallsArray[i].right!=syscallsArray[++j].number){
  353.         distance++;
  354.         if (syscallsArray[j].right!=-1 || syscallsArray[j].left!=-1){
  355.           distance++;
  356.         }
  357.       }
  358.       outputFile<<"BPF_JUMP ( BPF_JMP | BPF_JEQ | BPF_K, "<<syscalls[k].name<<", "<<instructionsNumber<<", "<<distance<<"),"<<endl;
  359.       instructionsNumber--;
  360.     }
  361.     else{
  362.       k = syscallsArray[i].number+1>systemcallsSize?systemcallsSize:syscallsArray[i].number + 1;
  363.       while (syscallsArray[i].number!=syscalls[--k].number);
  364.       outputFile<<"BPF_JUMP ( BPF_JMP | BPF_JEQ | BPF_K, "<<syscalls[k].name<<", "<<instructionsNumber<<", "<<instructionsNumber-1<<"),"<<endl;
  365.       instructionsNumber--;
  366.     }
  367.   }
  368.   if(whiteListMode){
  369.     outputFile<<"BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ERRNO | (EPERM & SECCOMP_RET_DATA)),"<<endl;
  370.     outputFile<<"BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW),"<<endl;
  371.   }
  372.   else{
  373.     outputFile<<"BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW),"<<endl;
  374.     outputFile<<"BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ERRNO | (EPERM & SECCOMP_RET_DATA)),"<<endl;
  375.   }
  376.  
  377.   outputFile.close();
  378.  
  379.   delete[] syscallsArray;
  380.   syscallsArray = nullptr;
  381.  
  382.   cout<<"Seccomp filter created."<<endl;
  383.   return 0;
  384. }
  385.  

Źródło:

Ostatnie wpisy

Linki

Funkcje