From dd2d3d4ccd32193e96c57a225792a3e2b382736c Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 28 Dec 2020 17:23:33 +0000 Subject: [PATCH] fixed a potential issue by shared data access in mod-hawk.c --- hawk/lib/cli-imp.h | 2 +- hawk/lib/cli.c | 2 +- hawk/lib/fmt.c | 2 +- hawk/lib/hawk-cli.h | 2 +- hawk/lib/mod-hawk.c | 28 ++++++++++++++++++++++++++-- hawk/lib/std.c | 10 +++++----- 6 files changed, 35 insertions(+), 11 deletions(-) diff --git a/hawk/lib/cli-imp.h b/hawk/lib/cli-imp.h index bf7188b4..8e63fc8d 100644 --- a/hawk/lib/cli-imp.h +++ b/hawk/lib/cli-imp.h @@ -1,7 +1,7 @@ /* * $Id$ * - Copyright (c) 2014-2019 Chung, Hyung-Hwan. All rights reserved. + Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions diff --git a/hawk/lib/cli.c b/hawk/lib/cli.c index ab603cb5..43c2a56e 100644 --- a/hawk/lib/cli.c +++ b/hawk/lib/cli.c @@ -1,7 +1,7 @@ /* * $Id$ * - Copyright (c) 2014-2019 Chung, Hyung-Hwan. All rights reserved. + Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions diff --git a/hawk/lib/fmt.c b/hawk/lib/fmt.c index bbac54dc..adba322a 100644 --- a/hawk/lib/fmt.c +++ b/hawk/lib/fmt.c @@ -1,7 +1,7 @@ /* * $Id$ * - Copyright (c) 2014-2019 Chung, Hyung-Hwan. All rights reserved. + Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions diff --git a/hawk/lib/hawk-cli.h b/hawk/lib/hawk-cli.h index 53bdf41e..2d4b5dfc 100644 --- a/hawk/lib/hawk-cli.h +++ b/hawk/lib/hawk-cli.h @@ -1,7 +1,7 @@ /* * $Id$ * - Copyright (c) 2014-2019 Chung, Hyung-Hwan. All rights reserved. + Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions diff --git a/hawk/lib/mod-hawk.c b/hawk/lib/mod-hawk.c index 4decd0cd..f69e7307 100644 --- a/hawk/lib/mod-hawk.c +++ b/hawk/lib/mod-hawk.c @@ -26,7 +26,14 @@ #include "mod-hawk.h" #include "hawk-prv.h" +#include + +struct mod_data_t +{ + hawk_mtx_t mq_mtx; +}; +typedef struct mod_data_t mod_data_t; /* ----------------------------------------------------------------- */ /* @@ -150,7 +157,14 @@ static int fnc_function_exists (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) if (!rx) { hawk_mod_sym_t sym; + mod_data_t* md; + + md = (mod_data_t*)fi->mod->ctx; + /* hawk_query_module_with_name() may update some shared data under + * the hawk object. use a mutex for shared data safety */ + hawk_mtx_lock (&md->mq_mtx, HAWK_NULL); rx = (hawk_query_module_with_name(hawk_rtx_gethawk(rtx), &name, &sym) != HAWK_NULL); + hawk_mtx_unlock (&md->mq_mtx); } } hawk_rtx_freevaloocstr (rtx, a0, name.ptr); @@ -449,17 +463,27 @@ static void fini (hawk_mod_t* mod, hawk_rtx_t* rtx) static void unload (hawk_mod_t* mod, hawk_t* hawk) { - /* nothing to do */ + mod_data_t* md = (mod_data_t*)mod->ctx; + + hawk_mtx_fini (&md->mq_mtx); + hawk_freemem (hawk, md); } int hawk_mod_hawk (hawk_mod_t* mod, hawk_t* hawk) { + mod_data_t* md; + + md = hawk_allocmem(hawk, HAWK_SIZEOF(*md)); + if (HAWK_UNLIKELY(!md)) return -1; + + hawk_mtx_init (&md->mq_mtx, hawk_getgem(hawk)); + mod->query = query; mod->unload = unload; mod->init = init; mod->fini = fini; - mod->ctx = HAWK_NULL; + mod->ctx = md; return 0; } diff --git a/hawk/lib/std.c b/hawk/lib/std.c index 1d4e3918..ebe2b4ee 100644 --- a/hawk/lib/std.c +++ b/hawk/lib/std.c @@ -363,7 +363,7 @@ void* hawk_stdmodopen (hawk_t* hawk, const hawk_mod_spec_t* spec) else { void* h; - h = lt_dlopen(NULL); + h = lt_dlopen(HAWK_NULL); if (HAWK_UNLIKELY(!h)) hawk_seterrfmt (hawk, HAWK_NULL, HAWK_ESYSERR, HAWK_T("%hs"), lt_dlerror()); return h; } @@ -409,7 +409,7 @@ void* hawk_stdmodopen (hawk_t* hawk, const hawk_mod_spec_t* spec) else { HMODULE h; - h = GetModuleHandle(NULL); + h = GetModuleHandle(HAWK_NULL); if (!h) hawk_seterrnum (hawk, HAWK_NULL, hawk_syserr_to_errnum(GetLastError()); return h; } @@ -512,7 +512,7 @@ void* hawk_stdmodopen (hawk_t* hawk, const hawk_mod_spec_t* spec) else { void* h; - h = GetModuleHandle(NULL); + h = GetModuleHandle(HAWK_NULL); if (!h) hawk_seterrnum (hawk, HAWK_NULL, HAWK_ESYSERR); return h; } @@ -572,11 +572,11 @@ void hawk_stdmodclose (hawk_t* hawk, void* handle) #if defined(USE_LTDL) lt_dlclose (handle); #elif defined(_WIN32) - if (handle != GetModuleHandle(NULL)) FreeLibrary ((HMODULE)handle); + if (handle != GetModuleHandle(HAWK_NULL)) FreeLibrary ((HMODULE)handle); #elif defined(__OS2__) DosFreeModule ((HMODULE)handle); #elif defined(__DOS__) && defined(HAWK_ENABLE_DOS_DYNAMIC_MODULE) - if (handle != GetModuleHandle(NULL)) FreeModule (handle); + if (handle != GetModuleHandle(HAWK_NULL)) FreeModule (handle); #elif defined(USE_DLFCN) dlclose (handle); #else