summaryrefslogtreecommitdiff
path: root/common_modules/module_manager/src
diff options
context:
space:
mode:
Diffstat (limited to 'common_modules/module_manager/src')
-rw-r--r--common_modules/module_manager/src/txm_module_manager_object_deallocate.c41
-rw-r--r--common_modules/module_manager/src/txm_module_manager_util.c110
2 files changed, 141 insertions, 10 deletions
diff --git a/common_modules/module_manager/src/txm_module_manager_object_deallocate.c b/common_modules/module_manager/src/txm_module_manager_object_deallocate.c
index 43d96f3c..dd4c105d 100644
--- a/common_modules/module_manager/src/txm_module_manager_object_deallocate.c
+++ b/common_modules/module_manager/src/txm_module_manager_object_deallocate.c
@@ -44,9 +44,7 @@
/* DEPRECATED. This function is called internally by the Module */
/* Manager dispatch layer after a successful tx_*_delete() call from */
/* a module. It must not be called directly by module or application */
-/* code. Calling it on a live kernel object (one whose tx_*_delete() */
-/* has not yet been called) frees the backing pool memory while the */
-/* object is still referenced by the kernel (use-after-free). */
+/* code. */
/* */
/* Module authors: remove any explicit call to */
/* txm_module_object_deallocate(). Calling the appropriate */
@@ -59,6 +57,13 @@
/* header from in front of the address the caller supplied. An */
/* address that names none of this module's allocations returns */
/* TX_PTR_ERROR and is not dereferenced. */
+/* Memory holding a live kernel object is not given back. Such a */
+/* request returns TX_DELETE_ERROR and changes nothing: the object */
+/* stays created, the allocation stays on the module's allocation */
+/* list, and the memory stays owned by the module. Storage that was */
+/* allocated but never made into an object is still released, so a */
+/* module can still clean up after a create that failed or was */
+/* abandoned. */
/* */
/* INPUT */
/* */
@@ -74,6 +79,7 @@
/* _txe_mutex_put Release module instance mutex */
/* _txm_module_manager_allocated_object_find */
/* Find the module's allocation */
+/* _txm_module_manager_live_object_check Check for a live object */
/* _txe_byte_release Release object back to pool */
/* */
/* */
@@ -130,6 +136,35 @@ UINT return_value;
/* Set return value to invalid pointer. */
return_value = TX_PTR_ERROR;
}
+
+ /* Determine if a live kernel object is in this memory.
+
+ Releasing the memory of a created object leaves the kernel holding the
+ only references to it. The object stays on the created list for its
+ type, a thread stays on the ready or suspension list it was on and stays
+ schedulable, and an active timer stays on the timer list. None of those
+ consult a control block ID, so nothing about the freed memory stops the
+ kernel using it: a created list walk reads a name pointer out of it, a
+ create or delete of another object of the same type writes through the
+ links in it, the scheduler restores a context from the stack pointer in
+ it, and timer expiration calls the function pointer in it. The byte pool
+ is meanwhile free to hand those bytes to the next allocation, so what the
+ kernel goes on reading as a control block is whatever the next owner of
+ the memory puts there.
+
+ This is asked before the allocation is unlinked, so a refusal leaves the
+ allocation list, the object and the memory exactly as they were. What is
+ refused is releasing the memory of an object that is still created; an
+ allocation that was never made into an object, or one whose object has
+ been deleted, is released as before, which is what keeps cleanup after a
+ failed or abandoned create working and what makes the release the delete
+ dispatchers perform after a successful delete go through. */
+ else if (_txm_module_manager_live_object_check((ALIGN_TYPE) object_ptr) == TX_TRUE)
+ {
+
+ /* Set return value to indicate the object must be deleted first. */
+ return_value = TX_DELETE_ERROR;
+ }
else
{
diff --git a/common_modules/module_manager/src/txm_module_manager_util.c b/common_modules/module_manager/src/txm_module_manager_util.c
index ba7e5a29..756120fa 100644
--- a/common_modules/module_manager/src/txm_module_manager_util.c
+++ b/common_modules/module_manager/src/txm_module_manager_util.c
@@ -1021,13 +1021,18 @@ UINT exact_object;
/* This function determines whether an address is the exact address */
/* of an object on the kernel's created list for a module object type. */
/* */
-/* This is how an object the application created and shared with a */
-/* module is authenticated. Such an object is not in the manager's */
-/* object pool and the manager has no allocation record of it, so the */
-/* kernel's own created list is the only record of it that a module */
-/* cannot influence. Being on that list establishes at once that the */
-/* address is an object start, that the object is of this type, and */
-/* that it is created. */
+/* The created list is the record the create and delete services */
+/* maintain, so being on it establishes at once that the address is an */
+/* object start rather than an address inside an object, that the */
+/* object is of this type, and that it has not been deleted. It is */
+/* also the only record of an object the application created and */
+/* shared with a module, since the manager allocated no such object. */
+/* */
+/* Nothing the module can influence is consulted. In particular the */
+/* control block ID is not: a module can arrange for the value of an */
+/* ID to appear inside memory it owns, and distinct object types of */
+/* equal size exist, so an ID is not evidence that an object is there */
+/* nor of what type it is. */
/* */
/* INPUT */
/* */
@@ -1045,6 +1050,7 @@ UINT exact_object;
/* */
/* CALLED BY */
/* */
+/* _txm_module_manager_live_object_check Module object liveness check */
/* _txm_module_manager_param_check_typed_object_for_use */
/* Module object authentication */
/* */
@@ -1406,6 +1412,96 @@ UINT status;
/* */
/* FUNCTION RELEASE */
/* */
+/* _txm_module_manager_live_object_check PORTABLE C */
+/* 6.4.3 */
+/* AUTHOR */
+/* */
+/* Eclipse ThreadX contributors */
+/* */
+/* DESCRIPTION */
+/* */
+/* This function determines whether an address is the exact address */
+/* of a live kernel object of any type the Module Manager knows. */
+/* */
+/* It answers the question object deallocation has to ask before it */
+/* gives memory back: is the kernel still going to use these bytes as */
+/* a control block. Deallocation is told an address and nothing else, */
+/* so unlike the checks a typed service makes it cannot be given the */
+/* type to look for, and every type has to be looked for in turn. */
+/* */
+/* The eight created lists are searched whatever the size of the */
+/* allocation at the address. Skipping a type whose control block is */
+/* larger than the allocation would be sound only if every object had */
+/* been created through a size-checked path, and a module running */
+/* without memory protection creates objects without one. */
+/* */
+/* Each list is searched in its own interrupts-disabled window rather */
+/* than all of them in one, so the longest window is bounded by the */
+/* number of objects of a single type. The whole search costs one pass */
+/* over the objects the system has created, and is paid once per */
+/* object deallocation. */
+/* */
+/* INPUT */
+/* */
+/* object_ptr Address of object memory area */
+/* */
+/* OUTPUT */
+/* */
+/* TX_TRUE A live kernel object is there */
+/* TX_FALSE Anything else */
+/* */
+/* CALLS */
+/* */
+/* _txm_module_manager_created_object_type_check */
+/* Check one kernel created list */
+/* */
+/* CALLED BY */
+/* */
+/* _txm_module_manager_object_deallocate Deallocate object memory */
+/* */
+/* RELEASE HISTORY */
+/* */
+/* DATE NAME DESCRIPTION */
+/* */
+/* xx-xx-2026 Eclipse ThreadX Initial Version 6.4.3 */
+/* contributors */
+/* */
+/**************************************************************************/
+UINT _txm_module_manager_live_object_check(ALIGN_TYPE object_ptr)
+{
+
+UINT object_type;
+UINT status;
+
+
+ /* Assume no live object is there. */
+ status = TX_FALSE;
+
+ /* The eight module object types that have a kernel created list occupy a
+ contiguous range, so each is asked in turn by walking the range. A value in
+ the range that the per-type check does not recognise answers TX_FALSE, so a
+ type the manager stops knowing does not silently pass this search. */
+ for (object_type = ((UINT) TXM_BLOCK_POOL_OBJECT); object_type <= ((UINT) TXM_TIMER_OBJECT); object_type++)
+ {
+
+ if (_txm_module_manager_created_object_type_check(object_ptr, object_type) == TX_TRUE)
+ {
+
+ /* An address is the start of at most one object, so there is nothing
+ further to look for. */
+ status = TX_TRUE;
+ break;
+ }
+ }
+
+ return(status);
+}
+
+
+/**************************************************************************/
+/* */
+/* FUNCTION RELEASE */
+/* */
/* _txm_module_manager_util_code_allocation_size_and_alignment_get */
/* PORTABLE C */
/* 6.1 */