syscalltbl.c 4.4 KB

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