Seccomp_test.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "benc/String.h"
  16. #include "util/log/FileWriterLog.h"
  17. #include "memory/Allocator.h"
  18. #include "util/Seccomp.h"
  19. #include "util/events/EventBase.h"
  20. #include "util/events/Process.h"
  21. #include "util/events/PipeServer.h"
  22. #include "util/events/Pipe.h"
  23. #include "util/events/Timeout.h"
  24. #include "util/CString.h"
  25. #include "crypto/random/Random.h"
  26. #include <unistd.h>
  27. struct Context
  28. {
  29. struct Iface iface;
  30. struct Allocator* alloc;
  31. struct EventBase* eventBase;
  32. Identity
  33. };
  34. static void childComplete(void* vEventBase)
  35. {
  36. EventBase_endLoop((struct EventBase*)vEventBase);
  37. }
  38. struct ChildCtx
  39. {
  40. struct EventBase* base;
  41. struct Log* log;
  42. struct Pipe* pipe;
  43. struct Allocator* alloc;
  44. Identity
  45. };
  46. static void onConnectionChild(struct Pipe* pipe, int status)
  47. {
  48. struct ChildCtx* child = Identity_check((struct ChildCtx*) pipe->userData);
  49. Er_assert(Seccomp_dropPermissions(child->alloc, child->log));
  50. Assert_true(Seccomp_isWorking());
  51. struct Message* ok = Message_new(0, 512, child->alloc);
  52. Er_assert(Message_epush(ok, "OK", 3));
  53. struct Iface iface = { .send = NULL };
  54. Iface_plumb(&pipe->iface, &iface);
  55. Iface_send(&iface, ok);
  56. // just set a timeout long enough that we're pretty sure the parent will get the message
  57. // before we quit.
  58. Timeout_setInterval(childComplete, child->base, 10, child->base, child->alloc);
  59. }
  60. static void timeout(void* vNULL)
  61. {
  62. Assert_true(!"timed out");
  63. }
  64. static void timeout2(void* vNULL)
  65. {
  66. Assert_true(!"time out 2");
  67. }
  68. static int child(char* pipeName, struct Allocator* alloc, struct Log* logger)
  69. {
  70. struct ChildCtx* ctx = Allocator_calloc(alloc, sizeof(struct ChildCtx), 1);
  71. ctx->base = EventBase_new(alloc);
  72. ctx->alloc = alloc;
  73. ctx->log = logger;
  74. ctx->pipe = Er_assert(Pipe_named(pipeName, ctx->base, logger, alloc));
  75. ctx->pipe->onConnection = onConnectionChild;
  76. ctx->pipe->userData = ctx;
  77. Identity_set(ctx);
  78. Timeout_setTimeout(timeout, ctx->base, 2000, ctx->base, alloc);
  79. EventBase_beginLoop(ctx->base);
  80. return 0;
  81. }
  82. static Iface_DEFUN receiveMessageParent(struct Message* msg, struct Iface* iface)
  83. {
  84. struct Context* ctx = Identity_check((struct Context*) iface);
  85. // PipeServer pushes a uint32 identifier of the client who sent the message
  86. Er_assert(AddrIface_popAddr(msg));
  87. Assert_true(Message_getLength(msg) == 3);
  88. Assert_true(!Bits_memcmp(msg->msgbytes, "OK", 3));
  89. EventBase_endLoop(ctx->eventBase);
  90. return NULL;
  91. }
  92. int main(int argc, char** argv)
  93. {
  94. struct Allocator* alloc = Allocator_new(20000);
  95. struct Log* logger = FileWriterLog_new(stdout, alloc);
  96. if (!Seccomp_exists()) {
  97. Log_debug(logger, "Seccomp not supported on this system");
  98. return 0;
  99. }
  100. if (argc > 3 && !CString_strcmp("Seccomp_test", argv[1]) && !CString_strcmp("child", argv[2])) {
  101. child(argv[3], alloc, logger);
  102. // Allocator_free(alloc);
  103. // TODO: Freeing the allocator causes a Identity_check() assertion crash in Pipe.c, replace after async allocator free is abolished.
  104. return 0;
  105. }
  106. struct EventBase* eb = EventBase_new(alloc);
  107. struct Random* rand = Random_new(alloc, logger, NULL);
  108. char randName[32] = {0};
  109. Random_base32(rand, (uint8_t*)randName, 31);
  110. String* name = String_printf(alloc, "%s%scjdns-test-%s", Pipe_PATH, Pipe_PATH_SEP, randName);
  111. struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
  112. Identity_set(ctx);
  113. ctx->alloc = alloc;
  114. ctx->iface.send = receiveMessageParent;
  115. ctx->eventBase = eb;
  116. struct PipeServer* pipe = PipeServer_named(name->bytes, eb, NULL, logger, alloc);
  117. Iface_plumb(&ctx->iface, pipe->iface.iface);
  118. const char* path = Process_getPath(alloc);
  119. const char* args[] = { "Seccomp_test", "child", name->bytes, NULL };
  120. Assert_true(!Process_spawn(path, args, eb, alloc, NULL));
  121. Timeout_setTimeout(timeout2, NULL, 2000, eb, alloc);
  122. EventBase_beginLoop(eb);
  123. unlink(name->bytes);
  124. return 0;
  125. }