From 67ac0dde6e51dcf6d5436de61fc13af6960ac313 Mon Sep 17 00:00:00 2001 From: Christian Helmuth Date: Tue, 14 Mar 2017 13:19:02 +0100 Subject: [PATCH] libc: checks for initialization and user context We check if the libc (kernel singleton, config) was initialized and also if suspend() is called from the valid user context. Issue #2332 --- repos/libports/src/lib/libc/task.cc | 30 +++++++++++++++-------- repos/libports/src/lib/libc/vfs_plugin.cc | 4 +++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/repos/libports/src/lib/libc/task.cc b/repos/libports/src/lib/libc/task.cc index acf551db53..6052e7cfee 100644 --- a/repos/libports/src/lib/libc/task.cc +++ b/repos/libports/src/lib/libc/task.cc @@ -524,6 +524,13 @@ struct Libc::Kernel unsigned long _suspend_main(Suspend_functor &check, unsigned long timeout_ms) { + /* check if we're running on the user context */ + if (Thread::myself()->mystack().top != (Genode::addr_t)_user_stack) { + error("libc suspend() called from non-user context (", + __builtin_return_address(0), ") - aborting"); + exit(1); + } + if (!check.suspend()) return 0; @@ -774,9 +781,12 @@ static void resumed_callback() { kernel->entrypoint_resumed(); } void Libc::resume_all() { kernel->resume_all(); } -unsigned long Libc::suspend(Suspend_functor &s, - unsigned long timeout_ms) +unsigned long Libc::suspend(Suspend_functor &s, unsigned long timeout_ms) { + if (!kernel) { + error("libc kernel not initialized, needed for suspend()"); + exit(1); + } return kernel->suspend(s, timeout_ms); } @@ -789,8 +799,8 @@ unsigned long Libc::current_time() void Libc::schedule_suspend(void (*suspended) ()) { if (!kernel) { - error("libc kernel not initialized, needed for suspend()"); - return; + error("libc kernel not initialized, needed for fork()"); + exit(1); } kernel->schedule_suspend(suspended); } @@ -800,7 +810,7 @@ void Libc::schedule_select(Libc::Select_handler_base *h) { if (!kernel) { error("libc kernel not initialized, needed for select()"); - return; + exit(1); } kernel->schedule_select(h); } @@ -813,6 +823,11 @@ void Libc::execute_in_application_context(Libc::Application_code &app_code) * don't use this code. */ + if (!kernel) { + error("libc kernel not initialized, needed for with_libc()"); + exit(1); + } + static bool nested = false; if (nested) { @@ -825,11 +840,6 @@ void Libc::execute_in_application_context(Libc::Application_code &app_code) return; } - if (!kernel) { - error("libc kernel not initialized, needed for with_libc()"); - return; - } - nested = true; kernel->run(app_code); nested = false; diff --git a/repos/libports/src/lib/libc/vfs_plugin.cc b/repos/libports/src/lib/libc/vfs_plugin.cc index a63409b23e..9a3377c096 100644 --- a/repos/libports/src/lib/libc/vfs_plugin.cc +++ b/repos/libports/src/lib/libc/vfs_plugin.cc @@ -91,6 +91,10 @@ namespace Libc { Genode::Xml_node config() __attribute__((weak)); Genode::Xml_node config() { + if (!_config_node) { + error("libc config not initialized - aborting"); + exit(1); + } return *_config_node; }