syscalltbl.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * System call table mapper
  4. *
  5. * (C) 2016 Arnaldo Carvalho de Melo <acme@redhat.com>
  6. */
  7. #include "syscalltbl.h"
  8. #include <stdlib.h>
  9. #include <linux/compiler.h>
  10. #include <linux/zalloc.h>
  11. #ifdef HAVE_SYSCALL_TABLE_SUPPORT
  12. #include <string.h>
  13. #include "string2.h"
  14. #if defined(__x86_64__)
  15. #include <asm/syscalls_64.c>
  16. const int syscalltbl_native_max_id = SYSCALLTBL_x86_64_MAX_ID;
  17. static const char **syscalltbl_native = syscalltbl_x86_64;
  18. #elif defined(__s390x__)
  19. #include <asm/syscalls_64.c>
  20. const int syscalltbl_native_max_id = SYSCALLTBL_S390_64_MAX_ID;
  21. static const char **syscalltbl_native = syscalltbl_s390_64;
  22. #elif defined(__powerpc64__)
  23. #include <asm/syscalls_64.c>
  24. const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_64_MAX_ID;
  25. static const char **syscalltbl_native = syscalltbl_powerpc_64;
  26. #elif defined(__powerpc__)
  27. #include <asm/syscalls_32.c>
  28. const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_32_MAX_ID;
  29. static const char **syscalltbl_native = syscalltbl_powerpc_32;
  30. #elif defined(__aarch64__)
  31. #include <asm/syscalls.c>
  32. const int syscalltbl_native_max_id = SYSCALLTBL_ARM64_MAX_ID;
  33. static const char **syscalltbl_native = syscalltbl_arm64;
  34. #endif
  35. struct syscall {
  36. int id;
  37. const char *name;
  38. };
  39. static int syscallcmpname(const void *vkey, const void *ventry)
  40. {
  41. const char *key = vkey;
  42. const struct syscall *entry = ventry;
  43. return strcmp(key, entry->name);
  44. }
  45. static int syscallcmp(const void *va, const void *vb)
  46. {
  47. const struct syscall *a = va, *b = vb;
  48. return strcmp(a->name, b->name);
  49. }
  50. static int syscalltbl__init_native(struct syscalltbl *tbl)
  51. {
  52. int nr_entries = 0, i, j;
  53. struct syscall *entries;
  54. for (i = 0; i <= syscalltbl_native_max_id; ++i)
  55. if (syscalltbl_native[i])
  56. ++nr_entries;
  57. entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries);
  58. if (tbl->syscalls.entries == NULL)
  59. return -1;
  60. for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) {
  61. if (syscalltbl_native[i]) {
  62. entries[j].name = syscalltbl_native[i];
  63. entries[j].id = i;
  64. ++j;
  65. }
  66. }
  67. qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp);
  68. tbl->syscalls.nr_entries = nr_entries;
  69. tbl->syscalls.max_id = syscalltbl_native_max_id;
  70. return 0;
  71. }
  72. struct syscalltbl *syscalltbl__new(void)
  73. {
  74. struct syscalltbl *tbl = malloc(sizeof(*tbl));
  75. if (tbl) {
  76. if (syscalltbl__init_native(tbl)) {
  77. free(tbl);
  78. return NULL;
  79. }
  80. }
  81. return tbl;
  82. }
  83. void syscalltbl__delete(struct syscalltbl *tbl)
  84. {
  85. zfree(&tbl->syscalls.entries);
  86. free(tbl);
  87. }
  88. const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id)
  89. {
  90. return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL;
  91. }
  92. int syscalltbl__id(struct syscalltbl *tbl, const char *name)
  93. {
  94. struct syscall *sc = bsearch(name, tbl->syscalls.entries,
  95. tbl->syscalls.nr_entries, sizeof(*sc),
  96. syscallcmpname);
  97. return sc ? sc->id : -1;
  98. }
  99. int syscalltbl__strglobmatch_next(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
  100. {
  101. int i;
  102. struct syscall *syscalls = tbl->syscalls.entries;
  103. for (i = *idx + 1; i < tbl->syscalls.nr_entries; ++i) {
  104. if (strglobmatch(syscalls[i].name, syscall_glob)) {
  105. *idx = i;
  106. return syscalls[i].id;
  107. }
  108. }
  109. return -1;
  110. }
  111. int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
  112. {
  113. *idx = -1;
  114. return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
  115. }
  116. #else /* HAVE_SYSCALL_TABLE_SUPPORT */
  117. #include <libaudit.h>
  118. struct syscalltbl *syscalltbl__new(void)
  119. {
  120. struct syscalltbl *tbl = zalloc(sizeof(*tbl));
  121. if (tbl)
  122. tbl->audit_machine = audit_detect_machine();
  123. return tbl;
  124. }
  125. void syscalltbl__delete(struct syscalltbl *tbl)
  126. {
  127. free(tbl);
  128. }
  129. const char *syscalltbl__name(const struct syscalltbl *tbl, int id)
  130. {
  131. return audit_syscall_to_name(id, tbl->audit_machine);
  132. }
  133. int syscalltbl__id(struct syscalltbl *tbl, const char *name)
  134. {
  135. return audit_name_to_syscall(name, tbl->audit_machine);
  136. }
  137. int syscalltbl__strglobmatch_next(struct syscalltbl *tbl __maybe_unused,
  138. const char *syscall_glob __maybe_unused, int *idx __maybe_unused)
  139. {
  140. return -1;
  141. }
  142. int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
  143. {
  144. return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
  145. }
  146. #endif /* HAVE_SYSCALL_TABLE_SUPPORT */