Skip to content

Commit fdc3c44

Browse files
anchaoxiaoxiang781216
authored andcommitted
sched/group: fix task info heap-use-after-free
tg_info is still in use after task_uninit_info(), unifies lib_stream_* with life cycle of task info to avoid this issue. | ==1940861==ERROR: AddressSanitizer: heap-use-after-free on address 0xf47032e0 at pc 0x5676dc4f bp 0xf2f38c68 sp 0xf2f38c58 | |#10 0xf7abec89 in __asan::__asan_report_load2 (addr=4100993760) at ../../../../src/libsanitizer/asan/asan_rtl.cpp:119 |#11 0x5677356a in nxsem_destroy (sem=0xf47032e0) at semaphore/sem_destroy.c:73 |#12 0x56773695 in sem_destroy (sem=0xf47032e0) at semaphore/sem_destroy.c:120 |#13 0x5676faa2 in nxmutex_destroy (mutex=0xf47032e0) at include/nuttx/mutex.h:126 |#14 0x567a3430 in lib_stream_release (group=0xf4901ba0) at stdio/lib_libstream.c:98 |#15 0x5676da75 in group_release (group=0xf4901ba0) at group/group_leave.c:162 |#16 0x5676e51c in group_leave (tcb=0xf5377740) at group/group_leave.c:360 |#17 0x569fe79b in nxtask_exithook (tcb=0xf5377740, status=0) at task/task_exithook.c:455 |#18 0x569f90b9 in _exit (status=0) at task/exit.c:82 |#19 0x56742680 in exit (status=0) at stdlib/lib_exit.c:61 |#20 0x56a69c78 in iperf_showusage (progname=0xf2f28838 "iperf", exitcode=0) at iperf_main.c:91 |#21 0x56a6a6ec in iperf_main (argc=1, argv=0xf2f28830) at iperf_main.c:140 |#22 0x5679c148 in nxtask_startup (entrypt=0x56a69c78 <iperf_main>, argc=1, argv=0xf2f28830) at sched/task_startup.c:70 |#23 0x56767f58 in nxtask_start () at task/task_start.c:134 Signed-off-by: chao an <anchao@xiaomi.com>
1 parent 88dd705 commit fdc3c44

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

sched/group/group_create.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <nuttx/irq.h>
3333
#include <nuttx/fs/fs.h>
3434
#include <nuttx/kmalloc.h>
35-
#include <nuttx/lib/lib.h>
3635
#include <nuttx/semaphore.h>
3736
#include <nuttx/sched.h>
3837

@@ -165,14 +164,6 @@ int group_allocate(FAR struct task_tcb_s *tcb, uint8_t ttype)
165164
group->tg_mxmembers = GROUP_INITIAL_MEMBERS;
166165
#endif
167166

168-
/* Alloc task info for group */
169-
170-
ret = task_init_info(group);
171-
if (ret < 0)
172-
{
173-
goto errout_with_member;
174-
}
175-
176167
/* Attach the group to the TCB */
177168

178169
tcb->cmn.group = group;
@@ -185,11 +176,13 @@ int group_allocate(FAR struct task_tcb_s *tcb, uint8_t ttype)
185176

186177
files_initlist(&group->tg_filelist);
187178

188-
#ifdef CONFIG_FILE_STREAM
189-
/* Initialize file streams for the task group */
179+
/* Alloc task info for group */
190180

191-
lib_stream_initialize(group);
192-
#endif
181+
ret = task_init_info(group);
182+
if (ret < 0)
183+
{
184+
goto errout_with_member;
185+
}
193186

194187
#ifndef CONFIG_DISABLE_PTHREAD
195188
/* Initialize the pthread join mutex */

sched/group/group_leave.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <nuttx/irq.h>
3333
#include <nuttx/fs/fs.h>
3434
#include <nuttx/net/net.h>
35-
#include <nuttx/lib/lib.h>
3635
#include <nuttx/sched.h>
3736

3837
#ifdef CONFIG_BINFMT_LOADABLE
@@ -156,12 +155,6 @@ static inline void group_release(FAR struct task_group_s *group)
156155
pthread_release(group);
157156
#endif
158157

159-
#ifdef CONFIG_FILE_STREAM
160-
/* Free resource held by the stream list */
161-
162-
lib_stream_release(group);
163-
#endif /* CONFIG_FILE_STREAM */
164-
165158
/* Free all file-related resources now. We really need to close files as
166159
* soon as possible while we still have a functioning task.
167160
*/

sched/tls/task_initinfo.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <nuttx/kmalloc.h>
2828
#include <nuttx/mutex.h>
29+
#include <nuttx/lib/lib.h>
2930

3031
#include "tls.h"
3132

@@ -64,5 +65,11 @@ int task_init_info(FAR struct task_group_s *group)
6465
nxmutex_init(&info->ta_lock);
6566
group->tg_info = info;
6667

68+
#ifdef CONFIG_FILE_STREAM
69+
/* Initialize file streams for the task group */
70+
71+
lib_stream_initialize(group);
72+
#endif
73+
6774
return OK;
6875
}

sched/tls/task_uninitinfo.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <nuttx/kmalloc.h>
2626
#include <nuttx/mutex.h>
27+
#include <nuttx/lib/lib.h>
2728

2829
#include "tls.h"
2930

@@ -49,6 +50,12 @@ void task_uninit_info(FAR struct task_group_s *group)
4950
{
5051
FAR struct task_info_s *info = group->tg_info;
5152

53+
#ifdef CONFIG_FILE_STREAM
54+
/* Free resource held by the stream list */
55+
56+
lib_stream_release(group);
57+
#endif /* CONFIG_FILE_STREAM */
58+
5259
nxmutex_destroy(&info->ta_lock);
5360
group_free(group, info);
5461
}

0 commit comments

Comments
 (0)