fix use after free bug in the trigger handling code
authorJohn Crispin <blogic@openwrt.org>
Tue, 3 Sep 2013 17:36:43 +0000 (19:36 +0200)
committerJohn Crispin <blogic@openwrt.org>
Tue, 3 Sep 2013 17:50:13 +0000 (19:50 +0200)
Signed-off-by: John Crispin <blogic@openwrt.org>
instance.c
service.c

index 45706ba9ce28f465856091e9f9d337be70acaec9..c25c85989bf2449178fedd15a2c99e954a2d39bd 100644 (file)
@@ -317,10 +317,14 @@ instance_config_parse(struct service_instance *in)
                return false;
 
        in->command = cur;
-       in->trigger = tb[INSTANCE_ATTR_TRIGGER];
 
-       if (in->trigger)
+       if (tb[INSTANCE_ATTR_TRIGGER]) {
+               in->trigger = malloc(blob_len(tb[INSTANCE_ATTR_TRIGGER]));
+               if (!in->trigger)
+                       return -1;
+               memcpy(in->trigger, tb[INSTANCE_ATTR_TRIGGER], blob_len(tb[INSTANCE_ATTR_TRIGGER]));
                trigger_add(in->trigger, in);
+       }
 
        if ((cur = tb[INSTANCE_ATTR_NICE])) {
                in->nice = (int8_t) blobmsg_get_u32(cur);
@@ -395,6 +399,7 @@ instance_free(struct service_instance *in)
        uloop_process_delete(&in->proc);
        uloop_timeout_cancel(&in->timeout);
        trigger_del(in);
+       free(in->trigger);
        instance_config_cleanup(in);
        free(in->config);
        free(in);
index e485c534bf151e8338bfc442211065d90688c7cf..158e096d638c1d4947b3f196e801144e9b0ff284 100644 (file)
--- a/service.c
+++ b/service.c
@@ -101,11 +101,17 @@ service_update(struct service *s, struct blob_attr *config, struct blob_attr **t
        struct blob_attr *cur;
        int rem;
 
-       if (s->trigger)
+       if (s->trigger) {
                trigger_del(s);
+               free(s->trigger);
+               s->trigger = NULL;
+       }
 
        if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
-               s->trigger = tb[SERVICE_SET_TRIGGER];
+               s->trigger = malloc(blob_len(tb[SERVICE_SET_TRIGGER]));
+               if (!s->trigger)
+                       return -1;
+               memcpy(s->trigger, tb[SERVICE_SET_TRIGGER], blob_len(tb[SERVICE_SET_TRIGGER]));
                trigger_add(s->trigger, s);
        }
 
@@ -128,6 +134,8 @@ service_delete(struct service *s)
        vlist_flush_all(&s->instances);
        avl_delete(&services, &s->avl);
        trigger_del(s);
+       s->trigger = NULL;
+       free(s->trigger);
        free(s->config);
        free(s);
 }