All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Kiszka <jan.kiszka@domain.hid>
To: Xenomai core <Xenomai-core@domain.hid>
Subject: [Xenomai-core] [PATCH 2/2] native: Fix error cleanup of rt_task_create
Date: Thu, 30 Jun 2011 11:36:29 +0200	[thread overview]
Message-ID: <4E0C439D.2030602@domain.hid> (raw)

When creating of a shadow task fails, rt_task_create has to free the
task object consistently, not only on registry errors. Then we need to
delete the core thread when fastlock allocation failed. Moreover, fix a
double free of the fastlock object which is now released via the delete
hook. Finally, avoid a use-after-release of the fastlock object in
__task_delete_hook.

This fixes heap corruptions when running out of resources.

Signed-off-by: Jan Kiszka <jan.kiszka@domain.hid>
---

Applies on top of xenomai-gch.git gch/u_mode

 ksrc/skins/native/syscall.c |    4 ++-
 ksrc/skins/native/task.c    |   44 ++++++++++++++++++++++++------------------
 2 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/ksrc/skins/native/syscall.c b/ksrc/skins/native/syscall.c
index 17071da..b21705a 100644
--- a/ksrc/skins/native/syscall.c
+++ b/ksrc/skins/native/syscall.c
@@ -184,8 +184,10 @@ static int __rt_task_create(struct pt_regs *regs)
 	   the platform does not support it. */
 
 	err = rt_task_create(task, name, 0, prio, XNFPU | XNSHADOW | mode);
-	if (err)
+	if (err) {
+		task = NULL;
 		goto fail;
+	}
 
 	/* Apply CPU affinity */
 	set_cpus_allowed(p, task->affinity);
diff --git a/ksrc/skins/native/task.c b/ksrc/skins/native/task.c
index af62d56..c6e9a22 100644
--- a/ksrc/skins/native/task.c
+++ b/ksrc/skins/native/task.c
@@ -74,16 +74,14 @@ static void __task_delete_hook(xnthread_t *thread)
 
 	task = thread2rtask(thread);
 
-#if defined(CONFIG_XENO_OPT_NATIVE_MPS) && defined(CONFIG_XENO_FASTSYNCH)
-	xnheap_free(&xnsys_ppd_get(0)->sem_heap,
-		    task->msendq.fastlock);
-#endif
-
 #ifdef CONFIG_XENO_OPT_NATIVE_MPS
 	/* The nucleus will reschedule as needed when all the deletion
 	   hooks are done. */
 	xnsynch_destroy(&task->mrecv);
 	xnsynch_destroy(&task->msendq);
+#ifdef CONFIG_XENO_FASTSYNCH
+	xnheap_free(&xnsys_ppd_get(0)->sem_heap, task->msendq.fastlock);
+#endif
 #endif /* CONFIG_XENO_OPT_NATIVE_MPS */
 
 	xnsynch_destroy(&task->safesynch);
@@ -265,11 +263,15 @@ int rt_task_create(RT_TASK *task,
 	xnflags_t bflags;
 	spl_t s;
 
-	if (prio < T_LOPRIO || prio > T_HIPRIO)
-		return -EINVAL;
+	if (prio < T_LOPRIO || prio > T_HIPRIO) {
+		err = -EINVAL;
+		goto fail;
+	}
 
-	if (xnpod_asynch_p())
-		return -EPERM;
+	if (xnpod_asynch_p()) {
+		err = -EPERM;
+		goto fail;
+	}
 
 	bflags = mode & (XNFPU | XNSHADOW | XNSUSP);
 
@@ -283,10 +285,10 @@ int rt_task_create(RT_TASK *task,
 	attr.stacksize = stksize;
 	param.rt.prio = prio;
 
-	if (xnpod_init_thread(&task->thread_base,
-			      &attr, &xnsched_class_rt, &param) != 0)
-		/* Assume this is the only possible failure. */
-		return -ENOMEM;
+	err = xnpod_init_thread(&task->thread_base, &attr, &xnsched_class_rt,
+				&param);
+	if (err)
+		goto fail;
 
 	inith(&task->link);
 	task->suspend_depth = (bflags & XNSUSP) ? 1 : 0;
@@ -310,8 +312,10 @@ int rt_task_create(RT_TASK *task,
 	/* Allocate lock memory for in-kernel use */
 	fastlock = xnheap_alloc(&xnsys_ppd_get(0)->sem_heap,
 				sizeof(*fastlock));
-	if (fastlock == NULL)
+	if (fastlock == NULL) {
+		xnpod_delete_thread(&task->thread_base);
 		return -ENOMEM;
+	}
 #endif /* CONFIG_XENO_FASTSYNCH */
 	xnsynch_init(&task->mrecv, XNSYNCH_FIFO, NULL);
 	/*
@@ -333,18 +337,20 @@ int rt_task_create(RT_TASK *task,
 	   half-baked objects... */
 
 	err = xnthread_register(&task->thread_base, name ? task->rname : "");
-	if (err) {
-#if defined(CONFIG_XENO_OPT_NATIVE_MPS) && defined(CONFIG_XENO_FASTSYNCH)
-		xnheap_free(&xnsys_ppd_get(0)->sem_heap, fastlock);
-#endif
+	if (err)
 		xnpod_delete_thread(&task->thread_base);
-	}
 #if defined(CONFIG_XENO_OPT_NATIVE_MPS) && defined(CONFIG_XENO_FASTSYNCH)
 	else
 		xnsynch_fast_acquire(fastlock, xnthread_handle(&task->thread_base));
 #endif
 
 	return err;
+
+      fail:
+	if (xnthread_test_state(&task->thread_base, XNSHADOW))
+		xnfree(task);
+
+	return err;
 }
 
 /**
-- 
1.7.1


             reply	other threads:[~2011-06-30  9:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-30  9:36 Jan Kiszka [this message]
2011-06-30 11:09 ` [Xenomai-core] [PATCH 2/2] native: Fix error cleanup of rt_task_create Gilles Chanteperdrix
2011-06-30 11:32   ` Jan Kiszka
2011-07-04 19:29     ` Gilles Chanteperdrix

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4E0C439D.2030602@domain.hid \
    --to=jan.kiszka@domain.hid \
    --cc=Xenomai-core@domain.hid \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.